Skip to content

Shortest paths: Dijkstra's algorithm

Find shortest paths from one vertex in a weighted graph with Dijkstra's algorithm, tracing its distance table with a priority queue.

It opens on

There is a direct road from A to B of length 4. Is it the shortest way from A to B?

Why it matters

Dijkstra's algorithm is how maps, routers and games find cheapest routes, and tracing its table by hand, settling the closest vertex each time, is one of the most common exam questions in a data structures course; its O((V + E) log V) cost with a heap is the standard answer.

Good to know first

  • Breadth-first search: distances, parents and reading a path back
  • Building a heap and heapsort: a min-heap pops the smallest item in O(log n)
  • Graphs and their representations: road["B"]["D"] holds a weight

Step by step

  1. Relaxing edges

    Keep a tentative distance for each vertex and improve it by relaxing an edge, dist[u] + w < dist[v].

    From A, relaxing A–B gives B 4 and relaxing A–C gives C 2.

    tentative distances

    A common mix-up

    The path with the fewest edges is the shortest.

    In fact: With weights, A, C, B uses 2 roads and has length 3, shorter than the 1 road of length 4.

  2. The algorithm traced

    Run Dijkstra's algorithm with a min-heap, settling the closest unsettled vertex each round, and read the shortest paths from the parents.

    After A and C are settled, the heap's smallest pair is (3, B), so B is settled at 3. Relaxing B–D gives 3 + 5 = 8, better than 10, so D becomes 8.

    A common mix-up

    Dijkstra's algorithm settles vertices in the order of their number of edges from the start.

    In fact: It settles them in order of distance, here A, C, B, D, E, with B settled after C although both are one road from A.

  3. Cost and negative edges

    Give the cost of Dijkstra's algorithm with a binary heap, and show with a small graph that a negative edge makes it wrong.

    So the total is O((V + E) log V) with a binary heap. For a road map with a million places and a few million roads, that is tens of millions of steps, fast.

    A common mix-up

    Dijkstra's algorithm works with any weights as long as there is no negative cycle.

    In fact: Even one negative edge can make it wrong, as C came out 3 instead of 2.

Try it

If the road B–C were 3 instead of 1, what would B be after relaxing C's roads?Answer

Still 4, since 2 + 3 = 5 is not less than 4

Hint: Relax only if strictly smaller

Read the shortest path to D from the parents, and check its length.Answer

A, C, B, D, length 2 + 1 + 5 = 8

Hint: D's parent is B

In the four-vertex graph, what distance does the run give for A at the end, and why is that not enough to save C?Answer

1, since B's relaxation still lowered dist[A]; but C was settled from A's old value 2 and is never revisited

Hint: Is C ever relaxed again?

The closest unsettled vertex is now C at 2. Relax C's roads. What do B, D and E become?Answer

B becomes 3, since 2 + 1 is less than 4; D becomes 2 + 8 = 10; E becomes 2 + 10 = 12.

Which vertex is settled next, and what happens to E?Answer

D at 8; relaxing D–E gives 8 + 2 = 10, better than 12, so E becomes 10.

Shortest paths: Dijkstra's algorithm

30 minutes every month. No card.