intermediate8 min read·Updated October 1, 2026

BFS shortest path in an unweighted graph

Walk through one complete working example of BFS shortest path in an unweighted graph — setup, mental model, full trace, a twist, and edge cases.

By Learnisim AI·Published October 1, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Arrays or lists and how indexing works
  • How a hash map or set lookup works in constant time
  • Walking a tree or graph without getting lost in recursion
BFS Shortest Path: Layer-by-Layer Ripple Model (0 → 3) Comparing 2-hop BFS shortest path (0→1→3) against hypothetical direct edge Graph Topology & Ripple Expansion Ring d=1 Ring d=2 (Target reached) Extra 0→3 edge (if present later) cost 1 cost 1 cost 1 cost 1 Node 0 Start (d=0) Node 1 dist = 1 Node 2 dist = 1 Node 3 Target (d=2) Key Rule: Unweighted Hops = Edge Count BFS guarantees 0→1→3 or 0→2→3 is found at exactly 2 hops without deep DFS wandering. FIFO Queue & Distance Propagation Step 1: Init Start Node 0 Queue: [0] | dist = {0:0, 1:inf, 2:inf, 3:inf} Push start node 0 into queue at distance 0. Step 2: Pop Node 0 & Explore Ring 1 Queue: [1, 2] | dist = {0:0, 1:1, 2:1, 3:inf} Inspect neighbors 1 & 2; set their dist to 1 & push. Step 3: Pop Node 1 & Hit Target 3! Queue: [2, 3] | dist[3] = dist[1] + 1 = 2 First touch of target node 3! Shortest path 2 hops. Why BFS Beats DFS & Handles Extra Edges • BFS expands in uniform rings, guaranteeing minimum hops. • Even if an extra direct 0→3 edge is added later, BFS still correctly finds shortest path on first target contact.
Shortest hops 0→3 in 0-1, 0-2, 1-3, 2-3 with an extra 0-3 later? Use 0→1→3 vs 0→3 overview diagram
Why

Why Breadth-First Search Rules Unweighted Shortest Paths

Phase 1: The Problem of Navigating Networks

Imagine you are standing at node 0 in a network and need to reach node 3 using the fewest possible hops. The graph has edges: , , , and . Without any direct edge between 0 and 3, you are faced with a choice of routes. If you blindly wander down the first path you find—say, heading deep into a hypothetical detour or stumbling across a circuitous route—you might waste precious steps before reaching your target.
In an unweighted graph, every edge costs the exact same amount: 1 hop. That means the shortest path is purely a matter of counting edges. But how do you guarantee you find the absolute minimum number of edges without manually tracing every single combination? If you try a depth-first exploration, you might wander down a long chain of vertices and return a path of length 3 or 4 while a shorter path of length 2 was sitting right next to your starting point.
This is precisely why we need Breadth-First Search (BFS). Instead of committing to one deep trail, BFS explores outward in expanding rings or layers, checking all neighbors at distance 1 before moving on to distance 2. For our network, exploring layer by layer ensures that the moment our search touches node 3, we have found a valid path of length 2 ( or ) and can instantly stop without guessing.
Why BFS Rules Unweighted Shortest Paths (0 → 3) Expanding outward in layers guarantees minimum hops (Distance 1 before Distance 2) Network Topology & BFS Layers cost 1 cost 1 cost 1 cost 1 Longer path (cost 2) Dist = 1 (Layer 1) Dist = 2 (Target Layer) 0 Start (L0) 1 Neighbor 2 Neighbor 3 Target (L2) BFS Queue & Hop Verification Step 1: Explore Layer 1 (Distance 1) Queue pops [0] → Enqueue neighbors {1, 2} at distance 1. Step 2: Explore Layer 2 (Distance 2) Pop [1] & [2] → Reach Node [3]! Shortest Path Found: 0 → 1 → 3 (Cost = 2 hops) Why BFS Wins vs. Blind Search or Later Edges • Unweighted graphs: Every edge = 1 exact hop. • BFS guarantees shortest path because it visits all distance-1 nodes before any distance-2 nodes. • An extra direct edge (0→3) discovered later cannot beat or alter the already established minimum!
Why Breadth-First Search Rules Unweighted Shortest Paths diagram
Model

