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 

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!
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
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.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
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.
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.
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.
CS Data Structures Chapter 5 Trees. Forests  Definition: A forest is a set of n ≥ 0 disjoint trees.  When we remove a root from a tree, we’ll.
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.
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.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structures & Algorithms Union-Find Example Richard Newman.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
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,
Example Algorithms CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lecture X Disjoint Set Operations
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.
The Disjoint Set Class Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 24 © 2002 Addison Wesley.
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.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
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
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.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
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.
Parent Pointer Implementation
CSE 373: Data Structures and Algorithms
Chapter 8 Disjoint Sets and Dynamic Equivalence
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Disjoint Sets Chapter 8.
An application of trees: Union-find problem
Course Outline Introduction and Algorithm Analysis (Ch. 2)
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
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
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.
Union-Find Problem Given a set {1, 2, …, n} of n elements.
An application of trees: Union-find problem
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

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  S, a R b is either true or false. If a R b is true, then we say that a is related to b. 【 Definition 】 A relation, ~, over a set, S, is said to be an equivalence relation over S iff it is symmetric, reflexive, and transitive over S. 【 Definition 】 Two members x and y of a set S are said to be in the same equivalence class iff x ~ y. 1/12

§2 The Dynamic Equivalence Problem Given an equivalence relation ~, decide for any a and b if a ~ b. 〖 Example 〗 Given S = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } and 9 relations: 12  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  12. The equivalence classes are { 2, 4, 7, 11, 12 }, { 1, 3, 5 }, { 6, 8, 9, 10 } Algorithm: { /* step 1: read the relations in */ Initialize N disjoint sets; while ( read in a ~ b ) { if ( ! (Find(a) == Find(b)) ) Union the two sets; } /* end-while */ /* step 2: decide if a ~ b */ while ( read in a and b ) if ( Find(a) == Find(b) ) output( true ); else output( false ); } (Union / Find) Dynamic (on-line) 2/12

§2 The Dynamic Equivalence Problem  Elements of the sets: 1, 2, 3,..., N  Sets : S 1, S 2, and S i  S j =  ( if i  j ) —— disjoint 〖 Example 〗 S 1 = { 6, 7, 8, 10 }, S 2 = { 1, 4, 9 }, S 3 = { 2, 3, 5 } A possible forest representation of these sets Note: Pointers are from children to parents  Operations : (1) Union( i, j ) ::= Replace S i and S j by S = S i  S j (2) Find( i ) ::= Find the set S k which contains the element i. 3/12

§3 Basic Data Structure  Union ( i, j ) Idea: Make S i a subtree of S j, or vice versa. That is, we can set the parent pointer of one of the roots to the other root S1  S2S1  S2 S2  S1S2  S1 Implementation 1: S1 S2 S3    name[ ] S2  S1S2  S1  S 2 4/12

Here we use the fact that the elements are numbered from 1 to N. Hence they can be used as indices of an array. §3 Basic Data Structure Implementation 2: S [ element ] = the element’s parent. Note: S [ root ] = 0 and set name = root index 〖 Example 〗 The array representation of the three sets is ( S 1  S 2  S 1 )  S [ 4 ] = 10  Find ( i ) Implementation 1: name[k] S  j i... find ( i ) =‘S’ Implementation 2: SetType Find ( ElementType X, DisjSet S ) { for ( ; S[X] > 0; X = S[X] ) ; return X ; } void SetUnion ( DisjSet S, SetType Rt1, SetType Rt2 ) { S [ Rt2 ] = Rt1 ; } [1] 4 [2] 0 [10] 0 [9] 4 [8] 10 [7] 10 [6] 10 [5] 2 [4] 0 [3] 2 S 10 5/12

§3 Basic Data Structure  Analysis Practically speaking, union and find are always paired. Thus we consider the performance of a sequence of union- find operations. 〖 Example 〗 Given S = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } and 9 relations: 12  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  12. We have 3 equivalence classes { 2, 4, 7, 11, 12 }, { 1, 3, 5 }, and { 6, 8, 9, 10 } Algorithm using union-find operations { Initialize S i = { i } for i = 1,..., 12 ; for ( k = 1; k <= 9; k++ ) { /* for each pair i  j */ if ( Find( i ) != Find( j ) ) SetUnion( Find( i ), Find( j ) ); } Can you think of a worst case example? Sure. Try this one: union(2, 1), find(1); union(3, 2), find(1);...,... ; union(N, N  1), find(1). N N1N1  1 T =  ( N 2 ) ! That’s not good. 6/12

§4 Smart Union Algorithms  Union-by-Size -- Always change the smaller tree S [ Root ] = – size; /* initialized to be –1 */ Now T = O( N ) for the worst case example I gave. 1 2 N  【 Lemma 】 Let T be a tree created by union-by-size with N nodes, then Proof: By induction. (Each element can have its set name changed at most log 2 N times.) Time complexity of N Union and M Find operations is now O( N + M log 2 N ).  Union-by-Height -- Always change the shallow tree Please read Figure 8.13 on p.273 for detailed implementation. 7/12

§5 Path Compression SetType Find ( ElementType X, DisjSet S ) { if ( S[ X ] <= 0 ) return X; else return S[ X ] = Find( S[ X ], S ); } SetType Find ( ElementType X, DisjSet S ) { ElementType root, trail, lead; for ( root = X; S[ root ] > 0; root = S[ root ] ) ; /* find the root */ for ( trail = X; trail != root; trail = lead ) { lead = S[ trail ] ; S[ trail ] = root ; } /* collapsing */ return root ; } Note: Not compatible with union-by- height since it changes the heights. Just take “height” as an estimated rank. Slower for a single find, but faster for a sequence of find operations. 8/12

§6 Worst Case for Union-by-Rank and Path Compression 【 Lemma (Tarjan) 】 Let T( M, N ) be the maximum time required to process an intermixed sequence of M  N finds and N  1 unions. Then: k 1 M  ( M, N )  T( M, N )  k 2 M  ( M, N ) for some positive constants k 1 and k 2.  Ackermann’s Function and  ( M, N )  O( log * N )  4 log * N (inverse Ackermann function) = # of times the logarithm is applied to N until the result  1. log * = 5 since logloglogloglog ( )= 1 9/12

Home work: p A formatted version We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input: Input consists of several test cases. For each test case, the first line contains N (<=10,000), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format: I c1 c2 where I stands for inputting a connection between c1 and c2; or C c1 c2 where C stands for checking if it is possible to transfer files between c1 and c2; or S where S stands for stopping this case. 10/12

Output: For each C case, print in one line the word yes or no if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line The network is connected. if there is a path between any pair of computers; or There are k components. where k is the number of connected components in this network. Print a blank line between test cases. Sample Input: 3 C 1 2 I 1 2 C 1 2 S 3 I 3 1 I 2 3 C 1 2 S Sample Output: no yes There are 2 components. yes The network is connected. Must use union-by-size and path compression to obtain a worst-case running time of O( M  ( M, N ) ). 11/12

Bonus Problem 2 Attack of Panda (2 points) Due: Friday, November 13th, 2009 at 10:00pm Detailed requirements can be downloaded from 12/12