Union-Find Partition Structures

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
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.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Chapter 4: Trees Part II - AVL Tree
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
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.
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.
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.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
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.
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
Union-find Algorithm Presented by Michael Cassarino.
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.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
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.
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 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.
Leftist Trees Linked binary tree.
BCA-II Data Structure Using C
Data Structures for Disjoint Sets
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Lecture 22 Binary Search Trees Chapter 10 of textbook
Heaps 9/13/2018 3:17 PM Heaps Heaps.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Heaps and Priority Queues
Part-D1 Priority Queues
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
CSCE 411 Design and Analysis of Algorithms
Copyright © Aiman Hanna All rights reserved
CSE 332: Data Abstractions Union/Find II
CMSC 202 Trees.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
2-3-4 Trees Red-Black Trees
Heaps and Priority Queues
Union-Find Partition Structures
CMSC 341 Disjoint Sets.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
CMSC 341 Disjoint Sets.
Running Time Analysis Union is clearly a constant time operation.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
1 Lecture 13 CS2013.
Lecture 21 Amortized Analysis
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Union-Find Partition Structures Minimum Spanning Tree 2/24/2019 10:55 PM Union-Find Partition Structures Union-Find

Partitions with Union-Find Operations makeSet(x): Create a singleton set containing the element x and return the position storing x in this set union(A,B ): Return the set A U B, destroying the old A and B find(p): Return the set containing the element at position p Union-Find

List-based Implementation Each set is stored in a sequence represented with a linked-list Each node should store an object containing the element and a reference to the set name Union-Find

Analysis of List-based Representation When doing a union, always move elements from the smaller set to the larger set Each time an element is moved it goes to a set of size at least double its old set Thus, an element can be moved at most O(log n) times Total time needed to do n unions and finds is O(n log n). Union-Find

Tree-based Implementation Each element is stored in a node, which contains a pointer to a set name A node v whose set pointer points back to v is also a set name Each set is a tree, rooted at a node with a self-referencing set pointer For example: The sets “1”, “2”, and “5”: 1 2 5 4 7 3 6 8 10 9 11 Union-Find 12

Union-Find Operations 5 To do a union, simply make the root of one tree point to the root of the other To do a find, follow set-name pointers from the starting node until reaching a node whose set-name pointer refers back to itself 2 8 10 3 6 11 9 12 5 2 8 10 3 6 11 9 12 Union-Find

Union-Find Heuristic 1 Union by size: When performing a union, make the root of smaller tree point to the root of the larger Implies O(n log n) time for performing n union-find operations: Each time we follow a pointer, we are going to a subtree of size at least double the size of the previous subtree Thus, we will follow at most O(log n) pointers for any find. 5 2 8 10 3 6 11 9 12 Union-Find

Union-Find Heuristic 2 Path compression: After performing a find, compress all the pointers on the path just traversed so that they all point to the root Implies O(n log* n) time for performing n union-find operations: Proof is somewhat involved… (and not in the book) 5 5 8 10 8 10 11 11 2 12 2 12 3 6 3 6 9 9 Union-Find

Python Implementation Union-Find

Proof of log* n Amortized Time For each node v that is a root define n(v) to be the size of the subtree rooted at v (including v) identified a set with the root of its associated tree. We update the size field of v each time a set is unioned into v. Thus, if v is not a root, then n(v) is the largest the subtree rooted at v can be, which occurs just before we union v into some other node whose size is at least as large as v ’s. For any node v, then, define the rank of v, which we denote as r (v), as r (v) = [log n(v)]: Thus, n(v) ≥ 2r(v). Also, since there are at most n nodes in the tree of v, r (v) = [log n], for each node v. Union-Find

Proof of log* n Amortized Time (2) For each node v with parent w: r (v ) > r (w ) Claim: There are at most n/ 2s nodes of rank s. Proof: Since r (v) < r (w), for any node v with parent w, ranks are monotonically increasing as we follow parent pointers up any tree. Thus, if r (v) = r (w) for two nodes v and w, then the nodes counted in n(v) must be separate and distinct from the nodes counted in n(w). If a node v is of rank s, then n(v) ≥ 2s. Therefore, since there are at most n nodes total, there can be at most n/ 2s that are of rank s. Union-Find

Proof of log* n Amortized Time (3) Definition: Tower of two’s function: t(i) = 2t(i-1) Nodes v and u are in the same rank group g if g = log*(r(v)) = log*(r(u)): Since the largest rank is log n, the largest rank group is log*(log n) = (log* n) - 1 Union-Find

Proof of log* n Amortized Time (4) Charge 1 cyber-dollar per pointer hop during a find: If w is the root or if w is in a different rank group than v, then charge the find operation one cyber-dollar. Otherwise (w is not a root and v and w are in the same rank group), charge the node v one cyber-dollar. Since there are most (log* n)-1 rank groups, this rule guarantees that any find operation is charged at most log* n cyber-dollars. Union-Find

Proof of log* n Amortized Time (5) After we charge a node v then v will get a new parent, which is a node higher up in v ’s tree. The rank of v ’s new parent will be greater than the rank of v ’s old parent w. Thus, any node v can be charged at most the number of different ranks that are in v ’s rank group. If v is in rank group g > 0, then v can be charged at most t(g)-t(g-1) times before v has a parent in a higher rank group (and from that point on, v will never be charged again). In other words, the total number, C, of cyber-dollars that can ever be charged to nodes can be bounded by Union-Find

Proof of log* n Amortized Time (end) Bounding n(g): Returning to C: Union-Find