1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.

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 Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
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.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
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.
Minimum Spanning Trees. a b d f g e c a b d f g e c.
Minimum Spanning Tree By Jonathan Davis.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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)
© 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.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
Graph Algorithms B. S. Panda Department of Mathematics IIT Delhi.
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.
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
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
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Minimum spanning trees
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm

2 Problem: Laying Telephone Wire Central office

3 Wiring: Naïve Approach Central office Expensive!

4 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

5 Definition of MST Input: connected, and undirected Graph G=(V,E) –Each edge (u,v) in E carries a weight w(u,v) Also referred to as cost (length of edge) to connect u and v. Output: A subset T of E that connects all of the vertices in V and whose total weight is minimized. Since the total weight is minimized, the subset T must be acyclic (no circuit). –Thus, T is a tree. We call it a spanning tree. The problem of determining the tree T is called the minimum-spanning-tree problem.

What is a Minimum-Cost Spanning Tree Example: The graph Has 16 spanning trees. Some are: The graph has two minimum-cost spanning trees, each with a cost of 6:

All 16 of its Spanning TreesComplete Graph

Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph Complete GraphMinimum Spanning Tree

9 Application of MST: an example In the design of electronic circuitry, it is often necessary to make a set of pins electrically equivalent by wiring them together. To interconnect n pins, we can use n-1 wires, each connecting two pins. We want to minimize the total length of the wires. Minimum Spanning Trees can be used to model this problem.

10 Electronic Circuits:

11 Electronic Circuits:

12 Here is an example of a connected graph and its minimum spanning tree: a b h cd e fg i Notice that the tree is not unique: replacing (b,c) with (a,h) yields another spanning tree with the same minimum weight.

13 Growing a MST(Generic Algorithm) Set A is always a subset of some minimum spanning tree. This property is called the invariant Property. An edge (u, v) is a safe edge for A if adding the edge to A does not destroy the invariant. A safe edge is just the CORRECT edge to choose to add to T. GENERIC_MST(G,w) 1A:={} 2while A does not form a spanning tree do 3find an edge (u,v) that is safe for A 4A:=A ∪ {(u,v)} 5return A

14 V-S↓ a b h cd e fg i S↑S↑ ↑ S ↓ V-S This figure shows a cut (S,V-S) of the graph. The edge (d,c) is the unique light edge crossing the cut.

15 Safe edge We need some definitions and a theorem. A cut (S,V-S) of an undirected graph G=(V,E) is a partition of V. An edge crosses the cut (S,V-S) if one of its endpoints is in S and the other is in V-S. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

16 Theorem 1 Given: –G=(V, E) be a connected, undirected graph with a real-valued weight function w defined on E. –A be a subset of E that is included in some minimum spanning tree for G. –Let (S,V-S) be any cut of G such that for any edge e=(u, v) in A, {u, v}  S or {u, v}  (V-S). – Let (x, y) be a light edge crossing (S,V-S). Then, edge (x, y) is safe for A. Proof: Let T opt be a minimum spanning tree. (Blue) A --a subset of T opt and (x, y)-- a light edge crossing (S, V-S) If (x, y) is NOT safe, then (x, y) is not in T. –(See the red edge in Figure) There MUST be another edge (x’, y’) in T opt crossing (S, V-S).(Green) We can replace (x’, y’) in T opt with (x, y) and get another tree T’ opt Since (x, y) is light, T’ opt is also optimal

17 Corollary Given: Let G=(V, E) be a connected, undirected graph with a real-valued weight function w defined on E. –Let A be a subset of E that is included in some minimum spanning tree for G. –Let C be a connected component (tree) in the forest G A =(V, A). –Let (u, v) be a light edge (shortest among all edges connecting C with others components) connecting C to some other component in G A. Then, edge (u,v) is safe for A. (For Kruskal’s algorithm)

18 The algorithms of Kruskal and Prim The two algorithms are elaborations of the generic algorithm. They each use a specific rule to determine a safe edge in line 3 of GENERIC_MST. In Kruskal's algorithm, –The set A is a forest. –The safe edge added to A is always a least-weight edge in the graph that connects two distinct components. In Prim's algorithm, –The set 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.

Kruskal’s Algorithm 19

A BC D E F G H I J Complete Graph

A BC D E F G H I J AABD BB B CD JC C E F D DH JEG FFGI GGIJ HJJI

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize)

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge

A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

