From 3c9e526045ed4d379587c3c6434e0ae017a07485 Mon Sep 17 00:00:00 2001 From: Dmitry Krivoruchko <144147209+UmbrellaLeaf5@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:34:30 +0300 Subject: [PATCH] update graph class (code simplify and beautify) --- lib/src/graph.hpp | 116 ++++++++++------------- lib/src/utils.hpp | 5 +- task_01/src/test.cpp | 106 ++++++++++----------- task_02/src/main.cpp | 8 +- task_02/src/test.cpp | 76 +++++++-------- task_04/src/main.cpp | 18 ++-- task_04/src/test.cpp | 216 +++++++++++++++++++++---------------------- 7 files changed, 262 insertions(+), 283 deletions(-) diff --git a/lib/src/graph.hpp b/lib/src/graph.hpp index 6b21a7f..29cb990 100644 --- a/lib/src/graph.hpp +++ b/lib/src/graph.hpp @@ -100,8 +100,7 @@ class Graph { std::vector edges{}; edges.reserve(edges_pairs.size()); - for (const auto& edge : edges_pairs) - edges.push_back(Edge(edge.first, edge.second)); + for (const auto& edge : edges_pairs) edges.push_back(edge); return Graph(edges); } @@ -294,13 +293,7 @@ class Graph { } /// @brief Проверяет, взвешен ли граф - bool IsWeighted() const { - if (edges_.empty()) return false; - - bool is_weighted = true; - for (const auto& edge : edges_) is_weighted &= edge.IsWeighted(); - return is_weighted; - } + bool IsWeighted() const { return is_weighted_; } /// @return `size_t`: кол-во вершин size_t VertsAmount() const { return verts_.size(); } @@ -367,11 +360,8 @@ class Graph { return os; } - /** - * @brief Делает граф ненаправленным (удаляет лишние ребра) - * @param remove_duplicates: удалять ли дубликаты - */ - void MakeUndirected(bool remove_duplicates = false) { + /// @brief Делает граф ненаправленным (удаляет лишние ребра) + void MakeUndirected() { std::unordered_set seen_edges; std::vector unique_edges; unique_edges.reserve(EdgesAmount()); @@ -391,8 +381,6 @@ class Graph { edges_ = std::move(unique_edges); is_direct_ = false; - - if (remove_duplicates) RemoveDuplicates(); } /// @brief Делает граф направленным (ничего) @@ -401,19 +389,6 @@ class Graph { /// @brief Проверяет, направлен ли граф bool IsDirected() const { return is_direct_; } - /// @brief Удаляет из графа ребрами с одинаковым вершинами - void RemoveDuplicates() { - std::vector unique_edges; - unique_edges.reserve(EdgesAmount()); - - for (const auto& edge : edges_) - if (!Contains(unique_edges, edge)) unique_edges.push_back(edge); - - edges_ = std::move(unique_edges); - - if (!IsDirected()) MakeUndirected(); - } - /** * @return `std::vector>`: список смежности * @throw `std::logic_error("GetAdjListWithoutKeys: this method is deleted @@ -495,8 +470,6 @@ class Graph { * @return `true`: содержится * @return `false`: не содержится * @throw `std::logic_error("ContainsEdge: graph is not weighted.")` - * @throw `std::logic_error("ContainsEdge: weight must be greater than - * zero.")` */ bool ContainsEdge(const std::tuple& edge) const { if (!IsWeighted()) @@ -560,33 +533,22 @@ class Graph { if (!Contains(verts_, vert)) verts_.push_back(vert); } - /// @throw `std::invalid_argument(std::string("AddEdge: ") + ex.what())` - void AddEdge(vert_t start_vert, vert_t end_vert, weight_t weight) { - AddVert(start_vert); - AddVert(end_vert); - - try { - edges_.emplace_back(Edge(start_vert, end_vert, weight)); - } - - catch (const std::exception& ex) { - throw std::invalid_argument(std::string("AddEdge: ") + ex.what()); - } + /// @warning `"AddEdge: weighted graph must consist of weighted edges.` + void AddEdge(const std::tuple& edge_tuple) { + if (WeightFromTuple(edge_tuple) == 0) + AddEdge({StartVertFromTuple(edge_tuple), EndVertFromTuple(edge_tuple)}); + else + AddEdge_(edge_tuple); } - /** - * @throw `std::logic_error("AddEdge: weighted graph must consist of - * weighted edges.")` - */ - void AddEdge(vert_t start_vert, vert_t end_vert) { + /// @warning `"AddEdge: weighted graph must consist of weighted edges.` + void AddEdge(const std::pair& edge_pair) { if (IsWeighted()) - throw std::logic_error( - "AddEdge: weighted graph must consist of weighted edges."); + std::cerr << "Warning! AddEdge: weighted graph should consist of " + "weighted edges." + << std::endl; - AddVert(start_vert); - AddVert(end_vert); - - edges_.emplace_back(Edge(start_vert, end_vert)); + AddEdge_(Edge(edge_pair.first, edge_pair.second, static_cast(0))); } /// @throw `std::invalid_argument("RemoveVert: there is no such vert:")` @@ -622,8 +584,7 @@ class Graph { edges_.erase(std::remove_if(edges_.begin(), edges_.end(), [&edge_pair, this](const Edge& e) { - return (Edge(e.StartVert(), e.EndVert()) == - Edge(edge_pair)) || + return (e == Edge(edge_pair)) || (!IsDirected() && Edge(e.EndVert(), e.StartVert()) == Edge(edge_pair)); @@ -656,16 +617,21 @@ class Graph { public: Edge() = delete; - Edge(vert_t start_vert, vert_t end_vert) + Edge(const vert_t start_vert, const vert_t& end_vert) : start_vert_{start_vert}, end_vert_{end_vert} {} - Edge(vert_t start_vert, vert_t end_vert, weight_t weight) + Edge(const vert_t& start_vert, vert_t end_vert, weight_t weight) : start_vert_{start_vert}, end_vert_{end_vert}, weight_{weight} {} - Edge(std::pair edge_pair) + Edge(const std::pair& edge_pair) : start_vert_{edge_pair.first}, end_vert_{edge_pair.second} {} - Edge(std::tuple edge_tuple) + Edge(const std::pair& edge_pair, weight_t weight) + : start_vert_{edge_pair.first}, + end_vert_{edge_pair.second}, + weight_{weight} {} + + Edge(const std::tuple& edge_tuple) : start_vert_{StartVertFromTuple(edge_tuple)}, end_vert_{EndVertFromTuple(edge_tuple)}, weight_{WeightFromTuple(edge_tuple)} {} @@ -679,11 +645,13 @@ class Graph { void SetWeight(weight_t new_weight) { weight_ = new_weight; } - // friend Graph; - bool operator==(const Edge& rhs) const { - return start_vert_ == rhs.start_vert_ && end_vert_ == rhs.end_vert_ && - weight_ == rhs.weight_; + if (StartVert() != rhs.StartVert() || EndVert() != rhs.EndVert()) + return false; + + if (IsWeighted() && rhs.IsWeighted()) return Weight() == rhs.Weight(); + + return true; } bool operator!=(const Edge& rhs) const { return !(*this == rhs); } @@ -727,6 +695,7 @@ class Graph { std::vector verts_; bool is_direct_ = true; + bool is_weighted_ = false; public: friend std::ostream& operator<<(std::ostream& os, @@ -736,9 +705,14 @@ class Graph { } private: - Graph(const std::vector& edges) : edges_{edges}, verts_() { + Graph(const std::vector& edges) { if (edges.empty()) return; + for (const auto& edge : edges) { + if (edge.IsWeighted()) is_weighted_ = true; + AddEdge_(edge); + } + if constexpr (std::is_integral_v) { // кол-во вершин = максимальная вершина среди ребер, т.е. в этом случае // происходит заполнение вершин до наибольшей из них в списке ребер @@ -752,7 +726,7 @@ class Graph { verts_.resize(max_vert + 1); std::iota(verts_.begin(), verts_.end(), 0); - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) for (const auto& edge : edges_) { if (!Contains(Verts(), edge.StartVert())) verts_.push_back(edge.StartVert()); @@ -760,9 +734,15 @@ class Graph { if (!Contains(Verts(), edge.EndVert())) verts_.push_back(edge.EndVert()); } - } + } + + void AddEdge_(const Edge& edge) { + AddVert(edge.StartVert()); + AddVert(edge.EndVert()); + + if (!Contains(edges_, edge)) edges_.emplace_back(edge); - if (!IsDirected()) MakeUndirected(); + if (edge.Weight() != 0) is_weighted_ = true; } static std::pair ParseEdgeString_( diff --git a/lib/src/utils.hpp b/lib/src/utils.hpp index 7480e10..59853a4 100644 --- a/lib/src/utils.hpp +++ b/lib/src/utils.hpp @@ -174,7 +174,6 @@ inline std::istream& operator>>(std::istream& is, std::vector& vec) { * @return false: элемент не найден */ template -bool Contains(const std::vector& vec, const T& value) { - auto it = std::find(vec.begin(), vec.end(), value); - return it != vec.end(); +inline bool Contains(const std::vector& vec, const T& value) { + return std::find(vec.begin(), vec.end(), value) != vec.end(); } diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index b6306ce..239b178 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -12,12 +12,12 @@ TEST(TSTest, TopologicalSort) { graph.AddVert("E"); graph.AddVert("F"); - graph.AddEdge("A", "B", 1); - graph.AddEdge("A", "C", 2); - graph.AddEdge("B", "D", 3); - graph.AddEdge("C", "E", 4); - graph.AddEdge("D", "F", 5); - graph.AddEdge("E", "F", 6); + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"A", "C", 2}); + graph.AddEdge({"B", "D", 3}); + graph.AddEdge({"C", "E", 4}); + graph.AddEdge({"D", "F", 5}); + graph.AddEdge({"E", "F", 6}); std::vector expected_order = {"A", "C", "E", "B", "D", "F"}; @@ -55,10 +55,10 @@ TEST(TSTest, TopologicalSort_MultipleSources) { graph.AddVert("D"); graph.AddVert("E"); - graph.AddEdge("A", "C", 1); - graph.AddEdge("B", "C", 2); - graph.AddEdge("C", "D", 3); - graph.AddEdge("C", "E", 4); + graph.AddEdge({"A", "C", 1}); + graph.AddEdge({"B", "C", 2}); + graph.AddEdge({"C", "D", 3}); + graph.AddEdge({"C", "E", 4}); std::vector expected_order = {"B", "A", "C", "E", "D"}; @@ -75,8 +75,8 @@ TEST(TSTest, TopologicalSort_Disconnected) { graph.AddVert("D"); graph.AddVert("E"); - graph.AddEdge("A", "B", 1); - graph.AddEdge("C", "D", 2); + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"C", "D", 2}); std::vector expected_order = {"E", "C", "D", "A", "B"}; @@ -93,10 +93,10 @@ TEST(TSTest, TopologicalSort_LinearChain) { graph.AddVert("D"); graph.AddVert("E"); - graph.AddEdge("A", "B", 1); - graph.AddEdge("B", "C", 2); - graph.AddEdge("C", "D", 3); - graph.AddEdge("D", "E", 4); + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"B", "C", 2}); + graph.AddEdge({"C", "D", 3}); + graph.AddEdge({"D", "E", 4}); std::vector expected_order = {"A", "B", "C", "D", "E"}; @@ -115,12 +115,12 @@ TEST(TSTest, TopologicalSort_Tree) { graph.AddVert("F"); graph.AddVert("G"); - graph.AddEdge("A", "B", 1); - graph.AddEdge("A", "C", 2); - graph.AddEdge("B", "D", 3); - graph.AddEdge("B", "E", 4); - graph.AddEdge("C", "F", 5); - graph.AddEdge("C", "G", 6); + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"A", "C", 2}); + graph.AddEdge({"B", "D", 3}); + graph.AddEdge({"B", "E", 4}); + graph.AddEdge({"C", "F", 5}); + graph.AddEdge({"C", "G", 6}); std::vector expected_order = {"A", "C", "G", "F", "B", "E", "D"}; @@ -139,13 +139,13 @@ TEST(TSTest, TopologicalSort_Branching) { graph.AddVert("F"); graph.AddVert("G"); - graph.AddEdge("A", "B", 1); - graph.AddEdge("A", "C", 2); - graph.AddEdge("B", "D", 3); - graph.AddEdge("B", "E", 4); - graph.AddEdge("C", "F", 5); - graph.AddEdge("C", "G", 6); - graph.AddEdge("D", "F", 7); // Добавляем "ветку" + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"A", "C", 2}); + graph.AddEdge({"B", "D", 3}); + graph.AddEdge({"B", "E", 4}); + graph.AddEdge({"C", "F", 5}); + graph.AddEdge({"C", "G", 6}); + graph.AddEdge({"D", "F", 7}); // Добавляем "ветку" std::vector expected_order = {"A", "C", "G", "B", "E", "D", "F"}; @@ -182,9 +182,9 @@ TEST(TSTest, TopologicalSort_Cyclic) { graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2); - graph.AddEdge(2, 3); - graph.AddEdge(3, 1); + graph.AddEdge({1, 2}); + graph.AddEdge({2, 3}); + graph.AddEdge({3, 1}); ASSERT_THROW(TopologicalSort(graph), std::invalid_argument); } @@ -201,14 +201,14 @@ TEST(TSTest, TopologicalSort_Cyclic) { graph.AddVert("H"); graph.AddVert("A"); - graph.AddEdge("E", "B"); - graph.AddEdge("A", "G"); - graph.AddEdge("G", "F"); - graph.AddEdge("D", "H"); - graph.AddEdge("H", "G"); - graph.AddEdge("B", "F"); - graph.AddEdge("G", "E"); - graph.AddEdge("F", "D"); + graph.AddEdge({"E", "B"}); + graph.AddEdge({"A", "G"}); + graph.AddEdge({"G", "F"}); + graph.AddEdge({"D", "H"}); + graph.AddEdge({"H", "G"}); + graph.AddEdge({"B", "F"}); + graph.AddEdge({"G", "E"}); + graph.AddEdge({"F", "D"}); ASSERT_THROW(TopologicalSort(graph), std::invalid_argument); } @@ -228,19 +228,19 @@ TEST(TSTest, TopologicalSort_Cyclic) { graph.AddVert(10); graph.AddVert(11); - graph.AddEdge(1, 10); - graph.AddEdge(10, 11); - graph.AddEdge(5, 1); - graph.AddEdge(5, 6); - graph.AddEdge(4, 8); - graph.AddEdge(2, 3); - graph.AddEdge(10, 9); - graph.AddEdge(4, 5); - graph.AddEdge(5, 7); - graph.AddEdge(4, 7); - graph.AddEdge(1, 2); - graph.AddEdge(3, 4); - graph.AddEdge(1, 6); + graph.AddEdge({1, 10}); + graph.AddEdge({10, 11}); + graph.AddEdge({5, 1}); + graph.AddEdge({5, 6}); + graph.AddEdge({4, 8}); + graph.AddEdge({2, 3}); + graph.AddEdge({10, 9}); + graph.AddEdge({4, 5}); + graph.AddEdge({5, 7}); + graph.AddEdge({4, 7}); + graph.AddEdge({1, 2}); + graph.AddEdge({3, 4}); + graph.AddEdge({1, 6}); ASSERT_THROW(TopologicalSort(graph), std::invalid_argument); } diff --git a/task_02/src/main.cpp b/task_02/src/main.cpp index fc58981..2449cce 100644 --- a/task_02/src/main.cpp +++ b/task_02/src/main.cpp @@ -5,10 +5,10 @@ int main() { Graph g; - g.AddEdge(0, 1); - g.AddEdge(0, 2); - g.AddEdge(1, 2); - g.AddEdge(3, 2); + g.AddEdge({0, 1}); + g.AddEdge({0, 2}); + g.AddEdge({1, 2}); + g.AddEdge({3, 2}); g.MakeUndirected(); diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index d457dda..8b79b1d 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -29,15 +29,15 @@ TEST(FindBridgesTest, EmptyGraph) { TEST(FindBridgesTest, SimpleGraphs) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(1, 2); + graph.AddEdge({0, 1}); + graph.AddEdge({1, 2}); auto bridges = FindBridges(graph); std::vector> answer = {{0, 1}, {1, 2}}; ASSERT_EQ(bridges, answer); - graph.AddEdge(2, 3); + graph.AddEdge({2, 3}); bridges = FindBridges(graph); answer = {{0, 1}, {1, 2}, {2, 3}}; @@ -50,12 +50,12 @@ TEST(FindBridgesTest, SimpleGraphs) { TEST(FindBridgesTest, MultipleBridges) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(0, 2); - graph.AddEdge(1, 2); - graph.AddEdge(2, 3); - graph.AddEdge(3, 4); - graph.AddEdge(4, 5); + graph.AddEdge({0, 1}); + graph.AddEdge({0, 2}); + graph.AddEdge({1, 2}); + graph.AddEdge({2, 3}); + graph.AddEdge({3, 4}); + graph.AddEdge({4, 5}); auto bridges = FindBridges(graph); std::vector> answer = { @@ -66,16 +66,16 @@ TEST(FindBridgesTest, MultipleBridges) { TEST(FindBridgesTest, ComplexGraph) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(0, 2); - graph.AddEdge(1, 2); - graph.AddEdge(1, 3); - graph.AddEdge(2, 3); - graph.AddEdge(3, 4); - graph.AddEdge(4, 5); - graph.AddEdge(4, 6); - graph.AddEdge(5, 6); - graph.AddEdge(6, 7); + graph.AddEdge({0, 1}); + graph.AddEdge({0, 2}); + graph.AddEdge({1, 2}); + graph.AddEdge({1, 3}); + graph.AddEdge({2, 3}); + graph.AddEdge({3, 4}); + graph.AddEdge({4, 5}); + graph.AddEdge({4, 6}); + graph.AddEdge({5, 6}); + graph.AddEdge({6, 7}); graph.MakeUndirected(); auto bridges = FindBridges(graph); @@ -106,15 +106,15 @@ TEST(FindCutVerticesTest, EmptyGraph) { TEST(FindCutVerticesTest, SimpleGraphs) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(1, 2); + graph.AddEdge({0, 1}); + graph.AddEdge({1, 2}); auto cut_vertices = FindCutVertices(graph); std::vector answer = {1}; ASSERT_EQ(cut_vertices, answer); - graph.AddEdge(2, 3); + graph.AddEdge({2, 3}); cut_vertices = FindCutVertices(graph); answer = {1, 2}; @@ -127,12 +127,12 @@ TEST(FindCutVerticesTest, SimpleGraphs) { TEST(FindCutVerticesTest, MultipleBridges) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(0, 2); - graph.AddEdge(1, 2); - graph.AddEdge(2, 3); - graph.AddEdge(3, 4); - graph.AddEdge(4, 5); + graph.AddEdge({0, 1}); + graph.AddEdge({0, 2}); + graph.AddEdge({1, 2}); + graph.AddEdge({2, 3}); + graph.AddEdge({3, 4}); + graph.AddEdge({4, 5}); graph.MakeUndirected(); @@ -144,16 +144,16 @@ TEST(FindCutVerticesTest, MultipleBridges) { TEST(FindCutVerticesTest, ComplexGraph) { Graph graph; - graph.AddEdge(0, 1); - graph.AddEdge(0, 2); - graph.AddEdge(1, 2); - graph.AddEdge(1, 3); - graph.AddEdge(2, 3); - graph.AddEdge(3, 4); - graph.AddEdge(4, 5); - graph.AddEdge(4, 6); - graph.AddEdge(5, 6); - graph.AddEdge(6, 7); + graph.AddEdge({0, 1}); + graph.AddEdge({0, 2}); + graph.AddEdge({1, 2}); + graph.AddEdge({1, 3}); + graph.AddEdge({2, 3}); + graph.AddEdge({3, 4}); + graph.AddEdge({4, 5}); + graph.AddEdge({4, 6}); + graph.AddEdge({5, 6}); + graph.AddEdge({6, 7}); graph.MakeUndirected(); auto cut_vertices = FindCutVertices(graph); diff --git a/task_04/src/main.cpp b/task_04/src/main.cpp index b9b51fb..1da3be1 100644 --- a/task_04/src/main.cpp +++ b/task_04/src/main.cpp @@ -5,12 +5,12 @@ int main() { Graph graph; - graph.AddEdge(0, 1, 4); - graph.AddEdge(0, 2, 1); - graph.AddEdge(1, 3, 1); - graph.AddEdge(2, 1, 2); - graph.AddEdge(2, 3, 5); - graph.AddEdge(3, 4, 3); + graph.AddEdge({0, 1, 4}); + graph.AddEdge({0, 2, 1}); + graph.AddEdge({1, 3, 1}); + graph.AddEdge({2, 1, 2}); + graph.AddEdge({2, 3, 5}); + graph.AddEdge({3, 4, 3}); auto distances = Dijkstra(graph, 0); @@ -21,9 +21,9 @@ int main() { Graph graph_str; - graph_str.AddEdge("A", "B", 4.0); - graph_str.AddEdge("A", "C", 2.0); - graph_str.AddEdge("B", "C", 1.0); + graph_str.AddEdge({"A", "B", 4.0}); + graph_str.AddEdge({"A", "C", 2.0}); + graph_str.AddEdge({"B", "C", 1.0}); auto distances_str = Dijkstra(graph_str, std::string("A")); std::cout << std::endl << "Res with A:" << std::endl; diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 9e675de..5e5de5a 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -28,13 +28,13 @@ inline bool operator!=(const std::unordered_map& lhs, TEST(DijkstraTest, SimpleDAG_A) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("A")); @@ -46,13 +46,13 @@ TEST(DijkstraTest, SimpleDAG_A) { TEST(DijkstraTest, SimpleDAG_B) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("B")); @@ -65,13 +65,13 @@ TEST(DijkstraTest, SimpleDAG_B) { TEST(DijkstraTest, SimpleDAG_C) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("C")); @@ -83,13 +83,13 @@ TEST(DijkstraTest, SimpleDAG_C) { TEST(DijkstraTest, SimpleDAG_D) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("D")); @@ -102,13 +102,13 @@ TEST(DijkstraTest, SimpleDAG_D) { TEST(DijkstraTest, SimpleDAG_E) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("E")); @@ -121,13 +121,13 @@ TEST(DijkstraTest, SimpleDAG_E) { TEST(DijkstraTest, SimpleDAG_F) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); auto distances = Dijkstra(graph, std::string("F")); @@ -140,13 +140,13 @@ TEST(DijkstraTest, SimpleDAG_F) { TEST(DijkstraTest, UnweightedDAG) { Graph graph; - graph.AddEdge("A", "B"); - graph.AddEdge("A", "C"); - graph.AddEdge("B", "D"); - graph.AddEdge("C", "D"); - graph.AddEdge("C", "E"); - graph.AddEdge("D", "F"); - graph.AddEdge("E", "F"); + graph.AddEdge({"A", "B"}); + graph.AddEdge({"A", "C"}); + graph.AddEdge({"B", "D"}); + graph.AddEdge({"C", "D"}); + graph.AddEdge({"C", "E"}); + graph.AddEdge({"D", "F"}); + graph.AddEdge({"E", "F"}); auto distances = Dijkstra(graph, std::string("A")); @@ -158,10 +158,10 @@ TEST(DijkstraTest, UnweightedDAG) { TEST(DijkstraTest, NonDirectedGraph) { Graph graph; - graph.AddEdge("A", "B"); - graph.AddEdge("A", "C"); - graph.AddEdge("B", "D"); - graph.AddEdge("C", "D"); + graph.AddEdge({"A", "B"}); + graph.AddEdge({"A", "C"}); + graph.AddEdge({"B", "D"}); + graph.AddEdge({"C", "D"}); graph.MakeUndirected(); @@ -177,10 +177,10 @@ TEST(DijkstraTest, NonDirectedGraph) { TEST(DijkstraTest, NonExistingStartVertex) { Graph graph; - graph.AddEdge("A", "B"); - graph.AddEdge("A", "C"); - graph.AddEdge("B", "D"); - graph.AddEdge("C", "D"); + graph.AddEdge({"A", "B"}); + graph.AddEdge({"A", "C"}); + graph.AddEdge({"B", "D"}); + graph.AddEdge({"C", "D"}); graph.MakeUndirected(); @@ -189,15 +189,15 @@ TEST(DijkstraTest, NonExistingStartVertex) { TEST(DijkstraTest, DAGWithNoIncomingEdges) { Graph graph; - graph.AddEdge("A", "B", 5); - graph.AddEdge("A", "C", 3); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 4); - graph.AddEdge("C", "E", 1); - graph.AddEdge("D", "F", 7); - graph.AddEdge("E", "F", 2); + graph.AddEdge({"A", "B", 5}); + graph.AddEdge({"A", "C", 3}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 4}); + graph.AddEdge({"C", "E", 1}); + graph.AddEdge({"D", "F", 7}); + graph.AddEdge({"E", "F", 2}); - graph.AddEdge("G", "F", 3); + graph.AddEdge({"G", "F", 3}); auto distances = Dijkstra(graph, std::string("A")); @@ -221,7 +221,7 @@ TEST(DijkstraTest, OneVertDAG) { TEST(DijkstraTest, TwoVertsDAG) { Graph graph; - graph.AddEdge("A", "B", 5); + graph.AddEdge({"A", "B", 5}); auto distances = Dijkstra(graph, std::string("A")); @@ -234,10 +234,10 @@ TEST(DijkstraTest, TwoVertsDAG) { TEST(DijkstraTest, MultiplePathsToOneVertex) { Graph graph; - graph.AddEdge("A", "B", 1); - graph.AddEdge("A", "C", 4); - graph.AddEdge("B", "D", 2); - graph.AddEdge("C", "D", 1); + graph.AddEdge({"A", "B", 1}); + graph.AddEdge({"A", "C", 4}); + graph.AddEdge({"B", "D", 2}); + graph.AddEdge({"C", "D", 1}); auto distances = Dijkstra(graph, std::string("A")); std::unordered_map expected_distances = { @@ -249,9 +249,9 @@ TEST(DijkstraTest, MultiplePathsToOneVertex) { TEST(DijkstraTest, IntVerticesAndWeights) { Graph graph; - graph.AddEdge(1, 2, 5); - graph.AddEdge(1, 3, 2); - graph.AddEdge(2, 3, 1); + graph.AddEdge({1, 2, 5}); + graph.AddEdge({1, 3, 2}); + graph.AddEdge({2, 3, 1}); auto distances = Dijkstra(graph, 1); std::unordered_map expected_distances = {{1, 0}, {2, 5}, {3, 2}}; @@ -262,9 +262,9 @@ TEST(DijkstraTest, IntVerticesAndWeights) { TEST(DijkstraTest, CharVerticesAndDoubleWeights) { Graph graph; - graph.AddEdge("A", "B", 3.5); - graph.AddEdge("A", "C", 1.5); - graph.AddEdge("B", "C", 2.5); + graph.AddEdge({"A", "B", 3.5}); + graph.AddEdge({"A", "C", 1.5}); + graph.AddEdge({"B", "C", 2.5}); auto distances = Dijkstra(graph, std::string("A")); @@ -277,9 +277,9 @@ TEST(DijkstraTest, CharVerticesAndDoubleWeights) { TEST(DijkstraTest, StringVerticesAndLongWeights) { Graph graph; - graph.AddEdge("A", "B", 10); - graph.AddEdge("A", "C", 5); - graph.AddEdge("B", "C", 2); + graph.AddEdge({"A", "B", 10}); + graph.AddEdge({"A", "C", 5}); + graph.AddEdge({"B", "C", 2}); auto distances = Dijkstra(graph, std::string("A")); @@ -307,8 +307,8 @@ TEST(DijkstraTest, SimpleUnweightedGraph) { graph.AddVert(1); graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2); - graph.AddEdge(2, 3); + graph.AddEdge({1, 2}); + graph.AddEdge({2, 3}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances.size(), 3); EXPECT_EQ(distances[1], 0); @@ -321,8 +321,8 @@ TEST(DijkstraTest, SimpleWeightedGraph) { graph.AddVert(1); graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2, 4); - graph.AddEdge(2, 3, 2); + graph.AddEdge({1, 2, 4}); + graph.AddEdge({2, 3, 2}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances.size(), 3); EXPECT_EQ(distances[1], 0); @@ -335,7 +335,7 @@ TEST(DijkstraTest, GraphWithDisconnectedVertices) { graph.AddVert(1); graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2, 4); + graph.AddEdge({1, 2, 4}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances.size(), 3); @@ -348,7 +348,7 @@ TEST(DijkstraTest, NegativeWeightException) { Graph graph; graph.AddVert(1); graph.AddVert(2); - graph.AddEdge(1, 2, -1); + graph.AddEdge({1, 2, -1}); EXPECT_THROW(Dijkstra(graph, 1), std::logic_error); } @@ -365,11 +365,11 @@ TEST(DijkstraTest, MoreComplexGraph) { graph.AddVert(3); graph.AddVert(4); graph.AddVert(5); - graph.AddEdge(1, 2, 4); - graph.AddEdge(1, 3, 2); - graph.AddEdge(2, 3, 1); - graph.AddEdge(3, 4, 5); - graph.AddEdge(4, 5, 3); + graph.AddEdge({1, 2, 4}); + graph.AddEdge({1, 3, 2}); + graph.AddEdge({2, 3, 1}); + graph.AddEdge({3, 4, 5}); + graph.AddEdge({4, 5, 3}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances.size(), 5); @@ -385,9 +385,9 @@ TEST(DijkstraTest, GraphWithCycle) { graph.AddVert(1); graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2, 2); - graph.AddEdge(2, 3, 3); - graph.AddEdge(3, 1, 1); + graph.AddEdge({1, 2, 2}); + graph.AddEdge({2, 3, 3}); + graph.AddEdge({3, 1, 1}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances[1], 0); EXPECT_EQ(distances[2], 2); @@ -399,8 +399,8 @@ TEST(DijkstraTest, MultipleShortestPaths) { graph.AddVert(1); graph.AddVert(2); graph.AddVert(3); - graph.AddEdge(1, 2, 4); - graph.AddEdge(1, 3, 4); + graph.AddEdge({1, 2, 4}); + graph.AddEdge({1, 3, 4}); auto distances = Dijkstra(graph, 1); EXPECT_EQ(distances[1], 0); EXPECT_EQ(distances[2], 4); @@ -410,18 +410,18 @@ TEST(DijkstraTest, MultipleShortestPaths) { TEST(DijkstraTest, LargerGraph) { Graph graph; - graph.AddEdge(1, 2, 2); - graph.AddEdge(1, 3, 5); - graph.AddEdge(2, 4, 4); - graph.AddEdge(2, 5, 1); - graph.AddEdge(3, 6, 3); - graph.AddEdge(3, 7, 2); - graph.AddEdge(4, 8, 6); - graph.AddEdge(5, 9, 3); - graph.AddEdge(7, 10, 4); - graph.AddEdge(10, 9, 1); - graph.AddEdge(8, 6, 2); - graph.AddEdge(9, 4, 9); + graph.AddEdge({1, 2, 2}); + graph.AddEdge({1, 3, 5}); + graph.AddEdge({2, 4, 4}); + graph.AddEdge({2, 5, 1}); + graph.AddEdge({3, 6, 3}); + graph.AddEdge({3, 7, 2}); + graph.AddEdge({4, 8, 6}); + graph.AddEdge({5, 9, 3}); + graph.AddEdge({7, 10, 4}); + graph.AddEdge({10, 9, 1}); + graph.AddEdge({8, 6, 2}); + graph.AddEdge({9, 4, 9}); graph.MakeUndirected(); @@ -436,7 +436,7 @@ TEST(DijkstraTest, FloatingPointWeights) { Graph graph; graph.AddVert(1); graph.AddVert(2); - graph.AddEdge(1, 2, 2.5f); + graph.AddEdge({1, 2, 2.5f}); auto distances = Dijkstra(graph, 1); EXPECT_FLOAT_EQ(distances[1], 0.0f); EXPECT_FLOAT_EQ(distances[2], 2.5f);