Home / COMP2870 / Graph algorithms / Worksheet 1.1
Worksheet 1.1, explained
COMP2870 · graph terminology and minimum weight spanning trees
How to use this page: try each exercise first. Stuck? Open Nudge. Still stuck? Bigger hint. Only open the Worked answer to check yourself after a real attempt. Reading answers first feels productive, but teaches you almost nothing.
The toolkit (what the whole worksheet runs on)
| Definitions | subgraph, induced, spanning, tree, cut-edge: primer §2 (with ELI5s and pictures) |
| The 5 facts | a tree has exactly one path between any two vertices, a tree plus an edge has exactly one cycle, the detour, n−1 edges: Prim page, 5 facts |
| Lemma 2.1, Lemma 2.2 | explained |
| The swap | add an edge, get exactly one cycle, remove another edge on that cycle: still a spanning tree. tiny example |
| Prim, Kruskal | run by hand: animations. Proofs: Prim, Kruskal (below) |
| Proof methods | contradiction, counterexample, "prove or disprove": maths refresher |
The lemmas and proofs you need
Everything this worksheet relies on, in one place. Each card shows the statement. Open it for the plain-English idea and the proof (plain steps, with the notes' wording underneath). Definitions must be known by heart. Lemma numbers don't (in the exam the lemmas are given to you). What matters is that you can re-derive each proof from its idea.
Lemma 2.1 (cut-edges and cycles)
Let G be a connected graph. An edge e ∈ E(G) is not a cut-edge if and only if it belongs to a cycle.
Used in: Ex 3, 4, 5(a) Claim 3, 11(c), 12(b), and inside Lemma 2.2, Prim and Kruskal
The idea, and the proof
- Take a connected G and an edge e = xy.Let G be a connected graph and e = xy ∈ E(G).
- (⇐) e is on a cycle C. The rest of C goes from x to y without 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 that used e can use the detour instead, so G − e is still connected: e is not a cut-edge.Let u, v ∈ V(G) and Q a (u,v)-path. If e ∈ E(Q), replacing e by P gives a (u,v)-walk in G − e. Hence G − e is connected.
- (⇒) e is not a cut-edge, so G − e is connected. That gives an x–y path 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 path, plus e, is a cycle through e. □P has at least 2 edges, so P + e is a cycle containing e. □
From COMP1870. The notes quote it without proof.
Lemma 2.2 (spanning trees exist exactly for connected graphs)
A graph has a spanning tree if and only if it is connected.
Used in: Ex 5(a) Claim 1, and the base case of Prim (Theorem 2.4)
The idea, and the proof
- Start with "take any graph G".Let G be a graph.
- (⇒) Name the spanning tree T. It's part of G, reaches every vertex, and is connected, so G is connected.Assume G has a spanning tree T. 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. Pick T: the connected spanning subgraph with the fewest edges. G itself qualifies, so 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, and take an edge e on it.Suppose T contains a cycle C, and let e ∈ E(C).
- By Lemma 2.1, T − e is still connected. It's still spanning, and it has fewer edges. That contradicts the choice of T. So T is a spanning tree. □By Lemma 2.1, T − e is connected and spanning, with fewer edges than T. This contradicts the choice of T. So T is a spanning tree of G. □
Theorem 2.3 (what makes a tree)
For an n-vertex graph G, these are equivalent: (a) G is connected and acyclic; (b) G is connected with n − 1 edges; (c) G is acyclic with n − 1 edges; (d) for every pair u, v there is exactly one path from u to v.
Used in: Ex 2 (count n − 1 edges), 4, 5(a) Claims 2 and 3, 10, 11(c), 12(b)
The idea, and the proof
- Why "exactly one path": a tree is connected, so there's at least one path. Two different paths would form a cycle, and a tree has none, so there's at most one.
- Why "tree + edge xy has exactly one cycle": the unique x–y path in the tree, plus the new edge xy back, is a cycle (the path repeats no vertex, and xy isn't on it). Any cycle using xy must use an x–y path in the tree, and there's only one.Since T is a tree, by Theorem 2.3 ((a) ⇒ (d)) there is a unique (x,y)-path in T. Therefore T + xy contains a unique cycle.
From COMP1870. You cite it rather than prove it.
Every spanning tree contains every cut-edge (Ex 3(b))
Let G be a connected graph and e a cut-edge of G. Then every spanning tree of G contains e.
Used in: Ex 3(a), 3(b), 4 (forced edges), and to justify which edges a spanning tree must keep
The idea, and the proof
- Remove e: G falls into pieces. Take two of them, C1 and C2, and a vertex in each. (because e is a cut-edge)Let C1, C2 be two connected components of G − e, and let v1 ∈ V(C1), v2 ∈ V(C2).
- Every v1–v2 path in G uses e. (different islands: a path avoiding e would be a path in G − e)Since G is connected and G − e is disconnected, every path from v1 to v2 passes through e.
- Pretend a spanning tree T doesn't contain e. (contradiction)Suppose G has a spanning tree T not containing e.
- T has a v1–v2 path P. (spanning: it contains both; tree: it's connected)Since T is a spanning tree, there is a path P from v1 to v2 in T.
- P avoids e (e ∉ E(T)), and P is also a path in G (T ⊆ G).Since e ∉ E(T), P does not contain e. Since T is a subgraph of G, P is a path in G.
- That contradicts "every v1–v2 path uses e". So every spanning tree contains e. □This contradicts the fact that every path from v1 to v2 passes through e. □
The swap (the move behind 4, 5, 11 and 12)
If T is a spanning tree and f ∉ E(T), then T + f has exactly one cycle C. For any edge g ≠ f on C, T + f − g is again a spanning tree, with weight w(T) + w(f) − w(g).
Used in: Ex 4, 5(a), 11(c), 12(b), Prim (Theorem 2.4), Kruskal (Theorem 2.5)
The idea, and the proof
- T + f has exactly one cycle C. (Theorem 2.3: tree plus one edge)By Theorem 2.3 ((a) ⇒ (d)), T + f contains a unique cycle C.
- Remove g from C. The only cycle is broken, so no cycle is left. (acyclic)T + f − g is acyclic, since C was the unique cycle of T + f and g ∈ E(C) has been removed.
- g was on a cycle, so removing it disconnects nothing. (Lemma 2.1) It's still spanning (no vertex removed), so it's a spanning tree.By Lemma 2.1, T + f − g is connected, and so it is a spanning tree of G.
- Its weight is the old weight, plus f, minus g. If w(f) ≤ w(g), it's no heavier. If w(f) < w(g), it's strictly lighter.w(T + f − g) = w(T) + w(f) − w(g).
Theorem 2.4 (Prim finds an MST)
Prim's algorithm finds a minimum weight spanning tree of a connected edge-weighted graph.
Used in: Ex 6, 7, and the model for every "prove an algorithm correct" question
The idea, and the proof
- Claim: at every stage, T is inside some MST. At the end VT = V, so T is an MST.Induction on |VT|: T is a subtree of a minimum weight spanning tree of G.
- Base: one vertex. An MST exists (Lemma 2.2), and it contains every vertex.Base case |VT| = 1.
- Step: T ⊆ T*, and Prim adds e = xy (x is new). Claim 1: T + e is a tree (x has degree 1).Assume T ⊆ T* just before e is added. T + e is a tree.
- Claim 2: if e ∈ T*, done. Otherwise T* + e has one cycle, which crosses back via some e′. The swap T** = T* + e − e′ is a spanning tree. e was the cheapest crossing edge (labels), so w(e) ≤ w(e′), and T** is an MST containing T + e. □T** = (T* + e) − e′ is a minimum weight spanning tree of G, and T + e is a subtree of T**. □
Full walkthrough with animations: Why Prim works.
Theorem 2.5 (Kruskal finds an MST)
Kruskal's algorithm finds a minimum weight spanning tree of a connected weighted graph.
Used in: Ex 8, 9, 10, 11(c) (its mirror image), 12
The idea, and the proof
The full plain-English proof is in Background: why Kruskal works below.
The Ex 4 result ("the unique heaviest edge on the only cycle is in no MST") is itself a swap argument. Its proof is in Exercise 4 → Worked answer. Try it yourself first.
Background: why Kruskal works (Theorem 2.5)
This is lecture content, proved on 30 Sep. Exercises 9–12 lean on it, so it's here in plain English. For exercise 12(b), make your own notes and drawings. Don't copy these.
Analogy for part 2: suppose Kruskal's answer isn't the best. Among all the best answers, take the one that agrees with Kruskal the most. We'll find an even better-agreeing best answer, which is impossible. It's the same trick as Lemma 2.2's "fewest edges".
Claim 1: T is a spanning tree
- Kruskal starts with every vertex, so T is spanning. It never keeps an edge that makes a cycle, so T is acyclic.By line 2, VT = V. By line 3, T is acyclic.
- Still need: T is connected. Pretend it isn't. Take one of its pieces, T′.Suppose T is not connected, and let T′ be a connected component of T.
- G is connected, so some edge ei of G joins T′ to a different piece.Since G is connected, there is an edge ei ∉ ET with one end in V(T′) and the other in V ∖ V(T′).
- That edge joins two separate pieces, so it can't close a cycle. When Kruskal looked at ei, it had even fewer edges, so no cycle then either. Line 3 would have kept it. But it isn't in T. Contradiction.ei cannot be in a cycle of T + ei, which is also true when ei is considered in line 3. So ei would have been included in T, a contradiction.
Claim 2: T is a minimum weight spanning tree
- Pretend T isn't a best tree. Among all best trees, choose T* sharing the most edges with T.Suppose not. Let T* be a minimum weight spanning tree with the largest number of edges in common with T.
- Go down the sorted edge list. Let ei be the first edge that's in T but not in T*.Let ei be the smallest-indexed edge that is in T but not in T*.
- Adding ei to T* makes exactly one cycle C. T has no cycles, so C contains some edge ej that's not in T.By Theorem 2.3 ((a) ⇒ (d)), T* + ei contains a unique cycle C. Since T is acyclic, C contains an edge ej ∉ E(T), so ej ∈ E(T*) ∖ E(T).
- Swap: T** = T* + ei − ej is still a spanning tree, because ej was on a cycle (detour).T** = T* + ei − ej is a spanning tree (Lemma 2.1).
- Everything before ei in the list agrees in T and T*. If ej came before ei, it would be in T too, but it isn't. So ej comes later, and w(ei) ≤ w(ej).T* contains ej and all the edges of T chosen before ei, so i ≤ j and w(ei) ≤ w(ej).
- So T** costs no more than T*, which makes it a best tree too.w(T**) = w(T*) + w(ei) − w(ej) ≤ w(T*), so T** is a minimum weight spanning tree.
- But T** gained ei (in T) and lost ej (not in T), so it agrees with T on one more edge than T*. We chose T* to agree the most. Contradiction. □T** has more edges in common with T than T*, contradicting the choice of T*. □
Compared with Prim: Prim's proof is induction ("what's built stays inside some MST"). Kruskal's is contradiction plus an extremal choice ("the MST most like T"). The swap is the same.
Exercise 1: subgraphs of G1
How to approach it
Read off G1's edges first: E = {ab, ac, ad, bc, bd, cd, de}. For each part, write V and E explicitly, then check the definition word for word.
Nudge
Induced = choose the vertices, then you're forced to take every edge between them. "Not induced" = leave out at least one edge whose two ends you kept.Bigger hint
A spanning tree needs all 5 vertices and exactly 4 edges with no cycle. A subtree that isn't spanning: any tree that misses at least one vertex.Worked answer
- (a) V = {a, b, c}, E = {ab, ac, bc}. Every G1-edge between a, b, c is included.
- (b) V = {a, b, c}, E = {ab, bc}. Not induced, because ac ∈ E(G1) is missing.
- (c) V = {a, b, c, d, e}, E = {ab, ac, ad, de}. 4 = n − 1 edges, connected, no cycle.
- (d) V = {c, d, e}, E = {cd, de}. A tree, but it misses a and b.
Many answers are correct. Check yours against the definitions.
Exercise 2: counting spanning trees of G2
How to approach it
A spanning tree on 4 vertices has 3 edges. So list every way to pick 3 of the 5 edges, and throw out the choices that contain a cycle. Then compute each weight.
Nudge
There are 5 edges, so there are C(5,3) = 10 ways to choose 3. Which of those 10 contain a triangle?Bigger hint
The two triangles are a–b–c and b–c–d. Exactly one choice of 3 edges equals each triangle, so 10 − 2 = ?Worked answer
8 spanning trees. Weights: {ab, bc, bd}, {ab, bc, cd}, {ab, bd, cd} = 7 (these are the 3 MSTs); {ab, ac, bd}, {ab, ac, cd} = 9; {ac, bc, bd}, {ac, bc, cd}, {ac, bd, cd} = 11.
Note: there are 3 MSTs because of the tied weight-3 edges. That links to exercise 5.
Exercise 3: edges in every spanning tree; reading a proof
How to approach it
(a) An edge is in every spanning tree exactly when you can't avoid it: when removing it disconnects G. (b) is the same skill as 12(b): turn "boring text" into your own picture.
Nudge
(a) Which edges of G3 lie on no cycle?Bigger hint
(a) The two triangles are a–b–c and d–e–f. Which edges are left over? (b)(iii): what would a v1–v2 path that avoids e be, in G − e?Worked answer
- (a) cd and fg, the cut-edges (Lemma 2.1: on no cycle).
- (b)(i) Contradiction. Suppose a spanning tree T doesn't contain e…
- (b)(iii) v1 and v2 are in different components of G − e. A v1–v2 path avoiding e would be a path in G − e, joining two different components. That's impossible.
- (b)(iv) Your own notes. The key idea: T is connected and spanning, so it contains a v1–v2 path. If e ∉ T, that path avoids e and lives in G. That contradicts (iii).
Exercise 4: the heaviest edge on the only cycle (*) advanced
How to approach it
Prove-or-disprove method: first try small examples to decide which way it goes. If you can't break it, prove it. Here, the swap from the Prim proof does the job.
Nudge
Try a triangle with weights 1, 2, 3. Which spanning trees avoid the 3?Bigger hint
It's true. Suppose an MST T does contain e. T is acyclic, so some edge f of C is missing from T. What does T + f contain, and which cycle must it be?Worked answer
- Suppose, for contradiction, that some MST T contains e.
- T is acyclic, so it can't contain all of C. Pick an edge f ∈ E(C) with f ∉ E(T).
- T + f has a unique cycle (Theorem 2.3). It's a cycle of G, and C is G's only cycle, so it's C, which contains e.
- T′ = T + f − e is a spanning tree: e was on a cycle (Lemma 2.1), and the unique cycle is broken.
- e is the unique heaviest edge of C, so w(f) < w(e). Then w(T′) = w(T) + w(f) − w(e) < w(T), which contradicts T being minimum. So the statement is true. □
Exercise 5: distinct weights ⇒ unique MST (and the converse) summative
How to approach it
This is exactly the exam format she described: the strategy is given, and you justify each claim. For each claim: state which result you use, and check its conditions hold here, the way the notes do.
What the marker looks for
- Claim 1: you cite Lemma 2.2, and explain how "has a spanning tree" gives "has a minimum weight spanning tree".
- Claim 2: you name the two endpoints, and use the unique path from Theorem 2.3 to get a unique cycle.
- Claim 3: you show both "connected" (and say why, citing Lemma 2.1) and "acyclic" / tree.
- Claim 4: you use the definition of e (smallest weight among edges in one tree but not both) and "all weights different". Then ask yourself: which of e and e* is smaller, and why exactly?
- Contradiction: you say precisely which assumption it contradicts.
- (b) A false "if…then" needs one counterexample, drawn and checked. A true one needs a proof. Experiment with small graphs before deciding.
Want feedback? Write your own attempt, then ask me to check it in chat. I'll point out gaps, not write it for you.
Exercise 6: Prim by hand
How to approach it
Keep a table: each round, write every outside vertex's label, pick the smallest, record the edge, then update only the labels of the new vertex's neighbours.
Nudge
(a) Start: L(b) = 2, L(c) = 7, L(d) = 1, L(e) = 5, L(f) = 3. Which goes first?Bigger hint
(a) After d joins, L(e) drops to 4 and L(c) to 5. After b joins, L(c) drops to 1. (b) In G5 the first move is forced: a has only one edge.Worked answer
(a) Order: d (ad, 1), b (ab, 2), c (bc, 1), f (af, 3), e (fe, 2). Total weight 9.
(b) Order: b (ab 2500), d (bd 1000), c (bc 1500), f (cf 500), e (fe 2000). Minimum cost £7500.
Exercise 7: Prim on a disconnected graph
How to approach it
Run Prim on your example and watch the labels. The question is: what does line 5 do when every remaining label is ∞?
Nudge
Once Prim has added every vertex it can reach from its start vertex, what are the labels of the rest?Bigger hint
Line 5 would pick a vertex with label ∞, but there's no edge ex to add. "Edge of weight ∞" means "no edge". So what should happen instead?Worked answer
- (a) Any graph with two separate pieces, e.g. edges ab (1) and cd (2), and no edges between {a, b} and {c, d}.
- (b) Prim builds a spanning tree of the start vertex's component. Then every remaining label is ∞. Line 5 would choose a vertex whose "edge" doesn't exist, so the algorithm breaks (it isn't defined for this input). The loop condition VT ≠ V never becomes false properly.
- (c) When the smallest label is ∞, start a new tree: add that vertex with no edge, set the labels from it, and continue. Each restart grows a minimum spanning tree of a new component. The result is a minimum weight spanning forest.
Exercise 8: Kruskal by hand
How to approach it
Sort all edges first. Then for each one write: accepted, or rejected because it would close a cycle (name the cycle). Keep track of the pieces (components).
Nudge
(a) The sorted order starts df 86, ac 88, ef 90, de 92… What does de do?Bigger hint
(a) de would close the triangle d–e–f. (b) G7 has four weight-1 edges. Do any of them clash?Worked answer
(a) Accept df 86, ac 88, ef 90, de 92 (cycle d–e–f), bc 94, ab 96 (cycle a–b–c), fg 98, cd 100. Total 556.
(b) Accept ab, bd, bf, eg (all 1), ad 2 (cycle a–b–d), be 3, bc 4. The rest all close cycles. Total 11 cm.
Exercise 9: small facts about Kruskal
How to approach it
For each part, trace what Kruskal does at that exact moment, and say why the cycle check must pass.
Nudge
How many edges does a cycle in a simple graph need, at least?Bigger hint
(a) When the smallest edge is considered, T has no edges yet. (b) When the second-smallest is considered, T has at most 1 edge. (c) Does doubling every weight change which edge is smaller?Worked answer
- (a) e is considered first, when ET = ∅. One edge on its own can't form a cycle, so line 3 adds it.
- (b) e′ is considered second, when T has at most one edge. A cycle in a simple graph needs at least 3 edges, so T + e′ is acyclic, and e′ is added.
- (c) For any spanning tree T′, w′(T′) = 2·w(T′) ≥ 2·w(T) = w′(T), since T is minimum for w. So T is minimum for w′ too. (Alternatively: doubling keeps the sorted order, so Kruskal makes exactly the same choices.)
Exercise 10: stopping Kruskal early (*) advanced
How to approach it
Pattern for "modify and prove": show the modified algorithm makes exactly the same choices as the original, which is already proved correct.
Nudge
How many edges does a spanning tree on n vertices have?Bigger hint
Once |ET| = n − 1, T is a spanning tree. What happens if Kruskal tries to add any further edge to a tree?Worked answer
- (a) Count the edges. T is always acyclic and VT = V, so as soon as |ET| = n − 1, T is a spanning tree (Theorem 2.3(c)).
- (b) Change line 3 to: "for i = 1 to |E|, while |ET| < n − 1, do …" (stop once n − 1 edges are chosen).
- (c) After n − 1 edges, T is a spanning tree. Any further edge e joins two vertices of a tree, so T + e contains a cycle (Theorem 2.3: unique path plus e). The original Kruskal would therefore reject every remaining edge. So both versions output the same T, which is an MST by Theorem 2.5. □
Exercise 11: the Reverse-Delete algorithm
How to approach it
Reverse-Delete = Kruskal backwards. Start with every edge. Go from the heaviest down, and delete each edge if the graph stays connected. By Lemma 2.1, that's exactly when the edge is still on a cycle.
Nudge
(a) Sort G8's edges from heaviest: bc 12, ef 11, dg 10, ab 9, cf 8, … For each, ask: is it still on a cycle right now?Bigger hint
(a) You'll delete 6 edges and keep 6 (n − 1 = 6). cf 8 must stay: by then it's c's only edge. (c) Claim 1 needs connected (line 3 never disconnects) and acyclic (if a cycle survived, its heaviest-processed edge would have been deletable).Worked answer
(a) Delete bc, ef, dg, ab; keep cf; delete de; keep bd (else {a, d} is cut off); delete fg; keep be, eg, bf, ad. Result {ad, bf, eg, be, bd, cf}, weight 24. That matches Kruskal, which is a good check.
(b) Yes, greedy: each step makes the locally best move (throw away the most expensive edge it can) and never reconsiders.
(c) Sketch: Claim 1: spanning (line 2), connected (start connected, and line 3 never disconnects), acyclic (if T kept a cycle, each of its edges was on that cycle when considered, so by Lemma 2.1 removing it kept T connected, and line 3 would have deleted it). Claim 2: Theorem 2.3 (a ⇒ d). Claim 3: ej on the cycle ⇒ connected (Lemma 2.1), and the unique cycle broken ⇒ tree. Claim 4: ej comes before ei in the non-increasing order, so w(ej) ≥ w(ei) and w(T′) ≤ w(T*). Contradiction: T′ is an MST with more edges in common with T than T*.
Exercise 12: Kruskal trace, and notes on Theorem 2.5 summative
How to approach it
(a) Use the same method as exercise 8: sort every edge, then fill the table one row per edge, with a reason for each "No". Draw the final tree, and check it has n − 1 edges.
(b) The proof is explained in plain English above. Your notes must be your own: your own drawings, in colours, as general as possible.
What the marker looks for
- (a) All 9 rows filled, in correct sorted order. Every rejection is correct. The final tree is drawn, with n − 1 edges.
- (b)(i) You name all three properties proved for "spanning tree": spanning, acyclic, connected. Say which line or argument gives each.
- (b)(ii) Your drawings show T and T* distinguished (colour or line style), ei, the cycle C, ej, and the swap. Your notes explain why w(ei) ≤ w(ej), and what the final contradiction contradicts.
Want feedback? Ask me to check your trace or notes in chat.