CPSC 221

Minimum Spanning Trees; Prim’s Algorithm

Today

Announcements

  • Examlet 9 is open until Sunday
  • Lab Graphs is due Sunday 12/06
  • HW3 is due Monday 12/07
  • Final Practice is open


Today:

  • Minimum spanning trees: the problem
  • The partition property
  • Prim’s algorithm

Muddy City

You can pave road segments.
Each segment costs its number
of paving stones.

Goal: pave the __________ stones
so every house can reach every other.

 

Which roads do you pave?

How many stones total?

 

(CS Unplugged)

Minimum Spanning Tree

8 2 4 7 2 1 3 9 5 A B C D E F

Input: a connected, undirected graph \(G\)
with edge weights.

Output: a subgraph \(G'\) of \(G\) such that

  • \(G'\) spans \(G\) (it has every vertex)
  • \(G'\) is connected and acyclic (a tree)
  • \(G'\) has minimum total weight
    among all spanning trees

 

Number of edges in \(G'\): ______

MST of this graph?   Total weight: ______

Partition Property

S 8 2 4 7 2 1 3 9 5 A B C D E F

Split the vertices into two sets:
\(S\) and everything else.

Let \(e\) be a minimum-weight edge
crossing the partition.

Then \(e\) is in some MST.

 

Here: \(S = \{A, C, D\}\).

Crossing edges: __________________

Minimum: ______

Partition Property: Why

  1. Let \(e\) be a minimum crossing edge, and \(T\) an MST that does not contain \(e\).

  2. Add \(e\) to \(T\). That creates a __________.

  3. The cycle crosses the partition at \(e\), so it must cross back on another edge, \(f\).

  4. \(f\) is a crossing edge, so \(\text{weight}(f) \geq \text{weight}(e)\).

  5. Remove \(f\). \(T + e - f\) is still a spanning tree, and its weight is \(\leq\) ______.

  6. So \(T + e - f\) is an MST, and it contains \(e\).

From the Property to an Algorithm

Grow a tree from a start vertex \(s\).

  • Let \(S\) be the vertices already in the tree.
  • The partition: \(S\) and everything else.
  • Repeat \(n - 1\) times: add the minimum edge crossing the partition, and its new vertex.

 

Every edge we add is in some MST, by the partition property.

 

How do we find the minimum crossing edge quickly?   A _________________.

 

This is Prim’s algorithm (1957).

Prim’s, by Example

2 5 7 9 8 8 4 3 7 A B C D E F

Labelled:   ________________

d p
A
B
C
D
E
F
Prim(G, s):
  for all v: d[v] = INF, p[v] = null
  d[s] = 0
  PQ = all vertices, keyed by d
  repeat n times:
    v = PQ.removeMin()
    label v
    for each unlabelled w adjacent to v:
      if ________________ < d[w]:
        d[w] = ________________
        p[w] = v
  return p

Prim’s and Dijkstra’s

Dijkstra’s

for each unlabelled w adjacent to v:
  if d[v] + cost(v,w) < d[w]:
    d[w] = d[v] + cost(v,w)
    p[w] = v

\(d[w]\): shortest known path from \(s\) to \(w\).

Prim’s

for each unlabelled w adjacent to v:
  if cost(v,w) < d[w]:
    d[w] = cost(v,w)
    p[w] = v

\(d[w]\): ____________________________

Everything else is the same, so the running time is the same:

Adj matrix Adj list
heap O(n2 + m log n) O(n log n + m log n)
unsorted array O(n2) O(n2)

Negative edge weights?   Dijkstra’s: __________   Prim’s: __________

Same Graph, Two Trees

Dijkstra’s from A   (Wednesday’s practice)

2 5 7 9 8 8 4 3 7 A B C D E F

Total weight: ______

Prim’s from A

2 5 7 9 8 8 4 3 7 A B C D E F

Total weight: ______

A to E:   ______ in the shortest-path tree,   ______ in the MST.

Your Turn

Pause the video. Run Prim’s algorithm from A.

8 2 4 7 2 1 3 9 5 A B C D E F

Ties alphabetical.   Labelled order?   Final d and p?   Total weight?

Your Turn: Answer

8 2 4 7 2 1 3 9 5 A B C D E F
  • A 0: B 8, C 2, D 4
  • C 2: B 7, D 1, E 3, F 9
  • D 1: F 5
  • E 3: B 2
  • B 2
  • F 5

Labelled: A C D E B F

Total weight: 0 + 2 + 1 + 3 + 2 + 5 = 13

Next

Monday: Kruskal’s algorithm, a second MST algorithm.
It uses disjoint sets.

 

Examlet 9 is open until Sunday.