Lecture X Disjoint Set Operations

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
Chapter 23 Minimum Spanning Trees
Discussion #36 Spanning Trees
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
Minimum Spanning Trees (MST)
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structures Week 9 Spanning Trees We will now consider another famous problem in graphs. Recall the bridges problem again. The purpose of the bridge.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
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
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
Homework remarking requests BEFORE submitting a remarking request: a)read and understand our solution set (which is posted on the course web site) b)read.
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,
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
CSC 252a: Algorithms Pallavi Moorthy 252a-av Smith College December 14, 2000.
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Union-find Algorithm Presented by Michael Cassarino.
Union Find ADT Data type for disjoint sets: makeSet(x): Given an element x create a singleton set that contains only this element. Return a locator/handle.
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.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
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 
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:
Union By Rank Ackermann’s Function Graph Algorithms Rajee S Ramanikanthan Kavya Reddy Musani.
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Data Structures for Disjoint Sets
Disjoint Sets Data Structure
Disjoint Sets Data Structure (Chap. 21)
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Data Structures and Algorithms
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
CS200: Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Course: Data Structures Lecturer: Uri Zwick March 2008
CSE 373: Data Structures and Algorithms
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
Review of MST Algorithms Disjoint-Set Union Amortized Analysis
Presentation transcript:

Lecture X Disjoint Set Operations CS473-Algorithms I Lecture X Disjoint Set Operations CS 473 Lecture X

Disjoint Set Operations A disjoint-set data structure Maintains a collection of disjoint dynamic sets Each set is identified by a representative which is some member of the set In some applications, It doesn't matter which member is used as the representative We only care that, if we ask for the representative of a set twice without modifying the set between the requests, we get the same answer both times CS 473 Lecture X

Disjoint Set Operations In other applications, There may be a prescribed rule for choosing the representative E.G. Choosing the smallest member in the set Each element of a set is represented by an object “x” MAKE-SET(x) creates a new set whose only member is x Object x is the representative of the set x is not already a member of any other set UNION(x, y) unites the dynamic sets that contain x & y are assumed to be disjoint prior to the operation The new representative is some member of CS 473 Lecture X

Disjoint Set Operations Usually, the representative of either is chosen as the new representative We destroy sets , removing them from the collection since we require the sets in the collection to be disjoint FIND-SET(x) returns a pointer to the representative of the unique set containing x We will analyze the running times in terms of two parameters n : The number of MAKE-SET operations m : The total number of MAKE-SET, UNION and FIND-SET operations CS 473 Lecture X

Disjoint Set Operations Each union operation reduces the number of sets by one since the sets are disjoint Therefore, only one set remains after n - 1 union operations Thus, the number of union operations is n – 1 Also note that, m n always hold since MAKE-SET operations are included in the total number of operations CS 473 Lecture X

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CONNECTED-COMPONENTS (G) for each vertex v V[G] do MAKE-SET(v) endfor for each edge (u, v) E[G] do if FIND-SET(u) FIND-SET(v) then UNION(u, v) endif endfor end SAME-COMPONENT(u,v) if FIND-SET(u) = FIND-SET(v) then return TRUE else return FALSE endif CS 473 Lecture X

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) a b c d j g f e h i Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} (a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} (a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j} (h, i) {a, c} {b, d} {e, g} {f} {h, i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} (a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j} (h, i) {a, c} {b, d} {e, g} {f} {h, i} {j} (a, b) {a, b, c, d} {e, g} {f} {h, i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} (a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j} (h, i) {a, c} {b, d} {e, g} {f} {h, i} {j} (a, b) {a, b, c, d} {e, g} {f} {h, i} {j} (e, f) {a, b, c, d} {e, f, g} {h, i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

An Application of Disjoint-Set Data Structures Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} (b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j} (e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j} (a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j} (h, i) {a, c} {b, d} {e, g} {f} {h, i} {j} (b, c) {a, b, c, d} {e, f, g} {h, i} {j} (a, b) {a, b, c, d} {e, g} {f} {h, i} {j} (e, f) {a, b, c, d} {e, f, g} {h, i} {j} a b c d j g f e h i An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V,E) CS 473 Lecture X

Linked-List Representation of Disjoint Sets Represent each set by a linked-list The first object in the linked-list serves as its set representative Each object in the linked-list contains A set member A pointer to the object containing the next set member A pointer back to the representative Linked-List Representation of Disjoint Sets MAKE-SET(x) : O(1) Representative pointer x / Next Object Pointer FIND-SET(x) : We return the representative pointer of x CS 473 Lecture X

Linked-List Representation of Disjoint Sets A Simple Implementation of Union : UNION(c, y) APPEND x's list to the end of y 's list The representative of y 's list becomes the new representative UPDATE the representative pointer of each object originally on x's list which takes time linear in the length of x's list Linked-List Representation of Disjoint Sets c 's list / y 's list NIL CS 473 Lecture X

Linked-List Representation of Disjoint Sets c 's list / y 's list NIL A Simple Implementation of Union : UNION(c, y) Linked-List Representation of Disjoint Sets CS 473 Lecture X

Analysis of the Simple Union Implementation A sequence of m operations that requires time Suppose that we have n objects and let m = 2n - 1 Operation Number of Objects Updated Updated Objects (Denoted By ‘P’) MAKE-SET(c1) 1 {c1} P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} 1 MAKE-SET(c1) MAKE-SET(c2) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} 1 . MAKE-SET(c1) MAKE-SET(c2) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} {cn} 1 . MAKE-SET(c1) MAKE-SET(c2) MAKE-SET(cn) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P Analysis of the Simple Union Implementation CS 473 Lecture X

Analysis of the Simple Union Implementation Operation Number of Objects Updated Updated Objects (Denoted By ‘P’) P MAKE-SET(c1) MAKE-SET(c2) . MAKE-SET(cn) UNION(c1, c2) 1 . {c1} {c2} {cn} {c1} {c2} {c1, c2} P P P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} {cn} {c1} {c2} {c1, c2} {c1, c2} {c3} {c1, c2, c3} 1 . 2 MAKE-SET(c1) MAKE-SET(c2) MAKE-SET(cn) UNION(c1, c2) UNION(c2, c3) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} {cn} {c1} {c2} {c1, c2} {c1, c2} {c3} {c1, c2, c3} {c1, c2, c3} {c4} {c1, c2, c3,c4} 1 . 2 3 MAKE-SET(c1) MAKE-SET(c2) MAKE-SET(cn) UNION(c1, c2) UNION(c2, c3) UNION(c3, c4) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} {cn} {c1} {c2} {c1, c2} {c1, c2} {c3} {c1, c2, c3} {c1, c2, c3} {c4} {c1, c2, c3,c4} 1 . 2 3 MAKE-SET(c1) MAKE-SET(c2) MAKE-SET(cn) UNION(c1, c2) UNION(c2, c3) UNION(c3, c4) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation {c1} {c2} {cn} {c1} {c2} {c1, c2} {c1, c2} {c3} {c1, c2, c3} {c1, c2, c3} {c4} {c1, c2, c3,c4} {c1, c2,..,cn-1} {cn} {c1, c2,..,cn-1,cn,} 1 . 2 3 n - 1 MAKE-SET(c1) MAKE-SET(c2) MAKE-SET(cn) UNION(c1, c2) UNION(c2, c3) UNION(c3, c4) UNION(cn-1, cn) Updated Objects (Denoted By ‘P’) Number of Objects Updated Operation P CS 473 Lecture X

Analysis of the Simple Union Implementation The total number of representative pointer updates MAKE-SET operations UNION operations since Thus, on the average, each operation requires Q(m) time That is, the amortized time of an operation is Q(m) CS 473 Lecture X

A Weighted-Union Heuristic The simple implementation is inefficient because We may be appending a longer list to a shorter list during a UNION operation so that we must update the representative pointer of each member of the longer list Weighted Union Heuristic Maintain the length of each list Always append the smaller list to the longer list With ties broken arbitrarily A single UNION can still take (m) time if both sets have (m) members CS 473 Lecture X

Weighted Union Heuristic Theorem: A sequence of m MAKE-SET, UNION & FIND-SET operations, n of which are MAKE-SET operations, takes O(m+nlgn) time Proof: Try to compute an upper bound on the number of representative pointer updates for each object in a set of size n Consider a fixed object x Each time x’s R-PTR was updated, x was a member of the smaller set {x} {v}→ { ,v} 1-st update |Sx| 2 {x, v} {w1, w2} → { , v ,w1,w2} 2-nd update |Sx| 4 {x,v,w1,w2} {z1,z2,z3,z4} → { , v ,w1,w2,z1,z2,z3,z4}; |Sx| 4 3-rd update |S| 8 P c CS 473 Lecture X

