Chapter 7: Minimum Spanning Trees

Chapter 7: Minimum Spanning Trees · Study Questions
MST conceptCut and cycle propertiesKruskalUnion-findPrimBorůvkaReverse-delete
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · MST Concept

Question 1: Cut Property

The vertices are split into {a, b} (green) and {c, d, e} (blue); the edges crossing the cut are dashed. By the cut property, which edge definitely belongs to an MST?

abcde6245387

C: The edges crossing the cut are a–e (6), b–e (4) and b–c (5); the lightest is b–e, so it belongs to an MST. a–b and e–c are in the MST too, but this cut does not explain them: they do not cross it. Kruskal and Prim use this property at every step.

Multiple Choice · MST Concept

Question 2: Cycle Property

In the graph shown, the cycle b–e–c is dashed. If the cycle property is applied to this cycle alone, which edge can be said to be in no MST at all?

abcde6245387

A: The unique heaviest edge of a cycle cannot be in any MST; dropping it and keeping connectivity through the cycle's other edges is always cheaper. B and C cannot be excluded because of this cycle (they are in fact in the MST). The reverse-delete algorithm rests on this property.

Classic Question · MST Concept

Question 3: Multiple MSTs

Can a graph have more than one minimum spanning tree? Explain using the graph beside. Under what condition is the MST unique?

STAB1122
Show answer

Yes. If there is a choice among equal-weight edges, different trees give the same total.

STAB1122STAB1122

S–T and A–B (1) are in both trees; one more edge is needed to connect the four vertices: T–B or S–A (both 2). Both MSTs have weight 4.

If all edge weights are distinct, the MST is unique: the lightest edge of every cut is unique and must be in every MST.

Multiple Choice · MST Concept

Question 4: MST and Shortest Path

Which of the following is true for a minimum spanning tree?

B: MST algorithms only compare weights; a negative value is no problem (the problem is specific to Dijkstra). A is wrong: an MST minimizes the total weight, not the distance between pairs of vertices. C: Prim's starting vertex does not change the total. D: the directed counterpart is the Chu–Liu/Edmonds algorithm.

Output Question · Kruskal

Question 5: Kruskal's Edge Order

In which order does Kruskal's algorithm examine the edges of this graph? Which are added and which are rejected? What is the total weight of the MST?

ABCDEF413685729
Show answer
edgeA–CD–FB–CA–BC–EB–D
weight123456
result✓✓✓✗ cycle✓✓

A–B (4) is rejected: A and B are already in the same tree via the path A–C–B. B–D (6) is the fifth edge: V − 1 = 5 edges are complete, so the remaining edges (D–E, C–D, E–F) are not examined.

w(T) = 1 + 2 + 3 + 5 + 6 = 17.

Step-by-Step Trace · Kruskal

Question 6: Tracing Kruskal's Code

kruskal(6, es) adds which edges? How do the parent array (parent) and w(T) change?

12345642536178

Edges sorted in ascending order: (4,5)=1, (1,3)=2, (2,4)=3, (1,2)=4, (2,3)=5, (3,5)=6, (4,6)=7, (5,6)=8.

12345642536178

parent[i] = i: every vertex is the root of its own tree (a forest of 6 trees). The parent array is shown for 1..6.

12345642536178

(4,5) = 1: find(4) = 4, find(5) = 5 → different trees, edge added; parent[4] = 5.

12345642536178

(1,3) = 2: find(1) = 1, find(3) = 3 → different trees, edge added; parent[1] = 3.

12345642536178

(2,4) = 3: find(2) = 2, find(4) = 5 → different trees, edge added; parent[2] = 5.

12345642536178

(1,2) = 4: find(1) = 3, find(2) = 5 → different trees, edge added; parent[3] = 5.

12345642536178

(2,3) = 5: find(2) = find(3) = 5 → same tree; adding it would form a cycle; skipped.

12345642536178

