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)

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

A 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.

[ad type=”banner”]
  • 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}

  • Overlapping Subproblems

Following is simple recursive implementation of the Rod Cutting problem. The implementation simply follows the recursive structure mentioned above.

[pastacode lang=”java” manual=”%2F%2F%20%2F%2F%20A%20Naive%20recursive%20solution%20for%20Rod%20cutting%20problem%0Aclass%20RodCutting%0A%7B%0A%20%20%20%20%2F*%20Returns%20the%20best%20obtainable%20price%20for%20a%20rod%20of%20length%0A%20%20%20%20%20%20%20n%20and%20price%5B%5D%20as%20prices%20of%20different%20pieces%20*%2F%0A%20%20%20%20static%20int%20cutRod(int%20price%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20int%20max_val%20%3D%20Integer.MIN_VALUE%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Recursively%20cut%20the%20rod%20in%20different%20pieces%20and%0A%20%20%20%20%20%20%20%20%2F%2F%20compare%20different%20configurations%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20max_val%20%3D%20Math.max(max_val%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20price%5Bi%5D%20%2B%20cutRod(price%2C%20n-i-1))%3B%0A%20%0A%20%20%20%20%20%20%20%20return%20max_val%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20new%20int%5B%5D%20%7B1%2C%205%2C%208%2C%209%2C%2010%2C%2017%2C%2017%2C%2020%7D%3B%0A%20%20%20%20%20%20%20%20int%20size%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Maximum%20Obtainable%20Value%20is%20%22%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cutRod(arr%2C%20size))%3B%0A%20%0A%20%20%20%20%7D%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Output:

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)
   /
 /
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 and this) 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.

[ad type=”banner”] [pastacode lang=”java” manual=”%2F%2F%20A%20Dynamic%20Programming%20solution%20for%20Rod%20cutting%20problem%0Aclass%20RodCutting%0A%7B%0A%20%20%20%20%2F*%20Returns%20the%20best%20obtainable%20price%20for%20a%20rod%20of%0A%20%20%20%20%20%20%20length%20n%20and%20price%5B%5D%20as%20prices%20of%20different%20pieces%20*%2F%0A%20%20%20%20static%20int%20cutRod(int%20price%5B%5D%2Cint%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20val%5B%5D%20%3D%20new%20int%5Bn%2B1%5D%3B%0A%20%20%20%20%20%20%20%20val%5B0%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Build%20the%20table%20val%5B%5D%20in%20bottom%20up%20manner%20and%20return%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20last%20entry%20from%20the%20table%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%3C%3Dn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20max_val%20%3D%20Integer.MIN_VALUE%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20i%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_val%20%3D%20Math.max(max_val%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20price%5Bj%5D%20%2B%20val%5Bi-j-1%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20val%5Bi%5D%20%3D%20max_val%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20return%20val%5Bn%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20new%20int%5B%5D%20%7B1%2C%205%2C%208%2C%209%2C%2010%2C%2017%2C%2017%2C%2020%7D%3B%0A%20%20%20%20%20%20%20%20int%20size%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Maximum%20Obtainable%20Value%20is%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cutRod(arr%2C%20size))%3B%0A%20%20%20%20%7D%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Output:

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.

[ad type=”banner”]