Discrete Mathematics and Proof

Finding a shortest path

A map gives the miles on every road. From one town, what is the shortest drive to every other town, and how can it be found without listing every possible route?

Where you are 10 in Introductory graph theory

When you reach for it

You have a weighted graph, or a weighted directed graph, whose weights are all \ge 0: miles, minutes or dollars. You have a start s, and you want the weighted distance d_w(s, v) of Weighted and directed graphs to every vertex v, with a cheapest route. Listing every path works for five vertices, but in a network where every two of 10 places are joined there are 109{,}601 paths between two of them (Example 7). Dijkstra’s algorithm finds every distance from s in about v rounds for v vertices.

This lesson takes two days. Day 1 is the method, the route it records, and the proof that it is right (Theorem 1, Examples 1 to 3). Day 2 is the tree of routes, the subpath property, directed graphs, unweighted graphs, negative weights and the count of the work (Theorems 2 and 3, Examples 4 to 7).

Walks, paths and the plain distance d are those of Walks, paths, cycles and connected graphs. Weights here may be 0. A cheapest walk may then repeat a vertex, but cutting out the repeat removes a stretch of weight \ge 0, so d_w(s, v) is still the least weight of a path from s to v.

The procedure

Day 1: settling one vertex a round

The method keeps a set S of settled vertices, whose distances from s are known. Every vertex v carries a label \ell(v), the weight of the cheapest route to v found so far, and a predecessor p(v), the vertex before v on that route. At the start S is empty, \ell(s) = 0, and \ell(v) = \infty for every other v.

Each round settles one unsettled vertex u of least label, any one if several tie, and then scans it: for each unsettled neighbor x of u, the route to u followed by the edge ux is compared with the best route to x so far,

\ell(x) \text{ against } \ell(u) + w(ux).

If \ell(u) + w(ux) < \ell(x), strictly, the label drops to \ell(u) + w(ux) and p(x) becomes u. The run stops when every vertex is settled or the least unsettled label is \infty; the vertices left then cannot be reached from s (Exercise 7).

Only u is scanned because every other settled vertex was scanned when it was settled. Settling the least label is the heart of the method: any route to that vertex leaves the settled set somewhere, and Theorem 1 shows that leaving already costs at least that label.

Theorem 1 (settled labels are distances). Let every weight be \ge 0 and run the method from s. (a) When a vertex u is settled, \ell(u) = d_w(s, u), and the label never changes after. (b) For a settled u \ne s, following predecessors back from u reaches s without repeating a vertex, and this path, read from s, is a cheapest path from s to u.

Proof. Only unsettled vertices are scanned, so a settled label never changes. Next, every finite label is the weight of a walk from s: at the start the only finite label is \ell(s) = 0, the weight of the walk (s), and a change sets \ell(x) = \ell(u) + w(ux), the weight of a walk to u extended by the edge ux. So \ell(x) \ge d_w(s, x) whenever \ell(x) is finite.

For (a), induct on the order of settling, u_1, u_2, \ldots. First u_1 = s, with \ell(s) = 0 = d_w(s, s). Suppose u_1, \ldots, u_{j-1} were settled with correct labels, let u = u_j, and let S = \{u_1, \ldots, u_{j-1}\}. The run never settles a label \infty, so u is reachable; let P be a cheapest path from s to u. It starts in S and ends outside, so it has a first vertex y outside S, with x the vertex before it. Then x = u_i for some i < j, so \ell(x) = d_w(s, x), and when x was scanned y was unsettled, so \ell(y) became at most d_w(s, x) + w(xy); labels never rise. The part of P from s to x is a walk, so it weighs at least d_w(s, x). Therefore

\begin{aligned} \ell(y) &\le d_w(s, x) + w(xy) \\ &\le w(P \text{ up to } y) \\ &\le w(P) = d_w(s, u), \end{aligned}

where the last \le holds because the rest of P, from y to u, has weights \ge 0. The run chose u with least label among unsettled vertices, and y is unsettled, so \ell(u) \le \ell(y) \le d_w(s, u). With \ell(u) \ge d_w(s, u), equality holds.

For (b), p(x) = u is set while u is scanned, so p(x) was settled before x. Going back along predecessors, the order of settling strictly falls, so no vertex repeats, and the chain stops only at s, the one settled vertex whose label was never lowered. When p(x) = u was last set, \ell(x) = \ell(u) + w(ux), and neither number changes afterward. Adding along the chain, the path weighs \ell(u) = d_w(s, u). \square

The proof uses w \ge 0 in one place only: the rest of P weighs at least 0. Example 6 shows the method failing without it.

