CSE 100 Disjoint Set, Union Find

Similar documents
CSE 100: GRAPH ALGORITHMS

CSE 100: UNION-FIND HASH

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

CMSC 341 Lecture 20 Disjointed Sets

6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro

CMSC 341 Lecture 20 Disjointed Sets

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

CMSC 341 Lecture 20 Disjointed Sets

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Disjoint Sets. Based on slides from previous iterations of this course.

lecture24: Disjoint Sets

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Lecture 13: Minimum Spanning Trees Steven Skiena

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

Union-Find: A Data Structure for Disjoint Set Operations

CS 310 Advanced Data Structures and Algorithms

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Chapter 23. Minimum Spanning Trees

CS 161: Design and Analysis of Algorithms

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Minimum spanning trees

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

Lecture 4 Greedy algorithms

Disjoint Sets and the Union/Find Problem

CSC263 Week 11. Larry Zhang.

COMP Analysis of Algorithms & Data Structures

Reading. Disjoint Union / Find. Union. Disjoint Union - Find. Cute Application. Find. Reading. CSE 326 Data Structures Lecture 14

Maintaining Disjoint Sets

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

COMP251: Disjoint sets

Advanced Set Representation Methods

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CS S-14 Disjoint Sets 1

Union-find algorithms. (1) How do you determine whether a vertex x is connected to a vertex y in a graph?

Greedy Approach: Intro

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

Data Structures for Disjoint Sets

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Chapter 5. Greedy algorithms

Disjoint Sets. The obvious data structure for disjoint sets looks like this.

Lecture Summary CSC 263H. August 5, 2016

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

Complexity of Prim s Algorithm

N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label

CSE 638: Advanced Algorithms. Lectures 10 & 11 ( Parallel Connected Components )

CSE 202: Design and Analysis of Algorithms Lecture 3

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Priority Queues. 04/10/03 Lecture 22 1

Union-Find and Amortization

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Disjoint set (Union-Find)

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Kruskal s MST Algorithm

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Disjoint Sets. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION

Outline Purpose Disjoint Sets Maze Generation. Disjoint Sets. Seth Long. March 24, 2010

CSE 373: Data Structures and Algorithms. Disjoint Sets. Autumn Shrirang (Shri) Mare

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50

Amortized Analysis and Union-Find , Inge Li Gørtz

CSE 21 Mathematics for Algorithm and System Analysis

Optimization I : Brute force and Greedy strategy

( ) n 3. n 2 ( ) D. Ο

Course Review. Cpt S 223 Fall 2010

Introduction to Optimization

Union-Find: A Data Structure for Disjoint Set Operations

CS : Data Structures

Data Structures in Java. Session 18 Instructor: Bert Huang

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics:

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

CSE 373 Data Structures & Algorithms

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

What is a minimal spanning tree (MST) and how to find one

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS301 All Current Final Term Paper Subjective 2013 Solved with refernces

CS 561, Lecture 9. Jared Saia University of New Mexico

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Minimum Spanning Trees

Outline. Disjoint set data structure Applications Implementation

Priority queues. Priority queues. Priority queue operations

Union-Find Structures. Application: Connected Components in a Social Network

Lower Bound on Comparison-based Sorting

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CSE 530A. B+ Trees. Washington University Fall 2013

CISC 320 Midterm Exam

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Transcription:

CSE 100 Disjoint Set, Union Find

Union-find using up-trees The union-find data structure 1 0 2 6 7 4 3 5 8 Perform these operations: Find(4) =! Find(3) =! Union(1,0)=! Find(4)=! 2

Array representation of Up-trees A compact and elegant implementation Each entry is the up index -1 for the roots Write the forest of trees, showing parent pointers and node labels, represented by this array 1 0 3 2 5 6 7 8 Find(4) 4 0 1 2 3 4 5 6 7-1 0-1 0 1 2-1 -1 7 8 3

Performing a union operation Union(6,7) Fill in 6, 7 and 8 in the array 0 2 6 7 1 3 5 8 4 0 1 2 3 4 5 6 7 8-1 0-1 0 1 2-1 -1 7-1 0-1 0 1 2 4

Disjoint subsets using up-trees (Simple Union and Find) Start with 4 items: 0, 1, 2, 3 Suppose Union(i,j) makes the node Find(i) the parent of the node Find(j) Perform these operations: Union(2,3)! Union(1,2)! Find(0) = Find(3) = Union(0,1) Find(1) =! 0 1 2 3-1 -1-1 -1 5

