1 22c:31 Algorithms Union-Find for Disjoint Sets.

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
Advertisements

1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
CSE 326: Data Structures Disjoint Union/Find Ben Lerner Summer 2007.
Disjoint Union / Find CSE 373 Data Structures Lecture 17.
CSE 326: Data Structures Disjoint Union/Find. Equivalence Relations Relation R : For every pair of elements (a, b) in a set S, a R b is either true or.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 326: Data Structures Hashing Ben Lerner Summer 2007.
CSE 326: Data Structures Lecture #19 Disjoint Sets Dynamic Equivalence Weighted Union & Path Compression David Kaplan Today we’re going to get.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
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.
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lectures on Greedy Algorithms and Dynamic Programming
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
Fundamental Data Structures and Algorithms Peter Lee April 24, 2003 Union-Find.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
0 Union-Find data structure. 1 Disjoint set ADT (also Dynamic Equivalence) The universe consists of n elements, named 1, 2, …, n n The ADT is a collection.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
CMSC 341 Disjoint Sets. 2 Disjoint Set Definition Suppose we have N distinct items. We want to partition the items into a collection of sets such that:
MST, Topological Sort and Disjoint Sets
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Course Outline Introduction and Algorithm Analysis (Ch. 2)
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
CSE 332: Data Structures Disjoint Set Union/Find
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
CSE373: Data Structures & Algorithms Implementing Union-Find
CSE 417: Algorithms and Computational Complexity
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

1 22c:31 Algorithms Union-Find for Disjoint Sets

Equivalence Relations Relation R : For every pair of elements (a, b) in a set S, a R b is either true or false. If a R b is true, then a is related to b. An equivalence relation satisfies: 1.(Reflexive) a R a 2.(Symmetric) a R b iff b R a 3.(Transitive) a R b and b R c implies a R c 2

Equivalence Classes Given a set of things… { grapes, blackberries, plums, apples, oranges, peaches, raspberries, lemons, bananas } …define the equivalence relation All citrus fruit is related, all berries, all stone fruits, … …partition them into related subsets { grapes }, { blackberries, raspberries }, { oranges, lemons }, { plums, peaches }, { apples }, { bananas } Everything in an equivalence class is related to each other. 3

Determining equivalence classes Idea: give every equivalence class a name –{ oranges, limes, lemons } = “like-ORANGES” –{ peaches, plums } = “like-PEACHES” –Etc. To answer if two fruits are related: –FIND the name of one fruit’s e.c. –FIND the name of the other fruit’s e.c. –Are they the same name? 4

Building Equivalence Classes Start with disjoint, singleton sets: –{ apples }, { bananas }, { peaches }, … As you gain information about the relation, take UNION of sets that are now related: –{ peaches, plums }, { apples }, { bananas }, … E.g. if peaches R limes, then we get –{ peaches, plums, limes, oranges, lemons } 5

Disjoint Union - Find Maintain a set of pairwise disjoint sets. –{3,5,7}, {4,2,8}, {9}, {1,6} Each set has a unique name, one of its members –{3,5,7}, {4,2,8}, {9}, {1,6} 6

Union Union(x,y) – take the union of two sets named x and y –{3,5,7}, {4,2,8}, {9}, {1,6} –Union(5,1) {3,5,7,1,6}, {4,2,8}, {9}, 7

Find Find(x) – return the name of the set containing x. –{3,5,7,1,6}, {4,2,8}, {9}, –Find(1) = 5 –Find(4) = 8 8

Example 9 S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Find(8) = 7 Find(14) = 20 S {1,2,7,8,9,13,19,14,20 26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Union(7,20)

Implementing Disjoint Sets n elements, Total Cost of: m finds,  n-1 unions Target complexity: O(m+n) i.e. O(1) amortized per operation. O(1) worst-case for find as well as union would be great, but… Known result: find and union can be done practically in O(1) time 10

Implementing Disjoint Sets Observation: trees let us find many elements given one root… Idea: if we reverse the pointers (make them point up from child to parent), we can find a single root from many elements… Idea: Use one tree for each equivalence class. The name of the class is the tree root. 11

Up-Tree for Uinon/Find Initial state Intermediate state Roots are the names of each set.

Find Operation Find(x) follow x to the root and return the root Find(6) = 7

Union Operation Union(i,j) - assuming i and j roots, point i to j Union(1,7)

Simple Implementation Array of indices up Up[x] = 0 means x is a root.

Union 16 void Union(int[] up, int x, int y) { //precondition: x and y are roots// Up[x] := y } Constant Time!

Exercise Design Find operator –Recursive version –Iterative version 17 int Find(int[] up, int x) { //precondition: // x is in the range 1 to size ??? }

A Bad Case n … 1 23n Union(1,2) 1 2 3n Union(2,3) Union(n-1,n) … … n :::: Find(1) n steps!!

Now this doesn’t look good  Can we do better? Yes! 1.Improve union so that find only takes O(log n) Union-by-size Reduces complexity to Θ(m log n + n) 2.Improve find so that it becomes even better! Path compression Reduces complexity to almost O(m + n) 19

Weighted Union –Always point the smaller tree to the root of the larger tree W-Union(1,7) 2 4 1

Example Again n 1 23n Union(1,2) n Union(2,3) Union(n-1,n) … … :::: 1 2 3n … Find(1) constant time …

Analysis of Weighted Union With weighted union an up-tree of height h has weight at least 2 h. Proof by induction –Basis: h = 0. The up-tree has one node, 2 0 = 1 –Inductive step: Assume true for all h’ < h. 22 h-1 Minimum weight up-tree of height h formed by weighted unions T1T1 T2T2 TW(T 1 ) > W(T 2 ) > 2 h-1 Weighted union Induction hypothesis W(T) > 2 h h-1 = 2 h

Analysis of Weighted Union Let T be an up-tree of weight n formed by weighted union. Let h be its height. n > 2 h log 2 n > h Find(x) in tree T takes O(log n) time. Can we do better? 23

Worst Case for Weighted Union 24 n/2 Weighted Unions n/4 Weighted Unions

Example of Worst Cast (cont’) 25 After n -1 = n/2 + n/4 + …+ 1 Weighted Unions Find If there are n = 2 k nodes then the longest path from leaf to root has length k = log 2 (n). log 2 n

Elegant Array Implementation up weight

Weighted Union 27 void W_Union(int i,j){ //Pre: i and j are roots// int wi = weight[i]; int wj = weight[j]; if (wi < wj) { up[i] = j; weight[j] = wi + wj; } else { up[j] =i; weight[i] = wi + wj; }

Path Compression On a Find operation point all the nodes on the search path directly to the root PC-Find(3)

Self-Adjustment Works 29 PC-Find(x) x

Exercise: Draw the result of Find(e) 30 fha b c d e g i

Path Compression Find 31 int PC_Find(int i) { int r = i; while (up[r] != 0) //find root r = up[r]; if (i != r) { //compress path// int k = up[i]; while (k != r) { up[i] = r; i = k; k = up[k]; } return r; }

Interlude: A Really Slow Function Ackermann’s function is a really big function A(x, y) with inverse  (x, y) which is really small. How fast does  (x, y) grow?  (x, y) = 4 for x far larger than the number of atoms in the universe (2 300 )  shows up in: –Computation Geometry (surface complexity) –Combinatorics of sequences 32

A More Comprehensible Slow Function log* x = number of times you need to compute log to bring value down to at most 1 E.g. log* 2 = 1 log* 4 = log* 2 2 = 2 log* 16 = log* = 3 (log log log 16 = 1) log* = log* = 4 (log log log log = 1) log* = …………… = 5 Take this:  (m,n) grows even slower than log* n !! 33

Disjoint Union / Find with Weighted Union and Path Compression Worst case time complexity for a W-Union is O(1) and for a PC-Find is O(log n). Time complexity for m  n operations on n elements is O(m log* n) –Log * n < 7 for all reasonable n. Essentially constant time per operation! 34

Amortized Complexity For disjoint union / find with weighted union and path compression. –average time per operation is essentially a constant. – worst case time for a PC-Find is O(log n). An individual operation can be costly, but over time the average cost per operation is not. 35

Cute Application Build a random maze by erasing edges. 36

Cute Application Pick Start and End 37 Start End

Cute Application Repeatedly pick random edges to delete. 38 Start End

Desired Properties None of the boundary is deleted Every cell is reachable from every other cell. There are no cycles – no cell can reach itself by a path unless it retraces some part of the path. 39

A Cycle, not allowed 40 Start End

A Good Solution 41 Start End

A Hidden Tree 42 Start End

Number the Cells 43 Start End We have disjoint sets S ={ {1}, {2}, {3}, {4},… {36} } each cell is unto itself. We have all possible edges E ={ (1,2), (1,7), (2,8), (2,3), … } 60 edges total.

Basic Algorithm S = set of sets of connected cells E = set of edges 44 While there is more than one set in S pick a random, unused edge (x,y) from E u := Find(x); v := Find(y); if u  v { Union(u,v); remove (x, y) from E } else mark (x, y) as “used”; All remaining members of E form the maze

Example Step 45 Start End S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,30,32 33,34,35,36} Pick (8,14)

Example 46 S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Find(8) = 7 Find(14) = 20 S {1,2,7,8,9,13,19,14,20 26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Union(7,20)

Example 47 Start End S {1,2,7,8,9,13,19 14,20,26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Pick (19,20)

Example at the End 48 Start End S {1,2,3,4,5,6,7,… 36}

Problem 6-6 Suppose we are given the minimum spanning tree T of a given graph G (with n vertices and m edges) and a new edge e = (u; v) of weight w that we will add to G. Give an efficient algorithm to find the minimum spanning tree of the graph G + e. Your algorithm should run in O(n) time to receive full credit, although slower but correct algorithms will receive partial credit.

Problem [5] Let G = (V,E) be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source v.

Short Paths in a DAG If we update the edges in topologically sorted order, we correctly compute the shortest paths. Reason: the only paths to a vertex come from vertices before it in the topological sort s

Code for Shortest Paths in DAG 1DIJKSTRA(G, w, s) // DAG, weights, start vertex 2 for each vertex v in V[G] do 3d[v]   4  [v]  NIL 5d[s]  0 6Q  the topological order of V[G] starting at s 7while Q is not empty do 8u = EXTRACT-FIRST(Q) 9 for each vertex v in Adj[u] 10 d[v] = min{ d[v], d[u]+w(u, v)} 11 update  [v] if necessay

Shortest Paths in a DAG Theorem: For any vertex u in a DAG, if all the vertices before u in a topological sort of the dag have been updated, then d[u] is  (s,u), the shortest distance from s to u. Proof: By induction on the position of a vertex in the topological sort. Base case: d[s] is initialized to 0. Inductive hypothesis: Assume all vertices before u have been updated, and for all such vertices v, d[v] is  (s,v), the shortest distance from s to v. (continued)

Proof, Continued Inductive case: Some edge (v,u) where v is before u, must be on the shortest path to u, since there are no other paths to u. When v was updated, we set d[u] to d[v]+w(v,u) =  (s,v) + w(v,u) =  (s,u) Running time:  (V+E), same as topological sort