1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
Andreas Klappenecker [Based on slides by Prof. Welch]
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.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
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.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
COMP 261 Lecture 12 Disjoint Sets. Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking.
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
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,
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.
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.
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.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
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
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
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:
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
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
Disjoint Sets Chapter 8.
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
CSCE 411 Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find Partition Structures
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.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Presentation transcript:

1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains a collection of disjoint sets –each set has a representative element –supported operations: MakeSet(x) Find(x) Union(x,y)

2 Disjoint Sets Used in applications requiring the partition of a set into equivalence classes. –Maze generation –Several graph algorithms (e.g. Kruskal's algorithm for minimum spanning trees) –Compiler algorithms Equivalence of finite automata

3 Disjoint Sets Major operations: –MakeSet(x) Given an object x, create a set out of it. The representative of the set is x –Find(x) Given an object x, return the representative of the set containing x –Union(x) Given elements x, y, merge the sets they belong to. The original sets are destroyed. The new set has a new representative

4 Disjoint Sets In the discussion that follows: –n is the total number of elements (in all sets) –m is the total number of operations performed (a mix of MakeSet, Union, Find operations) m is at least equal to n since there must be a MakeSet operation for each element. The maximum number of Union operations that may be performed is n-1. –We will perform amortized analysis.

5 Disjoint Sets Implementation #1: Using linked lists –The head of the list is also the representative –Each node contains an element a pointer to the next node a pointer to the representative –Why? Because this will speed up the Find operation

6 Disjoint Sets Implementation #1: Using linked lists –MakeSet(x) Create a list with one node, x Time for one operation: O(1) –Find(x) Assuming we already have a pointer to x (*), just return the pointer to the representative Time for one operation: O(1) (*) usually, we have a vector of pointers to the individual nodes

7 Disjoint Sets Implementation #1: Using linked lists –Union(x, y) Perform Find(x) to find x's representative, r x Perform Find(y) to find y's representative, r y Append r y 's list to the end of r x 's list r x becomes the representative of the new set. –The elements that used to be in r y 's list should have their pointers to the representative updated. »Idea #1: Do a lazy update: set r y 's pointer to r x and leave the rest the way they are. This will make Union faster but will slow down the Find operation. »Idea #2: Update all applicable pointers. This will maintain the constant Find() time.

8 Disjoint Sets Implementation #1: Using linked lists –Union(x, y) A sequence of m operations may take O(m+n 2 ) time –How? Given elements 1, 2, 3,..., n, do Union(1, 2), Union(3, 1), Union(4, 1), etc. At step i, we attach a list of length i to a list of length 1, thus updating i pointers to the new representative. After n- 1 unions, we'll have a single set and we will have performed O(n 2 ) pointer updates. So let's be smart about it: –Keep track of the length of each list and always append the shorter list to the longer one.

9 Disjoint Sets Implementation #1: Using linked lists –Union(x, y) A sequence of m operations where all unions append the shorter list to the longer one takes O(m+nlgn) time Why? Because with each union we attach a list of length i to a list of length at least i, thus doubling the length of the list. By the time we get a single set containing all elements, each element's pointer to the representative will have been updated lgn times, thus giving us a total of nlgn pointer updates.

10 Disjoint Sets Implementation #2: Using arrays –Maintain an array of size n –Cell i of the array holds the representative of the set containing i. –Similar to lists, simpler to implement if we know the number of elements in advance.

11 Disjoint Sets Implementation #3: Using trees –Each set is represented by a tree structure where every node has a pointer to its parent. This tree is called an up-tree –The root is the representative of the set –The elements are not in any particular order.

12 Disjoint Sets Implementation #3: Using trees –MakeSet(x) Create a tree containing only the root, x Time for one operation O(1) –Find(x) Follow the parent pointers to the root. Time for one operation: O(depth of node) –Could be up to O(n)

13 Disjoint Sets

14 Disjoint Sets Implementation #3: Using trees –Union(x, y) Perform Find(x) to locate the representative of x, s x Perform Find(y) to locate the representative of y, s y Make s y a child of s x –Danger: if we are not smart about it, our tree may end up looking like a list

15 Disjoint Sets Implementation #3: Using trees –Union(x, y) Always make the smaller tree a child of the larger tree. How do we define "smaller"? –Heuristic #1: Union-by-weight »Smaller = fewer nodes »Store number of nodes at the representative »Add the two weights when performing a union –Heuristic #2: Union-by-height »Smaller = shorter »Store height at the representative »The height increases only when two trees of equal height are united.

16 Disjoint Sets Implementation #3: Using trees –Union(x, y) The height of a tree is at most logn+1 where n is the number of elements in the tree. We can do better than that! Optimizing the union through path compression. –Our goal is to minimize the height of the tree –Every time we perform a Find(z) operation, we make all nodes on the path from the root to z immediate children of the root. –When path compression is performed, a sequence of m operations takes O(mlgn).

17 Disjoint Sets Implementation #3: Using trees –Union(x, y) Path compression and union-by-weight can be performed at the same time. Path compression and union-by-height- can be performed at the same time. –It's more complex since path compression changes the height of the tree. –We usually prefer to estimate the height instead of computing it exactly. We then talk about union-by-rank with path compression. When we perform union-by-weight/height with path compression, a sequence of m operations is almost linear in m