1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
COL 106 Shweta Agrawal and Amit Kumar
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.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
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
Disjoint Sets Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of union.
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:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
CSE 326: Data Structures Lecture #19 Disjoint Sets Dynamic Equivalence Weighted Union & Path Compression David Kaplan Today we’re going to get.
Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Priority Queues and Disjoint Sets CSCI 2720 Spring 2005.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
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,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Union-find Algorithm Presented by Michael Cassarino.
Union Find ADT Data type for disjoint sets: makeSet(x): Given an element x create a singleton set that contains only this element. Return a locator/handle.
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.
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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 
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 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.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
Parent Pointer Implementation
CSE 326: Data Structures: Set ADT
CSE 373: Data Structures and Algorithms
Data Structures for Disjoint Sets
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
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.
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
ICS 353: Design and Analysis of Algorithms
Equivalence Relations
CSE373: Data Structures & Algorithms Implementing Union-Find
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.
Minimum Spanning Trees
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei

2 Issues: The equivalence problem The equivalence problem The first algorithm The first algorithm Smart union algorithms Smart union algorithms Union and Find Union and Find

3 Equivalence Relations A relation R is defined on a set S if for every pair of elements (a,b), a,b  S, a R b is either true or false. If a R b is true, then we say that a is related to b. A relation R is defined on a set S if for every pair of elements (a,b), a,b  S, a R b is either true or false. If a R b is true, then we say that a is related to b. An equivalence relation is a relation R that satisfy three properties: An equivalence relation is a relation R that satisfy three properties: –(reflexive) a R a, for all a  S. –(symmetric) a R b if and only if b R a. –(transitive) a R b and b R c implies that a R c.

4 The Dynamic Equivalence Problem The equivalence class of an element a  S is the subset of S that contains all the elements that are related to a. The equivalence class of an element a  S is the subset of S that contains all the elements that are related to a. Equivalence classes form a partition of S: every member of S appears in exactly one equivalence class. Equivalence classes form a partition of S: every member of S appears in exactly one equivalence class. To decide if two member are related, only need to check whether the two are in the same equivalence class. To decide if two member are related, only need to check whether the two are in the same equivalence class.

5 Disjoint Sets Make the input a collection of N sets, each with one element. Make the input a collection of N sets, each with one element. –All relations (except reflexive) are false; –Each set has a different element: S i  S j =  => it makes the sets disjoint. Find operation: returns the name of the set containing a given element. Find operation: returns the name of the set containing a given element. Add operation (e.g., add relation a~b) Add operation (e.g., add relation a~b) –Check if a and b are already related: if they are in the same equivalence class. –If not, apply “union”: merge the two equivalence classes containing a and b into a new equivalence class.

6 Example: On a set of subsets, the three operations amount to On a set of subsets, the three operations amount to Create a set of n disjoint subsets with every node in its own subset Create a set of n disjoint subsets with every node in its own subset Test wheter A and B are in the same subset Test wheter A and B are in the same subset If A and B are in the same subset, then do nothing, else unify the subsets to which A and B belong If A and B are in the same subset, then do nothing, else unify the subsets to which A and B belong

7 The most efficient way to implement the operation is: The most efficient way to implement the operation is: For every A it is possible to ask for the index of the subset to which A belongs For every A it is possible to ask for the index of the subset to which A belongs This will be denoted as find(A). And we have A ~ B find(A) == find(B). The operation of adding a relation between A and B will be denoted by union(A, B) (even though nothing needs to happen). Thus, for union(A, B) one performs This will be denoted as find(A). And we have A ~ B find(A) == find(B). The operation of adding a relation between A and B will be denoted by union(A, B) (even though nothing needs to happen). Thus, for union(A, B) one performs

8 coding void union(Node A, Node B) { if (find(A) != find(B)) if (find(A) != find(B)) unify(A, B); unify(A, B); }

9 The first algorithm All the elements in S are numbered sequentially from 0 to N-1—the numbering can be determined by hashing —we have S i ={i} for i=0 through N-1. All the elements in S are numbered sequentially from 0 to N-1—the numbering can be determined by hashing —we have S i ={i} for i=0 through N-1. Maintain, in an array, the name of the equivalence class for each element. Maintain, in an array, the name of the equivalence class for each element. “find” is just a simple O(1) lookup. “find” is just a simple O(1) lookup. “union(a,b)”: suppose that a is in equivalence class i and b is in equivalence class j. Scan through the array, changing each i to j. It takes O(N) for one operation and O(N 2 ) for N number of union “union(a,b)”: suppose that a is in equivalence class i and b is in equivalence class j. Scan through the array, changing each i to j. It takes O(N) for one operation and O(N 2 ) for N number of union

