vector<int> topoSort(int V, vector<int> adj[])
{
// code here
vector<int> res;
vector<int> indegree( V , 0);
for(int i = 0 ; i < V ; i++)
for(int nbr : adj[i])
indegree[nbr]++;
queue<int> q;
for(int i = 0; i < V; i++)
if(indegree[i] == 0)
q.push(i);
while(!q.empty()){
int curr = q.front(); q.pop();
res.push_back(curr);
for(int nbr : adj[curr])
if(--indegree[nbr] == 0)
q.push(nbr);
}
return res;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter