Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
cut_rod.cpp File Reference

Implementation of cutting a rod problem. More...

#include <array>
#include <cassert>
#include <climits>
#include <iostream>
Include dependency graph for cut_rod.cpp:

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 
namespace  cut_rod
 Implementation of cutting a rod problem.
 

Functions

template<size_t T>
int dynamic_programming::cut_rod::maxProfitByCuttingRod (const std::array< int, T > &price, const uint64_t &n)
 Cuts the rod in different pieces and stores the maximum profit for each piece of the rod.
 
static void test ()
 Function to test above algorithm.
 
int main ()
 Main function.
 

Detailed Description

Implementation of cutting a rod problem.

Given a rod of length n inches and an array of prices that contains prices of all pieces of size<=n. Determine the maximum profit obtainable by cutting up the rod and selling the pieces.

Algorithm

The idea is to break the given rod into every smaller piece as possible and then check profit for each piece, by calculating maximum profit for smaller pieces we will build the solution for larger pieces in bottom-up manner.

Author
Anmol
Pardeep

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
110 {
111 // Testing
112 test();
113 return 0;
114}
static void test()
Function to test above algorithm.
Definition cut_rod.cpp:71
Here is the call graph for this function:

◆ maxProfitByCuttingRod()

template<size_t T>
int dynamic_programming::cut_rod::maxProfitByCuttingRod ( const std::array< int, T > & price,
const uint64_t & n )

Cuts the rod in different pieces and stores the maximum profit for each piece of the rod.

Template Parameters
Tsize of the price array
Parameters
nsize of the rod in inches
pricean array of prices that contains prices of all pieces of size<=n
Returns
maximum profit obtainable for
Parameters
ninch rod.
44 {
45 int *profit =
46 new int[n + 1]; // profit[i] will hold maximum profit for i inch rod
47
48 profit[0] = 0; // if length of rod is zero, then no profit
49
50 // outer loop will select size of rod, starting from 1 inch to n inch rod.
51 // inner loop will evaluate the maximum profit we can get for i inch rod by
52 // making every possible cut on it and will store it in profit[i].
53 for (size_t i = 1; i <= n; i++) {
54 int q = INT_MIN;
55 for (size_t j = 1; j <= i; j++) {
56 q = std::max(q, price[j - 1] + profit[i - j]);
57 }
58 profit[i] = q;
59 }
60 const int16_t ans = profit[n];
61 delete[] profit;
62 return ans; // returning maximum profit
63}
T max(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Function to test above algorithm.

Returns
void
71 {
72 // Test 1
73 const int16_t n1 = 8; // size of rod
74 std::array<int32_t, n1> price1 = {1,2,4,6,8,45,21,9}; // price array
75 const int64_t max_profit1 =
77 const int64_t expected_max_profit1 = 47;
78 assert(max_profit1 == expected_max_profit1);
79 std::cout << "Maximum profit with " << n1 << " inch road is " << max_profit1
80 << std::endl;
81
82 // Test 2
83 const int16_t n2 = 30; // size of rod
85 1, 5, 8, 9, 10, 17, 17, 20, 24, 30, // price array
86 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
87 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
88
89 const int64_t max_profit2=
91 const int32_t expected_max_profit2 = 90;
92 assert(max_profit2 == expected_max_profit2);
93 std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2
94 << std::endl;
95 // Test 3
96 const int16_t n3 = 5; // size of rod
97 std::array<int32_t, n3> price3 = {2,9,17,23,45}; // price array
98 const int64_t max_profit3 =
100 const int64_t expected_max_profit3 = 45;
101 assert(max_profit3 == expected_max_profit3);
102 std::cout << "Maximum profit with " << n3 << " inch road is " << max_profit3
103 << std::endl;
104}
int maxProfitByCuttingRod(const std::array< int, T > &price, const uint64_t &n)
Cuts the rod in different pieces and stores the maximum profit for each piece of the rod.
Definition cut_rod.cpp:44
T endl(T... args)
Here is the call graph for this function: