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.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Lecture 10 Disjoint Set ADT.
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.
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.
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
© 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.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Heaps Heaps are used to efficiently implement two operations:
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
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.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
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.
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.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
CSE 373: Data Structures and Algorithms
Trees Chapter 15.
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
Binary Trees, Binary Search Trees
An application of trees: Union-find problem
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
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
Binary Trees, Binary Search Trees
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find Partition Structures
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.
Running Time Analysis Union is clearly a constant time operation.
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
Binary Trees, Binary Search Trees
CSE 373: Data Structures and Algorithms
Presentation transcript:

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 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 Note down examples from board. Equivalence relations constitute a partition

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?

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

Find(a) returns the number of the root node of the tree containing a. Union operation makes one tree sub-tree of another Root of one tree becomes child of the root of another. Note down example from board.

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 down example from board Note that we don’t use any pointers here.

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

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

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. Note down example from board. 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? Initially, what is the content of array S? Note down example from board

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

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 of a node A increases, then the earlier tree consisting the node 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 2, tree size is at least 4… depth is k, tree size is at least 2 k We know that 2 k <= N Thus k <= 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 Later Find operation on these 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 (say 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 * 2 32 is less than 5! Thus the worst case complexity is linear for all practical purposes.

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