Chapter 17: Minimum Spanning Trees

Data Structures and Algorithms with C · Study Questions
Spanning treesMinimum costKruskalPrimSummary
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Spanning Trees

Question 1: Adding a Non-Tree Edge

One of the non-tree edges is added to a spanning tree of a connected graph. Which statement is correct?

B: In a tree there is exactly one path between every pair of vertices; the added edge (u, v) forms a single cycle with this path. Deleting any edge of the cycle does not break connectivity and leaves n − 1 edges: a new spanning tree. The method of generating spanning trees is based on this.

Multiple Choice · Spanning Trees

Question 2: Not Always True

For a spanning tree of a connected graph with n vertices, which statement is not always true?

D: In a star-shaped spanning tree the center has degree n − 1 (like the four star-shaped spanning trees of K4). D holds only for path-shaped trees. A, B and C come from the definition of a tree: connected, acyclic, with the fewest edges.

Classic Question · Spanning Trees

Question 3: DFS and BFS Trees

The adjacency lists are sorted in increasing order. Which tree edges do recursive DFS and BFS starting at 0 choose? How many edges stay outside the tree in each?

012345
Show answer

DFS(0): (0,1), (1,2), (2,3), (3,4), (4,5): at every step it descends to the first unvisited neighbor; the tree is a path.

BFS(0): (0,1), (0,2), (1,3), (1,5), (2,4): first the neighbors of 0, then those of 1 and 2.

012345012345

DFS tree on the left, BFS tree on the right (bold)

In both, 10 − (6 − 1) = 5 edges stay outside the tree. For DFS N = {(0,2), (1,3), (1,5), (2,4), (3,5)}, for BFS N = {(1,2), (2,3), (3,4), (3,5), (4,5)}. The number is the same, the edges differ.

Multiple Choice · Minimum Cost

Question 4: The Cheapest Edge

In a connected graph whose edge weights are all different, which statement about the cheapest edge is certainly true?

A: Cut property: in a cut separating the edge's two ends, this edge is the cheapest crossing edge; so it is in an MST, which is unique because weights are distinct. Kruskal picks it first. Wherever Prim starts, once one end is in the tree this edge is the cheapest candidate.

Step-by-Step Trace · Kruskal

Question 5: Kruskal and the parent Array

Kruskal runs on the graph in the figure (unite is weighted, find has no collapsing). Which edges are chosen, which is rejected, and what is parent at the end?

435879261011012345

Edges sorted: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. Every vertex is its own set.

435879261011012345

(3,4) = 2: roots 3 and 4 differ → into the tree. Total 2.

435879261011012345

(0,2) = 3: roots 0 and 2 differ → into the tree. Total 5.

435879261011012345

(0,1) = 4: roots 0 and 1 differ → into the tree. Total 9.

435879261011012345

(1,2) = 5: both ends have root 0 → cycle, skipped.

435879261011012345

(3,5) = 6: roots 3 and 5 differ → into the tree. Total 15.

435879261011012345

(2,3) = 7: roots 0 and 3 differ → into the tree. Total 22.

435879261011012345

t = n − 1 = 5: the loop ends. The edges (1,3), (2,4), (4,5), (1,5) are never looked at. MST cost 22.

for (i = 0; i < n; i++) parent[i] = -1;
qsort(edges, e, sizeof(edge), cmp);
for (i = 0, t = 0; i < e && t < n - 1; i++) {
    int a = find(parent, edges[i].u);
    int b = find(parent, edges[i].v);
    if (a != b) {                 /* no cycle */
        T[t++] = edges[i];
        unite(parent, a, b);
    }                             /* else skip */
}
if (t < n - 1) printf("no spanning tree\n");

Multiple Choice · Kruskal

Question 6: Wrong Cycle Check

In Kruskal the cycle check is done not with union-find but by asking “are both ends of the edge already chosen?”. What happens on the graph in the figure?

435879261011012345

C: (3,4), (0,2), (0,1) are taken; (1,2) is rejected because both its ends are chosen (here the decision is right). (3,5) is taken. Both ends of (2,3) are chosen but they are in different clusters ({0,1,2} and {3,4,5}); the edge is rejected. The ends of the later edges are all chosen too, so it stops at 4 edges. Both ends being chosen does not mean they are in the same cluster.

Output Question · Kruskal

Question 7: Reversed Comparison

On this graph the Kruskal loop is unchanged; only the cmp_desc below is given to qsort. What does it print? What is the tree?

