Minimum Spanning Trees

Slides:



Advertisements
Similar presentations
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Advertisements

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Spanning Trees.
C.Eng 213 Data Structures Graphs Fall Section 3.
Great Theoretical Ideas in Computer Science
Minimum Spanning Trees and Shortest Paths
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Topological Sort (topological order)
Data Structures & Algorithms Graphs
Graph Algorithm.
Data Structures and Algorithms
Discrete Mathematics for Computer Science
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Dijkstra’s Algorithm Vertex Distance Predecessor Processed s w x u v t
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Spanning Trees.
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Outline This topic covers Prim’s algorithm:
CS 583 Analysis of Algorithms
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Lecture 19: Minimum Spanning Trees
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSC 380: Design and Analysis of Algorithms
Lecture 20: Disjoint Sets
Lecture 18: Implementing Graphs
Lecture 12 Algorithm Analysis
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
Prims’ spanning tree algorithm
Minimum-Cost Spanning Tree
Lecture 27: More Graph Algorithms
Minimum Spanning Trees
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Minimum Spanning Trees Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Announcements Project 3 Part 1 grades are out (only sent to one partner) Project 3 Part 2 due tonight. Next written homework out soon. CSE 373 SP 18 - Kasey Champion

Warm Up Run Dijkstra’s Algorithm on this graph to find the shortest paths from s to t. c d 3 2 2 3 1 a 5 10 s 8 7 b t 1 CSE 373 SP 18 - Kasey Champion

Today Last Time: Topological Sorting and Strongly Connected Components Today: One more graph algorithm. Something completely different: Minimum Spanning Trees CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees It’s the 1920’s. Your friend at the electric company needs to choose where to build wires to connect all these cities to the plant. B 6 3 E 2 1 C 10 A 9 5 7 4 D 8 She knows how much it would cost to lay electric wires between any pair of locations, and wants the cheapest way to make sure electricity from the plant to every city. CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees It’s the 1920’s. Your friend at the electric company needs to choose where to build wires to connect all these cities to the plant. 1950’s boss phone each other. B 6 3 E 2 1 C 10 A 9 5 7 4 D F 8 She knows how much it would cost to lay electric wires between any pair of locations, and wants the cheapest way to make sure electricity from the plant to every city. phone Everyone can call everyone else. CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees It’s the 1920’s. Your friend at the electric company needs to choose where to build wires to connect all these cities to the plant. today ISP Internet with fiber optic cable B 6 3 E 2 1 C 10 A 9 5 7 4 D 8 She knows how much it would cost to lay electric wires between any pair of locations, and wants the cheapest way to make sure electricity from the plant to every city. cable Everyone can reach the server CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees What do we need? A set of edges such that: Every vertex touches at least one of the edges. (the edges span the graph) The graph on just those edges is connected. The minimum weight set of edges that meet those conditions. Assume all edge weights are positive. Claim: The set of edges we pick never has a cycle. Why? CSE 373 SP 18 - Kasey Champion

Aside: Trees Our BSTs had: Our heaps had: On graphs our tees: A root Left and/or right children Connected and no cycles Our heaps had: Varying numbers of children On graphs our tees: Don’t need a root (the vertices aren’t ordered, and we can start BFS from anywhere) An undirected, connected acyclic graph. Tree (when talking about graphs) CSE 373 SP 18 - Kasey Champion

MST Problem What do we need? A set of edges such that: Every vertex touches at least one of the edges. (the edges span the graph) The graph on just those edges is connected. The minimum weight set of edges that meet those conditions. Our goal is a tree! We’ll go through two different algorithms for this problem today. Given: an undirected, weighted graph G Find: A minimum-weight set of edges such that you can get from any vertex of G to any other on only those edges. Minimum Spanning Tree Problem CSE 373 SP 18 - Kasey Champion

Example Try to find an MST of this graph: B 6 3 E 2 1 C 10 A 9 5 7 4 D 8 CSE 373 SP 18 - Kasey Champion

Prim’s Algorithm Algorithm idea: choose an arbitrary starting point. Add a new edge that: Will let you reach more vertices. Is as light as possible We’d like each not-yet-connected vertex to be able to tell us the lightest edge we could add to connect it. CSE 373 SP 18 - Kasey Champion

