CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.

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

Some Graph Algorithms.
Greed is good. (Some of the time)
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
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.
1 Spanning Trees Lecture 20 CS2110 – Spring
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.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
CSE 326: Data Structures Hashing Ben Lerner Summer 2007.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
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.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
COMP 261 Lecture 12 Disjoint Sets. Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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,
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
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.
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
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
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 
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
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:
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Chapter 8: Relations. 8.1 Relations and Their Properties Binary relations: Let A and B be any two sets. A binary relation R from A to B, written R : A.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE373: Data Structures & Algorithms Lecture 10: Amortized Analysis and Disjoint Sets Aaron Bauer Winter 2014.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
CSE373: Data Structures & Algorithms Lecture Supplement: Amortized Analysis Hunter Zahn Summer 2016 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms
CSE 373: Data Structures & Algorithms Disjoint Sets & Union-Find
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE 373: Data Structures & Algorithms Disjoint Sets & Union-Find
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.
Spanning Trees.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets & Union-Find Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
The UNION-FIND Abstract Data Type
Equivalence Relations
CSE373: Data Structures & Algorithms Implementing Union-Find
Minimum Spanning Trees
Disjoint Sets Textbook Chapter 8
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015

Announcements TA Sessions on Tuesday and Thursday to prep for midterm Midterm Friday on everything up to and through lecture 8 (including Floyd’s method that we covered today) Start homework 3 soon….. –Implement a priority queue (using given interface) as a Binary Heap (array as underlying d.s.) Three-Heap (array as underlying d.s.) Some other way (array, linked list, tree). Doesn’t have to be as fast as other implementations Don’t use Java methods (for copying, etc…) Look at comments on HW1, will be a little more harsh on HW3 (esp. if it does not compile!) 2

Where we are Last lecture: Priority queues and binary heaps Today: Disjoint sets The union-find ADT for disjoint sets Next lecture: Basic implementation of the union-find ADT with “up trees” Optimizations that make the implementation much faster 3

Disjoint sets A set is a collection of elements (no-repeats) Two sets are said to be disjoint if they have no element in common. S 1  S 2 =  For example, {1, 2, 3} and {4, 5, 6} are disjoint sets. For example, {x, y, z} and {t, u, x} are not disjoint. 4

Partitions A partition P of a set S is a set of sets {S1,S2,…,Sn} such that every element of S is in exactly one Si Put another way: –S 1  S 2 ...  S k = S –i  j implies S i  S j =  (sets are disjoint with each other) Example: –Let S be {a,b,c,d,e} –One partition: {a}, {d,e}, {b,c} –Another partition: {a,b,c}, {d}, {e} –A third: {a,b,c,d,e} –Not a partition: {a,b,d}, {c,d,e} …. element d appears twice –Not a partition: {a,b}, {e,c} …. missing element d 5

Binary relations A binary relation R is defined on a set S if for every pair of elements (x,y) in the set, R(x,y) is either true or false. If R(x,y) is true, we say x is related to y. –i.e. a collection of ordered pairs of elements of S –(Unary, ternary, quaternary, … relations defined similarly) Examples for S = people-in-this-room –Sitting-next-to-each-other relation –First-sitting-right-of-second relation –Went-to-same-high-school relation 6

Properties of binary relations A relation R over set S is: –reflexive, if R(a,a) holds for all a in S e.g. The relation “,,,,, } It is reflexive because,, are in this relation. –symmetric if and only if for any a and b in S, whenever is in R, is in R. e.g. The relation “=“ on the set of integers {1, 2, 3} is {, } and it is symmetric. –transitive if R(a,b) and R(b,c) then R(a,c) for all a,b,c in S e.g. The relation “ and in “ is also in “ <= “ (and similarly for the others) 7

Equivalence relations A binary relation R is an equivalence relation if R is reflexive, symmetric, and transitive Examples –Same gender –Electrical connectivity, where connections are metal wires –"Has the same birthday as" on the set of all people –…–… 8

Punch-line Equivalence relations give rise to partitions. Every partition induces an equivalence relation Every equivalence relation induces a partition Suppose P={S1,S2,…,Sn} is a partition –Define R(x,y) to mean x and y are in the same Si R is an equivalence relation Suppose R is an equivalence relation over S –Consider a set of sets S1,S2,…,Sn where (1) x and y are in the same Si if and only if R(x,y) (2) Every x is in some Si This set of sets is a partition 9

Example Let S be {a,b,c,d,e} One partition: {a,b,c}, {d}, {e} The corresponding equivalence relation: (a,a), (b,b), (c,c), (a,b), (b,a), (a,c), (c,a), (b,c), (c,b), (d,d), (e,e) 10

Example Let S be {a, b, c, d, e} The equivalence relation: (a,a),(a,b),(b,a), (b,b), (c,c), (d,d), (e,e) The corresponding partition? {a,b},{c},{d},{e} 11