Day 2: the tree of routes and the subpath property

Theorem 2 (the shortest-path tree). After a run from s, let R be the set of settled vertices. The edges p(x)x for x \in R, x \ne s, form a tree T with vertex set R, and for each x \in R the path from s to x in T is a cheapest path in G.

Proof. Each x \ne s in R gives one edge, and two vertices never give the same edge, since p(x) = y and p(y) = x would make each settled before the other. So T has \lvert R \rvert - 1 edges. Every x is joined to s by its predecessor chain, so T is connected, and a connected graph with one edge fewer than its vertices is a tree (Trees, Result 7). A tree has exactly one path between two vertices, so the path from s to x in T is the predecessor chain, a cheapest path by Theorem 1(b). \square

The tree is a shortest-path tree from s. One predecessor per vertex is enough to store every cheapest route because of the next fact.

Theorem 3 (the subpath property). Let every weight be \ge 0, and let P = (v_0, v_1, \ldots, v_k) be a cheapest path from v_0 to v_k. Then for 0 \le i \le j \le k, the part (v_i, \ldots, v_j) is a cheapest path from v_i to v_j.

Proof. Suppose some walk Q from v_i to v_j weighs less than (v_i, \ldots, v_j). Follow P from v_0 to v_i, then Q, then P from v_j to v_k. This is a walk from v_0 to v_k whose weight is w(P) minus the weight of (v_i, \ldots, v_j) plus w(Q), less than w(P) = d_w(v_0, v_k). No walk weighs less than d_w(v_0, v_k), a contradiction. \square

Directed graphs. In a weighted directed graph the run scans, from u, only the arcs (u, x) out of u. Theorems 1 to 3 hold as stated with directed paths, since each proof follows a path forward from its start: the step from x to y on P is an arc (x, y), scanned from x. A run from s gives distances from s, not to s (Example 5).

Unweighted graphs. Give every edge weight 1. Then d_w = d, every label is a layer number, and by Exercise 6 the run settles all of layer 0, then layer 1, and so on. This is the layer method of Walks, paths, cycles and connected graphs, grown one vertex at a time.

Negative weights. With a negative weight the proof of Theorem 1 breaks at its one use of w \ge 0, and the answers can be wrong (Example 6). The Bellman-Ford method, which passes over every edge v - 1 times, handles negative weights when no closed walk weighs less than 0; it is not taken up here.

Counting the work. For v vertices and e edges there are at most v rounds. Finding the least label looks at each unsettled label once, at most \displaystyle v + (v - 1) + \cdots + 1 = \frac{v(v+1)}{2} looks in all. Scanning u checks each edge at u, and each vertex is scanned once, so the checks total the sum of the degrees, 2e, by the handshake count of Degree, the handshake lemma and complete graphs. Since \displaystyle e \le \frac{v(v-1)}{2}, the work is at most about \displaystyle \frac{3}{2}v^2: it grows like v^2, while the number of paths can grow like (v - 2)!.

Worked examples

Example 1

Day 1: an ambulance from station A

Nobuhle dispatches the county ambulance from its station in town A. Figure 1 gives the road miles. Find the shortest drive from A to every town, and the route to F.

7 3 2 4 8 5 3 6 10 A B C D E F
Figure 1County roads in miles. The direct road AB is 7 miles, but (A, C, B) is 5.
Solution

Row k of the table is the state after round k. An entry 7_A means label 7 with predecessor A; the boxed entry is the vertex settled that round, and its column is blank after.

ABCDEF
\boxed{0}7_A3_A\infty\infty\infty
5_C\boxed{3_A}11_C8_C\infty
\boxed{5_C}9_B8_C\infty
9_B\boxed{8_C}18_E
\boxed{9_B}15_D
\boxed{15_D}

Round 2 settles C, the least label; through C, B costs 3 + 2 = 5 < 7, so \ell(B) drops and p(B) = C. Round 4 scans E: 8 + 3 = 11 is not below 9, so D keeps p(D) = B. Round 5 lowers F from 18 to 9 + 6 = 15.

By Theorem 1 the distances from A are 0, 5, 3, 9, 8, 15 for A to F. Going back from F: p(F) = D, p(D) = B, p(B) = C, p(C) = A, so the route is (A, C, B, D, F), with 3 + 2 + 4 + 6 = 15 miles. The road AB, at 7 > 5 = d_w(A, B), is on no cheapest route.

Example 2

Day 1: a light-rail ride with a tie

Itzamna rides the light rail of Figure 2 from stop G. Find the fastest time to every stop and every fastest route to M, and name the lines that are on no fastest route from G.

