Home / COMP2870 / Graph algorithms / Prim proof
Why Prim works
Theorem 2.4, built up from the ground
Plain English first, with the lecture notes' wording underneath.
How this page is built
The Prim proof is the top of a ladder. Each rung uses the one below it. If a lower rung is shaky, the proof reads like a wall of text. So climb it in order:
↑
Lemma 2.2 ← uses: the detour, plus "pick the smallest, then contradict"
↑
Lemma 2.1 ← uses: the detour
↑
5 basic facts ← trees, paths, cycles
The same two moves keep coming back: the detour (an edge on a cycle can go) and pick the extreme one, then contradict. Learn those two, and Kruskal's proof comes almost for free.
A · The ideas you need
Nothing here is the Prim proof yet. It's the toolkit it's built from.
What Prim claims, and why it needs a proof
In plain English: whatever connected weighted graph you give Prim, the tree it builds is a cheapest possible way to connect every vertex.
Why a proof? Prim is greedy: it grabs what's cheapest right now and never goes back. Greedy is often wrong. Paying 6p with coins {1, 3, 4} greedily gives 4+1+1 (3 coins), but 3+3 (2 coins) is better. So "it looks sensible" isn't enough.
Can greedy get stuck? (the question everyone asks)
- Prim isn't walking a route. It grows a blob, and each step it can add an edge from any vertex already in the blob. Taking a cheap edge on one side doesn't lock the other side away.
- Every vertex is mandatory. A spanning tree must reach every vertex. If the only way to reach some vertices is through expensive edges, then every spanning tree pays for them. Prim just pays at a different time.
- Where greedy does trap you is a single route (shortest paths). There you commit to where you're standing. That's a different problem, and why Dijkstra works differently.
tree cheapest edge into each vertex picked now
The 5 basic facts (rung 0)
These must be instant, each with its one-line "why". Most confusion in the Prim proof comes from one of these being shaky.
| Fact | Why, in one line | Where it's from |
|---|---|---|
| F1. A tree is connected and acyclic. | That's the definition. Always say both. | §1 |
| F2. In a tree there is exactly one path between any two vertices. | Connected ⇒ at least one path. Acyclic ⇒ at most one, since two different paths would make a cycle. | Theorem 2.3 (a)⇒(d) |
| F3. A tree plus one new edge has exactly one cycle. | Take the unique path from F2 between the new edge's ends, then come back along the new edge. | Theorem 2.3 |
| F4. Removing an edge that lies on a cycle doesn't disconnect the graph. | The rest of the cycle is a detour. | Lemma 2.1 |
| F5. A spanning tree on n vertices has n − 1 edges. | This is Theorem 2.3. | Theorem 2.3 |
Careful with F3: "a round trip" isn't automatically a cycle. Going x → y → x along the same edge is closed, but it isn't a cycle. What makes F3 work: the tree path has no repeated vertices, and the new edge isn't on that path.
See F2 and F3
See F4, the detour
edge path / cycle / detour new or removed edge
Lemma 2.1 (rung 1)
The idea: a cycle edge always has a detour, the rest of the cycle, so removing it cuts nobody off. And if removing an edge cuts nobody off, the way round plus the edge makes a cycle.
- Take a connected graph G and an edge e from x to y.Let G be a connected graph and e = xy ∈ E(G).
- (⇐) Suppose e is on a cycle C. The rest of the cycle is a way from x to y that avoids e: a detour.Suppose e lies on a cycle C. Then C − e is an (x,y)-path P with e ∉ E(P).
- Any route between two vertices that used e can use the detour instead. So even without e, everyone is still connected: e is not a cut-edge.Let u, v ∈ V(G) and let Q be a (u,v)-path. If e ∈ E(Q), replacing e by P gives a (u,v)-walk in G − e. So G − e is connected.
- (⇒) Suppose removing e cuts nobody off. Then there's still a way from x to y without e.Suppose e is not a cut-edge. Then G − e is connected, so there is an (x,y)-path P in G − e.
- That way round, plus e itself, is a cycle through e. □P has at least 2 edges, so P + e is a cycle containing e. □
From COMP1870. The notes use it without proof, but you should be able to explain it.
Lemma 2.2 (rung 2)
The idea: (⇒) a spanning tree already links everyone. (⇐) Take the connected, all-vertices subgraph with the fewest edges. If it had a cycle, you could delete a cycle edge (F4) and still be connected with fewer edges. That's impossible, so it has no cycle: it's a spanning tree.
- The statement is about every graph, so start with "take any graph G".Let G be a graph.
- (⇒) Assume G has a spanning tree, and name it T.Assume G has a spanning tree T.
- T is part of G, and it reaches every vertex and links them all. So G is connected.Since T ⊆ G, every path in T is a path in G. Since T is spanning and connected, G is connected.
- (⇐) Assume G is connected. Of all subgraphs that keep every vertex and are connected, take one with the fewest edges, T. (G itself is one, so such a T exists.)Assume G is connected. Let T be a connected spanning subgraph of G with the minimum number of edges.
- Pretend T has a cycle, C. (T, not G! G is allowed to have cycles.) Pick an edge e on it.Suppose T contains a cycle C, and let e ∈ E(C).
- e has a detour (F4, Lemma 2.1), so T − e is still connected. It still has every vertex, and it has fewer edges.By Lemma 2.1, T − e is connected. T − e is spanning, with fewer edges than T.
- That contradicts picking T with the fewest edges. So T has no cycle: it's a spanning tree. □This contradicts the choice of T. So T is a spanning tree of G. □
This "pick the one with the fewest / most …, then show a better one exists" move is exactly what Kruskal's proof does too.
The label fact (what Prim's labels really mean)
tree so far edges leaving the tree cheapest one
The trick, on a tiny example
Square A–B–C–D with weights AB = 1, AD = 2, BC = 2, DC = 1. There are two best trees, both of weight 4: Plan P = {AB, BC, CD} and Plan Q = {AB, AD, DC}.
Prim's tree rest of the best tree Prim's new edge loop removed
B · The proof
Now every piece is familiar. Here's how they fit together.
The whole proof in one minute
The analogy: bridges over a river. Picture the part Prim has built so far as one side of a river, and every vertex it hasn't reached yet as the other side. The edges between the two sides are bridges. Each step, Prim builds the cheapest bridge across.
Somewhere there's a best possible plan for the whole network. The worry: what if the best plan uses a different bridge? The answer is that the best plan still has to cross the river somewhere, so it has its own bridge e′. Swap it for Prim's bridge e. Everyone is still connected, and since Prim's bridge was the cheapest, the plan costs no more. So there's always a best plan that agrees with Prim. Prim never makes a wrong move.
Every step, in one line each
- Goal: show Prim never makes a wrong move, i.e. after every step, what it has built can still be finished into a best tree.
- Start: one vertex, no edges. A best tree exists (G is connected), and one vertex fits inside any tree. ✓
- Assume the last move was fine: there's a best plan T* containing everything so far. Prim now adds bridge e to reach a new vertex x.
- Claim 1: it's still a tree. x hangs off one edge, like a leaf, so no loop can form.
- Claim 2, easy case: the best plan already uses e. Nothing to do. ✓
- Otherwise, add e to the plan: a tree plus one edge always makes exactly one loop.
- The loop crosses the river twice: out over e, and back over some other bridge e′.
- Remove e′: e′ was on a loop, so nobody gets cut off. It's still a spanning tree.
- Compare costs: e was the cheapest bridge, so e′ costs at least as much. The new plan costs no more than the best, so it is a best plan, and it contains Prim's move. ✓
- Finish: this held at every step. At the end every vertex is in, so Prim's tree is a best tree. □
The shape of the proof
She stressed this in the lecture: at every point of a proof, be clear what statement you're trying to prove right now. Here is the whole proof as a map of goals. Each goal is proved by the ones indented under it.
Below, each of these is explained slowly, with pictures.
The plan: a promise that never breaks
For Prim, the promise is:
"What Prim has built so far is part of some best tree (an MST)."
We show it's true at the start and that each step keeps it true. This is induction, like dominoes: push the first one (the base case), and show that each domino knocks over the next (the induction step). Then they all fall.
Why "some" best tree matters. There can be several MSTs. The promise doesn't say which one. It might be a different MST after each step, and that freedom is exactly what makes the swap trick in Claim 2 work.
We prove by induction on the size of VT that T is a subtree of a minimum weight spanning tree of G spanning the vertices of VT.Who's who
| G | the connected weighted graph Prim is run on |
| T | the tree Prim has built so far |
| VT | the vertices already in T (drawn filled in the animations) |
| x | the vertex Prim is about to add: the outside vertex with the smallest label (line 5) |
| e = xy | the edge that gave x its label. y is already in the tree, and x is outside |
| T + e | the tree after Prim adds x and e (lines 6–7) |
| T* | a best tree (MST) that contains T. The promise says one exists |
| C | the loop you get by adding e to T* |
| e′ | another edge on that loop that crosses between VT and the outside |
| T** | T* with e swapped in and e′ swapped out |
Base case: the first domino
- At the start, the tree is a single vertex u and has no edges.Base case: |VT| = 1.
- G is connected, so it has a spanning tree. That was Lemma 2.2, last lecture.Since G is connected, by Lemma 2.2 it has a spanning tree.
- There are only finitely many spanning trees, so at least one of them is cheapest: an MST. Call it T*.Hence G has a minimum weight spanning tree T*.
- T* is spanning, so it contains every vertex, u included. A single vertex is trivially part of T*. The promise holds at the start.By lines 1 and 2, T consists of a single vertex, so T is a subtree of any minimum weight spanning tree of G.
Induction step. Claim 1: adding e keeps it a tree
Now assume the promise holds right before Prim adds x (that's the induction hypothesis), and show it still holds right after. The step splits into two claims. The first is the easy one.
- By the promise, T is a tree.By the induction hypothesis, T is a tree.
- e joins the new vertex x to a vertex y that is already in T. So T + e is still in one piece.Edge e connects vertex x (from line 5) to a vertex y in T. So T + e is connected.
- x has only one edge in T + e, so no loop can pass through x. T had no loops before, so T + e has none. Connected with no loops means it's a tree.Since x is of degree 1 in T + e, there cannot be a cycle that goes through x in T + e. Therefore T + e is connected and acyclic, i.e. it is a tree.
Induction step. Claim 2: it's still inside some best tree
See it
T (built so far) rest of T* e loop C e′ (swapped out)
6a. The easy case
- If T* already contains e, then T + e is inside T*. The promise holds. Done.By Claim 1, T + e is a tree, and so if e ∈ E(T*), then we are done.
6b. The loop
- Suppose T* doesn't contain e. Let x and y be the two ends of e.Now suppose that e ∉ E(T*). Let x and y be the endnodes of e.
- T* is a tree, so there's exactly one route between x and y in it.Since T* is a tree, by Theorem 2.3 ((a) ⇒ (d)), there is a unique (x,y)-path in T*.
- That route plus e forms exactly one loop, C.Therefore, T* + e contains a unique cycle C.
6c. The loop must cross back
- e goes from inside the tree (y) to outside (x). Walking round the loop from y, you leave via e and must eventually get back to y. So some other edge on the loop, e′, also crosses between inside and outside.Edge e connects a vertex in VT to a vertex in V ∖ VT. So there exists another edge e′ from VT to V ∖ VT on C.
6d. The swap gives a spanning tree
- Build T** by adding e to T* and removing e′.Let T** = (T* + e) − e′.
- No loops: C was the only loop in T* + e, and we just broke it by removing e′.T** is acyclic because C was the unique cycle of T* + e and e′ from C has been removed.
- Still connected: e′ was on a loop, so it wasn't a cut-edge. Removing it cuts nobody off.By Lemma 2.1, T** is connected, and so T** is a tree.
- Still spanning: we only swapped edges, never vertices. So T** is a spanning tree, and it contains T + e.In particular, T** is a spanning tree of G, and T + e is a subtree of T**.
6e. The swap doesn't cost more (the key step)
The notes say this in one line: "by line 5, w(e) ≤ w(e′)". Here it is in two small hops, because the exam wants the reason:
- Say e′ = ab, with a inside the tree and b outside. The label of b is the cheapest edge from the tree to b (lines 3 and 8, her key observation from lecture). e′ is one such edge, so L(b) ≤ w(e′).By lines 3 and 8, for every v ∈ V ∖ VT, L(v) is the minimum weight of an edge that connects v to a vertex of VT.
- Prim picked x because it had the smallest label of all outside vertices, and b is an outside vertex. So w(e) = L(x) ≤ L(b).So by line 5, w(e) ≤ w(e′).
- Put the two hops together: w(e) = L(x) ≤ L(b) ≤ w(e′).
- T** is T* plus e minus e′, so its weight is w(T*) + w(e) − w(e′), which is at most w(T*).w(T**) = w(T*) + w(e) − w(e′), and hence w(T**) ≤ w(T*).
- T* was already as cheap as possible, so T** can't be cheaper. It costs exactly the same, so T** is also a best tree, and it contains T + e. The promise holds. □Since T* is a minimum weight spanning tree, w(T**) = w(T*). So T** is a minimum weight spanning tree of G, and T + e is a subtree of T**. □
Why this finishes the theorem
What she stressed in the lecture (30 Sep)
- You don't need to memorise lemma numbers or statements. The exam says "prove the following statement; you may use these lemmas" and writes them out (e.g. "Lemma A: a graph is connected if and only if it has a spanning tree"). You cite the ones given.
- Definitions you must know by heart. Those aren't given.
- No completely unseen proofs. Expect a scaffolded proof: the strategy and structure are given, and you fill in the details of each step. That's exactly the format of worksheet exercises 5(a) and 11(c).
- "Maths is not about memory, it's about training logic. If you understand something, you can derive what you need on the spot."
- Know which statement you're proving at every moment. Mid-proof she kept asking the room: "what is it we're trying to prove right now?" In the step, the goal is exactly: "T + e is a subtree of a minimum weight spanning tree". The goal map in the summary is this habit written down.
- Why proving the promise is enough: it holds at every stage, and at the end VT = V. So Prim's final tree is itself inside an MST, and is that MST.
- For T**, there are two separate jobs: first show it's a spanning tree (acyclic, and connected by Lemma 2.1: "I removed an edge from a cycle, so it's not a cut-edge, so the graph is still connected"), then show it's minimum (w(e) ≤ w(e′)).
- Why w(e) ≤ w(e′): "remember what the algorithm did: it took the vertex with the smallest label, and the labels are the minimum edges connecting to the tree. So it picked the smallest-weight edge going across."
- Board sloppiness is not exam style. On the board she wrote V − VT and used T for its vertex set. In portfolios and exams, write V ∖ VT and VT. The perfect proof is the one in the notes.
- "100% we'll have something like this in the exam." This proof is under 2 pages. Learn it.
- Her method: the proof in the notes is "boring text". Your job is to go backwards, from the text to your own drawings, until the argument is visually clear to you.
- Work in groups. She recommended working through proofs with friends: "it's more fun that way than banging your head alone on why you don't understand a line."
- Running time (not tested, but worth knowing): with a Fibonacci heap, finding the minimum label is O(log n), done n times, giving n log n. The label updates total O(m). So O(m + n log n) overall.
Exam checklist
When you write this proof, a marker looks for each of these. (In the exam the lemmas will be given to you, possibly renamed "Lemma A, B…". Cite whichever one the paper gives you. In the portfolio, cite the notes' numbers.)
- Name the method: induction on |VT|. State the claim exactly: "T is a subtree of a minimum weight spanning tree of G".
- Base case: cite Lemma 2.2 for the existence of a spanning tree, and hence of an MST.
- State the induction hypothesis: "just before e is added".
- Claim 1: "x has degree 1 in T + e, so there's no cycle through x". Connected + acyclic ⇒ tree.
- Claim 2, case e ∈ E(T*): done.
- Claim 2, case e ∉ E(T*): cite Theorem 2.3 ((a) ⇒ (d)) for the unique path, so the cycle C is unique.
- Say why another crossing edge e′ exists: e crosses from VT to V ∖ VT, and C is a cycle.
- T** is a spanning tree: acyclic (C was the unique cycle and it's broken) and connected (Lemma 2.1).
- w(e) ≤ w(e′): cite lines 3, 5 and 8 (labels are the minimum crossing weights, and line 5 picks the minimum).
- Finish: w(T**) ≤ w(T*), T* is minimum, so they're equal and T** is an MST containing T + e.
Common slips: writing V − VT instead of V ∖ VT; saying T** is a tree without giving both acyclic and connected; forgetting that T* can be a different MST at each step; assuming e′ is unique (it doesn't need to be); writing "<" where you need "≤" (ties are allowed).
The full proof, as a single block
Show the formal proof (close to the lecture notes' wording)
Proof. Let G = (V,E) be a connected graph with edge-weights w : E → ℕ. We prove by induction on |VT| that T is a subtree of a minimum weight spanning tree of G spanning the vertices of VT. The algorithm terminates when VT = V (line 4), so at the end T is a minimum weight spanning tree of G.
Base case: |VT| = 1. Since G is connected, by Lemma 2.2 it has a spanning tree, and hence a minimum weight spanning tree. By lines 1 and 2, T consists of a single vertex, so T is a subtree of any minimum weight spanning tree of G.
Induction step: Assume the result holds for T just before edge e is added on line 6. Let T + e be the graph obtained by adding e and its associated vertex x (lines 6 and 7).
Claim 1: T + e is a tree. By the induction hypothesis T is a tree. Edge e connects x to a vertex y in T, so T + e is connected. Since x has degree 1 in T + e, no cycle of T + e passes through x. Therefore T + e is connected and acyclic, i.e. a tree.
Claim 2: T + e is a subtree of a minimum weight spanning tree of G. By the induction hypothesis, T is a subtree of some minimum weight spanning tree T*. If e ∈ E(T*), we are done by Claim 1. Suppose e ∉ E(T*), and let x, y be the endnodes of e. Since T* is a tree, by Theorem 2.3 ((a) ⇒ (d)) there is a unique (x,y)-path in T*, so T* + e contains a unique cycle C. Edge e connects a vertex in VT to a vertex in V ∖ VT, so there exists another edge e′ from VT to V ∖ VT on C. Let T** = (T* + e) − e′. T** is acyclic because C was the unique cycle of T* + e and e′ has been removed from C. By Lemma 2.1, T** is connected, so T** is a spanning tree of G, and T + e is a subtree of T**. Now w(T**) = w(T*) + w(e) − w(e′). By lines 3 and 8, for every v ∈ V ∖ VT, L(v) is the minimum weight of an edge that connects v to a vertex of VT, so by line 5, w(e) ≤ w(e′), and hence w(T**) ≤ w(T*). Since T* is a minimum weight spanning tree, w(T**) = w(T*). So T** is a minimum weight spanning tree of G, and T + e is a subtree of it. □
C · Getting fluent
How to get from "I followed it" to "I can write it on a blank page".
The three skeletons
Learn the skeleton (the goals and moves) first. Sentences come later. Test yourself by writing each one from a blank page.
What to memorise and what not to: definitions word for word (they're not given in the exam). Not lemma numbers (the exam gives you the lemmas). What you're really training is the strategy of each proof, plus the ability to justify each step with a given lemma, because the exam gives you the scaffold and asks you to fill in the details.
| Lemma 2.1 | (⇐) e on cycle C → C − e is a detour → any route using e can use the detour → G − e connected. (⇒) G − e connected → x–y path P without e → P + e is a cycle. |
| Lemma 2.2 | (⇒) T ⊆ G, spanning and connected → G connected. (⇐) T = connected spanning subgraph with fewest edges (G qualifies) → suppose a cycle → remove a cycle edge: still connected (Lemma 2.1), spanning, fewer edges → contradiction → T is a tree. |
| Prim (2.4) | Claim: T is always inside some MST (induction on |VT|). End: VT = V. Base: one vertex; an MST exists (Lemma 2.2). Step: T ⊆ T*, Prim adds e = xy. Claim 1: T + e is a tree (x has degree 1). Claim 2: e ∈ T* → done; else T* + e has one cycle (Theorem 2.3) → it crosses back via e′ → T** = T* + e − e′ is a spanning tree (Lemma 2.1) → w(e) ≤ w(e′) (labels) → T** is an MST containing T + e. |
The routine (30–40 min per proof)
- Read the proof in the notes with this page open next to it.
- Draw one picture per skeleton line, in colours. Use general blobs, not a specific graph (her point: a drawing of one example isn't a proof).
- Blank page: write the skeleton from memory. Check it, and circle what you missed.
- Expand: turn each skeleton line into one sentence with its reason ("by Lemma 2.1 …"). Compare with the notes.
- Next day: redo step 3 cold. Put anything you missed into Anki.
- Say it out loud or explain it to someone. A line you can't explain is a gap. Better still, do it in a group, as she suggested.
- Fill-in practice: take the full proof, cover one step's justification, and write it yourself. That's the exam format. Worksheet 5(a) and 11(c) are built exactly like this.
Suggested order: the 5 facts and Lemma 2.1, then Lemma 2.2, then Prim (Claim 1 and Claim 2 as separate sessions), then all three from a blank page on Sunday. Worksheet exercise 5(a) is the same swap argument, so it's a perfect test.
Next: Kruskal reuses the same two moves
| Prim (Theorem 2.4) | Kruskal (Theorem 2.5) | |
|---|---|---|
| Strategy | induction: "what's built stays inside some MST" | contradiction: "suppose T isn't an MST" |
| Extreme choice | none needed | T* = the MST with the most edges in common with T (like Lemma 2.2's "fewest edges") |
| Swap | T* + e − e′, with e′ crossing back over the boundary | T* + ei − ej, with ej on the cycle and not in T |
| Why no dearer | e was the cheapest crossing edge (labels) | ej is later in the sorted list, so w(ei) ≤ w(ej) |
| Punchline | T** contains T + e: the promise holds | T** has more edges in common with T than T*: contradiction |