Challenge - 5 Problems
BFS Shortest Path 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 in the given graph?
DSA Javascript
const graph = {
0: [1, 2],
1: [0, 3],
2: [0, 3],
3: [1, 2, 4],
4: [3]
};
function bfsShortestPath(graph, start) {
const queue = [start];
const distances = {};
for (const node in graph) {
distances[node] = -1;
}
distances[start] = 0;
while (queue.length > 0) {
const current = queue.shift();
for (const neighbor of graph[current]) {
if (distances[neighbor] === -1) {
distances[neighbor] = distances[current] + 1;
queue.push(neighbor);
}
}
}
return distances;
}
console.log(bfsShortestPath(graph, 0));Attempts:
2 left
💡 Hint
Think about how BFS explores neighbors level by level starting from the start node.
✗ Incorrect
BFS starts at node 0 with distance 0. Nodes 1 and 2 are directly connected to 0, so distance 1. Node 3 is connected to 1 and 2, so distance 2. Node 4 is connected to 3, so distance 3.
🧠 Conceptual
intermediateWhy BFS finds shortest path in unweighted graphs?
Why does BFS guarantee the shortest path in an unweighted graph?
Attempts:
2 left
💡 Hint
Think about how BFS visits nodes closest to the start before farther ones.
✗ Incorrect
BFS explores all nodes at distance k before nodes at distance k+1, ensuring the first time a node is visited is via the shortest path.
🔧 Debug
advancedIdentify the error in BFS shortest path code
What error will this BFS code produce when run on the graph below?
DSA Javascript
const graph = {
0: [1],
1: [0, 2],
2: [1]
};
function bfsShortestPath(graph, start) {
const queue = [start];
const distances = {};
for (const node in graph) {
distances[node] = 0;
}
distances[start] = 0;
while (queue.length > 0) {
const current = queue.shift();
for (const neighbor of graph[current]) {
if (distances[neighbor] === 0) {
distances[neighbor] = distances[current] + 1;
queue.push(neighbor);
}
}
}
return distances;
}
console.log(bfsShortestPath(graph, 0));Attempts:
2 left
💡 Hint
Check how unvisited nodes are marked and how the condition to visit neighbors is checked.
✗ Incorrect
Initializing all distances to 0 causes confusion between visited and unvisited nodes. Nodes with distance 0 are treated as visited, so some nodes are never visited, leading to wrong results.
❓ Predict Output
advancedOutput of BFS path reconstruction
What is the output path from node 0 to node 4 after running BFS and reconstructing the path?
DSA Javascript
const graph = {
0: [1, 2],
1: [0, 3],
2: [0, 3],
3: [1, 2, 4],
4: [3]
};
function bfsPath(graph, start, end) {
const queue = [start];
const visited = new Set([start]);
const parent = {};
while (queue.length > 0) {
const current = queue.shift();
if (current === end) break;
for (const neighbor of graph[current]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
parent[neighbor] = current;
queue.push(neighbor);
}
}
}
const path = [];
let node = end;
while (node !== undefined) {
path.push(node);
node = parent[node];
}
return path.reverse();
}
console.log(bfsPath(graph, 0, 4));Attempts:
2 left
💡 Hint
Check which neighbor is visited first in BFS and how parent links are set.
✗ Incorrect
BFS visits node 1 before node 2 from node 0, so the path to 4 is 0 -> 1 -> 3 -> 4.
🧠 Conceptual
expertWhy BFS is not suitable for weighted graphs
Why is BFS not suitable for finding shortest paths in weighted graphs with different edge weights?
Attempts:
2 left
💡 Hint
Think about how BFS counts steps versus actual edge costs.
✗ Incorrect
BFS counts the number of edges (steps) from start to node, assuming all edges cost the same. Weighted graphs need algorithms like Dijkstra that consider edge weights.
