|
| 1 | +// kosaraju.go |
| 2 | +// description: Implementation of Kosaraju's algorithm to find Strongly Connected Components (SCCs) in a directed graph. |
| 3 | +// details: The algorithm consists of three steps: |
| 4 | +// 1. Perform DFS and fill the stack with vertices in the order of their finish times. |
| 5 | +// 2. Create a transposed graph by reversing all edges. |
| 6 | +// 3. Perform DFS on the transposed graph in the order defined by the stack to find SCCs. |
| 7 | +// time: O(V + E), where V is the number of vertices and E is the number of edges in the graph. |
| 8 | +// space: O(V), where V is the number of vertices in the graph. |
| 9 | +// ref link: https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm |
| 10 | +// author: mapcrafter2048 |
| 11 | + |
| 12 | +package graph |
| 13 | + |
| 14 | +// Kosaraju returns a list of Strongly Connected Components (SCCs). |
| 15 | +func (g *Graph) Kosaraju() [][]int { |
| 16 | + stack := []int{} |
| 17 | + visited := make([]bool, g.vertices) |
| 18 | + |
| 19 | + // Step 1: Perform DFS and fill stack based on finish times. |
| 20 | + for i := 0; i < g.vertices; i++ { |
| 21 | + if !visited[i] { |
| 22 | + g.fillOrder(i, visited, &stack) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // Step 2: Create a transposed graph. |
| 27 | + transposed := g.transpose() |
| 28 | + |
| 29 | + // Step 3: Perform DFS on the transposed graph in the order defined by the stack. |
| 30 | + visited = make([]bool, g.vertices) |
| 31 | + var sccs [][]int |
| 32 | + |
| 33 | + for len(stack) > 0 { |
| 34 | + // Pop vertex from stack |
| 35 | + v := stack[len(stack)-1] |
| 36 | + stack = stack[:len(stack)-1] |
| 37 | + |
| 38 | + // Perform DFS if not already visited. |
| 39 | + if !visited[v] { |
| 40 | + scc := []int{} |
| 41 | + transposed.dfs(v, visited, &scc) |
| 42 | + sccs = append(sccs, scc) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return sccs |
| 47 | +} |
| 48 | + |
| 49 | +// Helper function to fill the stack with vertices in the order of their finish times. |
| 50 | +func (g *Graph) fillOrder(v int, visited []bool, stack *[]int) { |
| 51 | + visited[v] = true |
| 52 | + |
| 53 | + for neighbor := range g.edges[v] { |
| 54 | + if !visited[neighbor] { |
| 55 | + g.fillOrder(neighbor, visited, stack) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Push the current vertex to the stack after exploring all neighbors. |
| 60 | + *stack = append(*stack, v) |
| 61 | +} |
| 62 | + |
| 63 | +// Helper function to create a transposed (reversed) graph. |
| 64 | +func (g *Graph) transpose() *Graph { |
| 65 | + transposed := &Graph{ |
| 66 | + vertices: g.vertices, |
| 67 | + edges: make(map[int]map[int]int), |
| 68 | + } |
| 69 | + |
| 70 | + for v, neighbors := range g.edges { |
| 71 | + for neighbor := range neighbors { |
| 72 | + if transposed.edges[neighbor] == nil { |
| 73 | + transposed.edges[neighbor] = make(map[int]int) |
| 74 | + } |
| 75 | + transposed.edges[neighbor][v] = 1 // Add the reversed edge |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return transposed |
| 80 | +} |
| 81 | + |
| 82 | +// Helper DFS function used in the transposed graph to collect SCCs. |
| 83 | +func (g *Graph) dfs(v int, visited []bool, scc *[]int) { |
| 84 | + visited[v] = true |
| 85 | + *scc = append(*scc, v) |
| 86 | + |
| 87 | + for neighbor := range g.edges[v] { |
| 88 | + if !visited[neighbor] { |
| 89 | + g.dfs(neighbor, visited, scc) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments