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.
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 visitedUses: 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 visitedUses: 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.
| Method | Order | Structure |
|---|---|---|
| DFS | A, B, D, E, F, C | Stack: go deep, backtrack |
| BFS | A, B, C, D, E, F | Queue: level by level |
Search, insert and delete
- Search for a vertex or value: traverse (DFS or BFS) and stop when found.
- Insert a vertex: add a row and column to the matrix, or a new key with an empty list; insert an edge: set a cell or append to the lists (both lists if undirected).
- Delete an edge: clear the cell or remove it from the lists; delete a vertex: remove its row and column, or its list and every reference to it in other lists.
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).
- Visit A; queue [A]. Dequeue A: enqueue B, C. Queue [B, C].
- Dequeue B: enqueue D, E (A already visited). Queue [C, D, E].
- Dequeue C: enqueue F. Queue [D, E, F].
- Dequeue D, E, F: no new vertices.
Answer: A, B, C, D, E, F
Check yourself
- Which data structure supports a depth-first traversal?
- Stack
- Queue
- Hash table
- Circular queue
Show answer
Stack. Recursion uses the call stack.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary and Advanced Level in Computer Science specification (2026), Issue 1
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 paper, resource booklet and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.