Minimum Spanning Tree Algorithms

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
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.
Minimum Spanning Tree Algorithms
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Paths Definitions Single Source Algorithms
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. 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.
Minimum Spanning Tree Algorithms. What is A Spanning Tree? u v b a c d e f Given a connected, undirected graph G=(V,E), a spanning tree of that graph.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
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.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
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)
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Algorithms and Data Structures Lecture XIII
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Short paths and spanning trees
Minimum Spanning Trees
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Algorithms and Data Structures Lecture XIII
Autumn 2015 Lecture 10 Minimum Spanning Trees
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Lecture 12 Algorithm Analysis
Dijkstra Algorithm.
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Autumn 2016 Lecture 10 Minimum Spanning Trees
Dijkstra’s Shortest Path Algorithm
Lecture 12 Algorithm Analysis
Finding Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum Spanning Trees
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Minimum Spanning Tree Algorithms Prim’s Algorithm

What is A Spanning Tree? A spanning tree for an undirected graph G=(V,E) is a subgraph of G that is a tree and contains all the vertices of G Can a graph have more than one spanning tree? Can an unconnected graph have a spanning tree? a b u e c v f d

Minimal Spanning Tree.  4 The weight of a subgraph is the sum of the weights of it edges. A minimum spanning tree for a weighted graph is a spanning tree with minimum weight. Can a graph have more than one minimum spanning tree? 4 9 3 b u e 14 2 10 15 c v f 3 8 d  Mst T: w( T )= (u,v)  T w(u,v ) is minimized

Example of a Problem that Translates into a MST The Problem Several pins of an electronic circuit must be connected using the least amount of wire. Modeling the Problem The graph is a complete, undirected graph G = ( V, E ,W ), where V is the set of pins, E is the set of all possible interconnections between the pairs of pins and w(e) is the length of the wire needed to connect the pair of vertices. Find a minimum spanning tree.

Greedy Choice We will show two ways to build a minimum spanning tree. A MST can be grown from the current spanning tree by adding the nearest vertex and the edge connecting the nearest vertex to the MST. (Prim's algorithm) A MST can be grown from a forest of spanning trees by adding the smallest edge connecting two spanning trees. (Kruskal's algorithm)

Notation Prim’s Selection rule Tree-vertices: in the tree constructed so far Non-tree vertices: rest of vertices Prim’s Selection rule Select the minimum weight edge between a tree-node and a non-tree node and add to the tree

The Prim algorithm Main Idea Select a vertex to be a tree-node while (there are non-tree vertices) { if there is no edge connecting a tree node with a non-tree node return “no spanning tree” select an edge of minimum weight between a tree node and a non-tree node add the selected edge and its new vertex to the tree } return tree 6 4 5 2 15 8 10 14 3 u v b a c d f

Implementation Issues How is the graph implemented? Assume that we just added node u to the tree. The distance of the nodes adjacent to u to the tree may now be decreased. There must be fast access to all the adjacent vertices. So using adjacency lists seems better Alternatively, adjacency matrix can also be used easily How should the set of non-tree vertices be represented? The operations are: build set delete node closest to tree decrease the distance of a non-tree node from the tree check whether a node is a non- tree node

Implementation Issues How should the set of non-tree vertices be represented? A priority queue PQ may be used with the priority D[v] equal to the minimum distance of each non-tree vertex v to the tree. Each item in PQ contains: D[v], the vertex v, and the shortest distance edge (v, u) where u is a tree node This means: build a PQ of non-tree nodes with initial values - fast build heap O (V ) building an unsorted list O(V) building a sorted list O(V) (special case)

Implementation Issues delete node closest to tree (extractMin) O(lg V ) if heap and O(V) if unsorted list O(1) sorted list decrease the distance of a non-tree node to the tree We need to find the location i of node v in the priority queue and then execute (decreasePriorityValue(i, p)) where p is the new priority decreasePriorityValue(i, p) O(lg V) for heap, O(1) for unsorted list O(V ) for sorted list (too slow)

Implementation Issues What is the location i of node v in a priority queue? Find in Heaps, and sorted lists O(n) Unsorted – if the nodes are numbered 1 to n and we use an array where node v is the v item in the array O(1) Extended heap We will use extended heaps that contain a “handle” to the location of each node in the heap. When a node is not in PQ the “handle” will indicate that this is the case This means that we can access a node in the extended heap in O(1), and check v  PQ in O(1) Note that the “handle” must be updated whenever a heap operation is applied

