|
| 1 | +--- |
| 2 | +id: tarjans-algorithm |
| 3 | +title: Tarjan’s Algorithm for Strongly Connected Components |
| 4 | +sidebar_label: 0009 - Tarjan’s Algorithm |
| 5 | +tags: [Tarjan's Algorithm, Strongly Connected Components, Algorithm, C++, Problem Solving] |
| 6 | +description: This is a solution for implementing Tarjan’s Algorithm to find strongly connected components in a directed graph. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Tarjan’s Algorithm is used to find strongly connected components (SCCs) in a directed graph. SCCs are subgraphs where every vertex is reachable from every other vertex within the same subgraph. This algorithm is efficient and crucial for various graph-related problems. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +```plaintext |
| 20 | +Input: |
| 21 | +Graph: |
| 22 | +0 -> 1 |
| 23 | +1 -> 2 |
| 24 | +2 -> 0 |
| 25 | +1 -> 3 |
| 26 | +3 -> 4 |
| 27 | +
|
| 28 | +Output: |
| 29 | +SCCs: |
| 30 | +0 2 1 |
| 31 | +3 |
| 32 | +4 |
| 33 | +
|
| 34 | +Explanation: The graph contains three SCCs: {0, 1, 2}, {3}, and {4}. |
| 35 | +``` |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- The graph is a directed graph. |
| 40 | +- The algorithm should handle graphs with up to 10^5 vertices and edges. |
| 41 | + |
| 42 | +## Solution of Given Problem |
| 43 | + |
| 44 | +### Intuition and Approach |
| 45 | + |
| 46 | +Tarjan’s Algorithm uses depth-first search (DFS) to traverse the graph and identifies SCCs using a stack and low-link values |
| 47 | + |
| 48 | +- The algorithm follows these steps: |
| 49 | + |
| 50 | +1. Initialize a stack to keep track of visited vertices. |
| 51 | +2. Use a DFS traversal to explore the graph. |
| 52 | +3. For each vertex, assign discovery and low-link values. |
| 53 | +4. If a vertex's low-link value equals its discovery value, it is the root of an SCC. |
| 54 | +5. Pop vertices from the stack until the root vertex is reached, forming an SCC. |
| 55 | + |
| 56 | +### Approaches |
| 57 | + |
| 58 | +#### Codes in Different Languages |
| 59 | + |
| 60 | +<Tabs> |
| 61 | + <TabItem value="cpp" label="C++"> |
| 62 | + <SolutionAuthor name="sjain1909"/> |
| 63 | + ```cpp |
| 64 | + #include <bits/stdc++.h> |
| 65 | + using namespace std; |
| 66 | + |
| 67 | + class TarjanSCC { |
| 68 | + int V; |
| 69 | + list<int> *adj; |
| 70 | + vector<int> disc, low, stackMember; |
| 71 | + stack<int> st; |
| 72 | + int time; |
| 73 | + |
| 74 | + void SCCUtil(int u) { |
| 75 | + disc[u] = low[u] = ++time; |
| 76 | + st.push(u); |
| 77 | + stackMember[u] = true; |
| 78 | + |
| 79 | + for (int v : adj[u]) { |
| 80 | + if (disc[v] == -1) { |
| 81 | + SCCUtil(v); |
| 82 | + low[u] = min(low[u], low[v]); |
| 83 | + } |
| 84 | + else if (stackMember[v] == true) { |
| 85 | + low[u] = min(low[u], disc[v]); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + int w = 0; |
| 90 | + if (low[u] == disc[u]) { |
| 91 | + while (st.top() != u) { |
| 92 | + w = st.top(); |
| 93 | + cout << w << " "; |
| 94 | + stackMember[w] = false; |
| 95 | + st.pop(); |
| 96 | + } |
| 97 | + w = st.top(); |
| 98 | + cout << w << "\n"; |
| 99 | + stackMember[w] = false; |
| 100 | + st.pop(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +public: |
| 105 | + TarjanSCC(int V) { |
| 106 | + this->V = V; |
| 107 | + adj = new list<int>[V]; |
| 108 | + disc = vector<int>(V, -1); |
| 109 | + low = vector<int>(V, -1); |
| 110 | + stackMember = vector<int>(V, false); |
| 111 | + time = 0; |
| 112 | + } |
| 113 | + |
| 114 | + void addEdge(int v, int w) { |
| 115 | + adj[v].push_back(w); |
| 116 | + } |
| 117 | + |
| 118 | + void SCC() { |
| 119 | + for (int i = 0; i < V; i++) { |
| 120 | + if (disc[i] == -1) { |
| 121 | + SCCUtil(i); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +int main() { |
| 128 | + int V, E; |
| 129 | + cout << "Enter the number of vertices: "; |
| 130 | + cin >> V; |
| 131 | + cout << "Enter the number of edges: "; |
| 132 | + cin >> E; |
| 133 | + |
| 134 | + TarjanSCC g(V); |
| 135 | + for (int i = 0; i < E; ++i) { |
| 136 | + int v, w; |
| 137 | + cout << "Enter edge (v w): "; |
| 138 | + cin >> v >> w; |
| 139 | + g.addEdge(v, w); |
| 140 | + } |
| 141 | + |
| 142 | + cout << "Strongly Connected Components are:\n"; |
| 143 | + g.SCC(); |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
| 147 | + ``` |
| 148 | + </TabItem> |
| 149 | +</Tabs> |
| 150 | + |
| 151 | +### Complexity Analysis |
| 152 | + |
| 153 | +- **Time Complexity:** $O(V*E)$ |
| 154 | +- **Space Complexity:** $O(V)$ |
| 155 | + |
| 156 | +The time complexity is determined by the relaxation of edges in the graph. The space complexity is linear due to the storage of distances. |
| 157 | + |
| 158 | +## Video Explanation of Given Problem |
| 159 | + |
| 160 | + <LiteYouTubeEmbed |
| 161 | + id="2kREIkF9UAs" |
| 162 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 163 | + title="Problem Explanation | Solution | Approach" |
| 164 | + poster="maxresdefault" |
| 165 | + webp |
| 166 | + /> |
| 167 | +--- |
| 168 | + |
| 169 | +<h2>Authors:</h2> |
| 170 | + |
| 171 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 172 | +{['sjain1909'].map(username => ( |
| 173 | + <Author key={username} username={username} /> |
| 174 | +))} |
| 175 | +</div> |
0 commit comments