Cutting Rod Problem Mini Project Report
Cutting Rod Problem Mini Project Report
12202040503008
Sample Output:
Maximum Profit: PROFIT
Approach:
Since dynamic programming is mainly an optimization-over-plain recursion, we can use it to
optimize the above exponential time algorithm. The idea is to store the results of subproblems so
that we do not have to re-compute them when they are needed.
The possible range of length, of any sub-rod, is from 0 to 5. A sub-rod of a length within
this range can be made.
We have 4 different prices of different-length sub-rods
1
Mufaddal Challawala
12202040503008
In the first row, we only have the price 2 at a length of 1 in our array.
At index [0,1], putting a cut at 1 would give a profit of 2. No cut would give a profit of 2.
At index of [0,2], putting a cut at 1 would give a profit of 4. No cut would give a profit of
4. Hence, 4 is the better option.
Do this for the whole first row.
Your grid should look like this now:
You may have observed that we’re deciding, at every step, whether the newly added cut mark (i+1)
increases the profit of the given rod j, or not. We decide this by taking the maximum of 2 things:
The maximum profit of the length before the cut mark was made (i.e., the value at [i-1,j] ).
This is a case where we don’t include the newly added cut mark.
Price [i] + the maximum profit at length j-i-1 for the current array of prices (i.e., the value
at [i, j-i-1]) would have already been computed before this computation. This is a case
where we use the new cut mark for its effect on the profit.
So, the general expression used for filling the grid is:
2
Mufaddal Challawala
12202040503008
Take a look at the example below; it shows how to use the expression to fill values in a grid:
Algorithm:
1) Take input numbers
2) Fill the cell with maximum profit using formula T[i,j] = max(T[i,j],price[i] + T[i,j-i-1])
3) Take the cell extreme bottom-right as maximum profit.
Implementation:
#include<stdio.h>
#include<limits.h>
3
Mufaddal Challawala
12202040503008
return val[n];
}
int main()
{
int arr[] = {1, 5, 8, 9, 10, 17, 17, 20};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Maximum Obtainable Value is %d", cutRod(arr, size));
getchar();
return 0;
}
OUTPUT:
Maximum Obtainable Value is 22