Ejemplo n.º 1
0
func TestFindConnectedComponentsDifferentComponents(t *testing.T) {
	g := New()

	vertices := new([100]*Vertex)

	for i := 0; i < 100; i++ {
		vertices[i] = g.AddVertex()
	}

	for i := 0; i < 49; i++ {
		v1 := vertices[i]
		v2 := vertices[i+1]
		g.ConnectVertices(v1, v2)
	}

	for i := 51; i < 99; i++ {
		v1 := vertices[i]
		v2 := vertices[i+1]
		g.ConnectVertices(v1, v2)
	}

	result := g.FindConnectedComponents()

	v1 := vertices[49]
	v2 := vertices[51]

	if disjoint.Find(result[v1.Identifier()]) == disjoint.Find(result[v2.Identifier()]) {
		t.Errorf("Two vertices were unexpectedly in the same connected component")
	}

}
Ejemplo n.º 2
0
func TestFindConnectedComponents(t *testing.T) {
	g := New()

	vertices := new([100]*Vertex)

	for i := 0; i < 100; i++ {
		vertices[i] = g.AddVertex()
	}

	for i := 0; i < 99; i++ {
		v1 := vertices[i]
		v2 := vertices[i+1]
		g.ConnectVertices(v1, v2)
	}

	result := g.FindConnectedComponents()

	for i := 0; i < 99; i++ {
		v1 := vertices[i]
		v2 := vertices[i+1]

		if disjoint.Find(result[v1.Identifier()]) != disjoint.Find(result[v2.Identifier()]) {
			t.Errorf("Two nodes were not in the same connected component despite being connected")
		}
	}
}