int cmp_desc(const void *x, const void *y) {
    const edge *a = x, *b = y;
    return b->w - a->w;
}
/* ... */
qsort(edges, e, sizeof(edge), cmp_desc);
/* same Kruskal loop; total += edges[i].w */
for (j = 0; j < t; j++)
    printf("(%d,%d) ", T[j].u, T[j].v);
printf("= %d\n", total);
435879261011012345
Show answer

Output: (1,5) (4,5) (2,4) (1,3) (0,1) = 42

  • Edges are tried from largest to smallest: 11, 10, 9, 8 are taken. (2,3) 7, (3,5) 6 and (1,2) 5 form cycles and are skipped; (0,1) 4 is taken, t = 5.
  • The greedy choice still works, but the aim is reversed: this is the maximum spanning tree (42). The minimum tree is 22.

The direction of the comparator decides what the algorithm optimizes.

Multiple Choice · Kruskal

Question 8: The Order of Kruskal's Algorithm

Kruskal is run with qsort and weighted union-find on a sparse graph with n = 1000 vertices and e = 3000 edges. What is the order of the total time?

B: Sorting is O(e log e). The two find calls per edge cost O(log n) with weighted union, O(e log n) in total; since e ≤ n², log e ≤ 2 log n and the two are of the same order. C is the cost of a “bad” coding that searches the lists, A of array-based Prim. D would also need the collapsing rule, and the sorting would remain.

Spot the Bug · Kruskal

Question 9: An End Instead of a Root

One line of the Kruskal loop was changed; the program still finds 22 on this graph. What is the bug, why is parent wrong at the end, and when does it spoil the result?

for (i = 0, t = 0; i < e && t < n - 1; i++) {
    int a = find(parent, edges[i].u);
    int b = find(parent, edges[i].v);
    if (a != b) {
        T[t++] = edges[i];
        unite(parent, edges[i].u, edges[i].v);
    }
}
435879261011012345
Show answer

unite is given the ends of the edge instead of the roots (a, b). While (2,3) is processed, a = 0 and b = 3 are found but unite(parent, 2, 3) is called: parent[2] = 0 ≤ −3 is false, so 2 is cut from its own root and attached under 3.

At the end parent = [−3, 0, 3, −3, 3, 3]: two roots remain ({0,1} and {2,3,4,5}) and both counters are wrong (−3 at 0 but 2 elements; −3 at 3 but 4). The code finds 22 because the loop stops at t = 5. With one more edge, (1,3) would give different roots (0 and 3) and be taken despite closing a cycle (verified by simulation).

Fix: unite(parent, a, b);. A correct result does not show that the code is correct.

Multiple Choice · Prim

Question 10: Order in Prim's Algorithm

Prim is started at vertex 5 on the graph in the figure. In what order do the vertices join the tree?

435879261011012345

A: The candidates of 5 are (5,3) 6, (5,4) 10, (5,1) 11 → 3. Then (3,4) 2 → 4. Candidates (3,2) 7, (3,1) 8, (4,2) 9, (5,1) 11 → 2. Then (2,0) 3 → 0 and (0,1) 4 → 1. The total is again 22: a different start builds the same tree in a different order. In B there is no edge attached to 0 yet.

Step-by-Step Trace · Prim

Question 11: Array-Based Prim

O(n²) Prim is started at 0. Which vertex is chosen in each round and how does lowcost change? (Beside the vertices: the lowcost values; ✓: in the tree.)

4358792610110✓14233∞4∞5∞

TV = {0}. lowcost[v] = cost of the direct edge to 0: 1 → 4, 2 → 3; the others ∞.

4358792610110✓142✓37495∞

Smallest lowcost: u = 2, edge (0,2) = 3 joins the tree; updated: lowcost[3] = 7, lowcost[4] = 9.

4358792610110✓1✓2✓3749511

Smallest lowcost: u = 1, edge (0,1) = 4 joins the tree; updated: lowcost[5] = 11.

4358792610110✓1✓2✓3✓4256

Smallest lowcost: u = 3, edge (2,3) = 7 joins the tree; updated: lowcost[4] = 2, lowcost[5] = 6.

4358792610110✓1✓2✓3✓4✓56

Smallest lowcost: u = 4, edge (3,4) = 2 joins the tree; no update.

4358792610110✓1✓2✓3✓4✓5✓

Smallest lowcost: u = 5, edge (3,5) = 6 joins the tree; no update. Total 22: the same tree as Kruskal.

in_tree[0] = 1;                    /* TV = {0} */
for (v = 0; v < n; v++)
    lowcost[v] = w[0][v], closest[v] = 0;
