Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Graphlib, the one stop, C++ 23/20, header-only library for directional (and soon undirectional) graphs!

# Graphlib 🇬
Graphlib is a C++ library that empowers users to create and edit graphs!

If you want to contribute to GRAPHLIB, check out the CONTRIBUTING.md file!
Expand Down
19 changes: 19 additions & 0 deletions detail/add_edges.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@ void add_edge(const std::vector<node>& new_value, const node& key, const std::st
}
create_graph<node>(graph, input_file);
}

// Added in-memory abilities
template<typename node>

void add_edge(const std::vector<node>& new_value, const node& key, std::unordered_map<node, std::vector<node>>& graph, std::string& input_file) {
auto g_it = graph.find(key);

if (g_it == graph.end()) {
std::cout << "Key doesnt exist" << "\n";
return;
}

for (const auto& neighbor : new_value) {
if (std::find(g_it->second.begin(), g_it->second.end(), neighbor) == g_it->second.end()) {
g_it->second.push_back(neighbor);
}
}
create_graph<node>(graph, input_file);
}
}
20 changes: 20 additions & 0 deletions detail/add_edges_weighted.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ void add_edge(const std::vector<std::pair<node, weights>>& new_value, const node
}
create_graph<node, weights>(graph, input_file);
}

template<typename node, typename weights>

requires Number<weights>
void add_edge(const std::vector<std::pair<node, weights>>& new_value, const node& key, std::unordered_map<node, std::vector<std::pair<node, weights>>> graph, std::string& input_file) {
auto g_it = graph.find(key);

if (g_it == graph.end()) {
std::cout << "Key doesnt exist" << "\n";
return;
}

for (const auto &[neighbor, weight] : new_value) {
auto ex = std::find_if(g_it->second.begin(), g_it->second.end(), [neighbor](auto edge) {return edge.first == neighbor;});
if (ex == g_it->second.end()) {
g_it->second.push_back({neighbor, weight});
}
}
create_graph<node, weights>(graph, input_file);
}
}
32 changes: 32 additions & 0 deletions detail/bfs.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,36 @@ std::vector<node> bfs_algorithm(const node& starting_node, const std::string& in
}
return return_graph;
}

// Added in-memory abilities
template<typename node>

std::vector<node> bfs_algorithm(const node& starting_node, std::unordered_map<node, std::vector<node>>& graph) {

std::queue<node> q;
std::unordered_set<node> visited;
visited.insert(starting_node);
q.push(starting_node);

std::vector<node> return_graph;

while (!q.empty()) {
node node2 = q.front();
q.pop();
return_graph.push_back(node2);

auto g_it = graph.find(node2);
if (g_it == graph.end()) {
continue;
}

for (const auto& neighbor : g_it->second) {
if (visited.find(neighbor) == visited.end()) {
visited.insert(neighbor);
q.push(neighbor);
}
}
}
return return_graph;
}
}
33 changes: 33 additions & 0 deletions detail/dfs.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,37 @@ std::vector<node> dfs_algorithm(const node& starting_value, const std::string& i
}
return return_vector;
}

// Added in-memory abilities
template<typename node>

std::vector<node> dfs_algorithm(const node& starting_value, const std::unordered_map<node, std::vector<node>>& graph) {
std::unordered_set<node> visited;
std::stack<node> stack_of_numbers;

stack_of_numbers.push(starting_value);

std::vector<node> return_vector;

while (!stack_of_numbers.empty()) {
node current = stack_of_numbers.top();
stack_of_numbers.pop();

if (visited.contains(current)) {
continue;
}

visited.insert(current);
return_vector.push_back(current);

auto it = graph.find(current);
if (it == graph.end()) {
continue;
}
for (const auto& neighbor : it->second) {
stack_of_numbers.push(neighbor);
}
}
return return_vector;
}
}
42 changes: 42 additions & 0 deletions detail/dijkstra.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,46 @@ std::vector<node> dijkstras_algorithm(const node starting_node, const std::strin
}
return return_graph;
}

// Added in-memory abilities
template<typename node, typename weights>

requires Number<weights>
std::vector<node> dijkstras_algorithm(const node starting_node, std::unordered_map<node, std::vector<std::pair<node, weights>>>& graph) {
std::priority_queue<std::pair<weights, node>, std::vector<std::pair<weights, node>>, std::greater<std::pair<weights, node>>> q;
std::unordered_map<node, weights> visited;
visited[starting_node] = 0;
q.push(std::make_pair(0, starting_node));

std::vector<node> return_graph;

while (!q.empty()) {
auto [dist, current] = q.top();
q.pop();

auto v_it = visited.find(current);
if (v_it != visited.end() && dist > v_it->second) {
continue;
}

return_graph.push_back(current);

auto g_it = graph.find(current);
if (g_it == graph.end()) {
continue;
}

for (const auto& [n, w] : g_it->second) {
weights new_distance = dist + w;

auto n_it = visited.find(n);

if (n_it == visited.end() || new_distance < n_it->second) {
visited[n] = new_distance;
q.push({new_distance, n});
}
}
}
return return_graph;
}
}
15 changes: 15 additions & 0 deletions include/graphlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ template<typename node, typename weights>

requires Number<weights>
std::vector<node> dijkstras_algorithm(const node starting_node, const std::string& input_file);

// In memory
template<typename node, typename weights>
requires Number<weights>
void add_edge(const std::vector<std::pair<node, weights>>& new_value, const node& key, std::unordered_map<node, std::vector<std::pair<node, weights>>>& graph);
template<typename node>
void add_edge(const std::vector<node>& new_value, const node& key, std::unordered_map<node, std::vector<node>>& graph);

template<typename node>
std::vector<node> bfs_algorithm(const node& starting_node, std::unordered_map<node, std::vector<node>>& graph);
template<typename node>
std::vector<node> dfs_algorithm(const node& starting_value, const std::unordered_map<node, std::vector<node>>& graph);
template<typename node, typename weights>
requires Number<weights>
std::vector<node> dijkstras_algorithm(const node starting_node, std::unordered_map<node, std::vector<std::pair<node, weights>>>& graph);
}

#include "../detail/txt_to_un_map.tpp"
Expand Down
Loading
Loading