3.5.14 · D4Graphs

Exercises — Minimum Spanning Tree — Kruskal's (Union-Find), Prim's (priority queue)

2,763 words13 min readBack to topic

We reuse one small graph for most exercises so you build intuition on familiar ground. Meet it now:

Figure — Minimum Spanning Tree — Kruskal's (Union-Find), Prim's (priority queue)

Level 1 — Recognition

Exercise 1.1

How many edges does the MST of have, and why exactly that many? ( has vertices.)

Recall Solution

WHAT: A spanning tree touches every vertex and contains no cycle. WHY that count: A tree on vertices always has exactly edges — the very first edge joins vertices into one group, and every later edge must join one new vertex to the existing group (if it joined two already-connected vertices it would form a cycle). So each of the remaining vertices contributes exactly one edge. Here , so the MST has edges.

Exercise 1.2

In one sentence: what question does the operation find(x) answer in Union-Find, and what does union(a,b) return False mean?

Recall Solution

find(x) answers "which group (root) does vertex belong to right now?" by walking parent pointers up to the representative. union(a,b) returning False means " and were already in the same group" — merging them would create a cycle, so Kruskal must reject that edge. See Disjoint Set Union (Union-Find).

Exercise 1.3

True or false: "The globally cheapest edge in the whole graph is always in some MST." Justify.

Recall Solution

True. Take any cut that separates the two endpoints of that cheapest edge (for example, one endpoint alone on side ). The cheapest edge in the whole graph is certainly the cheapest edge crossing that cut. By the cut property, the minimum-weight crossing edge is safe → it belongs to some MST. In our graph that edge is .


Level 2 — Application

Exercise 2.1

Run Kruskal on by hand. List the edges you add in order, mark any you skip and say why, and give the total MST weight.

Recall Solution

WHAT we do: sort edges ascending, then add each if it joins two different components. Sorted edges: .

Step Edge Components before Action Why
1 add different sets
2 add different sets
3 add different sets
4 skip already together → cycle
5 add joins → now edges, stop

MST edges: . Total . See the highlighted tree:

Figure — Minimum Spanning Tree — Kruskal's (Union-Find), Prim's (priority queue)

Exercise 2.2

Run Prim from vertex using a lazy min-heap (as in the parent code). Show each pop and the resulting total.

Recall Solution

WHAT: grow a tree from , always popping the cheapest frontier edge; skip pops to already-visited vertices. Start: heap .

Pop Add? Push new frontier edges
visit (cost )
visit (cost )
visit (cost ) stale,
visit (cost )
already visited → skip
visit (cost ) done, all 5 visited

Total . Same as Kruskal — as it must be, since MST weight is unique. See Priority Queue (Binary Heap) for the heap machinery.

Exercise 2.3

Trace the DSU after Kruskal's step 3 above (edges added, union-by-rank + path compression). Give the parent array p and root of every vertex.

Recall Solution

Start , rank all .

  • union(0,1): equal ranks → attach under , r[0]=1. .
  • union(1,2): find(1)=0 (rank 1), find(2)=2 (rank 0) → attach under . .
  • union(2,3): find(2)=0 (rank 1), find(3)=3 (rank 0) → attach under . .

Final parents: . Roots: , and . So components are and — matching the table in 2.1.


Level 3 — Analysis

Exercise 3.1

Suppose we change edge to . Does the MST change? Recompute its weight.

Recall Solution

WHAT changes: vertex was reached cheapest through . Now that edge costs . The other edge into is . Re-sort the relevant tail: . Kruskal adds , skips (cycle), then adds — that gives edges, stop. New MST weight . The tree changed: the edge into is now instead of .

Exercise 3.2

A graph has all distinct edge weights. Argue whether its MST is unique, and connect this to the cut property.

Recall Solution

Claim: with all-distinct weights the MST is unique. Why (cut argument): For every cut, the minimum crossing edge is now strictly smallest, so it is forced — there is no tie to break differently. The cut property then says this exact edge is in the MST. Since every safe edge is uniquely determined by the cuts, no two different spanning trees can both be minimal. This is the cut/cycle property pushed to its strict form. (With ties, several MSTs can share the same minimum weight — as we saw comparing Kruskal and Prim.)

Exercise 3.3

Why is Kruskal but Prim with a binary heap ? Identify the dominating cost in each.

Recall Solution

Kruskal: the dominating step is sorting all edges. The Union-Find work over edges is only , essentially linear, so it doesn't dominate. Prim (binary heap): we do at most pushes and pops, each . The heap holds at most entries in the lazy version, but with an indexed/decrease-key heap it holds at most entries → each op , total . Note , so the two bounds are the same order — but on dense graphs () the vertex-based heap is the tighter description. Compare with Dijkstra's Algorithm, which shares Prim's heap skeleton.


Level 4 — Synthesis

Exercise 4.1

