Running Time Analysis Union is clearly a constant time operation.

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.
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.
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.
Heaps Heaps are used to efficiently implement two operations:
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.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
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,
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.
Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i):
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.
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.
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.
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 
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.
How many edges does a tree of n nodes have? 1.2n-4 2.n(n-1)/2 3.n-1 4.n/2 5.Between n and 2n.
CSE 373: Data Structures and Algorithms
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Chapter 8 Disjoint Sets and Dynamic Equivalence
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
CS200: Algorithm Analysis
CSE 332: Data Structures Disjoint Set Union/Find
ICS 353: Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Heap code in C++ template <class eType>
Union-Find Partition Structures
CMSC 341 Disjoint Sets.
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.
CMSC 341 Disjoint Sets.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
Timing with Optimization
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Disjoint Sets Data Structure (Chap. 21)
Dynamic Equivalence Problem
An application of trees: Union-find problem
Disjoint Sets Textbook Chapter 8
Binary Trees, Binary Search Trees
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Running Time Analysis Union is clearly a constant time operation. Running time of find(i) is proportional to the height of the tree containing node i. This can be proportional to n in the worst case (but not always) Goal: Modify union to ensure that heights stay small End of lecture 35, start of lecture 36

Lecture No.36 Data Structures Dr. Sohail Aslam

Union by Size Maintain sizes (number of nodes) of all trees, and during union. Make smaller tree the subtree of the larger one. Implementation: for each root node i, instead of setting parent[i] to -1, set it to -k if tree rooted at i has k nodes. This also called union-by-weight.

Union by Size union(i,j): root1 = find(i); root2 = find(j); if (root1 != root2) if (parent[root1] <= parent[root2]) { // first tree has more nodes parent[root1] += parent[root2]; parent[root2] = root1; } else { // second tree has more nodes parent[root2] += parent[root1]; parent[root1] = root2;

Union by Size 1 2 3 4 5 6 7 8 -1 1 2 3 4 5 6 7 8 Eight elements, initially in different sets.

Union by Size 1 2 3 4 5 7 8 6 -1 -2 4 1 2 3 5 6 7 8 Union(4,6)

Union by Size 1 2 4 5 7 8 3 6 -1 -2 2 4 1 3 5 6 7 8 Union(2,3)

Union by Size 2 4 5 7 8 3 1 6 4 -2 2 -3 -1 1 3 5 6 7 8 Union(1,4)

Union by Size 4 5 7 8 2 1 6 3 4 2 -5 -1 1 3 5 6 7 8 Union(2,4)

Union by Size 4 7 8 2 1 6 5 3 4 2 -6 -1 1 3 5 6 7 8 Union(5,4)

Analysis of Union by Size If unions are done by weight (size), the depth of any element is never greater than log2n.

Analysis of Union by Size Intuitive Proof: Initially, every element is at depth zero. When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). How often can each union be done? -- log2n times, because after at most log2n unions, the tree will contain all n elements.

Union by Height Alternative to union-by-size strategy: maintain heights, During union, make a tree with smaller height a subtree of the other. Details are left as an exercise.

Sprucing up Find So far we have tried to optimize union. Can we optimize find? Yes, using path compression (or compaction).

Sprucing up Find During find(i), as we traverse the path from i to root, update parent entries for all these nodes to the root. This reduces the heights of all these nodes. Pay now, and reap benefits later! Subsequent find may do less work

Sprucing up Find Updated code for find find (i) { if (parent[i] < 0) return i; else return parent[i] = find(parent[i]); }

Path Compression Find(1) 7 13 8 22 6 3 4 5 9 31 32 11 30 10 2 35 14 20 16 12 1 13 19 17 18

Path Compression Find(1) 7 13 8 22 6 3 4 5 9 31 32 11 30 10 2 35 14 20 16 12 1 13 19 17 18

Path Compression Find(1) 7 13 8 22 6 3 4 5 9 31 32 11 30 10 2 35 14 20 16 12 1 13 19 17 18

Path Compression Find(1) 7 13 8 22 6 3 4 5 9 31 32 11 30 10 2 35 14 20 16 12 1 13 19 17 18

Path Compression Find(1) 7 13 8 22 6 3 4 5 9 31 32 11 30 10 2 35 14 20 16 12 1 13 19 17 18

Path Compression f Find(a) e d c b a

Path Compression Find(a) f a b c d e

Timing with Optimization Theorem: A sequence of m union and find operations, n of which are find operations, can be performed on a disjoint-set forest with union by rank (weight or height) and path compression in worst case time proportional to (m (n)). (n) is the inverse Ackermann’s function which grows extremely slowly. For all practical puposes, (n)  4. Union-find is essentially proportional to m for a sequence of m operations, linear in m. End of lecture 36