You are on page 1of 23

ROD CUTTING PROBLEM

TEST TIME ON MINIMUM STACK

URL:https://forms.gle/cqoa4QcjjL8rPkzD8
ROD CUTTING PROBLEM

EXPLANATION

The rod cutting problem is a classic optimization problem where we need to

determine the maximum possible value that can be obtained by cutting a rod into

smaller pieces and selling them. Each piece of the rod has a certain value

associated with it.

The goal is to find the optimal cuts to maximize the total value.
ROD CUTTING PROBLEM
EXAMPLE

Input
price[] = [1, 5, 8, 9]

Rod length: 4

Output
10

Explanation
Cut the rod into two pieces of length 2 each to gain revenue of 5 + 5 = 10
ROD CUTTING PROBLEM

APPROACH

 Dynamic programming approach

 The Recursive (Top-Down) Approach


ROD CUTTING PROBLEM

APPROACH 1: DYNAMIC PROGRAMMING

The dynamic programming approach is commonly used to solve the rod cutting

problem. We create an auxiliary array, `val[]`, where `val[i]` represents the

maximum obtainable value for a rod of length `i`.

We initialize `val[0]` with 0, as there is no rod to cut. Then, for each rod

length from 1 to `n`, we iterate through all possible cuts and calculate the

maximum value.

By using this approach, we build up the solution to the problem from smaller

subproblems.
ROD CUTTING PROBLEM

APPROACH 1: DYNAMIC PROGRAMMING

Algorithm:

1. Create an array `val[]` of size `n+1` and initialize `val[0]` as 0.

2. For each rod length `i` from 1 to `n`, do the following:

a. Initialize `maxVal` to a very small integer value.

b. For each possible cut length `j` from 0 to `i-1`, do the following:

i. Calculate the maximum value `maxVal` by comparing `maxVal` with `price[j] + val[i-j-1]`,

where `price[j]` represents the value of the cut of length `j` and `val[i-j-1]` represents

the maximum value for the remaining length (`i-j-1`).

c. Set `val[i]` as `maxVal`.


ROD CUTTING PROBLEM

APPROACH 1: DYNAMIC PROGRAMMING

Pseudocode:

function cutRod(price[], n):

create an array val[] of size n+1 and initialize val[0] as 0

for i from 1 to n:

maxVal = MIN_VALUE

for j from 0 to i-1:

maxVal = max(maxVal, price[j] + val[i-j-1])

val[i] = maxVal

return val[n]
ROD CUTTING PROBLEM

