← Course home

Graphs · Lesson 35 of 52

Graph traversals

Trace depth-first and breadth-first search and account for visited state.

Full lessonAbout 10 min

You will learn to trace DFS and BFS discovery and explain O(V + E) traversal work.

Recommended first: Graph representations, Queue and deque operations.

On this page 6 sections

The idea

Breadth-first search from A on edges to B, C, and D visits A, B, C, D.

Breadth-first search uses a queue. Depth-first search uses a stack, or the call stack. Both mark a vertex when they first discover it so an edge back to a visited vertex does not enqueue it again. On the directed graph A→B, A→C, B→D, breadth-first order from A is A, B, C, D. B and C are both neighbors of A, and B is enqueued first because it is the first edge. D is discovered from B, so it waits behind C. The library function breadth_first_order returns that list.

A worked trace

StepStateWhat changed
enqueuevertex = AThe search marks A during enqueue.
dequeuevertex = AThe search marks A during dequeue.
enqueuevertex = BThe search marks B during enqueue.
enqueuevertex = CThe search marks C during enqueue.
dequeuevertex = BThe search marks B during dequeue.
enqueuevertex = DThe search marks D during enqueue.
dequeuevertex = CThe search marks C during dequeue.
dequeuevertex = DThe search marks D during dequeue.

Why it is correct

The queue's FIFO order is the reason BFS visits vertices in order of distance from the start. When A is dequeued, its neighbors are one edge away and they enter the queue together. The next dequeues are those neighbors, and their new neighbors are two edges away. A vertex is marked visited when it is enqueued, not when it is dequeued. A later edge cannot place it in the queue a second time. DFS would push neighbors on a stack and could visit D before C, because the most recently pushed neighbor is popped first.

A common mistake

A stack visits a different order. The container chooses the visit order.

What it costs

Each vertex is enqueued once and each edge is inspected once. The time is Θ(V + E) for the vertices and edges reachable from the start. The queue holds at most V vertices, so extra memory is Θ(V). An adjacency matrix would make the neighbor scan Θ(V) per vertex even when the graph is sparse, which is Θ(V²). The adjacency list is why the bound can mention E.

The same graph searched depth-first from A can visit D before C, because D is pushed after C and a stack pops D first. If your test expected BFS order and the code used a stack, this is the mismatch. The container is the algorithm.

Run it

python
from bitsandbytes.graphs.adjacency_list import Graph, breadth_first_ordergraph = Graph()graph.add_edge('A', 'B', 1)graph.add_edge('A', 'C', 1)graph.add_edge('B', 'D', 1)print(breadth_first_order(graph, 'A'))# prints: ['A', 'B', 'C', 'D'] 

Practice and next step

Open Directed acyclic graphs and topological order next. Topological order needs a directed acyclic graph and emits sources first.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.graphs.algorithms

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Breadth-first search from A

The library breadth_first_order on this graph returns [A, B, C, D]. The queue's left end is the front.

vertex
A

State

  1. 0Acomparing

The search marks A during enqueue.

Practice feedback is loading. The lesson explanation remains available above.