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.

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Disjoint Sets Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of union.
Disjoint-Set Operation
Heaps Heaps are used to efficiently implement two operations:
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Course: Data Structures Lecturer: Uri Zwick March 2008
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
1 CSCI 2400 section 3 Models of Computation Instructor: Costas Busch.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of.
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. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
Union & Find Problem 황승원 Fall 2010 CSE, POSTECH 2 2 Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
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.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
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.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
0 Union-Find data structure. 1 Disjoint set ADT (also Dynamic Equivalence) The universe consists of n elements, named 1, 2, …, n n The ADT is a collection.
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 
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
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:
General Trees A tree T is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T-{r})
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
1 Mathematical Preliminaries. 2 Sets Functions Relations Graphs Proof Techniques.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
Parent Pointer Implementation
Disjoint Sets Data Structure
Data Structures: Disjoint Sets
An application of trees: Union-find problem
Course Outline Introduction and Algorithm Analysis (Ch. 2)
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Course: Data Structures Lecturer: Uri Zwick March 2008
Union-Find Partition Structures
Union-Find Partition Structures
CMSC 341 Disjoint Sets.
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Given a set {1, 2, …, n} of n elements.
CMSC 341 Disjoint Sets.
Running Time Analysis Union is clearly a constant time operation.
Priority Queues Supports the following operations. Insert element x.
An application of trees: Union-find problem
Lecture 21 Amortized Analysis
Disjoint Sets Textbook Chapter 8
General Trees A general tree T is a finite set of one or more nodes such that there is one designated node r, called the root of T, and the remaining nodes.
Presentation transcript:

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

3 Union-find We assume there are n fixed elements We start with n sets each containing a single element Each element has a pointer to its representation in the set containing it

4 S1 = {e1} S2 = {e2} S3={e3} S4={e4} …… S1 = {e1} A= {e2,e3} S4={e4} …… A = Union(S3,S4) Find(e2)  A B = Union(A,S7) S1 = {e1} B= {e2,e3,e7} S4={e4} ……

5 Why ? Suppose we want to maintain an equivalence relation: y z t x b v s a y ≡ z

6 y, z t x b v s a y ≡ s

7 y, z, s t x b v a y ≡ s

8 Queries Equivalent?(y,a)

9 Can solve this with union-find Each equivalence class is a set y ≡ s  union(find(y),find(s)) Equivalent?(y,a)  return yes if find(y) = find(a)

10 e2 Representation Represent each set by a tree, each node represents an item, the root also represents the set e3e7 B

11 Concretely e3 e7 e2 B

12 Find(e10) e6 e9 e11 e8e10 C

13 Find(e10) e6 e9 e11 e8e10 Find(x) while (x.parent ≠null) do x ← x.parent return (x) C

14 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

15 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

16 D=Union(B,C) B e2 e3 e7 D e6 e9 e11 e8e10

17 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

18 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

19 D=Union(B,C) D e2 e3 e7 C e6 e9 e11 e8e10

20 Link by size For the find’s sake its better to hang the smaller on the larger

21 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e If (C.size > B.size) then B.Parent ← C; C.size = C.size + B.size return (C) Else C.parent ← B; B.size = B.size + C.size return (B)

22 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e If (C.size > B.size) then B.Parent ← C; C.size = C.size + B.size return (C) Else C.parent ← B; B.size = B.size + C.size return (B) D

23 Analysis Union: O(1) time Find: O(log(n)) time The depth of a tree is at most log(n)

24 Proof By induction on the sequence of operations: For every x Depth(x) ≤ log |T(x)| x

25 Path compression