Dijkstra’s Algorithm

Dijkstra’s algorithm finds the shortest path from a starting vertex to every other vertex in a graph whose edge weights are all nonnegative. It keeps a tentative distance for each vertex, repeatedly visits the unvisited vertex with the smallest distance, and updates its neighbors whenever a shorter route through that vertex appears.

Before this lesson: Graph Theory Basics, Hamilton Paths and Circuits and Planar Graphs

The idea

In a weighted graph, each edge has a number called its weight: a distance, a travel time or a cost. The length of a path is the sum of its weights, and a shortest path is a path with the smallest possible length. The route with the fewest edges is not always the shortest.

Dijkstra’s algorithm grows a set of vertices whose shortest distances are known, closest first. Imagine water spreading out from the start along the edges: it reaches the nearest places first. Because no weight is negative, once the closest unvisited vertex is reached, no later detour can get there faster.

Dijkstra’s algorithm steps

  1. Give the start vertex distance 0 and every other vertex distance ∞ (not yet reached). Mark every vertex unvisited.
  2. Choose the unvisited vertex u with the smallest tentative distance, and mark it visited. Its distance is now final.
  3. For each unvisited neighbor v of u, compute d(u) + w(u, v). If this is smaller than d(v), replace d(v) with it and record u as the previous vertex of v. This step is called relaxing the edge.
  4. Repeat steps 2 and 3 until every vertex is visited (or until the target vertex is visited, if you only need one path).
  5. To read a shortest path, start at the target and follow the previous vertices back to the start.
d(v) \leftarrow \min\big(d(v),\ d(u) + w(u, v)\big)

The algorithm works the same way on directed graphs, using only edges that leave u. It requires every weight to be nonnegative; Example 4 shows what goes wrong otherwise.

Dijkstra’s algorithm examples

Example 1: Shortest distances from A

A graph has vertices A to F and weighted edges AB = 4, AC = 2, BC = 1, BD = 5, CD = 8, CE = 10, DE = 2, DF = 6 and EF = 3. Find the shortest distance from A to every vertex.

d(A) = 0

Start: A gets 0, and every other vertex gets ∞.

d(B) = 0 + 4 = 4,\ d(C) = 0 + 2 = 2

Visit A (smallest, 0) and relax AB and AC.

d(B) = \min(4,\ 2 + 1) = 3

Visit C (smallest, 2); the route through C to B is shorter, so update B.

d(D) = 2 + 8 = 10,\ d(E) = 2 + 10 = 12

Still at C: relax CD and CE.

d(D) = \min(10,\ 3 + 5) = 8

Visit B (smallest, 3) and relax BD; the new route is shorter.

d(E) = \min(12,\ 8 + 2) = 10

Visit D (smallest, 8) and relax DE.

d(F) = 8 + 6 = 14

Still at D: relax DF.

d(F) = \min(14,\ 10 + 3) = 13

Visit E (smallest, 10) and relax EF.

d(F) = 13

Visit F, the last vertex; all distances are final.

VisitABCDEF
start0\infty\infty\infty\infty\infty
A (0)done42\infty\infty\infty
C (2)done3done1012\infty
B (3)donedonedone812\infty
D (8)donedonedonedone1014
E (10)donedonedonedonedone13
F (13)donedonedonedonedonedone

Answer: from A, the shortest distances are B = 3, C = 2, D = 8, E = 10 and F = 13.

Example 2: Reading the shortest path

Use the previous vertices from Example 1 to find the shortest path from A to F.

F \leftarrow E

F’s distance last changed when E was visited, so E is the previous vertex of F.

E \leftarrow D

E’s distance last changed at D.

D \leftarrow B

D’s distance last changed at B.

B \leftarrow C

B’s distance last changed at C.

C \leftarrow A

C’s distance was set at A, the start.

A, C, B, D, E, F

Reverse the chain to read the path from A.

2 + 1 + 5 + 2 + 3 = 13

Check: the weights along the path add to d(F).

A weighted graph with vertices A to F. Edge weights: AB 4, AC 2, BC 1, BD 5, CD 8, CE 10, DE 2, DF 6, EF 3. The shortest path from A to F, A to C to B to D to E to F, is drawn with thick solid blue edges; the other edges are thin dashed gray. Each vertex shows its final distance from A: A 0, B 3, C 2, D 8, E 10, F 13.
The shortest route from A to F has five edges and length 13; the two-edge route A, B, D is longer to D (9) than A, C, B, D (8).

Answer: A, C, B, D, E, F, with length 13. It uses five edges, while the route A, B, D, F uses only three but has length 4 + 5 + 6 = 15.

Example 3: One-way streets (a directed graph)

A directed graph has edges S→A = 5, S→B = 2, B→A = 1, A→T = 1 and B→T = 7. Find the shortest path from S to T.

d(A) = 5,\ d(B) = 2

Visit S (0) and relax its two outgoing edges.

d(A) = \min(5,\ 2 + 1) = 3

Visit B (smallest, 2) and relax B→A.

d(T) = 2 + 7 = 9

Still at B: relax B→T.

d(T) = \min(9,\ 3 + 1) = 4