(3,5) = 6: find(3) = find(5) = 5 → same tree; adding it would form a cycle; skipped.

12345642536178

(4,6) = 7: find(4) = 5, find(6) = 6 → different trees, edge added; parent[5] = 6.

12345642536178

tree.size() = 5 = V − 1: the loop ends; (5,6) = 8 is never examined. w(T) = 17.

void kruskal(int V, List<Edge> es) {
  Collections.sort(es);         // ascending
  int[] parent = new int[V + 1];
  for (int i = 1; i <= V; i++) parent[i] = i;
  List<Edge> tree = new ArrayList<>();
  int k = 0;
  while (tree.size() < V - 1) {
    Edge e = es.get(k++);
    int x = find(parent, e.u);
    int y = find(parent, e.v);
    if (x != y) {               // different trees
      tree.add(e);
      parent[x] = y;               // union
    }
  }
}

Multiple Choice · Kruskal

Question 7: Cycle Check

A student checks for cycles in Kruskal's algorithm by asking "is there a direct edge between u and v in the MST?" Why is this wrong?

B: The edge still closes a cycle. A cycle forms when u and v are in the same component; the length of the path between them does not matter. For example, after a–b and b–c are added there is no direct a–c edge, yet a–c closes a cycle. The right question is "is find(u) == find(v)?".

Step-by-Step Trace · Union-Find

Question 8: Tracing Union-Find

What does the program print? How do the unions and path compression change the forest?

012345

Every vertex is the root of its own set (green). Arrows point to parent[i]; a root points to itself.

012345

union(0, 1): find(0) = 0, find(1) = 1 → parent[0] = 1.

012345

union(1, 2): find(1) = 1, find(2) = 2 → parent[1] = 2.

012345

union(3, 4): find(3) = 3, find(4) = 4 → parent[3] = 4.

012345

union(2, 4): find(2) = 2 (the root of the chain 0 → 1 → 2), find(4) = 4 → parent[2] = 4. The chain grew: find(0) now takes 3 steps.

012345

union(5, 0): find(5) = 5; find(0) follows the path 0 → 1 → 2 → 4 and, on the way back, uses path compression to link 0, 1 and 2 directly to 4. Then parent[5] = 4.

012345

One set remains, with root 4. Output: [4, 4, 4, 4, 4, 4]. All later find calls take at most 1 step.

int find(int[] parent, int i) {
  if (parent[i] != i)
    parent[i] = find(parent, parent[i]); // compress
  return parent[i];
}
void union(int[] parent, int x, int y) {
  parent[find(parent, x)] = find(parent, y);
}
// main
for (int i = 0; i < 6; i++) parent[i] = i;
union(parent, 0, 1); union(parent, 1, 2);
union(parent, 3, 4); union(parent, 2, 4);
union(parent, 5, 0);
System.out.println(Arrays.toString(parent));

Output Question · Kruskal

Question 9: Disconnected Graph

The Kruskal code is called with V = 5 and edges (1,2,3), (2,3,1), (1,3,2), (4,5,4). What happens? How would you fix the code so it returns a minimum spanning forest?

while (tree.size() < V - 1) {
  Edge e = es.get(k++);
  ...
}
Show answer

Sorted edges: (2,3) ✓, (1,3) ✓, (1,2) ✗ cycle, (4,5) ✓. Since tree.size() = 3 < V − 1 = 4, the loop continues and es.get(4) is called: IndexOutOfBoundsException. The graph is not connected ({1, 2, 3} and {4, 5}); there is no MST.

Fix: while (k < es.size() && tree.size() < V - 1). If tree.size() < V − 1 at the end, the graph is not connected; tree is a minimum spanning forest (here 3 edges, weight 1 + 2 + 4 = 7).

Step-by-Step Trace · Prim

Question 10: Tracing Prim's Code

prim(G, 0) adds the vertices to the tree in which order? How do the key and parent arrays change?

012342638579k=0k=∞k=∞k=∞k=∞

