250 likes | 457 Views
Minimum Spanning Trees. Minimum Spanning Trees. Create a Tree that connect all vertices If is weight between u and v , then: Our job: minimize w ( T ) We can use a greedy approach. General Proof for MSTs. General idea for loop invariant:
E N D
Minimum Spanning Trees Jeff Chastine
Minimum Spanning Trees • Create a Tree that connect all vertices • If is weight between u and v, then: • Our job: minimize w(T) • We can use a greedy approach Jeff Chastine
General Proof for MSTs • General idea for loop invariant: • “Prior to each iteration, A is a subset of a MST” • Do this by finding safe edges that don’t violate this property • Other terms • A cut is a partitioning of V • A crossing edge crosses a cut • A light edge is a minimally weighted edge crossing the cut Jeff Chastine
A Tale of Two Algorithms • Pretty simple… • Kruskal’s: forms a forest • Prim’s: forms a single tree • Both: • Sort edges by weight • Add edges by increasing order if connects a new vertex Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
8 7 b c d 4 9 2 Kruskal’s 11 a i 4 14 e 7 6 10 8 h g f 1 2 Note: the algorithms still run on the remaining edges! 8 7 b c d 4 9 2 11 a i 4 14 e Prim’s 7 6 10 8 h g f 1 2 Jeff Chastine
7 b c d 4 9 2 Kruskal’s a i 4 e 8 h g f 1 2 Why do we get different shapes? 8 7 b c d 4 9 2 a i 4 e Prim’s h g f 1 2 Jeff Chastine
Summary • Minimum Spanning Trees • Calculate smallest weighted tree to connect all vertices • Use greedy algorithms • Prim’s algorithm (single tree) • Kruskal’s algorithm (forest) • Must sort the edges Jeff Chastine