Cutting a Rod
2014-02-04 22:01
232 查看
转自出处
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the
rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1)
The naive solution for this problem is to generate all configurations of different pieces and find the highest priced configuration. This solution is exponential in term of time complexity. Let us see how this problem possesses both important properties
of a Dynamic Programming (DP) Problem and can efficiently solved using Dynamic Programming.
1) Optimal Substructure:
We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.
Let cutRoad(n) be the required (best possible price) value for a rod of lenght n. cutRod(n) can be written as following.
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
2) Overlapping Subproblems
Following is simple recursive implementation of the Rod Cutting problem. The implementation simply follows the recursive structure mentioned above.
Output:
Considering the above implementation, following is recursion tree for a Rod of length 4.
In the above partial recursion tree, cR(2) is being solved twice. We can see that there are many subproblems which are solved again and again. Since same suproblems are called again, this problem has Overlapping Subprolems property. So the Rod Cutting problem
has both properties (see this andthis)
of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems
can be avoided by constructing a temporary array val[] in bottom up manner.
Output:
Time Complexity of the above implementation is O(n^2) which is much better than the worst case time complexity of Naive Recursive implementation.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Dynamic Programming | Set 13 (Cutting a Rod)
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of therod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
length | 1 2 3 4 5 6 7 8 -------------------------------------------- price | 1 5 8 9 10 17 17 20
And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1)
length | 1 2 3 4 5 6 7 8 -------------------------------------------- price | 3 5 8 9 10 17 17 20
The naive solution for this problem is to generate all configurations of different pieces and find the highest priced configuration. This solution is exponential in term of time complexity. Let us see how this problem possesses both important properties
of a Dynamic Programming (DP) Problem and can efficiently solved using Dynamic Programming.
1) Optimal Substructure:
We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.
Let cutRoad(n) be the required (best possible price) value for a rod of lenght n. cutRod(n) can be written as following.
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
2) Overlapping Subproblems
Following is simple recursive implementation of the Rod Cutting problem. The implementation simply follows the recursive structure mentioned above.
Maximum Obtainable Value is 22
Considering the above implementation, following is recursion tree for a Rod of length 4.
cR() ---> cutRod() cR(4) / / \ \ / / \ \ cR(3) cR(2) cR(1) cR(0) / | \ / \ | / | \ / \ | cR(2) cR(1) cR(0) cR(1) cR(0) cR(0) / \ | | / \ | | cR(1) cR(0) cR(0) cR(0)
In the above partial recursion tree, cR(2) is being solved twice. We can see that there are many subproblems which are solved again and again. Since same suproblems are called again, this problem has Overlapping Subprolems property. So the Rod Cutting problem
has both properties (see this andthis)
of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems
can be avoided by constructing a temporary array val[] in bottom up manner.
Maximum Obtainable Value is 22
Time Complexity of the above implementation is O(n^2) which is much better than the worst case time complexity of Naive Recursive implementation.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
相关文章推荐
- OCP-1Z0-053-200题-167题-526
- redhat9不能上网的解决办法
- 浙江大学ACM俱乐部 1016:计算利息
- OCP-1Z0-053-200题-168题-528
- OpenRisc-64-添加VGA和I2C模块到ORPSoC并测试验证
- 便捷编程-Xcode常用第三方插件 (随时更新)
- 线性代数系统软件包
- OCP-1Z0-053-200题-169题-531
- linux内核0.12之MINIX文件系统初探
- usaco 1.2 Name That Number(数字字母转化)
- 计算特征值问题的C软件包
- Rational Rose启动和退出时报错
- 寒假折腾Archlinux的一些经验(新手向)--桌面配置篇
- 原型模式与深浅拷贝
- 浙江大学ACM俱乐部 1015:构建矩阵
- Hibernate 操作数据库模板代码
- XML实体使用
- 浙江大学ACM俱乐部 1014:当总统
- javascript闭包问题
- 结构体