15.1 Rod cutting
15.1-1
Show that equation follows from equation and the initial condition .
- For , this holds since .
-
For , substituting into the recurrence, we have
15.1-2
Show, by means of a counterexample, that the following "greedy" strategy does not always determine an optimal way to cut rods. Define the density of a rod of length to be , that is, its value per inch. The greedy strategy for a rod of length cuts off a first piece of length , where , having maximum density. It then continues by applying the greedy strategy to the remaining piece of length .
The counterexample:
15.1-3
Consider a modification of the rod-cutting problem in which, in addition to a price for each rod, each cut incurs a fixed cost of . The revenue associated with a solution is now the sum of the prices of the pieces minus the costs of making the cuts. Give a dynamic-programming algorithm to solve this modified problem.
We can modify algorithm from section 15.1 as follows:
MODIFIED-CUT-ROD(p, n, c)
let r[0..n] be a new array
r[0] = 0
for j = 1 to n
q = p[j]
for i = 1 to j - 1
q = max(q, p[i] + r[j - i] - c)
r[j] = q
return r[n]
We need to account for cost on every iteration of the loop in lines 5-6 but the last one, when (no cuts).
We make the loop run to instead of , make sure is subtracted from the candidate revenue in line 6, then pick the greater of current best revenue and (no cuts) in line 7.
15.1-4
Modify to return not only the value but the actual solution, too.
MEMOIZED-CUT-ROD(p, n)
let r[0..n] and s[0..n] be new arrays
for i = 0 to n
r[i] = -โ
(val, s) = MEMOIZED-CUT-ROD-AUX(p, n, r, s)
print "The optimal value is" val "and the cuts are at" s
j = n
while j > 0
print s[j]
j = j - s[j]
MEMOIZED-CUT-ROD-AUX(p, n, r, s)
if r[n] โฅ 0
return r[n]
if n == 0
q = 0
else q = -โ
for i = 1 to n
(val, s) = MEMOIZED-CUT-ROD-AUX(p, n - i, r, s)
if q < p[i] + val
q = p[i] + val
s[n] = i
r[n] = q
return (q, s)
15.1-5
The Fibonacci numbers are defined by recurrence . Give an -time dynamic-programming algorithm to compute the nth Fibonacci number. Draw the subproblem graph. How many vertices and edges are in the graph?
FIBONACCI(n)
let fib[0..n] be a new array
fib[0] = 1
fib[1] = 1
for i = 2 to n
fib[i] = fib[i - 1] + fib[i - 2]
return fib[n]
There are vertices in the subproblem graph, i.e., .
- For , each has leaving edge.
- For , each has leaving edges.
Thus, there are edges in the subproblem graph.