Disjoint Union / Find CSE 373 Data Structures Lecture 17.

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.
Union-Find: A Data Structure for Disjoint Set Operations
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 326: Data Structures Hashing Ben Lerner Summer 2007.
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.
Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.
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.
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
COMP 261 Lecture 12 Disjoint Sets. Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 22c:31 Algorithms Union-Find for Disjoint Sets.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
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.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
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,
Fall 2005Costas Busch - RPI1 Mathematical Preliminaries.
Prof. Busch - LSU1 Mathematical Preliminaries. Prof. Busch - LSU2 Mathematical Preliminaries Sets Functions Relations Graphs Proof Techniques.
Lectures on Greedy Algorithms and Dynamic Programming
Union-find Algorithm Presented by Michael Cassarino.
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 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.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
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:
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
1 Mathematical Preliminaries. 2 Sets Functions Relations Graphs Proof Techniques.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
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
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Spring 2016.
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
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets & Union-Find Dan Grossman Fall 2013.
CSE 332: Data Abstractions Union/Find II
Mathematical Preliminaries
CSE373: Data Structures & Algorithms Implementing Union-Find
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Minimum Spanning Trees
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Disjoint Union / Find CSE 373 Data Structures Lecture 17

5/15/03Union/Find - Lecture 172 Reading ›Chapter 8 (you can skip Section 6)

5/15/03Union/Find - Lecture 173 Equivalence Relations A relation R is defined on set S if for every pair of elements a, b S, a R b is either true or false. An equivalence relation is a relation R that satisfies the 3 properties: ›Reflexive: a R a for all a S ›Symmetric: a R b iff b R a; a, b S ›Transitive: a R b and b R c implies a R c

5/15/03Union/Find - Lecture 174 Equivalence Classes Given an equivalence relation R, decide whether a pair of elements a, b S is such that a R b. The equivalence class of an element a is the subset of S of all elements related to a. Equivalence classes are disjoint sets

5/15/03Union/Find - Lecture 175 Dynamic Equivalence Problem Starting with each element in a singleton set, and an equivalence relation, build the equivalence classes Requires two operations: ›Find the equivalence class (set) of a given element ›Union of two sets It is a dynamic (on-line) problem because the sets change during the operations and Find must be able to cope!

5/15/03Union/Find - Lecture 176 Disjoint Union - Find Maintain a set of pairwise disjoint sets. ›{3,5,7}, {4,2,8}, {9}, {1,6} Each set has a unique name, one of its members ›{3,5,7}, {4,2,8}, {9}, {1,6}

5/15/03Union/Find - Lecture 177 Union Union(x,y) – take the union of two sets named x and y ›{3,5,7}, {4,2,8}, {9}, {1,6} ›Union(5,1) {3,5,7,1,6}, {4,2,8}, {9},

5/15/03Union/Find - Lecture 178 Find Find(x) – return the name of the set containing x. ›{3,5,7,1,6}, {4,2,8}, {9}, ›Find(1) = 5 ›Find(4) = 8

5/15/03Union/Find - Lecture 179 An Application Build a random maze by erasing edges.

5/15/03Union/Find - Lecture 1710 An Application (ct’d) Pick Start and End Start End

5/15/03Union/Find - Lecture 1711 An Application (ct’d) Repeatedly pick random edges to delete. Start End

5/15/03Union/Find - Lecture 1712 Desired Properties None of the boundary is deleted Every cell is reachable from every other cell. There are no cycles – no cell can reach itself by a path unless it retraces some part of the path.

5/15/03Union/Find - Lecture 1713 A Cycle (we don’t want that) Start End

5/15/03Union/Find - Lecture 1714 A Good Solution Start End

5/15/03Union/Find - Lecture 1715 Good Solution : A Hidden Tree Start End

5/15/03Union/Find - Lecture 1716 Number the Cells Start End We have disjoint sets S ={ {1}, {2}, {3}, {4},… {36} } each cell is unto itself. We have all possible edges E ={ (1,2), (1,7), (2,8), (2,3), … } 60 edges total.

5/15/03Union/Find - Lecture 1717 Basic Algorithm S = set of sets of connected cells E = set of edges Maze = set of maze edges initially empty While there is more than one set in S pick a random edge (x,y) and remove from E u := Find(x); v := Find(y); if u  v then Union(u,v) //knock down the wall between the cells (cells in //the same set are connected) else add (x,y) to Maze //don’t remove because there is already // a path between x and y All remaining members of E together with Maze form the maze

5/15/03Union/Find - Lecture 1718 Example Step Start End S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,30,32 33,34,35,36} Pick (8,14)

