📖 Scenario: You are helping a delivery robot find the shortest route between locations in a small city. The city map is shown as connections between places.Each connection is a two-way street, and all streets take the same time to cross.
🎯 Goal: Build a program that finds the shortest path from a starting location to a destination using Breadth-First Search (BFS) on an unweighted graph.The program will print the shortest path as a list of places visited in order.
📋 What You'll Learn
Create a graph as a dictionary with exact connections between places
Set the start and end locations as variables
Implement BFS to find the shortest path between start and end
Print the shortest path as a list of place names
💡 Why This Matters
🌍 Real World
Finding shortest routes in maps, social networks, or any system where connections have equal cost.
💼 Career
Understanding BFS and shortest path algorithms is essential for roles in software development, data analysis, and network engineering.
Progress0 / 4 steps
1
Create the city map graph
Create a dictionary called graph representing the city map with these exact connections: 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E']
DSA Python
Hint
Use a dictionary where keys are place names and values are lists of connected places.
2
Set start and end locations
Create two variables: start set to 'A' and end set to 'F'
DSA Python
Hint
Just assign the strings 'A' and 'F' to variables named start and end.
3
Implement BFS to find shortest path
Write a function called bfs_shortest_path(graph, start, end) that uses a queue to find the shortest path from start to end. Use a list as a queue and keep track of visited nodes and paths. Return the path as a list of places.
DSA Python
Hint
Use a queue to store paths. Pop the first path, check last node. If end, return path. Else add neighbors to queue with new paths.
4
Print the shortest path
Call bfs_shortest_path(graph, start, end) and print the returned path.
DSA Python
Hint
Call the function with graph, start, and end. Print the result.
Practice
(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The shortest path from a start node to all other nodes
B. The longest path in the graph
C. The node with the highest degree
D. The cycle in the graph
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level starting from the source node.
Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of distance, it finds the shortest path to each node.
Final Answer:
The shortest path from a start node to all other nodes -> Option A
Quick Check:
BFS = Shortest path in unweighted graph [OK]
Hint: BFS explores neighbors level-wise to find shortest paths [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest path
Assuming BFS works only for weighted graphs
2. Which Python data structure is best to implement the queue needed for BFS?
easy
A. list with append() and pop(0)
B. collections.deque for efficient pops from front
C. set to store unique nodes
D. dictionary to map nodes to distances
Solution
Step 1: Identify queue operations in BFS
BFS requires adding to the back and removing from the front efficiently.
Step 2: Choose data structure with O(1) pops from front
collections.deque supports fast append and popleft operations, unlike list pop(0) which is slow.
Final Answer:
collections.deque for efficient pops from front -> Option B
Quick Check:
Use deque for BFS queue [OK]
Hint: Use deque for fast queue operations in BFS [OK]
Common Mistakes:
Using list pop(0) causing slow performance
Using set which is unordered
Using dict which is not a queue
3. What is the output distance list after running BFS from node 0 on this graph?
graph = {0: [1, 2], 1: [0, 3], 2: [0], 3: [1]}
medium
A. [0, 2, 2, 3]
B. [1, 0, 1, 2]
C. [0, 1, 1, 2]
D. [0, 1, 2, 3]
Solution
Step 1: Initialize distances and start BFS from node 0
Distance to 0 is 0, others are infinity initially.
Step 2: Visit neighbors level-wise
Neighbors of 0 are 1 and 2, distance 1 each. Then neighbor of 1 is 3, distance 2.
Final Answer:
[0, 1, 1, 2] -> Option C
Quick Check:
Distances = [0,1,1,2] [OK]
Hint: Distance increments by 1 per BFS level [OK]
Common Mistakes:
Mixing node indices with distances
Assigning wrong distances to neighbors
Forgetting to update distances only once
4. Identify the bug in this BFS code snippet for shortest path:
def bfs(graph, start):
from collections import deque
queue = deque([start])
distances = {start: 0}
while queue:
node = queue.pop()
for neighbor in graph[node]:
if neighbor not in distances:
distances[neighbor] = distances[node] + 1
queue.append(neighbor)
return distances
medium
A. Using queue.pop() instead of queue.popleft() causes wrong order
B. Distances dictionary should be a list
C. Neighbors should be checked before popping from queue
D. Start node distance should be -1
Solution
Step 1: Check queue operations in BFS
BFS requires removing from front (FIFO), but pop() removes from end (LIFO).
Step 2: Identify correct method to remove front element
queue.popleft() removes from front, preserving BFS order.
Final Answer:
Using queue.pop() instead of queue.popleft() causes wrong order -> Option A
Quick Check:
Use popleft() for BFS queue [OK]
Hint: Use popleft() to dequeue in BFS, not pop() [OK]
Common Mistakes:
Using pop() causing DFS-like behavior
Confusing list and dict for distances
Incorrect initialization of distances
5. Given a grid where 0 represents open cell and 1 represents blocked cell, how can BFS be used to find the shortest path from top-left to bottom-right?
grid = [
[0, 0, 1],
[1, 0, 1],
[1, 0, 0]
]
hard
A. Use BFS only on rows, ignoring columns
B. Run DFS to explore all paths and pick shortest
C. Use BFS but ignore blocked cells as neighbors
D. Treat each cell as a node, edges connect adjacent open cells; run BFS from start to find shortest path
Solution
Step 1: Model grid as graph nodes and edges
Each open cell is a node; edges connect up/down/left/right neighbors if open.
Step 2: Run BFS from top-left to bottom-right
BFS finds shortest path in unweighted graph; blocked cells are excluded from edges.
Final Answer:
Treat each cell as a node, edges connect adjacent open cells; run BFS from start to find shortest path -> Option D
Quick Check:
Grid BFS = shortest path via open neighbors [OK]
Hint: Model grid as graph, BFS finds shortest path avoiding blocks [OK]
Common Mistakes:
Using DFS which is not shortest path
Ignoring blocked cells incorrectly
Not connecting neighbors properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.