Exemple #1
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))
}
Exemple #2
0
func (g *stableBernoulliDigraph) Arcs(f gogl.ArcStep) {
	if g.list == nil {
		g.list = make([][]bool, g.order, g.order)

		// Wrapping edge step function; records edges into the adjacency list, then passes edge along
		ff := func(e gogl.Arc) bool {
			if g.list[e.Source().(int)] == nil {
				g.list[e.Source().(int)] = make([]bool, g.order, g.order)
			}
			g.list[e.Source().(int)][e.Target().(int)] = true
			g.size++
			return f(e)
		}

		bernoulliArcCreator(ff, int(g.order), g.ρ, g.trial)
	} else {
		var e gogl.Arc
		for u, adj := range g.list {
			for v, exists := range adj {
				if exists {
					e = gogl.NewArc(u, v)
					if f(e) {
						return
					}
				}
			}
		}
	}
}
Exemple #3
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"})
}
Exemple #4
0
func bernoulliArcCreator(el gogl.ArcStep, order int, ρ float64, cmp bTrial) {
	var e gogl.Arc
	for u := 0; u < order; u++ {
		for v := 0; v < order; v++ {
			if u != v && cmp(ρ) {
				e = gogl.NewArc(u, v)
				if el(e) {
					return
				}
			}
		}
	}
}
Exemple #5
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)
}
Exemple #6
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)
}
Exemple #7
0
	. "github.com/sdboyer/gocheck"
	"github.com/sdboyer/gogl"
	"github.com/sdboyer/gogl/graph/al"
)

// Hook gocheck into the go test runner
func Test(t *testing.T) { TestingT(t) }

var dfEdgeSet = gogl.EdgeList{
	gogl.NewEdge("foo", "bar"),
	gogl.NewEdge("bar", "baz"),
	gogl.NewEdge("baz", "qux"),
}

var dfArcSet = gogl.ArcList{
	gogl.NewArc("foo", "bar"),
	gogl.NewArc("bar", "baz"),
	gogl.NewArc("baz", "qux"),
}

type DepthFirstSearchSuite struct{}

var _ = Suite(&DepthFirstSearchSuite{})

// 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)