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) }
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() } } }
func Scc(g *graph.DirGraph) []stack.Stack { s := stack.New() n := g.Order() SCCs := make([]stack.Stack, 0) visited := make(map[graph.VertexId]bool) vertices := g.VerticesIter() for s.Len() != n { vertex := <-vertices dfs.DirectedDfs(g, vertex, func(v graph.VertexId) { if visited[v] == false { fmt.Println(vertex, v) s.Push(v) visited[v] = true } }) } fmt.Println(s) r := g.Reverse() visited = make(map[graph.VertexId]bool) for s.Len() > 0 { vertex := s.Pop().(graph.VertexId) scc := stack.New() if visited[vertex] == false { dfs.DirectedDfs(r, vertex, func(v graph.VertexId) { fmt.Println(vertex, v) if visited[graph.VertexId(v)] == false { visited[graph.VertexId(v)] = true fmt.Println(vertex, v) scc.Push(v) } }) } if scc.Len() > 1 { SCCs = append(SCCs, *scc) } } return SCCs }
func TestUndirectedDfs(t *testing.T) { h := graph.NewUndirected() 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 UndirectedDfs(h, graph.VertexId(3), func(v graph.VertexId) { counter += int(v) }) if counter != 45 { fmt.Println(counter) t.Error() } }
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() } } }
func TestDijkstra(t *testing.T) { h := graph.NewUndirected() for i := 0; i < 5; i++ { h.AddVertex(graph.VertexId(i)) } h.AddEdge(graph.VertexId(0), graph.VertexId(1), 10) h.AddEdge(graph.VertexId(1), graph.VertexId(2), 20) h.AddEdge(graph.VertexId(2), graph.VertexId(3), 40) h.AddEdge(graph.VertexId(0), graph.VertexId(2), 50) h.AddEdge(graph.VertexId(0), graph.VertexId(3), 80) h.AddEdge(graph.VertexId(0), graph.VertexId(4), 10) h.AddEdge(graph.VertexId(4), graph.VertexId(3), 10) prev := ShortestPath(h, graph.VertexId(0)) if prev[1] != graph.VertexId(0) || prev[2] != graph.VertexId(1) || prev[3] != graph.VertexId(4) || prev[4] != graph.VertexId(0) { fmt.Println(prev) t.Error() } }
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() } }