WEEK 5 The Disjoint Set Class Ch 8.1-8.2-8.3-8.4-8.5 CE222 Dr. Senem Kumova Metin 2011-2012.

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!
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.
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.
Disjoint-Set Operation
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.
Union-Find Problem 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.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
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.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of.
1 22c:31 Algorithms Union-Find for Disjoint Sets.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
Homework remarking requests BEFORE submitting a remarking request: a)read and understand our solution set (which is posted on the course web site) b)read.
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,
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
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.
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.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Union & Find Problem 황승원 Fall 2010 CSE, POSTECH 2 2 Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
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 
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
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
Disjoint Sets Data Structure
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
An application of trees: Union-find problem
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.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
CSE 373 Data Structures and Algorithms
Equivalence Relations
Union-Find Partition Structures
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Running Time Analysis Union is clearly a constant time operation.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
An application of trees: Union-find problem
Disjoint Sets Textbook Chapter 8
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin

OUTLINE Definitions Dynamic Equivalence Problem Operations on Disjoint Sets Smart Union Algorithms Path compression Next Week – Worst Case Analysis – Example 2CE222_Dr. Senem Kumova Metin

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 are also sets Union of two sets A and B is a set C which consists of all elements in A and B Two sets are mutually disjoint if they do not have a common element  Disjoint Sets 3CE222_Dr. Senem Kumova Metin

DEFINITIONS Partition of a set is a collection of subsets such that union of all these subsets is the set itself EXAMPLE : S = {1,2,3,4}, A = {1,2}, B = {3,4}, C = {2,3,4}, D = {4} Is A, B a partition of S? YES Is A, C partition of S?NO Is A, D partition of S?NO CE222_Dr. Senem Kumova Metin4

