CSE 332. Data Abstractions

Similar documents
Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Minimum spanning tree

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

1 Some loose ends from last time

Design and Analysis of Algorithms

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

Data Structures for Disjoint Sets

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

CMPSCI611: The Matroid Theorem Lecture 5

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Equivalence Relations. Disjoint Union / Find. Dynamic Equivalence Problem. Equivalence Classes

Data Structures and and Algorithm Xiaoqing Zheng

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

General Methods for Algorithm Design

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

CS675: Convex and Combinatorial Optimization Fall 2014 Combinatorial Problems as Linear Programs. Instructor: Shaddin Dughmi

Disjoint-Set Forests

CS781 Lecture 3 January 27, 2011

Algorithm Design Strategies V

CS 161: Design and Analysis of Algorithms

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Design and Analysis of Algorithms

Shortest Path Algorithms

Algorithm Design and Analysis

9 Union Find. 9 Union Find. List Implementation. 9 Union Find. Union Find Data Structure P: Maintains a partition of disjoint sets over elements.

Equivalence Relations. Equivalence Classes. A new question. Building Equivalence Classes. CSE 326: Data Structures Disjoint Union/Find

Sublinear-Time Algorithms

Algorithm Design and Analysis

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Breadth-First Search of Graphs

CS60007 Algorithm Design and Analysis 2018 Assignment 1

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Greedy Algorithms and Data Compression. Curs 2018

IS 709/809: Computational Methods in IS Research Fall Exam Review

Steiner Trees in Chip Design. Jens Vygen. Hangzhou, March 2009

Minimum Spanning Trees

Greedy Algorithms My T. UF

CS 580: Algorithm Design and Analysis

Sublinear Algorithms Lecture 3. Sofya Raskhodnikova Penn State University

Matching Residents to Hospitals

1. Problem I calculated these out by hand. It was tedious, but kind of fun, in a a computer should really be doing this sort of way.

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

15.1 Matching, Components, and Edge cover (Collaborate with Xin Yu)

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

CS675: Convex and Combinatorial Optimization Fall 2016 Combinatorial Problems as Linear and Convex Programs. Instructor: Shaddin Dughmi

Proof methods and greedy algorithms

Design and Analysis of Algorithms

Lecture 4. 1 Estimating the number of connected components and minimum spanning tree

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Homework Assignment 1 Solutions

Algorithm Design and Analysis (NTU CSIE, Fall 2017) Homework #3. Homework #3. Due Time: 2017/12/14 (Thu.) 17:20 Contact TAs:

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Union-Find Structures

Graph Transformations T1 and T2

HOMEWORK #2 - MATH 3260

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CS599: Convex and Combinatorial Optimization Fall 2013 Lecture 17: Combinatorial Problems as Linear Programs III. Instructor: Shaddin Dughmi

Equivalence Relation. Equivalence Classes. Building Equivalence Classes. Disjoint Union Find 2016/2/9. Determining equivalence classes

Minimal Spanning Tree From a Minimum Dominating Set

SAT Modulo Monotonic Theories

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Dynamic Programming: Shortest Paths and DFA to Reg Exps

MAKING A BINARY HEAP

ICS 252 Introduction to Computer Design

Lecture 1: 01/22/2014

McGill University Faculty of Science. Solutions to Practice Final Examination Math 240 Discrete Structures 1. Time: 3 hours Marked out of 60

Static Program Analysis

CSC236 Week 3. Larry Zhang

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Lecture 10: Lovász Local Lemma. Lovász Local Lemma

Discrete Optimization Lecture 5. M. Pawan Kumar

Online Learning. Jordan Boyd-Graber. University of Colorado Boulder LECTURE 21. Slides adapted from Mohri

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS4311 Design and Analysis of Algorithms. Analysis of Union-By-Rank with Path Compression

CSE 202: Design and Analysis of Algorithms Lecture 3

Small distortion and volume preserving embedding for Planar and Euclidian metrics Satish Rao

SI545 VLSI CAD: Logic to Layout. Algorithms

Correctness of Dijkstra s algorithm

18.312: Algebraic Combinatorics Lionel Levine. Lecture (u, v) is a horizontal edge K u,v = i (u, v) is a vertical edge 0 else

Algorithms and Data Structures (COMP 251) Midterm Solutions

Boolean circuits. Lecture Definitions

CS294-9 September 14, 2006 Adam Chlipala UC Berkeley

Ma/CS 6b Class 3: Stable Matchings

Design and Analysis of Algorithms

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Exam in Discrete Mathematics

