1 / 23

WEEK 6 The Disjoint Set Class II

WEEK 6 The Disjoint Set Class II. CE222 – Data Structures & Algorithms II Chapter 8.6, 8.7 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006). OUTLINE. Worst Case Analysis An Application : MAZE GENERATION.

jimbo
Download Presentation

WEEK 6 The Disjoint Set Class II

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. WEEK 6The Disjoint Set Class II CE222 – Data Structures & Algorithms II Chapter 8.6, 8.7 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006)

  2. OUTLINE • Worst Case Analysis • An Application : MAZE GENERATION CE 222-Data Structures & Algorithms II, Izmir University of Economics

  3. Worst Case for Union by Rank and Path Compression void unionSets(root1, root2) { if(s[root2]<s[root1])s[root1]=root2; else {if(s[root1]==s[root2])s[root1]--; s[root2]=root1;}} • int Find(int x) • { if (parent[x] < 0)return x; • else return parent[x] = Find(parent[x]); } • // Note that s and parent arrays are the same • // without path compression  O(MlogN) • Worst case  Ɵ(M (M,N) provided that M>=N, (M,N) is the • functional inverse of Ackermann’s function • Ɵ(M (M,N) is almost linear!! CE 222-Data Structures & Algorithms II, Izmir University of Economics

  4. Worst Case for Union by Rank and Path Compression Ackermann Function A(1,j) = 2j for j≥1 A(i,1) = A(i-1,2) for i≥2 A(i,j) = A(i-1,A(i,j-1)) for i,j≥2 from this inverse of Ackermann’s function is (M,N)= min{i ≥1 | A(i, └M/N┘) > logN} • (M,N)≤4 • (M,N) is sometimes written as log*N, but it actually grows slower than log*N  Ɵ(M (M,N) ) ≈ Ɵ(Mlog *N ) • Example : log*65536 = 4 => loglogloglog .. 65536 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  5. MAZE GENERATION • We will use disjoint sets to generate a puzzle maze !! • A puzzle maze has a choice of paths, some of them dead-ends or leading you round in a circle !! • One unique solution!! CE 222-Data Structures & Algorithms II, Izmir University of Economics

  6. MAZE GENERATION What we will do is the following: • Use a grid of RxC squares • Start with the entire grid subdivided into squares • Represent each square as a separate disjoint set • Repeat the following algorithm: • randomly choose a wall between adjacent cells • if that wall connects two disjoint set of cells, then remove the wall and union the two sets till all the squares are in one set CE 222-Data Structures & Algorithms II, Izmir University of Economics

  7. MAZE GENERATION • Use a grid of RxC squares • Start with the entire grid subdivided into squares Example : 4x5 squares 4x5 disjoint sets with initial values : {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  8. MAZE GENERATION • Randomly choose a wall • If that wall connects two disjoint set of cells, then remove the wall and call union function • Assume that we choose the wall between 6 and 7 Find(6)!=Find(7)  disjoint sets What happens if 6 and 7 are not adjacent cells?? CE 222-Data Structures & Algorithms II, Izmir University of Economics

  9. MAZE GENERATION: How to find adjacent cells ?? • Number of columns =c • Number of rows = r • Exceptions if (i<c)  no cell above the cell i if (i>=(r*c-c))  no cell below the cell i if(i%c==0)  no left cell if ((i+1)%c==0)  no right cell CE 222-Data Structures & Algorithms II, Izmir University of Economics

  10. MAZE GENERATION We choose the wall between 6 and 7  6 and 7 are adjacent cells  Find(6)!=Find(7)  6 and 7 are in disjoint sets • Knock the wall =Union the sets {0} {1} {2} {3} {4} {5} {6 7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  11. MAZE GENERATION Before: {0} {1} {2} {3} {4} {5} {6 7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} We choose the wall between 12 and 13 12 and 13 are adjacent cells  Find(12)!=Find(13)  12 and 13 are in disjoint sets • Knock the wall =Union the sets After : {0} {1} {2} {3} {4} {5} {6 7} {8} {9} {10} {11} {12 13} {14} {15} {16} {17} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  12. MAZE GENERATION We choose the wall between 1 and 7 1 and 7 are not adjacent cells Try again !! Before : {0} {1} {2} {3} {4} {5} {6 7} {8} {9} {10} {11} {12 13} {14} {15} {16} {17} {18} {19} We choose the wall between 1 and 6 1 and 6 are adjacent cells  Find(1)!=Find(6)  1 and 6 are in disjoint sets • Knock the wall =Union the sets After: {0} {2} {3} {4} {5} {6 7 1} {8} {9} {10} {11} {12 13} {14} {15} {16} {17} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  13. MAZE GENERATION We choose the wall between 0 and 1 1 and 0 are adjacent cells  Find(0)!=Find(1)  0 and 1 are in disjoint sets • Knock the wall =Union the sets After : {2} {3} {4} {5} {6 7 1 0} {8} {9} {10} {11} {12 13} {14} {15} {16} {17} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  14. MAZE GENERATION We choose the wall between 12 and 17 12 and 17 are adjacent cells  Find(12)!=Find(17)  12 and 17 are in disjoint sets • Knock the wall =Union the sets After: {2} {3} {4} {5} {6 7 1 0} {8} {9} {10} {11} {12 13 17} {14} {15} {16} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  15. MAZE GENERATION We choose the walls between 10-15 and 5-10 respectively After Union (10,15)  {2} {3} {4} {5} {6 7 1 0} {8} {9} {10 15} {11} {12 13 17} {14} {16} {18} {19} After Union (5,10)  {2} {3} {4} {6 7 1 0} {8} {9} {10 15 5} {11} {12 13 17} {14} {16} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  16. MAZE GENERATION Before :{2} {3} {4} {6 7 1 0} {8} {9} {10 15 5} {11} {12 13 17} {14} {16} {18} {19} after Union(3,4) {2} {3 4} {6 7 1 0} {8} {9} {10 15 5} {11} {12 13 17} {14} {16} {18} {19} after Union(13,8) {2} {3 4} {6 7 1 0} {9} {10 15 5} {11} {12 13 17 8} {14} {16} {18} {19} after Union(8,9) {2} {3 4} {6 7 1 0} {10 15 5} {11} {12 13 17 8 9} {14} {16} {18} {19} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  17. MAZE GENERATION Before : {2} {3 4} {6 7 1 0} {10 15 5} {11} {12 13 17 8 9} {14} {16} {18} {19} after Union(14,19) {2} {3 4} {6 7 1 0} {10 15 5} {11} {12 13 17 8 9} {14 19} {16} {18} after Union(2,3) {3 4 2} {6 7 1 0} {10 15 5} {11} {12 13 17 8 9} {14 19} {16} {18} after Union(6,11) {3 4 2} {6 7 1 0 11} {10 15 5} {12 13 17 8 9} {14 19} {16} {18} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  18. MAZE GENERATION Before :{3 4 2} {6 7 1 0 11} {10 15 5} {12 13 17 8 9} {14 19} {16} {18} after Union(10,11) {3 4 2} {6 7 1 0 11 10 15 5} {12 13 17 8 9} {14 19} {16} {18} after Union(18,19) {3 4 2} {6 7 1 0 11 10 15 5} {12 13 17 8 9} {14 19 18} {16} after Union(16,17) {3 4 2} {6 7 1 0 11 10 15 5} {12 13 17 8 9 16} {14 19 18} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  19. MAZE GENERATION Before : {3 4 2} {6 7 1 0 11 10 15 5} {12 13 17 8 9 16} {14 19 18} after Union(8,3) {6 7 1 0 11 10 15 5} {12 13 17 8 9 16 3 4 2} {14 19 18} after Union(9,14) {6 7 1 0 11 10 15 5} {12 13 17 8 9 16 3 4 2 14 19 18} after Union(16,15) {12 13 17 8 9 16 3 4 2 14 19 18 6 7 1 0 11 10 15 5} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  20. MAZE GENERATION Finally maze is genarated !! Running time complexity? O(rc (4rc,rc) ) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  21. A Real Example CE 222-Data Structures & Algorithms II, Izmir University of Economics

  22. Maze Generation : Quick (slowest!) Implementation Initialize( int r, int c) { maze = new int [r*c]; for (int e= 0; e< r*c; e++) maze[e] = e;} void UnionSets( int i, int j ) { if(isadjacent(i,j)==TRUE) { rooti=find(i); rootj=find(j); // “union by size” is used in the given example // but it is ignored in the implementation for (int k=0; k<r*c; k++) if (maze[k] == rootj) maze[k] = rooti; } } int find( int i ) { return maze[i]; } CE 222-Data Structures & Algorithms II, Izmir University of Economics

  23. Homework Assignments • 8.1, 8.2, 8.4, 8.6 • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics

More Related