The Layer-by-Layer Ripple Model for Unweighted Graphs

Phase 2: The Ripple Model

To find the shortest path in our graph with edges (0, 1), (0, 2), (1, 3), and (2, 3) starting from node 0, we can visualize Breadth-First Search as an expanding ripple of water in a pond. Each step of the ripple corresponds to traversing exactly one edge. Node 0 is at the center (distance 0). Its immediate neighbors, nodes 1 and 2, form the first ripple ring at distance 1. When the ripple expands outward from that ring, it hits node 3 at distance 2.
Unlike depth-first search, which tumbles blindly down a single branch (such as 0 to 1 to 3 and potentially missing a shorter route, or wandering off into loops), BFS guarantees that we explore every node at distance before looking at any node at distance . Because every edge in an unweighted graph carries the exact same "cost" (one hop), the very first time our search wave touches target node 3, we are 100% certain we have found a minimum-edge route.
Distance 0: [0]
Distance 1: ├── [1]
└── [2]
Distance 2: └── [3] (Reached via 0->1->3 or 0->2->3)
To manage this expanding wave mechanically, BFS uses a First-In, First-Out (FIFO) queue data structure. The queue ensures that nodes discovered earlier are always processed before nodes discovered later, perfectly enforcing our layer-by-layer expansion rule.
BFS Shortest Path: The Layer-by-Layer Ripple Model Unweighted Graph 0→3 via 0-1, 0-2, 1-3, 2-3 (Min hops guaranteed on first touch) Dist 1 Dist 2 (Target Hit!) Extra 0-3 Edge (Ignored / Longer) 0 Start (d=0) 1 d=1 2 d=1 3 Target (d=2) Layer-by-Layer State Tree [0] Dist 0 [1], [2] (d=1) [3] Reached (d=2) FIFO Queue Wave Management Ensures nodes discovered earlier process before later ones. Node 0 Node 1 Node 2 Node 3 (Hit!) Why BFS Wins over DFS & Late Paths: • First touch on Node 3 guarantees minimum hops (Distance 2). • Later direct edge 0→3 is ignored because target is already visited.
The Layer-by-Layer Ripple Model for Unweighted Graphs diagram
Worked example

Tracing BFS Step-by-Step on our 4-Node Graph

Phase 3:

To see how the layer-by-layer ripple model functions in practice, let us trace our locked example graph: four nodes connected by undirected edges . We want to find the shortest path from start node 0 to target node 3. We maintain two core data structures: a FIFO queue for visiting nodes in order of discovery, and a distance map dist to record the shortest known hops from the start.

Given

- Graph nodes:
- Graph edges: (0, 1), (0, 2), (1, 3), (2, 3)
- Start node: 0
- Target node: 3

Steps

1. Initialization: Set dist[0] = 0, and set dist for all other nodes to infinity (inf). Push start node 0 into the queue.
- queue = [0]
- dist = {0: 0, 1: inf, 2: inf, 3: inf}
2. Pop Node 0: Remove 0 from the front of the queue. Inspect its unvisited neighbors 1 and 2. Since their current distance is inf, update them: dist[1] = dist[0] + 1 = 1 and dist[2] = dist[0] + 1 = 1. Push 1 and 2 into the queue.
- queue = [1, 2]
- dist = {0: 0, 1: 1, 2: 1, 3: inf}
3. Pop Node 1: Remove 1 from the queue. Inspect its unvisited neighbors. Node 0 is already visited, but node 3 is unvisited. Update dist[3] = dist[1] + 1 = 2. Push 3 into the queue.
- queue = [2, 3]
- dist = {0: 0, 1: 1, 2: 1, 3: 2}
4. Target Reached: Since we popped or discovered 3 (or check upon popping), we see that target 3 has a distance of 2. (We could also process node 2, which sees neighbor 3 is already visited with a distance ).

