Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Bipartite Matching, Extremal Problems, Matrix Tree Theorem.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Discussion #36 Spanning Trees
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Introduction to Graph Theory
1 Chapter 4 Minimum Spanning Trees Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Trees.
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Chapter 5 : Trees.
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Discrete Mathematicsq
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Chapter 5. Optimal Matchings
Short paths and spanning trees
Data Structures & Algorithms Graphs
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Graph Algorithm.
CSE 421: Introduction to Algorithms
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Trees
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Algorithms and Data Structures Lecture XII
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
CS 583 Analysis of Algorithms
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CSE 421: Introduction to Algorithms
Lecture 27 CSE 331 Nov 2, 2010.
Minimum Spanning Trees
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 12 Algorithm Analysis
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Winter 2019 Lecture 10 Minimum Spanning Trees
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Minimum Spanning Trees
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee CSE 421 Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee

Spanning Tree Given a connected undirected graph 𝐺 = 𝑉, 𝐸 . We call 𝑇 is a spanning tree of 𝐺 if All edges in 𝑇 are from 𝐸. 𝑇 includes all of the vertices of 𝐺. 𝑇 𝐺

Why spanning tree? Many problems is easy for tree. General framework: Approximate the graph by a tree. Solve the problem on a tree. We have covered different tree: BFS tree / Dijkstra tree Remember all shortest paths from 𝑠. DFS tree (see Trémaux tree in wiki) Every two adjacent vertices in G are related to each other as an ancestor and descendant in the tree There are many other different trees depending on applications.

Minimum Spanning Tree (MST) Given a connected undirected graph 𝐺 = (𝑉, 𝐸) with real-valued edge weights 𝑐 𝑒 ≥0. An MST 𝑇 is a spanning tree whose sum of edge weights is minimized. 24 4 4 23 9 6 9 6 18 5 5 11 11 16 8 8 7 7 10 14 21 𝑐 𝑇 = 𝑒∈𝑇 𝑐 𝑒 =50 𝐺 = (𝑉, 𝐸) See wiki for applications