Weighted Union Heuristic For any k n, after x’s R-PTR has been updated times the resulting set must have at least k members R-PTR of each object can be updated at most time over all UNION operations Analysis of The Weighted-Union Heuristic The figure below illustrates a worst case sequence for a set with n = 16 objects The total number of R-PTR updates lg n CS 473 Lecture X

Analysis of The Weighted-Union Heuristic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 CS 473 Lecture X

Analysis of The Weighted-Union Heuristic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 , 2 , , 6 , 8 ,10 ,12 CS 473 Lecture X

Analysis of The Weighted-Union Heuristic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 , , 3, 4 , 7, 8 , 11,12 ,15, 16 , 2 , 6 , 8 ,10 ,12 CS 473 Lecture X

Analysis of The Weighted-Union Heuristic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 , , 5, 6, 7, 8 , 13, 14, 15, 16 , 3, 4 , 7, 8 , 11,12 ,15, 16 , 2 , 6 , 8 ,10 ,12 CS 473 Lecture X

Analysis of The Weighted-Union Heuristic 1 2 3 4 5 6 7 8 , , 9, 10, 11, 12, 13, 14, 15, 16 9 10 11 12 13 14 15 16 , 5, 6, 7, 8 , 13, 14, 15, 16 , 3, 4 , 7, 8 , 11,12 ,15, 16 , 2 , 6 , 8 ,10 ,12 CS 473 Lecture X

Analysis of The Weighted-Union Heuristic Each MAKE-SET & FIND-SET operation takes O(1) time, and there are O(m) of them The total time for the entire sequence = O(m + nlgn) Disjoint Set Forests In a faster implementation, we represent sets by rooted trees Each node contains one member Each tree represents one set Each member points only to its parent The root of each tree contains the representative Each root is its own parent CS 473 Lecture X

Disjoint Set Forests UNION(x, y) x1 x2 x3 x4 y1 y2 y3 CS 473 Lecture X

Heuristics To Improve the Running Time Disjoint Set Forests Straightforward Implementation MAKE-SET : Simply creates a tree with just one node : O(1) FIND-SET : Follows parent pointers until the root node is found The nodes visited on this path toward the root constitute the FIND-PATH UNION : Makes the root of one tree to point to the other one Heuristics To Improve the Running Time Straightforward implementation is no faster than ones that use the linked-list representation A sequence of n – 1 UNIONs, following a sequence of n MAKE-SETs, may create a tree, which is just a linear chain of n nodes CS 473 Lecture X

First Heuristic : UNION by Rank Heuristics To Improve the Running Time First Heuristic : UNION by Rank Similar to the weighted-union used for the linked-list representation The idea is to make the root of the tree with fewer nodes point to the root of the tree with more nodes Rather than explicitly keeping the size of the subtree rooted at each node We maintain a rank that approximates the logarithm of the subtree size and is also an upperbound on the height of the node During a UNION operation make the root with smaller rank to point to the root with larger rank CS 473 Lecture X

Heuristics To Improve the Running Time Second Heuristic : Path Compression Use it during the FIND-SET operations Make each node on the FIND-PATH to point directly to the root a b c d e f FIND-SET(b) CS 473 Lecture X

Heuristics To Improve the Running Time Path Compression During FIND-SET(b) Operation a c d e f b CS 473 Lecture X

Pseudocodes For the Heuristics Implementation of UNION-BY-RANK Heuristic p[x] : Pointer to the parent of the node x rank[x] : An upperbound on the height of node x in the tree MAKE-SET(x) UNION(x,y) p[x] ← x LINK(FIND-SET(x),FIND-SET(y)) rank[x] ← 0 end end LINK(x,y) if rank[x] > rank[y] then p[y] ← x else p[x] ← y if rank[x] = rank[y] then rank[y] = rank[y] + 1 endif end CS 473 Lecture X

Implementation of UNION-BY-RANK Heuristic When a singleton set is created by a MAKE-SET the initial rank of the single node in the tree is zero Each FIND-SET operation leaves all ranks unchanged When applying a UNION to two trees, we make the root of tree with higher rank the parent of the root of lower rank Ties are broken arbitrarily CS 473 Lecture X

Implementation of the Path-Compression Heuristic The FIND-SET procedure with Path-Compression Iterative Version FIND-SET(x) y ← x while y p[y] do y ← p[y] endwhile root ← y while x p[x] do parent ← p[x] p[x] ← root x ← parent return root end Recursive Version FIND-SET(x) if x p[x] then p[x] ← FIND-SET(p[x]) endif return p[x] end CS 473 Lecture X