From 00cd4d0b3541c1026a4c0f8d66d953690299178a Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Fri, 10 Jan 2025 07:51:36 -0500 Subject: Add code for sem12 --- Seminar12/src/shortest_paths/shp_no_wight.cpp | 82 +++++++++++++++++ Seminar12/src/shortest_paths/shp_weighted.cpp | 128 ++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 Seminar12/src/shortest_paths/shp_no_wight.cpp create mode 100644 Seminar12/src/shortest_paths/shp_weighted.cpp (limited to 'Seminar12/src/shortest_paths') diff --git a/Seminar12/src/shortest_paths/shp_no_wight.cpp b/Seminar12/src/shortest_paths/shp_no_wight.cpp new file mode 100644 index 0000000..a05e517 --- /dev/null +++ b/Seminar12/src/shortest_paths/shp_no_wight.cpp @@ -0,0 +1,82 @@ +#include "../graph_impl/graph/graph.h" +#include +#include +#include +#include + +using vertex_t = graph::vertex_t; + +std::vector shortest_path(const graph& g, vertex_t from, vertex_t to) +{ + std::queue q; + q.push(from); + + std::unordered_map parent; + bool found_path = false; + + while(!q.empty() && !found_path) + { + vertex_t current = q.front(); + q.pop(); + + if(current == to) + { + found_path = true; + continue; + } + + const auto& adjacent = g.adjacent(current); + + for(const auto& vertex : adjacent) + { + if(parent.find(vertex) == parent.end()) + { + // This means the vertex is yet to be visited... + q.push(vertex); + parent[vertex] = current; + } + } + } + + if(!found_path) + return {}; + + std::vector path; + vertex_t it = to; + + while (true) + { + auto child_it = parent.find(it); + + if(child_it == parent.end()) + break; + + path.push_back(it); + it = child_it->second; + } + + path.push_back(from); + std::reverse(path.begin(), path.end()); + + return path; +} + +int main() +{ + graph g(6, false); + + g.add_edge(0, 1); + g.add_edge(0, 2); + g.add_edge(1, 2); + g.add_edge(1, 3); + g.add_edge(2, 4); + g.add_edge(3, 4); + g.add_edge(4, 5); + + auto path = shortest_path(g, 0, 5); + + for(auto vertex : path) + { + std::cout << vertex << " "; + } +} \ No newline at end of file diff --git a/Seminar12/src/shortest_paths/shp_weighted.cpp b/Seminar12/src/shortest_paths/shp_weighted.cpp new file mode 100644 index 0000000..66c06f6 --- /dev/null +++ b/Seminar12/src/shortest_paths/shp_weighted.cpp @@ -0,0 +1,128 @@ +#include "../graph_impl/weighted_graph/weighted_graph.h" +#include +#include +#include +#include +#include + +using vertex_t = weighted_graph::vertex_t; +using vertex_index_t = weighted_graph::vertex_index_t; +using weight_t = weighted_graph::weight_t; + +// A structure representind information which we +// obtained using dijkstra's algorithm +struct shp_info +{ + std::vector path; + size_t cost = 0; +}; + +bool relax(std::vector& distance, + const weighted_graph& g, + vertex_index_t reached, + vertex_index_t current_vertex) +{ + if (distance[reached] > distance[current_vertex] + g.get_weight(current_vertex, reached)) + { + distance[reached] = distance[current_vertex] + g.get_weight(current_vertex, reached); + return true; + } + return false; +} + +shp_info dijkstra_shortest_path(const weighted_graph& g, vertex_index_t from, vertex_index_t to) +{ + std::vector distances(g.vertex_count(), INT_MAX); + std::vector parent(g.vertex_count(), -1); + + struct vertex_comparator + { + bool operator()(const vertex_t& lhs, const vertex_t& rhs) const + { + return lhs.second > rhs.second; + } + }; + + std::priority_queue, vertex_comparator> q; + + q.push({from, 0}); + distances[from] = 0; + bool found_path = false; + + while(!q.empty() && !found_path) + { + auto current_vertex = q.top(); + q.pop(); + + if (current_vertex.first == to) + { + found_path = true; + continue; + } + + const auto& adjacent = g.adjacent(current_vertex.first); + + for(const auto& vertex : adjacent) + { + if(relax(distances, g, vertex.first, current_vertex.first)) + { + q.push({vertex.first, distances[vertex.first]}); + parent[vertex.first] = current_vertex.first; + } + } + } + + shp_info result; + + if(!found_path) + return result; + + vertex_index_t it = to; + while (true) + { + vertex_index_t it_parent = parent[it]; + result.path.push_back(it); + it = it_parent; + + if(it_parent == -1) + { + break; + } + } + + result.cost = distances[to]; + std::reverse(result.path.begin(), result.path.end()); + + return result; +} + +int main() +{ + weighted_graph g(6, false); + g.add_edge(0, 1, 2); + g.add_edge(0, 2, 1); + g.add_edge(2, 3, 6); + g.add_edge(1, 4, 3); + g.add_edge(3, 5, 1); + g.add_edge(4, 5, 2); + g.add_edge(2, 5, 4); + + auto result = dijkstra_shortest_path(g, 0, 5); + + if (result.path.empty()) + { + std::cout << "No path found from 0 to 5.\n"; + } + else + { + std::cout << "Shortest path cost: " << result.cost << "\n"; + std::cout << "Path: "; + for (int vertex : result.path) + { + std::cout << vertex << " "; + } + std::cout << "\n"; + } + + return 0; +} \ No newline at end of file -- cgit v1.3.1-26-gb76a