-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/origin/main'
- Loading branch information
Showing
15 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef KNAPSACK_H | ||
#define KNAPSACK_H | ||
|
||
#ifdef __cplusplus | ||
#include <iostream> | ||
#include <vector> | ||
#include <utility> | ||
#endif | ||
|
||
/** | ||
* @brief knapsack 0/1 function | ||
* @param v the passed vector | ||
* @param capacity the capacity of the knapsack | ||
* @return int: the maximum value you can collect with a knapsack of the passed capacity | ||
*/ | ||
int knapsack(std::vector<std::pair<int, int> > &v, int capacity) { | ||
if(capacity == 0) { | ||
return 0; | ||
} | ||
|
||
int n = int(v.size()); | ||
std::vector<std::vector<int > > dp(n + 1, std::vector<int>(capacity + 1)); | ||
|
||
for(int i = 1; i<=n; i++) { | ||
for(int j = 0; j<=capacity; j++) { | ||
dp[i][j] = dp[i - 1][j]; | ||
if(i == 0 || j == 0) { | ||
dp[i][j] = 0; | ||
} | ||
else if(v[i - 1].first <= j) { | ||
dp[i][j] = std::max(dp[i - 1][j], v[i - 1].second + dp[i - 1][j - v[i - 1].first]); | ||
} | ||
} | ||
} | ||
|
||
return dp[n][capacity]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef SUBSET_SUM_H | ||
#define SUBSET_SUM_H | ||
|
||
#ifdef __cplusplus | ||
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
#endif | ||
|
||
/** | ||
* @brief subset sum function | ||
* @details Returns the number of subsets you can create with sum of B | ||
* @param v: the passed vector | ||
* @param B: the sum | ||
* @returns: int, the total number of subsets you can create | ||
*/ | ||
int subset_sum(std::vector<int> &v, int B) { | ||
if(int(v.size()) == 0) { | ||
return 0; | ||
} | ||
int n = int(v.size()); | ||
std::vector<std::vector<int> > dp(n + 1, std::vector<int>(B + 1, 0)); | ||
for(int i = 0; i<=n; i++) { | ||
dp[i][0] = 1; | ||
} | ||
|
||
for(int i = 1; i<=n; i++) { | ||
for(int j = 0; j<=B; j++) { | ||
dp[i][j] = dp[i - 1][j]; | ||
|
||
if(j - v[i - 1] >= 0) { | ||
dp[i][j] += dp[i - 1][j - v[i - 1]]; | ||
} | ||
} | ||
} | ||
|
||
return dp[n][B]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "../../../third_party/catch.hpp" | ||
#include "../../../src/algorithms/dynamic_programming/knapsack.h" | ||
|
||
TEST_CASE("Testing knapsack 0/1 [1]") { | ||
std::vector<std::pair<int, int> > v = {{10, 60}, {20, 100}, {30, 120}}; | ||
|
||
REQUIRE(knapsack(v, 50) == 220); | ||
v.clear(); | ||
v = {{5, 40}, {3, 20}, {6, 10}, {3, 30}}; | ||
|
||
REQUIRE(knapsack(v, 10) == 70); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "../../../third_party/catch.hpp" | ||
#include "../../../src/algorithms/dynamic_programming/subset_sum.h" | ||
|
||
TEST_CASE("Testing subset sum [1]") { | ||
std::vector<int> v {4, 2, 1, 5, 7}; | ||
int B = 7; | ||
|
||
REQUIRE(subset_sum(v, B) == 3); | ||
v.clear(); | ||
|
||
REQUIRE(subset_sum(v, B) == 0); | ||
} | ||
|
||
TEST_CASE("Testing subset sum [2]") { | ||
std::vector<int> v {1, 4, 6, 9}; | ||
int B = 5; | ||
|
||
REQUIRE(subset_sum(v, B) == 1); | ||
B = 6; | ||
REQUIRE(subset_sum(v, 6) == 1); | ||
B = 9; | ||
REQUIRE(subset_sum(v, B) == 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters