lec15 - Lecture 15 An application of disjoint subsets...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 24 CSE 100, UCSD: LEC 15 Lecture 15 An application of disjoint subsets Disjoint subset structures and union/find algorithms Union-by-size and union-by-height Find with path compression Amortized cost analysis Reading: Weiss, Ch. 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 24 CSE 100, UCSD: LEC 15 Computing with equivalence classes Consider this general problem situation: You are given a set S of items You are given some pairs of items in S that satisfy some equivalence relation E() Given that information, you want to do things like Determine how many equivalence classes there are in S , as defined by the pairs of items satisfying E() that you have seen so far Given an item in S , determine which equivalence class is it in Given two items in S , determine whether they are in the same equivalence class Given a new pair of items in S that satisfy E() , update the system of equivalence classes appropriately Problems of that kind come up often in computer applications. We will look at one
Background image of page 2
Page 3 of 24 CSE 100, UCSD: LEC 15 Building a random maze Suppose you want to construct a nice maze on a NxM grid One good way to approach the problem is to see it as a problem of computing with equivalence classes Start with an array of NxM cells, each isolated from its neighbors by 4 ‘walls’. Consider the equivalence relation E( i , j ) true iff you can get from cell i to cell j Initially each cell is in its own singleton equivalence class Pick a wall at random. If knocking it down would join two distinct equivalence classes of cells, do so; otherwise leave it standing Continue until all the cells form one equivalence class, then stop The result is a connected undirected graph: there is a path between any two cells in the maze. Pick one as entrance, another as exit, and find a path between them! Question: Given a NxM maze constructed in this way, how many distinct simple paths are there from the “upper left corner” cell to the “lower right corner” cell?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 of 24 CSE 100, UCSD: LEC 15 A Disjoint Subset ADT An abstract data type designed for basic computations on equivalence classes is sometimes called a “disjoint subset” structure (since equivalence classes are disjoint subsets of their domain). It is sometimes also called a “union-find” structure because of the names of its principal operations Typical Disjoint Subset ADT operations: Create(int N) Create a system of N items, each initially in its own singleton equivalence class. These items are labelled with ints 0 up to N. The equivalence classes are also labelled with ints 0 up to N. int Find(int i) Return the int label of the equivalence class containing item i. int Union(int m, int n) Perform a set union operation on the equivalence classes with labels m and n; return the label of the result.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

lec15 - Lecture 15 An application of disjoint subsets...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online