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.
void kahnTopologicalSort(int V, vector<int> adj[]) {
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);
}
while (!q.empty()) {
int u = q.front(); q.pop();
// process u
for (int v : adj[u]) {
indegree[v]--;
if (indegree[v] == 0) q.push(v);
}
}
}
This code performs a topological sort on a directed graph using Kahn's BFS algorithm.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing all edges in the graph.
- How many times: Each edge is visited exactly once during indegree calculation and once during BFS processing.
As the number of nodes (V) and edges (E) grow, the algorithm visits each node and edge a fixed number of times.
| Input Size (V, E) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 10 + 15 = 25 operations |
| 100 nodes, 200 edges | About 100 + 200 = 300 operations |
| 1000 nodes, 5000 edges | About 1000 + 5000 = 6000 operations |
Pattern observation: The operations grow roughly in proportion to the sum of nodes and edges.
Time Complexity: O(V + E)
This means the time needed grows linearly with the number of nodes plus the number of edges in the graph.
[X] Wrong: "The algorithm takes O(V²) time because it uses nested loops."
[OK] Correct: The inner loops do not run V times for each node; they run once per edge. So total work depends on edges, not V squared.
Understanding this time complexity helps you explain why Kahn's Algorithm is efficient for dependency resolution and scheduling problems.
"What if the graph was 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 algorithms: shortest path (Dijkstra's or BFS), cycle detection in undirected graphs (DFS), and minimum spanning tree (Kruskal's or Prim's) are unrelated to 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 shortest path
- Thinking it works on undirected graphs
- Assuming it detects cycles directly
Solution
Step 1: Identify the data structure needed
Kahn's Algorithm uses a queue to process nodes with zero incoming edges in FIFO order.Step 2: Match with C++ STL containers
std::queue provides FIFO behavior, suitable for this purpose. std::stack is LIFO, std::vector is dynamic array without FIFO, std::set is ordered but not FIFO.Final Answer:
std::queue -> Option AQuick Check:
Queue = std::queue [OK]
- Using std::stack instead of std::queue
- Using std::vector without queue operations
- Using std::set which is ordered but not FIFO
std::vector graph = {{}, {}, {}, {}, {}, {}};
// Edges added as graph[5].push_back(0), etc.
// After running Kahn's Algorithm, which topological order is printed?
// Options are sequences of node numbers.Solution
Step 1: Build indegree and initialize queue
Nodes 4 and 5 have zero indegree, so queue starts with [4,5].Step 2: Process nodes in queue order
Process 4: remove edges 4->0, 4->1; indegree of 0 and 1 decrease. Process 5: remove edges 5->0, 5->2; indegree of 0 and 2 decrease. When indegree of 0 and 2 become zero, add to queue. Continue processing 0, 2, 3, 1 in order.Final Answer:
[4, 5, 0, 2, 3, 1] -> Option CQuick Check:
Topological order = [4,5,0,2,3,1] [OK]
- Starting queue with wrong nodes
- Not updating indegree correctly
- Mixing order of nodes with zero indegree
std::vector<int> indegree(V, 0);
for (int u = 0; u < V; u++) {
for (int v : graph[u]) {
indegree[v]++;
}
}
std::queue<int> q;
for (int i = 0; i < V; i++) {
if (indegree[i] == 0) q.push(i);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : graph[u]) {
indegree[v]--;
if (indegree[v] == 0) q.push(v);
}
// Missing code here
}Solution
Step 1: Analyze the loop behavior
The code correctly calculates indegree and pushes zero indegree nodes into the queue. It decrements indegree and pushes new zero indegree nodes inside the loop.Step 2: Identify missing action
The missing code is to store the popped node u into a topological order vector, e.g.,topo.push_back(u);afterq.pop();. Without it, the algorithm processes correctly but does not output the order.Final Answer:
Not printing or storing the topological order nodes -> Option BQuick Check:
Missing topo.push_back(u) [OK]
- Forgetting to store or print topological order
- Incorrect indegree decrement logic
- Not initializing queue with zero indegree nodes
Solution
Step 1: Understand Kahn's Algorithm processing
Kahn's Algorithm processes nodes with zero indegree and removes edges. If a cycle exists, some nodes never reach zero indegree and remain unprocessed.Step 2: Detect cycle by counting processed nodes
If the number of processed nodes is less than total nodes, it means some nodes are stuck due to cycles. This is a standard cycle detection method using Kahn's Algorithm.Final Answer:
Count processed nodes; if count < total nodes, a cycle exists -> Option AQuick Check:
Processed nodes count < total nodes = cycle [OK]
- Assuming Kahn's Algorithm alone detects cycles without counting
- Confusing BFS with DFS cycle detection methods
- Not tracking processed nodes count
Ready to prove it?
Pass the test and this pattern moves unknown
