Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.

Slides:



Advertisements
Similar presentations
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Advertisements

Lecture 15. Graph Algorithms
Lecture 10 Disjoint Set ADT.
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.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
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.
CSCE 210 Data Structures and Algorithms
Heaps Heaps are used to efficiently implement two operations:
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
10. Binary Trees A. Introduction: Searching a linked list.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 1. Data Modeling and ADTs.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
General Trees CS 400/600 – Data Structures. General Trees2.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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.
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,
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
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.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Union-find Algorithm Presented by Michael Cassarino.
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.
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.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 13. Abstract Data Types (ADT’s)
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
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 
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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
Analysis & Design of Algorithms (CSCE 321)
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
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCI 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
An application of trees: Union-find problem
Analysis & Design of Algorithms (CSCE 321)
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
ICS 353: Design and Analysis of Algorithms
Analysis & Design of Algorithms (CSCE 321)
CSE 373 Data Structures and Algorithms
ICS 353: Design and Analysis of Algorithms
Union-Find Partition Structures
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
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:

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets

Prof. Amr Goneid, AUC2 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, AUC3 A set S is a collection of elements of the same type We assume that the elements are the numbers 1,2,.. n. In practice, these numbers may be indices to a symbol table containing the names of the sets. Disjoint sets are collections of elements with no common elements between the sets. If S i and S j, i  j are two sets, then S i  S j = Ø Examples: S1 = {1,7,8,9}, S2 = {5,2,10}, S3 = {3,4,6} What are Disjoint Sets?

Prof. Amr Goneid, AUC4  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: S1 = {1,7,8,9}S2 = {5,2,10} S3 = {3,4,6} Union-Find Data StructureTree Representation Union-Find Data Structure:Tree Representation

Prof. Amr Goneid, AUC5 Find(i): Given the element (i), find the set containing (i), i.e. find the root of the tree. Union(i,j): Given two disjoint sets S i and S j, obtain a set containing the elements in both sets, i.e., S i  S j Basic Operations

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

Prof. Amr Goneid, AUC7 Find(i): Finding out which subset we are in is simple, for we keep traversing up the parent pointers until we hit the root. Basic Operations

Prof. Amr Goneid, AUC8  The value in p[i] represents the parent of node (i). For the sets S1,S2,S3 shown before, we have:  Algorithm for Simple find: Find the set containing node (i) = find the parent of (i): int find(int i) { while (p[i] >= 0) i = p[i]; return i; } Simple Find i p[i] find (1)  1 find (4)  3 find (10)  5

Prof. Amr Goneid, AUC9 Union(i,j): Making a union between two subsets is also easy. Just 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. Basic Operations

Prof. Amr Goneid, AUC10  Simple (disjoint) Union: Make set (i) the child of set (j) = union (i,j) Simple Union Example: union(5,1)

Prof. Amr Goneid, AUC11  The parent array would change to:  Algorithm: void union (int i, int j) { p [i] = j ; } Simple Union i p[i]

Prof. Amr Goneid, AUC12 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; } Disjoint sets class

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

Prof. Amr Goneid, AUC14 // 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; } Disjoint sets class

Prof. Amr Goneid, AUC15 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 ( n) steps = O(n 2 ) Hence, the average find cost is O(n) Analysis n n-1 2 1

Prof. Amr Goneid, AUC16 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. Improvement

Prof. Amr Goneid, AUC17 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} } Modified Disjoint sets class

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

Prof. Amr Goneid, AUC19 // 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; } Disjoint sets class

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

Prof. Amr Goneid, AUC21 The CSCI 321 course web site contains full definitions and implementations of : Sets class: Disjoint sets class with dynamic array representation Sets Class

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