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:

Slides:



Advertisements
Similar presentations
1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation.
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.
Heaps Heaps are used to efficiently implement two operations:
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Minimum Spanning Trees (MST)
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick June 2010
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Spring 2015 Lecture 11: Minimum Spanning Trees
MST and Max Flow CS3233. Overview Two Graph Problems Minimum Spanning Tree Maximum Flow/Minimum Cut Problem One Data Structure Disjoint Sets.
1 22c:31 Algorithms Union-Find for Disjoint Sets.
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.
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,
Lecture X Disjoint Set Operations
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Union-find Algorithm Presented by Michael Cassarino.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Review for Final Exam Non-cumulative, covers material since exam 2 Data structures covered: –Treaps –Hashing –Disjoint sets –Graphs For each of these data.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
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.
 Quotient graph  Definition 13: Suppose G(V,E) is a graph and R is a equivalence relation on the set V. We construct the quotient graph G R in the follow.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
Union By Rank Ackermann’s Function Graph Algorithms Rajee S Ramanikanthan Kavya Reddy Musani.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Lecture 12 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
CSE373: Data Structures & Algorithms Implementing Union-Find
CMSC 341 Disjoint Sets.
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
CMSC 341 Disjoint Sets.
Running Time Analysis Union is clearly a constant time operation.
Timing with Optimization
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

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: –each item is in a set –no item is in more than one set Examples –UMBC students according to class rank –CMSC 341 students according to GPA –Graph vertices according to connected components The resulting sets are said to be disjoint sets.

3 Disjoint Set Terminology We identify a set by choosing a representative element of the set. It doesn’t matter which element we choose, but once chosen, it can’t change Two operations of interest: –find (x) -- determine which set x in in. The return value is the representative element of that set –union (x, y) -- make one set out of the sets containing x and y. Disjoint set algorithms are sometimes called union-find algorithms.

4 Disjoint Set Example Find the connected components of the undirected graph G=(V,E) (maximal subgraphs that are connected). for (each vertex v in V) put v in its own set for (each edge (u,v) in E) if (find(u) != find(v)) union(u,v) Now we can find if two vertices x and y are in the same connected component by testing find(x) == find(y)

5 Up-Trees A simple data structure for implementing disjoint sets is the up-tree. A H W H, A and W belong to the same set. H is the representative B X R F X, B, R and F are in the same set. X is the representative

6 Operations in Up-Trees Find is easy. Just follow pointer to representative element. The representative has no parent. find(x) { if (parent(x))// not the representative return(find(parent(x)); else return (x); }

7 Union Union is more complicated. Make one representative element point to the other, but which way? Does it matter? In the example, some elements are now twice as deep as they were before

8 Union(H, X) A H W B X R F A H W B X R F X points to H B, R and F are now deeper H points to X A and W are now deeper

9 A worse case for Union Union can be done in O(1), but may cause find to become O(n) A BCDE Consider the result of the following sequence of operations: Union (A, B) Union (C, A) Union (D, C) Union (E, D)

10 Array Representation of Up-tree Assume each element is associated with an integer i=0…n-1. From now on, we deal only with i. Create an integer array, A[n] An array entry is the element’s parent A -1 entry signifies that element i is the representative element.

11 Array Representation of Up-tree (cont) Now the union algorithm might be: Union(x,y) { A[y] = x;// attaches y to x } The find algorithm would be find(x) { if (A[x] < 0) return(x); else return(find(A[x])); } Performance: ???

12 Array Representation of Up-tree (cont) There are two heuristics that improve the performance of union-find. –Path compression on find –Union by weight

13 Path Compression Each time we do a find on an element E, we make all elements on path from root to E be immediate children of root by making each element’s parent be the representative. find(x) { if (A[x]<0) return(x); A[x] = find(A[x]); return (A[x]); } When path compression is done, a sequence of m operations takes O(m lg n) time. Amortized time is O(lg n) per operation.

14 Union by Weight Heuristic Always attach smaller tree to larger. union(x,y) { rep_x = find(x); rep_y = find(y); if (weight[rep_x] < weight[rep_y]) { A[rep_x] = rep_y; weight[rep_y] += weight[rep_x]; } else { A[rep_y] = rep_x; weight[rep_x] += weight[rep_y]; }

15 Performance w/ Union by Weight If unions are done by weight, the depth of any element is never greater than lg N. Intuitive Proof: –Initially, ever element is at depth zero. –When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). –How often can each union be done? -- lg n times, because after at most lg n unions, the tree will contain all n elements. Therefore, find becomes O(lg n) when union by weight is used -- even without path compression.

16 Performance with Both Optimizations When both optimizations are performed, for a sequence of m operations (m  n) (unions and finds), it takes no more than O(m lg* n) time. –lg*n is the iterated (base 2) logarithm of n. The number of times you take lg n before n becomes  1. Union-find is essentially O(m) for a sequence of m operations (Amortized O(1)).

17 A Union-Find Application A random maze generator can use union- find. Consider a 5x5 maze:

18 Maze Generator Initially, 25 cells, each isolated by walls from the others. This corresponds to an equivalence relation -- two cells are equivalent if they can be reached from each other (walls been removed so there is a path from one to the other).

19 Maze Generator (cont’d) To start, choose an entrance and an exit

20 Maze Generator (cont’d) Randomly remove walls until the entrance and exit cells are in the same set. Removing a wall is the same as doing a union operation. Do not remove a randomly chosen wall if the cells it separates are already in the same set.

21 MakeMaze MakeMaze(int size) { entrance = 0; exit = size-1; while (find(entrance) != find(exit)) { cell1 = a randomly chosen cell cell2 = a randomly chosen adjacent cell if (find(cell1) != find(cell2) union(cell1, cell2) }