📖 Scenario: Imagine you are navigating a city with intersections connected by roads. You want to find the shortest number of roads to travel from one intersection to another.
🎯 Goal: Build a program that finds the shortest path length between two intersections in an unweighted graph using Breadth-First Search (BFS).
📋 What You'll Learn
Create a graph as an adjacency list with exact nodes and edges
Add variables for the start and target intersections
Implement BFS to find the shortest path length
Print the shortest path length from start to target
💡 Why This Matters
🌍 Real World
Finding shortest routes in maps, social networks, or any system modeled as connections without weights.
💼 Career
Understanding BFS and shortest path algorithms is essential for software engineers working on navigation, recommendation systems, and network analysis.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a variable called graph of type map[int][]int with these exact entries: 1: []int{2, 3}, 2: []int{1, 4, 5}, 3: []int{1, 6}, 4: []int{2}, 5: []int{2, 6}, 6: []int{3, 5}
DSA Go
Hint
Use a map where keys are intersection numbers and values are slices of connected intersections.
2
Set start and target intersections
Create two variables: start with value 1 and target with value 6
DSA Go
Hint
Use simple integer variables to store the start and target intersections.
3
Implement BFS to find shortest path length
Write a BFS loop using a queue to find the shortest path length from start to target. Use a map called visited to track visited nodes and a map called distance to store distances. Initialize visited[start] = true and distance[start] = 0. Loop until queue is empty or target is found. Update distance for neighbors and mark them visited.
DSA Go
Hint
Use a queue slice to hold nodes to visit. Mark nodes visited and track distance from start.
4
Print the shortest path length
Print the shortest path length from start to target using fmt.Println(distance[target])
DSA Go
Hint
The shortest path length from intersection 1 to 6 is 2 roads.
Practice
(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The node with the highest degree
B. The longest path in the graph
C. The shortest path from a start node to all other nodes
D. The minimum spanning tree
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level starting from the source node, visiting all neighbors before moving deeper.
Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of increasing distance, the first time it reaches a node is via the shortest path.
Final Answer:
The shortest path from a start node to all other nodes -> Option C
Quick Check:
BFS = Shortest path in unweighted graph [OK]
Hint: BFS visits nodes by distance layers to find shortest paths [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest paths
Mixing BFS with minimum spanning tree algorithms
2. Which Go data structure is best to implement the queue needed for BFS?
easy
A. map
B. channel
C. struct
D. slice
Solution
Step 1: Identify queue requirements in BFS
BFS requires a FIFO (first-in, first-out) structure to process nodes in order.
Step 2: Use slice as queue in Go
Slices can be used to append at the end and remove from the front, effectively acting as a queue.
Final Answer:
slice -> Option D
Quick Check:
Queue for BFS = slice [OK]
Hint: Use slice with append and slicing for queue in Go [OK]
Common Mistakes:
Using map which is unordered
Using struct without queue logic
Using channel without concurrency context
3. What is the output distance array after running BFS from node 0 on this graph?