Graph, queue and predecessors
A graph G=(V,E) contains vertices and edges. In a directed graph, follow only outgoing edges; an undirected edge can be used both ways. Distance here counts edges, not kilometres. BFS starts with d(s)=0 and all other vertices undiscovered. When it first discovers a neighbour v of u, it sets the distance to d(u)+1 and records u as its predecessor.[1]
Update v only on its first discovery. Mark it before enqueuing it so several neighbours cannot enqueue it repeatedly.[1]
- 1. Mark the start and enqueue it in a FIFO queue.
- 2. Remove the front vertex u and examine its neighbours.
- 3. Mark undiscovered neighbours, set distance/parent and append them to the back.
- 4. Continue until the queue is empty, or stop at first target discovery when only one shortest path is needed.
Why BFS finds shortest paths
FIFO processing handles all vertices at distance 0 before distance 1, then distance 2, and so on. Discovering v from layer k provides a path with k+1 edges. A shorter path would have reached v from an earlier layer. Its first distance is therefore optimal. If several shortest paths exist, neighbour order determines which predecessor is recorded, while the minimum distance stays unchanged.[1][2]
DFS uses recursion or a stack instead. It is useful for reachability and structural questions but does not guarantee that its first route is shortest. With adjacency lists, each vertex is discovered once and its edge list is scanned once, giving O(|V|+|E|); undirected edges appear in two lists. A complete neighbour scan using an adjacency matrix instead takes O(|V|2). Graphs with vertices and edges define the structure; Big-O notation describes search cost.[1]
Queue trace and counterexample
Explore the five-vertex graph in layers
- Given
- Undirected edges s–a,s–b,a–t,b–c,c–t; examine a before b at s.
- Find
- Find distances from s and one shortest path to t.
After s, the queue contains a,b, both at distance 1.
Vertex a discovers t with parent a; b then discovers c. The queue evolves [s]→[a,b]→[b,t]→[t,c]→[c]→[].
The predecessor chain from the target is t,a,s; reverse it to obtain the route.
Result: The path s–a–t has 2 edges. A DFS choosing b and then c first may initially reach t through 3 edges.
Check: There is no direct s–t edge, so distance 1 is impossible. Every edge in the stated paths actually exists.
Why unequal weights invalidate the BFS guarantee
- Given
- Directed edges s→t of cost 10, s→a of cost 1 and a→t of cost 1.
- Find
- Compare minimum edge count with minimum total cost.
BFS discovers t directly from s and counts one edge.
The route via a has two edges but costs only 1+1.
Result: The BFS route minimises edge count but not total cost. Dijkstra is one suitable method for variable nonnegative weights.
Check: Both candidates are valid directed paths. The failure is the incorrect identification of edge count with cost.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Practise graph search
Calculate · Foundation
An undirected graph has edges s–a,a–b,b–t and s–t. What is the BFS distance to t?
Show hint
Check direct neighbours of s first.
Learning goal: Use layers instead of an initially noticed long route.
Show solution and calculation
Vertex t is adjacent to s, so d(t)=1.
The longer route s–a–b–t with 3 edges does not change this.
Rearrange · Foundation
Given parent(t)=c,parent(c)=a,parent(a)=s and no parent for s, reconstruct the path.
Show hint
Follow the chain backwards, then reverse it.
Learning goal: Convert predecessor information into an output route.
Show solution and calculation
Backwards: t,c,a,s.
Forwards: s–a–c–t, containing 3 edges.
Its shortest-path guarantee requires that the chain came from BFS on an unweighted graph.
Interpret · Application
After complete BFS from s, x remains undiscovered. Is d(x)=0, or is there no path?
Show hint
Only the start has distance 0.
Learning goal: Distinguish unreachable vertices from zero distance.
Show solution and calculation
There is no path from s to x following the allowed edge directions.
Its distance remains infinite or absent.
BFS from another start may give a different result.
Error analysis · Application
In a cycle, an implementation marks vertices only when dequeuing them. What problem can arise?
Show hint
Several discovered parents may enqueue the same vertex.
Learning goal: Justify a robust discovery-marking rule.
Show solution and calculation
A vertex may be enqueued repeatedly, and its predecessor assignment may become ambiguous.
Mark it on first enqueue to preserve first-discovery information and the simple linear analysis.
Transfer · Challenge
A complete BFS processes 1000 vertices and 3000 directed edges. Compare adjacency lists with a matrix without inventing timings.
Show hint
Count the adjacency entries that must be examined.
Learning goal: Evaluate representation together with the cost model.
Show solution and calculation
The lists contain 3000 edge references plus 1000 vertex entries, linear in 4000.
A matrix has 10002=1000000 possible entries.
These are model costs, not exact CPU instructions or timings.
Check your understanding with the formula’s reference test.
7 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Choose the right method
A search from one start covers only its reachable part. To visit all components of an undirected graph, restart from undiscovered vertices. For directed graphs, this alone does not compute strongly connected components. Variable edge weights, negative cycles and infinite implicit graphs require additional methods and termination assumptions. Dijkstra for nonnegative weights addresses the weighted case. This article explains the algorithms; a few scalar inputs would not constitute a genuine universal graph solver.[1][2]
Sources for this explanation
Quanta · Updated November 8, 2026
- MIT 6.006, Fall2011: Lecture13, Graphs I: Breadth First Search, typed notes
- Daniel Wolleb-Graf, Swiss Olympiad in Informatics: Breitensuche
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Added a DE/EN article with BFS layer reasoning, a queue trace, weighted-edge limits and five exercises.
Cite this reference page
Quanta. Graph search: BFS, DFS and shortest paths. Updated November 8, 2026. https://quanta-study.de/en/formula-sheet/graphsuche-bfs-dfs-kuerzeste-wege
When citing a specific scientific statement, also cite the specialist source linked to that statement.