Rod cutting - Using Dynamic Programming

1.Problem Description

Serling Enterprises buys long steel rods and cuts them into shorter rods, which it then sells. Each cut is free. The management of Serling Enterprises wants to know the best way to cut up the rods.
We assume that we know, for i = 1,2,…, the price pi in dollars that Serling Enterprises charges for a rod of length i inches. Rod lengths are always an integral number of inches. Figure 1 gives a sample price table.

length i 1 2 3 4 5 6 7 8 9 10 price pi 1 5 8 9 10 17 17 20 24 30

Figrure 1:A sample price table for rods. Each rod of length i inches earns the company pi dollars of revenue.




Rod cutting description
Figure 2:The 8 possible ways of cutting up a rod of length 4. Above each piece is the value of that piece, according to the sample price chart of Figure 1. The optimal strategy is part (c)—cutting the rod into two pieces of length 2—which has total value 10.

So can we find a strategy to cut a rod so that we can earn most?

2.Problem Analyse

We can easily find a exhaustive way to list all the possibilities and find a optimal one. For a rod of length 10 inches, there are 210 possibilities in total. That’s really a big number! The algorithm grows in exponential way. So is there a algorithm which we can use to solve this problem using less time? Yes, dynamic programming is what we are looking for.

2.1 Optimal Substructure

Before we introduce dynamic programming, we should know what is optimal substructure.

If an optimal solution cuts the rod into k pieces, for some 1kn , then an optimal decomposition

n=i1+i2++ik

of the rod into pieces of lengths i1,i2,,ik provides maximum corresponding revenue
rn=pi1+pi2++pik

More generally, we can frame the values rn for n1 in terms of optimal revenues from shorter rods:
rn=max(pn,r1+rn1,r2+rn2,,rn1+r1)

The first argument, pn ,corresponds to making no cuts at all and selling the rod of length n as is. The other n1 arguments to max correspond to the maximum revenue obtained by making an initial cut of the rod into two pieces of size i and ni, for each i=1,2,,n1 , and then optimally cutting up those pieces further, obtaining revenues ri and rni from those two pieces. Since we don’t know ahead of time which value of i optimizes revenue, we have to consider all possible values for i and pick the one that maximizes revenue. We also have the option of picking no i at all if we can obtain more revenue by selling the rod uncut.

Note that to solve the original problem of size n, we solve smaller problems of the same type, but of smaller sizes. Once we make the first cut, we may consider the two pieces as independent instances of the rod-cutting problem. The overall optimal solution incorporates optimal solutions to the two related subproblems, maximizing revenue from each of those two pieces. We say that the rod-cutting problem exhibits optimal substructure: optimal solutions to a problem incorporate optimal solutions to related subproblems, which we may solve independently.

2.2 Using dynamic programming for optimal rod cutting

We now show how to convert CUT-ROD into an efficient algorithm, using dynamic programming.

Dynamic programming thus uses additional memory to save computation time; it serves an example of a time-memory trade-off. The savings may be dramatic: an exponential-time solution may be transformed into a polynomial-time solution.There are usually two equivalent ways to implement a dynamic-programming approach: top-down with memoization and bottom-up method. We will only introduce bottom-up method since it is more efficient.

Bottom-up method typically depends on some natural notion of the “size” of a subproblem, such that solving any particular subproblem depends only on solving “smaller” subproblems. We sort the subproblems by size and solve them in size order, smallest first. When solving a particular subproblem, we have already solved all of the smaller subproblems its solution depends upon, and we have saved their solutions. We solve each subproblem only once, and when we first see it, we have already solved all of its prerequisite subproblems.

code

For the bottom-up dynamic-programming approach, BOTTOM-UP-CUT-ROD uses the natural ordering of the subproblems: a problem of size i is “smaller” than a subproblem of size j if i<j . Thus, the procedure solves subproblems of sizes j=0,1,,n , in that order.

3.Code

double rodCutting(unsigned n, vector<double> &p) {
    vector<double> cost(n, -1);
    double max = -1;
    for (auto i = 0; i < n; ++i) {
        for (auto j = 0; j <= i; ++j) {
            if (j == 0)
                max = p[i];
            else {
                if (cost[j - 1] + cost[i - j] > max)
                    max = cost[j - 1] + cost[i - j];
            }
        }
        cost[i] = max;
        max = -1;
    }   
    return cost[n - 1];
}

References

[1] Introduction to Algorithms(Third Edition), page 360 to 370.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值