Minimum Spanning Trees

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
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.
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
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic 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.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
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)
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
1 22c:31 Algorithms Minimum-cost Spanning Tree (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
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Single-Source Shortest Paths
Minimum Spanning Trees
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Short paths and spanning trees
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
CS200: Algorithm Analysis
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
Kruskal’s Minimum Spanning Tree Algorithm
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Single-Source Shortest Paths
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington These slides are based on CLRS and “Algorithms in C” by R. Sedgewick Last updated: 5/6/18

Weighted Graphs: G,w Each edge has a weight. Examples: A transportation network (roads, railroads, subway). The weight of each road can be: Length. Expected time to traverse. Expected cost to build. A computer network - the weight of each edge (direct link) can be: Latency. 1 7 2 5 3 4 6 10 20 30 15 18 25 Problem: find edges that connect all nodes with minimum total cost. E.g. , you want to connect all cities to minimize highway cost, but do not care about duration to get from one to the other (e.g. ok if route from A to B goes through most of the other cities). Solution: Minimum Spanning Tree (MST)

Spanning Tree - Minimum spanning tree (MST) 1 7 2 5 3 4 6 6 2 1 7 3 5 Is a Spanning Tree: connects all vertices of the graph. Has the smallest total weight of edges. It is not unique: Two different spanning trees may have the (same) minimum weight. - A spanning tree is a tree that connects all vertices of the graph. - The weight/cost of a spanning tree is the sum of weights of its edges. 1 7 2 5 3 4 6 10 20 30 15 18 25 20 6 20 30 2 10 1 15 7 30 3 15 25 10 5 4 18 Weight:20+15+30+20+30+25+18 = 158 Weight:10+20+15+30+20+10+15 = 120

Minimum-Cost Spanning Tree (MST) 1 7 2 5 3 4 6 10 20 30 15 18 25 Minimum-Cost Spanning Tree (MST) Assume that the graph is: connected undirected edges can have negative weights. Warning: later in the course (when we discuss Dijkstra's algorithm) we will make the opposite assumptions: Allow directed graphs. Not allow negative weights. 1 7 2 5 3 4 6 10 20 30 15

Kruskal's Algorithm

Kruskal's Algorithm Uses a ‘forest’ (a set of trees). Initially, each vertex in the graph is its own tree. Keep merging trees together, until end up with a single tree. Pick the smallest edge that connects two different trees. Time complexity: O(ElgV) Note: ElgE = O(ElgE2) = O(2ElgV) = O(ElgV) Depends on: 1. Sort edges (with what method?) or use a Min-Heap? 2. Find-Set and Union=> O(lgV) (with union-by-rank or weighted-union) – See the Union-Find slides for more information. MST_Kruskal(G,w) // N = |V| -----> O(ElgV) A = empty set of edges int id[N], sz[N] For v = 0 -> N-1 id(v) = v; sz(v)=1 Sort edges of G in increasing order of weight For each edge (u,v) in increasing order of weight ---> O(E) if Find_Set(u,id) == Find_Set(v,id) ------> Θ(lgV) add edge (u,v) to A union(u,v,id,sz) ------> Θ(lgV) return A Θ(V) (mergesort) Θ(ElgE) O(ElgV) (Θ(1) when given the representatives)

Kruskal's Algorithm Uses a ‘forest’ (a set of trees). Initially, each vertex in the graph is its own tree. Keep merging trees together, until end up with a single tree. Pick the smallest edge that connects two different trees The abstract description is simple, but the implementation affects the runtime. How to maintain the forest See the Union-Find algorithm. How to find the smallest edge connecting two trees: Sort edges: Y/N? Put edges in a min-heap?

Kruskal’s Algorithm MST_Kruskal(G,w) // N = |V| A = empty set of edges int id[N], sz[N] For v = 0 -> N-1 id(v) = v; sz(v)=1 Sort edges of G in increasing order of weight For each edge (u,v) in increasing order of weight if Find_Set(u,id) == Find_Set(v,id) add edge (u,v) to A union(u,v,id,sz) return A Idea: - Initially, each vertex in the graph is its own tree. - Keep merging trees together, until end up with a single tree (pick the smallest edge connecting different trees). See Union-Find slides as well. 1 7 2 5 3 4 6 20 u, v, weight 4 2 30 8 15 12 9 11 10 18 25 5 11

Kruskal’s 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 4 4 30 2 30 2 8 8 9 15 12 9 15 12 11 11 10 10 18 25 18 25 5 5 11 11 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 4 4 2 30 2 30 8 8 15 12 9 15 12 9 11 10 11 10 18 25 5 18 25 5 11

Kruskal’s 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 4 4 30 2 30 2 8 8 15 9 15 12 9 12 11 11 10 10 18 25 18 25 5 5 11 11 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 4 4 2 30 2 30 8 8 9 9 15 12 15 12 11 11 10 10 18 25 18 25 5 5 11 11 Edge (4,6,10) was not picked b.c. it makes a cycle.

Kruskal’s Algorithm and the Union-Find Structure Note the Union-Find method is under the “Data Structures for Disjoint Sets” in CLRS, Chapter 21, page 561,

Kruskal & Union Find 1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 2 3 4 5 6 7 1 2 3 7 6 5 4 1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 2 3 4 5 6 7 1 2 3 7 6 5 4 0-1 2-6

1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 2 6 3 4 5 7 1 2 3 7 6 5 4 2-6 1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 2 6 3 4 7 5 1 2 3 7 6 5 4 4-7 0-5

1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 3 2 6 4 7 5 1 3 2 7 6 5 4 0-5 1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 3 2 6 4 7 5 1 3 2 4 7 6 5 2-7 4-5

1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 7 3 2 6 4 5 1 3 2 7 6 5 4 4-5 1 7 2 5 3 4 6 8 20 30 12 10 11 18 25 15 9 idx Id Sz 1 7 3 2 6 4 5 8 1 3 2 8 7 6 5 4 1-3

Prim's Algorithm

MST-Prim (G,w,7) Worksheet MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v = 0 -> N-1 d[v]=inf //min weight of edge connecting v to MST p[v]=-1 //(p[v],v) in MST and w(p[v],v) =d[v] d[s]=0 Q = PriorityQueue(G.V,w) While notEmpty(Q) u = removeMin(Q,w) //u is picked for each v adjacent to u if v in Q and w(u,v)<d[v] p[v]=u d[v] = w(u,v) decreasedKeyFix(Q,v,d) Start from ANY vertex, s. (this is an MST). Repeat until all vertices are added to the MST: Add to the MST the edge (and the non-MST vertex of that edge) that is the smallest of all edges connecting vertices from the MST to vertices outside of the MST. 1 7 2 5 3 4 6 / 30 9 8 15 18 20 12 25 11 CLRS pseudocode. Run Prims algorithm starting at vertex 7. __ / __ = d[v] / p[v] (p = predecessor or parent) (Edge weight changes: (0,1,20) and (4,6,30). ) u,v,w 11

MST-Prim (G,w,7) Answer MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v = 0 -> N-1 d[v]=inf //min weight of edge connecting u to MST p[v]=-1 //(p[v],v) in MST and w(p[v],v) =d[v] d[s]=0 Q = PriorityQueue(G.V,w) While notEmpty(Q) u = removeMin(Q,w) //u is picked for each v adjacent to u if v in Q and w(u,v)<d[v] p[v]=u d[v] = w(u,v) decreasedKeyFix(Q,v,d) Start from ANY vertex, s. (this is an MST). Repeat until all vertices are added to the MST: Add to the MST the edge (and the non-MST tree vertex of that edge) that is the smallest of all edges connecting vertices from the MST to vertices outside of the MST. 1 7 2 5 3 4 6 12 / 7 11 / 4 5 / 7 25 / 4 20 /0 0 /- 9 / 7 30 / 4 30 9 8 15 18 20 12 25 11 8 / 5 CLRS pseudocode. Run Prims algorithm starting at vertex 7. __ / __ = d[v] / p[v] (p = predecessor or parent) (Edge weight changes: (0,1,20) and (4,6,30). ) u,v,w 7,4,5 7,2,9 2,6,4 4,5,11 5,0,8 0,3,15 3,1,11 4 / 2 11 11/ 3 18 / 5 The p array stores the tree. Branches: ( p(i),i )

Prim’s Algorithm Time Complexity Q – is a priority queue Time complexity: MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf //min weight of edge connecting v to MST p[v]=-1 //MST vertex, s.t. w(p[v],v) =d[v] d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and w(u,v)<d[v] p[v]=u d[v] = w(u,v); decreasedKeyFix(Q,v,d) //v is neither index nor key

Prim’s Algorithm Time Complexity Q – is a priority queue Time complexity: O(ElgV) O(V + VlgV + E lgV) = O(ElgV) connected graph => |E| ≥ (|V|-1) MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v=0 -> N-1 --------------> O(V) d[v]=inf //min weight of edge connecting v to MST p[v]=-1 //MST vertex, s.t. w(p[v],v) =d[v] d[s]=0 Q = PriorityQueue(d) ------------> O(V) (build heap) While notEmpty(Q) ------------> O(V) u = removeMin(Q,w) ------------> O(lgV) for each v adjacent to u //lines 7 & 9 together: ----> O(E) if v in Q and w(u,v)<d[v] //(touch each edge twice) p[v]=u d[v] = w(u,v); decreasedKeyFix(Q,v,d) //v is neither index nor key --------> O(lgV) O(V*lgV) O(E*lgV) from lines: 7,9,13

Prim’s Algorithm Implementation Details MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf p[v]=-1 d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and w(u,v)<d[v] p[v]=u d[v] = w(u,v); decreasedKeyFix(Q,v,d) //v is neither index nor key See if v is in Q. Θ(1) if we have the Array->Heap mapping. Else, O(V). Find heap node corresponding to v. Needed to update the heap for according to smaller d[v]. Note the difference between v and node in heap corresponding to v. See heap slides : Index Heap Example

Other Variations start with an empty priority queue For dense graphs, keep and array (instead of a priority queue => O(V2) – optimal for dense graphs ) – see Sedgewick if interested. Make sure you understand what happens with the data in an implementation: How do you know if a vertex is still in the priority queue? Going from a vertex to its place in the priority queue. The updates to the priority queue.

Proof of Correctness Is the MST a specific type of problem? What type of method is: Kruskal’s Prim’s Can we prove that they give the MST? Optimization Greedy, Greedy

Definitions (CLRS, pg 625) A cut (S, V-S) of an graph is a partition of its vertices, V. An edge (u,v) crosses the cut (S, V-S) if one of its endpoints is in S and the other in V-S. Let A be a subset of a minimum spanning tree over G. An edge (u,v) is safe for A if A {(u,v)} is still a subset of a minimum spanning tree. A cut respects a set of edges, A, if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum weight of any edge crossing the cut.

Correctness of Prim and Kruskall (CLRS, pg 625) Invariant for both Prim and Kruskal: At every step of the algorithm, the set, A, of edges is a subset of a MST. Let G = (V,E) be a connected, undirected, weighted graph. Let A be a subset of some minimum spanning tree, T, for G, let (S, V-S) be some cut of G that respects A, and let (u,v) be a light edge crossing (S, V-S). Then, edge (u,v) is safe for A. Proof: If (u,v) is part of T, done Else, in T, u and v must be connected through another path, p. One of the edges of p, must connect a vertex x from A and a vertex, y, from V-A. Adding edge(u,v) to T will create a cycle with the path p. (x,y) also crosses (A, V-A) and (u,v) is light => weight(u,v) ≤ weight(x,y) => weight(T’) ≤weight(T) , but T is MST => T’ also MST (where T’ is T with (u,v) added and (x,y) removed) and A U {(u,v)} is a subset of T’.

Extra Materials: 1 7 2 5 3 4 6 20 4 2 30 8 15 11 9 11 10 18 25 5 11

Prim’s Alg Example 2 MST-Prim(G, w, 2) (here: r = 2) MST_Prim(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf p[v]=-1 d[s]=0 Q = PriorityQueue(G.V,w) While notEmpty(Q) u = removeMin(Q,w) //u is picked for each v adjacent to u if v in Q and w(u,v)<d[v] p[v]=u d[v] = w(u,v) decreasedKeyFix(Q,v,d) Prim’s Alg Example 2 MST-Prim(G, w, 2) (here: r = 2) (This example shows the frontier (edges and vertices). 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 20 30 20 30 10 10 15 15 30 30 15 25 25 10 15 10 18 18 Red - current MST Purple - potential edges and vertices Blue – unprocessed edges and vertices. The algorithm will keep a MIN-Priority Queue for the vertices.

1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 20 30 20 30 10 10 15 15 30 30 15 25 25 10 15 10 18 18 1 7 2 5 3 4 ` 6 20 1 7 2 5 3 4 6 20 30 20 30 20 10 10 15 15 30 30 25 15 25 15 10 10 18 18 5, 4 already in MST => (5,4, 18) not picked (4,6,30) not better than (0,6,20) => no 6 update

Prim’s Alg Example 2 - cont 1 7 2 5 3 4 6 20 1 7 2 5 3 4 6 20 20 30 20 30 10 10 15 15 30 30 15 25 25 10 15 10 18 18

Kruskal’s Algorithm Example 2

Kruskal's Algorithm: Example 2 workout 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25

Kruskal's Algorithm: Example 2 workout 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25 1 7 2 5 3 4 6 10 20 30 15 25