func Sort(g *graph.DirGraph) *stack.Stack { var visit func(v graph.VertexId) sorted := stack.New() visited := make(map[graph.VertexId]bool) marked := make(map[graph.VertexId]bool) visit = func(v graph.VertexId) { if marked[v] { panic("Not a Directed Acyclic Graph (DAG)") } marked[v] = true successors := g.GetSuccessors(v).VerticesIter() for successor := range successors { if !marked[successor] && !visited[successor] { visit(successor) } } visited[v] = true marked[v] = false sorted.Push(v) } vertices := g.VerticesIter() for vertex := range vertices { if !visited[vertex] { visit(vertex) } } return sorted }
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 UndirectedDfs(g *graph.UnGraph, v graph.VertexId, fn func(graph.VertexId)) { s := stack.New() s.Push(v) visited := make(map[graph.VertexId]bool) for s.Len() > 0 { v := s.Pop().(graph.VertexId) if _, ok := visited[v]; !ok { visited[v] = true fn(v) neighbours := g.GetNeighbours(v).VerticesIter() for neighbour := range neighbours { s.Push(neighbour) } } } }