Last active
May 15, 2016 21:55
-
-
Save Superty/2ca9fc925840e0a3caf9 to your computer and use it in GitHub Desktop.
performing topological sort
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 deg[maxn], rank[maxn]; | |
void topologicalSort() | |
{ | |
queue<int> q; | |
for(int i = 1; i <= n; i++) | |
{ | |
if(deg[i] == 0) | |
{ | |
rank[i] = 0; | |
q.push(i); | |
} | |
} | |
while(!q.empty()) | |
{ | |
int v = q.front()); | |
q.pop(); | |
for(int i = 0; i < e[v].size(); i++) | |
{ | |
deg[e[v][i]]--; | |
if(deg[e[v][i]] == 0) | |
{ | |
rank[e[v][i]] = rank[v] + 1; | |
q.push(e[v][i]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment