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

Similar documents
CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets

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

Union-Find: A Data Structure for Disjoint Set Operations

Disjoint Sets and the Union/Find Problem

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

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Union-Find: A Data Structure for Disjoint Set Operations

Chapter 5. Greedy algorithms

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

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

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

Advanced Set Representation Methods

CSE 100 Disjoint Set, Union Find

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

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

CSE 100: GRAPH ALGORITHMS

For this graph, give its (a) adjacency matrix, (b) upper triangular adjacency matrix input format, (c) adjacency list, and (d) adjacency list input

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Greedy Approach: Intro

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

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

COMP Analysis of Algorithms & Data Structures

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

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

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

CS 310 Advanced Data Structures and Algorithms

Bob s Notes for COS 226 Fall : Introduction, Union-Find, and Percolation. Robert E. Tarjan 9/15/13. Introduction

Lecture 4 Greedy algorithms

Data Structures for Disjoint Sets

Lecture Summary CSC 263H. August 5, 2016

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

Data structures. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

Disjoint set (Union-Find)

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

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

EECS 477: Introduction to algorithms. Lecture 10

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 3 Chapter 10 ABSTRACT ALGORITHMS 2- GREEDY METHODS

Union-Find Disjoint Set

Disjoint Sets. Andreas Klappenecker [using notes by R. Sekar]

CS 161: Design and Analysis of Algorithms

Data Structures in Java. Session 18 Instructor: Bert Huang

Graph Based Image Segmentation

Data Structures in Java. Session 18 Instructor: Bert Huang

Disjoint-set data structure: Union-Find. Lecture 20

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

lecture24: Disjoint Sets

Priority Queues. 04/10/03 Lecture 22 1

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

Optimization I : Brute force and Greedy strategy

Unit 3 Chapter 10 ABSTRACT ALGORITHMS 2GREEDY METHODS

looking ahead to see the optimum

CS S-14 Disjoint Sets 1

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

Union-Find and Amortization

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

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

CSE 5095 Notes. Author : Michael Tedford. Class Date : 18th February 2014

Disjoint Sets. Maintaining Disjoint Sets Complexity Analysis

COMP Data Structures

Lecture 13: Minimum Spanning Trees Steven Skiena

CPS222 Lecture: Sets. 1. Projectable of random maze creation example 2. Handout of union/find code from program that does this

CS-301 Data Structure. Tariq Hanif

Maintaining Disjoint Sets

CSC263 Week 11. Larry Zhang.

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

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

CSci 231 Final Review

Algorithms. Algorithms 1.5 UNION FIND. union find data type quick-find quick-union improvements applications ROBERT SEDGEWICK KEVIN WAYNE

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

Course Review for Finals. Cpt S 223 Fall 2008

Outline. Disjoint set data structure Applications Implementation

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

Solutions to Exam Data structures (X and NV)

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

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

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

Minimum Spanning Trees

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

Dynamic Data Structures. John Ross Wallrabenstein

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

CS F-14 Disjoint Sets 1

Union Find. Data Structures and Algorithms Andrei Bulatov

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CSE 100: UNION-FIND HASH

Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

Priority Queue: Heap Structures

Fibonacci Heaps Priority Queues. John Ross Wallrabenstein

Course Review. Cpt S 223 Fall 2009

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Disjoint Sets ADT 1. A disjoint sets data structure keeps track of multiple sets which do not share any elements. Here s the ADT: UnionFind ADT

Final Examination CSE 100 UCSD (Practice)

UNION FIND. naïve linking link-by-size link-by-rank path compression link-by-rank with path compression context

CSE 548: (Design and) Analysis of Algorithms

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

Transcription:

Disjoint Sets Based on slides from previous iterations of this course

Today s Topics Exam Discussion Introduction to Disjointed Sets Disjointed Set Example Operations of a Disjointed Set Types of Disjointed Sets Optimization of Disjointed Sets Code for Disjointed Sets Performance of Disjointed Sets

Introduction to Disjointed Sets

