1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Greed is good. (Some of the time)
Minimum Spanning Tree CSE 331 Section 2 James Daly.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
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.
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.
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.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.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
1 Fibonacci heaps, and applications. 2 Yet a better MST algorithm (Fredman and Tarjan) Iteration i: We grow a forest, tree by tree, as follows. Start.
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 CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
Minimum-Cost Spanning Tree weighted connected undirected graph cost of spanning tree is sum of edge costs find spanning tree that has minimum cost Network.
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 Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Data Structures & Algorithms Graphs
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
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
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Tree.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Lecture 14 Minimum Spanning Tree (cont’d)
Chapter 23: Minimum Spanning Trees: A graph optimization problem
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
Presentation transcript:

1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)

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 cost

Example The graph has 10 edges. Spanning tree has only n - 1 = 7 edges. Need to either select 7 edges or discard

We are interested in: Finding a tree T that contains all the vertices spanning tree of a graph G spanning tree and has the least total weight over all minimum-spanning tree(MST) such trees minimum-spanning tree (MST)

Crucial Fact min-weight “bridge“ edge The Crucial Fact about MST e

The basis of all algorithms Proposition: Let G = (V,E) be a weighted graph, and let V 1 and V 2 be a partiotion of V (that is, V 1 and V 2 are two disjoint nonempty subsets and V = V 1 U V 2 ). Furthermore, let e be an edge with minimum weight from among those with one vertex in V 1 and the other in V 2. There is a minimum spanning tree T that has e as one of its edges.

Crucial Fact min-weight “bridge“ edge e a Justification: If T doesn’t use e but use a to connect V 1 and V 2, then T’ = T U { e} – { a } is another spanning tree lighter than T.

Edge Selection Greedy Strategies Start with an n-vertex 0-edge forest. Consider edges in ascending order of cost. Select edge if it does not form a cycle with already selected edges.  Kruskal’s method. Start with one vertex (a tree of a single vertex) and grow it into an n-vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge.  Prim’s method.

Edge Rejection Greedy Strategies Start with the connected graph. Repeatedly find a cycle and eliminate the highest cost edge on this cycle. Stop when no cycles remain. Consider edges in descending order of cost. Eliminate an edge provided this leaves behind a connected graph.

Kruskal’s Method Start with a forest that has no edges Consider edges in ascending order of cost. Edge (1,2) is considered first and added to the forest.

Kruskal’s Method Edge (7,8) is considered next and added Edge (3,4) is considered next and added. 4 Edge (5,6) is considered next and added. 6 Edge (2,3) is considered next and added. 7 Edge (1,3) is considered next and rejected because it creates a cycle.

Kruskal’s Method Edge (2,4) is considered next and rejected because it creates a cycle Edge (3,5) is considered next and added Edge (3,6) is considered next and rejected. 7 Edge (5,7) is considered next and added. 14

Kruskal’s Method n - 1 edges have been selected and no cycle formed. So we must have a spanning tree. Cost is 46. Minimum-cost spanning tree is unique when all edge costs are different

Prim’s Method Start with any vertex (a 1-vertex tree) Get a 2-vertex tree by adding a cheapest edge. 6 6 Get a 3-vertex tree by adding a cheapest edge Grow the tree one edge at a time until the tree has n - 1 edges (and hence has all n vertices)

Greedy Minimum-Cost Spanning Tree Methods Can prove that all result in a minimum-cost spanning tree. Let n = |V| and m = |E|. Kruskal’s uses union-find trees to run in –O(m log m) time (for sorting edges) Prim’s method:  O(n 2 ) using adjacency matrix.  O((n + m) log n) using adjacency list and a heap.

Pseudocode For Kruskal’s Method Let G = (V, E), n = |V|. Start with an empty set T of edges. while (E is not empty && |T| != n-1) { Let (u,v) be a least-cost edge in E. E = E - {(u,v)}. // delete edge from E if ((u,v) does not create a cycle in T) Add edge (u,v) to T. } if ( |T| == n-1) T is a mininum-cost spanning tree. else G has no spanning tree.

Data Structures For Kruskal’s Method Edge set E. Operations are:  Is E empty?  Select and remove a least-cost edge. Use a min heap of edges.  Initialize. O(m) time.  Remove and return m least-cost edges: O(m log m) time We may sort E first and select edges one by one from left to right. O(m log m) time

Data Structures For Kruskal’s Method Set of selected edges T. Operations are:  Does T have n - 1 edges?  Does the addition of an edge (u, v) to T result in a cycle?  Add an edge to T.

Data Structures For Kruskal’s Method Use an array linear list for the edges of T.  Does T have n - 1 edges? Check size of linear list. O(1) time.  Does the addition of an edge (u, v) to T result in a cycle? Not easy.  Add an edge to T. Add at right end of linear list. O(1) time.

Data Structures For Kruskal’s Method Does the addition of an edge (u, v) to T result in a cycle? Each component of T is a tree. When u and v are in the same component, the addition of the edge (u,v) creates a cycle. When u and v are in the different components, the addition of the edge (u,v) does not create a cycle.

Data Structures For Kruskal’s Method Each component of T is defined by the vertices in the component. Represent each component as a set of vertices.  {1, 2, 3, 4}, {5, 6}, {7, 8} Two vertices are in the same component iff they are in the same set of vertices.

Data Structures For Kruskal’s Method When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component In our set representation of components, the set that has vertex u and the set that has vertex v are united.  {1, 2, 3, 4} U {5, 6} => {1, 2, 3, 4, 5, 6}

Data Structures For Kruskal’s Method Initially, T is empty Initial sets are:  {1} {2} {3} {4} {5} {6} {7} {8} Does the addition of an edge (u, v) to T result in a cycle? If not, add edge to T. Each set has a name and let find(x) return the name of the set containing vertex x. s 1 = find(u); s 2 = find(v); if (s 1 != s 2 ) union(s 1, s 2 );

Data Structures For Kruskal’s Method Use FastUnionFind: each of union and find takes practically constant time. Initialize.  O(n) time. At most 2m finds and n-1 unions.  Very close to O(n + m). Min heap operations to get edges in increasing order of cost take O(m log m). Overall complexity of Kruskal’s method is O(n + m log m).

Prim‘s Algorithm 1.All vertices are marked as not visited 2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C) 3.The smallest-weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST and u is added into C. 4. The process is repeated until a spanning tree is formed

Prim’s MST Algorithm Prim’s algorithm: key[v] := min(key[v], w(u,v)) Dijkstra’s algorithm: key[v] := min(key[v], key[u]+w(u,v))

Prim’s Example

Prim’s Algorithm

Get spanning tree by connecting nodes with their parents:

Runtime for Prim’s Algorithm The inner loop takes O(E lg V) for the heap update inside the O(E) loop. This is over all executions, so it is not multiplied by O(V) for the while loop (this is included in the O(E) runtime through all edges. The Extract-Min requires O(V lg V) time. O(lg V) for the Extract-Min and O(V) for the while loop. Total runtime is then O(V lg V) + O(E lg V) which is O(E lg V) in a connected graph (a connected graph will always have at least V-1 edges). O(V) if using a heap O(V) O(lgV) if using a heap O(E) over entire while(Q<>NIL) loop O(lgV) to update if using a heap!