CSE 326 Union Find Richard Anderson Text Chapter 8 7-29-2005 CSE 326.

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!
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 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
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.
Minimum Spanning Trees (MST)
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 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.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
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.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 22c:31 Algorithms Union-Find for Disjoint Sets.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
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,
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.
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.
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
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:
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
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.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
CSE 326: Data Structures: Set ADT
CSE 373: Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
An application of trees: Union-find problem
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
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
CSE373: Data Structures & Algorithms Implementing Union-Find
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.
Lecture 12 Algorithm Analysis
Dynamic Equivalence Problem
Minimum Spanning Trees
An application of trees: Union-find problem
Lecture 21 Amortized Analysis
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

CSE 326 Union Find Richard Anderson Text Chapter 8 7-29-2005 CSE 326

Union Find Problem Fixed collection of items Disjoint subsets Find(x) – which subset is x in? Union(x,y) – merge the sets containing x and y What run times do you expect for implementations of Union and Find? 7-29-2005 CSE 326

Applications of Union Find Building connected components foreach edge (e = (v1, v2)){ if (Find(v1) != Find(v2)) Union(v1, v2); } 7-29-2005 CSE 326

Equivalence Classes Fixed collection of items partitioned into equivalence classes Equivalence classes identified by one of their members {1, 3, 5}, {2, 8}, {4}, {6, 9, 10, 11}, {7} 7-29-2005 CSE 326

Draw an in-tree representation of the following equivalence relation {1,11}, {2, 4, 6, 8, 10, 12}, {3, 5, 7, 9}, {13}, {14}, {15} 7-29-2005 CSE 326

Array representation Store parents in the array P 3 8 3 4 3 9 7 8 9 9 6 7-29-2005 CSE 326

Basic Find operation Write the code for Find(x) using the Array P[ ] 7-29-2005 CSE 326

Union Union(x, y){ x1 = Find(x); y1 = Find(y); P[x1] = y1; } 7-29-2005 CSE 326

Unions create long chains 7-29-2005 CSE 326

Weighted Union W[x]: number of descendents of x (written wt(x)) Union(x, y){ x1 = Find(x); y1 = Find(y); if (W[x1] > W[y1]){ P[y1] = x1; W[x1] = W[x1] + W[y1]; } else { P[x1] = y1; W[y1] = W[x1] + W[y1]; W[x]: number of descendents of x (written wt(x)) Weighted union: smaller tree points to the bigger tree 7-29-2005 CSE 326

Binomial Trees, B0, B1, …, Bk B0 Bk = Union(Bk-1, Bk-1) Bk-1 Bk-1 Bk 7-29-2005 CSE 326

Binomial Trees Draw B4 What is the height of Bk? What is the weight of Bk? 7-29-2005 CSE 326

Theorem: Weight balanced unions guarantee O(log n) depth Show wt(T) >= 2ht(T) Show this is an invariant maintained by Union and Find operations Base case – true for singleton node Trivial case – invariant for Find operations Interesting case: T = Union(T1, T2) 7-29-2005 CSE 326

wt(T1) >= 2ht(T1), wt(T2) >= 2ht(T2) Show wt(T) >= 2ht(T) Assume wt(T2) >= wt(T1) Idea, consider cases: ht(T1) >= ht(T2) ht(T1) < ht(T2) T1 T T2 7-29-2005 CSE 326

Case 1 ht(T1) >= ht(T2) T Show 2ht(T) <= wt(T) 2ht(T) = 2ht(T1)+1 <= 2wt(T1) <= wt(T) T1 T T2 Show 2ht(T) <= wt(T) Hint: what is ht(T)? Recall: wt(T2) >= wt(T1) 2ht(T1) <= wt(T1) 2ht(T2) <= wt(T2) 7-29-2005 CSE 326

Case 2: ht(T1) < ht(T2) T Show 2ht(T) <= wt(T) 2ht(T) = 2ht(T2) <= wt(T2) <= wt(T) T1 T T2 Show 2ht(T) <= wt(T) Hint: What is ht(T)? Recall: wt(T2) >= wt(T1) 2ht(T1) <= wt(T1) 2ht(T2) <= wt(T2) 7-29-2005 CSE 326

Path Compression Long chains are only expensive if they are traversed multiple times Idea: Compress chains when they are traversed 7-29-2005 CSE 326

Path Compression Algorithms Find(x) { y = x; while (P[x] != x) x = P[x]; while (P[y] != x){ t = y; P[y] = x; y = t; } return x; Find(x){ while (P[x] != x) x = P[x] = P[P[x]]; return x; } 7-29-2005 CSE 326

Amortized Complexity Cost of n intermixed Union-Find operations Basic algorithm O(n2) Weighted union O(n log n) Path Compression O(n log n) Weighted union + Path Compression O(a(n) n) 7-29-2005 CSE 326