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.

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!
0 Course Outline n Abstract data types and algorithm analysis (Ch. 2, 3) n C++ review (Ch. 1) n Sets in general: Balanced search trees (Ch. 4 and 12.2)
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.
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.
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.
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.
CSE 326: Data Structures Lecture #19 Disjoint Sets Dynamic Equivalence Weighted Union & Path Compression David Kaplan Today we’re going to get.
Course: Data Structures Lecturer: Uri Zwick March 2008
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.
Binary Trees Chapter 6.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Chapter 6: Union-Find and Related Structures CS6310 ADVANCED DATA STRUCTURE SHADHA MUHI & HASNAA IMAD.
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.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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,
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.
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.
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.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
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
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
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 
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
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
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
An application of trees: Union-find problem
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
CSE 332: Data Structures Disjoint Set Union/Find
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find Partition Structures
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:

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 of sets of elements n Each element is in exactly one set  sets are disjoint  to start, each set contains one element n Each set has a name, which is the name of one of its elements (any one will do)

2 Disjoint set ADT, continued Setname = find ( elementname )  returns the name of the unique set that contains the given element  not the same as “find” in search trees (lousy terminology, for historical reasons…) n union ( Setname1, Setname2 )  replaces both sets with a new set  the name of the new set is not specified n Analysis: worst-case total running time of a sequence of f finds and u unions

3 Toy application: mazes without loops elements are 1, 2, … 25; sets are connected parts of the maze start with each element in its own set; repeat { pick two adjacent elements p and q (= p ± 1 or p ± 5) at random; if (psetname = find(p)) != (qsetname = find(q)) { erase the wall between p and q; union(psetname, qsetname); } } until 24 walls have been erased

4 First Try: Quick Find Array implementation. Items are 1, …, N n Setname[i] = name of the set containing item I n Find : O(1), Union : O(N) n u Union, f Find operations: O(u*N+f ) n N-1 Unions and O(N) Finds: O(N 2 ) total time Initialize(int N) Setname = new int [N+1]; for (int e=1; e<=N; e++) Setname[e] = e; Union (int i, int j) for (int k=1; k<=N; k++) if (Setname[k] == j) Setname[k] = i; int Find (int e) return Setname[e];

5 Union(5,11) Union(12,4)Union(1,5)Union(15,1)

6 Quick Find Analysis Find : O(1), Union : O(N) n u Union, f Find operations: O(u*N+f ) n N-1 Unions and O(N) Finds: O(N 2 ) total time

7 Quick Union: Tree implementation n Each set a tree: Root serves as SetName  To Find, follow parent pointers to the root  Initially parent pointers set to self  To union(u,v), make v’s parent point to u n After union(4,5), union(6,7), union(4,6)

8 Analysis of Quick Union n Complexity in the worst case:  Union is O(1) but Find is O(n)  u Union, f Find : O(u + f n)  N-1 Unions and O(N) Finds: still O(N 2 ) total time Initialize (int N) parent = new int [N+1]; for (int e=1; e<=N; e++) parent[e] = 0; int Find (int e) while (parent[e] != 0) e = parent[e]; return e; Union (int i, int j) parent[j] = i; N1N1 N Union (N-1, N); Union (N-2, N-1); Union (N-3, N-2); … Union (1, 2); Find (1); Find (2); … Find (N);

9 n union(u,v): make smaller tree point to bigger one’s root n That is, make v’s root point to u if v’s tree is smaller. n Union(4,5), union(6,7), union(4,6). n Now perform union(3, 4). Smaller tree made the child node. Smart Union (or Union by Size)

10 Union by Size: link smaller tree to larger one Initialize (int N) setsize = new int[N+1]; parent = new int [N+1]; for (int e=1; e <= N; e++) parent[e] = 0; setsize[e] = 1; int Find (int e) while (parent[e] != 0) e = parent[e]; return e; Union (int i, int j) if setsize[i] < setsize[j] then setsize[j] += setsize[i]; parent[i] = j; else setsize[i] += setsize[j]; parent[j] = i ; Lemma: After n union ops, the tree height is at most log n.

11 n Find(u) takes time proportional to u’s depth in its tree. n Show that if u’s depth is h, then its tree has at least 2 h nodes. n When union(u,v) performed, the depth of u only increases if its root becomes the child of v. n That only happens if v’s tree is larger than u’s tree. n If u’s depth grows by 1, its (new) treeSize is > 2 * oldTreeSize n Each increment in depth doubles the size of u’s tree. n After n union operations, size is at most n, so depth at most log n. n Theorem: With Union-By-Size, we can do find in O(log n) time and union in O(1) time (assuming roots of u, v known). n N-1 Unions, O(N) Finds: O(N log N) total time Union by Size: Analysis

12 The Ultimate Union-Find: Path compression int Find (int e) if (parent[e] == 0) return e else parent[e] = Find (parent[e]) return parent[e] While performing Find, direct all nodes on the path to the root. n Example: Find(14)

13 The Ultimate Union-Find: Path compression int Find (int e) if (parent[e] == 0) return e else parent[e] = Find (parent[e]) return parent[e] n Any single find can still be O(log N), but later finds on the same path are faster n Analysis of UF with Path Compression a tour de force [Robert Tarjan] n u Unions, f Finds: O(u + f  (f, u)) n  (f, u) is a functional inverse of Ackermann’s function n N-1 Unions, O(N) Finds: “almost linear” total time

14 A perspective on Inverse Ackermann n We are familiar with the log function. Log 2 10 = 10 n Log* n (iterated log) how many times log applied to reach 1 n Log* = 4 n Log* = 5 ( is a 20,000 digit number) n Growth of Inverse Ackermann’s is far slower than log* !

15 O(1) time for both Union and Find? n Can one achieve worst-case O(1) time for both Union and Find? n Inverse Ackermann’s function is a constant for all practical purposes, but it does grow (very slowly). n Tarjan proved that the strange Ackermann function is intrinsic to UF complexity: tight bound. n An amazing but extremely non-trivial and complex analysis. n Tarjan won Turning award in 1986.