1 / 15

CSCE 210 Data Structures and Algorithms

CSCE 210 Data Structures and Algorithms. Prof. Amr Goneid AUC Part 9. Disjoint Sets. Disjoint Sets. What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and Simple Union Disjoint Sets Class Some Applications. What are Disjoint Sets?.

ahanu
Download Presentation

CSCE 210 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. Union-Find Data Structure:Tree Representation • One possible representation is a tree where a set can be identified by a parentnode 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

  5. 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 p[i] Prof. Amr Goneid, AUC

  6. 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 (p[i] >= 0) i = p[i]; return i; } Prof. Amr Goneid, AUC

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

  8. 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) { p [i] = j ; } Prof. Amr Goneid, AUC

  9. Simple Union 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

  10. Simple Union • The parent array would change to: i p[i] Prof. Amr Goneid, AUC

  11. 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

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

  13. 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

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

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

More Related