Пример #1
0
func TestBreadth(t *testing.T) {
	// Create a simple graph and check breadth visit order
	g := graph.New(6)
	g.Connect(0, 1)
	g.Connect(1, 2)
	g.Connect(1, 4)
	g.Connect(2, 3)
	g.Connect(4, 5)
	g.Connect(3, 5)

	// Check the bfs paths
	b := New(g, 0)
	if p := b.Path(5); len(p) != 4 || p[0] != 0 || p[1] != 1 || p[2] != 4 || p[3] != 5 {
		t.Errorf("path mismatch: have %v, want%v.", p, []int{0, 1, 4, 5})
	}
}
Пример #2
0
// Small API demo based on a trie graph and a few disconnected vertices.
func Example_usage() {
	// Create the graph
	g := graph.New(7)
	g.Connect(0, 1)
	g.Connect(1, 2)
	g.Connect(1, 4)
	g.Connect(2, 3)
	g.Connect(4, 5)

	// Create the breadth first search algo structure for g and source node #2
	b := bfs.New(g, 0)

	// Get the path between #0 (source) and #2
	fmt.Println("Path 0->5:", b.Path(5))
	fmt.Println("Order:", b.Order())
	fmt.Println("Reachable #4 #6:", b.Reachable(4), b.Reachable(6))

	// Output:
	// Path 0->5: [0 1 4 5]
	// Order: [0 1 2 4 3 5]
	// Reachable #4 #6: true false
}
Пример #3
0
// Creates a simple graph, and prints the degree of each vertex.
func Example_usage() {
	// Create a star shaped 5 vertex graph
	g := graph.New(5)
	g.Connect(0, 2)
	g.Connect(0, 3)
	g.Connect(1, 3)
	g.Connect(1, 4)
	g.Connect(2, 4)

	// For each vertex, count the outgoing edges
	for v := 0; v < g.Vertices(); v++ {
		degree := 0
		g.Do(v, func(peer interface{}) {
			degree++
		})
		fmt.Printf("%v: %v\n", v, degree)
	}
	// Output:
	// 0: 2
	// 1: 2
	// 2: 2
	// 3: 2
	// 4: 2
}
Пример #4
0
func TestBFS(t *testing.T) {
	for i, tt := range graphTests {
		// Assemble the graph
		g := graph.New(tt.nodes)
		for v, peers := range tt.edges {
			for _, peer := range peers {
				g.Connect(v, peer)
			}
		}
		// Create a bfs structure and verify it
		for src := 0; src < tt.nodes; src++ {
			b := New(g, src)

			// Ensure that paths are indeed connected links
			for dst := 0; dst < tt.nodes; dst++ {
				if b.Reachable(dst) {
					// If reachable, generate the path and verify each link
					if path := b.Path(dst); path == nil {
						t.Errorf("test %d: reachable nil path %v->%v.", i, src, dst)
					} else {
						for p := 1; p < len(path); p++ {
							a := path[p-1]
							b := path[p]
							if a > b {
								a, b = b, a
							}
							found := false
							for _, v := range tt.edges[a] {
								if v == b {
									found = true
									break
								}
							}
							if !found {
								t.Errorf("test %d: path link %v-%v not found.", i, a, b)
							}
						}
					}
				} else {
					// If not reachable, make sure path is also nil
					if path := b.Path(dst); path != nil {
						t.Errorf("test %d: non reachable path %v->%v: have %v, want %v.", i, src, dst, path, nil)
					}
				}
			}
			// Ensure that the order is consistent with the paths returned
			ord := b.Order()
			for dst := 0; dst < tt.nodes; dst++ {
				if b.Reachable(dst) {
					path := b.Path(dst)
					for oi, pi := 0, 0; pi < len(path); pi++ {
						for ord[oi] != path[pi] {
							if oi >= len(ord) {
								t.Errorf("test %d: order/path mismatch: o=%v, p=%v.", i, ord, path)
							}
							oi++
						}
					}
				}
			}
		}
	}
}