Applied Combinatorics VII: Minimum Spanning Trees and Dijkstra's AlgorithmTextbook
Motivation
Two optimization problems on weighted networks sit at the base of operations research and algorithm design. The first asks for the cheapest way to connect every node of a network, such as a cable, pipeline or communication network. The answer is a minimum weight spanning tree. The second asks for the shortest route from a depot to every other node of a road or data network, the single-source shortest path problem. Chapter 12 of Keller and Trotter's Applied Combinatorics (appliedcombinatorics.org, CC BY-SA 4.0) treats both. It proves the structural lemmas behind the greedy spanning tree algorithms of Kruskal (1956) and Prim (1957), and the correctness of the shortest path algorithm of Dijkstra (1959).
The minimum spanning tree problem goes back to Borůvka (1926), who designed an electrical network for Moravia. Kruskal and Prim gave the two greedy algorithms taught today, and Dijkstra's 1959 note treated both problems. Dijkstra's shortest path method, with heap-based refinements such as Fredman and Tarjan (1987), remains the standard solver for non-negative lengths and a building block of routing, scheduling and network flow codes.
Setting
A graph has a finite vertex set and a set of 2-element subsets of . A weight is attached to each edge, and a set of edges has weight . A spanning forest of is an acyclic graph with . A spanning tree is a spanning forest that is connected. The weight of a spanning tree is the weight of its edge set. In Lean these are SimpleGraph V with [Fintype V], IsSpanningForest G H ( and acyclic), IsSpanningTree G T ( and a tree), and weight w T for a weight w : Sym2 V → ℕ.
A digraph has with for every directed edge . Each directed edge has a length . The length is extended by for non-edges. A directed path from to is a sequence of distinct vertices in which consecutive pairs are directed edges. Its length is . The distance is the minimum length of a directed path from to , and is when no such path exists. A shortest path is a directed path attaining it. In Lean this is WeightedDigraph V with ext, IsDirPath, pathLength, dist and IsShortestPath.
Dijkstra's algorithm (Algorithm 12.14) with root and keeps a sequence of permanent vertices, a value and a sequence for each vertex. Step 1 sets , , , and , for . Step with scans from the last permanent vertex . For every temporary it sets , and on a strict decrease it replaces by followed by . Each step ends by appending to a temporary vertex of minimum , chosen arbitrarily among ties. The algorithm halts at Step . DijkstraRun G r i s holds when some sequence of admissible choices leads to state s at the start of Step .
Formalization targets
Goal: correctness of Dijkstra's algorithm (Theorem 12.18)
For every halted state of every run, and every vertex ,
Milestones
- Proposition 12.3. A spanning forest of a graph on vertices has and exactly components. It is a spanning tree if and only if .
- Proposition 12.4 (Exchange Principle). Let be a spanning tree and . Then contains a unique path , and replacing any edge of it by gives a spanning tree.
- Lemma 12.6. In a connected weighted graph, let be a spanning forest and a component of . A minimum weight edge leaving lies in some spanning tree that has minimum weight among the spanning trees containing .
- Proposition 12.16. Every prefix and every suffix of a shortest path is a shortest path.
- Proposition 12.17. When the algorithm halts, .
Milestones 4 and 5 are the two statements the book's proof of the goal rests on. Milestones 1–3 are the spanning tree half of the chapter. Lemma 12.6 is the result from which the book derives the correctness of Kruskal's and Prim's algorithms.
Significance
Theorem 12.18 certifies that one pass of steps computes all distances from and a shortest path tree, with no condition on the digraph beyond non-negative lengths. Lemma 12.6 is the cut property. Every greedy minimum spanning tree method (Kruskal, Prim, Borůvka) is an instance of it, and the exchange principle is the matroid basis-exchange axiom specialised to the graphic matroid.
All of these results are classical and proved. None is formalized in this form on the platform. Mathlib has spanning trees of connected graphs, uniqueness of paths in acyclic graphs, and the edge count of a tree. It has no edge–component count for forests, no exchange principle, no weighted spanning trees, and no Dijkstra. On Prove2Me, FamousTheorems.tree_card_edges_6b and ClassicalGaps.isAcyclic_edges_eq_card_sub_one_imp_connected cover only the tree case of Proposition 12.3. KServer.mst_cut_property is a cut property for complete graphs encoded by parent maps, a different statement. The label-correcting algorithm of Dynamic Programming and Optimal Control II (BertsekasDP.label_correcting_*) is a different algorithm: it keeps an open list and scans in arbitrary order, not by minimum label.
Difficulty
The goal is a statement about the final state of a run, but the facts it depends on only become visible across steps: a permanent vertex's and never change again, and is always the length of the current . None of this is recorded in the final state itself. An argument over the steps of the run has to show that each remains a path with distinct vertices, including when edges of length allow ties. It also has to handle the value , where and a comparison between two infinite values never counts as a decrease. Tie-breaking is arbitrary, so no argument may depend on which minimum is chosen. For Lemma 12.6 the difficulty is the exchange step: removing an edge of a tree path and adding a crossing edge must again give a tree that still contains the forest , and this is a statement about cycles and components, not about counts.
Formalization scope
- Graphs are
SimpleGraph Vover aFintype V. Weights areSym2 V → ℕ(the book's ; values off are never used). Acyclic, tree and connected components are Mathlib's. In Proposition 12.3, is written and is assumed, which the bound presupposes. - Lemma 12.6 assumes connected, the section's standing assumption (p. 239). The page's "to avoid trivialities, we assume " is not imposed, because the statement holds for every . The crossing edge may have either endpoint in .
- Lengths in the digraph are
ℕ, and and distances areℕ∞, where is⊤, never a large finite number. A version with real orℝ≥0lengths would be a generalization and is not what is asked. - Dijkstra's algorithm is defined step by step exactly as on pp. 246–247, including and for non-neighbours at Step 1. The goal quantifies over every halted state, so it holds for every tie-breaking. A halted state always exists; a sorry-free check of this is in the workspace. For a vertex not reachable from the book is silent. The distance there is read as , and the shortest-path conclusion is asserted only at finite distance.
- A trivializing formalization is ruled out: is computed by the update rule of Algorithm 12.14, not defined as the distance, and the theorem is not stated for an arbitrary procedure satisfying its own conclusion.
- The book uses no bounds or approximate constants in these statements, so there are no constants to instantiate.
- Reusable infrastructure: a list-based theory of directed paths and distances in
ℕ∞, the invariants of Dijkstra's algorithm, and forest edge counting. Contributions of general lemmas (walks shortcut to paths without increasing length, component counts under edge insertion) are welcome.
Selected references
- M. T. Keller and W. T. Trotter, Applied Combinatorics, 2017 Edition, Chapter 12. https://www.appliedcombinatorics.org/
- E. W. Dijkstra, "A note on two problems in connexion with graphs", Numerische Mathematik 1 (1959) 269–271. https://doi.org/10.1007/BF01386390
- J. B. Kruskal, "On the shortest spanning subtree of a graph and the traveling salesman problem", Proc. AMS 7 (1956) 48–50. https://doi.org/10.1090/S0002-9939-1956-0078686-7
- R. C. Prim, "Shortest connection networks and some generalizations", Bell System Technical Journal 36 (1957) 1389–1401. https://doi.org/10.1002/j.1538-7305.1957.tb01515.x
- M. L. Fredman and R. E. Tarjan, "Fibonacci heaps and their uses in improved network optimization algorithms", J. ACM 34 (1987) 596–615. https://doi.org/10.1145/28869.28874
- O. Borůvka, "O jistém problému minimálním", Práce Moravské přírodovědecké společnosti 3 (1926) 37–58. https://dml.cz/handle/10338.dmlcz/500114