You are on page 1of 5

Analysis of Algorithms

C
Recurrences

Recurrence and composition of recursive functions are about all you


need for computation.
Of course, you need ways to communicate with the external world.
and this has the potential to lead to unforeseen problems: side ef-
fects. These can be encapsulated in an algebraic structure, called a
monad, but this beyond the scope of the class and this exam.

Outcome
Students are able to derive a recurrence and initial conditions given
pseudo-code, or the name of a known algorithm.

1. ( What are the recurrence and initial conditions for binary


search?
Answer:

2. ( What are the recurrence and initial conditions for the merge-
sort?

3. (
Multi-precision multiplication of integers. Suppose X and Y
are two n-bit integers written in binary notation. Assume n is
even and break X and Y into two (n/2)-bit integers A, B, and C,
D, respectively, so

X = A2n/2 + B and Y = C2n/2 + D

The product of X and Y can be calculated as

XY = AC2n + (( A − B)( D − C ) + AC + BD )2n/2 + BD

How many multiplications are needed to evaluate XY and what


is the bit-count of the factors in these products? What is the big-O
term to complete the evaluation of XY with additions, shift, and
other necessary operations.
Answer:
analysis of algorithms 2

Outcome
Students are able to solve various recurrences.

1. ( Show how to solve the recurrence for binary search.


Answer:

2. ( Show how to solve the recurrence for merge-sort. Answer:

3. ( Show how to solve the recurrence for multiplication of


integers.

Greedy Algorithms

Suppose you are given a connected graph G where each edge has a
non-negative weight.

Outcome
Students know vocabulary.

1. ( What does it mean to say that graph G is connected?


Answer:

2. ( What is spanning tree T for G?


Answer:

3. ( What is a minimal spanning tree T for G?


Answer:

Outcome
Students can derive or locate algorithms that correctly compute solu-
tions to a problems.

1. ( State the minimal spanning tree problem and provide


code that implements a correct solution to the problem.
Answer:
analysis of algorithms 3

Outcome
Students can compile, debug, and profile their code. to solutions to
problems.

1. ( Debug, Compile, and execute your minimal spanning tree


code to collect profiling data.
Answer:

2. Provide some evidence that you completed the first step.


Answer:
analysis of algorithms 4

Dynamic Program Algorithms

The 0 − 1 knapsack problem.


(10 pts) Given weights hw0 , w1 , . . . , wn−1 i of provisions and their
profits h p0 , p1 , . . . , pn−1 i, a knapsack capacity C, and a bit vector
hb0 , b1 , . . . , bn−1 i, which specifies whether or not a provision is or is
not in the knapsack.
For 0 ≤ m ≤ C and 0 ≤ j < n, define P( j, m) to be the profit of
the optimal solution to the 0 − 1 knapsack problem using provisions
0, . . . j and knapsack capacity m.

1. ( Explain the how these recurrences are helpful in framing a


solution to the problem.

P( j, m) = P( j − 1, m) if w j > m
P( j, m) = max P( j − 1, m), p j + P( j − 1, m − w j ) if w j ≤ m

2. ( Explain the initial conditions

P(0, m) = p0 if w0 ≤ m
P(0, m) = 0 if w0 > m

3. ( Let T(n) be the time complexity to calculate P(n − 1, m)


recursively. Argue that the time complexity is modeled by the
recurrence T(n) = 2T(n − 1) + c, solve this recurrence. describe the
rate of growth of the T(n).
Consider an instance of the 0 − 1 knapsack problem with 6 provi-
sions that have weights: 2, 3, 5, 8, 13, and 16. With corresponding
profits 1, 2, 3, 5, 7, and 10. And a knapsack capacity C = 30.

4. ( Show how to use the recurrences to fill the missing values in a


dynamic programming table below.

j 0 1 2 3 4 5 ... 30
0 0 0 1 1 1 1 ... 1
1 0 0 1 2 3 3 ... 3
2 0 0 1 2 2 3 ... 6
3 0 0 1 2 2 3 ... 11
4 0 0 1 2 2 3 ... 17
5 0 0 1 2 2 3 ... 18

5. (5 Given n, the number of provisions, and C the knapsack


analysis of algorithms 5

capacity, what is the time complexity to complete the dynamic


programming table and solve an instance of the problem
Answer:

6. ( Explain why this dynamic programming approach does not


lead to a polynomial-time algorithm for the 0 − 1 knapsack
problem. (Hint: the capacity C could be exponentially large in n)

7. ( It can be shown that the 0 − 1 knapsack problem is NP-complete


What does this mean?

You might also like