4 9 3 7 2 6 3 4 1 G H J K L M
Figure 2Riding times in minutes between light-rail stops.
Solution
GHJKLM
\boxed{0}4_G9_G\infty\infty\infty
\boxed{4_G}7_H11_H\infty\infty
\boxed{7_H}9_J13_J\infty
\boxed{9_J}12_K13_K
\boxed{12_K}13_K
\boxed{13_K}

K is first reached through H at 4 + 7 = 11, then through J at 7 + 2 = 9. In round 5, scanning L offers M the time 12 + 1 = 13, equal to its label, not less, so p(M) stays K.

The times are 0, 4, 7, 9, 12, 13 for G to M. The chain M, K, J, H, G gives (G, H, J, K, M), 4 + 3 + 2 + 4 = 13 minutes. The tie shows a second route, (G, H, J, K, L, M), 4 + 3 + 2 + 3 + 1 = 13, and these are the only two: any fastest route to M arrives from K or L, and by Theorem 3 its earlier part is the single fastest route to K or to L found above. Theorem 1 promises one cheapest route; ties are where there are more.

The lines GJ (9 > 7), HK (7 > d_w(H, K) = 5) and JL (6 > d_w(J, L) = 5) are on no fastest route at all, by Result 3 of Weighted and directed graphs.

Example 3

Day 1: two stations at once

The county opens a second station in town F of Figure 1. For each town, which station is nearer, and how far?

Solution

Add a new vertex z joined to A and to F by edges of weight 0. A path from z begins with zA or zF, so d_w(z, x) is the smaller of d_w(A, x) and d_w(F, x). The weights are still \ge 0, so Theorem 1 applies, and after z is settled and scanned the labels are \ell(A) = 0 and \ell(F) = 0. So the run may simply start with both labels 0:

ABCDEF
\boxed{0}7_A3_A\infty\infty0
7_A3_A6_F10_F\boxed{0}
5_C\boxed{3_A}6_F8_C
\boxed{5_C}6_F8_C
\boxed{6_F}8_C
\boxed{8_C}

Each predecessor chain ends at A or at F, naming the station. A serves B (5 miles), C (3) and E (8); F serves D (6). For E, the run from F alone would give 6 + 3 = 9, more than 8.

Example 4

Day 2: the tree of cheapest routes

Draw the predecessor edges of Example 1, check that they form a tree, and use it to find d_w(B, F) and d_w(C, D) without a new run.

7 3 2 4 8 5 3 6 10 A B C D E F
Figure 3The predecessors of Example 1 drawn as edges. They form a tree, and its path from A to each town is a cheapest route.
Solution

The predecessors give the edges AC, CB, BD, CE, DF (Figure 3): five edges on six vertices, connected through A, so a tree by Theorem 2.

The route (A, C, B, D, F) is a cheapest path, so by Theorem 3 its part (B, D, F) is a cheapest path from B to F, and d_w(B, F) = 4 + 6 = 10. Its part (C, B, D) gives d_w(C, D) = 2 + 4 = 6, less than the road CD of 8.

The tree’s roads total 3 + 2 + 4 + 5 + 6 = 20 miles. Swapping CE for DE gives a network of 18 miles that still reaches every town, but its route from A to E is (A, C, B, D, E), 12 miles instead of 8. The cheapest network is a different question, that of Spanning trees and minimum spanning trees.

Example 5

Day 2: conveyor belts that run one way

In a parcel sorting center, Delwyn sends bins from the loading dock at U over the conveyor belts of Figure 4. Find the shortest time to each station, and compare d_w(U, W) with d_w(W, U).

3 9 5 4 2 2 6 2 3 1 U V W X Y Z
Figure 4Conveyor belts in a sorting center, with travel times in minutes. Each belt runs one way; only Y and Z are joined both ways, by two belts.
Solution

Scanning u now follows only the arrows out of u.

UVWXYZ
\boxed{0}3_U\infty9_U\infty\infty
\boxed{3_U}8_V7_V\infty\infty
8_V\boxed{7_V}13_X\infty
\boxed{8_V}13_X10_W
11_Z\boxed{10_W}
\boxed{11_Z}

In round 3 the arc (X, W) offers 7 + 2 = 9, not below 8. In round 5 the arc (Z, Y) lowers Y to 10 + 1 = 11. In round 6 both arcs out of Y lead to settled vertices. The times are 0, 3, 8, 7, 11, 10 for U to Z, and d_w(U, W) = 8 by (U, V, W).

