/* Graph edges: 0 -> 1 0 -> 2 1 -> 3 2 -> 3 */ #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int V = 4; vector<vector<int>> adj(V); adj[0].push_back(1); adj[0].push_back(2); adj[1].push_back(3); adj[2].push_back(3); vector<int> indegree(V, 0); for (int i = 0; i < V; i++) { for (int node : adj[i]) { indegree[node]++; } } queue<int> q; for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.push(i); } vector<int> topo_order; while (!q.empty()) { int u = q.front(); q.pop(); topo_order.push_back(u); for (int v : adj[u]) { indegree[v]--; if (indegree[v] == 0) q.push(v); } } for (int node : topo_order) { cout << node << " "; } cout << endl; return 0; }
Kahn's algorithm starts with nodes having zero indegree. Here, node 0 has zero indegree. After processing 0, nodes 1 and 2 become zero indegree and are added to the queue. The order depends on queue insertion order, so 1 is processed before 2, resulting in 0 1 2 3.
/* Graph edges: 0 -> 1 1 -> 2 2 -> 0 (cycle) */ #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int V = 3; vector<vector<int>> adj(V); adj[0].push_back(1); adj[1].push_back(2); adj[2].push_back(0); vector<int> indegree(V, 0); for (int i = 0; i < V; i++) { for (int node : adj[i]) { indegree[node]++; } } queue<int> q; for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.push(i); } vector<int> topo_order; while (!q.empty()) { int u = q.front(); q.pop(); topo_order.push_back(u); for (int v : adj[u]) { indegree[v]--; if (indegree[v] == 0) q.push(v); } } if ((int)topo_order.size() != V) { cout << "Cycle detected" << endl; } else { for (int node : topo_order) { cout << node << " "; } cout << endl; } return 0; }
Kahn's algorithm cannot process nodes in a cycle because their indegree never becomes zero. Here, no node has zero indegree initially, so the queue is empty and no nodes are processed. The algorithm detects a cycle and prints Cycle detected.
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int V = 3; vector<vector<int>> adj(V); adj[0].push_back(1); adj[1].push_back(2); vector<int> indegree(V, 0); for (int i = 0; i < V; i++) { for (int node : adj[i]) { indegree[i]++; } } queue<int> q; for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.push(i); } vector<int> topo_order; while (!q.empty()) { int u = q.front(); q.pop(); topo_order.push_back(u); for (int v : adj[u]) { indegree[v]--; if (indegree[v] == 0) q.push(v); } } for (int node : topo_order) { cout << node << " "; } cout << endl; return 0; }
The bug is in the indegree calculation loop. It increments indegree[i] for each neighbor, but it should increment indegree[node] because node is the destination of the edge. This causes wrong indegree counts and incorrect processing order.
/* Graph edges: 0 -> 2 1 -> 2 2 -> 3 */ #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int V = 4; vector<vector<int>> adj(V); adj[0].push_back(2); adj[1].push_back(2); adj[2].push_back(3); vector<int> indegree(V, 0); for (int i = 0; i < V; i++) { for (int node : adj[i]) { indegree[node]++; } } queue<int> q; for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.push(i); } vector<int> topo_order; while (!q.empty()) { int u = q.front(); q.pop(); topo_order.push_back(u); for (int v : adj[u]) { indegree[v]--; if (indegree[v] == 0) q.push(v); } } for (int node : topo_order) { cout << node << " "; } cout << endl; return 0; }
Initially, nodes 0 and 1 have zero indegree and are added to the queue in order 0 then 1. The algorithm processes 0 first, then 1, then 2, then 3. So the output is 0 1 2 3.
Kahn's Algorithm works by repeatedly removing nodes with zero indegree. In a cycle, nodes always have at least one incoming edge, so their indegree never becomes zero. Hence, they are never processed, and the final topological order has fewer nodes than the graph, indicating a cycle.
