πŸ–¨οΈ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1

Prim's, Kruskal's, and the Cut Property

2

Learning Goals

3

Problem Definition

4

Spanning Tree

5

Minimum Spanning Tree (MST)

6

Uniqueness

7

The Cut Property

8

Definition of a Cut

9

The Cut Property (Crucial)

10

Proof of Cut Property

11

Generic Greedy MST

12

Generic Algorithm

13

Two Instantiations

14

Kruskal's Algorithm

15

Strategy

16

Kruskal(G)

A = empty
for each vertex v:
    Make-Set(v)
Sort edges E by weight ascending
for each edge (u, v) in sorted E:
    if Find-Set(u) != Find-Set(v):
        A = A βˆͺ {(u, v)}
        Union(u, v)
return A
17

Disjoint Set Union (DSU / Union-Find)

18

Union-Find Deep Dive

19

Union-Find: The Big Picture

20

Representing Sets as Trees

21

Make-Set and Naive Find

22

Trick 1: Path Compression

23

Trick 2: Union by Rank

24

Both Tricks Together

25

Union-Find Implementation

Make-Set(x):
    parent[x] = x
    rank[x] = 0

Find-Set(x):              // with path compression
    if parent[x] β‰  x:
        parent[x] = Find-Set(parent[x])
    return parent[x]

Union(x, y):              // with union by rank
    rx = Find-Set(x)
    ry = Find-Set(y)
    if rx = ry: return     // already same set
    if rank[rx] < rank[ry]:
        parent[rx] = ry
    else if rank[rx] > rank[ry]:
        parent[ry] = rx
    else:
        parent[ry] = rx
        rank[rx] = rank[rx] + 1
26

Union-Find: Step-by-Step

27

Interactive: Union-Find

28

Kruskal's Analysis

29

Kruskal Trace

30

Interactive: Kruskal's Algorithm

31

Prim's Algorithm

32

Strategy

33

Prim(G, r)

for each u in V:
    key[u] = ∞
    Ο€[u] = NIL
key[r] = 0
Q = V   // Min-priority queue keyed by key[]
while Q is not empty:
    u = Extract-Min(Q)
    for each v in Adj[u]:
        if v ∈ Q and w(u, v) < key[v]:
            Ο€[v] = u
            key[v] = w(u, v)   // Decrease-Key
34

Prim's Key Concepts

35

Prim's Analysis

36

Prim Trace

37

Interactive: Prim's Algorithm

38

Comparison

39

Kruskal vs Prim

40

Advanced Topics

41

Reverse-Delete Algorithm

42

BorΕ―vka's Algorithm

43

Common Pitfalls

44

Watch Out For...

45

All Interactive Demos

46

Summary

47

Lecture Summary

48

Supplementary Resources