I wanted to know ,what does the following part of code mean ,
I understood the whole function but the for loop at the end
is making me confused and not able to understand.
So please tell me what it is doing there?
void topologicalSort(vector<int> adj[], int V)
{
vector<int> in_degree(V, 0);
for (int u = 0; u < V; u++) {
for (int x:adj[u])
in_degree[x]++;
}
queue<int> q;
for (int i = 0; i < V; i++)
if (in_degree[i] == 0)
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
cout<<u<<" ";
for (int x: adj[u])
if (--in_degree[x] == 0)
q.push(x);
}
}
This the link to whole code https://ide.geeksforgeeks.org/PSNw3VplJL
Kahn's algoritm (which has nothing to do with BFS):
q
in your code).That last for loop does step (3).