Skip to content
SimpleScope

Graphs

Breadth-first search vs Dijkstra's algorithm

Breadth-first search and Dijkstra's algorithm both explore a graph, in different ways. Breadth-first search: explore level by level with a queue. Dijkstra's algorithm: greedy: settle the closest unfinished node first (a simple list as the queue).

At a glance

PropertyBreadth-first searchDijkstra's algorithm
TechniqueExplore level by level with a queueGreedy: settle the closest unfinished node first (a simple list as the queue)
Best case timeO(V + E)O(V²)
Average case timeO(V + E)O(V²)
Worst case timeO(V + E)O(V²)
Extra spaceO(V)O(V)
Frontier (what holds the nodes to visit)Queue: first in, first outPriority queue: here a linear scan for the smallest distance
Reads edge weightsNoYes
Path guaranteeFewest edges from the startLowest total weight from the start

When to choose each

Breadth-first search: when to choose it

Choose breadth-first search when the edges are interchangeable — a maze of equal steps, a social graph, a board where every move costs the same. With equal weights, counting edges and summing weights are the same question, and BFS answers it with a plain queue: one pass, no distance comparisons, no re-opening of a node. It is strictly the cheaper tool there, and the honest reason to prefer it is that it cannot be wrong about the thing it claims. Give it weighted edges, though, and it does not approximate the cheapest route, it answers a different question entirely.

Dijkstra's algorithm: when to choose it

Choose Dijkstra's algorithm when edges carry non-negative weights and you need the cheapest route rather than the shortest one. It keeps a tentative distance per node, always settles the closest unfinished node, and relaxes that node's edges — which is what lets a longer chain of cheap roads beat a single expensive one. The price is bookkeeping BFS does not need: a priority queue, a comparison per edge against the distance already stored, and a distance that may be improved several times before it is settled. In this lesson that queue is a linear scan, which is why the table shows O(V²) rather than the O((V + E) log V) a binary heap would give.

The same inputs, counted

These numbers come from running both real implementations: edge checks: one for every neighbour the algorithm looks at, on the three graphs from the lessons. Writes are not counted here: breadth-first search and Dijkstra's algorithm record very different amounts of state per node, so a combined total would compare two different things.

InputBreadth-first searchDijkstra's algorithm
Road map1414
Ring with shortcuts1616
Disconnected612

What the numbers show

Both make the same number of edge checks on the two connected graphs — 14 for BFS and 14 for Dijkstra on the road map, 16 and 16 on the ring — so on a connected graph the relaxation Dijkstra does is not extra edge traffic — it is the same 2E look at every edge from both ends, with a weight comparison attached instead of an "is this new" test. The work that separates them is in the writes and in the queue scan, not in the number of edges touched.

The answers, however, differ on exactly the graphs you would hope. On the road map BFS reports B at distance 1 and Dijkstra reports it at 5: there is a direct road A–B, so it is one edge, but it costs 7, while A–C–B costs 2 + 3 = 5. BFS's one-edge answer is correct and useless if the numbers are kilometres. On the ring it is sharper still — BFS puts D one edge away via the direct A–D road, Dijkstra settles D at 8 by going A–F–E–D, three edges, because that direct road costs 9. The settling order shows the same thing: BFS takes A, B, D, F, C, E, Dijkstra takes A, F, B, C, E, D, closest-first.

The disconnected graph is where their costs actually diverge: BFS makes 6 edge checks, Dijkstra 12, twice as many. This implementation seeds Dijkstra's priority queue with every node, so its loop keeps going until the queue is empty and it dequeues the unreachable D, E and F at distance ∞ and relaxes their edges anyway. BFS never puts an unreachable node in its queue at all. Both still report ∞ for those three nodes — Dijkstra just pays 2E over the whole graph to say so, where BFS pays 2E over the reachable part.

See them run

More comparisons