All subjects · Computer Science · WCP03 Advanced Principles of Computer Science

Graph traversal: depth-first and breadth-first

Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science

Traversing a graph means visiting every reachable vertex once. Depth-first goes as deep as possible before backtracking; breadth-first visits all neighbours before moving further out. Tracing them by hand is a standard exam task.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Depth-first traversal (DFS)

Uses a stack (or recursion). Visit a vertex, mark it visited, then go to its first unvisited neighbour, and so on; when a vertex has no unvisited neighbours, backtrack.

def dfs(graph, v, visited):
    visited.append(v)
    for w in graph[v]:
        if w not in visited:
            dfs(graph, w, visited)
    return visited

Uses: finding a path, solving mazes, detecting cycles, topological ordering.

Breadth-first traversal (BFS)

Uses a queue. Visit the start vertex and enqueue it; repeatedly dequeue a vertex and enqueue all its unvisited neighbours (marking them visited as they are queued).

from collections import deque
def bfs(graph, start):
    visited, queue = [start], deque([start])
    while queue:
        v = queue.popleft()
        for w in graph[v]:
            if w not in visited:
                visited.append(w)
                queue.append(w)
    return visited

Uses: shortest path by number of edges in an unweighted graph, finding everything within k steps (friends of friends), web crawling.

Tracing by hand

Graph: A-B, A-C, B-D, B-E, C-F, E-F, neighbours taken in alphabetical order, starting at A.

MethodOrderStructure
DFSA, B, D, E, F, CStack: go deep, backtrack
BFSA, B, C, D, E, FQueue: level by level

Search, insert and delete

Worked example

Trace a breadth-first traversal from A on the graph A-B, A-C, B-D, B-E, C-F, E-F (alphabetical order).

  1. Visit A; queue [A]. Dequeue A: enqueue B, C. Queue [B, C].
  2. Dequeue B: enqueue D, E (A already visited). Queue [C, D, E].
  3. Dequeue C: enqueue F. Queue [D, E, F].
  4. Dequeue D, E, F: no new vertices.

Answer: A, B, C, D, E, F

Key idea: DFS: stack or recursion, go deep then backtrack. BFS: queue, all neighbours first, level by level; finds fewest-edge paths in unweighted graphs.

Check yourself

  1. Which data structure supports a depth-first traversal?
    • Stack
    • Queue
    • Hash table
    • Circular queue
    Show answer

    Stack. Recursion uses the call stack.

  2. Which data structure supports a breadth-first traversal?
    • Stack
    • Binary tree
    • Queue
    • Linked list of weights
    Show answer

    Queue. First in, first out gives level-by-level order.

  3. Give the depth-first order from A for the graph A-B, A-C, B-D, B-E, C-F, E-F, taking neighbours alphabetically.
    Show answer

    A, B, D, E, F, C. A, then B, then D (dead end, back to B), E, F, then C via F: A, B, D, E, F, C.

  4. Give the breadth-first order from C for the same graph, taking neighbours alphabetically.
    Show answer

    C, A, F, B, E, D. C; its neighbours A, F; then A's B; F's E; then B's D: C, A, F, B, E, D.

  5. Give the depth-first order from F for the same graph, taking neighbours alphabetically.
    Show answer

    F, C, A, B, D, E. F, C, A, B, D, back up to B, E: F, C, A, B, D, E.

  6. Which traversal finds the route with the fewest edges in an unweighted graph?
    • Depth-first
    • Post-order
    • In-order
    • Breadth-first
    Show answer

    Breadth-first. BFS reaches every vertex at distance 1, then 2, and so on.

  7. Why do graph traversals keep a list of visited vertices?
    • To sort the vertices
    • Graphs can contain cycles, so without it a traversal could loop forever
    • To calculate edge weights
    • Because trees need it
    Show answer

    Graphs can contain cycles, so without it a traversal could loop forever. Marking visited vertices prevents revisiting.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.