Disjoint Sets A data structure that keeps track of a set of elements partitioned into a number of disjoint (non overlapping) subsets

Universe of Items Universal set is made up of all of the items that can be a member of a set Universe of Items A E B D C

Disjoint Sets A group of sets where no item can be in more than one set Universe of Items S1 S2 A E S3 B D S4 C

Disjoint Sets A group of sets where no item can be in more than one set S1 S3 Supported Operations: Find() Union() MakeSet() A E B D S4 C S2

Uses for Disjointed Sets Maze generation Kruskal's algorithm for computing the minimum spanning tree of a graph 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 Determining if there are cycles in a graph

Disjoint Set Example

Disjoint Set with No Unions 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A negative number means we are at the root A positive number means we need to move or walk to that index to find our root The LONGER the path, the longer it takes to find, and moves farther away from our goal of a constant timed function

Disjoint Set with Some Unions 9 4 8 0 1 2 3 5 6 7 10 11 12 13 1 1 4 1 1 1 1 8 9 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Notice: Value of index is where the index is linked to

Operations of a Disjoint Set

Find() Determine which subset an element is in Returns the name of the subset Find() typically returns an item from this set that serves as its "representative By comparing the result of two Find() operations, one can determine whether two elements are in the same subset

Find() Asks the question, what set does item E belong to currently? S1 S3 What does Find(E) return? Returns S2 A B S4 C E D S2

Union() Union() Merge two sets (w/ one or more items) together Order can be important One of the roots from the 2 sets will become the root of the merged set

Union() Join two subsets into a single subset. S1 S3 Before Union(S2, S1) A B S4 After Union(S2, S1) C E D S2

MakeSet() Makes a set containing only a given element (a singleton) Implementation is generally trivial

Types of Disjoint Sets

Types of Disjoint Sets There are two types of disjoint sets 1. Array Based Disjoint Sets 2. Tree Based Disjoint Sets (We can also implement with a linked list)

Array Based Disjoint Sets We will assume that elements are 0 to n 1 Maintain an array A: for each element i, A[i] is the name of the set containing i

Array Based Disjoint Sets Find(i) Runs in O(1) Union(i,j) returns A[i] requires scanning entire array Runs in O(n) for (k = 0;k < n; k++) { if (A[k] == A[j]) { A[k] = A[i]; } }

Tree Based Disjoint Sets Disjoint set forests are data structures Each set is represented by a tree data structure Each node holds a reference to its parent node In a disjoint set forest, the representative of each set is the root of that set's tree

Tree Based Disjoint Sets Find() follows parent nodes until it reaches the root Union() combines two trees into one by attaching the root of one to the root of the other

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 (B, C) Union (C, D) Union (D, E)

Optimization of Disjointed Sets

Optimization Three main optimization operations: 1. Union by rank (size) 2. Union by rank (height) 3. Path Compression

Be very clear about how the array representations change for different things (union by size, union by height, etc.)

Union by Rank (size) Size = number of nodes (including root) in given set A strategy to keep items in a tree from getting too deep (large paths) by uniting sets intelligently At each root, we record the size of its sub tree The number of nodes in the collective tree

Union by Rank (size) 10 9 4 0 1 2 3 5 6 7 8 11 12 13 1 1 4 1 2 1 1 8 9 5 9 9 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Notice two things: 1. Value of index is where the index is linked to 2. As the size of the set increases, the negative number size of the root increases (see 4 and 9)

Union by Rank (height) A strategy to keep items in a tree from getting too deep (large paths) by uniting sets intelligently At each root, we record the height of its sub tree When uniting two trees, make the smaller tree a subtree of the larger one So that the tree that is larger does not add another level!!

Union by Rank (height) 9 4 8 0 1 2 3 5 6 7 10 11 12 13 1 1 4 1 2 1 1 8 9 3 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Notice two things: 1. Value of index is where the index is linked to 2. As the size of the set increases, the negative number height of the root increases (see 4 and 9)

Union by Rank (height) 9 4 8 0 1 2 3 5 6 7 10 11 12 13 1 1 4 1 2 1 1 8 9 3 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 What if we merge {2,4} with {7, 8, 9}? Because 9 has a greater height than 4, 4 would be absorbed into 9.

