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:

Prim's proof     ← uses: tree + edge = one cycle, the detour, the label fact
  ↑
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

Theorem 2.4. Prim's algorithm finds a minimum weight spanning tree of a connected edge-weighted graph G.

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)

The worry: "Prim takes a cheap edge now, and later gets stuck with an expensive one." In the lecture, a lot of people (including people in the room after the break) had exactly this worry. The answer: for spanning trees, you can't fall into a trap, for two reasons.
  1. 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.
  2. 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.
  3. 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.

FactWhy, in one lineWhere 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)

Statement: in a connected graph G, an edge e is not a cut-edge ⇔ e lies on a cycle.
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.
  1. Take a connected graph G and an edge e from x to y.Let G be a connected graph and e = xy ∈ E(G).
  2. (⇐) 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).
  3. 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.
  4. (⇒) 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.
  5. 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)

Statement: a graph has a spanning tree ⇔ it is connected.
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.
  1. The statement is about every graph, so start with "take any graph G".Let G be a graph.
  2. (⇒) Assume G has a spanning tree, and name it T.Assume G has a spanning tree T.
  3. 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.
  4. (⇐) 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.
  5. 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).
  6. 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.
  7. 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)

Her key observation: at every moment, the label L(v) of an outside vertex v is the cheapest edge from the tree to v. So when Prim picks the smallest label, it's picking the cheapest edge leaving the tree. The whole proof leans on this one fact.
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.

tree so far edges leaving the tree cheapest one

The trick, on a tiny example

The point of this example: a graph can have more than one best tree. When Prim makes a move that the best tree you had in mind doesn't use, you can edit that best tree into a different best tree that does use it. That edit is the heart of the proof.

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

  1. 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.
  2. Start: one vertex, no edges. A best tree exists (G is connected), and one vertex fits inside any tree. ✓
  3. 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.
  4. Claim 1: it's still a tree. x hangs off one edge, like a leaf, so no loop can form.
  5. Claim 2, easy case: the best plan already uses e. Nothing to do. ✓
  6. Otherwise, add e to the plan: a tree plus one edge always makes exactly one loop.
  7. The loop crosses the river twice: out over e, and back over some other bridge e′.
  8. Remove e′: e′ was on a loop, so nobody gets cut off. It's still a spanning tree.
  9. 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. ✓
  10. 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.

Theorem 2.4: Prim's output is a minimum weight spanning tree.
↳ It's enough to prove the promise: at every stage, T is a subtree of some MST. (At the end, VT = V.)
↳ Base case, |VT| = 1: one vertex is inside an MST. (Lemma 2.2)
↳ Induction step: if T is inside an MST T*, then T + e is inside some MST.
↳ Claim 1: T + e is a tree. (x has degree 1)
↳ Claim 2: T + e is a subtree of some MST.
↳ If e ∈ T*: done.
↳ If e ∉ T*: build T** = T* + e − e′. Then it's enough to show:
↳ (i) T** is a spanning tree. (Theorem 2.3, Lemma 2.1)
↳ (ii) T** has minimum weight. (lines 3, 5, 8: w(e) ≤ w(e′))

Below, each of these is explained slowly, with pictures.

The plan: a promise that never breaks

The idea: think of building a jigsaw. After each piece you place, ask: "can this still be finished into a perfect picture?" If the answer is yes after every single piece, then the finished puzzle is perfect.

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

Gthe connected weighted graph Prim is run on
Tthe tree Prim has built so far
VTthe vertices already in T (drawn filled in the animations)
xthe vertex Prim is about to add: the outside vertex with the smallest label (line 5)
e = xythe edge that gave x its label. y is already in the tree, and x is outside
T + ethe tree after Prim adds x and e (lines 6–7)
T*a best tree (MST) that contains T. The promise says one exists
Cthe 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

  1. At the start, the tree is a single vertex u and has no edges.Base case: |VT| = 1.
  2. 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.
  3. 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*.
  4. 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.