Visit A (smallest, 3) and relax A→T.

S, B, A, T

Visit T (4); follow the previous vertices T ← A ← B ← S.

Answer: S, B, A, T, with length 2 + 1 + 1 = 4. The direct-looking route S, A, T has length 6, and S, B, T has length 9.

Example 4: Why negative weights break the algorithm

A directed graph has edges A→B = 2, A→C = 3 and C→B = −2. Run Dijkstra’s algorithm from A and compare with the true shortest distance to B.

d(B) = 2,\ d(C) = 3

Visit A and relax both edges.

d(B) = 2 \text{ (final)}

Visit B (smallest, 2); the algorithm marks it final.

3 + (-2) = 1

Visit C (3). The route A, C, B has length 1, but B is already final.

1 \lt 2

The true shortest distance to B is 1, not 2.

Answer: Dijkstra’s algorithm reports 2, but the true distance is 1. A negative edge can make a longer-looking route shorter after a vertex is already final, so the algorithm needs nonnegative weights. The Bellman–Ford algorithm handles negative weights.

Example 5: The fastest delivery route (word problem)

A delivery van starts at warehouse W. Drive times in minutes are WX = 7, WY = 3, XY = 2, XZ = 4, YZ = 8, XT = 6 and ZT = 1, and every road runs both ways. What is the fastest route from W to stop T?

d(X) = 7,\ d(Y) = 3

Visit W (0) and relax WX and WY.

d(X) = \min(7,\ 3 + 2) = 5

Visit Y (smallest, 3); going through Y reaches X sooner.

d(Z) = 3 + 8 = 11

Still at Y: relax YZ.

d(Z) = \min(11,\ 5 + 4) = 9

Visit X (smallest, 5) and relax XZ.

d(T) = 5 + 6 = 11

Still at X: relax XT.

d(T) = \min(11,\ 9 + 1) = 10

Visit Z (smallest, 9) and relax ZT.

W, Y, X, Z, T

Visit T (10); follow T ← Z ← X ← Y ← W.

Answer: W, Y, X, Z, T takes 3 + 2 + 4 + 1 = 10 minutes, one minute faster than W, Y, X, T.

Common mistakes

MistakeExample of the errorFix
Moving to the nearest neighbor instead of the nearest unvisited vertex overallAfter visiting C in Example 1, jumping to D because CD is the next edgeAlways visit the unvisited vertex with the smallest tentative distance from the start, wherever it is.
Adding the edge weight to the wrong distanceUpdating B with 0 + 1 in Example 1The new distance is d(u) + w(u, v), using the distance of the vertex just visited: 2 + 1 = 3.
Not updating when a shorter route appearsLeaving d(B) = 4 after visiting CCompare and keep the smaller value: min(4, 3) = 3.
Changing a vertex that is already finalLowering d(A) after A was visitedA visited vertex is final; only unvisited vertices are updated.
Using the algorithm with negative weightsTrusting d(B) = 2 in Example 4Dijkstra’s algorithm needs nonnegative weights; use Bellman–Ford otherwise.
Recording distances but not previous verticesKnowing d(F) = 13 but not the routeRecord the previous vertex each time a distance improves, then trace back.

Related ideas

Dijkstra’s algorithm finds shortest paths from one starting vertex. To connect every vertex at the lowest total cost instead, use the minimum spanning tree algorithms of Kruskal and Prim.

Efficiency

With n vertices, the simple version above checks every unvisited vertex at each step, which takes about n^2 steps. Storing tentative distances in a priority queue (a heap) brings the time down to about (n + m)\log n for a graph with m edges, which is what map and network software use.

Other shortest-path methods

If every edge has weight 1, Dijkstra’s algorithm becomes breadth-first search, which simply counts edges. The Bellman–Ford algorithm allows negative weights, and the Floyd–Warshall algorithm finds shortest distances between all pairs of vertices at once. The A* search adds a distance estimate to the target so that it explores fewer vertices.

Shortest-path trees

The edges from each vertex to its previous vertex form a shortest-path tree rooted at the start. It holds the shortest route to every vertex at once. Trees are the subject of the next unit, starting with trees in graph theory.

Practice problems

  1. In the graph of Example 1, what is the shortest distance from A to D, and which path gives it?
  2. A graph has edges AB = 3, AC = 1, CB = 1, BD = 2 and CD = 5. Find the shortest distance from A to each vertex.
  3. In Problem 2, list the vertices in the order Dijkstra’s algorithm visits them.
  4. A graph has edges PQ = 1, QR = 1, RS = 1, SP = 5 and PR = 3. Find the shortest distance from P to S.
  5. A directed graph has edges X→Y = 4, X→Z = 1, Z→Y = 2, Y→W = 1 and Z→W = 6. Find the shortest path from X to W.
  6. A directed graph has edges A→B = 1, A→C = 4 and C→B = −4. What does Dijkstra’s algorithm report for B, and what is the true shortest distance?
  7. Every edge of the 6-cycle 1, 2, 3, 4, 5, 6, 1 has weight 1. What is the shortest distance from vertex 1 to vertex 4?
  8. Flights cost HJ = $120, HL = $90, LJ = $20, JK = $150 and LK = $200, in either direction. Find the cheapest way to fly from H to K.
  9. In Example 1, suppose the weight of BC changes from 1 to 6. What is the new shortest distance from A to F, and which path gives it?
  10. In Problem 2, give the previous vertex of B, C and D, and use them to write the shortest path from A to D.
