Chapter 8 Disjoint Sets and Dynamic Equivalence

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.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
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.
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.
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.
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,
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.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
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.
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.
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
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 326: Data Structures: Set ADT
CSE 373: Data Structures and Algorithms
Data Structures for Disjoint Sets
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Week 7 - Friday CS221.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
UNIT III TREES.
Disjoint Sets Chapter 8.
Data Structures: Disjoint Sets
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
Ch. 8 Priority Queues And Heaps
CSE 332: Data Structures Disjoint Set Union/Find
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms Union-Find Example
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Equivalence Relations
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
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
Amortized Analysis and Heaps Intro
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Running Time Analysis Union is clearly a constant time operation.
An application of trees: Union-find problem
Lecture 21 Amortized Analysis
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
General Trees A general tree T is a finite set of one or more nodes such that there is one designated node r, called the root of T, and the remaining nodes.
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Chapter 8 Disjoint Sets and Dynamic Equivalence

Equivalence Relations An equivalence relation R has three properties: reflexive: for any x, xRx is true symmetric: for any x and y, xRy implies yRx transitive: for any x, y, and z, xRy and yRz implies xRz Example equivalence relations? Born in the same year as Live on same island as In same department, where two people are related if they are in the same department This relationship is an equivalence relation if someone was in only one department An example of a relation in which town a is related to town b if traveling from a to b by road is possible. This relationship is an equivalence relation if the roads are two-way. Disjoint Sets 2

Example Building electrical network for a new community. Want to make sure everybody has access to electricity with minimal cost. Union – add wiring between two homes. Find – Are you in same community as power supply? Disjoint Sets 3

Disjoint Set Union/Find ADT Union/Find ADT operations union find create destroy Disjoint set equivalence property: every element of a Disjoint set structure belongs to exactly one set Dynamic equivalence property: the sets can change after execution of a union {1,4,8} {7} {6} {5,9,10} {2,3} find(4) Name of 8 union(2,6) {2,3,6} Disjoint Sets 4

Disjoint Set Union/Find More Formally Given a set U = {a1, a2, … , an} Maintain a partition of U, a set of subsets of U {S1, S2, … , Sk} such that: each pair of subsets Si and Sj are disjoint: together, the subsets cover U: Union(a, b) creates a new subset which is the union of a’s subset and b’s subset NOTE: outside agent decides when/what to union – Abstract Data Type (ADT) is just the bookkeeper Find(a) returns a unique name for a’s subset NOTE: set names are arbitrary! We only care that: Find(a) == Find(b)  a and b are in the same subset Note: we’ll assume that a union is actually passed the names of the subsets. So, there are no hidden finds inside a union. Disjoint Sets 5

How implement? Union(1,4) Union(2,3) Group ID 1 4 2 3 5 6 7 8 9 10 11 Union(1,4) Union(2,3) Every other element it its own set. How long to find? How long to union? Disjoint Sets 6

Linked Lists? How long to find? How long to union? Disjoint Sets 7

Up-Tree Intuition Finding the representative member of a set is somewhat like the inverse of finding whether a given item exists in a set. Instead of using trees with pointers from each node to its children; let’s use trees with a pointer from each node to its parent. Note we never need to know the members of a set – just what set something is in. Disjoint Sets 8

Question With this representation, how would you do a union? Could you find out who is in the set and point them all to the parent (directly)? Would you want to do that? (The doorbell rings…) Disjoint Sets 9

Union-Find Up-Tree Data Structure Each subset is an up-tree with its root as its representative member All members of a given set are nodes in that set’s up-tree It data isn’t linear, Hash table maps input data to the node associated with that data a c g h d b f i k e Up-trees are not necessarily binary! Disjoint Sets 10

Union-Find Up-Tree Data Structure can be stored as an array ** b c d e f g h i j k a c g h d b f i k e Up-trees are not necessarily binary! Disjoint Sets 11

Find – name is at root find(f) find(e) a c g h d k b f i e O(depth) e Just traverse to the root! runtime: Disjoint Sets 12

Union union(a,c) a c g h d b f i k e Just have one root point to the other! runtime: Disjoint Sets 13

Example (1/11) union(b,e) a b c d e f g h i a b c d f g h i e

Example (2/11) union(a,d) a b c d f g h i e a b c f g h i d e

Example (3/11) union(a,b) a b c f g h i d e a c f g h i b d e

Example (4/11) find(d) = find(e) No union! f g h a b c i d e While we’re finding e, could we do anything else?

Example (5/11) union(h,i) a c f g h i a c f g h b d i b d e e

Example (6/11) union(c,f) a c f g h a c g h b d i b d f i e e

Example (7/11) union(e,f) find(e) find(f) union(a,c) f g h a b c i d e Could we do a better job on this union?

Example (8/11) find(f) find(i) union(c,h) c g h c g a f i a f h b d b

