Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Rod Cutting Problem

Learning Objective
By the end of this module, the learner will be able to:

1. Identiy the application of memoization in solving a Dynamic


Programming problem.
2. Apply the top-down and bottom-up way of solving a Dynamic
Programming problem.
3. Learn about the techniques to solve the Rod-Cutting problem
using the various aspects of Dynamic Programming.

Time Required
2 hours (approx)

Introduction
A company buys long steel rods and cuts them into shorter rods before
they sell. We assume that each cut is free. Let the price of a rod of
length i is of pi. So, given a rod of length n inches and a table of prices
pi for i = 1, 2, ..n. Below is a sample price table. The Company wants to
know the optimal way to break a rod of certain length n so that the
total price of all pieces is the maximum(optimal) price among all the
possible prices. There will be only one optimal value, but there might
be many optimal solutions. Our goal is to determine one such optimal
solution(rn) to this problem with the optimal value(maximum value in
this case). If price of the rod is large enough, an optimal solution may
require no cutting at all.
Analysis of the problem
We will start to analyze the problem with n=4. There are as many as 8
ways to cut the rod of 4 inches in length, including the way with no cuts
at all. For a rod of length 4 inches, we can cut it at 1 inch distance from
left, 2 inch distance from left and 3 inch distance from left. Below is a
picture that shows all the 8 ways to cut the rod of length 4.
Let us denote a decomposition into pieces using ordinary additive
notation: For example, 7 = 2 + 2 + 3 represents that a rod of length is
decomposed into two rods of length 2 and one of length 3. If an
optimal solution cuts the rods into k pieces, for some then
the optimal decomposition
n = i1 + i2 + ... + ik.
of the rod into pieces of length i1, i2, ..., ik provides maximum
corresponding revenue
rn = pi1 + pi2 + ... + pik.
For our sample problem, the optimal revenue figures ri for i = 1, 2, ...,
10 with corresponding optimal decompositions are shown below.
r1 = 1 [ Optimal Solution : 1 = 1 (no cuts) ]
r2 = 5 [ Optimal Solution : 2 = 2 (no cuts) ]
r3 = 8 [ Optimal Solution : 3 = 3 (no cuts) ]
r4 = 10 [ Optimal Solution : 4 = 2 + 2 ]
r5 = 13 [ Optimal Solution: 5 = 2 + 3 ]
r6 = 17 [ Optimal Solution: 6 = 6 (no cuts) ]
r7 = 18 [ Optimal Solutions : 7 = 1 + 6 or 7 = 2 + 2 + 3 ]
r8 = 22 [ Optimal Solution : 8 = 2 + 6 ]
r9 = 25 [ Optimal Solution : 9 = 3 + 6 ]
r10 = 30 [ Optimal Solution : 10 = 10 ( no cuts ) ]

First Solution to the problem


As we can see from the above calculations, we can frame the values
rn for in terms of optimal revenues for shorter rods:
rn = max(pn, r1 + rn-1, r2 + rn-2, ..., rn-1 + r1).
The first argument pn to max corresponds to making no cuts at all and
selling the length of rod n as it is. The other n-1 arguments
to max correspond to the maximum revenue obtained by making an
initial cut of rod into two pieces of sizes i and n-i, for each i = 1, 2,..., n-
1, and then optimally cutting up those pieces further.The revenues
ri and rn-i are obtained from those two pieces. Here the original problem
for a rod of size n is reduced to smaller subproblems of same type. The
overall optimal solution incorporates the optimal solution to the two
related subproblems maximizing the revenue from each of those two
pieces. This property is called optimal substructure: Optimal solutions
of problem include optimal solutions to subproblems of smaller size,
which can be solved independently.
As always our question while thinking of a solution to a problem should
be: Can we do better than this? Here instead of the optimal solution
having two subproblems of sizes i and n-i, we can re-frame the
recurrence relation as below to embody optimal solution of only one
subproblem.
rn = max 1 <= i <= n (pi + rn-i)
The top-down recursive implementation of the recurrence relation is
below:
Dynamic Programming Solution
We need to somehow make it possible to get the solution by solving
each subproblem only once. The main trick we can employ is to store
the solution to a subproblem after it is evaluated, use it whenever it is
required later in the process. This method might take more space, but
sometimes converts an exponential-time solution to a polynomial-time
solution, provided the number of distinct subproblems involved is
polynomial in the input size. So this kind of a solution is a time-memory
trade off.
The solution in dynamic programming can be based on one of the
approaches:
1. Top-down with Memoization: The procedure of computation follows
the recurrence relation. Once the solution to a subproblem is obtained,
it is stored for future use. The procedure checks if it has already solved
the subproblem. If done, the procedure just returns the saved solution
saving the branch of computation; if not done, it computes the solution
for the subproblem and stores its value. The recursive procedure is
called memoized( taken the word 'memo' which means to remember or
note down), which means the procedure remembers what it has
computed previously.
2. Bottom-up method: The subproblems are sorted based on their size
and are solved in the increasing order of their size. When solving a
subproblem, we have already solved all its subproblems and saved their
solutions. So the notion of size here is, solving a subproblem of a
particular size depends only on solving smaller subproblems. So in this
procedure, we solve each subproblem only once. Also when we first see
a problem, all its pre-requisites are already solved.
The solution presented below is based on the first technique "Top-
down with Memoization":
The first routine 'MEMOIZED-CUT-ROD' takes two input parameters:
price table, size of the rod n. This routine initializes a array r[1..n] with
all values as negative infinity. This array is used to store the computed
solutions for each size of the rod in the range of 1 to n.
The second routine is a helper function to the first routine. This
function first sees if the value is already computed. If so, the value is
used and computation proceeds. If not, the value is calculated and
stored for future use.This is the routine that performs memoization.

You might also like