Skip to main content

Topological sort

Problem

Given a Directed Acyclic Graph (DAG) with V vertices and E edges, Find any Topological Sorting of that Graph.

Solution Approach

Expected Time complexity: O(n)O(n)

Click - to see solution code
class Solution {
public:
void findTopoSort(int node, vector<int>& vis, stack<int>& st,
vector<int> adj[]) {
vis[node] = 1;

for (auto it : adj[node]) {
if (!vis[it]) {
findTopoSort(it, vis, st, adj);
}
}
st.push(node);
}

public:
vector<int> topoSort(int N, vector<int> adj[]) {
stack<int> st;
vector<int> vis(N, 0);
for (int i = 0; i < N; i++) {
if (vis[i] == 0) {
findTopoSort(i, vis, st, adj);
}
}
vector<int> topo;
while (!st.empty()) {
topo.push_back(st.top());
st.pop();
}
return topo;
}
};