Disjoint Sets Textbook Chapter 8

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
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.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation.
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.
Heaps Heaps are used to efficiently implement two operations:
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Minimum Spanning Trees (MST)
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.
Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >
1 22c:31 Algorithms Union-Find for Disjoint Sets.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
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.
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.
Fundamental Data Structures and Algorithms Peter Lee April 24, 2003 Union-Find.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
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.
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
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:
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
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
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 326: Data Structures: Set ADT
CSE 373: Data Structures and Algorithms
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Spring 2016.
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.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Winter 2015.
CSE 332: Data Structures Disjoint Set Union/Find
Data Structures & Algorithms Union-Find Example
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms Union-Find Example
Trees CMSC 202, Version 5/02.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets & Union-Find Dan Grossman Fall 2013.
CSE 332: Data Abstractions Union/Find II
Lecture 12 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Union-Find.
CSE373: Data Structures & Algorithms Implementing Union-Find
CMSC 341 Disjoint Sets.
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Equivalence relations
CMSC 341 Disjoint Sets.
Running Time Analysis Union is clearly a constant time operation.
Timing with Optimization
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
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:

Disjoint Sets Textbook Chapter 8 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, a R b is either true or false. If a R b is true, we say that “a is related to b”. An equivalence relation is a relation R that satisfies three properties (Reflexive) a R a for all a  S (Symmetric) a R b if and only if b R a (Transitive) a R b and b R c implies that a R c 8/3/2007 UMBC CMSC 341 DisjointSets

Equivalence Relation Examples =, but not ≤ Students with the same eye color All cities in the same country Computers connected in a network 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Equivalence Classes The equivalence class for an element a  S is the subset of S that contains all the elements that are related to a. The subsets that represent the equivalence classes will be “disjoint” Example All students in CMSC 341 who are juniors 8/3/2007 UMBC CMSC 341 DisjointSets

Equivalence Relation Application Suppose we have an application involving N distinct items. We will not be adding new items, nor deleting any items. Our application requires us to use an equivalence relation to partition the items into a collection of equivalence classes (subsets) such that: each item is in a set, no item is in more than one set. Examples Classify UMBC students according to class rank. Classify CMSC 341 students according to GPA. Example: student records s1 = set of students with 0< GPA <= 1 s2 = set of students with 1 < GPA <= 2 etc Question: Are x and y in the same set? Yes, if find(x) == find(y). 8/3/2007 UMBC CMSC 341 DisjointSets

Disjoint Set Terminology We identify a set by choosing a representative element of the set. It doesn’t matter which element we choose, but once chosen, it can’t change. There are two operations of interest: find ( x ) -- determine which set x is in. The return value is the representative element of that set union ( x, y ) -- make one set out of the sets containing x and y. Disjoint set algorithms are sometimes called union-find algorithms. 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Disjoint Set Example Given a set of cities, C, and a set of roads, R, that connect two cities (x, y) determine if it’s possible to travel from any given city to another given city. for (each city in C) put each city in its own set for (each road (x,y) in R) if (find( x ) != find( y )) union(x, y) Now we can determine if it’s possible to travel by road between two cities c1 and c2 by testing find(c1) == find(c2) Example: V = {a, b, f, h, r, w} E = {(a,h),(w,h), (b,x), (r,x), (f,x)} C1 = {a, h, w} C2 = {b, x, r, f} 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Up-Trees A simple data structure for implementing disjoint sets is the up-tree. X H F B R A W H, A and W belong to the same set. H is the representative. X, B, R and F are in the same set. X is the representative. 8/3/2007 UMBC CMSC 341 DisjointSets

