Skip to the workspace
Pebellslearn by building
Graphs  ›  Lesson

Breadth-First Search & Degrees of Separation

Breadth First Search 20 minExploring outward one ring at a time

dsa / Breadth-First Search & Degrees of Separation

Explore each scene at your own pace.

01 · Degrees of separation on Tunebox

Degrees of separation on Tunebox

Breadth-First Search & Degrees of Separation

Definition
Breadth-first search explores a graph outward from a start vertex one ring at a time, using a queue and a visited set, so the first time it reaches a vertex is along a path with the fewest edges.
In plain words
It is like a ripple spreading from a stone dropped in water: first everyone one step away, then everyone two steps away, and so on. Nobody is visited twice, and the first time you reach someone is by the shortest route.

In short: breadth-first search (BFS) explores a graph outward one ring at a time — everyone one hop away, then everyone two hops away — using a queue (first in, first out) and a visited set so nobody is explored twice. Because it never skips ahead to a farther ring, the first time BFS reaches a vertex is always along a shortest possible route — which is why it powers degrees of separation, "people you may know", maze solvers and any "fewest steps" question on an unweighted graph. Cost: O(V + E) time, O(V) memory for the queue and visited set.

A stone-in-water diagram: ana sits in the centre ring 0, her direct follows ben and cal sit in ring 1, and dia and eli — reached only through ben and cal — sit in ring 2, with a FIFO queue box on the side holding ben, cal, then dia, eli

Degrees of separation on Tunebox

Ana opens Dia's profile and Tunebox says 2nd-degree connection. Working that out means rippling outward from Ana, ring by ring, until Dia turns up:

watch it move · graph

BFS spreads out in rings, and a listener's ring is fixed the first time the walk reaches them.

Ring 0 is Ana alone, zero hops from herself. 5 nodes, 6 edges, directed. At ana. Frontier: ana. ana: 0.

1 / 6

Classic BFS keeps that ripple with one queue instead of explicit rings: discoveries join the back, you explore from the front, so everything closer is explored before anything farther:

watch it move · stack

Names join at the back and leave from the front, so every ring-1 name leaves before any ring-2 name.

The search starts with Ana alone in the queue. Queue, 1 item. Head is ana, tail is ana.

1 / 6
What does this print?
from collections import deque
queue = deque(["ana"])
queue.append("ben")
queue.append("cal")
print(queue.popleft(), queue.pop())

❓ Cross-question — "Why deque? A list has append and pop(0)." It does, but list.pop(0) shifts every remaining item left — O(n) per call — so BFS over a big graph quietly turns quadratic. deque.popleft() is O(1).

The mistake: marking a listener seen when they leave the queue instead of when they join it. Dia gets queued once by Ben and again by Cal, because neither visit has "left" yet when the other happens:

broken — fix it

Each listener should appear once, but Dia appears twice. Fix where the visited set is updated.

Scene 1 of 7
Project context and further resources
You're building a piece ofTunebox — recommendations
This piece — degrees_between(): How many hops separate two listeners.
Scenario Ana opens the profile of someone she's never met, and Tunebox shows "2nd-degree connection". That number is a breadth-first search through the follow graph.
degrees_between.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
degrees_between(graph, start, goal) → intFewest follow hops from start to goal; 0 for the same listener, -1 if unreachable.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.