NivaarExam PrepOfficial exam papers ↗

19-Soft-A1 Algorithms & Data Structures · May 2013

Question 4 of 8: Spanning Tree

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

National Exams — May 2013 — 04-Soft-A1 Algorithms & Data Structures. Three-hour, closed-book exam (Casio or Sharp approved calculator only). Format: eight questions in two parts — candidates choose 4 of the first 5 (10 marks each) and must answer Q6, Q7 and Q8 (20 marks each), with Q7 itself asking for 5 of 6 sub-concepts. All eight questions, and all sub-parts within them, are solved below for completeness.

Reference texts: Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms (3rd ed., MIT Press) — asymptotic analysis, heaps, graph algorithms, divide-and-conquer, NP-completeness; Sedgewick & Wayne, Algorithms (4th ed., Addison-Wesley) — linked-list and array data structures, sorting; Weiss, Data Structures and Algorithm Analysis in C (2nd ed., Pearson) — array-based binary trees and heaps.

Question 4: Spanning Tree (10 marks: 2, 5, 3)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. $V=\{a,b,c,d,e,f,g,h\}$ (8 vertices) and the 12 weighted edges listed above. Find. a spanning tree of $G$ of minimum total edge weight.

1. The graph — 2 marks

Graph G = (V, E, w)133645445212abcdefgh
Figure 2 — The undirected weighted graph $G$, edge weights as given.

2. Algorithm choice — 5 marks

Kruskal's algorithm. Sort all edges by non-decreasing weight. Process them in that order, adding an edge to the growing forest only if its two endpoints are currently in different components (i.e. adding it would not create a cycle); skip it otherwise. A disjoint-set (union–find) structure tracks components in near-constant time per operation. The process stops once $|V|-1$ edges have been added, at which point the forest is a single spanning tree, and — by the cut property (the lightest edge crossing any cut belongs to some MST) — it is guaranteed minimum.

3. Applying Kruskal — 3 marks

Approach. Sort the 12 edges by weight, then scan them in order, unioning components and recording an edge whenever its endpoints are not already connected; stop at 7 edges ($|V|-1=8-1$).

  1. Sort edges by weight. $(a,b,1),(f,h,1),(f,g,2),(g,h,2),(a,c,3),(b,c,3),(b,e,4),(d,f,4),(d,g,4),(c,e,5),(e,g,5),(c,d,6)$.
  2. Scan and union.
    Edge (weight)Endpoints' components beforeAction
    (a,b,1){a} , {b}add — connects a,b
    (f,h,1){f} , {h}add — connects f,h
    (f,g,2){f,h} , {g}add — connects f,g,h
    (g,h,2)both in {f,g,h}skip — would close a cycle
    (a,c,3){a,b} , {c}add — connects a,b,c
    (b,c,3)both in {a,b,c}skip — cycle
    (b,e,4){a,b,c} , {e}add — connects a,b,c,e
    (d,f,4){d} , {f,g,h}add — connects d,f,g,h
    (d,g,4)both in {d,f,g,h}skip — cycle
    (c,e,5)both in {a,b,c,e}skip — cycle
    (e,g,5){a,b,c,e} , {d,f,g,h}add — connects all 8 vertices, 7th edge, stop
  3. Result. $$\text{MST} = \{(a,b,1),(f,h,1),(f,g,2),(a,c,3),(b,e,4),(d,f,4),(e,g,5)\}, \qquad \boxed{w(\text{MST}) = 1+1+2+3+4+4+5 = 20}.$$
Minimum spanning tree (Kruskal) — bold blue edges, total weight 20133645445212abcdefgh
Figure 3 — Selected MST edges in bold blue; rejected (cycle-forming) edges dashed grey. The remaining edge $(c,d,6)$ was never even reached before the tree completed.
QuantityResult
MST edge setab, fh, fg, ac, be, df, eg
Number of MST edges7 (= |V| − 1)
Total MST weight20