int instack[MaxVertices];
int stack[MaxVertices];
int dfn[MaxVertices],low[MaxVertices];
int stackLen;
int index;
int min(int a,int b)
{
return a<b?a:b;
}
void tarjan(Graph G,Vertex vert, void (*visit)(Vertex V))
{
dfn[vert] = low[vert] = ++index;
stack[stackLen++] = vert;
instack[vert] = 1;
for(PtrToVNode vnode = G->Array[vert];vnode;vnode=vnode->Next)
{
Vertex other = vnode->Vert;
if(dfn[other] == 0)
{
tarjan(G,other,visit);
low[vert] = min(low[vert],low[other]);
}
else if(instack[other])
{
low[vert] = min(low[vert],dfn[other]);
}
}
if(dfn[vert] == low[vert])
{
while(stack[stackLen-1]!=vert)//why while(dfn[Vert]!=low[vert]) does not work?
{
Vertex top = stack[stackLen-1];
stackLen--;
instack[top]=0;
visit(top);
}
stackLen--;
instack[vert]=0;
visit(vert);
printf("\n");
}
}
void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) )
{
for(int i=0;i<G->NumOfVertices;i++)
{
if(dfn[i] == 0)
tarjan(G,i,visit);
}
}