Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Nov 20, 2024
2 parents e8312f6 + 18835f9 commit 8d016db
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 7 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/macos_test_cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/algorithms/dynamic_programming/knapsack.h
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
40 changes: 40 additions & 0 deletions src/algorithms/dynamic_programming/subset_sum.h
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
2 changes: 1 addition & 1 deletion src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ template <typename T> class avl_tree {
* @brief operator << for avl_tree class
*/
friend std::ostream & operator << (std::ostream &out, avl_tree<T> &t){
std::vector<std::vector<T> > order = t.inorder();
std::vector<T> order = t.inorder();
for(int i = 0; i<order.size(); i++){
if(i != order.size() - 1){
out << order[i] << ", ";
Expand Down
2 changes: 1 addition & 1 deletion src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ template <typename T> class bst {
* @brief operator << for bst class
*/
friend std::ostream & operator << (std::ostream &out, bst<T> &t){
std::vector<std::vector<T> > order = t.inorder();
std::vector<T> order = t.inorder();
for(int i = 0; i<order.size(); i++){
if(i != order.size() - 1){
out << order[i] << ", ";
Expand Down
2 changes: 1 addition & 1 deletion src/classes/tree/splay_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ template <typename T> class splay_tree {
* @brief operator << for splay tree class
*/
friend std::ostream & operator << (std::ostream &out, splay_tree<T> &t){
std::vector<std::vector<T> > order = t.inorder();
std::vector<T> order = t.inorder();
for(int i = 0; i<order.size(); i++){
if(i != order.size() - 1){
out << order[i] << ", ";
Expand Down
2 changes: 1 addition & 1 deletion src/classes/tree/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ template <typename T> class tree {
* @brief operator << for bst class
*/
friend std::ostream & operator << (std::ostream &out, tree<T> &t){
std::vector<std::vector<T> > order = t.inorder();
std::vector<T> order = t.inorder();
for(int i = 0; i<order.size(); i++){
if(i != order.size() - 1){
out << order[i] << ", ";
Expand Down
4 changes: 2 additions & 2 deletions src/machine_learning/regression/linear_regression/lin_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class linear_regression{
double Sxy, Sxx;
double x_mean, y_mean;
public:

/**
* @brief Constructor for linear regression class
* @param points: 2D points that user pass to the regressor
Expand All @@ -41,7 +41,7 @@ class linear_regression{
std::cerr << e.what() << '\n';
}
}

/**
* @brief get_results function
* @return pair<double,double> the values of a and b
Expand Down
12 changes: 12 additions & 0 deletions tests/algorithms/dynamic_programming/knapsack.cc
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);
}
23 changes: 23 additions & 0 deletions tests/algorithms/dynamic_programming/subset_sum.cc
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);
}
8 changes: 8 additions & 0 deletions tests/tree/avl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> t({1, 10, 2, 8, 6, 5});
REQUIRE_NOTHROW(cout << t);

avl_tree<char> tt({'a', 'g', 'd', 'z', 'w', 'f'});
REQUIRE_NOTHROW(cout << tt);
}

#define TREE_VISUALIZATION_H
#ifdef TREE_VISUALIZATION_H

Expand Down
8 changes: 8 additions & 0 deletions tests/tree/bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ TEST_CASE("testing level order in bst"){
REQUIRE(produced == sol);
}

TEST_CASE("Testing opearator << for bst") {
bst<int> t({1, 10, 5, 3, 9, 25});
REQUIRE_NOTHROW(cout << t);

bst<char> tt({'a', 'g', 'q', 'v', 'w'});
REQUIRE_NOTHROW(cout << tt);
}

#define TREE_VISUALIZATION_H
#ifdef TREE_VISUALIZATION_H

Expand Down
8 changes: 8 additions & 0 deletions tests/tree/splay_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ TEST_CASE("testing level order for splay tree"){
REQUIRE(produced==sol);
}

TEST_CASE("Testing operator << for splay tree") {
splay_tree<int> t({1, 5, 3, 2, 9, 11});
REQUIRE_NOTHROW(cout << t);

splay_tree<char> tt({'a', 'b', 'w', 'z', 'f', 'd'});
REQUIRE_NOTHROW(cout << tt);
}

#define TREE_VISUALIZATION_H
#ifdef TREE_VISUALIZATION_H

Expand Down
21 changes: 21 additions & 0 deletions tests/tree/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ TEST_CASE("testing level order traversal in tree class [TREECLASS]"){
REQUIRE(produced == sol);
}

TEST_CASE("Testing operator << for tree") {
tree<int> 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<std::string> tt;
tt.insert("", "hello");
tt.insert("r", "world");
tt.insert("l", "universe");
REQUIRE_NOTHROW(cout << tt);
}

#define TREE_VISUALIZATION_H
#ifdef TREE_VISUALIZATION_H

Expand Down

0 comments on commit 8d016db

Please sign in to comment.