Union by Rank (height) 2 4 8 9 0 1 3 5 6 7 10 11 12 13 Update 4 to point to 9 1 1 4 1 9 1 1 8 9 3 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 When uniting two trees, make the smaller tree a sub-tree of the larger one so that the one tree that is larger does not add another level!!

Example of Unions If we union 5 and 9, how will they be joined?

By rank (size)? Example of Unions 9 becomes a child of 5 By rank (height)? 5 becomes a child of 9

Path Compression If our path gets longer, operations take longer We can shorten this (literally and figuratively) by updating the element values of each child directly to the root node value No more walking through to get to the root Done as part of Find() So the speed up will be eventual

Path Compression Theoretically flattens out a tree Uses recursion Base case Until you find the root Return the root value Reassign as the call stack collapses

Path Compression Before Path Compression 4 0 1 2 3 5 6 10 7 8 9 11 12 1 1 4 1 1 1 10 8 9 1 9 9 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 13 13 During a Find(), we update the index to point to the root

Path Compression After Path Compression 4 0 1 2 3 5 10 6 8 9 7 11 12 13 1 1 4 1 1 1 9 9 9 1 9 9 9 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 After we run Find(6)we update it to point to 9 After we run Find(13)and Find(7)we update them to point to 9 Along with all other nodes between 13 and 9!

Code for Disjoint Sets

function MakeSet(x) x.parent := x Generic Code function Find(x) if x.parent == x return x else return Find(x.parent) function Union(x, y) xroot := Find(x) yroot := Find(y) xroot.parent := yroot

C++ Implementation class UnionFind { int[] u; UnionFind(int n) { u = new int[n]; for (int i = 0; i < n; i++) u[i] = -1; } int find(int i) { int j,root; for (j = i; u[j] >= 0; j = u[j]) ; root = j; while (u[i] >= 0) { j = u[i]; u[i] = root; i = j; } return root; } } void union(int i,int j) { i = find(i); j = find(j); if (i!=j) { if (u[i] < u[j]) { u[i] += u[j]; u[j] = i; } else { u[j] += u[i]; u[i] = j; } } }

The UnionFind class class UnionFind { int[] u; UnionFind(int n) { u = new int[n]; for (int i = 0; i < n; i++) u[i] = -1; } int find(int i) {... } } void union(int i,int j) {... }

Trick 1: Iterative find int find(int i) { int j, root; for (j = i; u[j] >= 0; j = u[j]) ; root = j; while (u[i] >= 0) { j = u[i]; u[i] = root; i = j; } } return root;

Trick 2: Union by size void union(int i,int j) { i = find(i); j = find(j); } if (i!= j) { if (u[i] < u[j]) { u[i] += u[j]; u[j] = i; } else { u[j] += u[i]; u[i] = j; } }

Disjointed Sets Performance

In a nutshell Performance Running time complexity: O(1) for union Using ONE pointer to connect from one root to another Running time of find depends on implementation Union by size: Find is O(log(n)) Union by height: Find is O(log(n)) Union operations obviously take Θ(1) time Code has no loops or recursion Θ(f(n)) is when the worst case and best case are identical

Performance The average running time of any find and union operations in the quick union data structure is so close to a constant that it's hardly worth mentioning that, in an asymptotic sense, it's slightly slower in real life

Performance A sequence of f find and u union operations (in any order and possibly interleaved) takes Theta(u + f α(f + u, u)) time in the worst case α is an extremely slowly growing function Known as the inverse Ackermann function. This function is never larger than 4 for any values of f and u you could ever use (though it can get arbitrarily large for unimaginably large values of f and u). Hence, for all practical purposes think of quick union as having find operations that run, on average, in constant time.

A Union-Find Application A random maze generator can use unionfind. Consider a 5x5 maze: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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).

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

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.

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) } }

Initial State

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

A Different Intermediate State Algorithm selects wall between 18 and 19. What happens?

Final State