Skip to content

15.1 Rod cutting

15.1-1

Show that equation (15.4)\text{(15.4)} follows from equation (15.3)\text{(15.3)} and the initial condition T(0)=1T(0) = 1.

  • For n=0n = 0, this holds since 20=12^0 = 1.
  • For n>0n > 0, substituting into the recurrence, we have

    T(n)=1+โˆ‘j=0nโˆ’12j=1+(2nโˆ’1)=2n. \begin{aligned} T(n) & = 1 + \sum_{j = 0}^{n - 1} 2^j \\ & = 1 + (2^n - 1) \\ & = 2^n. \end{aligned}

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 ii to be pi/ip_i / i, that is, its value per inch. The greedy strategy for a rod of length nn cuts off a first piece of length ii, where 1โ‰คiโ‰คn1 \le i \le n, having maximum density. It then continues by applying the greedy strategy to the remaining piece of length nโˆ’in - i.

The counterexample:

length i1234price pi1203336pi/i110119 \begin{array}{c|cccc} \text{length $i$} & 1 & 2 & 3 & 4 \\ \hline \text{price $p_i$} & 1 & 20 & 33 & 36 \\ p_i / i & 1 & 10 & 11 & 9 \end{array}

15.1-3

Consider a modification of the rod-cutting problem in which, in addition to a price pip_i for each rod, each cut incurs a fixed cost of cc. 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 BOTTOM-UP-CUT-ROD\text{BOTTOM-UP-CUT-ROD} 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 cc on every iteration of the loop in lines 5-6 but the last one, when i=ji = j (no cuts).

We make the loop run to jโˆ’1j - 1 instead of jj, make sure cc is subtracted from the candidate revenue in line 6, then pick the greater of current best revenue qq and p[j]p[j] (no cuts) in line 7.

15.1-4

Modify MEMOIZED-CUT-ROD\text{MEMOIZED-CUT-ROD} 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 (3.22)\text{(3.22)}. Give an O(n)O(n)-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 n+1n + 1 vertices in the subproblem graph, i.e., v0,v1,โ€ฆ,vnv_0, v_1, \dots, v_n.

  • For v0,v1v_0, v_1, each has 00 leaving edge.
  • For v2,v3,โ€ฆ,vnv_2, v_3, \dots, v_n, each has 22 leaving edges.

Thus, there are 2nโˆ’22n - 2 edges in the subproblem graph.