1 / 42

CSE 326: Data Structures: Set ADT

CSE 326: Data Structures: Set ADT. Lecture 18: Friday, Feb 21, 2003. Today’s Outline. Making a “good” maze Disjoint Set Union/Find ADT Up-trees Weighted Unions Path Compression. What’s a Good Maze?. What’s a Good Maze?. Connected Just one path between any two rooms Random.

tracymorris
Télécharger la présentation

CSE 326: Data Structures: Set ADT

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. CSE 326: Data Structures: Set ADT Lecture 18: Friday, Feb 21, 2003

  2. Today’s Outline • Making a “good” maze • Disjoint Set Union/Find ADT • Up-trees • Weighted Unions • Path Compression

  3. What’s a Good Maze?

  4. What’s a Good Maze? • Connected • Just one path between any two rooms • Random

  5. The Maze Construction Problem • Given: • collection of rooms: V • connections between rooms (initially all closed): E • Construct a maze: • collection of rooms: V = V • designated rooms in, iV, and out, oV • collection of connections to knock down: E  E such that one unique path connects every two rooms

  6. The Middle of the Maze • So far, a number of walls have been knocked down while others remain. • Now, we consider the wall between A and B. • Should we knock it down?When should we not knock it? A B

  7. Maze Construction Algorithm While edges remain in E • Remove a random edge e = (u, v) from E How can we do this efficiently? • If u and v have not yet been connected • add e to E • mark u and v as connected How to check connectedness efficiently?

  8. Equivalence Relations An equivalence relation R must have three properties • reflexive: • symmetric: • transitive: Connection between rooms is an equivalence relation • Why?

  9. Equivalence Relations An equivalence relation R must have three properties • reflexive: for any x, xRx is true • symmetric: for any x and y, xRy implies yRx • transitive: for any x, y,and z, xRy and yRz implies xRz Connection between rooms is an equivalence relation • any room is connected to itself • if room a is connected to room b, then room b is connected to room a • if room a is connected to room b and room b is connected to room c, then room a is connected to room c

  10. find(4) {1,4,8} {6} 8 {7} {2,3,6} {5,9,10} {2,3} union(3,6) Disjoint Set Union/Find ADT name of set • Union/Find operations create(v) destroy(s) union(s1, s2) find(v)

  11. Disjoint Set Union/Find ADT • Disjoint setpartition property: every element of a DS U/F structure belongs to exactly one set with a unique name • Dynamicequivalence property: Union(a, b) creates a new set which is the union of the sets containing a and b

  12. a b c 3 10 2 1 6 d e f 4 7 11 9 8 g h i 12 5 Example Construct the maze on the right Initial (the name of each set is underlined): {a}{b}{c}{d}{e}{f}{g}{h}{i} Randomly select edge 1 Order of edges in blue

  13. 3 10 2 1 6 4 7 11 9 8 12 5 Example, First Step {a}{b}{c}{d}{e}{f}{g}{h}{i} find(b) b find(e) e find(b)  find(e) so: add 1 to E union(b, e) {a}{b,e}{c}{d}{f}{g}{h}{i} a b c d e f g h i Order of edges in blue

  14. {a}{b,e}{c}{d}{f}{g}{h}{i} a b c 3 10 2 6 d e f 4 7 11 9 8 g h i 12 5 Example, Continued Order of edges in blue

  15. Up-Tree Intuition Finding the representative member of a set is somewhat like the opposite of finding whether a given key exists in a set. So, instead of using trees with pointers from each node to its children; let’s use trees with a pointer from each node to its parent.

  16. Up-Tree Union-Find Data Structure • Each subset is an up-tree with its root as its representative member • All members of a given set are nodes in that set’s up-tree a c g h f i d b e Up-trees are not necessarily binary!

  17. Find find(f) find(e) a b c 10 a c g h d e f 7 11 9 8 f i d b g h i 12 e Just traverse to the root! runtime:

  18. Union union(a,c) a b c 10 a c g h d e f 11 9 8 f i d b g h i 12 e Just hang one root from the other! runtime:

  19. a b c 3 10 2 1 6 The Whole Example (1/11) d e f 4 7 11 9 8 union(b,e) g h i 12 5 a b c d e f g h i a b c d f g h i e

  20. a b c 3 10 2 6 The Whole Example (2/11) d e f 4 7 11 9 8 union(a,d) g h i 12 5 a b c d f g h i e a b c f g h i d e

  21. a b c 3 10 6 The Whole Example (3/11) d e f 4 7 11 9 8 union(a,b) g h i 12 5 a b c f g h i d e a c f g h i b d e

  22. a c f g h i b d e While we’re finding e, could we do anything else? a b c 10 6 The Whole Example (4/11) d e f 4 7 11 9 8 find(d) = find(e) No union! g h i 12 5

  23. a b c 10 6 The Whole Example (5/11) d e f 7 11 9 8 union(h,i) g h i 12 5 a c f g h i a c f g h b d i b d e e

  24. a b c 10 6 The Whole Example (6/11) d e f 7 11 9 8 union(c,f) g h i 12 a c f g h a c g h b d b d f i i e e

  25. a c g h c g h b d f a f i i b d e Could we do a better job on this union? e a b c 10 The Whole Example (7/11) d e f 7 find(e) find(f) union(a,c) 11 9 8 g h i 12

  26. a b c 10 The Whole Example (8/11) d e f find(f) find(i) union(c,h) 11 9 8 g h i 12 c g h c g a f a f h i b d b d i e e

  27. a b c 10 The Whole Example (9/11) d e f find(e) = find(h) and find(b) = find(c) So, no unions for either of these. 11 9 g h i 12 c g a f h b d i e

  28. a b c The Whole Example (10/11) d e f find(d) find(g) union(c, g) 11 g h i 12 c g g c a f h a f h b d i b d i e e

  29. a b c The Whole Example (11/11) d e f find(g) = find(h) So, no union. And, we’re done! g h i 12 g a b c c d e f a f h g h i b d i Ooh… scary! Such a hard maze! e

  30. 0 (a)1 (b)2 (c)3 (d)4 (e)5 (f)6 (g)7 (h)8 (i) -1 0 -1 0 1 2 -1 -1 7 Nifty storage trick A forest of up-trees can easily be stored in an array. Node names  integerswith a hash table a c g h b d f i e up-index:

  31. ID find(Object x) { assert(HashTable.contains(x)); ID xID = HashTable[x]; while(up[xID] != -1) { xID = up[xID]; } return xID; } Implementation typedef ID int; ID up[10000]; runtime: O(depth)

  32. Implementation typedef ID int; ID up[10000]; ID union(Object x, Object y) { ID rootx = find(x); ID rooty = find(y); assert(rootx != rooty); up[y] = x; } runtime: O(1)

  33. a c g h b d f i e Room for Improvement:Weighted Union • Always makes the root of the larger tree the new root • Often cuts down on height of the new up-tree c g h a g h a f b d i c i b d f e Could we do a better job on this union? e Weighted union!

  34. Weighted Union Code typedef ID int; ID union(Object x, Object y) { rx = Find(x); ry = Find(y); assert(rx != ry); if (weight[rx] > weight[ry]) { up[ry] = rx; weight[rx] += weight[ry]; } else { up[rx] = ry; weight[ry] += weight[rx]; } } new runtime of union: new runtime of find:

  35. Weighted Union Find Analysis • Finds with weighted union are O(max up-tree height) • But, an up-tree of height h with weighted union must have at least 2h nodes • , 2max heightn and max height  log n • So, find takes O(log n) Base case: h = 0, tree has 20 = 1 node Induction hypothesis: assume true for h < h and consider the sequence of unions. Case 1: Union does not increase max height. Resulting tree still has  2h nodes. Case 2: Union has height h’= 1+h, where h = height of each of the input trees. By induction hypothesis each tree has  2h-1 nodes, so the merged tree has at least 2h nodes. QED.

  36. Alternatives to Weighted Union • Union by height • Ranked union (cheaper approximation to union by height) • See Weiss chapter 8.

  37. Room for Improvement: Path Compression • Points everything along the path of a find to the root • Reduces the height of the entire access path to 1 a c f g h i a c f g h i b d b d e e While we’re finding e, could we do anything else? Path compression!

  38. Path Compression Example find(e) c c g g a f h a f h b e b d d i i e

  39. Path Compression Code ID find(Object x) { assert(HashTable.contains(x)); ID xID = HashTable[x]; ID hold = xID; while(up[xID] != -1) { xID = up[xID]; } while(up[hold] != -1) { temp = up[hold]; up[hold] = xID; hold = temp; } return xID; } runtime:

  40. Digression: Inverse Ackermann’s Let log(k) n = log (log (log … (log n))) Then, let log* n = minimumk such that log(k) n  1 How fast does log* n grow? log* (2) = 1 log* (4) = 2 log* (16) = 3 log* (65536) = 4 log* (265536) = 5 (a 20,000 digit number!) log* (2265536) = 6 k logs

  41. Complex Complexity of Weighted Union + Path Compression • Tarjan (1984) proved that m weighted union and find operations with path commpression on a set of n elements have worst case complexity O(m log*(n)) actually even a little better! • For all practical purposes this is amortized constant time

  42. To Do • Read Chapter 8 • Graph Algorithms • Weiss Ch 9 Coming Up

More Related