Skip to content

Commit

Permalink
added prim algo for graph
Browse files Browse the repository at this point in the history
AVL tree needs change to shared_ptr in order to generate the iterator. Every data structure need to be converted to smart pointers.
  • Loading branch information
spirosmaggioros committed Jan 17, 2024
1 parent 0d246bd commit 9bad71a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
"stack": "cpp",
"unordered_set": "cpp",
"__errc": "cpp",
"__mutex_base": "cpp"
"__mutex_base": "cpp",
"__tree": "cpp",
"fstream": "cpp",
"future": "cpp",
"iomanip": "cpp",
"map": "cpp",
"regex": "cpp",
"set": "cpp",
"thread": "cpp"
}
}
4 changes: 4 additions & 0 deletions examples/tree/avl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ int main() {
std::cout << x << " ";
}
std::cout << '\n';

avl_tree<int> b1({5, 9, -5, 3});
std::cout << "iterators for avl trees" << '\n';
std::cout << b << '\n';
}
30 changes: 29 additions & 1 deletion src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ template <typename T> class weighted_graph {

std::vector<T> topological_sort();

int64_t prim(T start);

private:
std::unordered_map<T, std::vector<std::pair<T, int64_t>>> adj;
std::string __type;
Expand All @@ -260,7 +262,7 @@ template <typename T> int64_t weighted_graph<T>::shortest_path(T start, T end) {
return -1;
}

if (!cycle()) {
if (!cycle() && __type == "directed") {
std::vector<T> top_sort = topological_sort();
std::reverse(top_sort.begin(), top_sort.end());
std::stack<T> s;
Expand Down Expand Up @@ -447,4 +449,30 @@ template <typename T> std::vector<T> weighted_graph<T>::topological_sort() {

return top_sort;
}

template <typename T> int64_t weighted_graph<T>::prim(T __temp) {
std::priority_queue<std::pair<T, int64_t>, std::vector<std::pair<T, int64_t>>,
std::greater<std::pair<T, int64_t>>>
q;
std::unordered_map<T, bool> visited;
int64_t cost = 0;
q.push(std::make_pair(0, __temp));
while (!q.empty()) {
std::pair<T, int64_t> current = q.top();
q.pop();
__temp = current.first;
if (visited.find(__temp) != visited.end()) {
continue;
}
cost += current.second;
visited[__temp] = true;
for (std::pair<T, int64_t> &x : adj[current.first]) {
if (visited.find(x.first) == visited.end()) {
q.push(x);
}
}
}
return cost;
}

#endif
16 changes: 16 additions & 0 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#ifdef __cplusplus
#include "../../plotting/iterator/avl_tree_iterator.h"
#include <functional>
#include <vector>
#endif
Expand All @@ -14,6 +15,10 @@ template <typename T> class avl_tree {
}
~avl_tree() noexcept {}

avl_iter<T> begin() { return avl_iter<T>(root); }

avl_iter<T> end() { return avl_iter<T>(nullptr); }

void insert(T key) { root = __insert(root, key); }

void remove(T key) { root = __remove(root, key); }
Expand All @@ -39,6 +44,17 @@ template <typename T> class avl_tree {
return path;
}

friend std::ostream &operator<<(std::ostream &out, avl_tree<T> &t1) {
out << '{';
avl_iter<T> it = t1.begin();
it++;
for (; it != t1.end(); it++) {
out << *it << ' ';
}
out << '}' << '\n';
return out;
}

private:
typedef struct node {
T info;
Expand Down
55 changes: 55 additions & 0 deletions src/plotting/iterator/avl_tree_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifdef __cplusplus
#include <iostream>
#include <vector>
#endif

template <typename T> class avl_iter {
public:
avl_iter() noexcept {
__elements = _get_inorder(root);
index = 0;
}
avl_iter(const node *p) noexcept : root(p) {}
~avl_iter();

avl_iter &operator++() {
if (index >= 0 && index < __elements.size()) {
index++;
}
return *this;
}

avl_iter operator++(int) {
avl_iter it = *this;
++*this;
return it;
}

bool operator!=(const avl_iter &it) { return root != it.root; }
T operator*() { return __elements[index]; }

private:
typedef struct node {
T info;
int64_t height;
struct node *left;
struct node *right;
} node;
node *root;
std::vector<T> __elements;
int64_t index;

std::vector<T> __get_inorder(node *root) {
std::vector<T> p;
__inorder(root, p);
return p;
}

void __inorder(node *root, std::vector<T> &p) {
if (root) {
__inorder(root->left, p);
p.push_back(root->info);
__inorder(root->right, p);
}
}
};
10 changes: 10 additions & 0 deletions tests/graph/weighted_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ TEST_CASE("testing shortest path in DAG") {
g3.add_edge(1, 6, 1);
g3.add_edge(3, 2, 4);
REQUIRE(g3.shortest_path(0, 6) == 1);
}

TEST_CASE("testing mst with prim's algo") {
weighted_graph<int> g("directed");
g.add_edge(1, 2, 2);
g.add_edge(0, 1, 0);
g.add_edge(2, 6, 3);
g.add_edge(1, 6, 1);
g.add_edge(3, 2, 4);
REQUIRE(g.prim(0) == 3);
}

0 comments on commit 9bad71a

Please sign in to comment.