You are given a connected graph and one specific edge . Prove: is in no MST if and only if there is a cycle through in which is the strictly heaviest edge. (This is the cycle property.) Then apply it to edge in our graph.

Recall Solution

() Suppose is the strictly heaviest edge on some cycle . If any MST contained , deleting splits into two components; some other edge of crosses that split (a cycle re-connects). Since was strictly heaviest, , so is a spanning tree of smaller weight — contradiction. Hence is in no MST. () If is in no MST, then adding to any MST creates a cycle where is the heaviest (otherwise the swap argument would let replace a heavier edge and improve the MST, forcing into an MST). Apply: cycle has weights . Edge is strictly heaviest → it is in no MST, exactly why Kruskal skipped it in 2.1. This mirrors the greedy exchange argument of Greedy Algorithms.

Exercise 4.2

Design a check: given a candidate set of edges, describe an test using Union-Find that decides whether is a spanning tree (connected, acyclic) of . Then verify the set passes.

Recall Solution

Algorithm:

  1. Create a fresh DSU on vertices.
  2. For each edge in : if union(u,v) returns False, there is a cycle → reject.
  3. After all edges, if we accepted exactly edges and every vertex shares one root → it is a spanning tree.

Union-Find gives near-constant per operation, so total . Verify for on :

  • union(0,1)✓, union(1,2)✓, union(2,3)✓, union(2,4)✓ — all True, 4 unions, no cycle.
  • All five vertices end with the same root → connected. ✅ It is a spanning tree (and indeed the MST from 2.1).

Exercise 4.3

Prim from vertex produced total . If instead we start Prim from vertex , will the total change? Trace it.

Recall Solution

WHAT: the starting seed changes the order of growth but not the final MST weight — every step still picks a minimum crossing edge, all justified by the cut property. Start , heap :

  • pop → visit , push
  • pop → visit , push
  • pop → visit , push
  • pop → visit
  • pop → visit ; all visited, stop

Total . Same weight, different growth order — as required.


Level 5 — Mastery

Exercise 5.1

Add a new vertex to with edges and . Recompute the MST and its total weight, and state which single edge attaches vertex .

Recall Solution

New sorted edges: . Now , so MST needs edges. Kruskal:

  • add
  • add
  • add — joins and
  • add
  • skip (cycle )
  • add — joins group with group → now edges, stop

MST weight . Vertex attaches through — the only cheap bridge into the isolated pair , forced by the cut whose cheapest crossing edge is .

Exercise 5.2

A student claims: "Multiply every edge weight by a positive constant ; the MST edge set stays the same, and its total scales by ." Prove it, and give the new total for the original with .

Recall Solution

Proof: Both Kruskal and Prim only ever compare weights () — never add along paths in a way that mixes scales. For , , so every comparison outcome is unchanged → the algorithm makes the identical accept/reject decisions → identical edge set. The total is a sum of the chosen weights, each multiplied by , so the total also multiplies by . Apply: original MST total , so with the new total , same edges . (Note: this fails for or non-monotone transforms, since comparisons flip.)

Exercise 5.3

Capstone. Explain — using only the cut property — why Kruskal and Prim, though structurally opposite (edge-driven vs vertex-driven), can never produce spanning trees of different total weight. Then confirm on using both totals you computed.

Recall Solution

WHAT each does at every step: Prim's cut is explicit — with the grown tree; it picks the minimum crossing edge → safe by cut property. Kruskal's cut is implicit — when it accepts edge , and lie in two separate components; consider the cut with one of those components as ; the accepted edge is the cheapest edge crossing that cut among remaining edges, and no cheaper crossing edge was rejected (rejected edges only ever formed cycles inside a component, never crossing this cut). So Kruskal too always adds a safe minimum crossing edge. WHY totals match: Every step of both algorithms adds only safe edges, and both build a full spanning tree. A spanning tree assembled entirely from cut-property-safe edges achieves the minimum possible total (this is the exchange-argument optimum). Two minimum totals over the same graph must be equal — even if the edge sets differ when weights tie. Confirm on : Kruskal total (2.1), Prim-from-0 total (2.2), Prim-from-4 total (4.3). All equal. ∎


Recall One-line self-test

Why does Kruskal skip edge but keep ? ::: closes cycle where it is the heaviest edge (cycle property → in no MST); joins two separate components and is the cheapest crossing edge of that cut (cut property → safe).

Connections

  • Disjoint Set Union (Union-Find) — the accept/reject engine used all over L2–L4.
  • Priority Queue (Binary Heap) — Prim's frontier selector.
  • Cut Property and Cycle Property — every proof above rides on these.
  • Greedy Algorithms — the exchange argument in 4.1 and 5.3.
  • Dijkstra's Algorithm — same heap skeleton as Prim, different key.
  • Graph Representations — edge list (Kruskal) vs adjacency list (Prim).