Disjoint Sets Given a set {1, 2, …, n} of n elements.

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Union-Find structure Mikko Malinen School of Computing University of Eastern Finland.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
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.
CS Data Structures Chapter 5 Trees. Forests  Definition: A forest is a set of n ≥ 0 disjoint trees.  When we remove a root from a tree, we’ll.
Minimum Spanning Trees (MST)
© 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.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
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.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
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 
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.
Parent Pointer Implementation
Data Structures for Disjoint Sets
CSCE 210 Data Structures and Algorithms
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Binary Search Trees A binary search tree is a binary tree
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Chapter 8 Disjoint Sets and Dynamic Equivalence
Chapter 11: Multiway Search Trees
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.
Multi-Way Search Trees
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
ICS 353: Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Binary and Binomial Heaps
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
Running Time Analysis Union is clearly a constant time operation.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

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 and find operations is performed. A union operation combines two sets into one. Each of the n elements is in exactly one set at any time. A find operation identifies the set that contains a particular element. Application of trees (not binary trees) and simulated pointers. Simple application of simulated pointers; one that does not require an explicit storage management system. In this application we are concerned with the time taken to perform an intermixed sequence of operations rather than the time for an individual operation.

Using Arrays And Chains Best time complexity using arrays and chains is O(n + u log u + f), where u and f are, respectively, the number of union and find operations that are done. Using a tree (not a binary tree) to represent a set, the time complexity becomes almost O(n + f) (assuming at least n/2 union operations). Number of union operations cannot exceed n-1.

A Set As A Tree S = {2, 4, 5, 9, 11, 13, 30} Some possible tree representations: 11 4 2 9 30 5 13 4 2 9 11 30 5 13 4 2 9 30 5 13 11

Result Of A Find Operation find(i) is to identify the set that contains element i. In most applications of the union-find problem, the user does not provide set identifiers. The requirement is that find(i) and find(j) return the same value iff elements i and j are in the same set. 4 2 9 11 30 5 13 find(i) will return the element that is in the tree root.

Strategy For find(i) 4 2 9 30 5 13 11 Start at the node that represents element i and climb up the tree until the root is reached. Return the element in the root. To climb the tree, each node must have a parent pointer.

Trees With Parent Pointers 7 8 3 22 6 10 20 16 14 12 4 2 9 30 5 13 11 1

Possible Node Structure Use nodes that have two fields: element and parent. Use an array table[] such that table[i] is a pointer to the node whose element is i. To do a find(i) operation, start at the node given by table[i] and follow parent fields until a node whose parent field is null is reached. Return element in this root node.

Example (Only some table entries are shown.) table[] 4 2 9 30 5 13 11 5 10 15

Better Representation Use an integer array parent[] such that parent[i] is the element that is the parent of element i. 4 2 9 30 5 13 11 1 parent[] 5 10 15 2 9 13 13 4 5

Union Operation union(i,j) i and j are the roots of two different trees, i != j. To unite the trees, make one tree a subtree of the other. parent[j] = i

Union Example 7 8 3 22 6 10 20 16 14 12 4 2 9 30 5 13 11 1 union(7,13)

void simpleUnion(int i, int j) {parent[i] = j;} The Union Function void simpleUnion(int i, int j) {parent[i] = j;}

Time Complexity Of simpleUnion()

The Find Function int simpleFind(int i) { while (parent[i] >= 0) i = parent[i]; // move up the tree return i; }

Time Complexity of simpleFind() Tree height may equal number of elements in tree. union(2,1), union(3,2), union(4,3), union(5,4)… 5 4 3 2 1 So complexity is O(u).

u Unions and f Find Operations O(u + uf) = O(uf) Time to initialize parent[i] = 0 for all i is O(n). Total time is O(n + uf). Worse than using a chain! Back to the drawing board.

Smart Union Strategies 7 8 3 22 6 10 20 16 14 12 4 2 9 30 5 13 11 1 union(7,13) Which tree should become a subtree of the other?

Height Rule Make tree with smaller height a subtree of the other tree. Break ties arbitrarily. 4 2 9 30 5 13 11 1 7 8 3 22 6 10 20 16 14 12 union(7,13)

Weight Rule Make tree with fewer number of elements a subtree of the other tree. Break ties arbitrarily. 7 8 3 22 6 10 20 16 14 12 4 2 9 30 5 13 11 1 union(7,13)

Implementation Root of each tree must record either its height or the number of elements in the tree. When a union is done using the height rule, the height increases only when two trees of equal height are united. When the weight rule is used, the weight of the new tree is the sum of the weights of the trees that are united.

Height Of A Tree Suppose we start with single element trees and perform unions using either the height or the weight rule. The height of a tree with p elements is at most floor (log2p) + 1. Proof is by induction on p. See text.

Sprucing Up The Find Method 12 14 20 10 22 a 4 2 9 30 5 13 11 1 7 8 3 6 16 b c d e f g a, b, c, d, e, f, and g are subtrees find(1) Do additional work to make future finds easier.

Path Compaction a, b, c, d, e, f, and g are subtrees Make all nodes on find path point to tree root. find(1) 12 14 20 10 22 a 4 2 9 30 5 13 11 1 7 8 3 6 16 b c d e f g a, b, c, d, e, f, and g are subtrees Makes two passes up the tree.

Path Splitting a, b, c, d, e, f, and g are subtrees Nodes on find path point to former grandparent. find(1) 12 14 20 10 22 a 4 2 9 30 5 13 11 1 7 8 3 6 16 b c d e f g a, b, c, d, e, f, and g are subtrees Makes only one pass up the tree.

Path Halving a, b, c, d, e, f, and g are subtrees Parent pointer in every other node on find path is changed to former grandparent. find(1) 12 14 20 10 22 a 4 2 9 30 5 13 11 1 7 8 3 6 16 b c d e f g a, b, c, d, e, f, and g are subtrees Changes half as many pointers.

Time Complexity Ackermann’s function. Inverse of Ackermann’s function. A(i,j) = 2j, i = 1 and j >= 1 A(i,j) = A(i-1,2), i >= 2 and j = 1 A(i,j) = A(i-1,A(i,j-1)), i, j >= 2 Inverse of Ackermann’s function. a(p,q) = min{z>=1 | A(z, p/q) > log2q}, p >= q >= 1

Time Complexity Ackermann’s function grows very rapidly as i and j are increased. A(2,4) = 265,536 The inverse function grows very slowly. a(p,q) < 5 until q = 2A(4,1) A(4,1) = A(2,16) >>>> A(2,4) In the analysis of the union-find problem, q is the number, n, of elements; p = n + f; and u >= n/2. For all practical purposes, a(p,q) < 5. The number of atoms in the Universe is about 1079 and the number of photons and neutrinos is about 1088. A(2,4) (and so also A(4,1)) dwarfs these numbers!

Time Complexity Lemma 5.6 [Tarjan and Van Leeuwen] Let T(f,u) be the maximum time required to process any intermixed sequence of f finds and u unions. Assume that u >= n/2. k1*(n + f*a(f+n, n)) <= T(f,u) <= k2*(n + f*a(f+n, n)) where k1 and k2 are constants. These bounds apply when we start with singleton sets and use either the weight or height rule for unions and any one of the path compression methods for a find.