Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020
Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020
Solution:
Each of these recurrences can be solved by the method of backward substitutions.
Question 2: Set up and solve a recurrence relation for the number of calls made by F(n), the recursive
algorithm for computing n!.
Solution:
The recurrence relation in question is almost identical to the recurrence relation for the number of
multiplications, which is set up and solved in the section 2.4 of the text book.
Page 1 of 4
Question 3: Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) =
13 + 23 + ... + n3.
a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation
is executed.
b. How does this algorithm compare with the straightforward nonrecursive algorithm for
computing this function?
Solution:
a. The question is similar to that about the efficiency of the recursive algorithm for computing n!.
b. Write a pseudocode for the nonrecursive algorithm and determine its efficiency.
a. Set up a recurrence relation for this function’s values and solve it to determine 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.
Solution:
a. Note that you are asked here about a recurrence for the function’s values, not about a recurrence for
the number of times its operation is executed. Just follow the pseudocode to set it up. It is easier to
solve this recurrence by forward substitutions (see Appendix B).
Page 2 of 4
Question 5: In the original version of the Tower of Hanoi puzzle, as it was published by Edouard Lucas, a
French mathematician, in the 1890s, the world will end after 64 disks have been moved from a mystical
Tower of Brahma.
a. 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. Design a nonrecursive algorithm for the Tower of Hanoi puzzle.
Solution:
a. Use the formula for the number of disk moves derived in the section.
b. Solve the problem for 3 disks to investigate the number of moves made by each of the disks. Then
generalize the observations and prove their validity for the general case of n disks.
c. If you fail, do not feel discouraged: though a nonrecursive algorithm for this problems is not
complicated, it is not easy to discover. As a consolation, find a solution on the Web.
Question 6: Consider algorithm for solving the problem by recursively dividing an array into two halves:
a. Set up a recurrence relation for the algorithm’s basic operation and solve it.
b. Can you suggest an algorithm for the problem the given algorithm solve that would be more
efficient?
Solution:
Page 3 of 4
a. What does this algorithm compute?
b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.
Solution:
Question 8: Consider the following algorithm to check whether the graph defined by its adjacency
matrix is complete.
Solution:
Page 4 of 4