class Main { return val[n];


}
public static int cutRod(int[] price, int n)
{ public static void main(String[] args)
int[] val = new int[n + 1]; {
val[0] = 0; int[] price = {1, 5, 8, 9, 10, 17, 17,
20};
for (int i = 1; i <= n; i++) { int n = price.length;
int maxVal = Integer.MIN_VALUE;
for (int j = 0; j < i; j++) { int maxVal = cutRod(price, n);
maxVal = Math.max(maxVal, price[j] + val[i - System.out.println("Maximum obtainable
j - 1]); value is" + maxVal);
} }
val[i] = maxVal; }
}
ROD CUTTING PROBLEM

TIME AND SPACE COMPLEXITY

The time complexity of the rod cutting algorithm is O(n^2) because we have two

nested loops that iterate through all possible cuts.

The space complexity is O(n) because we use an auxiliary array of size n+1 to

store the maximum values for each rod length.


ROD CUTTING PROBLEM

APPROACH 2:RECURSIVE (TOP-DOWN) APPROACH WITH MEMORIZATION

 The Recursive (Top-Down) Approach with Memoization for Rod Cutting involves defining a

recursive function that explores all possible cut combinations.

 Memoization is employed to store and reuse previously computed results, avoiding

redundant calculations.

 The algorithm computes the maximum obtainable value for a given rod length by

iteratively considering different cut lengths and recursively solving subproblems.


ROD CUTTING PROBLEM

ALGORITHM

1. Create a memoization array, `memo[]`, of size `n+1` and initialize all its values to -1.

2. Define a recursive function, `cutRodRecursive(price, n, memo)`, that takes the price

array, the length of the rod, and the memoization array as parameters.

3. Inside the function:

a. If the value for `memo[n]` is already computed, return `memo[n]`.

b. If `n` is 0, return 0 (base case).

c. Initialize `maxVal` to a very small integer value.


ROD CUTTING PROBLEM

ALGORITHM

d. For each possible cut length `i` from 1 to `n`, do the following:

1. Calculate the maximum value `maxVal` by comparing `maxVal` with `price[i-1] +

cutRodRecursive(price, n-i, memo)`, where `price[i-1]` represents the value of the cut of

length `i`.

e. Store `maxVal` in `memo[n]`.

f. Return `maxVal` as the maximum obtainable value for the given rod length.

4. Call the `cutRodRecursive` function passing the price array, the length of the rod, and

the memoization array.


ROD CUTTING PROBLEM

PSEUDOCODE

function cutRodRecursive(price[], n, memo[]):

if memo[n] is not -1:

return memo[n]

if n = 0:

return 0

maxVal = MIN_VALUE

for i from 1 to n:

maxVal = max(maxVal, price[i-1] + cutRodRecursive(price, n-i, memo))

memo[n] = maxVal

return maxVal
ROD CUTTING PROBLEM

import java.util.Arrays; public static void main(String[] args) {


public class Main { int[] price = {1, 5, 8, 9, 10, 17,
public static int 17, 20};
cutRodRecursive(int[] price, int n, int[] int n = price.length;
memo) { int[] memo = new int[n + 1];
if (memo[n] != -1) { Arrays.fill(memo, -1);
return memo[n]; int maxVal =
} cutRodRecursive(price, n, memo);
if (n == 0) { System.out.println("Maximum
return 0; obtainable value is " + maxVal);
} }
int maxVal = Integer.MIN_VALUE; }
for (int i = 1; i <= n; i++) {
maxVal = Math.max(maxVal,
price[i-1] + cutRodRecursive(price, n-i,
memo));
}
memo[n] = maxVal;
return maxVal;
}
ROD CUTTING PROBLEM

TIME AND SPACE COMPLEXITY

The time complexity of the recursive approach with memoization is O(n^2) because for each

rod length, we iterate through all possible cuts. However, with memoization, we avoid

redundant calculations, significantly improving the overall performance.

The space complexity is O(n) to store the memoization array, where n is the length of the

rod.
INTERVIEW QUESTIONS

1. Explain the Rod Cutting problem.

Answer: The Rod Cutting problem involves determining the maximum

obtainable value by cutting a given rod of a certain length into pieces.

Each piece has an associated value, and the goal is to find the optimal

cutting strategy to maximize the total value.


INTERVIEW QUESTIONS

2. Describe a dynamic programming approach to solve the Rod Cutting

problem.

Answer: The dynamic programming approach involves breaking down the

problem into subproblems, solving them, and storing their solutions to

avoid redundant computations. For Rod Cutting, a memoization array can be

used to store the maximum values for subproblems, ensuring an efficient

solution.
INTERVIEW QUESTIONS

3.What is the time complexity of the dynamic programming solution for the
Rod Cutting problem?

Answer: The time complexity is O(n^2), where n is the length of the rod.

This is due to the nested loop structure used to calculate the optimal

values for different rod lengths.


INTERVIEW QUESTIONS

4. Can you optimize the Rod Cutting algorithm for better time complexity?

Answer: Yes, an optimized solution using bottomup dynamic programming can

achieve O(n^2) time complexity. Additionally, more advanced techniques

like matrix exponentiation can further improve the time complexity to O(n

log n).
INTERVIEW QUESTIONS

5.How does the choice of rod lengths and values impact the efficiency of
the Rod Cutting algorithm?

Answer: The efficiency is influenced by the structure of the input. If the rod

lengths are limited or if there are repeating patterns in the lengths, dynamic

programming can be more effective. The values associated with the rod lengths

also play a crucial role, and a careful selection of values can lead to a more

efficient solution.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like