Topological Sort Using Kahn's Algorithm BFS in DSA C# - Time & Space Complexity
We want to understand how the time needed to perform topological sort using Kahn's Algorithm grows as the graph size increases.
Specifically, how the number of nodes and edges affects the work done.
Analyze the time complexity of the following code snippet.
// Kahn's Algorithm for Topological Sort
int V = graph.Length; // number of vertices
int[] inDegree = new int[V];
Queue<int> queue = new Queue<int>();
// Calculate in-degree of each vertex
for (int i = 0; i < V; i++) {
foreach (int neighbor in graph[i]) {
inDegree[neighbor]++;
}
}
// Enqueue vertices with in-degree 0
for (int i = 0; i < V; i++) {
if (inDegree[i] == 0) queue.Enqueue(i);
}
// Process vertices
while (queue.Count > 0) {
int node = queue.Dequeue();
foreach (int neighbor in graph[node]) {
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) queue.Enqueue(neighbor);
}
}
This code performs a topological sort on a directed graph using Kahn's BFS-based algorithm.
- Primary operation: Traversing all vertices and their edges to calculate in-degree and then processing each vertex and its edges in the queue.
- How many times: Each vertex is visited once, and each edge is checked once during in-degree calculation and once during processing.
As the number of vertices (V) and edges (E) grow, the algorithm visits each vertex and edge a fixed number of times.
| Input Size (V, E) | Approx. Operations |
|---|---|
| 10 vertices, 15 edges | About 10 + 15 = 25 operations |
| 100 vertices, 200 edges | About 100 + 200 = 300 operations |
| 1000 vertices, 5000 edges | About 1000 + 5000 = 6000 operations |
Pattern observation: The operations grow roughly in proportion to the sum of vertices and edges.
Time Complexity: O(V + E)
This means the time needed grows linearly with the number of vertices plus the number of edges in the graph.
[X] Wrong: "The algorithm takes O(V * E) time because it has nested loops over vertices and edges."
[OK] Correct: Each edge and vertex is processed only a constant number of times, not nested repeatedly, so the total work is proportional to V plus E, not their product.
Understanding this time complexity helps you explain why Kahn's Algorithm is efficient for scheduling tasks or resolving dependencies in real projects.
"What if the graph is represented using an adjacency matrix instead of adjacency lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand Kahn's Algorithm goal
Kahn's Algorithm is designed to produce a linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every directed edge u -> v, u comes before v in the ordering.Step 2: Compare with other graph algorithms
Other options describe different graph problems: cycle detection, shortest path, and minimum spanning tree, which are not the purpose of Kahn's Algorithm.Final Answer:
To find a topological ordering of a Directed Acyclic Graph (DAG) -> Option DQuick Check:
Topological sort = Kahn's Algorithm purpose [OK]
- Confusing topological sort with cycle detection
- Thinking it finds shortest paths
- Mixing with minimum spanning tree algorithms
Solution
Step 1: Identify the role of zero in-degree nodes
Kahn's Algorithm processes nodes with zero incoming edges in the order they are discovered, which requires a First-In-First-Out (FIFO) structure.Step 2: Match data structure to FIFO behavior
A Queue provides FIFO behavior, making it ideal to store and process zero in-degree nodes in the correct order.Final Answer:
Queue -> Option AQuick Check:
Zero in-degree nodes use FIFO queue [OK]
- Using stack which is LIFO, not suitable here
- Choosing dictionary which stores key-value pairs
- Using hashset which has no order
var graph = new Dictionary<int, List<int>> {
{1, new List<int> {2, 3}},
{2, new List<int> {4}},
{3, new List<int> {4}},
{4, new List<int> ()}
};
var result = TopologicalSort(graph);
Console.WriteLine(string.Join(", ", result));
List<int> TopologicalSort(Dictionary<int, List<int>> graph) {
var inDegree = new Dictionary<int, int>();
foreach (var node in graph.Keys) inDegree[node] = 0;
foreach (var edges in graph.Values)
foreach (var v in edges) inDegree[v]++;
var queue = new Queue<int>(inDegree.Where(kv => kv.Value == 0).Select(kv => kv.Key));
var topoOrder = new List<int>();
while (queue.Count > 0) {
var u = queue.Dequeue();
topoOrder.Add(u);
foreach (var v in graph[u]) {
inDegree[v]--;
if (inDegree[v] == 0) queue.Enqueue(v);
}
}
return topoOrder;
}Solution
Step 1: Calculate in-degree for each node
Node 1: 0, Node 2: 1 (from 1), Node 3: 1 (from 1), Node 4: 2 (from 2 and 3).Step 2: Process nodes in queue order
Start with node 1 (in-degree 0). Remove it, reduce in-degree of 2 and 3 to 0, enqueue both. Then dequeue 2, reduce in-degree of 4 to 1. Dequeue 3, reduce in-degree of 4 to 0, enqueue 4. Finally dequeue 4.Final Answer:
[1, 2, 3, 4] -> Option BQuick Check:
Topological order = [1, 2, 3, 4] [OK]
- Ignoring in-degree updates after dequeue
- Mixing order of nodes with same in-degree
- Starting with wrong initial nodes
var inDegree = new Dictionary<int, int>(); foreach (var node in graph.Keys) inDegree[node] = 0; foreach (var edges in graph.Values) foreach (var v in edges) inDegree[v] = 0; // Bug here var queue = new Queue<int>(inDegree.Where(kv => kv.Value == 0).Select(kv => kv.Key));
Solution
Step 1: Analyze in-degree calculation
The second loop incorrectly sets inDegree[v] = 0 for each adjacent node, overwriting the count instead of incrementing it.Step 2: Correct in-degree update
It should increment inDegree[v] by 1 for each incoming edge to count properly, i.e., inDegree[v]++.Final Answer:
The second loop resets in-degree to zero instead of incrementing -> Option CQuick Check:
In-degree must be incremented, not reset [OK]
- Setting in-degree to zero inside the counting loop
- Using stack instead of queue
- Misinitializing in-degree dictionary
Solution
Step 1: Calculate in-degree for each node
Nodes: 0(in-degree 2 from 4,5), 1(in-degree 2 from 3,4), 2(in-degree 1 from 5), 3(in-degree 1 from 2), 4(in-degree 0), 5(in-degree 0).Step 2: Process nodes with zero in-degree first
Start with 5 and 4. Removing 5 reduces in-degree of 2 to 0 and 0 to 1; removing 4 reduces in-degree of 0 to 0 and 1 to 1. Then process 2 and 0 (in some order), followed by 3, then 1.Final Answer:
[5, 4, 2, 3, 1, 0] -> Option AQuick Check:
Valid topological order respects edges [OK]
- Placing nodes before their dependencies
- Ignoring multiple valid orders
- Starting with nodes that have incoming edges
Ready to prove it?
Pass the test and this pattern moves unknown
