Skip to content

Commit f53ccbc

Browse files
authored
Merge pull request #81 from Debashis08/feature-dp-implementation
Feature dp implementation
2 parents 94eec36 + 6d8acfe commit f53ccbc

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#include<iostream>
3+
#include<vector>
4+
using namespace std;
5+
6+
namespace RodCutting
7+
{
8+
class DynamicProgramming
9+
{
10+
private:
11+
int _totalLength;
12+
vector<int> _price;
13+
vector<int> _cutPositions;
14+
public:
15+
DynamicProgramming(vector<int> price);
16+
int RecursiveRodCutting(int length);
17+
pair<int, vector<int>> DpGetMaximumProfitWithCuts(int length);
18+
};
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "../../include/0005_DynamicProgramming/0001_RodCutting.h"
2+
#include<iostream>
3+
#include<vector>
4+
using namespace std;
5+
6+
namespace RodCutting
7+
{
8+
DynamicProgramming::DynamicProgramming(vector<int> price)
9+
{
10+
this->_price = price;
11+
this->_totalLength = this->_price.size();
12+
}
13+
14+
int DynamicProgramming::RecursiveRodCutting(int length)
15+
{
16+
// Base case
17+
if (length == 0)
18+
{
19+
return 0;
20+
}
21+
22+
int result = 0;
23+
24+
for (int cut = 1; cut <= length; cut++)
25+
{
26+
result = max(result, this->_price[cut - 1] + this->RecursiveRodCutting(length - cut));
27+
}
28+
29+
return result;
30+
}
31+
32+
pair<int, vector<int>> DynamicProgramming::DpGetMaximumProfitWithCuts(int length)
33+
{
34+
vector<int> dp(this->_price.size() + 1, 0);
35+
this->_cutPositions = vector<int>(this->_price.size() + 1, 0);
36+
37+
// Find maximum value for all rod of length i.
38+
for (int i = 1; i <= this->_totalLength; i++)
39+
{
40+
for (int j = 1; j <= i; j++)
41+
{
42+
if (dp[i] < (this->_price[j - 1] + dp[i - j]))
43+
{
44+
dp[i] = this->_price[j - 1] + dp[i - j];
45+
this->_cutPositions[i] = j - 1;
46+
}
47+
}
48+
}
49+
50+
// Re-construct the cuts
51+
vector<int> cutLengths;
52+
int currentLength = length;
53+
while (currentLength > 0)
54+
{
55+
cutLengths.push_back(this->_cutPositions[currentLength] + 1);
56+
currentLength -= cutLengths.back();
57+
}
58+
59+
return { dp[length] ,cutLengths };
60+
}
61+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Specify the source files
2+
set(0005DYNAMICPROGRAMMING_SOURCES
3+
0001_RodCutting.cc
4+
5+
)
6+
7+
# Create a library target
8+
add_library(0005DYNAMICPROGRAMMING ${0005DYNAMICPROGRAMMING_SOURCES})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<gtest/gtest.h>
2+
#include "../../include/0005_DynamicProgramming/0001_RodCutting.h"
3+
#include "../0000_CommonUtilities/UnitTestHelper.h"
4+
5+
namespace RodCutting
6+
{
7+
UnitTestHelper unitTestHelper;
8+
9+
TEST(DpRodCutting, RecursiveTest)
10+
{
11+
// Arrange
12+
vector<int> price = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
13+
int length = 8;
14+
DynamicProgramming dp(price);
15+
int expectedResult = 22;
16+
17+
// Act
18+
int actualResult = dp.RecursiveRodCutting(length);
19+
20+
// Assert
21+
ASSERT_EQ(actualResult, expectedResult);
22+
}
23+
24+
TEST(DpRodCutting, DpTest)
25+
{
26+
// Arrange
27+
vector<int> price = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
28+
int length = 8;
29+
DynamicProgramming dp(price);
30+
int expectedProfit = 22;
31+
vector<int> expectedCuts = { 2, 6 };
32+
33+
// Act
34+
pair<int, vector<int>> actualResult = dp.DpGetMaximumProfitWithCuts(length);
35+
36+
// Assert
37+
ASSERT_EQ(actualResult.first, expectedProfit);
38+
ASSERT_EQ(actualResult.second, expectedCuts);
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_policy(SET CMP0135 NEW)
2+
3+
include(FetchContent)
4+
FetchContent_Declare(
5+
googletest
6+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
7+
)
8+
9+
# For Windows: Prevent overriding the parent project's compiler/linker settings
10+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
11+
FetchContent_MakeAvailable(googletest)
12+
13+
enable_testing()
14+
15+
add_executable(
16+
0005DynamicProgrammingTests
17+
0001_RodCuttingTest.cc
18+
19+
)
20+
21+
target_link_libraries(
22+
0005DynamicProgrammingTests
23+
GTest::gtest_main
24+
0005DYNAMICPROGRAMMING
25+
)
26+
27+
# Add .clang-tidy configuration to this library.
28+
if(CLANG_TIDY_EXE)
29+
set_target_properties(0005DYNAMICPROGRAMMING PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
30+
endif()
31+
32+
include(GoogleTest)
33+
gtest_discover_tests(0005DynamicProgrammingTests DISCOVERY_TIMEOUT 30)

0 commit comments

Comments
 (0)