Last active
January 26, 2021 17:35
-
-
Save dabasajay/606de2e27f9738a5fe51f984664782d9 to your computer and use it in GitHub Desktop.
dsa/graphs/sorting/ | desc: topological ordering in DAG
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
#include <bits/stdc++.h> | |
using namespace std; | |
int N; | |
vector<vector<int>> graph; | |
vector<int> bfs(){ | |
vector<int> indeg(N, 0); | |
vector<int> ans; | |
for (auto i : graph) | |
for (auto j : i) indeg[j]++; | |
queue<int> q; | |
for (int i = 0; i < N; i++) | |
if (indeg[i] == 0) q.push(i); | |
while (!q.empty()) { | |
int u = q.front(); | |
q.pop(); | |
ans.push_back(u); | |
for (auto i : graph[u]) | |
if (--indeg[i] == 0) q.push(i); | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment