- designing a maze - start with a k by l grid of squares for a total of k*l = m squares - initially, there are walls between all squares - maze building algorithm: for each of the m squares: Make-Set(square) let W be all the interior walls while |W| > 0 pick a random wall in W and remove from W let x and y be the squares on either side of the wall if Find-Set(x) != Find-Set(y) // not in the same set remove that wall in the maze (connecting the two sets of squares) Union(set(x), set(y) pick to random edge walls and remove them, picking one as entrance and one as exit - I claim that this is guaranteed to build a maze with a single unique path through it (I'll let you think about this) - How quickly can we do this? - disjoint set - a data structure to hold n distinct data items - the data items are stored in k different disjoint sets S = {S1, S2, ..., Sk} - must support the following operations - Make-Set(x): Creates a new set in the disjoin set with a single data item - Find-Set(x): Return a "representative" member from the set that x belongs to. If Find-Set is called multiple times without modifying the set we should get the same representative - Union(x, y): Merge together the sets containing data elements x and y. - Why is this useful? - making mazes! - connected components graph algorithm - Linked list representation - Each set is represented by a head and tail pointer, which point to an item in the set - The "representative" member of the set is pointed to by head - Each data item points to it's next neighbor AND points back to the original set object (allowing for quick lookup of the representative) - implementation of the operations - Make-Set: just make a new set object and linked list - Find-Set: - Assuming we have a link into the linked list: just follow the pointer back to the set and then get the head - If we don't, then we have to search the set (most of the time we do) - Union: - Concatenate y's list on the end of x's - Update all of y's pointers to point to x's set - runtime - Make-Set: O(1) - Find-Set - O(1) - O(size of the set) - Union - O(size of the smaller set) - When we talk about run-time, we often talk about a sequence of events (since this is how they're commonly used), specifically: - n total operations (some combination of Make-Set, Union and Find-Set) - m Make-Set operations - What is the running time of the above approach for n total operations and m Make-Set operations?