Beispiel #1
0
func TestAStar(t *testing.T) {
	for _, test := range aStarTests {
		pt, _ := AStar(simple.Node(test.s), simple.Node(test.t), test.g, test.heuristic)

		p, cost := pt.To(simple.Node(test.t))

		if !topo.IsPathIn(test.g, p) {
			t.Error("got path that is not path in input graph for %q", test.name)
		}

		bfp, ok := BellmanFordFrom(simple.Node(test.s), test.g)
		if !ok {
			t.Fatalf("unexpected negative cycle in %q", test.name)
		}
		if want := bfp.WeightTo(simple.Node(test.t)); cost != want {
			t.Errorf("unexpected cost for %q: got:%v want:%v", test.name, cost, want)
		}

		var got = make([]int, 0, len(p))
		for _, n := range p {
			got = append(got, n.ID())
		}
		if test.wantPath != nil && !reflect.DeepEqual(got, test.wantPath) {
			t.Errorf("unexpected result for %q:\ngot: %v\nwant:%v", test.name, got, test.wantPath)
		}
	}
}
Beispiel #2
0
func TestIsPath(t *testing.T) {
	dg := concrete.NewDirectedGraph()
	if !topo.IsPathIn(dg, nil) {
		t.Error("IsPath returns false on nil path")
	}
	p := []graph.Node{concrete.Node(0)}
	if topo.IsPathIn(dg, p) {
		t.Error("IsPath returns true on nonexistant node")
	}
	dg.AddNode(p[0])
	if !topo.IsPathIn(dg, p) {
		t.Error("IsPath returns false on single-length path with existing node")
	}
	p = append(p, concrete.Node(1))
	dg.AddNode(p[1])
	if topo.IsPathIn(dg, p) {
		t.Error("IsPath returns true on bad path of length 2")
	}
	dg.SetEdge(concrete.Edge{p[0], p[1]}, 1)
	if !topo.IsPathIn(dg, p) {
		t.Error("IsPath returns false on correct path of length 2")
	}
	p[0], p[1] = p[1], p[0]
	if topo.IsPathIn(dg, p) {
		t.Error("IsPath erroneously returns true for a reverse path")
	}
	p = []graph.Node{p[1], p[0], concrete.Node(2)}
	dg.SetEdge(concrete.Edge{p[1], p[2]}, 1)
	if !topo.IsPathIn(dg, p) {
		t.Error("IsPath does not find a correct path for path > 2 nodes")
	}
	ug := concrete.NewGraph()
	ug.SetEdge(concrete.Edge{p[1], p[0]}, 1)
	ug.SetEdge(concrete.Edge{p[1], p[2]}, 1)
	if !topo.IsPathIn(dg, p) {
		t.Error("IsPath does not correctly account for undirected behavior")
	}
}
Beispiel #3
0
func main() {
	// graph G {
	G := NewGraphNode(0)
	// e
	e := NewGraphNode(1)

	// subgraph clusterA {
	clusterA := NewGraphNode(2)

	// a -- b
	a := NewGraphNode(3)
	b := NewGraphNode(4)
	a.AddNeighbor(b)
	b.AddNeighbor(a)
	clusterA.AddRoot(a)
	clusterA.AddRoot(b)

	// subgraph clusterC {
	clusterC := NewGraphNode(5)
	// C -- D
	C := NewGraphNode(6)
	D := NewGraphNode(7)
	C.AddNeighbor(D)
	D.AddNeighbor(C)

	clusterC.AddRoot(C)
	clusterC.AddRoot(D)
	// }
	clusterA.AddRoot(clusterC)
	// }

	// subgraph clusterB {
	clusterB := NewGraphNode(8)

	// d -- f
	d := NewGraphNode(9)
	f := NewGraphNode(10)
	d.AddNeighbor(f)
	f.AddNeighbor(d)
	clusterB.AddRoot(d)
	clusterB.AddRoot(f)
	// }

	// d -- D
	d.AddNeighbor(D)
	D.AddNeighbor(d)

	// e -- clusterB
	e.AddNeighbor(clusterB)
	clusterB.AddNeighbor(e)

	// clusterC -- clusterB
	clusterC.AddNeighbor(clusterB)
	clusterB.AddNeighbor(clusterC)

	G.AddRoot(e)
	G.AddRoot(clusterA)
	G.AddRoot(clusterB)
	// }

	if !topo.IsPathIn(G, []graph.Node{C, D, d, f}) {
		fmt.Println("Not working!")
	} else {
		fmt.Println("Working!")
	}
}