diff --git a/.DS_Store b/.DS_Store index 263b422c..1954b2ec 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.github/workflows/macos_test_cases.yml b/.github/workflows/macos_test_cases.yml index 4c88fe47..9f7bc219 100644 --- a/.github/workflows/macos_test_cases.yml +++ b/.github/workflows/macos_test_cases.yml @@ -42,7 +42,7 @@ jobs: # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail run: ctest --output-on-failure - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} slug: CSRT-NTUA/AlgoPlus diff --git a/src/algorithms/dynamic_programming/knapsack.h b/src/algorithms/dynamic_programming/knapsack.h new file mode 100644 index 00000000..391c3edb --- /dev/null +++ b/src/algorithms/dynamic_programming/knapsack.h @@ -0,0 +1,39 @@ +#ifndef KNAPSACK_H +#define KNAPSACK_H + +#ifdef __cplusplus +#include +#include +#include +#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 > &v, int capacity) { + if(capacity == 0) { + return 0; + } + + int n = int(v.size()); + std::vector > dp(n + 1, std::vector(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 diff --git a/src/algorithms/dynamic_programming/subset_sum.h b/src/algorithms/dynamic_programming/subset_sum.h new file mode 100644 index 00000000..f8e85082 --- /dev/null +++ b/src/algorithms/dynamic_programming/subset_sum.h @@ -0,0 +1,40 @@ +#ifndef SUBSET_SUM_H +#define SUBSET_SUM_H + +#ifdef __cplusplus +#include +#include +#include +#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 &v, int B) { + if(int(v.size()) == 0) { + return 0; + } + int n = int(v.size()); + std::vector > dp(n + 1, std::vector(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 diff --git a/src/classes/tree/avl_tree.h b/src/classes/tree/avl_tree.h index 875da11c..69d613c2 100644 --- a/src/classes/tree/avl_tree.h +++ b/src/classes/tree/avl_tree.h @@ -211,7 +211,7 @@ template class avl_tree { * @brief operator << for avl_tree class */ friend std::ostream & operator << (std::ostream &out, avl_tree &t){ - std::vector > order = t.inorder(); + std::vector order = t.inorder(); for(int i = 0; i class bst { * @brief operator << for bst class */ friend std::ostream & operator << (std::ostream &out, bst &t){ - std::vector > order = t.inorder(); + std::vector order = t.inorder(); for(int i = 0; i class splay_tree { * @brief operator << for splay tree class */ friend std::ostream & operator << (std::ostream &out, splay_tree &t){ - std::vector > order = t.inorder(); + std::vector order = t.inorder(); for(int i = 0; i class tree { * @brief operator << for bst class */ friend std::ostream & operator << (std::ostream &out, tree &t){ - std::vector > order = t.inorder(); + std::vector order = t.inorder(); for(int i = 0; i the values of a and b diff --git a/tests/algorithms/dynamic_programming/knapsack.cc b/tests/algorithms/dynamic_programming/knapsack.cc new file mode 100644 index 00000000..6136ef1b --- /dev/null +++ b/tests/algorithms/dynamic_programming/knapsack.cc @@ -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 > 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); +} diff --git a/tests/algorithms/dynamic_programming/subset_sum.cc b/tests/algorithms/dynamic_programming/subset_sum.cc new file mode 100644 index 00000000..9fac4143 --- /dev/null +++ b/tests/algorithms/dynamic_programming/subset_sum.cc @@ -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 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 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); +} diff --git a/tests/tree/avl.cc b/tests/tree/avl.cc index 1752d9fa..8ead82d6 100644 --- a/tests/tree/avl.cc +++ b/tests/tree/avl.cc @@ -164,6 +164,14 @@ TEST_CASE("Testing get_root function in avl tree"){ REQUIRE(t.get_root() == 36); } +TEST_CASE("Testing operator << for avl tree") { + avl_tree t({1, 10, 2, 8, 6, 5}); + REQUIRE_NOTHROW(cout << t); + + avl_tree tt({'a', 'g', 'd', 'z', 'w', 'f'}); + REQUIRE_NOTHROW(cout << tt); +} + #define TREE_VISUALIZATION_H #ifdef TREE_VISUALIZATION_H diff --git a/tests/tree/bst.cc b/tests/tree/bst.cc index 9a260aa2..830b3e50 100644 --- a/tests/tree/bst.cc +++ b/tests/tree/bst.cc @@ -151,6 +151,14 @@ TEST_CASE("testing level order in bst"){ REQUIRE(produced == sol); } +TEST_CASE("Testing opearator << for bst") { + bst t({1, 10, 5, 3, 9, 25}); + REQUIRE_NOTHROW(cout << t); + + bst tt({'a', 'g', 'q', 'v', 'w'}); + REQUIRE_NOTHROW(cout << tt); +} + #define TREE_VISUALIZATION_H #ifdef TREE_VISUALIZATION_H diff --git a/tests/tree/splay_tree.cc b/tests/tree/splay_tree.cc index 04e2926e..01b8a754 100644 --- a/tests/tree/splay_tree.cc +++ b/tests/tree/splay_tree.cc @@ -128,6 +128,14 @@ TEST_CASE("testing level order for splay tree"){ REQUIRE(produced==sol); } +TEST_CASE("Testing operator << for splay tree") { + splay_tree t({1, 5, 3, 2, 9, 11}); + REQUIRE_NOTHROW(cout << t); + + splay_tree tt({'a', 'b', 'w', 'z', 'f', 'd'}); + REQUIRE_NOTHROW(cout << tt); +} + #define TREE_VISUALIZATION_H #ifdef TREE_VISUALIZATION_H diff --git a/tests/tree/tree.cc b/tests/tree/tree.cc index d0de6f33..138888f4 100644 --- a/tests/tree/tree.cc +++ b/tests/tree/tree.cc @@ -53,6 +53,27 @@ TEST_CASE("testing level order traversal in tree class [TREECLASS]"){ REQUIRE(produced == sol); } +TEST_CASE("Testing operator << for tree") { + tree t; + t.insert("", 1); + t.insert("l", 2); + t.insert("r", 3); + t.insert("ll", 4); + t.insert("lr", 5); + t.insert("rl", 6); + t.insert("rr", 7); + t.insert("lll", 8); + t.insert("llr", 9); + t.insert("lrl", 10); + REQUIRE_NOTHROW(cout << t); + + tree tt; + tt.insert("", "hello"); + tt.insert("r", "world"); + tt.insert("l", "universe"); + REQUIRE_NOTHROW(cout << tt); +} + #define TREE_VISUALIZATION_H #ifdef TREE_VISUALIZATION_H