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.

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.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
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:
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.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Minimum Spanning Trees. a b d f g e c a b d f g e c.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick June 2010
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
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.
Lecture X Disjoint Set Operations
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.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
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.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
MST – KRUSKAL UNIT IV. Disjoint-Set Union Problem Want a data structure to support disjoint sets – Collection of disjoint sets S = {S i }, S i ∩ S j =
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)
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
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
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Disjoint Sets Data Structure
Many slides here are based on D. Luebke slides
Data Structures and Algorithms
Disjoint Sets with Arrays
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
CS200: Algorithm Analysis
CSC 413/513: Intro to Algorithms
CSE 373: Data Structures and Algorithms
Course: Data Structures Lecturer: Uri Zwick March 2008
Union-Find Partition Structures
CS 332: Algorithms Amortized Analysis Continued
Union-Find Partition Structures
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:

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 sets. Remove these sets and add their union. Choose a representative for it. FIND-SET(x) : return the representative of the set containing x. We have a collection of disjoint sets of elements. Each set is identified by a representative element. We want to perform union operations, and tell which set something is in. This is useful in a minimum spanning tree algorithm and many other applications. Formally, we have the following operations.

p3. Example : MAKE-SET(1) MAKE-SET(2) MAKE-SET(3) MAKE-SET(4) FIND(3) (returns 3) FIND(2) (returns 2) UNION(1,2) (representative 1, say) FIND(2) (returns 1) FIND(1) (returns 1) UNION(3,4) (representative 4, say) FIND(4) (returns 4) FIND(3) (returns 4) UNION(1,3) (representative 4, say) FIND(2) (returns 4) FIND(1) (returns 4) FIND(4) (returns 4) FIND(3) (returns 4) {1} {2} {3} {4} {1,2} {3,4} {1,2,3,4}

p4. An application of disjoint-set data structure : Same-Component(u,v) if Find-Set(u) == Find-Set(v) return TRUE else return FALSE Connected-Components(G) for each vertex v ∈ G.V Make-Set(v) for each edge (u,v) ∈ G.E if Find-Set(u) ≠ Find-Set(v) Union( u, v ) {1}, {2} {1,2}

p5. Linked List Implementation with Concatenation : Each cell has an element, a pointer to the next member, and a pointer to the first element in the list, which is the set representative. For union, we append one list to another, changing the pointers to the set representative on the list at the end. MAKE-SET O(1) time FIND-SET O(1) time UNION O(m) time Thus we get quadratic worst case performance. Make-Set(x 1 ),……, Make-Set(x n ) Union(x 1,x 2 ), Union(x 2,x 3 ), Union(x 3,x 4 ) ……, Union(x q-1,x q ) 123 q-1

p6. Disjoint-set union: Make-Set : O(1) Find-Set : O(1) Union(A,B) : Copy elements of A into B, O(A). Use a linked list to represent a set representative

p7. Worst-case analysis : Improvement : Copy smaller set into larger. initially. Union(S 1, S 2 )1 Union(S 2, S 3 )2 Union(S n-1, S n )n-1 A single Union can still take, but n unions take only time.

p8. Refined Linked List Implementation : (Append shorter list onto longer one) MAKE-SET O(1) time FIND-SET O(1) time UNION O(log n) time (rough bound) Proof of the bound for UNION : A set has to double in size each time it is concatenated onto the end. Thus we get O(n log n) worst case performance for n operations.

p9. Amortized analysis : In a set of size n, any element can be copied at most lgn times. Each time copied, it was in smaller set. n elements each copied O(lgn) times  n Unions take O(nlgn) time. Each UNION takes amortized O(lgn).

Forest representation : Here we represent each set as a tree, and the representative is the root. For example, the following forest represents the set {1,2,3}, {4,5}, {6} : Implementation MAKE-SET(x) Create a tree FIND-SET(x) Go to the root UNION(x,y) Add a pointer Thus we would get the following form UNION(1,4) This representation does not improve the running time in the worst case over the linked list representation.

p11. Path compression and ranks : These are refinements of the forest representation which make it significantly faster. FIND-SET: Do path compression UNION: Use ranks “Path compression” means that when we do FIND-SET(X), we make all nodes encountered point directly to the representative element for x. Initially, all elements have rank 0. The ranks of representative elements are updated so that if two sets with representatives of the same rank are unioned, then the new representative is incremented by one.

Example : We show a sequence of operations and what the forest would look like. MAKE-SET(1) … MAKE-SET(6) RANKS : 0 UNION(1,2) UNION(4,5) RANK(2)=1 14 RANK(5)=1 UNION(1,3) RANK(2)=1 14 RANK(5)=1 3 UNION(5,6) 2 1 RANK(5)= RANK(2)=1

UNION(4,3) 2 1 RANK(5)= RANK(2)=2 3 FIND(4) FIND(3) no change FIND(6) (path compression)

p14. Make_Set(x) : Algorithm : Union(x,y) : Algorithm : Link(x,y) : Algorithm : Make_Set(x) { x.p = x x.rank = 0 } Union(x,y) {Link(Find-Set(x),Find-Set(y)) } Link(x,y) { if x.rank > y.rank y.p = x else x.p = y if x.rank == y.rank y.rank++ } xy x y

p15. Find_Set(x) : Algorithm : Find_Set(x) { if x ≠ x.p x.p = Find-Set(x.p) return x.p } f e d c x.p x f edc x

p16. Analysis of MST with disjoint set union: Sort : O(V) : Make-Set’s O(E) : Find-Set’s O(V) : Union’s Above disjoint-set operations together take, using best known algorithm for disjoint-set union. Total : m operations on n sets :, where is a functional inverse” of Ackermann’s function.