1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.

Slides:



Advertisements
Similar presentations
Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.
Advertisements

Lecture 10 Disjoint Set ADT.
CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
Equivalence Relations
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.
Applied Discrete Mathematics Week 11: Graphs
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.
Relations & Their Properties. Copyright © Peter Cappello2 Introduction Let A & B be sets. A binary relation from A to B is a subset of A x B. Let R be.
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:
Sets, Relation and Functions A set is a collection of objects (elements) or a container of objects. Defining sets –Empty set (Ø): no elements –Universal.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
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,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
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 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.
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
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
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.
General Trees A tree T is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T-{r})
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
1 Equivalence relations Binary relations: –Let S1 and S2 be two sets, and R be a (binary relation) from S1 to S2 –Not every x in S1 and y in S2 have such.
Parent Pointer Implementation
CSE 373: Data Structures and Algorithms
Chapter 8 Disjoint Sets and Dynamic Equivalence
Greedy method Idea: sequential choices that are locally optimum combine to form a globally optimum solution. The choices should be both feasible and irrevocable.
Disjoint Sets Chapter 8.
Heaps and Priority Queues
Priority Queues and Heaps
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
Heaps and Priority Queues
Data Structures & Algorithms Union-Find Example
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Heaps and Priority Queues
ICS 353: Design and Analysis of Algorithms
Union-Find Partition Structures
Heaps and Priority Queues
Union-Find Partition Structures
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Equivalence relations
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 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union

2 8.1 Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b), a, b  S, aRb (a ~ b) is either true or false. An equivalence relation is a relation R that satisfies 3 properties: –Reflexive aRa, for all a  S. –Symmetric aRb if and only if bRa. –Transitive aRb and bRc implies that aRc. Example: network connectivity

3 8.2 Dynamic Equivalence Problem The equivalence class of an element a  S is the subset of S that contains all the elements related to a. The equivalence classes form a partition of S (S i  S j =  ); i.e., the sets are disjoint. Find (X) returns the name of the set (equivalence class) containing a given element X. Union (X, Y) returns the new root

4 8.3 Basic Data Structure Use a tree to represent each set, and the root to name a set. Find (X) returns the name of the set (equivalence class) containing a given element X. Union (X, Y) returns the new root of the union of the 2 sets, with roots X and Y. The new root is X.

5 8.3 Basic Data Structure Figure 8.1 Eight elements, initially in different sets Figure 8.2 After Union (5,6)

6 8.3 Basic Data Structure Figure 8.3 After Union (7,8) Figure 8.4 After Union (5, 7)

7 8.3 Basic Data Structure /* Fig. 8.6 Disjoint set declaration */ typedef int DisjSet [NumSets + 1]; typedef int SetType; typedef int ElementType; void Initialize (DisjSet S); void SetUnion (DisjSet S, SetType Root1, SetType Root2); SetType Find( ElementType X, DisjSet S );

8 8.3 Basic Data Structure /* Fig. 8.7 Disjoint set initialization routine */ void Initialize (DisjSet S) { int i; for (i = NumSets; i > 0; i--) S [i] = 0; }

9 8.3 Basic Data Structure /* Fig. 8.8 Union */ /* Assumes Root1 and Root2 are roots */ /* union is a C keyword, so this routine */ /* is named SetUnion */ void SetUnion (DisjSet S, SetType Root1, SetType Root2) { S [Root2] = Root1; }

Basic Data Structure /* Fig. 8.9 Find algorithm */ SetType Find (ElementType X, DisjSet S) { if (S [X] <= 0) return X; else return Find (S [X], S); }

Smart Union Algorithms The previous union algorithm arbitrarily makes the second tree a subtree of the first. Heuristics to reduce the depth of the tree. Union by size –making the smaller tree a subtree of the larger –depth of any node is never more than log N –find operation is O(log N)

Smart Union Algorithms –has to keep track of the size of each tree for a non-root node, record the name of the parent node for a root node, record the negative value of the size of the tree (number of nodes in the tree)

Smart Union Algorithms Figure 8.10 Result of union by size after Union (4,5)

Smart Union Algorithms node value Figure 8.11 Result of an arbitrary union (4,5)

Smart Union Algorithms Union by height –keep track of the height, and make the shallow tree a subtree of the deeper tree –the height of a tree increases only when 2 trees of the same height are joined –result of Union (4,5) is the same as that for union by size.

Smart Union Algorithms –to keep track of the depth of a tree for a non-root node, record the name of the parent node for a root node, record the negative value of the height of the tree node value

Smart Union Algorithms /* Fig Union by height (rank) */ /* Assume Root1 and Root2 are roots */ /* union is a C keyword, so this routine */ /* is named SetUnion */ void SetUnion (DisjSet S, SetType Root1, SetType Root2) { if (S [Root2] < S [Root1]) /* Root2 is deeper */ S [Root1] = Root2; /* new root */

Smart Union Algorithms else { if (S [Root1] == S [Root2]) /* Same height, */ S [Root1]--; /* so update */ S [Root2] = Root1; }

Path Compression Modify Find (X) such that every node on the path from X to the root has its parent changed to the root. Effectively reduce the length of the path Incompatible with union by height because it is difficult to determine the correct height of a tree (which has several branches).

Path Compression Result of Find (7) for the disjoint set in Fig

Path Compression /* Fig Find with path compression */ SetType Find (ElementType X, DisjSet S) { if (S [X] <= 0) return X; else return S [X] = Find (S [X], S); } Skip rest of Chapter 8