
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Cutting a Rod
Rod Cutting Problem
The Rod Cutting problem is a classic example of dynamic programming. The goal is to cut a rod into pieces to maximize the total value. Each piece has a specific length and value, and we must choose the cuts in such a way that the total value is as high as possible.
This problem is similar to the 0-1 Knapsack problem and can be solved using recursion and dynamic programming techniques. It is useful in resource allocation and pricing strategies.
Rod Cutting Problem Statement
You are given a rod of length n and an array price[] where price[i] represents the price of a rod of length i+1. Your task is to determine the maximum value obtainable by cutting up the rod and selling the pieces.
Solving Rod Cutting Problem Using Recursive (Brute-force) Approach
This approach tries all possible ways to cut the rod and chooses the one with the maximum profit. It checks each combination recursively, but becomes inefficient as the length increases.
Steps
- If the rod length becomes 0, return 0.
- Try every possible cut and recursively calculate the total value.
- Return the maximum of all possible values.
Example
In this example, we try cutting the rod at every length and recursively find the best value that can be achieved -
# Recursive approach to Rod Cutting def cut_rod(price, n): if n == 0: return 0 max_val = float('-inf') # Try every possible cut for i in range(n): max_val = max(max_val, price[i] + cut_rod(price, n - i - 1)) return max_val # Test input price = [1, 5, 8, 9, 10, 17, 17, 20] n = len(price) print(cut_rod(price, n))
Following is the output obtained -
22
The maximum value is calculated by testing every way to cut and choosing the most profitable one.
Solving Rod Cutting Problem Using Dynamic Programming (Bottom-Up)
The recursive method recalculates the same subproblems many times. Using dynamic programming, we store the result of each subproblem and build the final result from the bottom up.
Steps
- Create a table dp[] of size n+1 and initialize it to 0.
- Iteratively calculate the best value for every rod length from 1 to n.
- For each length, try all cuts and update the maximum value in dp[] array.
Example
In this example, we fill the dp[] array by computing the maximum value for every rod length using known subproblem results -
# Bottom-up DP approach to Rod Cutting def cut_rod(price, n): dp = [0] * (n + 1) for i in range(1, n + 1): max_val = float('-inf') for j in range(i): max_val = max(max_val, price[j] + dp[i - j - 1]) dp[i] = max_val return dp[n] # Test input price = [1, 5, 8, 9, 10, 17, 17, 20] n = len(price) print(cut_rod(price, n))
We get the output as shown below -
22
The DP array stores the best prices for smaller lengths and builds the solution.