Run time of Simple union and find (using up-trees) If we have no rule in place for performing the union of two up trees, what is the worst case run time of find and union operations in terms of the number of elements N? A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) 6

Improving Union operations In the Union operation, a tree becomes like a linked list if, when joining two trees, the root of the smaller tree (e.g. a single node) always becomes the root of the new tree We can avoid this by making sure that in a Union operation, the larger of the two trees roots becomes the root of the new tree (ties are broken arbitrarily) a c g h c g h a g h b d f i a f i b d c i e b d e f e Henry Kautz, U. Washington 7

Smarter Union operations Avoid this problem by having the larger of the 2 trees become the root Union-by-size Each root stores the size (# nodes) of its respective tree The root with the larger size becomes the parent Update its size = sum of its former size and the size of its new child Break ties arbitrarily a c b d f e Scott B. Baden / CSE 100-A / Spring 2013 8

Disjoint subsets using trees (Union-by-size and simple Find) Start with 4 items: 0, 1, 2, 3 Assume that Union(i,j) makes the root of the smaller tree a child of the root of the larger tree. Perform these operations: Union(2,3)! Union(1,2)! Find(0) =! Find(3) =! Union(0,2)! Find(1) =! Find(0) =! 9

Smarter Union operations Avoid this problem by having the larger of the 2 trees become the root Union-by-size Union-by-height (also called union by rank) Each root stores the height of its respective tree If one root has a greater height than the other, it becomes the parent. Its stored height doesn t need to be updated If the roots show equal height, pick either one as the parent Its stored height should be increased by one Break ties arbitrarily a c b d f e Scott B. Baden / CSE 100-A / Spring 2013 10

Disjoint subsets using trees (Union-by-height and simple Find) Start with 4 items: 0, 1, 2, 3 Assume that Union(i,j) makes the root of the shorter tree a child of the root of the taller tree. Perform these operations: Union(2,3)! Union(1,2)! Find(0) =! Find(3) =! Union(0,2) =! Find(1) =! Find(0) =! 11

Cost of disjoint subsets operations with smarter Union and simple Find Either union-by-size or union-by-height will guarantee that the height of any tree is no more than log 2 N, where N is the total number of nodes in all trees Q: What is the worst case run time of find and union operations in terms of the number of elements N? A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) Solo vote! 12

Bounding the height of the up-tree using union by size. Initially all the nodes are in singleton trees (with height 1) Take the perspective of a single node. The only time the height of the tree which the node is part of increases by 1 is when the node joins a larger group i.e. if the height of the node s uptree increases by 1, the number of nodes in that tree would have at least doubled The maximum number of nodes in any tree is N, so the height of the resulting tree can be at most log N 13

Cost of disjoint subsets operations with smarter Union and simple Find Q: What is the worst case run time of find and union operations in terms of the number of elements N? A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) Therefore, doing N-1 union operations (the maximum possible) and M find operations takes time O(N + M log 2 N) worst case With simple unions the complexity was: This is a big improvement; but we can do still better, by a slight change to the Find operation: adding path compression (next lecture) Discuss and revote 14

What is the improvement from simple union find? 15 Ref: Tim Roughgarden (stanford) Kruskal s algorithm run time with smart union path compression find: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E 3a. S1=find(u); S2=find(v) 3b. if (S1!=S2){ //If T U {e i =u,v} has no cycles Add e i to T union(s1, S2)}

Union-Find Code Using the array representation for disjoint subsets, the code for implementing the Disjoint Subset ADT s methods is compact class DisjSets{ int *array; }!! /**! * Construct the disjoint sets object! * numelements is the initial number of disjoint sets! */! DisjSets( int numelements )! { array = new int [ numelements ];! for( int i = 0; i < numelements; i++ ) array[ i ] = -1;! }! 16

Union-by-height /**! * Union two disjoint sets using the height heuristic.! * For simplicity, we assume root1 and root2 are distinct! * and represent set labels.! *! root1 is the root of set 1! *! root2 is root of set 2! * returns the root of the union! */! int union ( int root1, int root2 )!{!! if( array[ root2 ] < array[ root1 ] ) { // root2 is higher! array[ root1 ] = root2;!! // Make root2 new root! return root2;! } else!{! }!! }! if( array[ root1 ] == array[ root2 ] )! array[ root1 ]--;!! array[ root2 ] = root1;! return root1;! // Update height if same! // Make root1 new root! 17