CS 4349 Lecture October 18th, 2017

Similar documents
CS 4349 Lecture October 23rd, 2017

Minimum Spanning Trees

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CS 4349 Lecture August 21st, 2017

Kruskal s MST Algorithm

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

tree follows. Game Trees

Minimum Spanning Trees My T. UF

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

6.1 Minimum Spanning Trees

Week 11: Minimum Spanning trees

CS 561, Lecture 9. Jared Saia University of New Mexico

2 A Template for Minimum Spanning Tree Algorithms

Minimum Spanning Trees

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Minimum spanning trees

22 Elementary Graph Algorithms. There are two standard ways to represent a

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

Lecture 4: Trees and Art Galleries

Minimum-Cost Spanning Tree. Example

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Dijkstra s algorithm for shortest paths when no edges have negative weight.

22 Elementary Graph Algorithms. There are two standard ways to represent a

Lecture 10. Finding strongly connected components

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

Announcements Problem Set 5 is out (today)!

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

Lecture Notes for Chapter 23: Minimum Spanning Trees

Lecture Summary CSC 263H. August 5, 2016

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Distributed minimum spanning tree problem

Greedy Algorithms Part Three

MITOCW watch?v=tkwnms5irbu

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

11.9 Connectivity Connected Components. mcs 2015/5/18 1:43 page 419 #427

Problem Score Maximum MC 34 (25/17) = 50 Total 100

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Professor: Padraic Bartlett. Lecture 9: Trees and Art Galleries. Week 10 UCSB 2015

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Lecture 25 Spanning Trees

Minimum Spanning Tree

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Design and Analysis of Algorithms

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

CS 6783 (Applied Algorithms) Lecture 5

Chapter 23. Minimum Spanning Trees

Exam 3 Practice Problems

Solving Linear Recurrence Relations (8.2)

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Principles of Algorithm Design

Minimum Spanning Trees

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

14.1 Encoding for different models of computation

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Undirected Graphs. Hwansoo Han

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

Chapter 9 Graph Algorithms

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

(Refer Slide Time: 02.06)

Kuratowski Notes , Fall 2005, Prof. Peter Shor Revised Fall 2007

Lecture 6 Basic Graph Algorithms

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Minimum spanning trees

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

(Refer Slide Time: 00:18)

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Lecture 20 : Trees DRAFT

20 Minimum Spanning Trees

CSE 100: GRAPH ALGORITHMS

Figure 1: A directed graph.

Problem Set 2 Solutions

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science Algorithms For Inference Fall 2014

Practice Problems for the Final

Priority queue ADT part 1

CS 561, Lecture 10. Jared Saia University of New Mexico

5 MST and Greedy Algorithms

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Simple Graph. General Graph

Minimum Spanning Trees

looking ahead to see the optimum

Minimum Spanning Trees. 7.1 Distinct Edge Weights

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Transcription:

CS 4349 Lecture October 18th, 2017 Main topics for #lecture include #minimum_spanning_trees. Prelude Homework 6 due today. Homework 7 due Wednesday, October 25th. Homework 7 has one normal homework problem. It also has two extra credit problems. If you get 10s on both of them, that s worth 1/4 of the midterm or 7.5% of your final grade. It may not sound like much, but it should be more than enough to boost you at least one third of a letter grade. They re extra credit, so they don t effect the curve. If you re satisfied with your grade, you can just pretend they aren t there. They re the same difficulty as an exam problem, but you get the whole week. Because you have the time and as a check that you understand the solution you write, I m going to grade those extra credit problems the same as I do any other extra credit problem. Part of the score will go toward a proof a correctness, although these proofs should be relatively light. See the exam solutions to understand the level of detail I m expecting. I expect you not to look toward outside sources for help. I read your feedback. Some people suggested more interactive or entertaining examples like the Towers of Hanoi. My son doesn t have toys appropriate for every lecture, but I ll try my best to help you stay focused. We re getting to more visually interesting graph algorithms now, so that might help. Another comment I saw was that the extra credit wasn t worth enough points to bother with. I have to disagree. Two students actually had their midterm grade go up a third of a letter, because they did the two extra credit problems from the homework, and I m trying to balance it so a good amount of extra credit across the semester will be worth about that much for final grades. Minimum Spanning Tree Alright, we re going to continue graph algorithms this week, but hopefully the lecture will be a little less dry. So consider this problem: Let s say we have a bunch of computers that we want to connect in a network. We can represent the computers as circles on the board.

