Find strongly connected components in a directed graph using Tarjan's algorithm with a single DFS pass and a stack to track low-link values. Handle a graph with 1 million nodes within 2 seconds using non-recursive DFS to avoid stack overflow.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's efficient C++ code implementing Tarjan's algorithm iteratively to find strongly connected components in a directed graph with up to 1 million nodes, avoiding recursion and using explicit stacks for DFS and low-link tracking.
A standard recursive Tarjan runs in (O(V{+}E)) time, but with a million vertices you will blow the C++ call stack long before you run out of work. The algorithm itself does not require recursion, it just needs a LIFO call stack and a second LIFO “Tarjan stack” to keep track of the current component. You can simulate the recursion with your own stack frames:
idx[v] is the discovery index assigned in DFS order.
low[v] is the minimum idx reachable from v by following one tree edge and zero or more back edges.
onStack[v] marks whether v is currently in the Tarjan stack.
The call stack holds frames of the form {v,parent,next} where next is the next adjacency list position to explore.
When you finish exploring a vertex v, you propagate its low value up to its parent and, if low[v] == idx[v], you pop a whole SCC off the Tarjan stack.
Here's a complete, non‑recursive implementation that will handle a million nodes in the required time if compiled with optimisation:
c++
#include <bits/stdc++.h>
using namespace std;
struct Frame {
int v;
int parent; // parent in DFS tree, -1 for root
size_t next = 0; // next neighbor index to explore
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<vector<int>> g(n);
g.reserve(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
}
vector<int> idx(n, -1), low(n), compId(n, -1), onStack(n, 0);
stack<int> tarjanStack;
vector<Frame> callStack;
callStack.reserve(n);
int time = 0, compCount = 0;
for (int start = 0; start < n; ++start) {
if (idx[start] != -1) continue;
// initialise a new DFS tree
callStack.push_back({start, -1, 0});
idx[start] = low[start] = time++;
tarjanStack.push(start);
onStack[start] = 1;
while (!callStack.empty()) {
Frame &f = callStack.back();
int v = f.v;
if (f.next < g[v].size()) {
int w = g[v][f.next++];
if (idx[w] == -1) {
// tree edge to an unvisited vertex
callStack.push_back({w, v, 0});
idx[w] = low[w] = time++;
tarjanStack.push(w);
onStack[w] = 1;
} else if (onStack[w]) {
// back edge to a vertex in the current SCC
low[v] = min(low[v], idx[w]);
}
} else {
// all neighbors processed, backtrack
callStack.pop_back();
if (f.parent != -1) {
low[f.parent] = min(low[f.parent], low[v]);
}
if (low[v] == idx[v]) {
// v is the root of an SCC
while (true) {
int w = tarjanStack.top();
tarjanStack.pop();
onStack[w] = 0;
compId[w] = compCount;
if (w == v) break;
}
++compCount;
}
}
}
}
cout << compCount << "\n";
// If you want to see the component id of each vertex:
// for (int i = 0; i < n; ++i) cout << compId[i] << "\n";
return 0;
}
The algorithm still runs in (O(V{+}E)) time. By using vector‑backed stacks and turning off iostream synchronisation, it will comfortably run within a couple of seconds on a million vertices and a few million edges. Compile with -O2 or higher to get the necessary speed.