The run says nothing about trips back to U. Out of W the only arc goes to Z, out of Z only to Y, and (Y, U) is an arc, so (W, Z, Y, U) is the only path and d_w(W, U) = 2 + 1 + 2 = 5, not 8.

Example 6

Day 2: a rebate breaks the method

Figure 5 gives shipping legs in dollars, where a partner pays 3 dollars back on the leg from b to a. Run the method from s, compare with the true cheapest costs, and say where Theorem 1’s proof fails. Would adding 3 to every weight repair it?

2 4 −3 1 s a b t
Figure 5Shipping legs in dollars. The leg from b to a pays a 3 dollar rebate, so its weight is -3.
Solution

Round 1 settles s and gives a the label 2, b the label 4. Round 2 settles a (least label 2) and gives t the label 3. Round 3 settles t, round 4 settles b, whose arc leads to the settled a. The run reports 2 for a and 3 for t.

But (s, b, a) costs 4 - 3 = 1, and (s, b, a, t) costs 2. The method settled a too early. In the proof, with u = a, the cheapest path P = (s, b, a) leaves the settled set \{s\} at y = b, and the rest of P, from b to a, weighs -3, not \ge 0. There is no closed walk here, so the costs are well defined; the method alone is at fault.

Adding 3 makes the weights 5, 7, 0, 4. Now (s, a, t) costs 9 and (s, b, a, t) costs 11: a route with more legs gains more, so the cheapest route changes.

Example 7

Day 2: counting the work against listing paths

In the complete graph K_{10} every two of the 10 vertices are joined by an edge, each with some weight \ge 0. How many paths join two given vertices s and t, and how many looks and checks does a run from s take?

Solution

A path from s to t is fixed by its list of middle vertices, k different vertices in order from the other 8, which can be chosen in \displaystyle \frac{8!}{(8-k)!} ways. Adding over k = 0, \ldots, 8:

\begin{aligned} &1 + 8 + 56 + 336 + 1{,}680 + 6{,}720 \\ &+ 20{,}160 + 40{,}320 + 40{,}320 = 109{,}601. \end{aligned}

The run has 10 rounds. Finding the least label takes at most 10 + 9 + \cdots + 1 = 55 looks, and each of the 10 vertices, scanned once, checks its 9 edges: 90 checks. That is 145 in all, against 109{,}601 path weights to add up and compare.

Boundaries

  • Weights \ge 0. Zero weights are fine (Example 3); one negative weight can give wrong answers (Example 6).
  • One start. A run gives distances from s only. In a directed graph the distances to s can be different (Example 5), and all pairs need a run from every vertex.
  • Ties. Distances do not depend on how ties are broken; the predecessors and the tree can (Example 2).
  • Reach. Vertices that cannot be reached from s keep the label \infty and are never settled.

Where this goes next

Spanning trees and minimum spanning trees asks for the cheapest network rather than the cheapest route, and one of its methods also grows a tree from a single vertex, but adds the cheapest single edge each round instead of the cheapest total route. Negative weights call for the Bellman-Ford method.

Exercises

Day 1: running the method

  1. Run the method from E in Figure 1. Give every distance, predecessor and the order of settling, and name any town with two cheapest routes from E.
  2. Run the method from M in Figure 2. Check that d_w(M, G) agrees with Example 2.
  3. The table gives the price in dollars of a direct freight leg between five ports. Run the method from P and give the cheapest route to T.
PQRST
P629
Q634
R2348
S941
T481
  1. In Exercise 3 only the price from P to S is wanted. After which round may the run stop, and what allows it? Which vertex is unsettled then, and is its label already its distance?
  2. Stops G and M of Figure 2 both have rail yards. For each stop, find the nearer yard and the time to it.

Day 1: reasoning

  1. With all weights \ge 0, prove that the labels of the vertices, at the moment each is settled, never decrease from one round to the next.
  2. Prove that a run settles exactly the vertices that can be reached from s, and that every other vertex keeps the label \infty.

Day 2: trees, directions and layers

  1. Give the shortest-path tree from P in Exercise 3 and its total weight, and check that it is a tree.
  2. In Example 2, find every shortest-path tree from G, that is, every tree whose path from G to each stop is a fastest route.
  3. Run the method from W in Figure 4.
  4. Every edge of the graph on \{1, \ldots, 7\} with edges 12, 13, 24, 34, 35, 46, 57, 67 has weight 1. Run the method from 1, breaking ties by the smaller number. Give the labels and the tree, and compare the labels with the layers from 1.