Implementation Issues 2. Unsorted list Array implementation where node v can be accessed as PQ[v] in O(1), and the value of PQ[v] indicates when the node is not in PQ.

Prim’s Algorithm 1. for each u V 2. do D [u ]   3. D[ r ]  0 4. PQ make-heap(D,V, {})//No edges 5. T   6. 7. while PQ   do 8. (u,e )  PQ.extractMin() 9. add (u,e) to T 10. for each v  Adjacent (u ) // execute relaxation 11. do if v PQ && w( u, v ) < D [ v ] 12. then D [ v ]  w (u, v) 13. PQ.decreasePriorityValue ( D[v], v, (u,v )) 14. return T // T is a mst. Lines 1-5 initialize the priority queue PQ to contain all Vertices. Ds for all vertices except r, are set to infinity. r is the starting vertex of the T The T so far is empty Add closest vertex and edge to current T Get all adjacent vertices v of u, update D of each non-tree vertex adjacent to u Store the current minimum weight edge, and updated distance in the priority queue

Prim’s Algorithm Initialization Prim (G ) 1. for each u V 2. do D [u ]   3. D[ r ]  0 4. PQ make-heap(D,V, {})//No edges 5. T  

Building the MST (D[v], v, (u,v) ) 14. return T // solution check 7. while PQ   do //Selection and feasibility 8. (u,e )  PQ.extractMin() // T contains the solution so far . 9. add (u,e) to T 10. for each v  Adjacent (u ) 11. do if v  PQ && w( u, v ) < D [ v ] 12. then D [ v ]  w (u, v) 13. PQ.decreasePriorityValue (D[v], v, (u,v) ) 14. return T

Time Analysis Using unsorted PQ Lines 1 - 6 run in O (V ) Max Size of PQ is | V | Count7 = O (V ) Count7(8) = O (V )  O(V ) Count7(10) = O(deg(u ) ) = O( E ) Count7(10(11)) = O(1)O( E ) Count7(10(11(12))) = O(1) O( E ) Count7(10(13)) =O( 1)  O( E ) So total time for Prim's Algorithm is O (V + V2 + E ) = O (V2 ) For Sparse/Dense graph : O( V2 ) Note growth rate unchanged for adjacency matrix graph representation 1. for each u V 2. do D [u ]   3. D[ r ]  0 4. PQ make-PQ(D,V, {})//No edges 5. T   6. 7. while PQ   do 8. (u,e )  PQ.extractMin() 9. add (u,e) to T 10. for each v  Adjacent (u ) 11. do if v PQ && w( u, v ) < D [ v ] 12. then D [ v ]  w (u, v) 13. PQ.decreasePriorityValue (D[v], v, (u,v)) 15. return T // T is a mst. The Loop begining in line 10 is executed O(deg(u ) ) = O( E ). It is based only on the number of edges in the graph not vertices. We assume the PQ is implemented as an array (sequence). Therefore, removeMinElement( ) , costs O(V) because you have to find the minimum element and you will have to shift the elements to fill in the whole. If we know the position of the key to be decrease then the cost of change the value is O(1).

Prim - extended Heap After Initialization PQ T A handle A B C 1 2 3 1 0, (A, {}) 2 6 B 5 C G , (B, {}) , (C, {}) 2 3 Prim (G, r) 1. for each u V 2. do D [u ]   3. D[ r ]  0 4. PQ make-heap(D,V, { }) 5. T   G A B C B 2 C 6 A 2 C 5 A 6 B 5

Prim - extended Heap Build tree - after PQ.extractMin handle A B C Null 2 1 T (A, {}) PQ 2 1 , (C, {}) 6 B 5 C G , (B, {}) 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T 2

Update B adjacent to A handle A B C Null 1 2 T (A, {}) PQ A 1 2, (B, {A, B}) 2 6 B 5 C G , (C, {}) 2 10. for each v  Adjacent (u ) 11. // relaxation operation // relaxation 11. do if v PQ && w( u, v ) < D [ v ] 12. then D [ v ]  w (u, v) 13. PQ.decreasePriorityValue ( D[v], v, (u,v))

Update C adjacent to A handle A B C Null 1 2 T (A, {}) PQ 1 2, (B, {A, B}) 6, (C, {A, C}) A 2 2 6 B 5 C G

Build tree - after PQ.extractMin handle A B C Null 1 T (A, {}) (B, {A, B}) PQ 6, (C, {A, C}) 1 A 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T 2 6 B 5 C G