Show answers

1.

d(D) = 8

Read the final distance of D from Example 1.

A, C, B, D

Trace back: D ← B ← C ← A.

2 + 1 + 5 = 8

Check the weights along the path.

Answer: 8, along A, C, B, D

2.

d(B) = 3,\ d(C) = 1

Visit A (0) and relax AB and AC.

d(B) = \min(3,\ 1 + 1) = 2

Visit C (1) and relax CB.

d(D) = 1 + 5 = 6

Still at C: relax CD.

d(D) = \min(6,\ 2 + 2) = 4

Visit B (2) and relax BD; then visit D (4).

Answer: d(B) = 2, d(C) = 1, d(D) = 4

3.

0,\ 1,\ 2,\ 4

The final distances of A, C, B and D increase in this order.

Answer: A, C, B, D

4.

d(Q) = 1,\ d(R) = 3,\ d(S) = 5

Visit P (0) and relax PQ, PR and PS.

d(R) = \min(3,\ 1 + 1) = 2

Visit Q (1) and relax QR.

d(S) = \min(5,\ 2 + 1) = 3

Visit R (2) and relax RS; then visit S (3).

Answer: 3, along P, Q, R, S

5.

d(Y) = 4,\ d(Z) = 1

Visit X (0) and relax its outgoing edges.

d(Y) = \min(4,\ 1 + 2) = 3

Visit Z (1) and relax Z→Y.

d(W) = 1 + 6 = 7

Still at Z: relax Z→W.

d(W) = \min(7,\ 3 + 1) = 4

Visit Y (3) and relax Y→W; then visit W (4).

Answer: X, Z, Y, W, with length 4

6.

d(B) = 1 \text{ (final)}

Visit A, then B (smallest, 1); B is marked final.

4 + (-4) = 0

The route A, C, B has length 0.

Answer: Dijkstra’s algorithm reports 1, but the true shortest distance is 0

7.

1, 2, 3, 4

Go one way around the cycle: 3 edges.

1, 6, 5, 4

The other way is also 3 edges.

Answer: 3

8.

d(J) = 120,\ d(L) = 90

Visit H (0) and relax HJ and HL.

d(J) = \min(120,\ 90 + 20) = 110

Visit L (90) and relax LJ.

d(K) = 90 + 200 = 290

Still at L: relax LK.

d(K) = \min(290,\ 110 + 150) = 260

Visit J (110) and relax JK; then visit K (260).

Answer: H, L, J, K for $260

9.

d(B) = 4,\ d(C) = 2

Visit A (0) and relax AB and AC.

d(B) = \min(4,\ 2 + 6) = 4

Visit C (2); the new BC no longer helps.

d(D) = 10,\ d(E) = 12

Still at C: relax CD (2 + 8) and CE (2 + 10).

d(D) = \min(10,\ 4 + 5) = 9

Visit B (4) and relax BD.

d(E) = \min(12,\ 9 + 2) = 11,\ d(F) = 9 + 6 = 15

Visit D (9) and relax DE and DF.

d(F) = \min(15,\ 11 + 3) = 14

Visit E (11) and relax EF; then visit F (14).

Answer: 14, along A, B, D, E, F

10.

C \leftarrow A,\ B \leftarrow C,\ D \leftarrow B

Each vertex’s last improvement came from these vertices.

A, C, B, D

Trace back from D and reverse.

Answer: previous vertices B ← C, C ← A, D ← B; path A, C, B, D

Dijkstra’s algorithm FAQ

What does Dijkstra’s algorithm do?

It finds the shortest path from one starting vertex to every other vertex in a weighted graph with nonnegative edge weights, along with the length of each path.

Why doesn’t Dijkstra’s algorithm work with negative weights?

It marks a vertex as final as soon as it is the closest unvisited vertex. A negative edge found later could still make a route to that vertex shorter, but the algorithm never goes back to fix it.

Does Dijkstra’s algorithm work on directed graphs?

Yes. When you visit a vertex, relax only the edges that leave it. Example 3 is a directed graph.

Is Dijkstra’s algorithm greedy?

Yes. At each step it makes the choice that looks best right now, visiting the closest unvisited vertex, and with nonnegative weights that choice is always correct.

Summary

  • Dijkstra’s algorithm finds shortest paths from one start vertex when all weights are nonnegative.
  • Start with d = 0 at the start and ∞ elsewhere; repeatedly visit the unvisited vertex with the smallest d.
  • Relax each edge from the visited vertex: d(v) becomes min(d(v), d(u) + w(u, v)).
  • Record previous vertices, then trace back from the target to read the path.
  • Negative weights can make it give wrong answers; use Bellman–Ford for those graphs.

Previous lesson: Planar Graphs

Next lesson: Trees in Graph Theory

Back to the Discrete Math Index