Problem Solving

Breadth-First Search

Students color the nodes in successive layers away from the start.

Breadth-First Search definition

Breadth-first search explores a graph in layers of increasing edge distance from a starting vertex.

In Spanish
búsqueda en anchura
Topic
Graph algorithms and networks

Breadth-First Search in a picture

Breadth-First Search diagramSearch visits the start, its neighbors, and then vertices at distance two.Layer 0: start APut A into the queueLayer 1: neighbors B and CDiscover them while processing ALayer 2: newly reached verticesTheir shortest routes use two edges
Search visits the start, its neighbors, and then vertices at distance two.

How it works

Use a queue: mark the start as discovered and put it at the back. Remove vertices from the front and enqueue each undiscovered neighbor. Mark neighbors when first enqueued so a vertex is processed only once. On an unweighted graph it finds shortest distances to reachable vertices.

Worked examples

With edges A–B, A–C, and B–D, which vertices are at distance one from A?

B and C.

What distance does the search find for D?

Two, using A–B–D.

Does a single-start search visit an unreachable vertex?

No. Only reachable vertices enter the queue.

Common mistake

Mark a vertex when you enqueue it. Otherwise multiple neighbors may enqueue the same vertex.

Quick check

Which data structure keeps the search in layer order?

Can it find an unweighted shortest path?

Show answer

Yes. Save each discovered vertex’s predecessor.

Does neighbor order change the shortest distance?

Show answer

No, although it can change which tied path is returned.

Sources

Flashcard

Breadth-First Search

Reveal the definition

Breadth-first search explores a graph in layers of increasing edge distance from a starting vertex.

Effortless Math. “Breadth-First Search.” Math Dictionary.