



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




4.1.4. The sequence of Fibonacci numbers Fn is defined as F 1 = 1, F 2 = 1, and for n > 2 , Fn = Fn− 1 + Fn− 2. Write an algorithm for obtaining F 100. set F 1 to 1; set F 2 to 1; set i to 3; while i ≤ 100 do set Fi to Fi− 1 + Fi− 2 ; set i to i + 1; return F 100 ;
We see that this algorithm will proceed by first initializing F 1 and F 2 , then, in turn, setting i to 3 and calculating Fi, then setting i to 4 and calculating Fi, and so forth, until it has calculated Fi when i is 100, at which point it can stop.
4.1.6. Suppose we have two words of arbitrary length. If a computer can compare any two letters, describe an algorithm for determining which word comes first in alphabetical order. Input: words a 1 a 2 a 3... an and b 1 b 2 b 3... bm set i to 1; repeat if i > n and i > m then decide that a = b; if i > n and i ≤ m then decide that a < b; if i ≤ n and i > m then decide that a > b; if i ≤ n and i ≤ m then if ai < bi then decide that a < b; if ai > bi then decide that a > b; if ai = bi then set i to i + 1 and make no decision;
until we have made a judgment ; This algorithm is an expression of how we would compare words. The process is simply a matter of comparing the first letters (a 1 to b 1 ), and returning the result of that com- parison if unequal, and moving onto the next letter if impossible. i records which letter we are on at each stage of the process, so it starts at 1, since we start by comparing the first letters, and increments when a letter fails to provide an adequate comparison. The conditions at the beginning indicate what to do when we run out of letters from one or the other string. If we run out on one, it means that string is shorter (e.g. “ant” is lexicographically smaller than “anteater”); if we run out on both, it means we’ve exhausted both words without finding a way in which they differ, in which case the two words are identical.
4.1.10. Describe an algorithm that inputs a positive integer N and outputs its binary repre-
sentation. Input: integer N set i to 0; repeat set i to i + 1; if N is even then set ai to 0; if N is odd then set ai to 1 and set N to N − 1; set N to N 2 ; until N = 0 ; output aiai− 1 ai− 2 · · · a 0 ;
This algorithm works by repeated division of N by 2, recording the remainder in each division in the ais. We can see how this works on, for instance, 13: division by 2 yields 6 with a remainder of 1, another division yields 3 with a remainder of 0, another division yields 1 with a remainder of 1, and another division yields 0 with a remainder of 1. Our remainders are, in order: 1, 0, 1, 1. Reversing those gives the binary representation
f (n) =
f (
n 2
)0 if n is even
f (
n − 1 2
)1 if n is odd
The above algorithm could, in fact, be framed as a recursive procedure.
4.1.12. There are n gossips, each of whom knows some information that the others do not. Whenever two gossips call each other, they always tell each other all the information that they know.
(a) Describe a sequence of calls that will result in each gossip hearing all the infor- mation. Let us call our gossips g 1 , g 2 ,... , gn. The easiest way to distribute the information would be to collect all the information in one place, then transmit it all from there. We will use g 1 as our collection point, so we can have g 1 call each of g 2 , g 3 ,... , gn in turn, collecting each of their pieces of information. After these n − 1 calls, both g 1 and gn will know everything (before calling gn, g 1 knew everything except gns secret; when they shared information, they both got a complete set of secrets). g 1 now needs to share these secrets with all n − 2 of the uninformed gossips, and can do so by calling each of g 2 ,... , gn− 1 in turn. Thus, all secrets are shared in 2n − 3 calls if g 1 calls the following people in order: g 2 , g 3 ,... , gn, g 2 , g 3 ,... , gn − 1. (b) Show that for n ≥ 4 , everyone can learn all the gossip in 2 n − 4 calls. Above we saw that it was possible in 2n−3 calls: shaving one call off of that naive strategy requires some subtlety! We may note that 4 gossips can share every piece of information in 4 calls with the following sequence: g 1 calls g 2 , g 3 calls g 4 , g 1 calls g 3 , and g 2 calls g 4. Now we can show that 2(n + 1) − 4 suffices to solve the
4.2.5. If limn→∞ f g^ ((nn)) = k < ∞, show that f (n) is O(g(n)).
Let us state that limit in a classical theoretically justifiable form: given limn→∞ f g^ ((nn)) = k, we know that for any we might choose, there is a value of N such that if n > N , k − < f g^ ((nn)) < k + . Let us arbitrarily choose = 1 (we can choose pretty much anything, but this makes the math work well). Thus, we know there is some N such that for all n > N , f g^ ((nn)) < k + 1. We also have a lower bound on the ratio, but it’s not useful to us. Algebraically rearranging the above expression, we get that for n > N , f (n) < (k + 1)g(n). We have thus shown that, for sufficiently large n (namely, n > N ), it is the case that f (n) is bounded above by a constant multiple of g(n) (namely, (k +1) times g(n)), so f (n) must be O(g(n)).
4.2.8. For each of the following pairs of functions, prove that f (n) is not O(g(n)).
In this problem, it is most effective to use what is essentially the converse of the statement proven in problem 4.2.5: if limn→∞ f g^ ((nn)) = ∞, then f (n) is not O(g(n)). This proceeds along quite similar lines to the proof of problem 4.2.5: the definition of the limit asusres us that for any E we might choose, there is a value of N such that if n > N , f g^ ((nn)) > E. Thus, for any value of E we might choose, there is an n such that if n > N , f (n) > Eg(n). Thus, any constant multiple of g(n) we choose (represented above by Eg(n) will be exceeded for sufficiently large n by f (n). Thus, no constant multiple of g(n) serves as a long-term upper bound on f (n), so f (n) is not O(g(n)).
(a) f (n) = nm+1^ and g(n) = nm. Since f g^ ((nn)) = n, it follows that limn→∞ f g^ ((nn)) = ∞, so by the lemma proven above, f (n) is not O(g(n)). (b) f (n) = n^3 /^2 and g(n) = n log n. We use algebraic simplification combined with L’Hˆopital’s rule on the indetermi- nate form ∞∞ :
lim n→∞
f (n) g(n)
= lim n→∞
n^3 /^2 n log n
= lim n→∞
n log n
= lim n→∞
1 2 √n 1 n
= lim n→∞
n 2
so by the lemma proven above, f (n) is not O(g(n)). (c) f (n) = 2n^ and g(n) = nlog^ n. This ratio is more difficult to show goes to infinity than most of the others. We can rephrase the ratio f g^ ((nn)) as 2n−log^2 n^ log^ n; it then suffices to show that n − log 2 n log n gets arbitrarily large as n gets very large. We can do so by looking at n = 2s for some s; then, since log n < log 2 n, it follows that n − log 2 n log n > 2 s^ − s^2. We know 2s^ is not O(s^2 ) (using an argument similar to the one presented in part (d) of this problem, so 2s^ will eventually exceed any multiple of s^2 , so for any k, 2s^ − s^2 will eventually exceed (k − 1)s^2 , which can be arbitrarily large, so lims→∞ 2 s^ − s^2 = ∞, and thus limn→∞ f g^ ((nn)) = ∞.
(d) f (n) = 3n^ and g(n) = n^3. We use L’Hˆopital’s rule several times on the indeterminate form ∞∞ :
lim n→∞
f (n) g(n)
= lim n→∞
3 n n^3
= lim n→∞
(ln 3)3n 3 n^2
= lim n→∞
(ln 3)^23 n 6 n
= lim n→∞
(ln 3)^33 n 6
so by the lemma proven above, f (n) is not O(g(n)).
4.2.10. (a) Show that x^2 k can be computed using k multiplications. We can get x^2 by multiplying x · x; armed with that knowledge, we can get x^4 by multiplying x^2 · x^2 ; then we can get x^8 from x^4 · x^4. An inductive argument serves to upgrade our demonstration to a proof: The base case k = 0 is trivial: we already have x, so we get x^2 0 = x^1 with no multiplications at all. If we assume x^2 k can be calculated in k multiplications, we verify the inductive step that we can get x^2 (k+1) in k + 1 multiplications like so: find x^2 k (doable in k multiplications by our inductive hypothesis), and then perform the multiplication x^2 k · x^2 k to get
x^2 k^ )^2 = x^2 ·^2 k = x^2 k+ , so, having done k multiplications and then a final multiplication, we have performed k + 1 multiplications total. (b) Describe an O(log n) algorithm for computing xn. We saw above that we can get x raised to power-of-two powers very efficiently; thus, our best bet is to decompose n efficiently into powers of 2. So, for instance, to calculate x^13 , the most efficient algorithm would be to calculate x^2 , x^4 , and x^8 , then compute the product x · x^4 · x^8. Our choice of exponents is motivated by the binary representation of 13, which encodes the most compact way of representing 13 as a sum of powers of 2 (i.e. 13 = 1101 2 = 8 + 4 + 1). Eahc operation involved in this process (calculating a binary representation for n, raising x to the 2blog^2 nc power, and multiplying fewer than blog 2 nc numbers together) is completable in O(log n) time, so the entire process is O(log n). However, to make it more explicit, here is an actual, highly optimized algorithm, which is cannibalized from our algorithm in problem 4.1.10: Input: integer n, number x set y to x; set p to 1; repeat if n is odd then set p to p · y and set n to n − 1; set y to y · y; set n to n 2 ; until n = 0 ; output p;
We can see how this would work by looking at, for instance, entering n = 42:
And we know, by the inequality produced above, that:
rk− 1 < n − m rk− 2 < m − rk− 1 rk− 3 < rk− 1 − rk− 2 .. . ri− 2 < ri − ri− 1 .. . 0 < r 2 − r 1
Rearraging the above, we see that each ri > ri− 1 + ri− 2. Since we assume r 1 and r 2 are nonzero (since the algorithm would terminate earlier if they were, we have r 1 > 1, r 2 > 1, and the recurrence-inequality ri > ri− 1 + ri− 2. Since each of these inequalities errs on the side of making things larger than their corresponding Fibonacci number, it follows that ri > fi. Therefore, if the Euclidean algorithm takes k steps to terminate, it must be the case that the first remainder (rk in the above notation) exceeds fk, which is only possible if m > fk. The contrapositive of this statement guarantees that if m < fk (or if n < fk), then the Euclidean algorithm will terminate in fewer than k steps.
We know from solving the Fibonacci recurrence that fk grows approximately as
1+ √ 5 2
)k ;
we can in fact guarantee that fk <
1+ √ 5 2
)k , so the Euclidean algorithm takes only k
iterations if n <
1+ √ 5 2
)k ; the resultant value of k is
log n log 1+
√ 5 2
, which is O(log n).