5/15/03Union/Find - Lecture 1719 Example S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Find(8) = 7 Find(14) = 20 S {1,2,7,8,9,13,19,14,20 26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Union(7,20)

5/15/03Union/Find - Lecture 1720 Example Start End S {1,2,7,8,9,13,19 14,20,26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Pick (19,20)

5/15/03Union/Find - Lecture 1721 Example at the End Start End S {1,2,3,4,5,6,7,… 36} E Maze

5/15/03Union/Find - Lecture 1722 Up-Tree for DU/F Initial state Intermediate state Roots are the names of each set.

5/15/03Union/Find - Lecture 1723 Find Operation Find(x) follow x to the root and return the root Find(6) = 7

5/15/03Union/Find - Lecture 1724 Union Operation Union(i,j) - assuming i and j roots, point i to j Union(1,7)

5/15/03Union/Find - Lecture 1725 Simple Implementation Array of indices (Up[i] is parent of i) up Up [x] = 0 means x is a root.

5/15/03Union/Find - Lecture 1726 Union Union(up[] : integer array, x,y : integer) : { //precondition: x and y are roots// Up[x] := y } Constant Time!

5/15/03Union/Find - Lecture 1727 Find Design Find operator ›Recursive version ›Iterative version Find(up[] : integer array, x : integer) : integer { //precondition: x is in the range 1 to size// ??? }

5/15/03Union/Find - Lecture 1728 A Bad Case 123n … 1 23n Union(1,2) 1 2 3n Union(2,3) Union(n-1,n) … … n :::: Find(1) n steps!!

5/15/03Union/Find - Lecture 1729 Weighted Union Weighted Union (weight = number of nodes) ›Always point the smaller tree to the root of the larger tree W-Union(1,7) 2 4 1

5/15/03Union/Find - Lecture 1730 Example Again 123n 1 23n Union(1,2) n Union(2,3) Union(n-1,n) … … :::: 1 2 3n … Find(1) constant time …

5/15/03Union/Find - Lecture 1731 Analysis of Weighted Union With weighted union an up-tree of height h has weight at least 2 h. Proof by induction ›Basis: h = 0. The up-tree has one node, 2 0 = 1 ›Inductive step: Assume true for all h’ < h. h-1 Minimum weight up-tree of height h formed by weighted unions T1T1 T2T2 TW(T 1 ) > W(T 2 ) > 2 h-1 Weighted union Induction hypothesis W(T) > 2 h h-1 = 2 h

5/15/03Union/Find - Lecture 1732 Analysis of Weighted Union Let T be an up-tree of weight n formed by weighted union. Let h be its height. n > 2 h log 2 n > h Find(x) in tree T takes O(log n) time. Can we do better?

5/15/03Union/Find - Lecture 1733 Worst Case for Weighted Union n/2 Weighted Unions n/4 Weighted Unions

5/15/03Union/Find - Lecture 1734 Example of Worst Cast (cont’) After n -1 = n/2 + n/4 + …+ 1 Weighted Unions Find If there are n = 2 k nodes then the longest path from leaf to root has length k. log 2 n

5/15/03Union/Find - Lecture 1735 Elegant Array Implementation up weight Can save the extra space by storing the complement of weight in the space reserved for the root

5/15/03Union/Find - Lecture 1736 Weighted Union W-Union(i,j : index){ //i and j are roots// wi := weight[i]; wj := weight[j]; if wi < wj then up[i] := j; weight[j] := wi + wj; else up[j] :=i; weight[i] := wi +wj; }

5/15/03Union/Find - Lecture 1737 Path Compression On a Find operation point all the nodes on the search path directly to the root PC-Find(3)

5/15/03Union/Find - Lecture 1738 Self-Adjustment Works PC-Find(x) x

5/15/03Union/Find - Lecture 1739 Path Compression Find PC-Find(i : index) { r := i; while up[r]  0 do //find root// r := up[r]; if i  r then //compress path// k := up[i]; while k  r do up[i] := r; i := k; k := up[k] return(r) }

5/15/03Union/Find - Lecture 1740 Example i

5/15/03Union/Find - Lecture 1741 Disjoint Union / Find with Weighted Union and PC Worst case time complexity for a W- Union is O(1) and for a PC-Find is O(log n). Time complexity for m  n operations on n elements is O(m log* n) where log* n is a very slow growing function. ›log * n < 7 for all reasonable n. Essentially constant time per operation!

5/15/03Union/Find - Lecture 1742 Amortized Complexity For disjoint union / find with weighted union and path compression. ›average time per operation is essentially a constant. › worst case time for a PC-Find is O(log n). An individual operation can be costly, but over time the average cost per operation is not.

5/15/03Union/Find - Lecture 1743 Find Solutions Find(up[] : integer array, x : integer) : integer { //precondition: x is in the range 1 to size// if up[x] = 0 then return x else return Find(up,up[x]); } Find(up[] : integer array, x : integer) : integer { //precondition: x is in the range 1 to size// while up[x]  0 do x := up[x]; return x; } Recursive Iterative