From 89fcc126e6e979205b7a44b7c2a9c2e460fdf535 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Wed, 25 Mar 2026 19:16:30 +0530 Subject: [PATCH 1/3] fix graph construction --- include/coloring/edgeColoring.hpp | 28 +--------- include/cpp_common/basic_edge.hpp | 1 + src/coloring/edgeColoring.cpp | 91 +++++-------------------------- 3 files changed, 19 insertions(+), 101 deletions(-) diff --git a/include/coloring/edgeColoring.hpp b/include/coloring/edgeColoring.hpp index aed021ce67..67cb2329da 100644 --- a/include/coloring/edgeColoring.hpp +++ b/include/coloring/edgeColoring.hpp @@ -30,35 +30,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_COLORING_EDGECOLORING_HPP_ #pragma once -#include -#include #include #include #include -#include #include +#include "cpp_common/base_graph.hpp" #include "cpp_common/edge_t.hpp" #include "c_types/ii_t_rt.h" -#include "cpp_common/assert.hpp" #include "cpp_common/messages.hpp" namespace pgrouting { namespace functions { class Pgr_edgeColoring : public Pgr_messages { - public: - using EdgeColoring_Graph = - boost::adjacency_list; - - using V = boost::graph_traits::vertex_descriptor; - using E = boost::graph_traits::edge_descriptor; - using V_it = boost::graph_traits::vertex_iterator; - using E_it = boost::graph_traits::edge_iterator; - - public: + public: std::vector edgeColoring(); explicit Pgr_edgeColoring(const std::vector&); @@ -69,16 +56,7 @@ class Pgr_edgeColoring : public Pgr_messages { #endif private: - V get_boost_vertex(int64_t id) const; - int64_t get_vertex_id(V v) const; - int64_t get_edge_id(E e) const; - - - private: - EdgeColoring_Graph graph; - std::map id_to_V; - std::map V_to_id; - std::map E_to_id; + pgrouting:: UndirectedGraph graph; }; } // namespace functions diff --git a/include/cpp_common/basic_edge.hpp b/include/cpp_common/basic_edge.hpp index 25a6eb9f3f..1a30c44ff1 100644 --- a/include/cpp_common/basic_edge.hpp +++ b/include/cpp_common/basic_edge.hpp @@ -45,6 +45,7 @@ class Basic_edge{ int64_t id; double cost; + int64_t color; }; } // namespace pgrouting diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 02ffa43dd8..03bef38539 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -32,10 +32,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "cpp_common/identifiers.hpp" #include #include +#include "cpp_common/base_graph.hpp" #include "cpp_common/assert.hpp" #include "cpp_common/interruption.hpp" @@ -50,92 +50,31 @@ Pgr_edgeColoring::edgeColoring() { CHECK_FOR_INTERRUPTS(); try { - boost::edge_coloring(graph, boost::get(boost::edge_bundle, graph)); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), - std::string(__PGR_PRETTY_FUNCTION__)); - } + using E = pgrouting::UndirectedGraph::E; - for (auto e_i : boost::make_iterator_range(boost::edges(graph))) { - auto edge = get_edge_id(e_i); - int64_t color = graph[e_i]; - results.push_back({{edge}, {(color + 1)}}); - } - return results; -} + std::map color_storage; + auto color_map = boost::make_assoc_property_map(color_storage); -Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { - /* - * Inserting vertices - */ - Identifiers ids; - for (const auto &e : edges) { - ids += e.source; - ids += e.target; - } - - for (const auto id : ids) { - auto v = add_vertex(graph); - id_to_V.insert(std::make_pair(id, v)); - V_to_id.insert(std::make_pair(v, id)); - } - - /* - * Inserting edges - */ - bool added = false; - for (const auto &edge : edges) { - auto v1 = get_boost_vertex(edge.source); - auto v2 = get_boost_vertex(edge.target); - auto e_exists = boost::edge(v1, v2, graph); - // NOLINTNEXTLINE - if (e_exists.second) continue; + boost::edge_coloring(graph.graph, color_map); - if (edge.source == edge.target) continue; + for (auto e_i : boost::make_iterator_range(boost::edges(graph.graph))) { + int64_t edge_id = graph.graph[e_i].id; + int64_t color = color_map[e_i]; - if (edge.cost < 0 && edge.reverse_cost < 0) continue; - - E e; - // NOLINTNEXTLINE - boost::tie(e, added) = boost::add_edge(v1, v2, graph); - - E_to_id.insert(std::make_pair(e, edge.id)); - } -} - -Pgr_edgeColoring::V -Pgr_edgeColoring::get_boost_vertex(int64_t id) const { - try { - return id_to_V.at(id); + results.push_back({{edge_id}, {(color + 1)}}); + } } catch (...) { throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the vertex descriptor"), + std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), std::string(__PGR_PRETTY_FUNCTION__)); } -} -int64_t -Pgr_edgeColoring::get_vertex_id(V v) const { - try { - return V_to_id.at(v); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the vertex id"), - std::string(__PGR_PRETTY_FUNCTION__)); - } + return results; } -int64_t -Pgr_edgeColoring::get_edge_id(E e) const { - try { - return E_to_id.at(e); - } catch (...) { - throw std::make_pair( - std::string("INTERNAL: something went wrong when getting the edge id"), - std::string(__PGR_PRETTY_FUNCTION__)); - } +Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { + graph.insert_edges(edges); } } // namespace functions -} // namespace pgrouting +} // namespace pgrouting \ No newline at end of file From 1843ad727493d28dbca0eec96dde528394d44641 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Wed, 25 Mar 2026 19:36:26 +0530 Subject: [PATCH 2/3] remove unwanted code and improve code quality --- include/coloring/edgeColoring.hpp | 2 +- include/cpp_common/basic_edge.hpp | 1 - src/coloring/edgeColoring.cpp | 5 +++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/coloring/edgeColoring.hpp b/include/coloring/edgeColoring.hpp index 67cb2329da..670a046736 100644 --- a/include/coloring/edgeColoring.hpp +++ b/include/coloring/edgeColoring.hpp @@ -56,7 +56,7 @@ class Pgr_edgeColoring : public Pgr_messages { #endif private: - pgrouting:: UndirectedGraph graph; + pgrouting::UndirectedGraph graph; }; } // namespace functions diff --git a/include/cpp_common/basic_edge.hpp b/include/cpp_common/basic_edge.hpp index 1a30c44ff1..25a6eb9f3f 100644 --- a/include/cpp_common/basic_edge.hpp +++ b/include/cpp_common/basic_edge.hpp @@ -45,7 +45,6 @@ class Basic_edge{ int64_t id; double cost; - int64_t color; }; } // namespace pgrouting diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 03bef38539..5e90540766 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -64,7 +64,8 @@ Pgr_edgeColoring::edgeColoring() { results.push_back({{edge_id}, {(color + 1)}}); } } catch (...) { - throw std::make_pair( + throw std::make_pair + ( std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), std::string(__PGR_PRETTY_FUNCTION__)); } @@ -77,4 +78,4 @@ Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { } } // namespace functions -} // namespace pgrouting \ No newline at end of file +} // namespace pgrouting From c87e32a20c18410cfece1eaaf2d6f050e9d54b60 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Wed, 25 Mar 2026 20:09:58 +0530 Subject: [PATCH 3/3] add missing headers --- include/coloring/edgeColoring.hpp | 1 + src/coloring/edgeColoring.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/coloring/edgeColoring.hpp b/include/coloring/edgeColoring.hpp index 670a046736..db57893929 100644 --- a/include/coloring/edgeColoring.hpp +++ b/include/coloring/edgeColoring.hpp @@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include #include #include diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 5e90540766..aa0c94ff8a 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/edgeColoring.hpp" +#include + #include #include #include