|
| 1 | +// Kosaraju’s Algorithm — Find Strongly Connected Components (SCCs) |
| 2 | +// Problem |
| 3 | +// Given a directed graph, find all SCCs (maximal groups of vertices |
| 4 | +// where every vertex can reach every other vertex in the group). |
| 5 | +// |
| 6 | +// Approach |
| 7 | +// 1. Perform DFS and store vertices in a stack according to finish times. |
| 8 | +// 2. Reverse all edges (transpose the graph). |
| 9 | +// 3. Perform DFS on the transposed graph in the order of the stack. |
| 10 | +// Each DFS traversal finds one SCC. |
| 11 | +// |
| 12 | +// Complexity |
| 13 | +// Time : O(V + E) — DFS twice, transpose edges |
| 14 | +// Space : O(V + E) — adjacency list + visited arrays + recursion stack |
| 15 | +// |
| 16 | +// Input |
| 17 | +// - Number of vertices (V) |
| 18 | +// - List of edges {u, v} |
| 19 | +// |
| 20 | +// Output |
| 21 | +// - List of all SCCs |
| 22 | + |
| 23 | +#include <iostream> |
| 24 | +#include <vector> |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +int V; |
| 28 | +vector<vector<int>> adj; |
| 29 | +vector<vector<int>> adjT; |
| 30 | +vector<bool> visited; |
| 31 | + |
| 32 | +// DFS for finish time |
| 33 | +void dfs1(int u, vector<int> &order) |
| 34 | +{ |
| 35 | + visited[u] = true; |
| 36 | + for (size_t i = 0; i < adj[u].size(); i++) |
| 37 | + { |
| 38 | + int v = adj[u][i]; |
| 39 | + if (!visited[v]) |
| 40 | + dfs1(v, order); |
| 41 | + } |
| 42 | + order.push_back(u); // finished |
| 43 | +} |
| 44 | + |
| 45 | +// DFS for SCC on transposed graph |
| 46 | +void dfs2(int u, vector<int> &component) |
| 47 | +{ |
| 48 | + visited[u] = true; |
| 49 | + component.push_back(u); |
| 50 | + for (size_t i = 0; i < adjT[u].size(); i++) |
| 51 | + { |
| 52 | + int v = adjT[u][i]; |
| 53 | + if (!visited[v]) |
| 54 | + dfs2(v, component); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +int main() |
| 59 | +{ |
| 60 | + ios::sync_with_stdio(false); |
| 61 | + cin.tie(nullptr); |
| 62 | + |
| 63 | + int E; |
| 64 | + cin >> V >> E; |
| 65 | + adj.assign(V, vector<int>()); |
| 66 | + adjT.assign(V, vector<int>()); |
| 67 | + |
| 68 | + for (int i = 0; i < E; i++) |
| 69 | + { |
| 70 | + int u, v; |
| 71 | + cin >> u >> v; |
| 72 | + adj[u].push_back(v); |
| 73 | + adjT[v].push_back(u); // transpose edge |
| 74 | + } |
| 75 | + |
| 76 | + vector<int> order; |
| 77 | + visited.assign(V, false); |
| 78 | + |
| 79 | + // Step 1: DFS to get finish times |
| 80 | + for (int i = 0; i < V; i++) |
| 81 | + if (!visited[i]) |
| 82 | + dfs1(i, order); |
| 83 | + |
| 84 | + // Step 2: DFS on transposed graph |
| 85 | + visited.assign(V, false); |
| 86 | + vector<vector<int>> sccs; |
| 87 | + for (int i = V - 1; i >= 0; i--) |
| 88 | + { |
| 89 | + int u = order[i]; |
| 90 | + if (!visited[u]) |
| 91 | + { |
| 92 | + vector<int> component; |
| 93 | + dfs2(u, component); |
| 94 | + sccs.push_back(component); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Output SCCs |
| 99 | + cout << "Strongly Connected Components:\n"; |
| 100 | + for (size_t i = 0; i < sccs.size(); i++) |
| 101 | + { |
| 102 | + for (size_t j = 0; j < sccs[i].size(); j++) |
| 103 | + cout << sccs[i][j] << " "; |
| 104 | + cout << "\n"; |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | +Example Input: |
| 112 | +5 5 |
| 113 | +0 2 |
| 114 | +2 1 |
| 115 | +1 0 |
| 116 | +0 3 |
| 117 | +3 4 |
| 118 | +
|
| 119 | +Visualization: |
| 120 | +Graph: |
| 121 | +0 → 2 → 1 → 0 (cycle) |
| 122 | +0 → 3 → 4 |
| 123 | +
|
| 124 | +Step 1: DFS to record finish times |
| 125 | +Stack order (top = last finished): [4, 3, 0, 1, 2] |
| 126 | +
|
| 127 | +Step 2: Transpose graph |
| 128 | +Edges reversed: |
| 129 | +2 → 0 |
| 130 | +1 → 2 |
| 131 | +0 → 1 |
| 132 | +3 → 0 |
| 133 | +4 → 3 |
| 134 | +
|
| 135 | +Step 3: DFS on transposed graph using stack order |
| 136 | +- Pop 2 → DFS → {0,1,2} (SCC) |
| 137 | +- Pop 3 → DFS → {3} (SCC) |
| 138 | +- Pop 4 → DFS → {4} (SCC) |
| 139 | +
|
| 140 | +Final SCCs: |
| 141 | +{0,1,2}, {3}, {4} |
| 142 | +
|
| 143 | +Time : O(V + E) |
| 144 | +Space : O(V + E) |
| 145 | +*/ |
0 commit comments