CS3000: Algorithms & Data Jonathan Ullman Lecture 13: Minimum Spanning Trees • Mar 9, 2020
Midterm II • In Class Wednesday March 25 th • Working on a backup plan • Exactly the same format/rules as Midterm I • Topics: Graph Algorithms • Key definitions, properties • Representing graphs • DFS and topological sort • Shortest Paths: BFS, Dijkstra, Bellman-Ford } this week • Minimum spanning trees • Network flow
Minimum Spanning Trees
Network Design • Build a cheap, well connected network • We are given • a set of nodes ! = # $ , … , # ' • a set of potential edges ( ⊆ !×! • Want to build a network to connect these locations • Every # + , # , must be well connected • Must be as cheap as possible • Many variants of network design • Recall the bus routes problem from HW2
Minimum Spanning Trees (MST) nodes potentialedges edge costs • Input: a weighted graph - = !, (, . / • Undirected, connected, weights may be negative • All edge weights are distinct (makes life simpler) • Output: a minimum weight spanning tree 0 • A spanning tree of - is a subset of 0 ⊆ ( of the edges such that !, 0 forms a tree • Weight of a tree 0 is the sum of the edge weights Eet't • We’ll use 0 ∗ to denote “the” minimum spanning tree O Henesinter IT we
Minimum Spanning Trees (MST) 7 18 3 53 9 15 6 6 12 9 5 I 7 14 15 8 I 10 3
Minimum Spanning Trees (MST) 6 12 9 5 7 14 15 8 10 3
MST Algorithms • There are at least four reasonable MST algorithms • Borůvka’s Algorithm: start with 0 = ∅ , in each round add cheapest edge out of each connected component • Prim’s Algorithm: start with some 3 , at each step add cheapest edge that grows the connected component • Kruskal’s Algorithm: start with 0 = ∅ , consider edges in ascending order, adding edges unless they create a cycle • Reverse-Kruskal: start with 0 = ( , consider edges in descending order, deleting edges unless it disconnects
Cycles and Cuts • Cycle: a set of edges # $ , # 4 , # 4 , # 5 , … , # 6 , # $ 2 3 1 6 4 Cycle C = (1,2),(2,3),(3,4),(4,5),(5,6),(6,1) 5 8 7 • Cut: a partition of the nodes into 7, 7̅ 2 3 1 Cut S = {4, 5, 8} 6 4 Cutset = (5,6), (5,7), (3,4), (3,5), (7,8) S 5 8 7
Cycles and Cuts • Fact: a cycle and a cutset intersect in an even number of edges
Cycles and Cuts • Fact: removing an edge from a cycle doesn’t disconnect any nodes O O
Properties of MSTs • Cut Property: Let 7 be a cut. Let 9 be the minimum weight edge cut by 7 . Then the MST 0 ∗ contains 9 • We call such an 9 a safe edge • Cycle Property: Let : be a cycle. Let ; be the maximum weight edge in : . Then the MST 0 ∗ does not contain ; . • We call such an ; a useless edge
Proof of Cut Property • Cut Property: Let 7 be a cut. Let 9 be the minimum weight edge cut by 7 . Then the MST 0 ∗ contains 9 remove f Proof bycontradet ; 7 is the MST and Assume T N contain it doesnt e 9 __ If we add T there must 0 ∗ to e cycle C C contains 72 edges be a crossing the se f3 ut sole w f is lower than T T uEe3 the the total we remove f from If we Tau Edl Ef a tree is still is
Proof of Cycle Property • Cycle Property: Let : be a cycle. Let ; be the max weight edge in : . The MST 0 ∗ does not contain ; . Proof by contradiction ; 7 µ Assume T is the MST and J f contains 9 the graph THEM remove f 0 ∗ If we components 5,5 There is has two some cut Cf utle et by S etc edge a spanning hee 4 low a T 173 use weight is Thus
Ask the Audience • Assume - has distinct edge weights • True/False? If 9 is the edge with the smallest weight, then 9 is always in the MST 0 ∗ • True/False? If ; is the edge with the largest weight, then ; is never in the MST 0 ∗
The “Only” MST Algorithm • GenericMST: • Let 0 = ∅ • Repeat until 0 is connected: • Find one or more safe edges not in 0 • Add safe edges to 0 • Theorem: GenericMST outputs an MST Then it has multiple connected T not is Suppose ied is a safe edge crossing the art
5 I if Borůvka’s Algorithm i iii • Borůvka: • Let 0 = ∅ • Repeat until 0 is connected: • Let : $ , … , : 6 be the connected components of !, 0 • Let 9 $ , … , 9 6 be the safe edge for the cuts : $ , … , : < Br Ce • Add 9 $ , … , 9 6 to 0 Will contain duplicates • Correctness: every edge we add is safe
Borůvka’s Algorithm Label Connected Components 1 6 12 9 5 2 6 7 I 7 14 15 8 3 5 8 10 3 4
Borůvka’s Algorithm Add Safe Edges 1 6 12 9 5 2 6 7 7 14 15 8 3 5 8 10 3 4
Borůvka’s Algorithm Label Connected Components 1 6 12 9 5 1 1 1 7 14 15 8 2 1 1 10 3 2
Borůvka’s Algorithm Add Safe Edges 1 6 12 9 5 1 1 1 7 14 15 8 2 1 1 10 3 2
Borůvka’s Algorithm Done! 1 6 12 9 5 1 1 1 7 14 15 8 1 1 1 10 3 1
Borůvka’s Algorithm (Running Time) • Borůvka • Let 0 = ∅ • Repeat until 0 is connected: • Let : $ , … , : 6 be the connected components of !, 0 0 ntm • Let 9 $ , … , 9 6 be the safe edge for the cuts : $ , … , : < • Add 9 $ , … , 9 6 to 0 • Running time BFS the graph to find components • How long to find safe edges? Loop through edges keep track of m n ut edge for • How many times through the main loop? each component
Borůvka’s Algorithm (Running Time) FindSafeEdges(G,T): find connected components = > , … , = ? H using BFS Dfs let L[v] be the component of node @ Let S[i] be the safe edge of = A Hln I rally 4 for each edge (u,v) in E: If L[u] ≠ L[v]: If w(u,v) < w(S[L[u]]): S[L[u]] = (u,v) If w(u,v) < w(S[L[v]]): S[L[v]] = (u,v) Return {S[1],…,S[k]} May have duplicates
Borůvka’s Algorithm (Running Time) atleast • Claim: every iteration of the main loop halves the number of connected components. I Llogzln of then iterations If the claim true is
Borůvka’s Algorithm (Running Time) • Borůvka • Let 0 = ∅ • Repeat until 0 is connected: • Let : $ , … , : 6 be the connected components of !, 0 • Let 9 $ , … , 9 6 be the safe edge for the cuts : $ , … , : < • Add 9 $ , … , 9 6 to 0 is connected E l n V Ms so htm E 2am 11 04 • Running Time: per iteration • How long to find safe edges? 0 ntm • How many times through the main loop? OClogh OmLoglnl T.me
Prim’s Algorithm • Prim Informal • Let 0 = ∅ So Eos s • Let 3 be some arbitrary node and 7 = 3 • Repeat until 7 = ! • Find the cheapest edge 9 = C, # cut by 7 . Add 9 to 0 and add # to 7 • Correctness: every edge we add is safe
Prim’s Algorithm o i iii
Prim’s Algorithm Okhtm tog n Time 0cm log m Prim(G=(V,E)) let Q be a priority queue storing V value[v] ← ∞ , last[v] ←⊥ value[s] ← G for some arbitrary H while (Q ≠ ∅ ): u ← ExtractMin(Q) Extract nm n for each edge (u,v) in E: if v ∈ Q and w(u,v) < value[v]: DecreaseKey(v,w(u,v)) Decrease key m last[v] ← u T = {(1,last[1]),…,(n,last[n])} (excluding s) return T
Kruskal’s Algorithm • Kruskal’s Informal • Let 0 = ∅ • For each edge e in ascending order of weight: • If adding 9 would decrease the number of connected components add 9 to 0 • Correctness: every edge we add is safe
Kruskal’s Algorithm
Implementing Kruskal’s Algorithm • Union-Find : group items into components so that we can efficiently perform two operations: • Find(u): lookup which component contains u • Union(u,v): merge connected components of u,v • Can implement Union-Find so that • Find takes J 1 time • Any L Union operations takes J L log L time
Kruskal’s Algorithm (Running Time) • Kruskal’s Informal • Let 0 = ∅ • For each edge e in ascending order of weight: • If adding 9 would decrease the number of connected components add 9 to 0 • Time to sort: • Time to test edges: • Time to add edges:
Comparison • Can compute MST in time P Q RST Q • Boruvka’s Algorithm: • Only algorithm worth implementing • Low overhead, can be easily parallelized • Each iteration takes J U , very few iterations in practice • Prim’s/Kruskal’s Algorithms: • Reveal useful structure of MSTs • Templates for other algorithms
Recommend
More recommend