Code PrimMST(Graph G) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed foreach(edge (source, v) ) v.dist = w(source,v) while(there are unprocessed vertices){ let u be the closest unprocessed vertex add u.bestEdge to spanning tree foreach(edge (u,v) leaving u){ if(w(u,v) < v.dist){ v.dist = w(u,v) v.bestEdge = (u,v) } mark u as processed CSE 373 SP 18 - Kasey Champion

Try it Out G 50 6 B 2 3 E PrimMST(Graph G) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed foreach(edge (source, v) ) v.dist = w(source,v) while(there are unprocessed vertices){ let u be the closest unprocessed vertex add u.bestEdge to spanning tree foreach(edge (u,v) leaving u){ if(w(u,v) < v.dist){ v.dist = w(u,v) v.bestEdge = (u,v) } mark u as processed 4 C 5 A 9 2 7 7 F D 8 Vertex Distance Best Edge Processed A B C D E F G CSE 373 SP 18 - Kasey Champion

Does This Algorithm Always Work? Prim’s Algorithm is a greedy algorithm. Once it decides to include an edge in the MST it never reconsiders its decision. Greedy algorithms rarely work. There are special properties of MSTs that allow greedy algorithms to find them. Robbie can tell you more offline. In fact MSTs are so magical that there’s more than one greedy algorithm that works. CSE 373 SP 18 - Kasey Champion

A different Approach Prim’s Algorithm started from a single vertex and reached more and more other vertices. Prim’s thinks vertex by vertex (add the closest vertex to the currently reachable set). What if you think edge by edge instead? Start from the lightest edge; add it if it connects new things to each other (don’t add it if it would create a cycle) This is Kruskal’s Algorithm. CSE 373 SP 18 - Kasey Champion

Kruskal’s Algorithm KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } CSE 373 SP 18 - Kasey Champion

Try It Out B KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } 6 3 E 2 1 C 10 A 9 5 7 4 D F 8 CSE 373 SP 18 - Kasey Champion

Kruskal’s Algorithm: Running Time KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } CSE 373 SP 18 - Kasey Champion

Kruskal’s Algorithm: Running Time Running a new [B/D]FS in the partial MST, at every step seems inefficient. Do we have an ADT that will work here? Not yet… Kasey will tell you about the “Union-Find” data structure next week. CSE 373 SP 18 - Kasey Champion

Try it Out KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } G 50 6 B 2 3 E 4 C 5 A 9 2 7 7 F D 8 CSE 373 SP 18 - Kasey Champion

Some Extra Comments Prim was the employee at Bell Labs in the 1950’s The mathematician in the 1920’s was Boruvka He had a different also greedy algorithm for MSTs. Boruvka’s algorithm is trickier to implement, but is useful in some cases. There’s at least a fourth greedy algorithm for MSTs… If all the edge weights are distinct, then the MST is unique. If some edge weights are equal, there may be multiple spanning trees. Prim’s/Dijkstra’s are only guaranteed to find you one of them. CSE 373 SP 18 - Kasey Champion

Aside: A Graph of Trees A tree is an undirected, connected, and acyclic graph. How would we describe the graph Kruskal’s builds. It’s not a tree until the end. It’s a forest! A forest is any undirected and acyclic graph CSE 373 SP 18 - Kasey Champion

EVERY TREE IS A FOREST.

Appendix: MST Properties, Another MST Application CSE 373 SP 18 - Kasey Champion

Why do all of these MST Algorithms Work? MSTs satisfy two very useful properties: Cycle Property: The heaviest edge along a cycle is NEVER part of an MST. Cut Property: Split the vertices of the graph any way you want into two sets A and B. The lightest edge with one endpoint in A and the other in B is ALWAYS part of an MST. Whenever you add an edge to a tree you create exactly one cycle, you can then remove any edge from that cycle and get another tree out. This observation, combined with the cycle and cut properties form the basis of all of the greedy algorithms for MSTs. CSE 373 SP 18 - Kasey Champion

One More MST application Let’s say you’re building a new building. There are very important building donors coming to visit TOMORROW, and the hallways are not finished. You have n rooms you need to show them, connected by the unfinished hallways. Thanks to your generous donors you have n-1 construction crews, so you can assign one to each of that many hallways. Sadly the hallways are narrow and you can’t have multiple crews working on the same hallway. Can you finish enough hallways in time to give them a tour? Given: an undirected, weighted graph G Find: A spanning tree such that the weight of the maximum edge is minimized. Minimum Bottleneck Spanning Tree Problem CSE 373 SP 18 - Kasey Champion

MSTs and MBSTs Minimum Spanning Tree Problem Given: an undirected, weighted graph G Find: A minimum-weight set of edges such that you can get from any vertex of G to any other on only those edges. Minimum Bottleneck Spanning Tree Problem Given: an undirected, weighted graph G Find: A spanning tree such that the weight of the maximum edge is minimized. B B 2 2 3 3 C C A A 1 1 2 2 4 4 D D Graph on the right is a minimum bottleneck spanning tree, but not a minimum spanning tree. CSE 373 SP 18 - Kasey Champion

Finding MBSTs Algorithm Idea: want to use smallest edges. Just start with the smallest edge and add it if it connects previously unrelated things (and don’t if it makes a cycle). Hey wait…that’s Kruskal’s Algorithm! Every MST is an MBST (because Kruskal’s can find any MST when looking for MBSTs) but not vice versa (see the example on the last slide). If you need an MBST, any MST algorithm will work. There are also some specially designed MBST algorithms that are faster (see Wikipedia) Takeaway: When you’re modeling a problem, be careful to really understand what you’re looking for. There may be a better algorithm out there. CSE 373 SP 18 - Kasey Champion