Prim with an adjacency matrix, source 0. key[0] = 0, the rest ∞ (label k = key).

012342638579k=0k=∞k=∞k=∞k=∞

u = 0, with the smallest key, enters the tree.

012342638579k=0k=2k=∞k=6k=∞

Neighbors not in the tree are updated: key[1]: ∞ → 2, key[3]: ∞ → 6; their parent is 0.

012342638579k=0k=2k=∞k=6k=∞

u = 1, with the smallest key, enters the tree and the edge (0,1) = 2 is added to the MST.

012342638579k=0k=2k=3k=6k=5

Neighbors not in the tree are updated: key[2]: ∞ → 3, key[4]: ∞ → 5; their parent is 1.

012342638579k=0k=2k=3k=6k=5

u = 2, with the smallest key, enters the tree and the edge (1,2) = 3 is added to the MST.

012342638579k=0k=2k=3k=6k=5

u = 4, with the smallest key, enters the tree and the edge (1,4) = 5 is added to the MST.

012342638579k=0k=2k=3k=6k=5

The loop ran V − 1 = 4 times; the edge (0,3) = 6 of the last remaining vertex 3 is ready too. MST = the edges (parent[v], v), w(T) = 16.

void prim(int[][] G, int src) {
  int V = G.length;
  Arrays.fill(key, Integer.MAX_VALUE);
  key[src] = 0; parent[src] = -1;
  for (int s = 0; s < V - 1; s++) {
    int u = minKey(key, inTree);
    inTree[u] = true;
    for (int v = 0; v < V; v++)
      if (G[u][v] != 0 && !inTree[v]
          && G[u][v] < key[v]) {
        key[v] = G[u][v];
        parent[v] = u;
      }
  }
}

Output Question · Prim

Question 11: Writing the Sum in Prim

The condition and assignment in Prim's code are mistakenly written as key[u] + G[u][v] < key[v] and key[v] = key[u] + G[u][v]. For source 0, what are the tree of edges (parent[v], v) and its total weight? What does the correct Prim give?

012322324
Show answer

The faulty code becomes Dijkstra and gives the shortest-path tree: parent = [−, 0, 0, 0] → edges 0–1, 0–2, 0–3, total 9. (The shortest distances from 0 are 3 to vertex 2 and 4 to vertex 3.)

Correct Prim: parent = [−, 0, 1, 2] → 0–1, 1–2, 2–3, total 6.

In Prim, the key is the weight of the single edge connecting the vertex to the tree; in Dijkstra it is the total distance from the source. A shortest-path tree is a spanning tree but need not be minimum.

Multiple Choice · Prim

Question 12: Prim: Matrix or Heap?

On a graph with V = 1000 vertices, the matrix version of Prim (O(V²)) is compared with the binary-heap version (O(E log V)). Which does less work for E = 5,000 and for E = 400,000?

A: V² = 10⁶. E log V ≈ 5,000 · 10 = 5 · 10⁴ (sparse: the heap wins) and ≈ 400,000 · 10 = 4 · 10⁶ (dense: the matrix wins). On a dense graph (E ≈ V²) the heap version grows to O(V² log V).

Step-by-Step Trace · Borůvka

Question 13: Tracing Borůvka

Which edges does Borůvka pick in each round? How many rounds does it take, and what is the MST weight?

ABCDEFG7411910515612813

Initially every vertex is a separate component (7 components, different colors).

ABCDEFG7411910515612813

Round 1, cheapest outgoing edges (dashed): {A}: AD=4, {B}: AB=7, {C}: CE=5, {D}: AD=4, {E}: CE=5, {F}: DF=6, {G}: EG=8.

ABCDEFG7411910515612813

Added: AD=4, AB=7, CE=5, DF=6, EG=8 (an edge picked twice is skipped the second time). Components left: 2; total 30.

ABCDEFG7411910515612813

Round 2, cheapest outgoing edges (dashed): {ABDF}: BE=10, {CEG}: BE=10.

