0Pricing
Coding Interview Prep · Lesson

BFS for Shortest Unweighted Paths

Layer-by-layer distance from a source.

BFS for Shortest Unweighted Paths is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What BFS Does

BFS explores a graph in rings: first your start, then everything one step away, then two steps, and so on. 🌊

Why Rings Mean Shortest

Because BFS finishes each ring before the next, the first time it reaches a node is the shortest unweighted path to it.

The Queue Is the Engine

BFS uses a queue: first in, first out. You add new neighbors at the back and process the front next.

from collections import deque
q = deque([start])

Track What You Have Seen

Keep a visited marker so you never enqueue the same node twice. This keeps BFS fast and finite.

visited = [False] * (n + 1)
visited[start] = True

Store the Distance

A dist array holds each node's layer. Start gets 0; every neighbor is one more than its parent.

dist = [-1] * (n + 1)
dist[start] = 0

Pop the Front

Each step, take the node at the front of the queue. It is the closest unprocessed node, so handle it now.

u = q.popleft()

Expand the Neighbors

For each neighbor of u that is unvisited, mark it, set its distance, and push it to the back of the queue.

for v in adj[u]:
    if dist[v] == -1:
        dist[v] = dist[u] + 1
        q.append(v)

The Full Loop

Keep popping and expanding while the queue is not empty. When it drains, you have visited every reachable node.

while q:
    u = q.popleft()
    for v in adj[u]:
        if dist[v] == -1:
            dist[v] = dist[u] + 1
            q.append(v)

Mark on Enqueue

Set visited the moment you enqueue, not when you pop. Marking late lets duplicates slip into the queue.

Unreachable Stays -1

Any node still holding distance -1 after BFS is simply unreachable from your start. That answer is meaningful too.

BFS Is Linear

BFS touches every node and edge once, so it runs in O(n + m). That easily clears most contest limits.

Quick Check

Why does plain BFS give shortest paths?

Recap

You run BFS with a queue and a dist array: mark on enqueue, expand neighbors, and read shortest distances when it ends. 🎉

Frequently asked questions

Is the “BFS for Shortest Unweighted Paths” lesson free?

Yes — the full text of “BFS for Shortest Unweighted Paths” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “BFS for Shortest Unweighted Paths”?

Layer-by-layer distance from a source. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “BFS for Shortest Unweighted Paths” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Adjacency Lists from Input
  2. BFS for Shortest Unweighted Paths
  3. DFS, Recursion & Iterative Stacks
  4. Connected Components & Flood Fill
← Back to Coding Interview Prep