The Union-Find ADT The union-find ADT (or "Disjoint Sets" or "Dynamic Equivalence Relation") keeps track of a set of elements partitioned into a number of disjoint subsets. Many uses! –Road/network/graph connectivity (will see this again) keep track of “connected components” e.g., in social network –Partition an image by connected-pixels-of-similar-color Not as common as dictionaries, queues, and stacks, but valuable because implementations are very fast, so when applicable can provide big improvements 12

Union-Find Operations Given an unchanging set S, create an initial partition of a set –Typically each item in its own subset: {a}, {b}, {c}, … –Give each subset a “name” by choosing a representative element Operation find takes an element of S and returns the representative element of the subset it is in Operation union takes two subsets and (permanently) makes one larger subset –A different partition with one fewer set –Affects result of subsequent find operations –Choice of representative element up to implementation 13

Example Let S = {1,2,3,4,5,6,7,8,9} Let initial partition be (will highlight representative elements red) {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9} union (2,5): {1}, {2, 5}, {3}, {4}, {6}, {7}, {8}, {9} find (4) = 4, find (2) = 2, find (5) = 2 union (4,6), union (2,7) {1}, {2, 5, 7}, {3}, {4, 6}, {8}, {9} find (4) = 6, find (2) = 2, find (5) = 2 union (2,6) {1}, {2, 4, 5, 6, 7}, {3}, {8}, {9} 14

No other operations All that can “happen” is sets get unioned –No “un-union” or “create new set” or … As always: trade-offs – Implementations will exploit this small ADT Surprisingly useful ADT –But not as common as dictionaries or priority queues 15

Example application: maze-building Build a random maze by erasing edges –Possible to get from anywhere to anywhere Including “start” to “finish” –No loops possible without backtracking After a “bad turn” have to “undo” 16

Maze building Pick start edge and end edge 17 Start End

Repeatedly pick random edges to delete One approach: just keep deleting random edges until you can get from start to finish 18 Start End

Problems with this approach 1.How can you tell when there is a path from start to finish? –We do not really have an algorithm yet 2.We could have cycles, which a “good” maze avoids –Want one solution and no cycles 19 Start End

Revised approach Consider edges in random order (i.e. pick an edge) Only delete an edge if it introduces no cycles (how? TBD) When done, we will have a way to get from any place to any other place (including from start to end points) 20 Start End

Cells and edges Let’s number each cell –36 total for 6 x 6 An (internal) edge (x,y) is the line between cells x and y –60 total for 6x6: (1,2), (2,3), …, (1,7), (2,8), … 21

The trick Partition the cells into disjoint sets –Two cells in same set if they are “connected” –Initially every cell is in its own subset If removing an edge would connect two different subsets: –then remove the edge and union the subsets –else leave the edge because removing it makes a cycle 22 Start End

The algorithm P = disjoint sets of connected cells initially each cell in its own 1-element set E = set of edges not yet processed, initially all (internal) edges M = set of edges kept in maze (initially empty) while P has more than one set { –Pick a random edge (x,y) to remove from E –u = find (x) –v = find (y) –if u==v add (x,y) to M // same subset, leave edge in maze, do not create cycle else union (u,v) // connect subsets, remove edge from maze } Add remaining members of E to M, then output M as the maze 23

Example 24 Pick edge (8,14) Find(8) = 7 Find(14) = 20 Union(7,20) P {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21} {18} {25} {28} {31} {22,23,24,29,30,32 33,34,35,36} Start End

Example 25 P {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21} {18} {25} {28} {31} {22,23,24,29,30,32,33,34,35,36} Find(8) = 7 Find(14) = 20 Union(7,20) P {1,2,7,8,9,13,19,14,20,26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21} {18} {25} {28} {31} {22,23,24,29,30,32,33,34,35,36}

Example: Add edge to M step 26 P {1,2,7,8,9,13,19,14,20,26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21} {18} {25} {28} {31} {22,23,24,29,30,32 33,34,35,36} Start End Pick edge (19,20) Find (19) = 7 Find (20) = 7 Add (19,20) to M

At the end of while loop Stop when P has one set (i.e. all cells connected) Suppose green edges are already in M and black edges were not yet picked –Add all black edges to M 27 Start End P {1,2,3,4,5,6,7,… 36} Done!

Data structure for the union-find ADT Start with an initial partition of n subsets –Often 1-element sets, e.g., {1}, {2}, {3}, …, {n} May have any number of find operations May have up to n-1 union operations in any order –After n-1 union operations, every find returns same 1 set 28

Teaser: the up-tree data structure Tree structure with: –No limit on branching factor –References from children to parent Start with forest of 1-node trees Possible forest after several unions: –Will use roots for set names