for (k = 1; k < n; k++) {
    u = cheapest(lowcost, in_tree, n);  /* O(n) */
    if (u < 0) break;                /* no tree */
    in_tree[u] = 1;  total += lowcost[u];
    for (v = 0; v < n; v++)
        if (!in_tree[v] && w[u][v] < lowcost[v])
            lowcost[v] = w[u][v], closest[v] = u;
}

Step-by-Step Trace · Prim

Question 12: Sollin Rounds

Sollin's (Borůvka's) algorithm runs on the graph in the figure. Which edges are chosen in each round, and how many rounds are needed?

435879261011012345

Every vertex is its own component.

435879261011012345

Round 1 choices: 0 → (0,2) 3, 1 → (0,1) 4, 2 → (0,2) 3, 3 → (3,4) 2, 4 → (3,4) 2, 5 → (3,5) 6. Two components can choose the same edge.

435879261011012345

Four distinct edges are added; (0,2) and (3,4), the second time, already have both ends in one set. Components {0,1,2} and {3,4,5}.

435879261011012345

Round 2: both components choose (2,3) 7 as the cheapest edge leaving them.

435879261011012345

(2,3) is added, one component is left. Total 22 in two rounds: the same tree as Kruskal and Prim. The number of components at least halves in every round.

for (i = 0; i < n; i++) parent[i] = -1;
while (components > 1) {
    for (each component C)
        best[C] = cheapest edge leaving C;
    for (each component C)
        if (the ends of best[C] are in different sets) {
            add to T; unite(...);
            components--;
        }
}

Classic Question · Prim

Question 13: MST and the Shortest Path

In the MST of the graph in the figure (total 22), how long is the path from 0 to 5? How long is the shortest path from 0 to 5 in the graph? What is the total edge cost of the shortest-path tree rooted at 0 (Dijkstra; of equal distances the first path found is kept)?

435879261011012345
Show answer
  • In the MST the only path from 0 to 5 is 0 – 2 – 3 – 5: 3 + 7 + 6 = 16.
  • The shortest path is 0 – 1 – 5: 4 + 11 = 15. The MST does not minimize individual distances.
  • Shortest-path tree (Dijkstra, source 0): (0,1) 4, (0,2) 3, (2,3) 7, (2,4) 9, (1,5) 11. Distances 4, 3, 10, 12, 15. The path 0 – 2 – 3 – 4 to 4 is also 12, but updating with strict < keeps (2,4). The total edge cost is 34 > 22 (27 if (3,4) had been chosen; still > 22).

The MST minimizes the total connection cost, the shortest-path tree minimizes the distance from the root to every vertex; the goals differ.

Multiple Choice · Prim

Question 14: A Disconnected Graph

A graph has two components ({0, 1, 2} and {3, 4}); array-based Prim is run from 0 and Kruskal with all the edges. Which statement is correct?

D: In Prim, when all unmarked vertices are ∞, cheapest returns −1 and the loop breaks: only 0's component is covered. Kruskal unites the edges inside the components; it chooses 2 + 1 = 3 edges and, since t < n − 1, prints “no spanning tree”, but what it chose is a minimum spanning forest.

Multiple Choice · Summary

Question 15: Kruskal or Prim?

Which statement about Kruskal and Prim is false?

C: Prim considers only edges with one end in the tree (TV) and the other outside; the condition v ∉ TV already prevents cycles. Union-find is what Kruskal needs, because its clusters grow separately (A). B: n vertices are scanned in every round. D: with distinct weights the MST is unique.

Coding Question · Summary

Question 16: Is It a Spanning Tree?

Write the routine int is_spanning_tree(int n, const edge T[], int t) that uses union-find to test whether the array T of t edges is a spanning tree of a graph with n vertices (1: yes, 0: no, −1: out of memory). Allocate the parent array on the heap.

Show answer
int is_spanning_tree(int n, const edge T[], int t) {
  int *parent = malloc(n * sizeof *parent);
  if (parent == NULL) return -1;
  for (int i = 0; i < n; i++) parent[i] = -1;
  int result = (t == n - 1);         /* edge count */
  for (int i = 0; result && i < t; i++) {
    int a = find(parent, T[i].u);
    int b = find(parent, T[i].v);
    if (a == b) result = 0;            /* cycle */
    else unite(parent, a, b);
  }
  free(parent);
  return result;
}

With n − 1 edges and no cycle the tree is connected. On the example graph: Kruskal's tree gives 1; the set with (1,2) added and (2,3) removed gives 0 (cycle 0–1–2), and four edges give 0. free runs on every exit.

The End

Chapter 17: Minimum Spanning Trees: Study Questions
Lecture slides: Chapter 17: Minimum Spanning Trees · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.