Lecture 10 Disjoint Set ADT.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
COL 106 Shweta Agrawal and Amit Kumar
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.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heaps Heaps are used to efficiently implement two operations:
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
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.
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.
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 
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
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})
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.
CS 367 Introduction to Data Structures Lecture 8.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373: Data Structures and Algorithms
Trees Chapter 15.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Sorting And Searching CSE116A,B 2/23/2019 B.Ramamurthy.
Union-Find Partition Structures
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Union-Find Partition Structures
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.
Running Time Analysis Union is clearly a constant time operation.
Dynamic Equivalence Problem
Binary Trees, Binary Search Trees
CSE 373: Data Structures and Algorithms
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Lecture 10 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 are sets Union of two sets A and B is a set C which consists of all elements in A and B Two sets are mutually disjoint if they do not have a common element.

A partition of a set is a collection of subsets such that Union of all these subsets is the set itself Any two subsets are mutually disjoint S = {1,2,3,4}, A = {1,2}, B = {3,4}, C = {2,3,4}, D = {4} Is A, B a partition of S? Yes Is A, C partition of S? No Is A, D partition of S? No

Union and Find Operations Operations on partitions. Union Need to form union of two different sets of a partition Find Need to find out which set an element belongs to

Every set in the partition has a number. The numbers can be anything as long as different sets have distinct numbers. Find(a) returns the number of the set containing a. Can two different sets contain the same element? No, the sets in a partition are disjoint

Disjoint Set Data Structure Every element has a number. Elements of a set are stored in a tree (not necessarily binary) The set is represented by the root of the tree. The number assigned to a set is the number of the root element.

B = {3, 4} 3 4 B is assigned number 3 Are the numbers distinct for different sets? No two sets have the same root as they are disjoint, thus they have distinct numbers

Find(a) returns the number of the root node of the tree containing a. Find(4) returns? 3 3 4 Find(3) returns? 3 Union operation makes one tree sub-tree of another Root of one tree becomes child of the root of another.

B = {3, 4} A = {1,2} 3 4 1 2 Want to do A union B 1 2 3 4 We have:

Tree Representation Will use an array based representation, array S Let the elements be 1,2,….N S[j] contains the number for the parent of j S[j] = 0 if j is the root. Initially all trees are singletons Trees build up with unions. Note that we don’t use any pointers here.

B = {3, 4} A = {1,2} 3 4 1 2 1 3 S Want to do A union B 1 2 3 4 We have: S 1 1 3

Pseudo Code for Find Find(a) { If S[a] = 0, return a; else Find(S[a]); } O(N) Complexity?

Pseudo-Code for Union Union(root 1, root 2) { S[root2] = root1; } Complexity? O(1)

More Efficient Union Will improve the worst case find complexity to log N When we do a union operation make the smaller tree a subtree of the bigger one Thus the root of the smaller subtree becomes a child of the root of the bigger one. A = {1,2,3} B = {4} Root of B is root of A after union, B is subtree of A Alternatively, union operation can be done by height as well Tree of lesser height is made subtree of the other. We consider only size here.

Array storage changes somewhat. If j is a root, S[j] = - size of tree rooted at j If j is not a root, S[j] = parent of j Why is S[j] not equal to the size of tree j, if j is a root? Size of tree j is an integer, if S[j]=size of tree j and j is root, then it would look like root of j is another element, thus j is not root Initially, what is the content of array S? All elements are -1

Pseudo-Code for Union Union(root 1, root 2) { If S[root2] < S[root1], S[root1] = root2; else S[root2]=root1; } Complexity? O(1)

Pseudo Code for Find Find(a) { If S[a] < 0, return a; else Find(S[a]); return; }

Complexity Analysis for Find Operation If the depth (distance from root) of a node A increases, then the earlier tree consisting the node A becomes a subtree of another. Since only a smaller tree becomes a subtree of another, total size of the combined tree must be at least twice the previous one consisting A. Each time depth of a node increases, the size of the tree increases by at least a factor of 2. At first every node has depth 0

Next time depth is 1, tree size is at least 2, depth is k, tree size is at least 2k We know that 2k <= N Thus k <= log N Depth of any tree is at most log N Complexity of Find operation is O(log N) Complexity of any M operations is O(MlogN)

Path Compression Makes all operations almost linear in the worst case. Whenever you do Find(j) make S[k]=Find(j) for all elements on the path of j to the root, except the root. All nodes on the path of j now point to the root directly

1 2 3 4 5 Do Find(5) Find(5) encounters 5, 4 and 3 before reaching root 1 After Find(5): 1 2 3 4 5

Later Find operations will have lower costs as their depths have been reduced. Any Find operation reduces the cost of future ones.

Pseudo Code for New Find Find(a) { If S[a] < 0, return a; else S[a]=Find(S[a]); return; }

Complexity Analysis Any M operations take O(Mlog*N) if M is (N) log*N is the number of times we take loglog….logN so as to get a number less than or equal to 1 (log base 2, even otherwise asymptotic order remains the same). log*N grows very slowly with N and is less than 4 or 5 for all practical values of N, log*232 is less than 5 Thus the worst case complexity is linear for all practical purposes.

Reading Assignment Chapter 8, till section 8.61. (i.e. section 8.6.1 onwards can be omitted).