DEFINITIONS 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: – (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. An equivalence relation partitions a set into distinct equivalence classes CE222_Dr. Senem Kumova Metin5

The Dynamic Equivalence Problem How can we decide for any a and b if a is related to b? Answer : A two dimensional array of Boolean variables  Result in constant time What happens if the relation is not explicitly defined ??? Relations : a1~a3, a3~a5, a4~a5 CE222_Dr. Senem Kumova Metin6

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. To decide if two member are related, only need to check whether the two are in the same equivalence class. Five element set { a1, a2, a3, a4, a5}, if following relations are given a1~a3, a3~a5, a5~a4.. Is a1 related to a4?? CE222_Dr. Senem Kumova Metin7

The Dynamic Equivalence Problem Each equivalence class may be represented by a single object: the representative object For the relations given : a1~a3, a3~a5, a5~a4 CE222_Dr. Senem Kumova Metin8

Operations on Disjoint Sets Union (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, merge the two equivalence classes containing a and b into a new equivalence class. Find Return the name (pointer or index of representative) of the set containing a given element CE222_Dr. Senem Kumova Metin9

A simple Implementation : Example Consider the following disjoint set on the ten decimal digits:

A simple Implementation : Example After UnionSets(1,2)  After ( Add relation 1~2 )

A simple Implementation : Example After UnıonSets(2,4)  find(4)=>4 and find(2)=>1

A simple Implementation : Example After UnıonSets(5,6)  find(5)=>5 and find(6)=>6

A simple Implementation : Example After UnıonSets(6,7)  find(6)=>5 and find(7)=>7

A simple Implementation : Example After UnıonSets(2,9)  find(2)=>1 and find(9)=>9

A simple Implementation : Example After UnıonSets(5,1)  find(5)=>5 and find(1)=>1

A simple Implementation : Example After UnıonSets(3,0)  find(5)=>5 and find(1)=>1

A simple Implementation : Example After UnionSets(0,8)  find(0)=>3 and find(8)=>8

A simple Implementation : Example After UnionSets(3,5)  find(3)=>3 and find(5)=>5

Tree Implementation For simplicity, we will assume we are creating disjoint sets with N integers We will define an array initialize(int N) {parent = new int[N]; for ( int i = 0; i < N; ++i ) { parent[i] = -1; } } If parent[i] == -1, then i is a root node. Initially, each integer is in its own set

Tree Implementation : FIND and UNION // ITERATIVE int find( int i ) { while( parent[i]!=-1) i = parent[i]; return i;} //RECURSIVE int find( int i ) const { if(parent[i]==-1) return i; else return find(parent[i]); } void UnionSets( int i, int j ) { i = find( i ); // root of i j = find( j ); // root of j if ( i != j ) parent[j] = i; // 2nd set is appended to 1st set }

Tree Implementation : Time Complexity // ITERATIVE int find( int i ) { while( parent[i]!=-1) i = parent[i]; return i;} // worst case O(N) void UnionSets( int i, int j ) { i = find( i ); j = find( j ); if ( i != j ) parent[j] = i; } // O(1) If we have u Union, f Find operations then complexity is O(u + f N) M consecutive operations could take O(MN) time in worst case

Array Implementation: Example

Array Implementation: Time Complexity int find( int i ) { return array[i];} // O(1) void UnionSets( int i, int j ) {rooti=find(i); rootj=find(j); for (int k=1; k<=N; k++) if (array[k] == rootj) array[k] = rooti;} //O(N) Initialize( int N ) {array = new int [N+1]; for (int e=1; e<=N; e++) array[e] = e; } If we have u Union, f Find operations then complexity is O(uN + f ) M consecutive operations could take O(MN) time in worst case

C++ Implementation from Text Book CE222_Dr. Senem Kumova Metin25 class DisjSets {public: DisjSets(int numElements):s(numElements) {for(int j=0; j<s.size(); j++) s[j]=-1;} int find(int x) const {if(s[x]<0)return x; else return find(s[x]);} void unionSets(int root1,int root2) {s[root2]=root1;} private: vector s; // an array with varying size };

Linked List Implementation of Disjoint Sets CE222_Dr. Senem Kumova Metin26 After Union (f,c)

Linked List Implementation of Disjoint Sets Each set is represented by a linked list The first object in each linked list serves as its set's representative. Each object in the linked list contains – a set member, – a pointer to the object containing the next set member, – a pointer back to the representative. Each list maintains pointers head, to the representative, and tail, to the last object in the list. Within each linked list, the objects may appear in any order (subject to our assumption that the first object in each list is the representative ). CE222_Dr. Senem Kumova Metin27

Smart Union Algorithms 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 worst-case trees are binomial trees Union by height (Union by rank) CE222_Dr. Senem Kumova Metin28

Smart Union Algorithms Union by height (Union by rank) /*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[root2]=root1; } } CE222_Dr. Senem Kumova Metin29

CE222_Dr. Senem Kumova Metin30 Smart Union Algorithms : Example

Analysis of Smart Union Algorithms Suppose each list also includes the length of the list and that we always append the smaller list onto the longer (weighted-union  union by height), with ties broken arbitrarily. Theorem 2.1: Using the linked-list representation of disjoint sets and the weighted-union heuristic, a sequence of m operations take O(m + n lg n) time. CE222_Dr. Senem Kumova Metin31

Analysis of Smart Union Algorithms: Theorem 2.1 Proof: Consider a fixed object x. We know that each time x's representative pointer was updated, x must have started in the smaller set. The first time x's representative pointer was updated, therefore, the resulting set must have had at least 2 members. Similarly, the next time x's representative pointer was updated, the resulting set must have had at least 4 members. Continuing on, we observe that for any k ≤ n, after x's representative pointer has been updated ⌈ lg k ⌉ times, the resulting set must have at least k members. Since the largest set has at most n members, each object's representative pointer has been updated at most ⌈ lg n ⌉ times over all the UNION operations. The total time used in updating the n objects is thus O(n lg n). The time for the entire sequence of m operations follows easily. Each MAKE-SET and FIND-SET operation takes O(1) time, and there are O(m) of them. The total time for the entire sequence is thus O(m + n lg n). CE222_Dr. Senem Kumova Metin32

33 Path compression int Find(int x) if (parent[x] == 0) return x else return parent[x] = Find(parent[x]) Any single find can still be O(log N), but later finds on the same path are faster u Unions, f Finds: O(u + f  (f, u))  (f, u) is a functional inverse of Ackermann’s function N-1 Unions, O(N) Finds: “almost linear” total time

Path compression CE222_Dr. Senem Kumova Metin34