Chapter 8 The Disjoint Sets Class

I may not have gone where I intended to go, but I think I have ended up where I needed to be. Douglas Adams

Greedy Algorithms and Data Compression. Curs 2017

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

Greedy Alg: Huffman abhi shelat

P, NP, NP-Complete, and NPhard

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

Transcription:

Adam Blank Lecture 23 Autumn 2015 CSE 332 Data Abstractions

CSE 332: Data Abstractions Graphs 4: Minimum Spanning Trees

Final Dijkstra s Algorithm 1 1 dijkstra(g, source) { 2 dist = new Dictionary(); 3 worklist = []; 4 for (v : V) { 5 if (v == source) { dist[v] = 0; } 6 else { dist[v] = ; } 7 worklist.add((v, dist[v])); 8 } 9 10 while (worklist.haswork()) { 11 v = next(); 12 for (u : v.neighbors()) { 13 dist[u] = min(dist[u], dist[v] + w(v, u)); 14 worklist.decreasekey(u, dist[u]); 15 } 16 } 17 18 return dist; 19 }

Final Dijkstra s Algorithm 2 1 dijkstra(g, source) { 2 dist = new Dictionary(); 3 worklist = []; 4 for (v : V) { 5 if (v == source) { dist[v] = 0; } 6 else { dist[v] = ; } 7 worklist.add((v, dist[v])); 8 } 9 10 while (worklist.haswork()) { 11 v = next(); 12 for (u : v.neighbors()) { 13 dist[u] = min(dist[u], dist[v] + w(v, u)); 14 worklist.decreasekey(u, dist[u]); 15 } 16 } 17 18 return dist; 19 } What Does Dijkstra s Algorithm Do Now?

Minimum Spanning Trees 3 Definition (Minimum Spanning Tree) Given a graph G = (V,E), find a subgraph G = (V,E ) such that G is a tree. V = V (G is spanning.) w(e) is minimized. e E Example b 30 c 40 d 10 20 15 45 a 25 e 35 f

What For? 4 Given a layout of houses, where should we place the phone lines to minimize cost? How can we design circuits to minimize the amount of wire? Implementing efficient multiple constant multiplications Minimizing the number of packets transmitted across a network Machine learning (e.g., real-time face verification) Graphics (e.g., image segmentation)

MST Example 5 MST Example Find a Minimum Spanning Tree of this graph Are there any others? Come up with a simple algorithm to find MSTs a 1 b 7 f 3 2 5 25 9 c 12 g 25 h 10 13 4 d 4 e MST Uniqueness If a graph has all unique edges, there is a unique MST. Otherwise, there might be multiple MSTs.

Back To Dijkstra s Prim s Algorithm 6 1 prim(g) { 2 conns = new Dictionary(); 3 worklist = []; 4 for (v : V) { 5 conns[v] = null; 6 worklist.add((v, )); 7 } 8 while (worklist.haswork()) { 9 v = next(); 10 for (u : v.neighbors()) { 11 if (w(v, u) < w(conns[u], u)) { 12 conns[u] = v; 13 worklist.decreasekey( 14 u, w(v, u) 15 ); 16 } 17 } 18 } 19 return conns; 20 } d 2 6 c 1 e 1 2 g 10 5 6 5 This really is almost identical to Dijkstra s Algorithm! We build up an MST by adding vertices to a done set and keeping track of what edge got us there. Do we have to use vertices? Can we use edges instead? b 1 f 1 3 a

A Simple Algorithm to Find MSTs 7 Simple MST 1 findmst(g) { 2 mst = {}; 3 for ((v, w) sorted(e)) { 4 foundv = foundw = false; 5 for ((a, b) mst) { 6 foundv = (a == v) (b == v); 7 foundw = (a == w) (b == w); 8 } 9 if (!foundv!foundw) { 10 mst.add((v, w)); 11 } 12 } 13 return mst; 14 } Some Questions! How many edges is the MST? Every MST will have V 1 edges; one edge to include each vertex What is the runtime of this algorithm? O( E lg( E ) + E V ), because sorting takes O( E lg( E )), the MST has at worst O( V ) edges, and we have to iterate through the MST E times. What is the slow operation of this algorithm? Checking if a vertex is already in our MST is very slow here. Can we do better?