Day 2: negative weights and counting

  1. A directed graph has arcs (s, x) of weight 4, (s, y) of 6, (y, x) of -3, (x, z) of 3 and (s, z) of 8. Run the method from s and compare with the true distances.
  2. A road network has 2{,}000 vertices and 5{,}000 edges. Bound the looks and the checks of a run, counted as in the procedure.
  3. In K_6, how many paths join two given vertices?

Day 2: reasoning

  1. With all weights positive, prove that a vertex x lies on some cheapest path from s to t if and only if d_w(s, t) = d_w(s, x) + d_w(x, t).
  2. Refute with a counterexample: “in a shortest-path tree from s, the path between any two vertices is a cheapest path between them.”
  3. In a directed graph no arc enters s, the arcs out of s may have negative weights, and all other arcs have weights \ge 0. Prove that a run from s still gives every distance from s.
Answers
  1. Order E, D, C, B, A, F. Distances and predecessors: D 3 (E), C 5 (E), B 7 (D), A 8 (C), F 9 (D). B has two: (E, D, B) and (E, C, B), both 7; C was scanned after D and offered 5 + 2 = 7, not less.
  2. Order M, L, K, J, H, G; L 1 (M), K 4 (M), J 6 (K), H 9 (J), G 13 (H). The route (M, K, J, H, G) weighs 4 + 2 + 3 + 4 = 13, matching Example 2. K ties: (M, L, K) is also 4.
  3. Order P, R, Q, S, T; R 2 (P), Q 5 (R), S 6 (R), T 7 (S). T falls from 10 to 9 to 7. Route (P, R, S, T), 2 + 4 + 1 = 7 dollars.
  4. After round 4, when S is settled: Theorem 1 makes its label 6 final. T is unsettled with label 7, which is in fact its distance, but the run has not yet shown it.
  5. Start with \ell(G) = \ell(M) = 0. H: G, 4. J: M, 6 (from G it is 7). K: M, 4. L: M, 1.
  6. Let u be settled with label m. Every unsettled label is \ge m, as u’s was least. Scanning u sets labels m + w(ux) \ge m. So every unsettled label stays \ge m, and the next vertex settled has label \ge m. \square
  7. A finite label is the weight of a walk from s (proof of Theorem 1), so an unreachable vertex keeps \infty and is never settled. If a reachable x were unsettled at the end, take a path from s to x, its first unsettled vertex y and the vertex before it, settled and so scanned: \ell(y) is finite, so the run had not stopped. \square
  8. Edges PR, RQ, RS, ST, weight 2 + 3 + 4 + 1 = 10: four edges on five vertices, all joined to P, so a tree by Trees.
  9. Two: GH, HJ, JK, KL, KM (the run’s), and the same with LM in place of KM. Every other stop has one fastest route.
  10. Z 2 (W), Y 3 (Z), U 5 (Y), V 8 (U), X 12 (V). X first gets 5 + 9 = 14, then 8 + 4 = 12.
  11. Labels 0, 1, 1, 2, 2, 3, 3 for 1 to 7, equal to the layers; tree edges 12, 13, 24, 35, 46, 57. Vertex 4 keeps p(4) = 2, since 3 offers 2, not less.
  12. The run settles s, x, y, z with labels 0, 4, 6, 7. True: d_w(s, x) = 3 by (s, y, x) and d_w(s, z) = 6 by (s, y, x, z); y is right at 6.
  13. Looks at most \displaystyle \frac{2000 \cdot 2001}{2} = 2{,}001{,}000; checks 2 \cdot 5000 = 10{,}000.
  14. 1 + 4 + 12 + 24 + 24 = 65, adding \displaystyle \frac{4!}{(4-k)!} for k = 0, \ldots, 4.
  15. If x is on a cheapest path P, its parts from s to x and from x to t are cheapest (Theorem 3), so their weights add to d_w(s, t). Conversely, a cheapest path from s to x followed by one from x to t is a walk through x of weight d_w(s, t), so a cheapest walk; with positive weights a cheapest walk is a path (Result 1 of Weighted and directed graphs). \square
  16. Triangle abc with every weight 1. The tree from a is ab, ac; its path (b, a, c) weighs 2, but d_w(b, c) = 1.
  17. Let c be large enough that w(s, x) + c \ge 0 for every arc out of s, and add c to those arcs only. No arc enters s, so a walk from s leaves s once and never returns: every walk from s gains exactly c, and the cheapest walks stay the same. Both runs settle s first, then every other label differs by exactly c, so they make the same choices. The new run is correct by Theorem 1, so the old one is too, with c subtracted. \square
kg1128 · updated 2026-10-22 · CC-BY-SA-4.0