Lecture 18: Minimum Spanning Trees Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Introduction to Algorithms
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Minimum spanning tree Shortest path algorithms
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

Lecture 18: Minimum Spanning Trees Shang-Hua Teng

Midterm Format Open Books Open Notes Cover topics up to the Strongly Connected Components Lecture

Midterm Format One problem of multiple choices One problem of short answers two problems of running algorithms on given examples Two problems on algorithm design and analysis Total 120 points

Weighted Undirected Graphs Positive weight on edges for distance JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v

Weighted Spanning Trees Weighted Undirected Graph G = (V,E,w): each edge (v, u)  E has a weight w(u, v) specifying the cost (distance) to connect u and v. Spanning tree T  E connects all of the vertices of G Total weight of T

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

Applications ATT Phone service Internet Backbone Layout

Growing a MST Generic algorithm Grow MST one edge at a time Manage a set of edges A, maintaining the following loop invariant: –Prior to each iteration, A is a subset of some MST At each iteration, we determine an edge (u, v) that can be added to A without violate this invariant –A  {(u, v)} is also a subset of a MST –(u, v) is called a safe edge for A

GENERIC-MST Loop in lines 2-4 is executed |V| - 1 times Any MST tree contains |V| - 1 edges The execution time depends on how to find a safe edge

First Edge Which edge is clearly safe (belongs to MST) Is the shortest edge safe? HBC GED F A

How to Find A Safe Edge? A Structure Theorem Let A be a subset of E that is included in some MST, let (S, V-S) be any cut of G that respects A Let (u, v) be a light edge crossing (S, V-S). Then edge (u, v) is safe for A –Cut (S, V-S): a partition of V –Crossing edge: one endpoint in S and the other in V-S –A cut respects a set of A of edges if no edges in A crosses the cut –A light edge crossing a partition if its weight is the minimum of any edge crossing the cut

First Edge So the shortest edge safe!!! HBC GED F A

An Example of Cuts and light Edges

A={(a,b}, (c, i}, (h, g}, {g, h}} S={a, b, c, i, e}; V-S = {h, g, f, d}: there are many kinds of cuts respect A (c, f) is the light edges crossing S and V-S and will be a safe edge More Example

Proof of The Theorem Let T be a MST that includes A, and assume T does not contain the light edge (u, v), since if it does, we have nothing more to prove Construct another MST T’ that includes A  {(u, v)} from T –Add (u,v) to T induce a cycle, and let (x,y) be the edge crossing (S,V-S), then w(u,v) <= w(x,y) –T’ = T – (x, y)  (u, v) –T’ is also a MST since W(T’) = W(T) – w(x, y) + w(u, v)  W(T) (u, v) is actually a safe edge for A –Since A  T and (x, y)  A  A  T’ –therefore A  {(u, v)}  T’

Property of MST MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees –Let T be an MST of G with an edge (u,v) in the middle –Removing (u,v) partitions T into two trees T 1 and T 2 –Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) (Do V 1 and V 2 share vertices? Why?) –Proof: w(T) = w(u,v) + w(T 1 ) + w(T 2 ) (There can’t be a better tree than T 1 or T 2, or T would be suboptimal)

Greedy Works

GENERIC-MST Loop in lines 2-4 is executed |V| - 1 times Any MST tree contains |V| - 1 edges The execution time depends on how to find a safe edge

Properties of GENERIC-MST As the algorithm proceeds, the set A is always acyclic G A =(V, A) is a forest, and each of the connected component of G A is a tree Any safe edge (u, v) for A connects distinct component of G A, since A  {(u, v)} must be acyclic Corollary: Let A be a subset of E that is included in some MST, and let C = (V C, E C ) be a connected components (tree) in the forest G A =(V, A). If (u, v) is a light edge connecting C to some other components in G A, then (u, v) is safe for A

Kruskal 1.A   2.for each vertex v  V[G] 3.do Make-Set(v) 4.sort edges of E into non-decreasing order by weight w 5.  edge (u,v)  E, taken in nondecreasing order by weight 6.do if Find-Set(u)  Find-Set(v) 7. then A  A  {(u,v)} 8.Union(u,v) 9. return A

Kruskal We select edges based on weight. In line 6, if u and v are in the same set, we can’t add (u,v) to the graph because this would create a cycle. Line 8 merges S u and S v since both u and v are now in the same tree..

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Example of Kruskal HBC GED F A

Complexity of Kruskal 4.sort edges of E into non-decreasing order by weight w How would this be done? We could use mergesort, with |E| lg |E| run time. 6.do if Find-Set(u)  Find-Set(v) This is O(E) since there are |E| edges. Union(u,v) from line 8 is also O(E). The run time, shown by methods we haven’t studied, turns out to be O(E lg E)

The Algorithms of Kruskal and Prim Kruskal’s Algorithm –A is a forest –The safe edge added to A is always a least-weight edge in the graph that connects two distinct components Prim’s Algorithm –A forms a single tree –The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in the tree

Prim’s Algorithm The edges in the set A always forms a single tree The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V At each step, a light edge is added to the tree A that connects A to an isolated vertex of G A =(V, A) Greedy since the tree is augmented at each step with an edge that contributes the minimum amount possible to the tree’s weight

Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm (Cont.) How to efficiently select the safe edge to be added to the tree? –Use a min-priority queue Q that stores all vertices not in the tree Based on key[v], the minimum weight of any edge connecting v to a vertex in the tree –Key[v] =  if no such edge  [v] = parent of v in the tree A = {(v,  [v]): v  V-{r}-Q}  finally Q = empty

Example: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); r

Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

Prim’s Algorithm At each step in the algorithm, a light edge is added to the tree, so we end up with a tree of minimum weight. Prim’s is a greedy algorithm, which is a type of algorithm that makes a decision based on what the current best choice is, without regard for future.