Disjoint Sets ADT 8 A disjoint sets data structure keeps track of multiple sets which do not share any elements. Here s the ADT: UnionFind ADT find(x) union(x, y) Returns a number representing the set that x is in. Updates the sets so whatever sets x and y were in are now considered the same sets. Example 1 list = [1, 2, 3, 4, 5, 6]; 2 UF uf = new UF(list); // State: {1}, {2}, {3}, {4}, {5}, {6} 3 uf.find(1); // Returns 1 4 uf.find(2); // Returns 2 5 uf.union(1, 2); // State: {1, 2}, {3}, {4}, {5}, {6} 6 uf.find(1); // Returns 1 7 uf.find(2); // Returns 1 8 uf.union(3, 5); // State: {1, 2}, {3, 5}, {4}, {6} 9 uf.union(1, 3); // State: {1, 2, 3, 5}, {4}, {6} 10 uf.find(3); // Returns 1 11 uf.find(6); // Returns 6

Kruskal s Algorithm 9 Simple MST 1 kruskal(g) { 2 mst = {}; 3 forest = new UnionFind(V); 4 for ((v, w) sorted(e)) { 5 if (forest.find(v)!= forest.find(w)) { 6 mst.add((v, w)); 7 forest.union(v, w); 8 } 9 } 10 return mst; 11 } Forest Graph c b c 15 b 2 8 4 1 d g a d 20 g 1 a 7 6 4 10 e f e 3 f

Kruskal s Algorithm Correctness 10 Proving Correctness To prove that Kruskal s Algorithm is correct, we must prove: 1 The output is some spanning tree The output is some spanning tree 2 The output has minimum weight Kruskal s Algorithm Outputs SOME Spanning Tree We must show that the output, G is spanning, connected, and acyclic. The algorithm adds an edge whenever one of its ends is not already in the tree. This means that every vertex has an edge in the tree. It s acyclic because we check before adding an edge. Connected? The original graph is connected; there exists a path between u and v Consider the first edge that we look at which is on some path between u and v. Since we haven t previously considered any edge on any path between u and v, it must be the case that u and v are in distinct sets in the disjoint sets data structure. So, we add that edge. Since there is a path between every u and v in the graph in G, G is

Kruskal s Algorithm Correctness 11 Proving Correctness To prove that Kruskal s Algorithm is correct, we must prove: 1 The output is some spanning tree 2 The output has minimum weight So, now, we know that G is a spanning tree! Kruskal s Algorithm Outputs Some MINIMUM Spanning Tree Let the edges we add to G be, in order, e 1,e 2,...e k. Claim: For all 0 i k, {e 1,e 2,...e i } T i for some MST T i. Proof: We go by induction. Base Case. G for every graph G. Induction Hypothesis. Suppose the claim is true for iteration i. Induction Step. By our IH, we know that {e 1,...,e i } T i, where T i is some MST of G. We consider two cases: If e i+1 T i, then we choose T i+1 = T i, and we re done. Otherwise...

Kruskal s Algorithm Correctness 12 So far, we know... T i is a spanning tree of G. (earlier proof) that {e 1,...,e i } T i, where T i is some MST of G. (induction hypothesis) e i+1 / T i. (handled that case) Kruskal s Algorithm Outputs Some MINIMUM Spanning Tree (cont.) Claim: For all 0 i k, {e 1,e 2,...e i } T i for some MST T i. Since T i is a spanning tree, it must have some other edge (call it e ) which was added in place of e i+1. It follows that T i +e i+1 must have a cycle! Note that w(t i e +e i+1 ) = w(t i ) w(e )+w(e). Since we considered e i+1 before e, and the edges were sorted by weight, we know w(e) w(e ) w(e) w(e ) 0. So, w(t i e +e i+1 ) = w(t i ) w(e )+w(e) w(t i ) This means that T i e +e i+1 has no more than the weight of any MST!

Almost There... 13 So far, we know... T i is a spanning tree of G. (earlier proof) that {e 1,...,e i } T i, where T i is some MST of G. (induction hypothesis) e i+1 / T i. (handled that case) w(t i e +e i+1 ) w(t i ) Kruskal s Algorithm Outputs Some MINIMUM Spanning Tree (cont.) Claim: For all 0 i k, {e 1,e 2,...e i } T i for some MST T i. Finally, choose T i+1 = T i e +e i+1. We already know it has the weight of an MST. Note that e connects the same nodes as e ; so, it s also a spanning tree. That s it! For each i, we found an MST that extends the previous one. So, the last one must also be an MST!

Kruskal s Algorithm Runtime 14 Sort takes O(nlgn) We don t know how UnionFind works, but if we know... find is O(lgn) union takes O(lgn) time The runtime is O( E lg( E ) + E lg( V )) Just how does union-find work? Stay tuned!