Challenge - 5 Problems
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS shortest path distance array
What is the output of the distance array after running BFS from node 0 on the given graph?
DSA C++
/* Graph edges: 0 - 1 0 - 2 1 - 2 1 - 3 2 - 4 3 - 4 BFS from node 0 to find shortest distances */ #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 5; vector<vector<int>> adj(n); adj[0] = {1, 2}; adj[1] = {0, 2, 3}; adj[2] = {0, 1, 4}; adj[3] = {1, 4}; adj[4] = {2, 3}; vector<int> dist(n, -1); queue<int> q; dist[0] = 0; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); } } } for (int d : dist) cout << d << " "; return 0; }
Attempts:
2 left
💡 Hint
Trace BFS layer by layer starting from node 0.
✗ Incorrect
Starting from node 0, nodes 1 and 2 are at distance 1. Node 3 is reached via node 1 at distance 2. Node 4 is reached via node 2 or 3 at distance 2.
🧠 Conceptual
intermediateWhy BFS finds shortest path in unweighted graphs?
Why does BFS guarantee the shortest path distance in an unweighted graph?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in BFS.
✗ Incorrect
BFS visits nodes layer by layer, so the first time it reaches a node is via the shortest path.
🔧 Debug
advancedIdentify the error in BFS shortest path code
What error will this BFS code produce when finding shortest path distances?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 4; vector<vector<int>> adj(n); adj[0] = {1}; adj[1] = {0, 2}; adj[2] = {1, 3}; adj[3] = {2}; vector<int> dist(n, 0); queue<int> q; dist[0] = 0; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (dist[v] == 0) { dist[v] = dist[u] + 1; q.push(v); } } } for (int d : dist) cout << d << " "; return 0; }
Attempts:
2 left
💡 Hint
Check how unvisited nodes are marked.
✗ Incorrect
Using 0 as initial distance causes confusion with the start node's distance 0, so unvisited nodes are not detected properly.
❓ Predict Output
advancedOutput of BFS shortest path parent array
What is the parent array after running BFS from node 0 on this graph?
DSA C++
/* Graph edges: 0 - 1 0 - 2 1 - 3 2 - 3 BFS from node 0 to find parents */ #include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 4; vector<vector<int>> adj(n); adj[0] = {1, 2}; adj[1] = {0, 3}; adj[2] = {0, 3}; adj[3] = {1, 2}; vector<int> parent(n, -1); queue<int> q; parent[0] = 0; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (parent[v] == -1) { parent[v] = u; q.push(v); } } } for (int p : parent) cout << p << " "; return 0; }
Attempts:
2 left
💡 Hint
Check which node discovers node 3 first.
✗ Incorrect
Node 3 is discovered first from node 1, so parent[3] = 1. Nodes 1 and 2 have parent 0.
🧠 Conceptual
expertWhy BFS is not suitable for weighted graphs?
Why does BFS fail to find the shortest path in graphs with weighted edges?
Attempts:
2 left
💡 Hint
Think about how BFS measures distance.
✗ Incorrect
BFS counts edges, not weights, so it cannot find shortest weighted paths; algorithms like Dijkstra are needed.
