Disjoint-Set Operation

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]
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic 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.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Course: Data Structures Lecturer: Uri Zwick March 2008
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.
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.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
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.
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.
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.
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.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Disjoint Sets Data Structure
Disjoint Sets Data Structure (Chap. 21)
Many slides here are based on D. Luebke slides
Data Structures and Algorithms
Disjoint Sets with Arrays
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

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.

Example: MAKE-SET(1) {1} MAKE-SET(2) {2} MAKE-SET(3) {3} {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) {1,2} {3,4} {1,2,3,4}

An application of disjoint-set data structure: Connected-Components(G) { for each vertex do Make-Set(v) for each edge(u,v) do if Find-Set(u) Find-Set(v) then Union(u,v) } 1 2 3 7 8 4 5 6 {1} , {2} {1,2} Uma vez que este procedimento termine, Find-set(u) = Find-set(v) sse u e v estão no mesmo componente conexo.

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, the set representative. representative

Linked List Implementation with Concatenation representative For union, we append one list to another, changing the pointers to the set representative on the list at the end. Cost of operations: MAKE-SET O(1) time FIND-SET O(1) time UNION O(len) time, where len is the size of the list.

Linked List Implementation with Concatenation For union, we append one list to another, changing the pointers to the set representative on the list at the end.

Linked List Implementation with Concatenation MAKE-SET O(1) time FIND-SET O(1) time UNION O(len) time Thus, if we have n objects (n make–set operations) and we perform m operations Union (x1, x2) Union (x2,x3), Union (x3,x4), … Quanto tempo será gasto para reunir todos os objetos?

Worst-case analysis: Improvement ???? initially. Union(S1,S2) 1 Union(Sn-1,Sn) n-1 Improvement ????

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

Refined Linked List Implementation: (Concatenate shorter one into longer list) 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.

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

Forest Representation Here we represent - each set as a tree, and - the representative is the root . For example, the following forest represents the sets {1,2,3}, {4,5}, {6}: 2 5 6 1 3 4 Implementation MAKE-SET(x) Create a tree FIND-SET(x) Go to the root UNION(x,y) Add a pointer

Forest Representation Thus we would get the following form UNION(1,4) 2 5 6 2 6 1 3 4 1 3 5 4 This representation does not improve the running time in the worst case over the linked list representation. (WHY???)

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) 1 2 3 4 5 6 RANKS: 0 UNION(1,2) UNION(4,5) 2 3 5 6 RANK(2)=1 RANK(5)=1 1 4 UNION(1,3) 2 5 6 RANK(2)=1 RANK(5)=1 1 3 4 UNION(5,6) 2 5 RANK(2)=1 RANK(5)=1 1 3 4 6

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

Make_Set(x): Union(x,y): Link(x,y): Algorithm: Make_Set(x) { P(x)  x Rank[x]  0 } Union(x,y) x y { Link(Find-Set(x),Find-Set(y)) } Link(x,y) x y { if rank[x] > rank[y] then P[y]  x else P[x]  y if rank[x]=rank[y] then rank[y]++ }

Find_Set(x): Algorithm: WHAT ABOUT THE RANK???? Find_Set(x) { if x P[x] then P[x]  Find-Set(P[x]) return P[x] } f f x e c d e P[x] d x c WHAT ABOUT THE RANK????

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.