Unformatted Document Excerpt
Coursehero >>
Illinois >>
University of Illinois, Urbana Champaign >>
CS 473
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
pluribus Algorithms
E unum (Out of many, one)
Lecture 16: Disjoint Sets [Sp'10]
-- Official motto of the United States of America John: Who's your daddy? C'mon, you know who your daddy is! Who's your daddy? D'Argo, tell him who his daddy is!" D'Argo: I'm your daddy. -- Farscape, "Thanks for Sharing" (June 15, 2001) What rolls down stairs, alone or in pairs, rolls over your neighbor's dog? What's great for a snack, and fits on your back? It's Log, Log, Log! It's Log! It's Log! It's big, it's heavy, it's wood! It's Log! It's Log! It's better than bad, it's good! -- Ren & Stimpy, "Stimpy's Big Day/The Big Shot" (August 11, 1991) lyrics by John Kricfalusi The thing's hollow - it goes on forever - and - oh my God! - it's full of stars! -- Capt. David Bowman's last words(?) 2001: A Space Odyssey by Arthur C. Clarke (1968)
16
Data Structures for Disjoint Sets
In this lecture, we describe some methods for maintaining a collection of disjoint sets. Each set is represented as a pointer-based data structure, with one node per element. We will refer to the elements as either `objects' or `nodes', depending on whether we want to emphasize the set abstraction or the actual data structure. Each set has a unique `leader' element, which identifies the set. (Since the sets are always disjoint, the same object cannot be the leader of more than one set.) We want to support the following operations. MAKESET(x): Create a new set {x} containing the single element x. The object x must not appear in any other set in our collection. The leader of the new set is obviously x. FIND(x): Find (the leader of) the set containing x. UNION(A, B): Replace two sets A and B in our collection with their union A B. For example, UNION(A, MAKESET(x)) adds a new element x to an existing set A. The sets A and B are specified by arbitrary elements, so UNION(x, y) has exactly the same behavior as UNION(FIND(x), FIND( y)). Disjoint set data structures have lots of applications. For instance, Kruskal's minimum spanning tree algorithm relies on such a data structure to maintain the components of the intermediate spanning forest. Another application is maintaining the connected components of a graph as new vertices and edges are added. In both these applications, we can use a disjoint-set data structure, where we maintain a set for each connected component, containing that component's vertices.
16.1
Reversed Trees
One of the easiest ways to store sets is using trees, in which each node represents a single element of the set. Each node points to another node, called its parent, except for the leader of each set, which points to itself and thus is the root of the tree. MAKESET is trivial. FIND traverses parent pointers up to the leader. UNION just redirects the parent pointer of one leader to the other. Unlike most tree data structures, nodes do not have pointers down to their children.
c Copyright 2010 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision.
1
Algorithms
FIND(x): while x = parent(x) x parent(x) return x
Lecture 16: Disjoint Sets [Sp'10]
UNION(x, y): x FIND(x) y FIND( y) parent( y) x
MAKESET(x): parent(x) x
a b c d q
p r b c a d
p q r
Merging two sets stored as trees. Arrows point to parents. The shaded node has a new parent.
MAKE-SET clearly takes (1) time, and UNION requires only O(1) time in addition to the two FINDs. The running time of FIND(x) is proportional to the depth of x in the tree. It is not hard to come up with a sequence of operations that results in a tree that is a long chain of nodes, so that FIND takes (n) time in the worst case. However, there is an easy change we can make to our UNION algorithm, called union by depth, so that the trees always have logarithmic depth. Whenever we need to merge two trees, we always make the root of the shallower tree a child of the deeper one. This requires us to also maintain the depth of each tree, but this is quite easy.
UNION(x, y) x FIND(x) y FIND( y) if depth(x) > depth( y) parent( y) x else parent(x) y if depth(x) = depth( y) depth( y) depth( y) + 1
MAKESET(x): parent(x) x depth(x) 0
FIND(x): while x = parent(x) x parent(x) return x
With this new rule in place, it's not hard to prove by induction that for any set leader x, the size of x's set is at least 2depth(x) , as follows. If depth(x) = 0, then x is the leader of a singleton set. For any d > 0, when depth(x) becomes d for the first time, x is becoming the leader of the union of two sets, both of whose leaders had depth d - 1. By the inductive hypothesis, both component sets had at least 2d-1 elements, so the new set has at least 2d elements. Later UNION operations might add elements to x's set without changing its depth, but that only helps us. Since there are only n elements altogether, the maximum depth of any set is lg n. We conclude that if we use union by depth, both FIND and UNION run in (log n) time in the worst case.
16.2
Shallow Threaded Trees
Alternately, we could just have every object keep a pointer to the leader of its set. Thus, each set is represented by a shallow tree, where the leader is the root and all the other elements are its children. With this representation, MAKESET and FIND are completely trivial. Both operations clearly run in constant time. UNION is a little more difficult, but not much. Our algorithm sets all the leader pointers in one set to point to the leader of the other set. To do this, we need a method to visit every element 2
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
in a set; we will `thread' a linked list through each set, starting at the set's leader. The two threads are merged in the UNION algorithm in constant time.
a b c d q
p r p q r
a b c d
Merging two sets stored as threaded trees. Bold arrows point to leaders; lighter arrows form the threads. Shaded nodes have a new leader.
UNION(x, y): x FIND(x) y FIND( y) MAKESET(x): leader(x) x next(x) x FIND(x): return leader(x) yy leader( y) x while (next( y) = NULL) y next( y) leader( y) x next( y) next(x) next(x) y
The worst-case running time of UNION is a constant times the size of the larger set. Thus, if we merge a one-element set with another n-element set, the running time can be (n). Generalizing this idea, it is quite easy to come up with a sequence of n MAKESET and n - 1 UNION operations that requires (n2 ) time to create the set {1, 2, . . . , n} from scratch.
WORSTCASESEQUENCE(n): MAKESET(1) for i 2 to n MAKESET(i) UNION(1, i)
We are being stupid in two different ways here. One is the order of operations in WORSTCASESEQUENCE. Obviously, it would be more efficient to merge the sets in the other order, or to use some sort of divide and conquer approach. Unfortunately, we can't fix this; we don't get to decide how our data structures are used! The other is that we always update the leader pointers in the larger set. To fix this, we add a comparison inside the UNION algorithm to determine which set is smaller. This requires us to maintain the size of each set, but that's easy.
WEIGHTEDUNION(x, y) x FIND(x) y FIND( y) if size(x) > size( y) UNION(x, y) size(x) size(x) + size( y) else UNION( y, x) size(x) size(x) + size( y)
MAKEWEIGHTEDSET(x): leader(x) x next(x) x size(x) 1
The new WEIGHTEDUNION algorithm still takes (n) time to merge two n-element sets. However, in an amortized sense, this algorithm is much more efficient. Intuitively, before we can merge two large sets, we have to perform a large number of MAKEWEIGHTEDSET operations. 3
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
Theorem 1. A sequence of m MAKEWEIGHTEDSET operations and n WEIGHTEDUNION operations takes O(m + n log n) time in the worst case. Proof: Whenever the leader of an object x is changed by a WEIGHTEDUNION, the size of the set containing x increases by at least a factor of two. By induction, if the leader of x has changed k times, the set containing x has at least 2k members. After the sequence ends, the largest set contains at most n members. (Why?) Thus, the leader of any object x has changed at most lg n times. Since each WEIGHTEDUNION reduces the number of sets by one, there are m - n sets at the end of the sequence, and at most n objects are not in singleton sets. Since each of the non-singleton objects had O(log n) leader changes, the total amount of work done in updating the leader pointers is O(n log n). The aggregate method now implies that each WEIGHTEDUNION has amortized cost O(log n).
16.3
Path Compression
Using unthreaded tress, FIND takes logarithmic time and everything else is constant; using threaded trees, UNION takes logarithmic amortized time and everything else is constant. A third method allows us to get both of these operations to have almost constant running time. We start with the original unthreaded tree representation, where every object points to a parent. The key observation is that in any FIND operation, once we determine the leader of an object x, we can speed up future FINDs by redirecting x's parent pointer directly to that leader. In fact, we can change the parent pointers of all the ancestors of x all the way up to the root; this is easiest if we use recursion for the initial traversal up the tree. This modification to FIND is called path compression.
p a b c
Path compression during Find(c). Shaded nodes have a new parent.
p r c b a d q r
q d
FIND(x) if x = parent(x) parent(x) FIND(parent(x)) return parent(x)
If we use path compression, the `depth' field we used earlier to keep the trees shallow is no longer correct, and correcting it would take way too long. But this information still ensures that FIND runs in (log n) time in the worst case, so we'll just give it another name: rank. The following algorithm is usually called union by rank:
UNION(x, y) x FIND(x) y FIND( y) if rank(x) > rank( y) parent( y) x else parent(x) y if rank(x) = rank( y) rank( y) rank( y) + 1
MAKESET(x): parent(x) x rank(x) 0
4
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
FIND still runs in O(log n) time in the worst case; path compression increases the cost by only most a constant factor. But we have good reason to suspect that this upper bound is no longer tight. Our new algorithm memoizes the results of each FIND, so if we are asked to FIND the same item twice in a row, the second call returns in constant time. Splay trees used a similar strategy to achieve their optimal amortized cost, but our up-trees have fewer constraints on their structure than binary search trees, so we should get even better performance. This intuition is exactly correct, but it takes a bit of work to define precisely how much better the performance is. As a first approximation, we will prove below that the amortized cost of a FIND operation is bounded by the iterated logarithm of n, denoted log n, which is the number of times one must take the logarithm of n before the value is less than 1: lg n = 1
if n 2,
1 + lg (lg n) otherwise.
Our proof relies on several useful properties of ranks, which follow directly from the UNION and FIND algorithms. If a node x is not a set leader, then the rank of x is smaller than the rank of its parent. Whenever parent(x) changes, the new parent has larger rank than the old parent. Whenever the leader of x's set changes, the new leader has larger rank than the old leader. The size of any set is exponential in the rank of its leader: size(x) 2rank(x) . (This is easy to prove by induction, hint, hint.) In particular, since there are only n objects, the highest possible rank is lg n . For any integer r, there are at most n/2 r objects of rank r. Only the last property requires a clever argument to prove. Fix your favorite integer r. Observe that only set leaders can change their rank. Whenever the rank of any set leader x changes from r - 1 to r, mark all the objects in x's set. Since leader ranks can only increase over time, each object is marked at most once. There are n objects altogether, and any object with rank r marks at least 2 r objects. It follows that there are at most n/2 r objects with rank r, as claimed.
16.4
O(log n) Amortized Time
The following analysis of path compression was discovered just a few years ago by Raimund Seidel and Micha Sharir.1 Previous proofs2 relied on complicated charging schemes or potential-function arguments; Seidel and Sharir's analysis relies on a comparatively simple recursive decomposition.3 Seidel and Sharir phrase their analysis in terms of two more general operations on set forests. Their more general COMPRESS operation compresses any directed path, not just paths that lead to the root. The new SHATTER operation makes every node on a root-to-leaf path into its own parent.
Raimund Seidel and Micha Sharir. Top-down analysis of path compression. SIAM J. Computing 34(3):515525, 2005. Robert E. Tarjan. Efficiency of a good but not linear set union algorithm. J. Assoc. Comput. Mach. 22:215225, 1975. 3 For an even simpler analysis, see: Seth Pettie. Applications of forbidden 0-1 matrices to search tree and path compressionbased data structures. Proc. 21st Ann. ACM-SIAM Symp. Discrete Algorithms, 14571467, 2010.
2 1
5
Algorithms
COMPRESS(x, y): y must be an ancestor of x if x = y COMPRESS(parent(x), y) parent(x) parent( y)
Lecture 16: Disjoint Sets [Sp'10]
SHATTER(x): if parent(x) = x SHATTER(parent(x)) parent(x) x
Clearly, the running time of FIND(x) operation is dominated by the running time of COMPRESS(x, y), where y is the leader of the set containing x. This implies that we can prove the upper bound by analyzing an arbitrary sequence of UNION and COMPRESS operations. Moreover, we can assume that the arguments to each UNION operation are set leaders, so that each UNION takes only constant worst-case time. Finally, since each call to COMPRESS specifies the top node in the path to be compressed, we can reorder the sequence of operations, so that every UNION occurs before any COMPRESS, without changing the number of pointer assignments.
y x
x
y x y
y y x x
Top row: A COMPRESS followed by a UNION. Bottom row: The same operations in the order.
x
y
Each opposite UNION requires only constant time in the worst case, so we only need to analyze the amortized cost of COMPRESS. The running time of COMPRESS is proportional to the number of parent pointer assignments, plus O(1) overhead, so we will phrase our analysis in terms of pointer assignments. Let T (m, n, r ) denote the worst case number of pointer assignments in any sequence of at most m COMPRESS operations, executed on a forest of at most n nodes, with maximum rank at most r. The following trivial upper bound will be the base case for our recursive argument. Theorem 2. T (m, n, r) nr Proof: Each node can change parents at most r times, because each new parent has higher rank than the previous parent. Fix a forest F of n nodes with maximum rank r, and a sequence C of m COMPRESS operations on F , and let T (F, C) denote the total number of pointer assignments executed by this sequence. Let s be an arbitrary positive rank. Partition F into two sub-forests: a `low' forest F- containing all nodes with rank at most s, and a `high' forest F+ containing all nodes with rank greater than s. Since ranks increase as we follow parent pointers, every ancestor of a high node is another high node. Let n- and n+ denote the number of nodes in F- and F+ , respectively. Finally, let m+ denote the number of COMPRESS operations that involve any node in F+ , and let m- = m - m+ . Any sequence of COMPRESS operations on F can be decomposed into a sequence of COMPRESS operations on F+ , plus a sequence of COMPRESS and SHATTER operations on F- , with the same total 6
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
F
rank s rank < s
F+ F
rank s rank < s
Splitting the forest F (in this case, a single tree) into sub-forests F+ and F- at rank s.
cost. This requires only one small modification to the code: We forbid any low node from having a high parent. Specifically, if x is a low node and y is a high node, we replace any assignment parent(x) y with parent(x) x.
A Compress operation in F splits into a Compress operation in F+ and a Shatter operation in F-
This modification is equivalent to the following reduction:
COMPRESS(x, y, F ): y is an ancestor of x if rank(x) > r COMPRESS(x, y, F+ ) in C+ else if rank( y) r COMPRESS(x, y, F- ) in C- else z highest ancestor of x in F with rank at most r COMPRESS(parent F (z), y, F+ ) in C+ SHATTER(x, z, F- ) parent(z) z ()
The pointer assignment in the last line looks redundant, but it is actually necessary for the analysis. Each execution of line () mirrors an assignment of the form parent(x) y, where x is a low node, y is a high node, and the previous parent of x was a high node. Each of these `redundant' assignments happens immediately after a COMPRESS in the top forest, so we perform at most m+ redundant assignments. Each node x is touched by at most one SHATTER operation, so the total number of pointer reassignments in all the SHATTER operations is at most n. Thus, by partitioning the forest F into F+ and F- , we have also partitioned the sequence C of COMPRESS operations into subsequences C+ and C- , with respective lengths m+ and m- , such that the following inequality holds: T (F, C) T (F+ , C+ ) + T (F- , C- ) + m+ + n
7
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
Since there are only n/2i nodes of any rank i, we have n+ i>s n/2i = n/2s . The number of different ranks in F+ is r - s < r. Thus, Theorem 2 implies the upper bound T (F+ , C+ ) < r n/2s . Let us fix s = lg r , so that T (F+ , C+ ) n. We can now simplify our earlier recurrence to T (F, C) T (F- , C- ) + m+ + 2n, or equivalently, T (F, C) - m T (F- , C- ) - m- + 2n. Since this argument applies to any forest F and any sequence C, we have just proved that T (m, n, r) T (m, n, lg r ) + 2n , where T (m, n, r) = T (m, n, r) - m. The solution to this recurrence is T (n, m, r) 2n lg r. Voil! Theorem 3. T (m, n, r) m + 2n lg r
16.5
Turning the Crank
There is one place in the preceding analysis where we have significant room for improvement. Recall that we bounded the total cost of the operations on F+ using the trivial upper bound from Theorem 2. But we just proved a better upper bound in Theorem 3! We can apply precisely the same strategy, using Theorem 3 instead of Theorem 2, to improve the bound even more. Suppose we fix s = lg r, so that n+ = n/2lg r . Theorem 3 implies that T (F+ , C+ ) m+ + 2n This implies the recurrence T (F, C) T (F- , C- ) + 2m+ + 3n, which in turn implies that T (m, n, r) T (m, n, lg r) + 3n, where T (m, n, r) = T (m, n, r) - 2m. The solution to this equation is T (m, n, r) 2m + 3n lg r , where lg r is the iterated iterated logarithm of r: lg r = 1
lg r 2lg
r
m+ + 2n.
if r 2,
1 + lg (lg r) otherwise.
Naturally we can apply the same improvement strategy again, and again, as many times as we like, each time producing a tighter upper bound. Applying the reduction c times, for any positive integer c, gives us T (m, n, r) cm + (c + 1)n lg r where lg r =
c
c
lg r
if c = 0,
1 if r 2, c c-1 1 + lg (lg r) otherwise. 8
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
Each time we `turn the crank', the dependence on m increases, while the dependence on n and r decreases. For sufficiently large values of c, the cm term dominates the time bound, and further iterations only make things worse. The point of diminishing returns can be estimated by the minimum number of stars such that lg r is smaller than a constant: (r) = min c 1 lg n 3 . (The threshold value 3 is used here because lg 5 2 for all c.) By setting c = (r), we obtain our final upper bound. Theorem 4. T (m, n, r) m(r) + 3n((r) + 1) We can assume without loss of generality that m n by ignoring any singleton sets, so this upper bound can be further simplified to T (m, n, r) = O(m(r)) = O(m(n)). It follows that if we use union by rank, FIND with path compression runs in O((n)) amortized time. Even this upper bound is somewhat conservative if m is larger than n. A closer estimate is given by the function c (m, n) = min c 1 log (lg n) m/n . It's not hard to prove that if m = (n), then (m, n) = ((n)). On the other hand, if m n lg n, for any constant number of stars, then (m, n) = O(1). So even if the number of FIND operations is only slightly larger than the number of nodes, the amortized cost of each FIND is constant. O((m, n)) is actually a tight upper bound for the amortized cost of path compression; there are no more tricks that will improve the analysis further. More surprisingly, this is the best amortized bound we obtain for any pointer-based data structure for maintaining disjoint sets; the amortized cost of every FIND algorithm is at least ((m, n)). The proof of the matching lower bound is, unfortunately, far beyond the scope of this class.4
c c
16.6
The Ackermann Function and its Inverse
The iterated logarithms that fell out of our analysis of path compression are the inverses of a hierarchy of recursive functions defined by Wilhelm Ackermann in 1928.5 2 if n = 1
2 n=
c
2n if c = 0 c-1 c 2 (2 (n - 1)) otherwise
For each fixed c, the function 2 c n is monotonically increasing in n, and these functions grow incredibly faster as the index c increases. 2 n is the familiar power function 2n . 2 n is the tower function 2 ; this function is also sometimes called tetration. John Conway named 2 n the wower function: 2 n = 2 2 2. And so on, et cetera, ad infinitum.
n
4 Robert E. Tarjan. A class of algorithms which require non-linear time to maintain disjoint sets. J. Comput. Syst. Sci. 19:110127, 1979. 5 Ackermann didn't define his functions this way--I'm actually describing a slightly cleaner hierarchy defined 35 years later by R. Creighton Buck--but the exact details of the definition are surprisingly irrelevant! The mnemonic up-arrow notation for these functions was introduced by Don Knuth in the 1970s.
22
.2 ..
n
9
Algorithms
c
Lecture 16: Disjoint Sets [Sp'10]
For any fixed c, the function log n is the inverse of the function 2 c+1 n, the (c + 1)th row in the Ackerman hierarchy. Thus, for any remotely reasonable values of n, say n 2256 , we have log n 5, c log n 4, and log n 3 for any c 3. The function (n) is usually called the inverse Ackerman function.6 Our earlier definition is equivalent to (n) = min{c 1 | 2 c+2 3 n}; in other words, (n) + 2 is the inverse of the third column in the c Ackermann hierarchy. The function (n) grows much more slowly than log n for any fixed c; we have (n) 3 for all even remotely imaginable values of n. Nevertheless, the function (n) is eventually larger than any constant, so it is not O(1).
2 c n 2n 2n 2 n 2 n 2 n 2 n n=1 2 2 2 2 2 2 4 4 4 4 4
2...2
2...2
n=3 6 8 16 65536 2
. ..
n=4 8 16 65536 2 2
2 .. 2. 65536
n=5 10 32 265536 2
2 .. 2. 65536 2 ..
22
.2 ..
65536
22
.2 ..
2. 22
65536
22
.2 ..
65536
2...2
2...2
2...2 . ..
65536
22
Yeah, right. Argh! My eyes!
2...2
65536
2
4
2
22
Very funny.
Small (!!) values of Ackermann's functions.
16.7
To infinity. . . and beyond!
Of course, one can generalize the inverse Ackermann function to functions that grow arbitrarily more slowly, starting with the iterated inverse Ackermann function (n) = 1
if n 4
1 + ((n)) otherwise,
then the iterated iterated inverse Ackermann function (n) = 1
if n 4
1 + ((n)) otherwise,
and then the diagonalized inverse Ackermann function Head-asplode(n) = min{c 1 | n 4}, and so on forever. Fortunately(?), such functions appear extremely rarely in algorithm analysis. In fact, the only example (as far as Jeff knows) of a naturally-occurring super-constant sub-inverse-Ackermann function is a recent result of Seth Pettie7 , who proved that if a splay tree is used as a double-ended queue -- insertions and deletions of only smallest or largest elements -- then the amortized cost of any operation is O( (n)).
Strictly speaking, the name `inverse Ackerman function' is inaccurate. One good formal definition of the true inverse c ~ Ackerman function is (n) = min c 1 lg n c = min c 1 2 c+2 c n . However, it's not hard to prove that ~ ~ (n) (n) (n) + 1 for all sufficiently large n, so the inaccuracy is completely forgivable. As I said in the previous footnote, the exact details of the definition are surprisingly irrelevant! 7 Splay trees, Davenport-Schinzel sequences, and the deque conjecture. Proceedings of the 19th Annual ACM-SIAM Symposium on Discrete Algorithms, 11151124, 2008.
6 c
10
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
Exercises
1. Consider the following solution for the union-find problem, called union-by-weight. Each set leader x stores the number of elements of its set in the field weight(x). Whenever we UNION two sets, the leader of the smaller set becomes a new child of the leader of the larger set (breaking ties arbitrarily).
MAKESET(x): parent(x) x weight(x) 1 FIND(x): while x = parent(x) x parent(x) return x UNION(x, y) x FIND(x) y FIND( y) if weight(x) > weight( y) parent( y) x weight(x) weight(x) + weight( y) else parent(x) y weight(x) weight(x) + weight( y)
Prove that if we use union-by-weight, the worst-case running time of FIND(x) is O(log n), where n is the cardinality of the set containing x. 2. Consider a union-find data structure that uses union by depth (or equivalently union by rank) without path compression. For all integers m and n such that m 2n, prove that there is a sequence of n MakeSet operations, followed by m UNION and FIND operations, that require (m log n) time to execute. 3. Consider an arbitrary sequence of m MAKESET operations, followed by u UNION operations, followed by f FIND operations, and let n = m + u + f . Prove that if we use union by rank and FIND with path compression, all n operations are executed in O(n) time. 4. Describe and analyze a data structure to support the following operations on an array X [1 .. n] as quickly as possible. Initially, X [i] = 0 for all i. Given an index i such that X [i] = 0, set X [i] to 1. Given an index i, return X [i]. Given an index i, return the smallest index j i such that X [ j] = 0, or report that no such index exists. For full credit, the first two operations should run in worst-case constant time, and the amortized cost of the third operation should be as small as possible. 5. (a) Describe and analyze an algorithm to compute the size of the largest connected component of black pixels in an n n bitmap B[1 .. n, 1 .. n]. For example, given the bitmap below as input, your algorithm should return the number 9, because the largest conected black component (marked with white dots on the right) contains nine pixels.
11
Algorithms
Lecture 16: Disjoint Sets [Sp'10]
9
(b) Design and analyze an algorithm BLACKEN(i, j) that colors the pixel B[i, j] black and returns the size of the largest black component in the bitmap. For full credit, the amortized running time of your algorithm (starting with an all-white bitmap) must be as small as possible. For example, at each step in the sequence below, we blacken the pixel marked with an X. The largest black component is marked with white dots; the number underneath shows the correct output of the BLACKEN algorithm.
9
14
14
16
17
(c) What is the worst-case running time of your BLACKEN algorithm? 6. Consider the following game. I choose a positive integer n and keep it secret; your goal is to discover this integer. We play the game in rounds. In each round, you write a list of at most n integers on the blackboard. If you write more than n numbers in a single round, you lose. (Thus, in the first round, you must write only the number 1; do you see why?) If n is one of the numbers you wrote, you win the game; otherwise, I announce which of the numbers you wrote is smaller or larger than n, and we proceed to the next round. For example:
You 1 4, 42 8, 15, 16, 23, 30 9, 10, 11, 12, 13, 14 Me It's bigger than 1. It's between 4 and 42. It's between 8 and 15. It's 11; you win!
Describe a strategy that allows you to win in O((n)) rounds!
c Copyright 2010 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision.
12
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more.
Course Hero has millions of course specific materials providing students with the best way to expand
their education.
Below is a small sample set of documents:
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 17: Basic Graph Properties [Sp'10]Obie looked at the seein' eye dog. Then at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one. . . and then he looked at the seein' eye d
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 18: Minimum Spanning Trees [Sp'10]We must all hang together, gentlemen, or else we shall most assuredly hang separately. - Benjamin Franklin, at the signing of the Declaration of Independence (July 4, 1776) It is a very sad thing that
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 19: Shortest Paths [Sp'10]Well, ya turn left by the fire station in the village and take the old post road by the reservoir and. . . no, that won't do. Best to continue straight on by the tar road until you reach the schoolhouse and th
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 20: All-Pairs Shortest PathsThe tree which fills the arms grew from the tiniest sprout; the tower of nine storeys rose from a (small) heap of earth; the journey of a thousand li commenced with a single step. - Lao-Tzu, Tao Te Ching, ch
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 21: Maximum Flows and Minimum Cuts [Fa'10]Col. Hogan: One of these wires disconnects the fuse, the other one fires the bomb. Which one would you cut, Shultz? Sgt. Schultz: Don't ask me, this is a decision for an officer. Col. Hogan: Al
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 22: Max-Flow Algorithms [Fa'10]A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it. - The First Law of Mentat, in Frank Herbert's Dune (1965) There's a diff
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 23: Applications of Maximum Flow [Fa'10]For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly sm
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 18: Extensions of Maximum Flow [Fa'10]"Who are you?" said Lunkwill, rising angrily from his seat. "What do you want?" "I am Majikthise!" announced the older one. "And I demand that I am Vroomfondel!" shouted the younger one. Majikthise
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 25: Linear Programming [Fa '10]The greatest flood has the soonest ebb; the sorest tempest the most sudden calm; the hottest love the coldest end; and from the deepest desire oftentimes ensues the deadliest hate. - Socrates Th' extremes
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 25: Linear Programming [Fa '10]The greatest flood has the soonest ebb; the sorest tempest the most sudden calm; the hottest love the coldest end; and from the deepest desire oftentimes ensues the deadliest hate. - Socrates Th' extremes
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 26: Linear Programming Algorithms [Fa'10]Simplicibus itaque verbis gaudet Mathematica Veritas, cum etiam per se simplex sit Veritatis oratio. [And thus Mathematical Truth prefers simple words, because the language of Truth is itself si
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 27: Lower Bounds [Sp'10]It was a Game called Yes and No, where Scrooge's nephew had to think of something, and the rest must find out what; he only answering to their questions yes or no, as the case was. The brisk fire of questioning
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 28: Adversary Arguments [Sp'10]An adversary means opposition and competition, but not having an adversary means grief and loneliness. - Zhuangzi (Chuang-tsu) c. 300 BC It is possible that the operator could be hit by an asteroid and yo
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 29: NP-Hard Problems [Fa'10]The wonderful thing about standards is that there are so many of them to choose from. - Real Admiral Grace Murray Hopper If a problem has no solution, it may not be a problem, but a fact - not to be solved,
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsLecture 30: Approximation Algorithms [Fa'10]Le mieux est l'ennemi du bien. [The best is the enemy of the good.] - Voltaire, La Bgueule (1772) Who shall forbid a wise skepticism, seeing that there is no practical question on which any thing mor
University of Illinois, Urbana Champaign - CS - 473
Appendix I: Proof by Induction [Fa10]AlgorithmsJeder Genieende meint, dem Baume habe es an der Frucht gelegen;aber ihm lag am Samen.[Everyone who enjoys thinks that the fundamental thing about trees is the fruit,but in fact it is the seed.] Friedric
University of Illinois, Urbana Champaign - CS - 473
Appendix II: Solving Recurrences [Fa10]AlgorithmsChange is certain. Peace is followed by disturbances; departure of evil men by theirreturn. Such recurrences should not constitute occasions for sadness but realities forawareness, so that one may be ha
University of Illinois, Urbana Champaign - CS - 473
AlgorithmsDepartment of Computer ScienceUniversity of Illinois at Urbana-ChampaignInstructor: Jeff EricksonTeaching Assistants: Spring 1999: Mitch Harris and Shripad Thite Summer 1999 (IMCS): Mitch Harris Summer 2000 (IMCS): Mitch Harris Fall 2000
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 0, due August 31, 2000 at the beginning of className: Net ID:Alias:Neatly print your name (rst name rst, with no comma), your network ID, and a short alias into the boxes above. Do not sign your name
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 0, due August 31, 2000 at the beginning of className: Net ID:Alias:Neatly print your name (rst name rst, with no comma), your network ID, and a short alias into the boxes above. Do not sign your name
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2002Homework 0, due September 5, 2002 at the beginning of className: Net ID:Alias:UGNeatly print your name (rst name rst, with no comma), your network ID, and an alias of your choice into the boxes above. Circle
University of Illinois, Urbana Champaign - CS - 473
CS 473G: Combinatorial Algorithms, Fall 2005Homework 0Due Thursday, September 1, 2005, at the beginning of class (12:30pm CDT)Name:Net ID:Alias:I understand the Homework Instructions and FAQ. Neatly print your full name, your NetID, and an alias of
University of Illinois, Urbana Champaign - CS - 473
CS 473U: Undergraduate Algorithms, Fall 2006Homework 0Due Friday, September 1, 2006 at noon in 3229 Siebel CenterName:Net ID:Alias:I understand the Homework Instructions and FAQ. Neatly print your full name, your NetID, and an alias of your choice
University of Illinois, Urbana Champaign - CS - 473
CS 573: Graduate Algorithms, Fall 2008Homework 0Due in class at 12:30pm, Wednesday, September 3, 2008Name:Net ID:Alias:I understand the course policies. Each student must submit their own solutions for this homework. For all future homeworks,group
University of Illinois, Urbana Champaign - CS - 473
CS 573Homework 0 (due September 1, 2010)Fall 2010CS 573: Graduate Algorithms, Fall 2010Homework 0Due Wednesday, September 1, 2010 in class This homework tests your familiarity with prerequisite material (http:/www.cs.uiuc.edu/class/fa10/cs573/stuff
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Spring 1999Final Exam (May 7, 1999)Name: Net ID:Alias:This is a closed-book, closed-notes exam!If you brought anything with you besides writing instruments and your two 8 1 11 cheat sheets, please leave it at the fro
University of Illinois, Urbana Champaign - CS - 473
CS473ugHead Banging Session #39/19/06 - 9/21/061. Championship Showdown What excitement! The Champaign Spinners and the Urbana Dreamweavers have advanced to meet each other in the World Series of Basketweaving! The World Champions will be decided by a
University of Illinois, Urbana Champaign - CS - 473
CS473ugHead Banging Session #510/03/06 - 10/05/061. Simulating Queues with Stacks A queue is a rst-in-rst-out data structure. It supports two operations push and pop. Push adds a new item to the back of the queue, while pop removes the rst item from th
University of Illinois, Urbana Champaign - CS - 473
CS473ugHead Banging Session #810/24/06 - 10/26/061. Alien Abduction Mulder and Scully have computed, for every road in the United States, the exact probability that someone driving on that road wont be abducted by aliens. Agent Mulder needs to drive fr
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 0, due August 31, 2000 at the beginning of className: Net ID:Alias:Neatly print your name (rst name rst, with no comma), your network ID, and a short alias into the boxes above. Do not sign your name
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 0 (due 1/26/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/ cs373 Homework 0 (due January 26, 1999 by the beginning of class)Name: Net ID:Alias:Neatly print your name (rst name rst, with no c
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 1 (due September 12, 2000 at midnight)Name: Net ID: Name: Net ID: Name: Net ID:Alias:U 3/4 1Alias:U 3/4 1Alias:U 3/4 1Starting with Homework 1, homeworks may be done in teams of up to three peop
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 1 (due 2/9/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/~cs373 Homework 1 (due February 9, 1999 by noon)Name: Net ID:Alias:Everyone must do the problems marked . Problems marked are for 1-u
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 2 (due September 28, 2000 at midnight)Name: Net ID: Name: Net ID: Name: Net ID:Alias:U 3/4 1Alias:U 3/4 1Alias:U 3/4 1Starting with Homework 1, homeworks may be done in teams of up to three peop
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 2 (due 2/18/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/~cs373 Homework 2 (due Thu. Feb. 18, 1999 by noon)Name: Net ID:Alias:Everyone must do the problems marked . Problems marked are for
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 3 (due October 17, 2000 at midnight)Name: Net ID: Name: Net ID: Name: Net ID:Alias:U 3/4 1Alias:U 3/4 1Alias:U 3/4 1Starting with Homework 1, homeworks may be done in teams of up to three people
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 3 (due 3/11/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/~cs373 Homework 3 (due Thu. Mar. 11, 1999 by noon)Name: Net ID:Alias:Everyone must do the problems marked . Problems marked are for
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Fall 2000Homework 4 (due October 26, 2000 at midnight)Name: Net ID: Name: Net ID: Name: Net ID:Alias:U 3/4 1Alias:U 3/4 1Alias:U 3/4 1Homeworks may be done in teams of up to three people. Each team turns in just
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 4 (due 4/1/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/~cs373 Homework 4 (due Thu. Apr. 1, 1999 by noon)Name: Net ID:Alias:Everyone must do the problems marked . Problems marked are for 1-
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 5 (due 4/22/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/~cs373 Homework 5 (due Thu. Apr. 22, 1999 by noon)Name: Net ID:Alias:Everyone must do the problems marked . Problems marked are for
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Spring 1999Midterm 1 (February 23, 1999)Name: Net ID:Alias:This is a closed-book, closed-notes exam!If you brought anything with you besides writing instruments and your 8 1 11 cheat sheet, please leave it at the fro
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Spring 1999Midterm 2 (April 6, 1999)Name: Net ID:Alias:This is a closed-book, closed-notes exam!If you brought anything with you besides writing instruments and your 8 1 11 cheat sheet, please leave it at the front o
University of Illinois, Urbana Champaign - CS - 473
CS 373: Combinatorial Algorithms, Spring 2001Homework 0, due January 23, 2001 at the beginning of className: Net ID:Alias:Neatly print your name (rst name rst, with no comma), your network ID, and a short alias into the boxes above. Do not sign your n
University of Illinois, Urbana Champaign - CS - 473
CS 373U: Combinatorial Algorithms, Spring 2004Homework 0Due January 28, 2004 at noonName:Net ID:Alias:I understand the Homework Instructions and FAQ. Neatly print your full name, your NetID, and an alias of your choice in the boxes above.Grades wi
University of Illinois, Urbana Champaign - CS - 473
CS 473G: Graduate Algorithms, Spring 2007Homework 0Due in class at 11:00am, Tuesday, January 30, 2007Name:Net ID:Alias:I understand the Course Policies. Neatly print your full name, your NetID, and an alias of your choice in the boxes above, andst
University of Illinois, Urbana Champaign - CS - 473
CS 473Homework 0 (due January 27, 2009)Spring 2009CS 473: Undergraduate Algorithms, Spring 2009Homework 0Due in class at 11:00am, Tuesday, January 27, 2009 This homework tests your familiarity with prerequisite materialbig-Oh notation, elementaryal
University of Illinois, Urbana Champaign - CS - 473
CS 473Homework 0 (due January 26, 2009)Spring 2010CS 473: Undergraduate Algorithms, Spring 2010Homework 0Due Tuesday, January 26, 2009 in class This homework tests your familiarity with prerequisite materialbig-Oh notation, elementaryalgorithms and
University of Illinois, Urbana Champaign - CS - 473
CS 373Homework 0 (due 1/26/99)Spring 1999CS 373: Combinatorial Algorithms, Spring 1999http:/www-courses.cs.uiuc.edu/ cs373 Homework 0 (due January 26, 1999 by the beginning of class)Name: Net ID:Alias:Neatly print your name (rst name rst, with no c
Tulane - GEOL - 212
2 Component Phase DiagramsEENS 2110MineralogyTulane UniversityProf. Stephen A. NelsonTWO COMPONENT (BINARY) PHASE DIAGRAMSThis document last updated on 07-Feb-2011Experimental Determination of 2-Component Phase DiagramsAs an example, we're going t
Tulane - GEOL - 212
Contact MetamorphismEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityContact MetamorphismThis document last updated on 30-Mar-2011As discussed previously, contact metamorphism occurs as a result of a high geothermalgradient produced local
Tulane - GEOL - 212
General Classification of Igneous RocksEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityGeneral Classification of Igneous RocksThis document last updated on 11-Jan-2011Classification of igneous rocks is one of the most confusing aspects of
Tulane - GEOL - 212
Magmatic DifferentiationEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityMagmatic DifferentiationThis document last updated on 23-Jan-2011Chemical Variation in Rock SuitesSoon after geologists began doing chemical analyses of igneous rock
Tulane - GEOL - 212
Metamorphic Mineral AssemblagesEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityMetamorphic Mineral AssemblagesThis document last updated on 21-Mar-2011The mineral assemblages that occur in metamorphic rocks depend on four factors:The bul
Tulane - GEOL - 212
Metamorphic ReactionsEENS 212PetrologyProf. Stephen A. NelsonTulane UniversityMetamorphic Reactions, Isograds, and Reaction MechanismsThis document last updated on 22-Mar-2011Types of Metamorphic ReactionsChemical reactions that take place during
Tulane - GEOL - 212
Metamorphic TexturesEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityMetamorphic Rock TexturesThis document last updated on 10-Mar-2011Metamorphic rocks exhibit a variety of textures. These can range from textures similar to theoriginal p
Tulane - GEOL - 212
Regional MetamorphismEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityRegional MetamorphismThis document last updated on 31-Mar-2011Regional metamorphism is metamorphism that occurs over broad areas of the crust. Mostregionally metamorpho
Tulane - GEOL - 212
Textures of Igneous RocksEENS 212 Prof. Stephen A. NelsonPetrology Tulane UniversityTextures of Igneous RocksThis document last updated on 12-Feb-2004Introduction to Igneous Rocks An igneous rock is any crystalline or glassy rock that forms from cool
Tulane - GEOL - 212
Thermodynamics and MetamorphismEENS 212Prof. Stephen A. NelsonThermodynamics and MetamorphismPetrologyTulane UniversityThis document last updated on 18-Mar-2010Equilibrium and ThermodynamicsAlthough the stability relationships between various phas
Tulane - GEOL - 212
Triangular Plots in Metamorphic PetrologyEENS 212Prof. Stephen A. NelsonTriangular Plots in Metamorphic PetrologyPetrologyTulane UniversityThis document last updated on 15-Mar-2010Like igneous rocks, most metamorphic rocks are composed of 9 or more
Tulane - EENS - 212
Igneous Rocks of Contintental LithosphereEENS 2120PetrologyProf. Stephen A. NelsonIgneous Rocks of the Continental LithosphereThis document last updated on 15-Feb-2011IntroductionA wide variety of igneous rocks occur in the continental lithosphere,
Tulane - EENS - 212
Convergent MarginsEENS 2120Prof. Stephen A. NelsonPetrologyTulane UniversityIgneous Rocks of the Convergent MarginsThis document last updated on 08-Feb-2011The convergent plate margins are the most intense areas of active magmatism above sea level