0
0
DSA Cprogramming~20 mins

Shortest Path in Unweighted Graph Using BFS in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BFS shortest path distance array
What is the output distance array after running BFS from node 0 in this unweighted graph?
DSA C
int graph[5][5] = {
  {0, 1, 0, 0, 0},
  {1, 0, 1, 1, 0},
  {0, 1, 0, 0, 1},
  {0, 1, 0, 0, 1},
  {0, 0, 1, 1, 0}
};

int dist[5];
for (int i = 0; i < 5; i++) dist[i] = -1;
dist[0] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == -1) {
      dist[v] = dist[u] + 1;
      queue[rear++] = v;
    }
  }
}

for (int i = 0; i < 5; i++) printf("%d ", dist[i]);
A[0, 1, 2, 1, 2]
B[0, 1, 1, 2, 2]
C[0, 1, 2, 2, 3]
D[0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Trace BFS layer by layer starting from node 0.
🧠 Conceptual
intermediate
1:30remaining
Why BFS finds shortest path in unweighted graphs?
Why does BFS guarantee the shortest path in an unweighted graph?
ABecause BFS uses a priority queue to select the next node.
BBecause BFS uses a stack to explore nodes deeply first.
CBecause BFS marks nodes visited only after exploring all neighbors.
DBecause BFS explores nodes in increasing order of their distance from the start node.
Attempts:
2 left
💡 Hint
Think about the order in which BFS visits nodes.
🔧 Debug
advanced
2:00remaining
Identify the bug in BFS shortest path code
What error will this BFS code cause when run on an unweighted graph?
DSA C
int dist[5];
for (int i = 0; i < 5; i++) dist[i] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

dist[0] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == 0) {
      dist[v] = dist[u] + 1;
      queue[rear++] = v;
    }
  }
}
AInfinite loop because nodes are revisited endlessly
BNo error, code runs correctly
CArray index out of bounds error
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check how visited nodes are tracked.
Predict Output
advanced
2:30remaining
Output of BFS path reconstruction
What is the reconstructed shortest path from node 0 to node 4 after BFS?
DSA C
int parent[5];
for (int i = 0; i < 5; i++) parent[i] = -1;

int dist[5];
for (int i = 0; i < 5; i++) dist[i] = -1;
dist[0] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == -1) {
      dist[v] = dist[u] + 1;
      parent[v] = u;
      queue[rear++] = v;
    }
  }
}

int path[5];
int count = 0;
for (int v = 4; v != -1; v = parent[v]) {
  path[count++] = v;
}

for (int i = count - 1; i >= 0; i--) printf("%d ", path[i]);
A0 2 4
B0 1 2 4
C0 1 3 4
D0 3 4
Attempts:
2 left
💡 Hint
Trace parent links from node 4 back to 0.
🧠 Conceptual
expert
1:30remaining
Time complexity of BFS shortest path in adjacency matrix
What is the time complexity of BFS to find shortest path in an unweighted graph represented by an adjacency matrix of size N x N?
AO(N^2) because BFS checks all possible edges in the matrix
BO(N) because BFS visits each node once
CO(E) where E is number of edges, because BFS visits edges only
DO(log N) because BFS uses a queue
Attempts:
2 left
💡 Hint
Consider how adjacency matrix stores edges and how BFS scans neighbors.