This Java program solves the rod cutting problem using dynamic programming. The goal is to maximize profit by cutting a rod of a given length into pieces, with each piece having a specific price.

Java Program


public class RodCutting {

// Function to calculate the maximum profit by cutting the rod
public static int maximizeProfit(int[] prices, int rodLength) {
// DP array where dp[i] represents the maximum profit for a rod of length i
int[] dp = new int[rodLength + 1];

// Initialize the DP table with 0 profit for a rod of length 0
dp[0] = 0;

// Build the dp table in a bottom-up manner
for (int i = 1; i <= rodLength; i++) {
int maxProfit = Integer.MIN_VALUE;

// Try cutting the rod at every length and take the maximum profit
for (int j = 0; j < i; j++) {
maxProfit = Math.max(maxProfit, prices[j] + dp[i – j – 1]);
}

dp[i] = maxProfit;
}

// The maximum profit for a rod of length rodLength is stored in dp[rodLength]
return dp[rodLength];
}

public static void main(String[] args) {
// Example: Prices of pieces of rod of different lengths
int[] prices = {1, 5, 8, 9, 10, 17, 17, 20};
int rodLength = 8;

// Solve the rod cutting problem
int maxProfit = maximizeProfit(prices, rodLength);
System.out.println(“Maximum profit by cutting the rod is: ” + maxProfit);
}
}

Explanation of the Program Structure

This program solves the rod cutting problem using dynamic programming. The problem involves cutting a rod of a given length into smaller pieces to maximize profit, with each piece having a specific price.

  • maximizeProfit function: This function implements the dynamic programming solution for the rod cutting problem. It takes two inputs: an array prices[] where each element represents the price of a rod of length i+1, and an integer rodLength which is the length of the rod to be cut.
  • Dynamic Programming Array (dp): The array dp[i] stores the maximum profit that can be obtained by cutting a rod of length i. The array is filled in a bottom-up manner by trying to cut the rod at every possible length and calculating the maximum profit for each length.
  • Main logic: The program iterates through all lengths of the rod, and for each length, it tries cutting the rod at every possible position to calculate the profit. The final result, dp[rodLength], contains the maximum profit for the full length of the rod.
  • Base case: When the rod length is 0, the profit is 0 (since no cuts are made). This is represented by initializing dp[0] to 0.
  • main function: This function initializes a sample set of prices for different lengths of the rod and calls the maximizeProfit function to compute and print the maximum profit that can be obtained by cutting the rod.

Step-by-Step Process:

  1. We initialize a dynamic programming array dp of size (rodLength+1), where each element dp[i] stores the maximum profit for a rod of length i.
  2. We iterate through all possible lengths of the rod, from 1 to rodLength. For each length, we try every possible cut (from 1 to the current length) and update the DP table with the maximum profit.
  3. The base case handles when the rod length is 0, in which case the profit is 0.
  4. The final result, stored in dp[rodLength], represents the maximum profit for the full length of the rod.

Example:

Consider the following prices for rods of different lengths:


Prices: {1, 5, 8, 9, 10, 17, 17, 20}
Rod length: 8

In this case, the maximum profit is 22, obtained by cutting the rod into two pieces of length 2 and 6. The program will output:


Maximum profit by cutting the rod is: 22

Conclusion:

This Java program efficiently solves the rod cutting problem using dynamic programming. The time complexity of this solution is O(n^2), where n is the length of the rod. This approach ensures that all possible ways of cutting the rod are considered, resulting in an optimal solution for maximizing profit.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)