Graph Traversal: BFS and DFS
Aligned to the AQA 7517 specification
- Level
- Intermediate
- Reading time
- 8 min
- Published
- 6 June 2026
- Updated
- 1 July 2026
On this page
Key takeaways
- Breadth-first search (BFS) uses a queue (FIFO) to explore a graph level by level from the source, while depth-first search (DFS) uses a stack (LIFO) or recursion to go as deep as possible along one branch before backtracking.
- Both BFS and DFS maintain a visited set so each reachable vertex is processed once; without it the traversal loops indefinitely in any graph containing cycles.
- In BFS a vertex is marked visited when it is added to the queue, not when it is dequeued, to stop it being enqueued more than once.
- BFS guarantees the shortest path by edge count from the source in an unweighted graph; in a weighted graph use Dijkstra's algorithm because fewer edges can still mean higher total cost.
- DFS suits maze navigation and is also used for detecting cycles, topological sorting, and checking graph connectivity.
Studying this for an exam?
Generate a personalised learning path for this subject. Free to get started.
Key terms
- Graph
- A data structure of vertices (nodes) connected by edges (arcs), which can be undirected, directed, or weighted.
- Graph traversal
- Visiting every reachable vertex exactly once by following edges from a chosen starting vertex.
- Breadth-first search (BFS)
- A traversal using a queue (FIFO) that explores the graph level by level from the source.
- Depth-first search (DFS)
- A traversal using a stack (LIFO) or recursion that follows one branch as far as possible before backtracking.
- Visited set
- The record of already-processed vertices that ensures no vertex is visited twice and prevents infinite loops in cyclic graphs.
- Queue (FIFO)
- A first-in, first-out structure used by BFS to process vertices in order of distance from the source.
- Stack (LIFO)
- A last-in, first-out structure (or the recursive call stack) used by DFS to handle backtracking.
- Backtracking
- Returning to a previous vertex in DFS to try another route once a branch has no unvisited neighbours.
Frequently asked questions
BFS uses a queue to explore a graph level by level, visiting all vertices at distance 1 before distance 2, and so on. DFS uses a stack or recursion to follow one branch as deep as possible before backtracking. BFS spreads outward; DFS plunges downward.
Mark a vertex visited when it is added to the queue, not when it is dequeued. If you wait until it is dequeued, a vertex with two already-processed neighbours can be added to the queue twice, producing an incorrect trace.
BFS guarantees the shortest path by edge count only in an unweighted graph, because it reaches each vertex via the fewest edges. In a weighted graph the path with fewer edges can have a higher total cost, so Dijkstra's algorithm is needed instead.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Vectors
Reverse Polish Notation
Related lessons
5 min
7 min
8 min
9 min