Example (9/11) find(e) = find(h) and find(b) = find(c) So, no unions for either of these. 11 c g a f h b d i e

Example (10/11) a b c d e f find(d) find(g) union(c, g) g h i c g g c 12 h i c g g c a f h a f h b d i b d i e e

Example (11/11) find(g) = find(h) So, no union. g c a f h b d i e

Disjoint set data structure f g h a b c i d e A forest of up-trees can easily be stored in an array. Also, if the node names are integers or characters, we can use a very simple, perfect hash. Nifty storage trick! -1 1 2 7 0 (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 (f) 6 (g) 7 (h) 8 (i) up-index: Disjoint Sets 25

How can we make find cheaper? Disjoint Sets 26

Room for Improvement: Smart Union Always makes the root of the larger tree the new root Cuts down on the number of nodes at the lower level f g h a b c i d e c g h a g h a f i b d c i I use weighted because it makes the analysis a bit easier; your book suggests height-based union, and that’s probably fine. b d e f Could we do a better job on this union? e Smart union! Disjoint Sets 27

Union by size A simple improvement to the previous algorithm is to make the smaller tree (in terms of number of nodes) a subtree of the larger, breaking ties by any method. This is called union-by-size. How could we keep track of the number of nodes in a set? Disjoint Sets 28

Disjoint Sets 29

Example shows the worst case tree possible after 15 union by size operations. Disjoint Sets 30

Another idea – union by height Another implementation is union-by-height in which we keep track of the height of the trees and perform union operations by making a shallower tree a subtree of the deeper tree. Disjoint Sets 31

Union by height Code new runtime of union: new runtime of find: UnionSets( int root1, int root2 ) { assertIsRoot(root1); assertIsRoot(root2); if (s[root2] < s[root1]) s[root1]=root2; else {if(s[root1] == s[root2]) s[root1]--; // since the rank is stored as a negative, this makes the value have higher rank. s[root2] = root1; } // else } //UnionSets new runtime of union: Union still takes constant time. Find hasn’t changed, so it still takes O(depth) But what is the maximum depth now? new runtime of find: Disjoint Sets 32

Weighted Union Find Analysis Finds with weighted union are O(max up-tree height) But, an up-tree of height h with weighted union must have at least 2h nodes  2max height  n and max height  log n So, find takes O(log n) Base case: h = 0, tree has 20 = 1 node Induction hypothesis: assume true for h < h A merge can only increase tree height by one over the smaller tree. So, a tree of height h-1 was merged with a larger tree to form the new tree. Each tree then has  2h-1 nodes by the induction hypotheses for a total of at least 2h nodes. QED. When a node gets deeper, it only gets one deeper, and its up-tree must have been the smaller one in the merge. That means that its tree doubled in size. How many times can a tree double in size? Log n. So find takes O(log n). Disjoint Sets 33

could we do a little tidying up? Room for Improvement: Simple idea with enormous consequences Path Compression Points everything along the path (that you were searching anyway) of a find to the root Reduces the height of the entire access path to 1 f g h a b c i d e a c f g h i b d e Path compression! While we’re finding e, could we do a little tidying up? Disjoint Sets 34

Analogy This is a little bit like the doorbell analogy. The doorbell rings. The person at the door is trick or treating. You get the items from the basement, but leave them near the door – so you don’t have to fetch them again. You place yourself near the door so you don’t have to travel to the door again. Disjoint Sets 35

Path Compression Example find(e) c g c g a f h a f h b e b Note that everything along the path got pointed to c. Note also that this is definitely not a binary tree! d d i i e Disjoint Sets 36

Complexity of Weighted Union + Path Compression Ackermann created a function A(x, y) which grows very fast! Inverse Ackermann function (x, y) grows very slooooowwwly … Single-variable inverse Ackermann function is called log* n How fast does log n grow? log n = 4 for n = 16 Let log(k) n = log (log (log … (log n))) Then, let log* n = minimum k such that log(k) n  1 How fast does log* n grow? log* n = 4 for n = 65536 log* n = 5 for n = 265536 (20,000 digit number!) How fast does (x, y) grow? Even sloooowwwer than log* n (x, y) = 4 for n far larger than the number of atoms in the universe (2300) Disjoint Sets 37

Complexity of Weighted Union + Path Compression Tarjan proved that m weighted union and find operations on a set of n elements have worst case complexity O(m(m, n)) This is essentially amortized constant time In some practical cases, weighted union or path compression or both are unnecessary because trees do not naturally get very deep. Disjoint Sets 38

Summary Disjoint Set Union/Find ADT Simple ADT, simple data structure, simple code Complex complexity analysis, but extremely useful result: essentially, constant time! Lots of potential applications Object-property collections Index partitions: e.g. parts inspection To say nothing of maze construction In some applications, it may make sense to have meaningful (non-arbitrary) set names Disjoint Sets 39