ABCDEFG7411910515612813

Added: BE=10 (an edge picked twice is skipped the second time). Components left: 1; total 40.

ABCDEFG7411910515612813

One component remains: after 2 rounds the MST = 40 (6 edges). The edges were not sorted; each round scanned all edges once.

while (numComp > 1) {
  Edge[] cheapest = new Edge[V];
  for (Edge e : edges) {        // 1) cheapest
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;
    if (cheapest[k1] == null || e.w < cheapest[k1].w)
      cheapest[k1] = e;
    if (cheapest[k2] == null || e.w < cheapest[k2].w)
      cheapest[k2] = e;
  }
  boolean added = false;
  for (Edge e : cheapest) {          // 2) add them
    if (e == null) continue;
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;
    tree.add(e); parent[k1] = k2; numComp--; added = true;
  }
  if (!added) break;  // forest
}

Multiple Choice · Borůvka

Question 14: Borůvka Round Count

On a connected graph with V = 1000 vertices, at most how many rounds does Borůvka's algorithm run, and what is the total time?

C: In every round each component merges with at least one other, so the number of components at least halves: ⌈log₂ 1000⌉ = 10 rounds. Each round scans all edges once (O(E)) → O(E log V). A assumes only one edge is added per round (Kruskal's step count).

Output Question · Reverse-Delete

Question 15: Reverse-Delete Result

Reverse-delete is applied to the graph shown. Which edges are deleted and which remain? Compare the result with Kruskal's.

ABCDEF413685729
Show answer

Descending order: E–F 9, C–D 8, D–E 7, B–D 6, C–E 5, A–B 4, B–C 3, D–F 2, A–C 1.

  • E–F (9): deleted (on a cycle)
  • C–D (8): deleted (on a cycle)
  • D–E (7): deleted (on a cycle)
  • B–D (6): kept (deleting it disconnects the graph)
  • C–E (5): kept (deleting it disconnects the graph)
  • A–B (4): deleted (on a cycle)
  • B–C (3): kept (deleting it disconnects the graph)
  • D–F (2): kept (deleting it disconnects the graph)
  • A–C (1): kept (deleting it disconnects the graph)

The remaining 5 edges: A–C, B–C, C–E, B–D, D–F → total 17: the same tree Kruskal found (the MST is unique because the weights are distinct).

Multiple Choice · Reverse-Delete

Question 16: Cost of Reverse-Delete

The reverse-delete algorithm runs a BFS/DFS connectivity test for every edge. What is the total time complexity?

D: For each of the E edges an O(V + E) connectivity test is run; the O(E log E) of sorting is small next to it. That is why reverse-delete is mostly instructive: it mirrors Kruskal (it deletes the large edges instead of adding the small ones) and rests on the cycle property.

Output Question · Comparison

Question 17: Bottleneck Path

In the graph shown, among the paths from E to F, which is the one whose heaviest edge is lightest (the bottleneck path)? Find it using the MST. Is this path a shortest path?

ABCDEF413685729
Show answer

MST: A–C, D–F, B–C, C–E, B–D. In the MST there is a single path from E to F: E → C → B → D → F; its edges are 5, 3, 6, 2 → the heaviest edge is 6.

E → F (9), E → D → F (7) and E → C → D → F (8) contain a heavier edge; E → C → A → B → D → F also stays at 6, so the bottleneck value is 6. The path in the MST is always a bottleneck path.

It is not a shortest path: E → C → B → D → F totals 16, while E → D → F totals 9. An MST minimizes the total weight, not the distance between two vertices.

Multiple Choice · Comparison

Question 18: Which Algorithm?

Which of the following pairings is wrong?

D is wrong: Prim grows a single tree; on a disconnected graph it covers only the component of the start vertex. Kruskal and Borůvka naturally give a minimum spanning forest. The other pairings agree with the comparison table in the lecture slides.

The End

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