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
Disjoint Set Union/Find ADT Union/Find operations –create –destroy –union –find Disjoint set partition property: every element of a DS U/F structure belongs to exactly one set with a unique name Dynamic equivalence property: Union(a, b) creates a new set which is the union of the sets containing a and b {1,4,8} {7}{7} {6}{6} {5,9,10} {2,3} find(4) 8 union(3,6) {2,3,6} name of set

11
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 a d b e c f ghi 3 2 4 11 10 1 7 9 6 8 125

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

15
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 Hash table maps input data to the node associated with that data acgh db e Up-trees are not necessarily binary! fi

32
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 f gha b c i d e f gh a b c i d e Could we do a better job on this union? Weighted union! f gha b c i d e

40
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