CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

0 Course Outline n Abstract data types and algorithm analysis (Ch. 2, 3) n C++ review (Ch. 1) n Sets in general: Balanced search trees (Ch. 4 and 12.2)
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
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.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
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.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 22c:31 Algorithms Union-Find for Disjoint Sets.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
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.
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,
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.
The Disjoint Set Class Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 24 © 2002 Addison Wesley.
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
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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 
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
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.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
CSE 326: Data Structures: Set ADT
CSE 373: Data Structures and Algorithms
ADT description Implementations
Data Structures for Disjoint Sets
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
Minimum Spanning Trees and Shortest Paths
Data Structures: Disjoint Sets
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.
CSE 332: Data Structures Disjoint Set Union/Find
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!

Today’s Outline Making a “good” maze Disjoint Set Union/Find ADT Up-trees Weighted Unions Path Compression

What’s a Good Maze?

1.Connected 2.Just one path between any two rooms 3.Random

The Maze Construction Problem Given: –collection of rooms: V –connections between rooms (initially all closed): E Construct a maze: –collection of rooms: V = V –designated rooms in, i  V, and out, o  V –collection of connections to knock down: E  E such that one unique path connects every two rooms

The Middle of the Maze So far, a number of walls have been knocked down while others remain. Now, we consider the wall between A and B. Should we knock it down? When should we not knock it? A B

Maze Construction Algorithm While edges remain in E  Remove a random edge e = (u, v) from E How can we do this efficiently?  If u and v have not yet been connected -add e to E -mark u and v as connected How to check connectedness efficiently?

Equivalence Relations An equivalence relation R must have three properties –reflexive: –symmetric: –transitive: Connection between rooms is an equivalence relation –Why?

Equivalence Relations An equivalence relation R must have 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 Connection between rooms is an equivalence relation –any room is connected to itself –if room a is connected to room b, then room b is connected to room a –if room a is connected to room b and room b is connected to room c, then room a is connected to room c

Disjoint Set Union/Find ADT Union/Find operations –create –destroy –union –find Disjoint set partition property: every element of a DS U/F structure belongs to exactly one set with a unique name Dynamic equivalence property: Union(a, b) creates a new set which is the union of the sets containing a and b {1,4,8} {7}{7} {6}{6} {5,9,10} {2,3} find(4) 8 union(3,6) {2,3,6} name of set

Example Construct the maze on the right Initial (the name of each set is underlined): {a}{b}{c}{d}{e}{f}{g}{h}{i} Randomly select edge 1 Order of edges in blue a d b e c f ghi

Example, First Step {a}{b}{c}{d}{e}{f}{g}{h}{i} find(b)  b find(e)  e find(b)  find(e) so: add 1 to E union(b, e) {a}{b,e}{c}{d}{f}{g}{h}{i} a d b e c f ghi Order of edges in blue

Example, Continued {a}{b,e}{c}{d}{f}{g}{h}{i} Order of edges in blue a d b e c f ghi

Up-Tree Intuition Finding the representative member of a set is somewhat like the opposite of finding whether a given key exists in a set. So, 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.

Up-Tree Union-Find 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 Hash table maps input data to the node associated with that data acgh db e Up-trees are not necessarily binary! fi

Find acgh db e fi find(f) find(e) a d b e c f ghi Just traverse to the root! runtime:

Union acgh db e fi union(a,c) a d b e c f ghi Just hang one root from the other! runtime:

For Your Reading Pleasure...

The Whole Example (1/11) e fghabcdi union(b,e) efghabcdi a d b e c f ghi

The Whole Example (2/11) union(a,d) a d b e c f ghi e fghabcdi fghabci de

The Whole Example (3/11) union(a,b) a d b e c f ghi fghabci de fgha b ci d e

The Whole Example (4/11) find(d) = find(e) No union! a d b e c f ghi fgha b ci d e While we’re finding e, could we do anything else?

The Whole Example (5/11) union(h,i) a d b e c f ghi fgha b ci d e fgha b c i d e

The Whole Example (6/11) union(c,f) a d b e c f ghi fgha b c i d e f gha b c i d e

The Whole Example (7/11) find(e) find(f) union(a,c) a d b e c f ghi f gha b c i d e f gh a b c i d e Could we do a better job on this union?

The Whole Example (8/11) a d b e c f ghi f g ha b c i d e f gh a b c i d e find(f) find(i) union(c,h)

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

The Whole Example (10/11) find(d) find(g) union(c, g) a d b e c f ghi f g ha b c i d e f g ha b c i d e

The Whole Example (11/11) find(g) = find(h) So, no union. And, we’re done! a d b e c f ghi 12 f g ha b c i d e a d b e c f ghi Ooh… scary! Such a hard maze!

f gha b c i d e (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 (f) 6 (g) 7 (h) 8 (i) Nifty storage trick 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. up-index:

Implementation ID find(Object x) { assert(HashTable.contains(x)); ID xID = HashTable[x]; while(up[xID] != -1) { xID = up[xID]; } return xID; } ID union(Object x, Object y) { ID rootx = find(x); ID rooty = find(y); assert(rootx != rooty); up[y] = x; } typedef ID int; ID up[10000]; runtime: O(depth) or …runtime: O(1)

Room for Improvement: Weighted Union Always makes the root of the larger tree the new root Often cuts down on height of the new up-tree f gha b c i d e f gh a b c i d e Could we do a better job on this union? Weighted union! f gha b c i d e

Weighted Union Code ID union(Object x, Object y) { rx = Find(x); ry = Find(y); assert(rx != ry); if (weight[rx] > weight[ry]) { up[ry] = rx; weight[rx] += weight[ry]; } else { up[rx] = ry; weight[ry] += weight[rx]; } typedef ID int; new runtime of union: new runtime of find:

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 2 h nodes , 2 max height  n and max height  log n So, find takes O(log n) Base case: h = 0, tree has 2 0 = 1 node Induction hypothesis: assume true for h < h and consider the sequence of unions. Case 1: Union does not increase max height. Resulting tree still has  2 h nodes. Case 2: Union has height h’= 1+h, where h = height of each of the input trees. By induction hypothesis each tree has  2 h-1 nodes, so the merged tree has at least 2 h nodes. QED.

Alternatives to Weighted Union Union by height Ranked union (cheaper approximation to union by height) See Weiss chapter 8.

Room for Improvement: Path Compression fgha b ci d e While we’re finding e, could we do anything else? Points everything along the path of a find to the root Reduces the height of the entire access path to 1 fgha b ci d e Path compression!

Path Compression Example fha b c d e g find(e) i fha c d e g b i

Path Compression Code ID find(Object x) { assert(HashTable.contains(x)); ID xID = HashTable[x]; ID hold = xID; while(up[xID] != -1) { xID = up[xID]; } while(up[hold] != -1) { temp = up[hold]; up[hold] = xID; hold = temp; } return xID; } runtime:

Digression: Inverse Ackermann’s 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 * (2) = 1 log * (4) = 2 log * (16) = 3 log * (65536) = 4 log * ( ) = 5 (a 20,000 digit number!) log * ( ) = 6 k logs

Complex Complexity of Weighted Union + Path Compression Tarjan (1984) proved that m weighted union and find operations with path commpression on a set of n elements have worst case complexity O(m  log*(n)) actually even a little better! For all practical purposes this is amortized constant time

To Do Read Chapter 8 Graph Algorithms –Weiss Ch 9 Coming Up