CSCE 210 Data Structures and Algorithms

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.
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.
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.
© 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.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i):
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.
Fundamental Data Structures and Algorithms Peter Lee April 24, 2003 Union-Find.
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.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCI 210 Data Structures and Algorithms
CMSC 341 Lecture 13 Leftist Heaps
CSCE 210 Data Structures and Algorithms
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
An application of trees: Union-find problem
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Analysis & Design of Algorithms (CSCE 321)
Course Outline Introduction and Algorithm Analysis (Ch. 2)
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
CSE 332: Data Structures Disjoint Set Union/Find
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSCE 411 Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
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 Given a set {1, 2, …, n} of n elements.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
CSE 373: Data Structures and Algorithms
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.
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets Prof. Amr Goneid, AUC

Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and Simple Union Disjoint Sets Class Some Applications Prof. Amr Goneid, AUC

What are Disjoint Sets? A set S is a collection of elements of the same type A disjoint-set data structure keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. Disjoint sets: If Si and Sj, i  j are two sets, then Si  Sj = Ø Examples: S1 = {1,7,8,9}, S2 = {5,2,10}, S3 = {3,4,6} Prof. Amr Goneid, AUC

Union-Find Data Structure: Tree Representation One possible representation is a tree where a set can be identified by a parent node and children nodes. In this representation, children point to their parents, rather than the reverse. The representative of the subset is the ancestor of all elements in that subset (e.g. 1 , 5 , 3) S1 = {1,7,8,9} S2 = {5,2,10} S3 = {3,4,6} 5 3 1 4 6 7 8 9 2 10 Prof. Amr Goneid, AUC

Parent Array Representation n disjoint nodes can be represented as n disjoint sets where each node is its own parent, i.e. p[i] = -1: n 1 2 3 i 1 2 3 n -1 p[i] Prof. Amr Goneid, AUC

Basic Operations Find(i): Given the element (i), find the set containing (i), i.e. find the representative of (i) = the root of the tree. Algorithm for Simple find: Keep traversing up the parent pointers until we hit the root. int find(int i) { while (pi >= 0) i = pi return i; } Prof. Amr Goneid, AUC

Simple Find The value in p[i] represents the parent of node (i). For the sets S1,S2,S3 shown before, we have: i 1 2 3 4 5 6 7 8 9 10 -1 p[i] find (1)  1 find (4)  3 find (10) 5 Prof. Amr Goneid, AUC

Basic Operations Union(i,j): Given two disjoint sets Si and Sj, obtain a set containing the elements in both sets, i.e., Si  Sj Algorithm for Simple union: Make the root of one of two trees point to the other, so now all elements have the same root and thus the same subset name. void union (int i, int j) { pi = j ; }   Prof. Amr Goneid, AUC

Simple Union 1 1 Example: union(5,1) 7 8 9 5 7 8 9 5 2 10 2 10 Make set (i) the child of set (j) = union (i,j) 1 1 Example: union(5,1) 7 8 9 5 7 8 9 5 2 10 2 10 Prof. Amr Goneid, AUC

Simple Union p[i] The parent array would change to: i 1 2 3 4 5 6 7 8 9 10 -1 p[i] Prof. Amr Goneid, AUC

Disjoint sets class class Sets { private: int *p, n; public: Sets(int Size): n(Size) // Constructor p = new int[n+1]; for (int i=0; i<=n; i++) p[i] = -1; } Prof. Amr Goneid, AUC

Disjoint sets class ~Sets() // Destructor { delete [ ] p; } void SimpleUnion(int i, int j); int SimpleFind(int i); }; Prof. Amr Goneid, AUC

Disjoint sets class // Make set(i) the child of set(j) void Sets::SimpleUnion(int i, int j) { p[i] = j; } // Find the parent set of subset(i) int Sets::SimpleFind(int i) { while (p[i]>=0) i = p[i]; return i; } Prof. Amr Goneid, AUC

Analysis Union: takes constant time, i.e., O(1) Find: Suppose we do the following sequence of unions: union(1,2) , union(2,3),...., union(n-1,n) The time of find parent of node (i) will be i. To process all finds, we need (1+2+....+n) steps = O(n2) Hence, the average find cost is O(n) n n-1 2 1 Prof. Amr Goneid, AUC

Improvement We can improve the find cost by modifying the union algorithm to be: “make the root of the smaller tree point to the root of the bigger tree” In this case, the cost of find is O(log n) Implementation: Store the node count in each root. Modify the count when making a union. Prof. Amr Goneid, AUC

Modified Disjoint sets class class Sets { private: int *p, *c, n; public: Sets(int Size): n(Size) // Constructor p = new int[n+1]; c = new int[n+1]; for (int i=0; i<=n; i++) {p[i] = -1; c[i] = 1} } Prof. Amr Goneid, AUC

Disjoint sets class ~Sets() // Destructor { delete [ ] p; delete [ ] c;} void SimpleUnion(int i, int j); int SimpleFind(int i); }; Prof. Amr Goneid, AUC

Disjoint sets class // Make a union between set(i) and set(j) void Sets::SimpleUnion(int i, int j) { int sum = c[i] + c[j]; if (c[i] > c[j]) {p[j] = i; c[i] = sum; } else { p[i] = j; c[j] = sum;} } // Find the parent set of subset(i) int Sets::SimpleFind(int i) { while (p[i]>=0) i = p[i]; return i; Prof. Amr Goneid, AUC

Some Applications Representation of disjoint collections of data Representation of Trees, Forests and Graphs Prof. Amr Goneid, AUC

Learn on your own about: The C++ STL set container Prof. Amr Goneid, AUC