Docsity

Randomized Algorithms: Nuts and Bolts Problem, Study notes of Computer Science

Toggle sidebar
bg1
CS 373 Lecture 3: Randomized Algorithms Fall 2002
The first nuts and bolts appeared in the middle 1400’s. The bolts were just screws with straight
sides and a blunt end. The nuts were hand-made, and very crude. When a match was found
between a nut and a bolt, they were kept together until they were finally assembled.
In the Industrial Revolution, it so on became obvious that threaded fasteners made it easier
to assemble products, and they also meant more reliable products. But the next big step came
in 1801, with Eli Whitney, the inventor of the cotton gin. The lathe had been recently improved.
Batches of bolts could now be cut on different lathes, and they would all fit the same nut.
Whitney set up a demonstration for President Adams, and Vice-President Jefferson. He had
piles of musket parts on a table. There were 10 similar parts in each pile. He went from pile to
pile, picking up a part at random. Using these completely random parts, he quickly put together
a working musket.
— Karl S. Kruszelnicki (‘Dr. Karl’), Karl Trek, December 1997
3 Randomized Algorithms (September 12)
3.1 Nuts and Bolts
Suppose we are given nnuts and nbolts of different sizes. Each nut matches exactly one bolt and
vice versa. The nuts and bolts are all almost exactly the same size, so we can’t tell if one bolt is
bigger than the other, or if one nut is bigger than the other. If we try to match a nut witch a bolt,
however, the nut will be either too big, too small, or just right for the bolt.
Our task is to match each nut to its corresponding bolt. But before we do this, let’s try to solve
some simpler problems, just to get a feel for what we can and can’t do.
Suppose we want to find the nut that matches a particular bolt. The obvious algorithm — test
every nut until we find a match — requires exactly n−1 tests in the worst case. We might have
to check every bolt except one; if we get down the the last bolt without finding a match, we know
that the last nut is the one we’re looking for.1
Intuitively, in the ‘average’ case, this algorithm will look at approximately n/2 nuts. But what
exactly does ‘average case’ mean?
3.2 Deterministic vs. Randomized Algorithms
Normally, when we talk about the running time of an algorithm, we mean the worst-case running
time. This is the maximum, over all problems of a certain size, of the running time of that algorithm
on that input:
Tworst-case(n) = max
|X|=nT(X).
On extremely rare occasions, we will also be interested in the best-case running time:
Tbest-case(n) = min
|X|=nT(X).
The average-case running time is best defined by the expected value, over all inputs Xof a certain
size, of the algorithm’s running time for X:2
Taverage-case(n) = E
|X|=n[T(X)] = X
|X|=n
T(x)·Pr[X].
1“Whenever you lose something, it’s always in the last place you look. So why not just look there first?”
2The notation E[ ] for expectation has nothing to do with the shift operator Eused in the annihilator method for
solving recurrences!
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Randomized Algorithms: Nuts and Bolts Problem and more Study notes Computer Science in PDF only on Docsity!

The first nuts and bolts appeared in the middle 1400’s. The bolts were just screws with straight sides and a blunt end. The nuts were hand-made, and very crude. When a match was found between a nut and a bolt, they were kept together until they were finally assembled. In the Industrial Revolution, it soon became obvious that threaded fasteners made it easier to assemble products, and they also meant more reliable products. But the next big step came in 1801, with Eli Whitney, the inventor of the cotton gin. The lathe had been recently improved. Batches of bolts could now be cut on different lathes, and they would all fit the same nut. Whitney set up a demonstration for President Adams, and Vice-President Jefferson. He had piles of musket parts on a table. There were 10 similar parts in each pile. He went from pile to pile, picking up a part at random. Using these completely random parts, he quickly put together a working musket. — Karl S. Kruszelnicki (‘Dr. Karl’), Karl Trek, December 1997

3 Randomized Algorithms (September 12)

3.1 Nuts and Bolts

Suppose we are given n nuts and n bolts of different sizes. Each nut matches exactly one bolt and vice versa. The nuts and bolts are all almost exactly the same size, so we can’t tell if one bolt is bigger than the other, or if one nut is bigger than the other. If we try to match a nut witch a bolt, however, the nut will be either too big, too small, or just right for the bolt. Our task is to match each nut to its corresponding bolt. But before we do this, let’s try to solve some simpler problems, just to get a feel for what we can and can’t do. Suppose we want to find the nut that matches a particular bolt. The obvious algorithm — test every nut until we find a match — requires exactly n − 1 tests in the worst case. We might have to check every bolt except one; if we get down the the last bolt without finding a match, we know that the last nut is the one we’re looking for.^1 Intuitively, in the ‘average’ case, this algorithm will look at approximately n/2 nuts. But what exactly does ‘average case’ mean?

3.2 Deterministic vs. Randomized Algorithms

Normally, when we talk about the running time of an algorithm, we mean the worst-case running time. This is the maximum, over all problems of a certain size, of the running time of that algorithm on that input: Tworst-case(n) = max |X|=n

T (X).

On extremely rare occasions, we will also be interested in the best-case running time:

Tbest-case(n) = min |X|=n

T (X).

The average-case running time is best defined by the expected value, over all inputs X of a certain size, of the algorithm’s running time for X:^2

Taverage-case(n) = E |X|=n

[T (X)] =

|X|=n

T (x) · Pr[X].

(^1) “Whenever you lose something, it’s always in the last place you look. So why not just look there first?” (^2) The notation E[ ] for expectation has nothing to do with the shift operator E used in the annihilator method for solving recurrences!

The problem with this definition is that we rarely, if ever, know what the probability of getting any particular input X is. We could compute average-case running times by assuming a particular probability distribution—for example, every possible input is equally likely—but this assumption doesn’t describe reality very well. Most real-life data is decidedly non-random. Instead of considering this rather questionable notion of average case running time, we will make a distinction between two kinds of algorithms: deterministic and randomized. A determinis- tic algorithm is one that always behaves the same way given the same input; the input completely determines the sequence of computations performed by the algorithm. Randomized algorithms, on the other hand, base their behavior not only on the input but also on several random choices. The same randomized algorithm, given the same input multiple times, may perform different computa- tions in each invocation. This means, among other things, that the running time of a randomized algorithm on a given input is no longer fixed, but is itself a random variable. When we analyze randomized algorithms, we are typically interested in the worst-case expected running time. That is, we look at the average running time for each input, and then choose the maximum over all inputs of a certain size:

Tworst-case expected (n) = max |X|=n

E[T (X)].

It’s important to note here that we are making no assumptions about the probability distribution of possible inputs. All the randomness is inside the algorithm, where we can control it!

3.3 Back to Nuts and Bolts

Let’s go back to the problem of finding the nut that matches a given bolt. Suppose we use the same algorithm as before, but at each step we choose a nut uniformly at random from the untested nuts. ‘Uniformly’ is a technical term meaning that each nut has exactly the same probability of being chosen.^3 So if there are k nuts left to test, each one will be chosen with probability 1/k. Now what’s the expected number of comparisons we have to perform? Intuitively, it should be about n/2, but let’s formalize our intuition. Let T (n) denote the number of comparisons our algorithm uses to find a match for a single bolt out of n nuts.^4 We still have some simple base cases T (1) = 0 and T (2) = 1, but when n > 2, T (n) is a random variable. T (n) is always between 1 and n − 1; it’s actual value depends on our algorithm’s random choices. We are interested in the expected value or expectation of T (n), which is defined as follows:

E[T (n)] =

n∑− 1

k=

k · Pr[T (n) = k]

If the target nut is the kth nut tested, our algorithm performs min{k, n − 1 } comparisons. In particular, if the target nut is the last nut chosen, we don’t actually test it. Because we choose the next nut to test uniformly at random, the target nut is equally likely—with probability exactly 1 /n—to be the first, second, third, or kth bolt tested, for any k. Thus:

Pr[T (n) = k] =

1 /n if k < n − 1, 2 /n if k = n − 1. (^3) This is what most people think ‘random’ means, but they’re wrong. (^4) Note that for this algorithm, the input is completely specified by the number n. Since we’re choosing the nuts to test at random, even the order in which the nuts and bolts are presented doesn’t matter. That’s why I’m using the simpler notation T (n) instead of T (X).

Along with the trivial base case T (0) = 0, this recurrence solves to

T (n) =

∑^ n

i=

(2n − 1) = n^2.

In the worst case, this algorithm tests every nut-bolt pair! We could have been a little more clever—for example, if the pivot bolt is the smallest bolt, we only need n − 1 tests to partition everything, not 2n − 1—but cleverness doesn’t actually help that much. We still end up with about n^2 /2 tests in the worst case. However, since this recursive algorithm looks almost exactly like quicksort, and everybody ‘knows’ that the ‘average-case’ running time of quicksort is Θ(n log n), it seems reasonable to guess that the average number of nut-bolt comparisons is also Θ(n log n). As we shall see shortly, if the pivot bolt is always chosen uniformly at random, this intuition is exactly right.

3.5 Reductions to and from Sorting

The second algorithm for mathing up the nuts and bolts looks exactly like quicksort. The algorithm not only matches up the nuts and bolts, but also sorts them by size. In fact, the problems of sorting and matching nuts and bolts are equivalent, in the following sense. If the bolts were sorted, we could match the nuts and bolts in O(n log n) time by performing a binary search with each nut. Thus, if we had an algorithm to sort the bolts in O(n log n) time, we would immediately have an algorithm to match the nuts and bolts, starting from scratch, in O(n log n) time. This process of assuming a solution to one problem and using it to solve another is called reduction—we can reduce the matching problem to the sorting problem in O(n log n) time. There is a reduction in the other direction, too. If the nuts and bolts were matched, we could sort them in O(n log n) time using, for example, merge sort. Thus, if we have an O(n log n) time algorithm for either sorting or matching nuts and bolts, we automatically have an O(n log n) time algorithm for the other problem. Unfortunately, since we aren’t allowed to directly compare two bolts or two nuts, we can’t use heapsort or mergesort to sort the nuts and bolts in O(n log n) worst case time. In fact, the problem of sorting nuts and bolts deterministically in O(n log n) time was only ‘solved’ in 1995^5 , but both the algorithms and their analysis are incredibly technical, the constant hidden in the O(·) notation is extremely large, and worst of all, the solutions are nonconstructive—We know the algorithms exist, but we don’t know what they look like! Reductions will come up again later in the course when we start talking about lower bounds and NP-completeness.

3.6 Recursive Analysis

Intuitively, we can argue that our quicksort-like algorithm will usually choose a bolt of approxi- mately median size, and so the average numbers of tests should be O(n log n). We can now finally formalize this intuition. To simplify the notation slightly, I’ll write T (n) in place of E[T (n)] every- where. Our randomized matching/sorting algorithm chooses its pivot bolt uniformly at random from the set of unmatched bolts. Since the pivot bolt is equally likely to be the smallest, second smallest,

(^5) J´anos Koml´os, Yuan Ma, and Endre Szemer´edi, Sorting nuts and bolts in O(n log n) time, SIAM J. Discrete Math 11(3):347–372, 1998. See also Phillip G. Bradford, Matching nuts and bolts optimally, Technical Report MPI-I-95-1-025, Max-Planck-Institut f¨ur Informatik, September 1995. Bradford’s algorithm is slightly simpler.

or kth smallest for any k, the expected number of tests performed by our algorithm is given by the following recurrence:

T (n) = 2n − 1 + Ek

[

T (k − 1) + T (n − k)

]

= 2 n − 1 +

n

n∑− 1

k=

T (k − 1) + T (n − k)

The base case is T (0) = 0. (We can save a few tests by setting T (1) = 1, but the analysis will be easier if we’re a little stupid.) Yuck. At this point, we could simply guess the solution, based on the incessant rumors that quicksort runs in O(n log n) time in the average case, and prove our guess correct by induction. A similar inductive proof appears in [CLR, pp. 166–167], but it was removed from the new edition [CLRS]. That’s okay; nobody ever understood that proof anyway. However, if we’re only interested in asymptotic bounds, we can afford to be a little conservative. What we’d really like is for the pivot bolt to be the median bolt, so that half the bolts are bigger and half the bolts are smaller. This isn’t very likely, but there is a good chance that the pivot bolt is close to the median bolt. Let’s say that a pivot bolt is good if it’s in the middle half of the final sorted set of bolts, that is, bigger than at least n/4 bolts and smaller than at least n/4 bolts. If the pivot bolt is good, then the worst split we can have is into one set of 3n/4 pairs and one set of n/ 4 pairs. If the pivot bolt is bad, then our algorithm is still better than starting over from scratch. Finally, a randomly chosen pivot bolt is good with probability 1/2. These simple observations give us the following simple recursive upper bound for the expected running time of our algorithm:

T (n) ≤ 2 n − 1 +

T

( (^3) n 4

+ T

( (^) n 4

· T (n)

A little algebra simplifies this even further:

T (n) ≤ 4 n − 2 + T

( (^3) n 4

+ T

( (^) n 4

We can easily solve this recurrence using the recursion tree method, giving us the completely

unsurprising upper bound T (n) = O(n log n). Similar observations give us the matching lower

bound T (n) = Ω(n log n).

3.7 Iterative Analysis

By making a simple change to our algorithm, which will have no effect on the number of tests, we can analyze it much more directly and exactly, without needing to solve a recurrence. The recursive subproblems solved by quicksort can be laid out in a binary tree, where each node corresponds to a subset of the nuts and bolts. In the usual recursive formulation, the algorithm partitions the nuts and bolts at the root, then the left child of the root, then the leftmost grandchild, and so forth, recursively sorting everything on the left before starting on the right subproblem. But we don’t have to solve the subproblems in this order. In fact, we can visit the nodes in the recursion tree in any order we like, as long as the root is visited first, and any other node is visited after its parent. Thus, we can recast quicksort in the following iterative form. Choose a pivot bolt, find its match, and partition the remaining nuts and bolts into two subsets. Then pick a second pivot bolt and partition whichever of the two subsets contains it. At this point, we have

Putting everything together, we get the following summation.

E[T (n)] =

∑^ n

i=

∑^ n

j=

E[Xij ]

∑^ n

i=

E[Xii] + 2

∑^ n

i=

∑^ n

j=i+

E[Xij ]

= n + 4

∑^ n

i=

∑^ n

j=i+

j − i + 1

This is quite a bit simpler than the recurrence we got before. In fact, with just a few lines of algebra, we can turn it into an exact, closed-form expression for the expected number of comparisons.

E[T (n)] = n + 4

∑^ n

i=

n∑−i+

j=

k [substitute k = j − i + 1]

= n + 4

∑^ n

k=

n−∑k+

i=

k [reorder summations]

= n + 4

∑^ n

k=

n − k + 1 k

= n + 4

(n − 1)

∑^ n

k=

k

∑^ n

k=

= n + 4((n + 1)(Hn − 1) − (n − 1)) = 4 nHn − 7 n + 4Hn

Sure enough, it’s Θ(n log n).

*3.9 Masochistic Analysis

If we’re feeling particularly masochistic, it is possible to solve the recurrence directly, all the way to an exact closed-form solution. [I’m including this only to show you it can be done; this won’t be on the test.] First we simplify the recurrence slightly by combining symmetric terms.

T (n) = 2n − 1 +

n

n∑− 1

k=

T (k − 1) + T (n − k)

= 2n − 1 +

n

n∑− 1

k=

T (k)

We then convert this ‘full history’ recurrence into a ‘limited history’ recurrence by shifting, and subtracting away common terms. (I call this “Magic step #1”.) To make this slightly easier, we

first multiply both sides of the recurrence by n to get rid of the fractions.

nT (n) = 2n^2 − n + 2

n∑− 1

k=

T (k)

(n − 1)T (n − 1) = 2(n − 1)^2 − (n − 1) ︸ ︷︷ ︸ 2 n^2 − 5 n+

n∑− 2

k=

T (k)

nT (n) − (n − 1)T (n − 1) = 4n − 3 + 2T (n − 1)

T (n) = 4 −

n

n + 1 n

T (n − 1)

To solve this limited-history recurrence, we define a new function t(n) = T (n)/(n + 1). (I call this “Magic step #2”.) This gives us an even simpler recurrence for t(n) in terms of t(n − 1):

t(n) = T (n) n + 1 =

n + 1

n

  • (n + 1)

T (n − 1) n

n + 1

n(n + 1)

  • t(n − 1)

n + 1

n

  • t(n − 1)

I used the technique of partial fractions (remember calculus?) to replace (^) n(n^1 +1) with (^) n^1 − (^) n+1^1 in the last step. The base case for this recurrence is t(0) = 0. Once again, we have a recurrence that translates directly into a summation, which we can solve with just a few lines of algebra.

t(n) =

∑^ n

i=

i + 1

i

∑^ n

i=

i + 1

∑^ n

i=

i = 7(Hn+1 − 1) − 3 Hn

= 4Hn − 7 +

n + 1

The last step uses the recursive definition of the harmonic numbers: Hn+1 = Hn + (^) n+1^1. Finally, substituting T (n) = (n+1)t(n) and simplifying gives us the exact solution to the original recurrence.

T (n) = 4(n + 1)Hn − 7(n + 1) + 7 = 4 nHn − 7 n + 4Hn

Surprise, surprise, we get exactly the same solution!