Consider a shortest path p from vertex i to vertex j containing at most m edges.

- If i=j, then p has no edges
- If ij, then p = , where p' contains m-1 edges

By Lemma 24.1 (subpaths of shortest paths are shortest paths), p' is a shortest path from i to k, and .

Thus, the shortest path problem exhibits optimal substructure.