The idea: Prim attaches x by exactly one edge. A vertex with only one edge is a dead end, and a loop can't pass through a dead end (it would have to leave the same way it came in). So no loop is created, and it's still a tree.
  1. By the promise, T is a tree.By the induction hypothesis, T is a tree.
  2. 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.
  3. 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

The idea: the promise gives us a best tree T* that contains everything so far. If T* already uses e, we're done. If not, we swap: push e into T* and pull out a different edge e′ that costs at least as much. The result is still a tree, it's no more expensive, and it contains T + e.

See it

T (built so far) rest of T* e loop C e′ (swapped out)

6a. The easy case

  1. 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

ELI5: in a tree, there's exactly one route between any two vertices. Add a direct edge between them and you've built a loop: the old route out, the new edge back.
  1. 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.
  2. 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*.
  3. That route plus e forms exactly one loop, C.Therefore, T* + e contains a unique cycle C.

6c. The loop must cross back

ELI5: think of the tree so far as a room. e is a door going out of the room (y inside, x outside). The loop goes out through that door, and a loop always ends where it started, so it has to come back in through some other door. That other door is e′.
  1. 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

ELI5: T* + e has exactly one loop. Snip any edge of that loop and the loop is gone: no loops left. And snipping an edge on a loop never cuts anything off, because you can still go the other way round.
  1. Build T** by adding e to T* and removing e′.Let T** = (T* + e) − e′.
  2. 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.
  3. 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.
  4. 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)

ELI5: Prim chose e because it was the cheapest door out of the room. e′ is also a door out of the room. So e can't cost more than e′.

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:

  1. 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.
  2. 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′).
  3. Put the two hops together: w(e) = L(x) ≤ L(b) ≤ w(e′).
  4. 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*).
  5. 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

ELI5: the promise held at the start and survived every step. Prim stops once every vertex is in, so at the end the whole tree is inside some best tree. But a spanning tree inside another spanning tree must be that tree, since both have exactly n − 1 edges. So Prim's tree is a best tree.
The algorithm terminates when VT = V (by line 4), so at the end of the algorithm this implies that T is in fact a minimum weight spanning tree of G, as desired.

What she stressed in the lecture (30 Sep)

How this appears in the exam (her words, end of lecture):

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.)

  1. Name the method: induction on |VT|. State the claim exactly: "T is a subtree of a minimum weight spanning tree of G".
  2. Base case: cite Lemma 2.2 for the existence of a spanning tree, and hence of an MST.
  3. State the induction hypothesis: "just before e is added".
  4. Claim 1: "x has degree 1 in T + e, so there's no cycle through x". Connected + acyclic ⇒ tree.
  5. Claim 2, case e ∈ E(T*): done.
  6. Claim 2, case e ∉ E(T*): cite Theorem 2.3 ((a) ⇒ (d)) for the unique path, so the cycle C is unique.
  7. Say why another crossing edge e′ exists: e crosses from VT to V ∖ VT, and C is a cycle.
  8. T** is a spanning tree: acyclic (C was the unique cycle and it's broken) and connected (Lemma 2.1).
  9. w(e) ≤ w(e′): cite lines 3, 5 and 8 (labels are the minimum crossing weights, and line 5 picks the minimum).
  10. 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)

  1. Read the proof in the notes with this page open next to it.
  2. 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).
  3. Blank page: write the skeleton from memory. Check it, and circle what you missed.
  4. Expand: turn each skeleton line into one sentence with its reason ("by Lemma 2.1 …"). Compare with the notes.
  5. Next day: redo step 3 cold. Put anything you missed into Anki.
  6. 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.
  7. 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)
Strategyinduction: "what's built stays inside some MST"contradiction: "suppose T isn't an MST"
Extreme choicenone neededT* = the MST with the most edges in common with T (like Lemma 2.2's "fewest edges")
SwapT* + e − e′, with e′ crossing back over the boundaryT* + ei − ej, with ej on the cycle and not in T
Why no dearere was the cheapest crossing edge (labels)ej is later in the sorted list, so w(ei) ≤ w(ej)
PunchlineT** contains T + e: the promise holdsT** has more edges in common with T than T*: contradiction