Skip to content

Commit

Permalink
added algorithm folder with gcd
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jan 23, 2024
1 parent ce747d5 commit 1d13b2d
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 74 deletions.
3 changes: 1 addition & 2 deletions examples/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ int main() {

std::cout << g.shortest_path(0, 10) << '\n';

graph<int> g2("directed", { {1, {2,3}}, {2, {4, 5}}});
graph<int> g2("directed", {{1, {2, 3}}, {2, {4, 5}}});
std::vector<int> v = g2.bfs(0);
for (auto &x : v) {
std::cout << x << " ";
}
std::cout << '\n';


// now you can visualize graphs with algoplus
g2.visualize();
}
63 changes: 63 additions & 0 deletions src/algorithms/number_theory/gcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifdef __cplusplus
#include <iostream>
#endif

#ifndef GCD_H
#define GCD_H

namespace gcd {

/*
*binary_gcd function.
*@param a: first element.
*@param b: second element.
*Returns the gcd(a,b).
*/
int64_t binary_gcd(const int64_t a, const int64_t b) {
if (a == b) {
return a;
}
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}

if (~a & 1) {
if (~b & 1) {
return binary_gcd(a >> 1, b >> 1) << 1;
} else {
return binary_gcd(a >> 1, b);
}
}
if (~b & 1) {
return binary_gcd(a, b >> 1);
}

if (a > b) {
return binary_gcd((a - b) >> 1, b);
}
return binary_gcd((b - a) >> 1, a);
}

/*
*euclidean_gcd function.
*@param a: first element.
*@param b: second element.
*Returns the gcd(a,b).
*/
int64_t euclidean_gcd(int64_t a, int64_t b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return (a + b);
}

}; // namespace gcd

#endif
138 changes: 72 additions & 66 deletions src/classes/disjoint_set/disjoint_set.h
Original file line number Diff line number Diff line change
@@ -1,80 +1,86 @@
#ifndef DISJOINT_SET_H
#define DISJOINT_SET_H

#ifdef __cplusplus
#include <vector>
#endif

class dsu {
private:
std::vector<uint64_t> p; ///< keeps track of the parent of ith element
std::vector<uint64_t> depth; ///< tracks the depth(rank) of i in the tree
std::vector<uint64_t> setSize; ///< size of each chunk(set)
std::vector<uint64_t> maxElement; ///< maximum of each set to which i belongs to
std::vector<uint64_t> minElement; ///< minimum of each set to which i belongs to
public:

explicit dsu(uint64_t n) {
p.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
p[i] = i;
}
depth.assign(n, 0);
maxElement.assign(n, 0);
minElement.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
depth[i] = 0;
maxElement[i] = i;
minElement[i] = i;
}
setSize.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
setSize[i] = 1;
}
private:
std::vector<uint64_t> p; ///< keeps track of the parent of ith element
std::vector<uint64_t> depth; ///< tracks the depth(rank) of i in the tree
std::vector<uint64_t> setSize; ///< size of each chunk(set)
std::vector<uint64_t>
maxElement; ///< maximum of each set to which i belongs to
std::vector<uint64_t>
minElement; ///< minimum of each set to which i belongs to
public:
explicit dsu(uint64_t n) {
p.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
p[i] = i;
}
depth.assign(n, 0);
maxElement.assign(n, 0);
minElement.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
depth[i] = 0;
maxElement[i] = i;
minElement[i] = i;
}

