Beispiel #1
0
func (s *DepthFirstSearchSuite) TestToposort(c *C) {
	g := gogl.Spec().Directed().
		Mutable().Using(dfArcSet).
		Create(al.G).(gogl.Digraph)

	tsl, err := Toposort(g, "foo")
	c.Assert(err, IsNil)
	c.Assert(tsl, DeepEquals, []gogl.Vertex{"qux", "baz", "bar", "foo"})

	// add a cycle, ensure error comes back
	g.(gogl.MutableDigraph).AddArcs(gogl.NewArc("bar", "foo"))
	tsl, err = Toposort(g, "foo")
	c.Assert(err, ErrorMatches, "Cycle detected in graph")

	// undirected
	ug := gogl.Spec().Using(dfEdgeSet).Create(al.G)

	_, err = Toposort(ug)
	c.Assert(err, ErrorMatches, ".*do not have sources.*")

	tsl, err = Toposort(ug, "foo")
	// no such thing as a 'cycle' (of that kind) in undirected graphs
	c.Assert(err, IsNil)
	c.Assert(tsl, DeepEquals, []gogl.Vertex{"qux", "baz", "bar", "foo"})
}
Beispiel #2
0
func (v *TestVisitor) TestTraverse(c *C) {
	v.c = c

	el := gogl.ArcList{
		gogl.NewArc("foo", "bar"),
		gogl.NewArc("bar", "baz"),
		gogl.NewArc("bar", "foo"),
		gogl.NewArc("bar", "quark"),
		gogl.NewArc("baz", "qux"),
	}
	g := gogl.Spec().Directed().
		Mutable().Using(el).
		Create(al.G).(gogl.Digraph)

	v.vertices = []string{"foo", "bar", "baz", "qux", "quark"}

	v.colors = make(map[string]int)
	for _, vtx := range v.vertices {
		v.colors[vtx] = white
	}

	v.found_edges = make([]gogl.Arc, 0)

	Traverse(g, v, "foo")

	for vertex, color := range v.colors {
		c.Log("Checking that vertex '", vertex, "' has been finished")
		c.Assert(color, Equals, black)
	}

	for _, e := range el {
		c.Assert(v.found_edges, Contains, e)
	}
	c.Assert(len(v.found_edges), Equals, len(el))
}
Beispiel #3
0
func (s *DepthFirstSearchSuite) TestSearchVertexVerification(c *C) {
	g := gogl.Spec().Mutable().Directed().
		Create(al.G).(gogl.MutableDigraph)
	g.EnsureVertex("foo")

	_, err := Search(g.(gogl.Digraph), "foo", "bar")
	c.Assert(err, ErrorMatches, "Start vertex.*")
	_, err = Search(g.(gogl.Digraph), "bar", "foo")
	c.Assert(err, ErrorMatches, "Target vertex.*")
}
Beispiel #4
0
// Basic test of outermost search functionality.
func (s *DepthFirstSearchSuite) TestSearch(c *C) {
	// extra edge demonstrates non-productive search paths are not included
	extraSet := append(dfArcSet, gogl.NewArc("bar", "quark"))
	// directed
	g := gogl.Spec().Directed().Using(extraSet).
		Create(al.G).(gogl.Digraph)

	path, err := Search(g, "qux", "bar")
	c.Assert(path, DeepEquals, []gogl.Vertex{"qux", "baz", "bar"})
	c.Assert(err, IsNil)

	// undirected
	//ug := gogl.BuildGraph().Using(extraSet).Create(al.AdjacencyList)

	// TODO accidentally passed wrong graph - fix!
	path, err = Search(g, "qux", "bar")
	c.Assert(path, DeepEquals, []gogl.Vertex{"qux", "baz", "bar"})
	c.Assert(err, IsNil)
}
Beispiel #5
0
func (s *DepthFirstSearchSuite) TestFindSources(c *C) {
	g := gogl.Spec().Directed().
		Mutable().Using(dfArcSet).
		Create(al.G).(gogl.Digraph)

	sources, err := FindSources(g)
	c.Assert(fmt.Sprint(sources), Equals, fmt.Sprint([]gogl.Vertex{"foo"}))
	c.Assert(err, IsNil)

	// Ensure it finds multiple, as well
	g.(gogl.MutableDigraph).AddArcs(gogl.NewArc("quark", "baz"))
	sources, err = FindSources(g)

	possibles := [][]gogl.Vertex{
		{"foo", "quark"},
		{"quark", "foo"},
	}
	c.Assert(possibles, Contains, sources)
	c.Assert(err, IsNil)
}