Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created February 24, 2015 01:57
Show Gist options
  • Save nhocki/9f34696b473ced99f2c4 to your computer and use it in GitHub Desktop.
Save nhocki/9f34696b473ced99f2c4 to your computer and use it in GitHub Desktop.
Tarjan Algorithm in C++
/* Complexity: O(E + V)
Tarjan's algorithm for finding strongly connected
components.
*d[i] = Discovery time of node i. (Initialize to -1)
*low[i] = Lowest discovery time reachable from node
i. (Doesn't need to be initialized)
*scc[i] = Strongly connected component of node i. (Doesn't
need to be initialized)
*s = Stack used by the algorithm (Initialize to an empty
stack)
*stacked[i] = True if i was pushed into s. (Initialize to
false)
*ticks = Clock used for discovery times (Initialize to 0)
*current_scc = ID of the current_scc being discovered
(Initialize to 0)
*/
vector<int> g[MAXN];
int d[MAXN], low[MAXN], scc[MAXN];
bool stacked[MAXN];
stack<int> s;
int ticks, current_scc;
void tarjan(int u){
d[u] = low[u] = ticks++;
s.push(u);
stacked[u] = true;
const vector<int> &out = g[u];
for (int k=0, m=out.size(); k<m; ++k){
const int &v = out[k];
if (d[v] == -1){
tarjan(v);
low[u] = min(low[u], low[v]);
}else if (stacked[v]){
low[u] = min(low[u], low[v]);
}
}
if (d[u] == low[u]){
int v;
do{
v = s.top();
s.pop();
stacked[v] = false;
scc[v] = current_scc;
}while (u != v);
current_scc++;
}
}
@probablyangg
Copy link

probablyangg commented May 28, 2019

works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment