Levitin
Levitin
Exercises 2.3
1. Compute the following sums.
a. 1 + 3 + 5 + 7 + . . . + 999
b. 2 + 4 + 8 + 16 + . . . + 1024
n+1 n+1 n−1
c. i=3 1 d. i e. i(i + 1)
n +1
ni=3 n i=0
n
i=1 1/i(i + 1)
f. j
j =1 3 g. i=1 j =1 ij h.
2. Find the order of growth of the following sums. Use the (g(n)) notation with
the simplest function g(n) possible.
n−1 2 2 n−1 2
a. i=0 (i +1) b. i=2 lg i
n n−1 i−1
i=1(i + 1)2 j =0 (i + j )
c. i−1 d. i=0
3. The sample variance of n measurements x1, . . . , xn can be computed as either
n n
i=1(xi − x̄)
2 xi
where x̄ = i=1
n−1 n
or
n n
2
i=1 xi −( 2
i=1 xi ) /n
.
n−1
Find and compare the number of divisions, multiplications, and additions/
subtractions (additions and subtractions are usually bunched together) that
are required for computing the variance according to each of these formulas.
4. Consider the following algorithm.
ALGORITHM Mystery(n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S←S+i∗i
return S
Exercises 2.4
1. Solve the following recurrence relations.
a. x(n) = x(n − 1) + 5 for n > 1, x(1) = 0
b. x(n) = 3x(n − 1) for n > 1, x(1) = 4
c. x(n) = x(n − 1) + n for n > 0, x(0) = 0
d. x(n) = x(n/2) + n for n > 1, x(1) = 1 (solve for n = 2k )
e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k )
2. Set up and solve a recurrence relation for the number of calls made by F (n),
the recursive algorithm for computing n!.
3. Consider the following recursive algorithm for computing the sum of the first
n cubes: S(n) = 13 + 23 + . . . + n3.
2.4 Mathematical Analysis of Recursive Algorithms 77
ALGORITHM S(n)
//Input: A positive integer n
//Output: The sum of the first n cubes
if n = 1 return 1
else return S(n − 1) + n ∗ n ∗ n
a. Set up and solve a recurrence relation for the number of times the algo-
rithm’s basic operation is executed.
b. How does this algorithm compare with the straightforward nonrecursive
algorithm for computing this sum?
4. Consider the following recursive algorithm.
ALGORITHM Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1
a. Set up a recurrence relation for this function’s values and solve it to deter-
mine what this algorithm computes.
b. Set up a recurrence relation for the number of multiplications made by this
algorithm and solve it.
c. Set up a recurrence relation for the number of additions/subtractions made
by this algorithm and solve it.
5. Tower of Hanoi
a. In the original version of the Tower of Hanoi puzzle, as it was published in
the 1890s by Édouard Lucas, a French mathematician, the world will end
after 64 disks have been moved from a mystical Tower of Brahma. Estimate
the number of years it will take if monks could move one disk per minute.
(Assume that monks do not eat, sleep, or die.)
b. How many moves are made by the ith largest disk (1 ≤ i ≤ n) in this
algorithm?
c. Find a nonrecursive algorithm for the Tower of Hanoi puzzle and imple-
ment it in the language of your choice.
6. Restricted Tower of Hanoi Consider the version of the Tower of Hanoi
puzzle in which n disks have to be moved from peg A to peg C using peg
B so that any move should either place a disk on peg B or move a disk from
that peg. (Of course, the prohibition of placing a larger disk on top of a smaller
one remains in place, too.) Design a recursive algorithm for this problem and
find the number of moves made by it.