Created
May 30, 2018 00:51
-
-
Save atiq-cs/68c4b90970bcf648313871c9ae30a998 to your computer and use it in GitHub Desktop.
Example Topological Sort
This file contains hidden or 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
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