Update C adjacent to B handle A B C Null 1 T (A, {}) (B, {A, B}) T PQ 5, (C, {B, C}) 1 A 10. for each v  Adjacent (u ) 11. // relaxation operation 2 6 B 5 C G

Build tree - after PQ.extractMin handle A B C Null T (A, {}) (B, {A, B}) (C, {B, C}) T (A, {}) PQ A 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T 2 6 B 5 C G

Prim - unsorted list After Initialization PQ A A B C 12 4 B 0, (A, {}) , (B, {}) , (C, {}) 5 C G G A B C B 12 C 4 Prim (G, r) 1. for each u V 2. do D [u ]   3. D[ r ]  0 4. PQ make-PQ(D,V, { }) 5. T   A 12 C 5 A 4 B 5

Build tree - after PQ.extractMin 12 4 B Null , (B, {}) , (C, {}) 5 C G 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T

Update B, C adjacent to A T (A, {}) PQ A A B C 12 4 B Null 12, (B, {A, B}) 4, (C, {A, C}) 5 C G 10. for each v  Adjacent (u ) 11. // relaxation operation

Build tree - after PQ.extractMin (C, {A, C}) A 12 PQ 4 B 5 A B C C Null 12, (B, {A, B}) Null G 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T

Update B adjacent to C T (A, {}) T (A, {}) (C, {A, C}) PQ A 12 4 B 5 A Null 5, (B, {C, B}) Null G 10. for each v  Adjacent (u ) 11. // relaxation operation

Build tree - after PQ.extractMin (C, {A, C}) (B, {C, B}) PQ A 12 4 B 5 A B C C Null Null Null G 7. while PQ   do 8. (u,e)  PQ.extractMin() 9. add (u,e) to T

Lemma 1 Let G = ( V, E) be a connected, weighted undirected graph. Let T be a promising subset of E. Let Y be the set of vertices connected by the edges in T. If e is a minimum weight edge that connects a vertex in Y to a vertex in V - Y, then T  { e } is promising. Note: A feasible set is promising if it can be extended to produce not only a solution , but an optimal solution. In this algorithm: A feasible set of edges is promising if it is a subset of a Minimum Spanning Tree for the connected graph. This is the same proof for Kruskal’s algorithm.

Outline of Proof of Correctness of Lemma 1 T is the promising subset, and e the minimum cost edge of Lemma 1 Let T ' be the MST such that T  T ' We will show that if e  T' then there must be another MST T" such that T {e}  T". Proof has 4 stages: 1. Adding e to T', closes a cycle in T' {e}. 2. Cycle contains another edge e' T' but e'  T 3. T"=T'  {e} - {e’ } is a spanning tree 4. T" is a MST

The Promising Set of Edges Selected by Prim Lemma 1 The Promising Set of Edges Selected by Prim T X e  Y  V -Y X MST T' but e  T'

Lemma 1 Since T' is a spanning tree, it is connected. Adding the edge, e, creates a cycle. X T'  {e} Stage 1 u v X  Y  V -Y e In T' there is a path from uY to v  V -Y. Therefore the path must include another edge e' with one vertex in Y and the other in V-Y. X Stage 2 e'

Lemma 1 If we remove e’ from T’  { e } the cycle disappears. T”=T’  { e } - {e’} is connected. T’ is connected. Every pair of vertices connected by a path that does not include e’ is still connected in T”. Every pair of vertices connected by a path that included e’ , is still connected in T” because there is a path in T" =T’  { e } - { e’ } connecting the vertices of e’. X e T " b X  Y  V -Y Stage 3 w( e )  w( e' ) By the way Prim picks the next edge

Lemma 1 w( e )  w( e' ) by the way Prim picks the next edge. The weight of T", w(T") = w (T' ) + w( e ) - w( e' )  w(T'). But w (T' )  w(T") because T' is a MST. So w (T' ) = w(T") and T" is a MST X e T" X  Y  V -Y Stage 4 Conclusion T  { e } is promising

Theorem : Prim's Algorithm always produces a minimum spanning tree. Proof by induction on the set T of promising edges. Base case: Initially, T =  is promising. Induction hypothesis: The current set of edges T selected by Prim is promising. Induction step: After Prim adds the edge e, T  { e } is promising. Proof: T  { e } is promising by Lemma 1. Conclusion: When G is connected, Prim terminates with |T | = |V | -1, and T is a MST.