10 Optimizations Keep all the elements that are in the same equivalence class in a linked list. Keep all the elements that are in the same equivalence class in a linked list. By tracking the size of each equivalence class, we, when “union”, change the name of the smaller equivalence class to the larger. Thus the total time spent for N “union” is O(NlogN). Using this strategy, any sequence of M finds and up to N-1unions takes at most O(M+NlogN) time. By tracking the size of each equivalence class, we, when “union”, change the name of the smaller equivalence class to the larger. Thus the total time spent for N “union” is O(NlogN). Using this strategy, any sequence of M finds and up to N-1unions takes at most O(M+NlogN) time.

11

12

13

14 The O(M+N) algorithm Class DisjSets {pubic: explicit DisjSets(int numElements); int find(int x) const; int find(int x); void unionSets(int root1,int root2); private: vector s; }

15 The O(M+N) algorithm DisjSets::DisjSets(int numElements):s(numElements) { for(int j=0; j<s.size(); j++) s[j]=-1;} Void DisjSets::unionSets(root1, root2) {s[root2]=root1;}

16 The O(M+N) algorithm DisjSets:: find(int x) const {if(s[x]<0) return x; else return find(s[x]); }

17 Smart Union Algorithms Union-by-size: make the smaller tree a subtree of the larger. Union-by-size: make the smaller tree a subtree of the larger. If union-by-size, the depth of any node is never more than logN: a find operation is O(logN), and O(MlogN) for a sequence of M. The worse-case trees are binomial trees. If union-by-size, the depth of any node is never more than logN: a find operation is O(logN), and O(MlogN) for a sequence of M. The worse-case trees are binomial trees.

18 Example:

19

20 Path Compression for faster find Path compression for find(x): every node on the path from x to the root has its parent changed to the root: make the tree shallow. Path compression for find(x): every node on the path from x to the root has its parent changed to the root: make the tree shallow.

21 Smart Union Algorithms /* Union-by-height: make the shallow tree a subtree of the deeper. */ void DisjSets::unionSets(root1, root2) { if(s[root2]<s[root1]) //root2 is deeper s[root1]=root2; else { //update height if same if(s[root1]==s[root2]) s[root1]--; s[root1]--;s[root2]=root1;}}

22 DisjSets:: find(int x) {if(s[x]<0) return x; else return s[x]=find(s[x]); }

23 Union and Find The Union-Find problem starts with n elements (numbered 1 to n), each one representing a singleton set. We allow two operations to be performed:

24 Find (i) returns the ID number of the set that i currently is in. We assume the ID number of each set is the number of one of the elements in it. Union (i; j) combines the elements in the sets with ID numbers i and j into a single set. The ID number of the new set will be either i or j. This is a destructive operation in the sense that the original sets i and j are lost when they are combined to form the new set.

25 We can implement Union-Find by maintaining an array A[1 : : : n] of integers. If i is the ID number of a set, then A[i] will be the negative of the number of elements in this set. Otherwise, A[i] will the number of another element in this set. -1 We begin with each A[i] equal to -1The Union operation is easily implemented as follows:

26 void Union (int i, int j) { if (A [i] < A [j]) { A [i] += A [j]; A [j] = i; } else { A [j] += A [i]; A [i] = j; } return; } This simply makes the \leader" of the smaller set point to the leader of the larger set, and adjusts the size of the leader. The time complexity is O(1)

27 To implement Find (i) we just follow the pointers to the leader: int Find (int i) { while (A [i] > 0) i = A [i]; return i; } This yields O(lg n) time. We can do better, though, with path compression. After we find the leader, we make all the nodes we've passed through point directly to it:

28 int Find (int i) { int j = i, k; if (A [i] < 0) return i; while (A [j] > 0) j = A [j]; while (A [i] != j) { k = A [i]; A [i] = j; i = k; } return j; }

29 Using Union by size (rank) and Find with path compression, we get the following result: Starting with n singleton sets, any sequence of M Union and/or Find operations takes O(M logN) time.