21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.

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 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.
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
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.
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 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.
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.
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.
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
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)
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
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.
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
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
Chapter 9: Graphs Basic Concepts
CSE 373 Data Structures and Algorithms
CSCE 411 Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
CSC 413/513: Intro to Algorithms
CSE 373: Data Structures and Algorithms
Course: Data Structures Lecturer: Uri Zwick March 2008
CSE 373: Data Structures and Algorithms
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
Chapter 9: Graphs Basic Concepts
Review of MST Algorithms Disjoint-Set Union Amortized Analysis
Presentation transcript:

21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

2 Contents Disjoint-sets Disjoint-set operations An application of disjoint-set data structures Disjoint-set data structures

3 Disjoint sets  Two sets A and B are disjoint if A ∩ B = {}. Ex> A = {1, 2}, B = {3, 4}  Sets S 1, S 2, …, S k are disjoint if every two distinct sets S i and S j are disjoint. Ex> S 1 = {1, 2, 3}, S 2 = {4, 8}, S 3 ={5,7}

4 Disjoint sets A collection of disjoint sets  A set of disjoint sets is called a collection of disjoint sets. Ex> {{1, 2, 3}, {4, 8}, {5,7}}  Each set in a collection has a representative member and the set is identified by the member. Ex> {{1, 2, 3}, {4, 8}, {5,7}}

5 Disjoint sets A collection of dynamic disjoint sets  Dynamic: Sets are changing.  New sets are created.  {{1, 2, 3}, {4, 8}, {5,7}}  {{1, 2, 3}, {4, 8}, {5,7}, {9}}  Two sets are united.  {{1, 2, 3}, {4, 8}, {5,7} }  {{1, 2, 3}, {4, 8, 5, 7}}

6 Disjoint-set operations  MAKE-SET(x)  UNION(x, y)  FIND-SET(x)

7 Disjoint-set operations MAKE-SET(x)  Given a member x, generate a set for x.  MAKE-SET(9) {{1, 2, 3}, {4, 8}, {5,7}}  {{1, 2, 3}, {4, 8}, {5,7}, {9} }

8 Disjoint-set operations UNION(x, y)  Given two members x and y, unite the set containing x and another set containing y.  UNION(1,4)  {{1, 2, 3}, {4, 8}, {5,7}}  {{1, 2, 3, 4, 8}, {5,7}} FIND-SET(x)  Find the representative of the set containing x.  FIND-SET(5): 7

9 Disjoint-set data structures Problem  Developing data structures to maintain a collection of dynamic disjoint sets supporting disjoint-set operations, which are MAKE-SET(x), UNION(x,y), FIND-SET(x).

10 Parameters for running time analysis  #Total operations: m  #MAKE-SET ops: n  #UNION ops: u  #FIND-SET ops: f  m = n + u + f Disjoint-set data structures

11 u ≤ n – 1  n is the number of sets are generated by MAKE-SET ops.  Each UNION op reduces the number of sets by 1.  So, after n-1 UNION ops, we have only 1 set and then we cannot do UNION op more. Assumption  The first n operations are MAKE-SET operations. Disjoint-set data structures

12 Contents Disjoint-sets Disjoint-set operations An application of disjoint-set data structures Disjoint-set data structures

13 Computing connected components (CC)  Static graph  Depth-first search: Θ (V+E)  Dynamic graph  Depth-first search is inefficient.  Maintaining a disjoint-set data structure is more efficient. Application

14 Connected component computation ab cd ef g hj i {{a,b,c,d},{e,f,g}, {h,i},{j}}  {{a,b,c,d}, {e,f,g}, {h, i, j}} Depth first search: Θ (V + E) Disjoint-set data structures: UNION(h,j)

15 Computing CC using disjoint set operations CONNECTED-COMPONENTS(G) 1 for each vertex v ∈ V[G] 2 do MAKE-SET(v) 3 for each edge (u, v) ∈ E[G] 4 do if FIND-SET(u) ≠ FIND-SET(v) 5 then UNION(u, v) Connected component computation

16 ab cd ef g hj i Connected component computation Initial sets {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}

17 ab cd ef g hj i Connected component computation (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} (b,c) {a,b,c,d} {e,f,g} {h,i} {j}

18 SAME-COMPONENT(u, v) 1 if FIND-SET(u) = FIND-SET(v) 2 then return TRUE 3 else return FALSE Connected component computation

19 Contents Disjoint-sets Disjoint-set operations An application of disjoint-set data structures Disjoint-set data structures

20 Disjoint-set data structures  Linked-list representation  Forest representation Disjoint-set data structures

21 Linked-list representation  Each set is represented by a linked list.  Members of a disjoint set are objects in a linked list.  The first object in the linked list is the representative.  All objects have pointers to the representative. Linked-list representation

22 Linked-list representation {{b,c,e,h}, {d,f,g}}: Two linked lists are needed. c head tail heb f head gd tail

23 MAKE-SET(x)  Θ (1) FIND-SET(x)  Θ (1) Linked-list representation x head tail

24 Linked-list representation b head ce tail f head d tail b head ce tail fd UNION(x,y): Attaching a linked list to the other

25 UNION(x,y): Attaching a linked list to the other  Θ (m 2 ) time where m 2 is the number of objects in the linked list being attached.  Changing tail pointer & linking two linked lists: Θ (1)  Changing pointers to the representative: Θ (m 2 ) Linked-list representation b head ce tail fd

26 Running time for m (= n + f + u) operations  Simple implementation of union  O(n+f+u 2 ) time  O(m+n 2 ) time  Because u < n  A weighted-union heuristic  O(n+f+ulgu) time  O(m+mlgm) time Linked-list representation

27 Forest representation  Each set is represented by a tree.  Each member points to its parent.  The root of each tree is the rep. Forest representation {{b,c,e,h},{f,d,g}} c h e b f d g

28 MAKE-SET(x) 1 p[x] ← x FIND-SET(x) 1if x = p[x] 2 then return x 3 else FIND-SET(p[x]) 4 return Forest representation

29 Union by rank  Idea: Attach the shorter tree to the higher tree.  Each node maintains a rank,which is an upper bound on the height of the node.  Compare the ranks of the two roots and attach the tree whose root’s rank is smaller to the other. Forest representation

30 c h e b f d g c h e b d g f Forest representation

31 MAKE-SET(x) 1 p[x] ← x 2 rank[x] ← 0 UNION(x, y) 1 LINK(FIND-SET(x), FIND-SET(y)) LINK(x, y) 1 if rank[x] > rank[y] 2 then p[y] ← x 3 else p[x] ← y 4 if rank[x] = rank[y] 5 then rank[y] ← rank[y] + 1 Forest representation

32 Path compression  Change the parent to the root during FIND-SET(x). Forest representation FIND-SET(x) 1 if x ≠ p[x] 2 then p[x] ← FIND-SET(p[x]) 3 return p[x]

33 f e d c b a f e dc b a Forest representation

34 Worst case running time : O(m α(n)) α(n) ≤ 4 :for all practical situations. Forest representation