Kruskal’s Algorithm [1956] Kruskal(G, c) { Sort edges weights so that 𝒄 𝟏 ≤ 𝒄 𝟐 ≤⋯≤ 𝒄 𝒎 . 𝑻←∅ foreach (𝒖∈𝑽) make a set containing singleton {𝒖} for 𝒊=𝟏 to 𝒎 Let 𝒖,𝒗 = 𝒆 𝒊 if (𝒖 and 𝒗 are in different sets) { 𝑻← 𝑻∪{ 𝒆 𝒊 } merge the sets containing 𝒖 and 𝒗 } return 𝑻 Kruskal Prim add the cheapest edge from the tree to another vertex. Sort edges weight. Add edges whenever it does not create cycle. The proof is easier. Exercise.

S V-S Cuts In a graph 𝐺=(𝑉,𝐸), a cut is a bipartition of V into disjoint sets 𝑆,𝑉−𝑆 for some 𝑆⊆𝑉. We show it by (𝑆,𝑉−𝑆). An edge 𝑒={𝑢,𝑣} is in the cut (𝑆,𝑉−𝑆) if exactly one of 𝑢,𝑣 is in 𝑆. x v u S V-S

Green edge is not in the MST Properties of the OPT Simplifying assumption: All edge costs 𝑐 𝑒 are distinct. Cut property: Let 𝑆 be any subset of nodes (called a cut), and let 𝑒 be the min cost edge with exactly one endpoint in 𝑆. Then every MST contains 𝑒. Cycle property. Let 𝐶 be any cycle, and let 𝑓 be the max cost edge belonging to 𝐶. Then no MST contains 𝑓. 10 7 7 S V-S 3 2 4 5 5 red edge is in the MST Green edge is not in the MST

Cycles and Cuts Claim. A cycle crosses a cut (from 𝑆 to 𝑉−𝑆) an even number of times. Proof. (by picture) u S V - S C Every time the cycle crosses a cut, it goes from 𝑆 to 𝑉−𝑆 or from 𝑉−𝑆 to 𝑆.

(coz all tree has 𝑛−1 edges) Cut Property: Proof Simplifying assumption: All edge costs 𝑐 𝑒 are distinct. Cut property. Let 𝑆 be any subset of nodes, and let 𝑒 be the min cost edge with exactly one endpoint in 𝑆. Then the 𝑇 ∗ contains 𝑒. Proof. By contradiction Suppose 𝑒 = {𝑢,𝑣} does not belong to 𝑇 ∗ . Adding 𝑒 to 𝑇 ∗ creates a cycle 𝐶 in 𝑇 ∗ . There is a path from 𝑢 to 𝑣 in 𝑇 ∗  there exists another edge, say 𝑓, that leaves 𝑆. 𝑇 = 𝑇 ∗  {𝑒} − {𝑓} is also a spanning tree. Since 𝑐 𝑒 < 𝑐 𝑓 , 𝑐 𝑇 <𝑐( 𝑇 ∗ ). This is a contradiction. (coz all tree has 𝑛−1 edges) f S v u e T*

Cycle Property: Proof Simplifying assumption: All edge costs 𝑐 𝑒 are distinct. Cycle property: Let 𝐶 be any cycle in 𝐺, and let 𝑓 be the max cost edge belonging to 𝐶. Then the MST 𝑇 ∗ does not contain 𝑓. Proof. By contradiction Suppose 𝑓 belongs to 𝑇 ∗ . Deleting 𝑓 from 𝑇 ∗ cuts 𝑇 ∗ into two connected components. There exists another edge, say 𝑒, that is in the cycle and connects the components. 𝑇 = 𝑇 ∗  {𝑒} − {𝑓} is also a spanning tree. Since 𝑐 𝑒 < 𝑐 𝑓 , 𝑐 𝑇 <𝑐( 𝑇 ∗ ). This is a contradiction. Every connected graph has a spanning tree. Hence it has at least 𝑛−1 edges. f S e T*

Proof of Correctness (Kruskal) Consider edges in ascending order of weight. Case 1: adding 𝑒 to 𝑇 creates a cycle, 𝑒 is the maximum weight edge in that cycle. cycle property show 𝑒 is not in any minimum spanning tree. Case 2: 𝑒=(𝑢,𝑣) is the minimum weight edge in the cut 𝑆 where 𝑆 is the set of nodes in 𝑢’s connected component. So, 𝑒 is in all minimum spanning tree. v u Case 2 e S Case 1 𝑒 This proves MST is unique if weights are distinct.

Implementation: Kruskal’s Algorithm Implementation. Use the union-find data structure. Build set 𝑇 of edges in the MST. Maintain a set for each connected component. O(𝑚 log⁡𝑛) for sorting and O(𝑚 log⁡𝑛) for union-find Kruskal(G, c) { Sort edges weights so that 𝒄 𝟏 ≤ 𝒄 𝟐 ≤⋯≤ 𝒄 𝒎 . 𝑻←∅ foreach (𝒖∈𝑽) make a set containing singleton {𝒖} for 𝒊=𝟏 to 𝒎 Let 𝒖,𝒗 = 𝒆 𝒊 if (𝒖 and 𝒗 are in different sets) { 𝑻← 𝑻∪{ 𝒆 𝒊 } merge the sets containing 𝒖 and 𝒗 } return 𝑻

Union Find Data Structure Each set is represented as a tree of pointers, where every vertex is labeled with longest path ending at the vertex To check whether A,Q are in same connected component, follow pointers and check if root is the same. D,2 W,1 A,1 B,0 P,0 Q,0 C,0 {W,P,Q} {A,B,C,D}

Union Find Data Structure Merge: To merge two connected components, make the root with the smaller label point to the root with the bigger label (adjusting labels if necessary). Runs in 𝑂(1) time D,2 A,1 B,0 C,0 W,1 P,0 Q,0 D,2 W,1 A,1 B,0 P,0 Q,0 C,0 At most one label must be adjusted W,2 Q,0 D,1 A,0 W,1 Q,0 D,1 A,0

Depth vs Size Claim: If the label of a root is 𝑘, there are at least 2 𝑘 elements in the set. Therefore, the depth of any tree in algorithm is at most log 2 ⁡𝑛 So, we can check if 𝑢,𝑣 are in the same component in time 𝑂(log 𝑛) D,2 A,1 B,0 C,0 W,1 P,0 Q,0

Depth vs Size: Correctness Claim: If the label of a root is 𝑘, there are at least 2 𝑘 elements in the set. Proof: By induction on 𝑘. Base Case (𝑘=0): this is true. The set has size 1. Inductive Step: If we merge roots with labels 𝑘 1 > 𝑘 2 , the number of vertices only increases while the label stays the same. If 𝑘 1 = 𝑘 2 , the merged tree has label 𝑘 1 +1, and by induction, it has at least 2 𝑘 1 + 2 𝑘 2 = 2 𝑘 1 +1 elements.

Kruskal’s Algorithm with Union Find Implementation. Use the union-find data structure. Build set 𝑇 of edges in the MST. Maintain a set for each connected component. O(m log n) for sorting and O(m log n) for union-find Kruskal(G, c) { Sort edges weights so that 𝒄 𝟏 ≤ 𝒄 𝟐 ≤⋯≤ 𝒄 𝒎 . 𝑻←∅ foreach (𝒖∈𝑽) make a set containing singleton {𝒖} for 𝒊=𝟏 to 𝒎 Let 𝒖,𝒗 = 𝒆 𝒊 if (𝒖 and 𝒗 are in different sets) { 𝑻← 𝑻∪{ 𝒆 𝒊 } merge the sets containing 𝒖 and 𝒗 } return 𝑻 Find roots and compare Merge at the roots