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 distances
What is the output of the following code that finds shortest path distances from node 0 in an unweighted graph?
DSA Python
from collections import deque def bfs_shortest_path(graph, start): distances = {node: -1 for node in graph} distances[start] = 0 queue = deque([start]) while queue: current = queue.popleft() for neighbor in graph[current]: if distances[neighbor] == -1: distances[neighbor] = distances[current] + 1 queue.append(neighbor) return distances graph = { 0: [1, 2], 1: [0, 3], 2: [0, 3], 3: [1, 2, 4], 4: [3] } result = bfs_shortest_path(graph, 0) print(result)
Attempts:
2 left
💡 Hint
Remember BFS explores neighbors level by level, so distances increase by 1 each step.
✗ Incorrect
Starting from node 0, nodes 1 and 2 are at distance 1, node 3 is reached next at distance 2, and node 4 is at 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 level by level.
✗ Incorrect
BFS explores all nodes at distance d before nodes at distance d+1, so the first time it reaches a node, it is via the shortest path.
🔧 Debug
advancedIdentify the error in BFS shortest path code
What error does the following code produce when run on the given graph?
DSA Python
from collections import deque def bfs_shortest_path(graph, start): distances = {node: None for node in graph} distances[start] = 0 queue = deque([start]) while queue: current = queue.pop() for neighbor in graph[current]: if distances[neighbor] is None: distances[neighbor] = distances[current] + 1 queue.append(neighbor) return distances graph = {0: [2, 1], 1: [3], 2: [4], 3: [4], 4: []} print(bfs_shortest_path(graph, 0))
Attempts:
2 left
💡 Hint
Check how the queue is used to explore nodes.
✗ Incorrect
Using pop() removes elements from the right end, making the queue behave like a stack (DFS), which can cause incorrect shortest path distances in BFS.
🚀 Application
advancedShortest path length in disconnected graph
Given this graph and start node 0, what is the shortest path distance to node 4?
DSA Python
graph = {
0: [1],
1: [0, 2],
2: [1],
3: [4],
4: [3]
}
# BFS shortest path distances from node 0
from collections import deque
def bfs_shortest_path(graph, start):
distances = {node: -1 for node in graph}
distances[start] = 0
queue = deque([start])
while queue:
current = queue.popleft()
for neighbor in graph[current]:
if distances[neighbor] == -1:
distances[neighbor] = distances[current] + 1
queue.append(neighbor)
return distances
result = bfs_shortest_path(graph, 0)
print(result[4])Attempts:
2 left
💡 Hint
Check if node 4 is reachable from node 0.
✗ Incorrect
Nodes 3 and 4 form a disconnected component from node 0, so distance remains -1 indicating unreachable.
🧠 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 in terms of edges.
✗ Incorrect
BFS counts the number of edges, assuming each edge has equal cost. For weighted graphs, algorithms like Dijkstra consider edge weights to find true shortest paths.
