Skip to content

Instantly share code, notes, and snippets.

@atiq-cs
Created May 30, 2018 00:51
Show Gist options
  • Save atiq-cs/68c4b90970bcf648313871c9ae30a998 to your computer and use it in GitHub Desktop.
Save atiq-cs/68c4b90970bcf648313871c9ae30a998 to your computer and use it in GitHub Desktop.
Example Topological Sort
Class GraphDemo {
void DFS(int u) {
if (HasCycle)
return ;
if (color[u] == GRAY) {
HasCycle = true;
return ;
}
if (color[u] == BLACK)
return ;
color[u] = GRAY;
foreach (int v in AdjList[u])
DFS(v);
color[u] = BLACK;
linkedList.AddToHead(u);
}
void RunTopo() {
INIT();
// nV = number of vertices
for (int i=0; i<nV; i++)
if (color[i] == WHITE)
DFS(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment