0% found this document useful (0 votes)
37 views4 pages

Cutting Rod Problem Mini Project Report

This document describes the rod cutting problem and provides an algorithm to solve it using dynamic programming. The problem is to cut a rod of length n into pieces that maximize the total price obtained. A dynamic programming approach is used, storing solutions to subproblems in a 2D table. The algorithm fills the table using the recurrence relation that the maximum price of a length is either cutting it or not. It returns the maximum price, which is the bottom-right of the table, in O(n^2) time and O(n) space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

Cutting Rod Problem Mini Project Report

This document describes the rod cutting problem and provides an algorithm to solve it using dynamic programming. The problem is to cut a rod of length n into pieces that maximize the total price obtained. A dynamic programming approach is used, storing solutions to subproblems in a 2D table. The algorithm fills the table using the recurrence relation that the maximum price of a length is either cutting it or not. It returns the maximum price, which is the bottom-right of the table, in O(n^2) time and O(n) space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mufaddal Challawala

12202040503008

DESIGN AND ANALYSIS OF ALGORITHM


Mini Project

Title: Cutting a Rod


Description:
Given a rod of length n, and an array that contains the prices of all the pieces smaller than n,
determine the maximum profit you could obtain from cutting up the rod and selling its pieces.
Input Format:
 The input will be an array which will contain profit for different length of rod.
Output Format:
Output the maximum amount of money we can make.

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:

T is the grid, and price is the array of prices.

2
Mufaddal Challawala
12202040503008

Take a look at the example below; it shows how to use the expression to fill values in a grid:

The Answer will be the cell in the extreme bottom-right:

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>

int max(int a, int b) { return (a > b)? a : b;}

int cutRod(int price[], int n)


{
int val[n+1];
val[0] = 0;
int i, j;

3
Mufaddal Challawala
12202040503008

for (i = 1; i<=n; i++)


{
int max_val = INT_MIN;
for (j = 0; j < i; j++)
max_val = max(max_val, price[j] + val[i-j-1]);
val[i] = max_val;
}

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

Time Complexity: O(n^2)


Space Complexity: O(n)

You might also like