A BC D E F G H I J A BC D E F G H I J Minimum Spanning TreeComplete Graph

35 Kruskal's algorithm (basic part) 1(Sort the edges in an increasing order) 2A:={} 3while E is not empty do { 3 take an edge (u, v) that is shortest in E and delete it from E 4 if u and v are in different components then add (u, v) to A } Note: each time a shortest edge in E is considered.

36 Kruskal's algorithm MST_KRUSKAL(G,w) 1A:={} 2for each vertex v in V[G] 3do MAKE_SET(v) 4sort the edges of E by nondecreasing weight w 5for each edge (u,v) in E, in order by nondecreasing weight 6do if FIND_SET(u) != FIND_SET(v) 7thenA:=A ∪ {(u,v)} 8UNION(u,v) 9return A

37 Disjoint-Set Keep a collection of sets S 1, S 2,.., S k, –Each S i is a set, e,g, S 1 ={v 1, v 2, v 8 }. Three operations –Make-Set(x)-creates a new set whose only member is x. –Union(x, y) –unites the sets that contain x and y, say, S x and S y, into a new set that is the union of the two sets. –Find-Set(x)-returns a pointer to the representative of the set containing x. –Each operation takes O(log n) time.

38 Our implementation uses a disjoint-set data structure to maintain several disjoint sets of elements. Each set contains the vertices in a tree of the current forest. The operation FIND_SET(u) returns a representative element from the set that contains u. Thus, we can determine whether two vertices u and v belong to the same tree by testing whether FIND_SET(u) equals FIND_SET(v). The combining of trees is accomplished by the UNION procedure. Running time O(|E| lg (|E|)).

39 a b h cd e fg i The execution of Kruskal's algorithm (Moderate part) The edges are considered by the algorithm in sorted order by weight. The edge under consideration at each step is shown with a red weight number.

40 a b h cd e fg i a b h cd e fg i

41 a b h cd e fg i a b h cd e fg i

42 a b h cd e fg i a b h cd e fg i

43 a b h cd e fg i a b h cd e fg i

44 a b h cd e fg i a b h cd e fg i

Prim’s Algorithm 45

A BC D E F G H I J Complete Graph

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Old GraphNew Graph A BC D E F G H I J

A BC D E F G H I J Complete GraphMinimum Spanning Tree A BC D E F G H I J

58 Prim's algorithm (basic part) MST_PRIM(G,w,r) 1.A={} 2.S:={r} (r is an arbitrary node in V) 3. Q=V-{r}; 4. while Q is not empty do { 5 take an edge (u, v) such that (1) u  S and v  Q (v  S ) and (u, v) is the shortest edge satisfying (1) 6 add (u, v) to A, add v to S and delete v from Q }

59 Prim's algorithm MST_PRIM(G,w,r) 1for each u in Q do 2key[u]:=∞ 3parent[u]:=NIL 4 key[r]:=0; parent[r]=NIL; 5Q  V[Q] 6while Q!={} do 7u:=EXTRACT_MIN(Q); if parent[u]  Nil print (u, parent[u]) 8for each v in Adj[u] do 9if v in Q and w(u,v)<key[v] 10thenparent[v]:=u 11key[v]:=w(u,v)

60 priority queue Q: three important operations –EXTRACT-MIN(Q) –takes log (n) times if there are n items in Q. –Insert an element (key u) into Q- takes log(n) time. –Update(u, k): update the key value of element u into k. It takes Olog (n) time for adaptable priority queue (heap).

61 Grow the minimum spanning tree from the root vertex r. Q is a priority queue, holding all vertices that are not in the tree now. key[v] is the minimum weight of any edge connecting v to a vertex in the tree. parent[v] names the parent of v in the tree. When the algorithm terminates, Q is empty; the minimum spanning tree A for G is thus A={(v,parent[v]):v ∈ V-{r}}. Running time: O(||E||lg |V|).

62 a b h cd e fg i a b h cd e fg i The execution of Prim's algorithm (basic part) the root vertex

63 a b h cd e fg i a b h cd e fg i

64 a b h cd e fg i a b h cd e fg i

65 a b h cd e fg i a b h cd e fg i

66 a b h cd e fg i Bottleneck spanning tree: A spanning tree of G whose largest edge weight is minimum over all spanning trees of G. The value of the bottleneck spanning tree is the weight of the maximum-weight edge in T. Theorem: A minimum spanning tree is also a bottleneck spanning tree. (Challenge problem)