Result

The shortest path distance from node 0 to node 3 is 2 hops. The valid paths achieving this are 0 -> 1 -> 3 or 0 -> 2 -> 3.
Phase 3: Tracing BFS Step-by-Step on Nodes {0, 1, 2, 3} Graph & Shortest Paths (Target = 3) 0 dist: 0 1 dist: 1 2 dist: 1 3 dist: 2 Valid Path: 0 -> 1 -> 3 (Hops = 2) or 0 -> 2 -> 3 Core Data Structures State FIFO Queue (Visitation Order) [2] 3 (Processed: 0, 1) Distance Map (dist) dist[0]: 0 dist[1]: 1 dist[2]: 1 dist[3]: 2 Trace Summary: • Pop 0 -> discover 1, 2 (dist = 1) • Pop 1 -> discover 3 (dist = 1 + 1 = 2) • Target 3 reached in exactly 2 hops!
Tracing BFS Step-by-Step on our 4-Node Graph diagram
Practice

Put BFS to the Test on a Modified Graph Structure

Phase 4: Practice

Now it is your turn to apply the layer-by-layer ripple model to a slight mutation of our working example. Recall our initial graph with edges , where the shortest path from 0 to 3 takes 2 hops ( or ).
Imagine we add a direct edge to the graph, so the edge list becomes . When BFS pops node 0 from the queue, it iterates through all its unvisited neighbors: 1, 2, and now 3.
Think about what happens to dist[3] and how the queue processes this new direct route. Work through the steps mentally before answering the question below.
Apply

Transferring BFS: Handling Multi-Source and Weighted Complications

Phase 5: Adapting the Ripple Pattern Beyond Basic Edges

We have successfully tracked our unweighted graph from start node 0 to target node 3, discovering that our standard BFS guarantees a minimum path length of 2 through intermediate steps like . But what happens when the rules of the graph change? Suppose our edges now carry weights, such that costs 10 units while costs a total of 2 units. A pure unweighted BFS still only counts edge hops, meaning it would still falsely treat and as equally valid paths of length 2, ignoring the actual edge weights entirely.
To handle weighted graphs, standard BFS must hand over the baton to Dijkstra's algorithm, substituting a priority queue for our standard FIFO queue. However, if every edge weight becomes uniform again—or if we simply want to find the shortest path from multiple simultaneous starting points instead of just node 0—our core BFS framework scales seamlessly. By pre-populating the queue with all source nodes at distance 0 before the main loop begins, the very same ripple pattern expands outward to find the nearest source for every target.

The Limits of Unifying Graph Traversals

Recognizing when standard BFS is insufficient prevents catastrophic bugs in routing and pathfinding systems. If you encounter negative edge weights, BFS and Dijkstra both fail, requiring specialized algorithms like Bellman-Ford. But whenever uniform step costs govern the environment, the layer-by-layer guarantees we observed in our 4-node graph remain your most reliable, linear-time tool.

FAQ

What is BFS shortest path in an unweighted graph, in one sentence?
In an unweighted graph, the first time you reach a target node during a breadth-first traversal is mathematically guaranteed to be the shortest path, because BFS explores all paths of length before checking any path of length .
How does the worked example in this BFS shortest path in an unweighted graph guide actually run?
Follow the Given → Steps → Result trace in “Tracing BFS Step-by-Step on our 4-Node Graph”. Replay every intermediate state on the same input until the result is obvious, then change one value and predict what happens.
What is the most common pitfall with BFS shortest path in an unweighted graph?
Do not use standard unweighted BFS on graphs where edges have varying costs; layer order will no longer correlate with true cost distance.
When is BFS shortest path in an unweighted graph the wrong tool?
In an unweighted graph, the queue's FIFO property naturally sorts nodes by their shortest-path distance from the source. The first time you pop the target node, its distance is strictly minimal.

Keep learning