using System; using System.Collections.Generic; class Program { static void Main() { int V = 4; List<int>[] graph = new List<int>[V]; for (int i = 0; i < V; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[0].Add(2); graph[1].Add(2); graph[2].Add(3); int[] indegree = new int[V]; for (int i = 0; i < V; i++) { foreach (var node in graph[i]) { indegree[node]++; } } Queue<int> q = new Queue<int>(); for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.Enqueue(i); } List<int> topoOrder = new List<int>(); while (q.Count > 0) { int u = q.Dequeue(); topoOrder.Add(u); foreach (var v in graph[u]) { indegree[v]--; if (indegree[v] == 0) q.Enqueue(v); } } Console.WriteLine(string.Join(" -> ", topoOrder) + " -> null"); } }
The algorithm starts with nodes having zero indegree, which is node 0. Then it removes edges from 0 to 1 and 2, updating indegrees. Node 1 becomes zero indegree next, then node 2, and finally node 3. The order is 0 -> 1 -> 2 -> 3 -> null.
using System; using System.Collections.Generic; class Program { static void Main() { int V = 3; List<int>[] graph = new List<int>[V]; for (int i = 0; i < V; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(2); graph[2].Add(0); // cycle here int[] indegree = new int[V]; for (int i = 0; i < V; i++) { foreach (var node in graph[i]) { indegree[node]++; } } Queue<int> q = new Queue<int>(); for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.Enqueue(i); } List<int> topoOrder = new List<int>(); while (q.Count > 0) { int u = q.Dequeue(); topoOrder.Add(u); foreach (var v in graph[u]) { indegree[v]--; if (indegree[v] == 0) q.Enqueue(v); } } if (topoOrder.Count != V) { Console.WriteLine("Cycle detected, topological sort not possible"); } else { Console.WriteLine(string.Join(" -> ", topoOrder) + " -> null"); } } }
The graph contains a cycle (0 -> 1 -> 2 -> 0). Kahn's algorithm cannot process all nodes, so the output indicates cycle detection.
Nodes with zero incoming edges have no dependencies and can be placed first in the topological order. Initializing indegree helps identify these nodes.
using System; using System.Collections.Generic; class Program { static void Main() { int V = 5; List<int>[] graph = new List<int>[V]; for (int i = 0; i < V; i++) graph[i] = new List<int>(); graph[0].Add(2); graph[1].Add(2); graph[1].Add(3); graph[3].Add(4); int[] indegree = new int[V]; for (int i = 0; i < V; i++) { foreach (var node in graph[i]) { indegree[node]++; } } Queue<int> q = new Queue<int>(); for (int i = 0; i < V; i++) { if (indegree[i] == 0) q.Enqueue(i); } List<int> topoOrder = new List<int>(); while (q.Count > 0) { int u = q.Dequeue(); topoOrder.Add(u); foreach (var v in graph[u]) { indegree[v]--; if (indegree[v] == 0) q.Enqueue(v); } } Console.WriteLine(string.Join(" -> ", topoOrder) + " -> null"); } }
Initially, nodes 0 and 1 have zero indegree. The queue enqueues 0 first then 1 (due to loop order). Processing 0 then 1 leads to order 0 -> 1 -> 2 -> 3 -> 4 -> null.
Node 0 must come first. Nodes 1 and 2 can be in any order after 0. So valid orders are: 0->1->2 and 0->2->1, total 2.
