Last active
May 15, 2016 21:55
-
-
Save Superty/7505db3c72bbe375cacd to your computer and use it in GitHub Desktop.
finding strongly connected components (tarjan's)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int curComp = 1, curNum = 1; | |
int comp[MAX_N], num[MAX_N] = { }; | |
int low[MAX_N], stack[MAX_N], front = 0; | |
void findStronglyConnectedComponents(int v) | |
{ | |
num[v] = low[v] = curNum; | |
curNum++; | |
stack[front] = v; | |
front++; | |
comp[v] = -1; | |
for(int i = 1; i <= N; i++) | |
{ | |
if(e[v][i]) | |
{ | |
if(num[i] == 0) | |
{ | |
findStronglyConnectedComponents(i); | |
low[v] = min(low[v], low[i]); | |
} | |
else if(comp[i] == -1) | |
{ | |
low[v] = min(low[v], low[i]); | |
} | |
} | |
} | |
if(low[v] == num[v]) | |
{ | |
do | |
{ | |
front--; | |
int i = stack[front]; | |
comp[i] = curComp; | |
} while(stack[front] != v); | |
curComp++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment