0
0
DSA Cprogramming~10 mins

Topological Sort Using Kahn's Algorithm BFS in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Topological Sort Using Kahn's Algorithm BFS
Calculate in-degree of all nodes
Add nodes with in-degree 0 to queue
While queue not empty
Pop node from queue
Add node to topological order
Decrease in-degree of neighbors
If neighbor in-degree becomes 0, add to queue
Repeat until queue empty
Check if all nodes processed
Output topological order or detect cycle
This flow shows how Kahn's algorithm uses in-degree counts and a queue to produce a topological order by repeatedly removing nodes with zero in-degree.
Execution Sample
DSA C
int indegree[V] = {0};
queue<int> q;
for (int u = 0; u < V; u++) {
  for (auto v : adj[u]) {
    indegree[v]++;
  }
}
for (int u = 0; u < V; u++) {
  if (indegree[u] == 0) q.push(u);
}
This code calculates in-degree for each node and initializes the queue with nodes having zero in-degree.
Execution Table
StepOperationNode ProcessedQueue StateIn-degree ArrayTopological OrderVisual State
1Calculate in-degree-[ ][A:0, B:1, C:1, D:2, E:1, F:0][]Graph: A->B, A->C, B->D, C->D, D->E
2Initialize queue with zero in-degree nodes-[A, F][A:0, B:1, C:1, D:2, E:1, F:0][]Queue contains nodes with no incoming edges: A, F
3Pop from queueA[F][A:0, B:1, C:1, D:2, E:1, F:0][A]Remove A, process neighbors B and C
4Decrease in-degree of B-[F][A:0, B:0, C:1, D:2, E:1, F:0][A]B in-degree decreased from 1 to 0
5Decrease in-degree of C-[F][A:0, B:0, C:0, D:2, E:1, F:0][A]C in-degree decreased from 1 to 0
6Add B and C to queue-[F, B, C][A:0, B:0, C:0, D:2, E:1, F:0][A]B and C added to queue as their in-degree is zero
7Pop from queueF[B, C][A:0, B:0, C:0, D:2, E:1, F:0][A, F]Remove F, no neighbors to process
8Pop from queueB[C][A:0, B:0, C:0, D:2, E:1, F:0][A, F, B]Remove B, process neighbor D
9Decrease in-degree of D-[C][A:0, B:0, C:0, D:1, E:1, F:0][A, F, B]D in-degree decreased from 2 to 1
10Pop from queueC[][A:0, B:0, C:0, D:1, E:1, F:0][A, F, B, C]Remove C, process neighbor D
11Decrease in-degree of D-[][A:0, B:0, C:0, D:0, E:1, F:0][A, F, B, C]D in-degree decreased from 1 to 0
12Add D to queue-[D][A:0, B:0, C:0, D:0, E:1, F:0][A, F, B, C]D added to queue as in-degree is zero
13Pop from queueD[][A:0, B:0, C:0, D:0, E:1, F:0][A, F, B, C, D]Remove D, process neighbor E
14Decrease in-degree of E-[][A:0, B:0, C:0, D:0, E:0, F:0][A, F, B, C, D]E in-degree decreased from 1 to 0
15Add E to queue-[E][A:0, B:0, C:0, D:0, E:0, F:0][A, F, B, C, D]E added to queue as in-degree is zero
16Pop from queueE[][A:0, B:0, C:0, D:0, E:0, F:0][A, F, B, C, D, E]Remove E, no neighbors to process
17Check if all nodes processed-[][A:0, B:0, C:0, D:0, E:0, F:0][A, F, B, C, D, E]All nodes processed, topological order complete
💡 Queue empty and all nodes processed, topological order found
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 11After Step 15Final
Queue[][A, F][F, B, C][][E][]
In-degree[A:0, B:1, C:1, D:2, E:1, F:0][A:0, B:1, C:1, D:2, E:1, F:0][A:0, B:0, C:0, D:2, E:1, F:0][A:0, B:0, C:0, D:0, E:1, F:0][A:0, B:0, C:0, D:0, E:0, F:0][A:0, B:0, C:0, D:0, E:0, F:0]
Topological Order[][][A][A, F, B, C][A, F, B, C, D][A, F, B, C, D, E]
Key Moments - 3 Insights
Why do we add nodes with zero in-degree to the queue at the start?
Nodes with zero in-degree have no dependencies and can be processed first. This is shown in execution_table row 2 where nodes A and F are added to the queue initially.
What happens when we decrease the in-degree of neighbors?
Decreasing in-degree simulates removing the processed node's edges. When a neighbor's in-degree becomes zero, it means all its dependencies are processed, so it is added to the queue (see rows 4-6 and 11-12).
How do we know if the graph has a cycle?
If after processing, some nodes still have non-zero in-degree and the queue is empty, it means a cycle exists. Here, all nodes reach zero in-degree and are processed (row 17), so no cycle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what nodes are in the queue?
A[F, B, C]
B[A, F]
C[B, C]
D[C]
💡 Hint
Check the 'Queue State' column at step 6 in execution_table
At which step does node D get added to the queue?
AStep 11
BStep 12
CStep 13
DStep 14
💡 Hint
Look for the step where 'Add D to queue' operation happens in execution_table
If node E had an extra incoming edge, how would the in-degree array change after step 14?
AE's in-degree would be 1 instead of 0
BE's in-degree would be 0 as before
CD's in-degree would increase
DF's in-degree would increase
💡 Hint
Check the 'In-degree Array' column at step 14 and consider how extra edges affect in-degree
Concept Snapshot
Topological Sort (Kahn's Algorithm):
- Calculate in-degree for all nodes
- Add nodes with zero in-degree to a queue
- While queue not empty:
  - Remove node, add to order
  - Decrease in-degree of neighbors
  - Add neighbors with zero in-degree to queue
- If all nodes processed, order is valid
- Detect cycle if nodes remain unprocessed
Full Transcript
Topological Sort using Kahn's Algorithm works by first calculating the in-degree of each node, which counts how many edges come into it. Nodes with zero in-degree have no dependencies and are added to a queue. Then, the algorithm repeatedly removes nodes from the queue, adds them to the topological order, and decreases the in-degree of their neighbors. If a neighbor's in-degree becomes zero, it is added to the queue. This process continues until the queue is empty. If all nodes are processed, the topological order is complete. If some nodes remain with non-zero in-degree, it means the graph has a cycle and no valid topological order exists. The execution table shows each step, including queue contents, in-degree array, and the growing topological order. Key moments include understanding why zero in-degree nodes start the process, how in-degree decreases simulate edge removal, and how cycle detection works by checking unprocessed nodes. The visual quiz tests understanding of queue states, step when nodes are added, and effects of extra edges on in-degree.