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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Andreas Klappenecker [Based on slides by Prof. Welch]
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 6: Union-Find and Related Structures CS6310 ADVANCED DATA STRUCTURE SHADHA MUHI & HASNAA IMAD.
Spring 2015 Lecture 11: Minimum Spanning Trees
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
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.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
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.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
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.
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 
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.
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.
CSE 373: Data Structures and Algorithms
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Binary Trees, Binary Search Trees
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
A simpler implementation and analysis of Chazelle’s
CSE 373 Data Structures and Algorithms
CSCE 411 Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
CMSC 202 Trees.
Course: Data Structures Lecturer: Uri Zwick March 2008
Binary Trees, Binary Search Trees
Union-Find Partition Structures
Union-Find Partition Structures
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
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)
Lecture 21 Amortized Analysis
Binary Trees, Binary Search Trees
CSE 373: Data Structures and Algorithms
Presentation transcript:

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 for e in the data structure. find(x): Given a handle for an element x; find the set that contains x. Return a handle/identifier/pointer/label for this set. union(A,B): Given two set identifiers create the union of the two sets. 1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A A A A AA A A A A AAA A A

Union Find ADT Applications: keep track of the connected components of a dynamic graph that changes due to insertion of nodes and edges Kruskals Minimum Spanning Tree Algorithm 2

Union Find ADT List Implementation: the elements of a set are stored in a list; each node has a backward pointer to the tail the tail of the list contains the label for the set makeSet(x) needs constant time find(x) also needs constant time 3 A

Union Find ADT Union: take the smaller of the two sets change all its backward pointer to the label of the larger set insert the smaller list at the head of the larger time O(min(|A|,|B|)) 4 B A

Union Find Lemma. The amortized running times are: find O(1). makeSet O(log n). union O(1). Proof. Idea: We partially charge the cost for a union operation to the elements involved. In total we will charge at most O(log n) to an element. Since each element has to be created with makeSet(x) we get an amortized time bound by inflating the cost of makeSet(x) to O(log n). fi nd: actual cost and amortized cost are the same. union(A,B ): i f (A==B) do nothing  time O(1) for the check otherwise add the smaller set to the larger; charge the cost for this to the elements in the smaller set; each element is charged one. your cost: zero! 5

Union Find How much do we charge to an element? Observation. Whenever we charge one to an element x the size of the subset A x that contains x increases by at least a factor of 2.  total charge to an element is at most O(log n) 6

Implementation via trees: the root of the tree is the label of the set only pointer to parent exist; we cannot list all elements of a given set Problem: find is not constant anymore Union Find

Union of two sets: the root of the tree is the label of the set store the size of a subtree in the root Union Find: Tree Implementation

Union of two sets: the root of the tree is the label of the set store the size of a subtree in the root make the smaller tree the child of the larger Union Find: Tree Implementation

Find: go upwards until you find the root make all visited node into children of the root (path compression) Union Find: Tree Implementation

Find: go upwards until you find the root make all visited node into children of the root (path compression) Union Find: Tree Implementation

Tree Implementation: Analysis Analysis union (A,B) can be done in time O(1) makeSet(x) is still trivial: O(1) the cost for find(x) may be large. Observation: The height of the trees is at most O(log n). if for an element x the distance to the root increases this means that the number of elements in its sub-tree at least doubles.  the cost for find(x) is at most O(log n) without amortization. 12

Can we do better? Yes with amortization! Definitions: n(v) := the number of nodes that were in the subtree rooted at v, when v became child of another node. rank r(v) :=  n(v) ≥ 2 r(v) Lemma: The rank of a parent p must be strictly larger than the rank of its child c. after c is linked to p the rank of c does not change anymore while the rank of c might still increase. directly after the linking r(p) Tree Implementation: Analysis 13 ≥ ≥ = > r(c) = TexPoint Display

Tree Implementation: Analysis Theorem. There are at most n/2 s nodes of rank s. Proof: a node of rank s had at least 2 s nodes in its subtree, when it became a child of another node nodes of the same rank have disjoint subtrees as they cannot be ancestors of each other [observe that a node that was initially not in a subtree T cannot via path compression join this sub-tree. ] more precisely: each node in the tree sees during its lifetime at most one ancestor of rank s; for each rank s node there are at least 2 s nodes that have seen him; hence there can at most be n/2 s nodes of rank s. 14

Tree Implementation: Analysis Definitions: Theorem: We can obtain the following amortized running times: makeSet(x): find(x): union(A,B): O(1) 15

Tree Implementation: Analysis group-number: a node with rank r[v] is in rank-group this means the rank-group g contains ranks t(g-1)+1,…., t(g) there are at most different rank-groups 16

Tree Implementation: Analysis Accounting Scheme: create an account for every find-operation create an account for every node The cost of a find operation is equal to the length of the path traversed. We charge the cost for going from v to parent[v] in the following way: if the parent of v does not change due to path-compression we charge the cost to the find-account (at most cost 1 per find) if the group-number of rank[v] is the same as that of rank[parent(v)] (before starting path compression) we charge the cost to the node-account of v otherwise we charge the cost to the find-account 17

Tree Implementation: Analysis Observations: find(x) is only charged. ( max number of rank-groups ) after a node is charged its parent is re-assigned to a node higher up in the tree.  parent gets larger rank. after some time the parent is in a larger rank group.  node will never be charged again the charge to a node in rank-group g is at most t(g)-t(g-1)<= t(g) What is the total number of operations that is charged to nodes? the total charge is at most where n(g) is the number of nodes in group g 18

Tree Implementation: Analysis  hence: as there are only groups 19

Tree Implementation: Analysis If there are only n elements we charge at most to these elements.  Charging to every makeSet()-operation gives the result. The analysis is not tight. In fact it has been shown that the amortized time for the union-find implementation with path compression is O( ® (n)), were ® (n), is the inverse Ackermann function which grows a lot slower than. There is also a lower bound of  ( ® (n)). 20