19-Soft-A1 Algorithms & Data Structures · May 2013
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
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 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.
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.
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$).
| Edge (weight) | Endpoints' components before | Action |
|---|---|---|
| (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 |
| Quantity | Result |
|---|---|
| MST edge set | ab, fh, fg, ac, be, df, eg |
| Number of MST edges | 7 (= |V| − 1) |
| Total MST weight | 20 |