Operations in Up-Trees find( ) is easy. Just follow pointer to representative element. The representative has no parent. find(x) { if (parent(x)) // not the representative return(find(parent(x)); else return (x); // representative } Example: V={a,b,c,d}, E ={} union(a,b) union(c,a) union(d,c) 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Union Union is more complicated. Make one representative element point to the other, but which way? Does it matter? In the example, some elements are now twice as deep as they were before. 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Union(H, X) H X F X points to H. B, R and F are now deeper. A W B R H X H points to X. A and W are now deeper. F A W B R 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets A Worse Case for Union Union can be done in O(1), but may cause find to become O(n). A B C D E Consider the result of the following sequence of operations: Union (A, B) Union (C, A) Union (D, C) Union (E, D) 8/3/2007 UMBC CMSC 341 DisjointSets

Array Representation of Up-tree Assume each element is associated with an integer i = 0…n-1. From now on, we deal only with i. Create an integer array, s[ n ] An array entry is the element’s parent s[ i ] = -1 signifies that element i is the representative element. Example: V = {0,1,2,3,4,5,6,7} array: -1, -1, -1, -1, -1, -1, -1, -1, -1 union(4,5) union(6,7) union(4,6) now, E={(5,4),(7,6),(6,4)} array: -1, -1, -1, -1, -1, 4, 4, 6 8/3/2007 UMBC CMSC 341 DisjointSets

Union/Find with an Array Now the union algorithm might be: public void union(int root1, int root2) { s[root2 ] = root1; // attaches root2 to root1 } The find algorithm would be public int find(int x) { if (s[x ] < 0) return(x); else return(find(s[x ])); What’t the performance of Union and Find? 8/3/2007 UMBC CMSC 341 DisjointSets

Improving Performance There are two heuristics that improve the performance of union-find. Path compression on find Union by weight 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Path Compression Each time we find( ) an element E, we make all elements on the path from E to the root be immediate children of root by making each element’s parent be the representative. public int find(int x) { if (s[x ]<0) return(x); s[x ] = find(s[x ]); // new code return (s[x ]); } When path compression is used, a sequence of m operations takes O(m lg n) time. Amortized time is O(lg n) per operation. Example: Starting array: -1, -1, -1, -1, -1, 4, 4, 6 find(7) find(6) find(4) resulting array: -1, -1, -1, -1, -1, 4, 4, 4 8/3/2007 UMBC CMSC 341 DisjointSets

“Union by Weight” Heuristic Always attach the smaller tree to larger tree. public void union(int root1, int root2) { rep_root1 = find(root1); rep_root2 = find(root2); if(weight[rep_root1 ] < weight[rep_root2 ]){ s[rep_root1 ] = rep_root2; weight[rep_root2 ]+= weight[rep_root1 ]; } else { s[rep_root2 ] = rep_root1; weight[rep_root1 ] += weight[rep_root2 ]; Example: before: -1, 0, 0, -1, 3 union(0,3) after: -1, 0, 0, 0, 3 8/3/2007 UMBC CMSC 341 DisjointSets

Performance with Union by Weight If unions are performed by weight, the depth of any element is never greater than lg N. Intuitive Proof: Initially, every element is at depth zero. An element’s depth only increases as a result of a union operation if it’s in the smaller tree in which case it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). Only lg N such unions can be performed until all elements are in the same tree Therefore, find( ) becomes O(lg n) when union by weight is used -- even without path compression. Mathematical proof available in Tom A’s notes 8/3/2007 UMBC CMSC 341 DisjointSets

Performance with Both Optimizations When both optimizations are performed a sequence of m (m  n) operations (unions and finds), takes no more than O(m lg* n) time. lg*n is the iterated (base 2) logarithm of n -- the number of times you take lg n before n becomes  1. Union-find is essentially O(m) for a sequence of m operations (amortized O(1)). Example: N lg*N 2 1 lg(2) = 1 4 2 lg(lg(4)) = 1 8 3 lg(lg(lg(8)=3)=1.58) = .664 16 3 lg(lg(lg(16) = 4)=2)=1 65536 4 lg(lg(lg(lg(65536)=16)=4)=2)=1 2^65536 5 lg*(2^65536) = 1 + lg*(65536) 8/3/2007 UMBC CMSC 341 DisjointSets

A Union-Find Application A random maze generator can use union-find. Consider a 5x5 maze: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Maze Generator Initially, 25 cells, each isolated by walls from the others. This corresponds to an equivalence relation -- two cells are equivalent if they can be reached from each other (walls been removed so there is a path from one to the other). 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Maze Generator (cont.) To start, choose an entrance and an exit. IN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 OUT 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets Maze Generator (cont.) Randomly remove walls until the entrance and exit cells are in the same set. Removing a wall is the same as doing a union operation. Do not remove a randomly chosen wall if the cells it separates are already in the same set. 8/3/2007 UMBC CMSC 341 DisjointSets

UMBC CMSC 341 DisjointSets MakeMaze MakeMaze(int size) { entrance = 0; exit = size-1; while (find(entrance) != find(exit)) { cell1 = a randomly chosen cell cell2 = a randomly chosen adjacent cell if (find(cell1) != find(cell2) union(cell1, cell2) } 8/3/2007 UMBC CMSC 341 DisjointSets

Initial State

Intermediate State Algorithm selects wall between 8 and 13. What happens?

A Different Intermediate State Algorithm selects wall between 8 and 13. What happens?

Final State