We can connect some pairs of computers together using a single link, but each choice of link has a different cost. And our goal here is to connect the computers together in the cheapest way possible so you can get from any one computer to another just by following the links we choose. So I claim this is the best way. I ll explain how I know that as today s lecture. Computers are still pretty dry, so maybe we could talk about a different problem instead. Suppose we want to build a sewer system. The sewer has all these junction points and you can build lines between some pairs of them, but each line has a different cost. What s the cheapest set of lines to guarantee the sewer is connected? OK, so these are the same problem. Formally, let s say we re given a connected, undirected, weighted graph G = (V, E). The weights are a function w : E R that assigns weight w(e) to each edge e. We want to find the minimum spanning tree, the spanning tree T that minimizes w(t) = sum_{e in T} w(e). For simplicity, we ll assume edge weights are distinct: w(e) w(e ) for any pair of edges e and e. One neat consequence is that the minimum spanning tree is unique if you do this. I probably won t prove that fact today. If you do have ties, then you might have multiple minimum spanning trees. For example, all spanning trees have V - 1 edges, so if the weights are all 1, then every spanning tree is a minimum spanning tree. You can avoid the assumption if you have a consistent way to break ties, but that s all I ll say about that today. The Only Minimum Spanning Tree Algorithm Last week, we talked about the one and only graph traversal algorithm. Depending on how we implemented it, we got all the variants you normally think of like depth-first search and breadth-first search.

The same idea applies to minimum spanning trees. There is really only one minimum spanning tree algorithm (at least for this class), and all need to do to get your name attached to something is to figure out a fast way to implement it. So the idea is we re going to be adding edges one-by-one to build the minimum spanning tree. Let s say this is a snapshot of what the world looks like halfway through. We have this acyclic subgraph F we ll call the intermediate spanning forest. We re only adding edges, so F is a subgraph of the minimum spanning tree. And that means every component of F is a minimum spanning tree of its vertices. In particular, F consists of n one-node trees before we ve added any edges. As we add edges to F, we ll merge these trees together. The algorithm halts when F consists of a single n-node tree, the minimum spanning tree. But which edges are we going to add to F? We ll define two types of edges based on the current intermediate spanning forest F. Useless edges are outside of F, but both endpoints are in the same component of F. draw arrow to a useless edge Lemma: The minimum spanning tree contains no useless edge. If we added a useless edge to F, it would create a cycle! Each component is associated with one safe edge, the minimum weight edge with one endpoint in that component. Different components may or may not have different safe edges. Some edges are neither useless nor safe, at least for this forest F. Lemma: The minimum spanning tree contains every safe edge. Proof (of the day): We ll prove something stronger. For any subset of vertices S, the minimum spanning tree contains the minimum-weight edge e with exactly one endpoint in S. So a couple weeks ago, we talked about greedy algorithms and exchange arguments. We ll use one of those here. Suppose to the contrary that the minimum spanning tree T does not contain e. T contains a path between the endpoints of e, but that path starts in S and ends not-in S. There must be some edge e on the path with one endpoint in S. Here s the situation: The black vertices are S, the rest are V - S. T is acyclic, so if we remove e, we create a spanning forest with two components.