uint64_t findSet(uint64_t i) {
if (p[i] == i) {
return i;
}
return (p[i] = findSet(p[i]));
setSize.assign(n, 0);
for (uint64_t i = 0; i < n; i++) {
setSize[i] = 1;
}
void UnionSet(uint64_t i, uint64_t j) {
if (isSame(i, j)) {
return;
}
}

uint64_t x = findSet(i);
uint64_t y = findSet(j);
uint64_t findSet(uint64_t i) {
if (p[i] == i) {
return i;
}
return (p[i] = findSet(p[i]));
}
void UnionSet(uint64_t i, uint64_t j) {
if (isSame(i, j)) {
return;
}

if (depth[x] > depth[y]) {
std::swap(x, y);
}
p[x] = y;
uint64_t x = findSet(i);
uint64_t y = findSet(j);

if (depth[x] == depth[y]) {
depth[y]++;
}
setSize[y] += setSize[x];
maxElement[y] = std::max(maxElement[x], maxElement[y]);
minElement[y] = std::min(minElement[x], minElement[y]);
if (depth[x] > depth[y]) {
std::swap(x, y);
}

bool isSame(uint64_t i, uint64_t j) {
if (findSet(i) == findSet(j)) {
return true;
}
return false;
p[x] = y;

if (depth[x] == depth[y]) {
depth[y]++;
}

std::vector<uint64_t> get(uint64_t i) {
std::vector<uint64_t> ans;
ans.push_back(get_min(i));
ans.push_back(get_max(i));
ans.push_back(size(i));
return ans;
setSize[y] += setSize[x];
maxElement[y] = std::max(maxElement[x], maxElement[y]);
minElement[y] = std::min(minElement[x], minElement[y]);
}

bool isSame(uint64_t i, uint64_t j) {
if (findSet(i) == findSet(j)) {
return true;
}

uint64_t size(uint64_t i) { return setSize[findSet(i)]; }

uint64_t get_max(uint64_t i) { return maxElement[findSet(i)]; }

uint64_t get_min(uint64_t i) { return minElement[findSet(i)]; }
return false;
}

std::vector<uint64_t> get(uint64_t i) {
std::vector<uint64_t> ans;
ans.push_back(get_min(i));
ans.push_back(get_max(i));
ans.push_back(size(i));
return ans;
}

uint64_t size(uint64_t i) { return setSize[findSet(i)]; }

uint64_t get_max(uint64_t i) { return maxElement[findSet(i)]; }

uint64_t get_min(uint64_t i) { return minElement[findSet(i)]; }
};

#endif
9 changes: 6 additions & 3 deletions src/classes/heap/min_heap.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef MIN_HEAP_H
#define MIN_HEAP_H
#ifdef __cplusplus
#include <functional>
#include <iostream>
#include <vector>
#endif



template <typename T> class min_heap {
private:
T *arr;
Expand Down Expand Up @@ -89,4 +90,6 @@ template <typename T> class min_heap {
heapify(minim);
}
}
};
};

#endif
5 changes: 5 additions & 0 deletions src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef DOUBLY_LINKED_LIST_H
#define DOUBLY_LINKED_LIST_H

#ifdef __cplusplus
#include "../../plotting/iterator/list_iterator.h"
#include <iostream>
Expand Down Expand Up @@ -99,3 +102,5 @@ template <typename T> class doubly_linked_list {
return p;
}
};

#endif
5 changes: 5 additions & 0 deletions src/classes/list/linked_list.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#ifdef __cplusplus
#include "../../plotting/iterator/list_iterator.h"
#include <iostream>
Expand Down Expand Up @@ -117,3 +120,5 @@ template <typename T> class linked_list {
std::shared_ptr<__link<T>> root;
std::shared_ptr<__link<T>> tail;
};

#endif
7 changes: 6 additions & 1 deletion src/classes/list/skip_list.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef SKIP_LIST_H
#define SKIP_LIST_H

#ifdef __cplusplus
#include "../../plotting/iterator/list_iterator.h"
#include <iostream>
Expand Down Expand Up @@ -102,4 +105,6 @@ template <typename T> class skip_list {

int level;
std::shared_ptr<node> root;
};
};

#endif
5 changes: 5 additions & 0 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef AVL_TREE_H
#define AVL_TREE_H

#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
Expand Down Expand Up @@ -271,3 +274,5 @@ template <typename T> class avl_tree {
return __s;
}
};

#endif
5 changes: 5 additions & 0 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef BST_H
#define BST_H

#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
Expand Down Expand Up @@ -227,3 +230,5 @@ template <typename T> class bst {
return __s;
}
};

#endif
7 changes: 6 additions & 1 deletion src/plotting/iterator/list_iterator.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef LIST_ITERATOR_H
#define LIST_ITERATOR_H

#ifdef __cplusplus
#include "list_link.h"
#endif
Expand Down Expand Up @@ -102,4 +105,6 @@ template <typename T> class doubly_list_iter {

private:
std::shared_ptr<doubly_link<T>> curr;
};
};

#endif
7 changes: 6 additions & 1 deletion src/plotting/iterator/list_link.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef LIST_LINK_H
#define LIST_LINK_H

#ifdef __cplusplus
#include <iostream>
#endif
Expand Down Expand Up @@ -63,4 +66,6 @@ template <typename T> class doubly_link {
*Returns the previous pointer of the doubly_link type.
*/
std::shared_ptr<doubly_link> &prev() { return pprev; }
};
};

#endif
21 changes: 21 additions & 0 deletions tests/algorithms/gcd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define CATCH_CONFIG_MAIN
#include "../../src/algorithms/number_theory/gcd.h"
#include "../catch2/catch.hpp"
#include <string>

using namespace gcd;

TEST_CASE("testing binary gcd") {
int64_t a = 100, b = 4;
REQUIRE(binary_gcd(a, b) == 4);
REQUIRE(binary_gcd(b, a) == 4);
REQUIRE(euclidean_gcd(a, b) == 4);
a = 100;
b = 400;
REQUIRE(binary_gcd(a, b) == 100);
REQUIRE(euclidean_gcd(a, b) == 100);
a = 90;
b = 85430;
REQUIRE(binary_gcd(a, b) == 10);
REQUIRE(euclidean_gcd(a, b) == 10);
}

0 comments on commit 1d13b2d

Please sign in to comment.