Rod Cutting Problem using Dynamic Programming

This is a C++ Program that Solves Rod Cutting Problem using Dynamic Programming technique.

Problem Description

Given a rod of size n and values of various sizes of rod. The problem is to cut the rod in such a way that the sum of values of the pieces is maximum.

Problem Solution

The problem can be solved by calculating the maximum attainable value of rod by cutting in various pieces in a bottom up fashion by first calculating for smaller value of n and then using these values to calculate higher values of n.
The time complexity of this solution is O(n^2).

Expected Input and Output

Case-1:

n=5
value[]= 2 3 7 8 10 for sizes 1,2,3,4,5 respectively	 	
Expected result=11 by cutting the rod in pieces of sizes 1,1 and 3

Case-2:

advertisement
advertisement
n=7	
value[]= 2 7 8 25 17 28 30
 
Expected result=34 by cutting the rod in pieces of sizes 1,2 and 4
Program/Source Code

Here is source code of the C++ Program to Solve Rod Cutting Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. #include<iostream>
  3. #include<climits>
  4.  
  5. using namespace std;
  6.  
  7. int rodCutting(int n, int value[])
  8. {
  9.     int i,j;
  10.  
  11.     //we will calculate the maximum attainable value of rod in a bottom up fashion by first calculating for smaller value of n and then using these values to calculate higher values of n
  12.  
  13.     //create an array to store the results
  14.     int result[n+1];
  15.  
  16.     //result[i]=maximum attainable value of rod of size i
  17.  
  18.     //initialization
  19.     result[0]=0;
  20.  
  21.     //in every iteration, find the result for rod of size i
  22.     for(i=1;i<=n;i++)
  23.     {
  24.         result[i]=INT_MIN;
  25.  
  26.         //try to cut the rod of length i into various values of j and select the one which gives the maximum value
  27.         for(j=0;j<i;j++)
  28.         {
  29.             result[i]=max(result[i],value[j]+result[i-(j+1)]);
  30.         }
  31.     }
  32.  
  33.  
  34.     return result[n];
  35. }
  36.  
  37. int main()
  38. {
  39.     int n;
  40.     cout<<"Enter the length of the rod"<<endl;
  41.     cin>>n;
  42.  
  43.     int value[n];
  44.     //value[i]=value of rod of size i+1
  45.     //value[0]=value of rod of size 1
  46.     //value[1]=value of rod of size 2
  47.  
  48.     cout<<"Enter the values of pieces of rod of all size"<<endl;
  49.  
  50.     for(int i=0;i<n;i++)
  51.         cin>>value[i];
  52.  
  53.     cout<<"Maximum obtainable value by cutting up the rod in many pieces are"<<endl;
  54.     cout<<rodCutting(n,value);
  55.  
  56.     cout<<endl;
  57.     return 0;
  58. }
Program Explanation

In the main function, we ask the user to input the length of rod and values of pieces of rod of all size. We pass these values to the function rodCutting as parameters. This function will calculate the expected result and return it. The returned value will be displayed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Case-1:	 	 
$ g++ rod_cutting.cpp	 	
$ ./a.out
Enter the length of the rod	 
Enter the length of the rod
7
Enter the values of pieces of rod of all size	
2
7
8
25
17
28
30
Maximum obtainable value by cutting up the rod	
34

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.