Skip to content

Instantly share code, notes, and snippets.

@dabasajay
Last active January 26, 2021 17:35
Show Gist options
  • Save dabasajay/606de2e27f9738a5fe51f984664782d9 to your computer and use it in GitHub Desktop.
Save dabasajay/606de2e27f9738a5fe51f984664782d9 to your computer and use it in GitHub Desktop.
dsa/graphs/sorting/ | desc: topological ordering in DAG
#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