Each component contains an endpoint of e. Otherwise, the path we were talking about would not cross to the other component or contain e. So that means we can add e in to get a new spanning tree T = T - e + e. But w(e) < w(e ), so this new spanning tree has smaller total weight. T must not have been the minimum spanning tree. So, we can throw away useless edges, and we want to include every safe edge. Here s our algorithm: repeatedly add one or more safe edges to the evolving forest F. As we add new edges to F, some undecided edges become safe, and some edges become useless. We need to figure out which safe edges to add in each iteration, and how to identify new safe and new useless edges. Alright, so now it s your turn. I ll pick a couple of you and ask for a safe edge. Don t worry about what the other person is thinking about. Right, it didn t matter which safe edge you chose or even if you chose multiple safe edges at once. But we should probably write an algorithm for all graphs. I have some examples in mind, but we can go in any order. If you had to write an algorithm, what kind of safe edges would you choose? Borvka s Algorithm Found by Borvka in 1926. As often happens, many others rediscovered it including Sollin in the 1960s, and now some people call it Sollin s algorithm. Borvka: Add ALL the safe edges and recurse. How do we implement it? Count and label components of F so all vertices in the first component have label 1, all in the second have label 2,. I wrote an O(E) time algorithm for this last Wednesday. If F has one component, we re done. Otherwise, we ll compute an array S[1.. V] of safe edges where S[i] is the minimum weight edge with one endpoint in component i (or NULL if component i does not exist). To compute S we ll loop through all the edges, comparing the edge to the one stored for the label of its endpoints.

CountAndLabel takes O(V) time, because F has at most V - 1 edges. AddAllSafeEdges takes O(E) time and V E + 1, so the while loop takes O(E) time per iteration. In the worst case, each iteration combines pairs of components, reducing the total number of components by a factor of 2. So there are O(log V) iterations. The total running time is O(E log V). The original descriptions of this algorithm were too complicated, so nobody really took it seriously and it rarely appears in textbooks. But it has a lot of nice features. That O(log V) is a worst-case upper bound, and the algorithm may run much faster in practice. For some classes of graphs, like those that can be drawn in the plane without edge crossings, you can implement this algorithm so it runs in O(E) time. This algorithm is really easy to parallelize since you can search for the safe edge of each component in a separate thread. So if you need to implement minimum spanning trees, use this algorithm. I teach the others mostly so we can practice thinking about and proving things about minimum spanning trees. Jarník s ( Prim s ) Algorithm Found by Jarník in 1929. Prim found it 1957, and somehow he won the naming game. In this algorithm, F always has one non-trivial component T, and the rest are isolated vertices. Jarník: Repeatedly add T s safe edge to T.

We keep all edges adjacent to T in a priority queue. When we pull out an edge, we check if both endpoints are in T or not. If both endpoints are in T, we throw the edge away, because it is useless. Otherwise, the edge is safe for T and we add it. This is really just that third example from last week of using a priority queue in the generic graph traversal algorithm. There are O(E) priority queue operations taking O(log E) time each, but E = O(V^2) so the algorithm runs in O(E log V) time. You ve seen the pseudocode already, so I ll spare you! Kruskal s Algorithm Found by Kruskal in 1956. Oh, this one got the right name! Kruskal: Scan all edges in increasing weight order; if an edge is safe, add it to F. Since we re examining edges in increasing order of weight, an edge is safe if and only if its endpoints are in different components of F. If they were in the same component, well by definition that edge is useless. And if we find an edge between different components, it must be the cheapest such edge. Otherwise, the real safe edge for one of those components would have been found earlier and declared safe or useless. To implement the algorithm, we use something called a Union-Find data structure. In our setting, every component of F has a leader node. If you call Find(v), then it returns the leader of v s component. So Find(u) = Find(v) if and only if u and v are in the same component. If you call Union(u, v), then you are declaring to the data structure that you are combining u and v s components, and you select a new leader for the one combined component. MakeSet(v) makes v the leader of its own one-vertex component. So, for each edge in increasing order of weight, we ll check if the two leaders differ using Find and if they do, we ll add the edge to F and Union its two endpoints.

We ll take O(E log E) = O(E log V) time to sort the edges. We do O(E) find operations, one per edge. We do O(V) union operations, one per edge of the minimum spanning tree. Using the best Union-Find data structure these operations take O(E alpha(e, V)) time total where alpha(e, V) is a function that grows incredibly slowly. Like, for any graph you might possibly work with, alpha(e, V) will be no more than 5. But you can pick a number of vertices large enough to make that value 6 or even gasp 7. E alpha(e, V) = o(e log V), so the time to sort dominates. The total running time is O(E log V) just like before.