0
0
DSA Cprogramming~10 mins

Shortest Path in Unweighted Graph Using BFS in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Shortest Path in Unweighted Graph Using BFS
Start at source node
Initialize queue with source
While queue not empty
Dequeue current node
For each neighbor
If neighbor not visited
Mark visited, set distance, set parent
Enqueue neighbor
Repeat until queue empty
Build shortest path using parent pointers
Done
Start from the source node, explore neighbors level by level using a queue, track distances and parents to find shortest paths.
Execution Sample
DSA C
void bfs_shortest_path(int graph[][5], int n, int src, int dest) {
  int visited[5] = {0};
  int dist[5];
  int parent[5];
  int INF = 999;
  for(int i = 0; i < n; i++) {
    dist[i] = INF;
    parent[i] = -1;
  }
  dist[src] = 0;
  int q[5];
  int front = 0;
  int rear = -1;
  q[++rear] = src;
  visited[src] = 1;
  while(front <= rear) {
    int u = q[front++];
    for(int v = 0; v < n; v++) {
      if(graph[u][v] && !visited[v]) {
        visited[v] = 1;
        dist[v] = dist[u] + 1;
        parent[v] = u;
        q[++rear] = v;
      }
    }
  }
  // TODO: Reconstruct path from src to dest using parent array
}
This code runs BFS on an unweighted graph to find the shortest path from src to dest.
Execution Table
StepOperationCurrent NodeQueue StateVisited NodesDistance ArrayParent ArrayVisual State
1Initialize-[0][0,0,0,0,0][0,∞,∞,∞,∞][-1,-1,-1,-1,-1]Start with source node 0 in queue
2Dequeue0[][1,0,0,0,0][0,∞,∞,∞,∞][-1,-1,-1,-1,-1]Visit node 0
3Visit neighbors of 00[1,3][1,1,0,1,0][0,1,∞,1,∞][-1,0,-1,0,-1]Neighbors 1 and 3 enqueued
4Dequeue1[3][1,1,0,1,0][0,1,∞,1,∞][-1,0,-1,0,-1]Visit node 1
5Visit neighbors of 11[3,2][1,1,1,1,0][0,1,2,1,∞][-1,0,1,0,-1]Neighbor 2 enqueued
6Dequeue3[2][1,1,1,1,0][0,1,2,1,∞][-1,0,1,0,-1]Visit node 3
7Visit neighbors of 33[2,4][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]Neighbor 4 enqueued
8Dequeue2[4][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]Visit node 2
9Visit neighbors of 22[4][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]No new neighbors to enqueue
10Dequeue4[][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]Visit node 4
11Visit neighbors of 44[][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]No new neighbors to enqueue
12Queue empty-[][1,1,1,1,1][0,1,2,1,2][-1,0,1,0,3]BFS complete, shortest paths found
💡 Queue is empty, BFS traversal finished
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 7Final
visited[0,0,0,0,0][1,0,0,0,0][1,1,0,1,0][1,1,1,1,0][1,1,1,1,1][1,1,1,1,1]
dist[0,∞,∞,∞,∞][0,∞,∞,∞,∞][0,1,∞,1,∞][0,1,2,1,∞][0,1,2,1,2][0,1,2,1,2]
parent[-1,-1,-1,-1,-1][-1,-1,-1,-1,-1][-1,0,-1,0,-1][-1,0,1,0,-1][-1,0,1,0,3][-1,0,1,0,3]
queue[][][1,3][3,2][2,4][]
Key Moments - 3 Insights
Why do we mark a node as visited only when we enqueue it, not when we dequeue it?
Marking visited when enqueuing (see step 3) prevents adding the same node multiple times to the queue, ensuring BFS explores each node once.
How do we know the distance array holds shortest distances?
Distances are set when first visiting a node (step 3, 5, 7), and BFS explores nodes in order of increasing distance, so first distance set is shortest.
Why do we keep a parent array?
Parent array (step 3, 5, 7) helps reconstruct the shortest path from destination back to source after BFS finishes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, which nodes are added to the queue?
ANodes 1 and 3
BNodes 2 and 4
CNode 0 only
DNo nodes added
💡 Hint
Check the 'Queue State' and 'Operation' columns at step 3 in the execution table
At which step does the BFS queue become empty?
AStep 10
BStep 12
CStep 8
DStep 5
💡 Hint
Look at the 'Queue State' column and 'exit_note' in the execution table
If node 2 was connected to node 4, how would the queue change after visiting node 2?
ANode 4 would be added to the queue after node 2
BNode 4 would be added to the queue after node 3
CNo change, node 4 is already visited
DNode 2 would be removed from the queue earlier
💡 Hint
Check 'visited' array in variable_tracker after step 7 to see if node 4 is already visited
Concept Snapshot
Shortest Path in Unweighted Graph Using BFS:
- Use a queue to explore nodes level by level
- Mark nodes visited when enqueued to avoid repeats
- Track distance and parent for each node
- BFS guarantees shortest path in unweighted graphs
- Reconstruct path using parent array after BFS
Full Transcript
This visualization shows how BFS finds the shortest path in an unweighted graph. We start from the source node, add it to a queue, and mark it visited. Then we repeatedly dequeue nodes, visit their neighbors, and enqueue unvisited neighbors while recording their distance and parent. This process continues until the queue is empty. The distance array holds the shortest distance from the source to each node, and the parent array helps reconstruct the shortest path. Key points include marking nodes visited when enqueued to avoid duplicates and BFS's level-by-level exploration ensuring shortest paths.