コード例 #1
0
func TestBfs(t *testing.T) {
	h := graph.NewDirected()

	for i := 0; i < 10; i++ {
		v := graph.VertexId(i)
		h.AddVertex(v)
	}

	for i := 0; i < 9; i++ {
		h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
	}

	bfsMap := make(map[graph.VertexId]bool)
	checkVertices := func(v graph.VertexId) {
		bfsMap[v] = true
	}

	Bfs(h, graph.VertexId(3), checkVertices)

	for i := 3; i < len(bfsMap); i++ {
		if _, ok := bfsMap[graph.VertexId(i)]; !ok {
			fmt.Println(bfsMap)
			t.Error()
		}
	}
}
コード例 #2
0
func TestTopologicalSort(t *testing.T) {
	h := graph.NewDirected()
	addEdge := func(h *graph.DirGraph, i, j int) {
		h.AddEdge(graph.VertexId(i), graph.VertexId(j), 1)
	}

	for i := 1; i < 10; i++ {
		h.AddVertex(graph.VertexId(i))
	}

	addEdge(h, 1, 7)
	addEdge(h, 7, 4)
	addEdge(h, 4, 1)

	addEdge(h, 7, 9)

	addEdge(h, 9, 6)
	addEdge(h, 6, 3)
	addEdge(h, 3, 9)

	addEdge(h, 6, 8)

	addEdge(h, 5, 8)
	addEdge(h, 2, 5)
	addEdge(h, 8, 2)

	dfs.DirectedDfs(h, graph.VertexId(9), func(v graph.VertexId) {
		//fmt.Println(v)
	})

	s := Scc(h)
	fmt.Println(s)

}
コード例 #3
0
func TestTopologicalSort(t *testing.T) {
	h := graph.NewDirected()

	h.AddVertex(graph.VertexId(2))
	h.AddVertex(graph.VertexId(3))
	h.AddVertex(graph.VertexId(5))
	h.AddVertex(graph.VertexId(7))
	h.AddVertex(graph.VertexId(8))
	h.AddVertex(graph.VertexId(9))
	h.AddVertex(graph.VertexId(10))
	h.AddVertex(graph.VertexId(11))

	h.AddEdge(graph.VertexId(7), graph.VertexId(8), 1)
	h.AddEdge(graph.VertexId(7), graph.VertexId(11), 1)
	h.AddEdge(graph.VertexId(5), graph.VertexId(11), 1)
	h.AddEdge(graph.VertexId(3), graph.VertexId(8), 1)
	h.AddEdge(graph.VertexId(3), graph.VertexId(10), 1)
	h.AddEdge(graph.VertexId(8), graph.VertexId(9), 1)
	h.AddEdge(graph.VertexId(11), graph.VertexId(10), 1)
	h.AddEdge(graph.VertexId(11), graph.VertexId(2), 1)
	h.AddEdge(graph.VertexId(11), graph.VertexId(9), 1)

	s := Sort(h)

	first := int(s.Pop().(graph.VertexId))
	if first != 7 &&
		first != 5 &&
		first != 3 {
		fmt.Print(first, first != 7)
		t.Error()
	}
}
コード例 #4
0
func TestDirectedDfs(t *testing.T) {
	h := graph.NewDirected()

	for i := 0; i < 10; i++ {
		v := graph.VertexId(i)
		h.AddVertex(v)
	}

	for i := 0; i < 9; i++ {
		h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
	}

	counter := 0
	DirectedDfs(h, graph.VertexId(3), func(v graph.VertexId) {
		counter += int(v)
	})

	if counter != 42 {
		fmt.Println(counter)
		t.Error()
	}
}
コード例 #5
0
func TestBfsShortestPath(t *testing.T) {
	h := graph.NewDirected()

	for i := 0; i < 10; i++ {
		v := graph.VertexId(i)
		h.AddVertex(v)
	}

	for i := 0; i < 9; i++ {
		h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
	}

	distance := ShortestPath(h, graph.VertexId(0))

	for i := 0; i < len(distance); i++ {
		if distance[graph.VertexId(i)] != i {
			t.Error()
		}

		if GetDist(h, graph.VertexId(0), graph.VertexId(i)) != i {
			t.Error()
		}
	}
}