From b8f82f31ed000e00a71b9d362272e8c6ef16110c Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 3 Mar 2026 22:38:16 -0600 Subject: [PATCH 01/15] Matching code on coloring .c and driver.cpp --- src/coloring/bipartite.c | 42 ++++++++++-------- src/coloring/bipartite_driver.cpp | 30 ++++++++----- src/coloring/edgeColoring.c | 43 ++++++++----------- src/coloring/edgeColoring_driver.cpp | 24 +++++------ src/coloring/sequentialVertexColoring.c | 42 +++++++----------- .../sequentialVertexColoring_driver.cpp | 16 +++---- 6 files changed, 95 insertions(+), 102 deletions(-) diff --git a/src/coloring/bipartite.c b/src/coloring/bipartite.c index 1822bd0700..4dbf494023 100644 --- a/src/coloring/bipartite.c +++ b/src/coloring/bipartite.c @@ -29,23 +29,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include - #include "c_common/postgres_connection.h" -#include "c_types/ii_t_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" +#include "c_types/ii_t_rt.h" #include "drivers/coloring/bipartite_driver.h" - PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_bipartite); - static void -process(char* edges_sql, +process( + char* edges_sql, + II_t_rt **result_tuples, size_t *result_count) { pgr_SPI_connect(); @@ -53,7 +52,9 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - PGR_DBG("Starting timer"); + (*result_tuples) = NULL; + (*result_count) = 0; + clock_t start_t = clock(); pgr_do_bipartite( edges_sql, @@ -63,7 +64,6 @@ process(char* edges_sql, &err_msg); time_msg("processing pgr_bipartite()", start_t, clock()); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -75,31 +75,33 @@ process(char* edges_sql, pgr_SPI_finish(); } -PGDLLEXPORT Datum -_pgr_bipartite(PG_FUNCTION_ARGS) { +PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; II_t_rt *result_tuples = NULL; size_t result_count = 0; + if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) - != TYPEFUNC_COMPOSITE) + != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); + } + funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } @@ -122,12 +124,14 @@ _pgr_bipartite(PG_FUNCTION_ARGS) { for (i = 0; i < numb; ++i) { nulls[i] = false; } - values[0] = Int64GetDatum(result_tuples[call_cntr].d1.id); - values[1] = Int64GetDatum(result_tuples[call_cntr].d2.value); - tuple = heap_form_tuple(tuple_desc, values, nulls); - result = HeapTupleGetDatum(tuple); - SRF_RETURN_NEXT(funcctx, result); - } else { - SRF_RETURN_DONE(funcctx); - } + + values[0] = Int64GetDatum(result_tuples[call_cntr].d1.id); + values[1] = Int64GetDatum(result_tuples[call_cntr].d2.value); + + tuple = heap_form_tuple(tuple_desc, values, nulls); + result = HeapTupleGetDatum(tuple); + SRF_RETURN_NEXT(funcctx, result); + } else { + SRF_RETURN_DONE(funcctx); + } } diff --git a/src/coloring/bipartite_driver.cpp b/src/coloring/bipartite_driver.cpp index 07f740ad03..de678e5e32 100644 --- a/src/coloring/bipartite_driver.cpp +++ b/src/coloring/bipartite_driver.cpp @@ -27,19 +27,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ +#include "drivers/coloring/bipartite_driver.h" #include #include #include #include #include -#include "cpp_common/pgdata_getters.hpp" + #include "c_types/ii_t_rt.h" -#include "cpp_common/identifiers.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/alloc.hpp" +#include "cpp_common/assert.hpp" + + +#include "cpp_common/identifiers.hpp" + #include "cpp_common/path.hpp" #include "cpp_common/base_graph.hpp" -#include "drivers/coloring/bipartite_driver.h" + #include "coloring/bipartite_driver.hpp" @@ -49,6 +55,7 @@ pgr_do_bipartite( II_t_rt **return_tuples, size_t *return_count, + char **log_msg, char **notice_msg, char **err_msg) { @@ -61,7 +68,6 @@ pgr_do_bipartite( std::ostringstream err; const char *hint = nullptr; - try { // NOLINTBEGIN(clang-analyzer-cplusplus.NewDelete) pgassert(!(*log_msg)); @@ -80,22 +86,24 @@ pgr_do_bipartite( } hint = nullptr; - std::string logstr; + std::vector results; + pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - std::vector results; + pgrouting::functions::Pgr_Bipartite fn_Bipartite; results = fn_Bipartite.pgr_bipartite(undigraph); - logstr += fn_Bipartite.get_log(); - log << logstr; + auto count = results.size(); + if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - *log_msg = to_pg_msg(log); - *notice_msg = to_pg_msg(notice); + notice << "No results found"; + *log_msg = to_pg_msg(notice); return; } + (*return_tuples) = pgr_alloc(count, (*return_tuples)); for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; @@ -120,7 +128,7 @@ pgr_do_bipartite( err << except.what(); *err_msg = to_pg_msg(err); *log_msg = to_pg_msg(log); - } catch(...) { + } catch (...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; diff --git a/src/coloring/edgeColoring.c b/src/coloring/edgeColoring.c index 51b784790e..f74c5705da 100644 --- a/src/coloring/edgeColoring.c +++ b/src/coloring/edgeColoring.c @@ -2,12 +2,14 @@ File: edgeColoring.c Generated with Template by: +Generated with Template by: Copyright (c) 2015-2026 pgRouting developers Mail: project@pgrouting.org Function's developer: Copyright (c) 2021 Veenit Kumar Mail: 123sveenit@gmail.com + ------ This program is free software; you can redistribute it and/or modify @@ -26,10 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file edgeColoring.c - * @brief Connecting code with postgres. - * - */ #include #include "c_common/postgres_connection.h" @@ -37,15 +35,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - #include "c_types/ii_t_rt.h" - #include "drivers/coloring/edgeColoring_driver.h" PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_edgecoloring); - static void process( @@ -64,9 +59,7 @@ process( clock_t start_t = clock(); pgr_do_edgeColoring( edges_sql, - - result_tuples, - result_count, + result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); @@ -85,8 +78,7 @@ process( PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - TupleDesc tuple_desc; - + TupleDesc tuple_desc; II_t_rt *result_tuples = NULL; size_t result_count = 0; @@ -100,7 +92,7 @@ PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - funcctx->max_calls = result_count; + funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -120,23 +112,22 @@ PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { result_tuples = (II_t_rt*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { - HeapTuple tuple; - Datum result; - Datum *values; - bool* nulls; - - size_t num = 3; - values = palloc(num * sizeof(Datum)); - nulls = palloc(num * sizeof(bool)); - - + HeapTuple tuple; + Datum result; + Datum *values; + bool *nulls; + size_t call_cntr = funcctx->call_cntr; + + size_t numb = 2; + values =(Datum *)palloc(numb * sizeof(Datum)); + nulls = palloc(numb * sizeof(bool)); size_t i; - for (i = 0; i < num; ++i) { + for (i = 0; i < numb; ++i) { nulls[i] = false; } - values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].d1.id); - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].d2.value); + values[0] = Int64GetDatum(result_tuples[call_cntr].d1.id); + values[1] = Int64GetDatum(result_tuples[call_cntr].d2.value); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp index 85bfc54882..1367d0c2c0 100644 --- a/src/coloring/edgeColoring_driver.cpp +++ b/src/coloring/edgeColoring_driver.cpp @@ -31,28 +31,30 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "coloring/edgeColoring.hpp" - +#include "c_types/ii_t_rt.h" #include "cpp_common/pgdata_getters.hpp" #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" +#include "coloring/edgeColoring.hpp" + + void pgr_do_edgeColoring( const char *edges_sql, - II_t_rt **return_tuples, - size_t *return_count, + II_t_rt **return_tuples, + size_t *return_count, - char **log_msg, - char **notice_msg, - char **err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::pgr_alloc; using pgrouting::to_pg_msg; using pgrouting::pgr_free; std::ostringstream log; - std::ostringstream err; std::ostringstream notice; + std::ostringstream err; const char *hint = nullptr; try { @@ -71,11 +73,9 @@ void pgr_do_edgeColoring( } hint = nullptr; - std::vector results; pgrouting::functions::Pgr_edgeColoring fn_edgeColoring(edges); - results = fn_edgeColoring.edgeColoring(); auto count = results.size(); @@ -95,8 +95,8 @@ void pgr_do_edgeColoring( (*return_count) = count; pgassert(*err_msg == NULL); - *log_msg = log.str().empty() ? *log_msg : to_pg_msg(log); - *notice_msg = notice.str().empty() ? *notice_msg : to_pg_msg(notice); + *log_msg = to_pg_msg(log); + *notice_msg = to_pg_msg(notice); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/coloring/sequentialVertexColoring.c b/src/coloring/sequentialVertexColoring.c index ac34a1425b..8a538b2b72 100644 --- a/src/coloring/sequentialVertexColoring.c +++ b/src/coloring/sequentialVertexColoring.c @@ -1,13 +1,14 @@ /*PGR-GNU***************************************************************** File: sequentialVertexColoring.c -Generated with Template by: +Generated with Template by: Copyright (c) 2015-2026 pgRouting developers Mail: project@pgrouting.org Function's developer: Copyright (c) 2020 Ashish Kumar Mail: ashishkr23438@gmail.com + ------ This program is free software; you can redistribute it and/or modify @@ -26,10 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file sequentialVertexColoring.c - * @brief Connecting code with postgres. - * - */ #include #include "c_common/postgres_connection.h" @@ -37,10 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - - #include "c_types/ii_t_rt.h" - #include "drivers/coloring/sequentialVertexColoring_driver.h" PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS); @@ -64,9 +58,7 @@ process( clock_t start_t = clock(); pgr_do_sequentialVertexColoring( edges_sql, - - result_tuples, - result_count, + result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); @@ -85,8 +77,7 @@ process( PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - TupleDesc tuple_desc; - + TupleDesc tuple_desc; II_t_rt *result_tuples = NULL; size_t result_count = 0; @@ -120,23 +111,22 @@ PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { result_tuples = (II_t_rt*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { - HeapTuple tuple; - Datum result; - Datum *values; - bool* nulls; - - size_t num = 2; - values = palloc(num * sizeof(Datum)); - nulls = palloc(num * sizeof(bool)); - - + HeapTuple tuple; + Datum result; + Datum *values; + bool *nulls; + size_t call_cntr = funcctx->call_cntr; + + size_t numb = 2; + values =(Datum *)palloc(numb * sizeof(Datum)); + nulls = palloc(numb * sizeof(bool)); size_t i; - for (i = 0; i < num; ++i) { + for (i = 0; i < numb; ++i) { nulls[i] = false; } - values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].d1.id); - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].d2.value); + values[0] = Int64GetDatum(result_tuples[call_cntr].d1.id); + values[1] = Int64GetDatum(result_tuples[call_cntr].d2.value); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/coloring/sequentialVertexColoring_driver.cpp b/src/coloring/sequentialVertexColoring_driver.cpp index 9753093a82..fab12456f0 100644 --- a/src/coloring/sequentialVertexColoring_driver.cpp +++ b/src/coloring/sequentialVertexColoring_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "c_types/ii_t_rt.h" #include "cpp_common/pgdata_getters.hpp" #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" @@ -61,16 +62,16 @@ pgr_do_sequentialVertexColoring( II_t_rt **return_tuples, size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::pgr_alloc; using pgrouting::to_pg_msg; using pgrouting::pgr_free; std::ostringstream log; - std::ostringstream err; std::ostringstream notice; + std::ostringstream err; const char *hint = nullptr; try { @@ -89,10 +90,9 @@ pgr_do_sequentialVertexColoring( } hint = nullptr; - std::vector results; + std::vector results; pgrouting::UndirectedGraph undigraph; - undigraph.insert_edges(edges); results = pgr_sequentialVertexColoring(undigraph); @@ -102,7 +102,7 @@ pgr_do_sequentialVertexColoring( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << "No traversal found"; + notice << "No results found"; *log_msg = to_pg_msg(notice); return; } @@ -131,7 +131,7 @@ pgr_do_sequentialVertexColoring( err << except.what(); *err_msg = to_pg_msg(err); *log_msg = to_pg_msg(log); - } catch(...) { + } catch (...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; From b5478ea95d698ae30de6bdf80aba164fb6734852 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 4 Mar 2026 16:09:24 -0600 Subject: [PATCH 02/15] cleaning code of edgeColoring --- docqueries/coloring/edgeColoring.result | 2 +- include/coloring/edgeColoring.hpp | 43 +-------- include/cpp_common/base_graph.hpp | 7 +- src/coloring/edgeColoring.cpp | 117 +++++++----------------- src/coloring/edgeColoring_driver.cpp | 7 +- 5 files changed, 44 insertions(+), 132 deletions(-) diff --git a/docqueries/coloring/edgeColoring.result b/docqueries/coloring/edgeColoring.result index 35c0a0a700..e8e5d4451e 100644 --- a/docqueries/coloring/edgeColoring.result +++ b/docqueries/coloring/edgeColoring.result @@ -12,7 +12,7 @@ SELECT edge, color FROM pgr_edgeColoring( ); edge | color ------+------- - 1 | 3 + 1 | 1 2 | 2 3 | 3 4 | 4 diff --git a/include/coloring/edgeColoring.hpp b/include/coloring/edgeColoring.hpp index aed021ce67..f38456403d 100644 --- a/include/coloring/edgeColoring.hpp +++ b/include/coloring/edgeColoring.hpp @@ -30,56 +30,17 @@ 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/edge_t.hpp" #include "c_types/ii_t_rt.h" -#include "cpp_common/assert.hpp" -#include "cpp_common/messages.hpp" +#include "cpp_common/base_graph.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: - std::vector edgeColoring(); - - explicit Pgr_edgeColoring(const std::vector&); - Pgr_edgeColoring() = delete; - -#if BOOST_VERSION >= 106800 - friend std::ostream& operator<<(std::ostream &, const Pgr_edgeColoring&); -#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; - +std::vector edgeColoring(pgrouting::UndirectedGraph); - private: - EdgeColoring_Graph graph; - std::map id_to_V; - std::map V_to_id; - std::map E_to_id; -}; } // namespace functions } // namespace pgrouting diff --git a/include/cpp_common/base_graph.hpp b/include/cpp_common/base_graph.hpp index c19f36b1a7..2369775ffc 100644 --- a/include/cpp_common/base_graph.hpp +++ b/include/cpp_common/base_graph.hpp @@ -196,13 +196,13 @@ boost::adjacency_list < boost::vecS, boost::vecS, Basic_vertex, Basic_edge , true> DirectedGraph; typedef graph::Pgr_base_graph < -boost::adjacency_list < boost::listS, boost::vecS, +boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, XY_vertex, Basic_edge >, XY_vertex, Basic_edge , false> xyUndirectedGraph; typedef graph::Pgr_base_graph < -boost::adjacency_list < boost::listS, boost::vecS, +boost::adjacency_list < boost::vecS, boost::vecS, boost::bidirectionalS, XY_vertex, Basic_edge >, XY_vertex, Basic_edge , true> xyDirectedGraph; @@ -241,6 +241,9 @@ class Pgr_base_graph { using edges_size_type = typename boost::graph_traits::edges_size_type; using degree_size_type = typename boost::graph_traits::degree_size_type; using vertex_index_t = typename boost::property_map::type; +#if 0 + using edge_index_t = typename boost::property_map::type; +#endif /**@}*/ diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 02ffa43dd8..244bb51a6c 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -5,6 +5,10 @@ Generated with Template by: Copyright (c) 2021-2026 pgRouting developers Mail: project@pgrouting.org +Rewrite: +Copyright (c) 2026 Vicky Vergara +Mail: vicky at erosion.dev + Function's developer: Copyright (c) 2021 Veenit Kumar Mail: 123sveenit@gmail.com @@ -29,112 +33,57 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/edgeColoring.hpp" #include -#include #include +#include -#include "cpp_common/identifiers.hpp" +#include #include -#include +#include -#include "cpp_common/assert.hpp" #include "cpp_common/interruption.hpp" namespace pgrouting { namespace functions { -std::vector -Pgr_edgeColoring::edgeColoring() { - std::vector results; - - 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__)); - } - - 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; -} - -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; - - if (edge.source == edge.target) continue; - if (edge.cost < 0 && edge.reverse_cost < 0) continue; +std::vector +edgeColoring(pgrouting::UndirectedB_G g) { + std::vector results(boost::num_edges(g.graph)); + using B_G = pgrouting::UndirectedGraph::B_G; + using E = pgrouting::UndirectedB_G::E; - E e; - // NOLINTNEXTLINE - boost::tie(e, added) = boost::add_edge(v1, v2, graph); + // Create a std::map to store edge colors + std::map edge_colors_map; - E_to_id.insert(std::make_pair(e, edge.id)); - } -} + // Create an associated property map adaptor + boost::associative_property_map> color_map(edge_colors_map); -Pgr_edgeColoring::V -Pgr_edgeColoring::get_boost_vertex(int64_t id) const { + int64_t colors = 0; + // Run the edge coloring algorithm + CHECK_FOR_INTERRUPTS(); try { - return id_to_V.at(id); + colors = boost::edge_coloring(g.graph, color_map); } 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__)); + size_t i = 0; + boost::graph_traits::edge_iterator ei, ei_end; + for (boost::tie(ei, ei_end) = boost::edges(g.graph); ei != ei_end; ++ei, ++i) { + results[i].d1.id = static_cast(g[*ei].id); + + /** + * There is a problem with boost: + * Sometimes it returns a color with outsatnding large value + * When that happens changing color to: colors + 1 + */ + results[i].d2.value = get(color_map, *ei) < colors? get(color_map, *ei) + 1 : colors + 1 ; } -} -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__)); - } + return results; } } // namespace functions diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp index 1367d0c2c0..cfdfcf6926 100644 --- a/src/coloring/edgeColoring_driver.cpp +++ b/src/coloring/edgeColoring_driver.cpp @@ -73,10 +73,9 @@ void pgr_do_edgeColoring( } hint = nullptr; - std::vector results; - - pgrouting::functions::Pgr_edgeColoring fn_edgeColoring(edges); - results = fn_edgeColoring.edgeColoring(); + pgrouting::UndirectedGraph undigraph; + undigraph.insert_edges(edges); + auto results = pgrouting::functions::edgeColoring(undigraph); auto count = results.size(); From 6f09802a4ea60da350a4feb6638c51375ed01d26 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 14:51:39 -0600 Subject: [PATCH 03/15] (edgecoloring) pgtap: adjusting tests --- include/cpp_common/base_graph.hpp | 7 +- pgtap/coloring/edgeColoring/edge_cases.pg | 411 ++++++---------------- 2 files changed, 118 insertions(+), 300 deletions(-) diff --git a/include/cpp_common/base_graph.hpp b/include/cpp_common/base_graph.hpp index 2369775ffc..c19f36b1a7 100644 --- a/include/cpp_common/base_graph.hpp +++ b/include/cpp_common/base_graph.hpp @@ -196,13 +196,13 @@ boost::adjacency_list < boost::vecS, boost::vecS, Basic_vertex, Basic_edge , true> DirectedGraph; typedef graph::Pgr_base_graph < -boost::adjacency_list < boost::vecS, boost::vecS, +boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS, XY_vertex, Basic_edge >, XY_vertex, Basic_edge , false> xyUndirectedGraph; typedef graph::Pgr_base_graph < -boost::adjacency_list < boost::vecS, boost::vecS, +boost::adjacency_list < boost::listS, boost::vecS, boost::bidirectionalS, XY_vertex, Basic_edge >, XY_vertex, Basic_edge , true> xyDirectedGraph; @@ -241,9 +241,6 @@ class Pgr_base_graph { using edges_size_type = typename boost::graph_traits::edges_size_type; using degree_size_type = typename boost::graph_traits::degree_size_type; using vertex_index_t = typename boost::property_map::type; -#if 0 - using edge_index_t = typename boost::property_map::type; -#endif /**@}*/ diff --git a/pgtap/coloring/edgeColoring/edge_cases.pg b/pgtap/coloring/edgeColoring/edge_cases.pg index ef69df2405..8dbee3d413 100644 --- a/pgtap/coloring/edgeColoring/edge_cases.pg +++ b/pgtap/coloring/edgeColoring/edge_cases.pg @@ -6,7 +6,7 @@ BEGIN; UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost); -SELECT CASE WHEN min_version('4.0.0') THEN plan(29) ELSE plan(1) END; +SELECT CASE WHEN min_version('4.0.0') THEN plan(17) ELSE plan(1) END; CREATE OR REPLACE FUNCTION edge_cases() RETURNS SETOF TEXT AS @@ -27,409 +27,230 @@ FROM edges WHERE id > 18; RETURN QUERY -SELECT is_empty('q1', 'Graph with 0 edge and 0 vertex'); - -PREPARE edgeColoring1 AS -SELECT * FROM pgr_edgeColoring('q1'); - -RETURN QUERY -SELECT is_empty('edgeColoring1', 'Graph with 0 edge and 0 vertex -> Empty row is returned'); +SELECT is_empty($$SELECT * FROM pgr_edgeColoring('q1')$$, 'is empty: Graph with 0 edge and 0 vertex'); -- 1 vertex test +-- 6 -- 6 PREPARE q2 AS -SELECT id, source, 6 AS target, cost, reverse_cost -FROM edges -WHERE id = 2; - -PREPARE edgeColoring2 AS -SELECT * FROM pgr_edgeColoring('q2'); +SELECT 2 AS id, 6 AS source, 6 AS target, -1 AS cost, 1 AS reverse_cost; RETURN QUERY -SELECT is_empty('edgeColoring2', 'One vertex graph can not be edgeColored -> Empty row is returned'); +SELECT CASE WHEN min_lib_version('4.1.0') THEN +collect_tap( + isnt_empty($$SELECT * FROM pgr_edgeColoring('q2')$$, 'q2: One vertex self-loop graph has results') +) +ELSE +collect_tap( + is_empty($$SELECT * FROM pgr_edgeColoring('q2')$$, 'q2: One vertex self-loop graph is empty') +) +END; -- 2 vertices test (connected) +-- 3--7 PREPARE q3 AS -SELECT id, source, target, cost, reverse_cost -FROM edges -WHERE id = 7; - -RETURN QUERY -SELECT set_eq('q3', $$VALUES (7, 3, 7, 1, 1)$$, 'Graph with two connected vertices'); - -PREPARE edgeColoring3 AS -SELECT * FROM pgr_edgeColoring('q3'); +SELECT 7 AS id, 3 AS source, 7 AS target, 1 AS cost, 1 AS reverse_cost; RETURN QUERY -SELECT set_eq('edgeColoring3', $$VALUES (7, 1)$$, 'Edge is colored with color 1'); - +SELECT set_eq($$SELECT * FROM pgr_edgeColoring('q3')$$, + $$VALUES (7, 1)$$, 'q3: Edge is colored with color 1'); --- linear tests -- 3 vertices test +-- 6 -- {5---10} PREPARE q4 AS -SELECT id, source, target, cost, reverse_cost -FROM edges -WHERE id <= 2; - -RETURN QUERY -SELECT set_eq('q4', $$VALUES (1, 5, 6, 1, 1), (2, 6, 10, -1, 1)$$, 'Graph with three vertices'); - -PREPARE edgeColoring4 AS -SELECT * FROM pgr_edgeColoring('q4'); +SELECT id, source, target, cost, reverse_cost FROM edges WHERE id <= 2; RETURN QUERY -SELECT set_eq('edgeColoring4', $$VALUES (1, 1), (2, 2)$$, 'Two colors are expected'); +SELECT set_eq($$SELECT * FROM pgr_edgeColoring('q4')$$, $$VALUES (1, 1), (2, 2)$$, 'q4: Two colors are expected'); -- 4 vertices test +-- 6 -- {5---10}; 15--10; PREPARE q5 AS -SELECT id, source, target, cost, reverse_cost -FROM edges -WHERE id <= 3; - -RETURN QUERY -SELECT set_eq('q5', - $$VALUES - (1, 5, 6, 1, 1), - (2, 6, 10, -1, 1), - (3, 10, 15, -1, 1) - $$, - 'Graph with four vertices' -); +SELECT id, source, target, cost, reverse_cost FROM edges WHERE id <= 3; RETURN QUERY -SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q5')), 'At most three colors are expected'); +SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q5')), 'q5: At most three colors are expected'); -- even length cycle test -- 4 vertices length +-- {7,12}--{8,11} ; PREPARE q6 AS SELECT id, source, target, cost, reverse_cost -FROM edges -WHERE id IN (8, 10, 11, 12); +FROM edges WHERE id IN (8, 10, 11, 12); RETURN QUERY -SELECT set_eq('q6', - $$VALUES - (8, 7, 11, 1, 1), - (10, 7, 8, 1, 1), - (11, 11, 12, 1, -1), - (12, 8, 12, 1, -1) - $$, - 'Graph with four vertices (cyclic)' -); +SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q6')), 'q6: At most three colors are expected'); RETURN QUERY -SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q6')), 'At most three colors are expected'); +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q6')), 3, 'q6: Three colors are used'); -- changing the order of the edges will change the number of colors expected --- 4 vertices length - +-- {7,12}--{8,11} ; PREPARE q7 AS SELECT id, source, target, cost, reverse_cost FROM edges WHERE id IN (8, 10, 11, 12) ORDER BY id DESC; RETURN QUERY -SELECT set_eq('q7', - $$VALUES - (8, 7, 11, 1, 1), - (10, 7, 8, 1, 1), - (11, 11, 12, 1, -1), - (12, 8, 12, 1, -1) - $$, - 'Graph with four vertices (cyclic)' -); - -RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q7')), 2, 'Two colors are expected'); - - --- odd length cycle test - --- 3 vertices cyclic - -CREATE TABLE three_vertices_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q7')), 2, 'q7 == q6 DESC: Two colors are used'); -INSERT INTO three_vertices_table (source, target, cost, reverse_cost) VALUES - (3, 6, 20, 15), - (3, 8, 10, -10), - (6, 8, -1, 12); +-- 3--{6,8}; 8--3; 8--6; PREPARE q8 AS SELECT id, source, target, cost, reverse_cost -FROM three_vertices_table; +FROM (VALUES + (1, 3, 6, 20, 15), + (2, 3, 8, 10, -10), + (3, 6, 8, -1, 12)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT set_eq('q8', - $$VALUES - (1, 3, 6, 20, 15), - (2, 3, 8, 10, -10), - (3, 6, 8, -1, 12) - $$, - 'Cyclic Graph with three vertices 3, 6 and 8' -); - -RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q8')), 3, 'Three colors are expected'); - - --- 5 vertices cyclic - -CREATE TABLE five_vertices_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); - -INSERT INTO five_vertices_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (2, 3, 1, 1), - (3, 4, 1, -1), - (4, 5, 1, 1), - (5, 1, 1, -1); +SELECT CASE WHEN min_lib_version('4.1.0') THEN + collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q8')), 4, 'q8: Four colors are expected') + ) +ELSE + collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q8')), 3, 'q8: Three colors are expected') + ) +END; +-- 1--2--3--4--5--6--1 PREPARE q9 AS SELECT id, source, target, cost, reverse_cost -FROM five_vertices_table; - -RETURN QUERY -SELECT set_eq('q9', - $$VALUES - (1, 1, 2, 1, 1), - (2, 2, 3, 1, 1), - (3, 3, 4, 1, -1), - (4, 4, 5, 1, 1), - (5, 5, 1, 1, -1) - $$, - 'Cyclic Graph with five vertices 1, 2, 3, 4 and 5' -); +FROM (VALUES + (1, 1, 2, 1, 1), + (2, 2, 3, 1, 1), + (3, 3, 4, 1, -1), + (4, 4, 5, 1, 1), + (5, 5, 1, 1, -1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q9')), 3, 'Three colors are expected'); +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q9')), 3, 'q9: Three colors are expected'); -- self loop test --- 1 vertex self loop - -CREATE TABLE one_vertex_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); - -INSERT INTO one_vertex_table (source, target, cost, reverse_cost) VALUES - (1, 1, 1, 1); - +-- 1--1 PREPARE q10 AS SELECT id, source, target, cost, reverse_cost -FROM one_vertex_table; - -RETURN QUERY -SELECT set_eq('q10', - $$VALUES - (1, 1, 1, 1, 1) - $$, - 'Self loop Graph with one vertex 1' -); +FROM (VALUES + (1, 1, 1, 1, 1)) +AS t(id, source, target, cost, reverse_cost); PREPARE edgeColoring10 AS SELECT * FROM pgr_edgeColoring('q10'); RETURN QUERY -SELECT is_empty('edgeColoring10', 'One vertex self-loop graph can not be edgeColored -> Empty row is returned'); - - --- 2 vertex self loop - -CREATE TABLE two_vertices_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); +SELECT CASE WHEN min_lib_version('4.1.0') THEN +collect_tap( + isnt_empty($$SELECT * FROM pgr_edgeColoring('q10')$$, 'q10: One vertex self-loop graph has results') +) +ELSE +collect_tap( + is_empty($$SELECT * FROM pgr_edgeColoring('q10')$$, 'q10: One vertex self-loop graph is empty') +) +END; -INSERT INTO two_vertices_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (2, 2, 1, 1); +-- {1,2} --2 PREPARE q11 AS SELECT id, source, target, cost, reverse_cost -FROM two_vertices_table; - -RETURN QUERY -SELECT set_eq('q11', - $$VALUES - (1, 1, 2, 1, 1), - (2, 2, 2, 1, 1) - $$, - 'Self loop Graph with two vertices 1 and 2' -); - -PREPARE edgeColoring11 AS -SELECT * FROM pgr_edgeColoring('q11'); +FROM (VALUES + (1, 1, 2, 1, 1), + (2, 2, 2, 1, 1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT set_eq('edgeColoring11', $$VALUES (1, 1)$$, 'One color is required'); - - --- 7 vertices tree - -CREATE TABLE seven_vertices_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); +SELECT CASE WHEN min_lib_version('4.1.0') THEN +collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q11')), 2, 'q11: Two colors are used') +) +ELSE + collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q11')), 1, 'q11: One color is used') + ) +END; -INSERT INTO seven_vertices_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (1, 4, 1, 1), - (2, 3, 1, 1), - (2, 5, 1, 1), - (4, 6, 1, 1), - (4, 7, 1, 1); +-- 1--{2,4} ; 2--{3,5} ; 4--{6,7} PREPARE q12 AS SELECT id, source, target, cost, reverse_cost -FROM seven_vertices_table; +FROM (VALUES + (1, 1, 2, 1, 1), + (2, 1, 4, 1, 1), + (3, 2, 3, 1, 1), + (4, 2, 5, 1, 1), + (5, 4, 6, 1, 1), + (6, 4, 7, 1, 1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT set_eq('q12', - $$VALUES - (1, 1, 2, 1, 1), - (2, 1, 4, 1, 1), - (3, 2, 3, 1, 1), - (4, 2, 5, 1, 1), - (5, 4, 6, 1, 1), - (6, 4, 7, 1, 1) - $$, - 'A tree Graph with seven vertices 1, 2, 3, 4, 5, 6 and 7' -); - -RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q12')), 3, 'Three colors are expected'); +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q12')), 3, 'q12: Three colors are used'); -- 3 vertices multiple edge -CREATE TABLE multiple_edge_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); - -INSERT INTO multiple_edge_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (2, 3, 1, 1), - (3, 2, 1, 1); - +-- 1--2--3--2 PREPARE q13 AS SELECT id, source, target, cost, reverse_cost -FROM multiple_edge_table; - -RETURN QUERY -SELECT set_eq('q13', - $$VALUES - (1, 1, 2, 1, 1), - (2, 2, 3, 1, 1), - (3, 3, 2, 1, 1) - $$, - 'Multiple edge Graph with three vertices 1, 2 and 3' -); +FROM (VALUES + (1, 1, 2, 1, 1), + (2, 2, 3, 1, 1), + (3, 3, 2, 1, 1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q13')), 2, 'Two colors are expected'); +SELECT CASE WHEN min_lib_version('4.1.0') THEN +collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q13')), 3, 'q13: Three colors are expected') +) +ELSE + collect_tap( + is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q13')), 2, 'q13: Two colors are expected') + ) +END; -- 2 vertices multiple edge -CREATE TABLE two_vertices_multiple_edge_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); - -INSERT INTO two_vertices_multiple_edge_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (2, 1, 1, 1); - +-- 1--2--1 PREPARE q14 AS SELECT id, source, target, cost, reverse_cost -FROM two_vertices_multiple_edge_table; - -RETURN QUERY -SELECT set_eq('q14', - $$VALUES +FROM (VALUES (1, 1, 2, 1, 1), - (2, 2, 1, 1, 1) - $$, - 'Multiple edge Graph with two vertices 1 and 2' -); - -PREPARE edgeColoring14 AS -SELECT * FROM pgr_edgeColoring('q14'); + (2, 2, 1, 1, 1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT set_eq('edgeColoring14', $$VALUES (1, 1)$$, 'One color is required'); +SELECT is((SELECT count(*) FROM pgr_edgeColoring('q14'))::INTEGER, 2, 'q14: two rows'); +RETURN QUERY +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q14')), 2, 'q14: Teo colors are used'); -- 4 vertices disconnected graph -CREATE TABLE disconnected_graph_table ( - id BIGSERIAL, - source BIGINT, - target BIGINT, - cost FLOAT, - reverse_cost FLOAT -); - -INSERT INTO disconnected_graph_table (source, target, cost, reverse_cost) VALUES - (1, 2, 1, 1), - (3, 4, 1, 1); - PREPARE q15 AS SELECT id, source, target, cost, reverse_cost -FROM disconnected_graph_table; - -RETURN QUERY -SELECT set_eq('q15', - $$VALUES +FROM (VALUES (1, 1, 2, 1, 1), - (2, 3, 4, 1, 1) - $$, - 'Disconnected Graph with four vertices 1, 2, 3 and 4' -); + (2, 3, 4, 1, 1)) +AS t(id, source, target, cost, reverse_cost); RETURN QUERY -SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q15')), 1, 'One color is required'); - +SELECT is((SELECT count(DISTINCT color)::INTEGER FROM pgr_edgeColoring('q15')), 1, 'q15: One color is used'); END; $BODY$ From 0a5eb94ad47270969bdbbbea8d904f18649d86d1 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 18:55:39 -0600 Subject: [PATCH 04/15] cleaning --- src/coloring/edgeColoring.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 244bb51a6c..66f5c284b6 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -48,10 +48,10 @@ namespace functions { std::vector -edgeColoring(pgrouting::UndirectedB_G g) { +edgeColoring(pgrouting::UndirectedGraph g) { std::vector results(boost::num_edges(g.graph)); using B_G = pgrouting::UndirectedGraph::B_G; - using E = pgrouting::UndirectedB_G::E; + using E = pgrouting::UndirectedGraph::E; // Create a std::map to store edge colors std::map edge_colors_map; From 6aeded80fb847f3d2cbb629fbaa805aef2ccf954 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 19:03:33 -0600 Subject: [PATCH 05/15] More _driver similarities --- src/coloring/bipartite_driver.cpp | 15 ++------------- src/coloring/edgeColoring_driver.cpp | 1 + src/coloring/sequentialVertexColoring_driver.cpp | 7 +------ 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/coloring/bipartite_driver.cpp b/src/coloring/bipartite_driver.cpp index de678e5e32..4f9a6a28fc 100644 --- a/src/coloring/bipartite_driver.cpp +++ b/src/coloring/bipartite_driver.cpp @@ -29,23 +29,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "drivers/coloring/bipartite_driver.h" -#include #include -#include #include -#include +#include #include "c_types/ii_t_rt.h" #include "cpp_common/pgdata_getters.hpp" #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" - -#include "cpp_common/identifiers.hpp" - -#include "cpp_common/path.hpp" -#include "cpp_common/base_graph.hpp" - #include "coloring/bipartite_driver.hpp" @@ -86,13 +78,10 @@ pgr_do_bipartite( } hint = nullptr; - std::vector results; - pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - pgrouting::functions::Pgr_Bipartite fn_Bipartite; - results = fn_Bipartite.pgr_bipartite(undigraph); + auto results = fn_Bipartite.pgr_bipartite(undigraph); auto count = results.size(); diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp index cfdfcf6926..791b535c99 100644 --- a/src/coloring/edgeColoring_driver.cpp +++ b/src/coloring/edgeColoring_driver.cpp @@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "drivers/coloring/edgeColoring_driver.h" +#include #include #include diff --git a/src/coloring/sequentialVertexColoring_driver.cpp b/src/coloring/sequentialVertexColoring_driver.cpp index fab12456f0..007cfc16ac 100644 --- a/src/coloring/sequentialVertexColoring_driver.cpp +++ b/src/coloring/sequentialVertexColoring_driver.cpp @@ -30,7 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include #include #include "c_types/ii_t_rt.h" @@ -40,7 +39,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/sequentialVertexColoring.hpp" -/** @file sequentialVertexColoring_driver.cpp */ namespace { @@ -90,12 +88,9 @@ pgr_do_sequentialVertexColoring( } hint = nullptr; - std::vector results; - pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - - results = pgr_sequentialVertexColoring(undigraph); + auto results = pgr_sequentialVertexColoring(undigraph); auto count = results.size(); From a249428db42f6d43c9d889743a7f4e2325327f3e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 20:04:28 -0600 Subject: [PATCH 06/15] (pgr_bipartite) cleanup code --- include/coloring/bipartite.hpp | 48 +++++++++++++ include/coloring/bipartite_driver.hpp | 97 --------------------------- src/coloring/CMakeLists.txt | 3 + src/coloring/bipartite.cpp | 84 +++++++++++++++++++++++ src/coloring/bipartite_driver.cpp | 7 +- 5 files changed, 139 insertions(+), 100 deletions(-) create mode 100644 include/coloring/bipartite.hpp delete mode 100644 include/coloring/bipartite_driver.hpp create mode 100644 src/coloring/bipartite.cpp diff --git a/include/coloring/bipartite.hpp b/include/coloring/bipartite.hpp new file mode 100644 index 0000000000..fec97ad332 --- /dev/null +++ b/include/coloring/bipartite.hpp @@ -0,0 +1,48 @@ +/*PGR-GNU***************************************************************** +File: bipartite.hpp + +Generated with Template by: +Copyright (c) 2021-2026 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Prakash Tiwari +Mail: 85prakash2017@gmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + + +#ifndef INCLUDE_COLORING_BIPARTITE_HPP_ +#define INCLUDE_COLORING_BIPARTITE_HPP_ +#pragma once + +#include + +#include "cpp_common/base_graph.hpp" +#include "c_types/ii_t_rt.h" + +namespace pgrouting { +namespace functions { + +std::vector pgr_bipartite(pgrouting::UndirectedGraph&); + +} // namespace functions +} // namespace pgrouting + +#endif // INCLUDE_COLORING_BIPARTITE_HPP_ diff --git a/include/coloring/bipartite_driver.hpp b/include/coloring/bipartite_driver.hpp deleted file mode 100644 index 35349c6256..0000000000 --- a/include/coloring/bipartite_driver.hpp +++ /dev/null @@ -1,97 +0,0 @@ -/*PGR-GNU***************************************************************** -File: bipartite_driver.hpp - -Generated with Template by: -Copyright (c) 2013-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2020 Prakash Tiwari -Mail: 85prakash2017@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - - -#ifndef INCLUDE_COLORING_BIPARTITE_DRIVER_HPP_ -#define INCLUDE_COLORING_BIPARTITE_DRIVER_HPP_ -#pragma once - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "cpp_common/base_graph.hpp" -#include "cpp_common/messages.hpp" -#include "cpp_common/interruption.hpp" -typedef struct II_t_rt II_t_rt; -#include "c_types/ii_t_rt.h" - -namespace pgrouting { -namespace functions { - -template -class Pgr_Bipartite : public pgrouting::Pgr_messages { - public: - typedef typename G::V_i V_i; - std::vector print_Bipartite( - G &graph) { - std::vector results; - std::vector partition(graph.num_vertices()); - auto partition_map = - make_iterator_property_map(partition.begin(), boost::get(boost::vertex_index, graph.graph)); - - /* abort in case of an interruption occurs (e.g. the query is being cancelled) */ - CHECK_FOR_INTERRUPTS(); - try { - boost::is_bipartite(graph.graph, boost::get(boost::vertex_index, graph.graph), partition_map); - } catch (boost::exception const& ex) { - (void)ex; - throw; - } catch (std::exception &e) { - (void)e; - throw; - } catch (...) { - throw; - } - V_i v, vend; - for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) { - int64_t vid = graph[*v].id; - boost::get(partition_map, *v) == - boost::color_traits ::white() ? - results.push_back({{vid}, {0}}) : results.push_back({{vid}, {1}}); - } - return results; - } - std::vector pgr_bipartite( - G &graph ){ - std::vector results; - bool bipartite = boost::is_bipartite(graph.graph); - if (bipartite) results = print_Bipartite(graph); - return results; - } -}; -} // namespace functions -} // namespace pgrouting -#endif // INCLUDE_COLORING_BIPARTITE_DRIVER_HPP_ diff --git a/src/coloring/CMakeLists.txt b/src/coloring/CMakeLists.txt index e55009b8a9..f5abe67c07 100644 --- a/src/coloring/CMakeLists.txt +++ b/src/coloring/CMakeLists.txt @@ -6,7 +6,10 @@ ADD_LIBRARY(coloring OBJECT sequentialVertexColoring_driver.cpp bipartite.c bipartite_driver.cpp + edgeColoring.c edgeColoring_driver.cpp + + bipartite.cpp edgeColoring.cpp ) diff --git a/src/coloring/bipartite.cpp b/src/coloring/bipartite.cpp new file mode 100644 index 0000000000..2e3cd59846 --- /dev/null +++ b/src/coloring/bipartite.cpp @@ -0,0 +1,84 @@ +/*PGR-GNU***************************************************************** +File: bipartite.cpp + +Generated with Template by: +Copyright (c) 2026-2026 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Prakash Tiwari +Mail: 85prakash2017@gmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "coloring/bipartite.hpp" + +#include +#include + +#include +#include +#include +#include + +#include "cpp_common/interruption.hpp" + +namespace { + +std::vector get_bipartition(pgrouting::UndirectedGraph &graph) { + using V_i = pgrouting::UndirectedGraph::V_i; + + std::vector results; + std::vector partition(graph.num_vertices()); + auto partition_map = + make_iterator_property_map(partition.begin(), boost::get(boost::vertex_index, graph.graph)); + + CHECK_FOR_INTERRUPTS(); + try { + boost::is_bipartite(graph.graph, boost::get(boost::vertex_index, graph.graph), partition_map); + } catch (boost::exception const& ex) { + throw; + } catch (std::exception &e) { + throw; + } catch (...) { + throw; + } + + V_i v, vend; + for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) { + int64_t vid = graph[*v].id; + boost::get(partition_map, *v) == + boost::color_traits ::white() ? + results.push_back({{vid}, {0}}) : results.push_back({{vid}, {1}}); + } + return results; +} + +} // namespace + +namespace pgrouting { +namespace functions { + +std::vector pgr_bipartite(pgrouting::UndirectedGraph &graph ){ + bool bipartite = boost::is_bipartite(graph.graph); + return (bipartite)? get_bipartition(graph) : std::vector(); +} + +} // namespace functions +} // namespace pgrouting diff --git a/src/coloring/bipartite_driver.cpp b/src/coloring/bipartite_driver.cpp index 4f9a6a28fc..3cc3a9591d 100644 --- a/src/coloring/bipartite_driver.cpp +++ b/src/coloring/bipartite_driver.cpp @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" -#include "coloring/bipartite_driver.hpp" +#include "coloring/bipartite.hpp" void @@ -69,6 +69,8 @@ pgr_do_bipartite( pgassert(*return_count == 0); // NOLINTEND(clang-analyzer-cplusplus.NewDelete) + using pgrouting::functions::pgr_bipartite; + hint = edges_sql; auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); if (edges.empty()) { @@ -80,8 +82,7 @@ pgr_do_bipartite( pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - pgrouting::functions::Pgr_Bipartite fn_Bipartite; - auto results = fn_Bipartite.pgr_bipartite(undigraph); + auto results = pgr_bipartite(undigraph); auto count = results.size(); From 6344e6effe63ab998d4803d41156488fd1871136 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 20:07:13 -0600 Subject: [PATCH 07/15] fixup --- include/coloring/bipartite.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/coloring/bipartite.hpp b/include/coloring/bipartite.hpp index fec97ad332..3ff49c3fc6 100644 --- a/include/coloring/bipartite.hpp +++ b/include/coloring/bipartite.hpp @@ -34,8 +34,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include -#include "cpp_common/base_graph.hpp" #include "c_types/ii_t_rt.h" +#include "cpp_common/base_graph.hpp" namespace pgrouting { namespace functions { From b2d0eca837cc21382773daac34076612915bb405 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 20:39:45 -0600 Subject: [PATCH 08/15] (sequentialVertexColoring.cpp) cleaning up code --- include/coloring/sequentialVertexColoring.hpp | 102 +-------------- src/coloring/CMakeLists.txt | 1 + src/coloring/sequentialVertexColoring.cpp | 118 ++++++++++++++++++ .../sequentialVertexColoring_driver.cpp | 19 +-- 4 files changed, 124 insertions(+), 116 deletions(-) create mode 100644 src/coloring/sequentialVertexColoring.cpp diff --git a/include/coloring/sequentialVertexColoring.hpp b/include/coloring/sequentialVertexColoring.hpp index eb413fa75e..0ff8fe3b43 100644 --- a/include/coloring/sequentialVertexColoring.hpp +++ b/include/coloring/sequentialVertexColoring.hpp @@ -28,117 +28,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_COLORING_SEQUENTIALVERTEXCOLORING_HPP_ #pragma once -#include #include -#include #include -#include -#include -#include -#include -#include -#include - #include "cpp_common/base_graph.hpp" -#include "cpp_common/interruption.hpp" - #include "c_types/ii_t_rt.h" namespace pgrouting { namespace functions { -//************************************************************* - -template < class G > -class Pgr_sequentialVertexColoring { - public: - typedef typename G::V V; - typedef typename G::E E; - typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS > Graph; - typedef boost::graph_traits < Graph > ::vertices_size_type vertices_size_type; - - /** @name SequentialVertexColoring - * @{ - * - */ - - /** @brief sequentialVertexColoring function - * - * It does all the processing and returns the results. - * - * @param graph the graph containing the edges - * - * @returns results, when results are found - * - * @see [boost::sequential_vertex_coloring] - * (https://www.boost.org/libs/graph/doc/sequential_vertex_coloring.html) - */ - std::vector sequentialVertexColoring(G &graph) { - std::vector results; - - auto i_map = boost::get(boost::vertex_index, graph.graph); - - // vector which will store the color of all the vertices in the graph - std::vector < vertices_size_type > colors(boost::num_vertices(graph.graph)); - - // An iterator property map which records the color of each vertex - auto color_map = boost::make_iterator_property_map(colors.begin(), i_map); - - /* abort in case of an interruption occurs (e.g. the query is being cancelled) */ - CHECK_FOR_INTERRUPTS(); - - try { - boost::sequential_vertex_coloring(graph.graph, color_map); - } catch (boost::exception const& ex) { - (void)ex; - throw; - } catch (std::exception &e) { - (void)e; - throw; - } catch (...) { - throw; - } - - results = get_results(colors, graph); - - return results; - } - - //@} - - private: - /** @brief to get the results - * - * Uses the `colors` vector to get the results i.e. the color of every vertex. - * - * @param colors vector which contains the color of every vertex - * @param graph the graph containing the edges - * - * @returns `results` vector - */ - std::vector get_results( - std::vector < vertices_size_type > &colors, - const G &graph) { - std::vector results; - - typename boost::graph_traits < Graph > ::vertex_iterator v, vend; - - for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) { - int64_t node = graph[*v].id; - auto color = colors[*v]; - results.push_back({{node}, {static_cast(color + 1)}}); - } - - // ordering the results in an increasing order of the node id - std::sort(results.begin(), results.end(), - [](const II_t_rt row1, const II_t_rt row2) { - return row1.d1.id < row2.d1.id; - }); + std::vector sequentialVertexColoring(const pgrouting::UndirectedGraph&); - return results; - } -}; } // namespace functions } // namespace pgrouting diff --git a/src/coloring/CMakeLists.txt b/src/coloring/CMakeLists.txt index f5abe67c07..5feedace13 100644 --- a/src/coloring/CMakeLists.txt +++ b/src/coloring/CMakeLists.txt @@ -12,4 +12,5 @@ ADD_LIBRARY(coloring OBJECT bipartite.cpp edgeColoring.cpp + sequentialVertexColoring.cpp ) diff --git a/src/coloring/sequentialVertexColoring.cpp b/src/coloring/sequentialVertexColoring.cpp new file mode 100644 index 0000000000..5e8f0c4ae8 --- /dev/null +++ b/src/coloring/sequentialVertexColoring.cpp @@ -0,0 +1,118 @@ +/*PGR-GNU***************************************************************** +File: sequentialVertexColoring.hpp + +Copyright (c) 2013-2026 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2020 Ashish Kumar +Mail: ashishkr23438@gmail.com + +------ +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "coloring/sequentialVertexColoring.hpp" + +#include +#include + +#include +#include "cpp_common/interruption.hpp" + +namespace { + +using vertices_size_type = pgrouting::UndirectedGraph::vertices_size_type; + +/** @brief to get the results + * + * Uses the `colors` vector to get the results i.e. the color of every vertex. + * + * @param colors vector which contains the color of every vertex + * @param graph the graph containing the edges + * + * @returns `results` vector + */ +std::vector get_results( + const std::vector &colors, + const pgrouting::UndirectedGraph &graph) { + std::vector results; + + pgrouting::UndirectedGraph::V_i v, vend; + + for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) { + int64_t node = graph[*v].id; + auto color = colors[*v]; + results.push_back({{node}, {static_cast(color + 1)}}); + } + + // ordering the results in an increasing order of the node id + std::sort(results.begin(), results.end(), + [](const II_t_rt row1, const II_t_rt row2) { + return row1.d1.id < row2.d1.id; + }); + + return results; +} + +} // namespace + +namespace pgrouting { +namespace functions { + +/** @brief sequentialVertexColoring function + * + * It does all the processing and returns the results. + * + * @param graph the graph containing the edges + * + * @returns results, when results are found + * + * @see [boost::sequential_vertex_coloring] + * (https://www.boost.org/libs/graph/doc/sequential_vertex_coloring.html) + */ + std::vector sequentialVertexColoring(const pgrouting::UndirectedGraph &graph) { + using vertices_size_type = pgrouting::UndirectedGraph::vertices_size_type; + + std::vector results; + + auto i_map = boost::get(boost::vertex_index, graph.graph); + + // vector which will store the color of all the vertices in the graph + std::vector colors(boost::num_vertices(graph.graph)); + + // An iterator property map which records the color of each vertex + auto color_map = boost::make_iterator_property_map(colors.begin(), i_map); + + CHECK_FOR_INTERRUPTS(); + + try { + boost::sequential_vertex_coloring(graph.graph, color_map); + } catch (boost::exception const& ex) { + (void)ex; + throw; + } catch (std::exception &e) { + (void)e; + throw; + } catch (...) { + throw; + } + + return get_results(colors, graph); + } + +} // namespace functions +} // namespace pgrouting + diff --git a/src/coloring/sequentialVertexColoring_driver.cpp b/src/coloring/sequentialVertexColoring_driver.cpp index 007cfc16ac..db5f48cfa1 100644 --- a/src/coloring/sequentialVertexColoring_driver.cpp +++ b/src/coloring/sequentialVertexColoring_driver.cpp @@ -39,20 +39,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/sequentialVertexColoring.hpp" - - -namespace { - -template < class G > -std::vector -pgr_sequentialVertexColoring(G &graph) { - pgrouting::functions::Pgr_sequentialVertexColoring < G > fn_sequentialVertexColoring; - auto results = fn_sequentialVertexColoring.sequentialVertexColoring(graph); - return results; -} - -} // namespace - void pgr_do_sequentialVertexColoring( const char *edges_sql, @@ -79,6 +65,9 @@ pgr_do_sequentialVertexColoring( pgassert(!(*return_tuples)); pgassert(*return_count == 0); + using pgrouting::functions::sequentialVertexColoring; + + hint = edges_sql; auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); if (edges.empty()) { @@ -90,7 +79,7 @@ pgr_do_sequentialVertexColoring( pgrouting::UndirectedGraph undigraph; undigraph.insert_edges(edges); - auto results = pgr_sequentialVertexColoring(undigraph); + auto results = sequentialVertexColoring(undigraph); auto count = results.size(); From a7ecd7d4f676614c45f1bbce100ec492778e36ed Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 20:48:34 -0600 Subject: [PATCH 09/15] edgeColoring.cpp --- src/coloring/edgeColoring.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/coloring/edgeColoring.cpp b/src/coloring/edgeColoring.cpp index 66f5c284b6..1fc28b2fe0 100644 --- a/src/coloring/edgeColoring.cpp +++ b/src/coloring/edgeColoring.cpp @@ -35,10 +35,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include -#include #include -#include #include "cpp_common/interruption.hpp" @@ -46,7 +45,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace pgrouting { namespace functions { - std::vector edgeColoring(pgrouting::UndirectedGraph g) { std::vector results(boost::num_edges(g.graph)); @@ -64,10 +62,14 @@ edgeColoring(pgrouting::UndirectedGraph g) { CHECK_FOR_INTERRUPTS(); try { colors = boost::edge_coloring(g.graph, color_map); + } catch (boost::exception const& ex) { + throw; + } catch (std::exception &e) { + throw; } catch (...) { throw std::make_pair( - std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), - std::string(__PGR_PRETTY_FUNCTION__)); + std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), + std::string(__PGR_PRETTY_FUNCTION__)); } size_t i = 0; From 177d9664e4cb7a9c8e943c973404841add764475 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 20:48:57 -0600 Subject: [PATCH 10/15] doing some formatting and include cleanup --- src/coloring/bipartite.cpp | 8 ++-- src/coloring/sequentialVertexColoring.cpp | 46 ++++++++++++----------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/coloring/bipartite.cpp b/src/coloring/bipartite.cpp index 2e3cd59846..51b8f9204d 100644 --- a/src/coloring/bipartite.cpp +++ b/src/coloring/bipartite.cpp @@ -30,11 +30,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/bipartite.hpp" #include +#include #include -#include -#include -#include #include #include "cpp_common/interruption.hpp" @@ -57,7 +55,9 @@ std::vector get_bipartition(pgrouting::UndirectedGraph &graph) { } catch (std::exception &e) { throw; } catch (...) { - throw; + throw std::make_pair( + std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), + std::string(__PGR_PRETTY_FUNCTION__));; } V_i v, vend; diff --git a/src/coloring/sequentialVertexColoring.cpp b/src/coloring/sequentialVertexColoring.cpp index 5e8f0c4ae8..06e9c62e1f 100644 --- a/src/coloring/sequentialVertexColoring.cpp +++ b/src/coloring/sequentialVertexColoring.cpp @@ -27,9 +27,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/sequentialVertexColoring.hpp" #include +#include #include #include + #include "cpp_common/interruption.hpp" namespace { @@ -83,36 +85,36 @@ namespace functions { * @see [boost::sequential_vertex_coloring] * (https://www.boost.org/libs/graph/doc/sequential_vertex_coloring.html) */ - std::vector sequentialVertexColoring(const pgrouting::UndirectedGraph &graph) { - using vertices_size_type = pgrouting::UndirectedGraph::vertices_size_type; - - std::vector results; +std::vector sequentialVertexColoring(const pgrouting::UndirectedGraph &graph) { + using vertices_size_type = pgrouting::UndirectedGraph::vertices_size_type; - auto i_map = boost::get(boost::vertex_index, graph.graph); + std::vector results; - // vector which will store the color of all the vertices in the graph - std::vector colors(boost::num_vertices(graph.graph)); + auto i_map = boost::get(boost::vertex_index, graph.graph); - // An iterator property map which records the color of each vertex - auto color_map = boost::make_iterator_property_map(colors.begin(), i_map); + // vector which will store the color of all the vertices in the graph + std::vector colors(boost::num_vertices(graph.graph)); - CHECK_FOR_INTERRUPTS(); + // An iterator property map which records the color of each vertex + auto color_map = boost::make_iterator_property_map(colors.begin(), i_map); - try { - boost::sequential_vertex_coloring(graph.graph, color_map); - } catch (boost::exception const& ex) { - (void)ex; - throw; - } catch (std::exception &e) { - (void)e; - throw; - } catch (...) { - throw; - } + CHECK_FOR_INTERRUPTS(); - return get_results(colors, graph); + try { + boost::sequential_vertex_coloring(graph.graph, color_map); + } catch (boost::exception const& ex) { + throw; + } catch (std::exception &e) { + throw; + } catch (...) { + throw std::make_pair( + std::string("INTERNAL: something went wrong while calling boost::edge_coloring"), + std::string(__PGR_PRETTY_FUNCTION__));; } + return get_results(colors, graph); +} + } // namespace functions } // namespace pgrouting From e58beb6c6983efeffc15027ac5da17d5c623ed88 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 21:29:07 -0600 Subject: [PATCH 11/15] edge coloring using the new coloring process and driver --- include/c_common/enums.h | 5 +- include/drivers/coloring_driver.hpp | 60 +++++++++++++ include/process/coloring_process.h | 56 ++++++++++++ src/coloring/CMakeLists.txt | 6 +- src/coloring/coloring_driver.cpp | 127 +++++++++++++++++++++++++++ src/coloring/coloring_process.cpp | 91 +++++++++++++++++++ src/coloring/edgeColoring.c | 8 +- src/coloring/edgeColoring_driver.cpp | 11 ++- 8 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 include/drivers/coloring_driver.hpp create mode 100644 include/process/coloring_process.h create mode 100644 src/coloring/coloring_driver.cpp create mode 100644 src/coloring/coloring_process.cpp diff --git a/include/c_common/enums.h b/include/c_common/enums.h index 2d8598fcdc..60892fc9e6 100644 --- a/include/c_common/enums.h +++ b/include/c_common/enums.h @@ -47,7 +47,10 @@ enum Which { DFS = 520, BFS = 530, DIJKSTRADD = 540, - MAXFLOW, PUSHRELABEL, BOYKOV, EDMONDSKARP + /* For flow */ + MAXFLOW, PUSHRELABEL, BOYKOV, EDMONDSKARP, + /* For coloring */ + EDGECOLORING }; #endif // INCLUDE_C_COMMON_ENUMS_H_ diff --git a/include/drivers/coloring_driver.hpp b/include/drivers/coloring_driver.hpp new file mode 100644 index 0000000000..66358c360b --- /dev/null +++ b/include/drivers/coloring_driver.hpp @@ -0,0 +1,60 @@ +/*PGR-GNU***************************************************************** +File: allpairs_driver.hpp + +Copyright (c) 2015-2026 pgRouting developers +Mail: project@pgrouting.org + +Design of one process & driver file by +Copyright (c) 2025 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +Copying this file (or a derivative) within pgRouting code add the following: + +Generated with Template by: +Copyright (c) 2015-2026 pgRouting developers +Mail: project@pgrouting.org + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_DRIVERS_COLORING_DRIVER_HPP_ +#define INCLUDE_DRIVERS_COLORING_DRIVER_HPP_ +#pragma once + +#include +#include +#include +#include + +#include "c_common/enums.h" + +using II_t_rt = struct II_t_rt; + +namespace pgrouting { +namespace drivers { + +void do_coloring( + const std::string&, + Which, + II_t_rt*&, size_t&, + std::ostringstream&, std::ostringstream&, std::ostringstream&); + +} // namespace drivers +} // namespace pgrouting + +#endif // INCLUDE_DRIVERS_COLORING_DRIVER_HPP_ diff --git a/include/process/coloring_process.h b/include/process/coloring_process.h new file mode 100644 index 0000000000..3059c2ef72 --- /dev/null +++ b/include/process/coloring_process.h @@ -0,0 +1,56 @@ +/*PGR-GNU***************************************************************** +File: allpairs_process.h + +Function's developer: +Copyright (c) 2025 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_PROCESS_COLORING_PROCESS_H_ +#define INCLUDE_PROCESS_COLORING_PROCESS_H_ +#pragma once + +#ifdef __cplusplus +#include +#include +using II_t_rt = struct II_t_rt; +#else +#include +#include +#include +typedef struct II_t_rt II_t_rt; +#endif + +#include "c_common/enums.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void pgr_process_coloring( + const char*, + enum Which, + II_t_rt**, size_t*); + +#ifdef __cplusplus +} +#endif + +#endif // INCLUDE_PROCESS_COLORING_PROCESS_H_ diff --git a/src/coloring/CMakeLists.txt b/src/coloring/CMakeLists.txt index 5feedace13..82ae1d65c1 100644 --- a/src/coloring/CMakeLists.txt +++ b/src/coloring/CMakeLists.txt @@ -8,9 +8,11 @@ ADD_LIBRARY(coloring OBJECT bipartite_driver.cpp edgeColoring.c - edgeColoring_driver.cpp bipartite.cpp edgeColoring.cpp sequentialVertexColoring.cpp - ) + + coloring_process.cpp + coloring_driver.cpp +) diff --git a/src/coloring/coloring_driver.cpp b/src/coloring/coloring_driver.cpp new file mode 100644 index 0000000000..bef428249c --- /dev/null +++ b/src/coloring/coloring_driver.cpp @@ -0,0 +1,127 @@ +/*PGR-GNU***************************************************************** +File: coloring_driver.cpp + +Generated with Template by: +Copyright (c) 2015-2026 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2025 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "drivers/coloring_driver.hpp" + +#include +#include +#include +#include +#include +#include + +#include "c_types/ii_t_rt.h" +#include "cpp_common/pgdata_getters.hpp" +#include "cpp_common/utilities.hpp" +#include "cpp_common/to_postgres.hpp" +#include "cpp_common/alloc.hpp" +#include "cpp_common/assert.hpp" + +#include "coloring/edgeColoring.hpp" + +namespace pgrouting { +namespace drivers { + +void do_coloring( + const std::string &edges_sql, + + Which which, + + II_t_rt* &return_tuples, + size_t &return_count, + std::ostringstream &log, + std::ostringstream ¬ice, + std::ostringstream &err) { + std::string hint = ""; + + try { + if (edges_sql.empty()) { + err << "Empty edges SQL"; + return; + } + + using pgrouting::pgget::get_edges; + using pgrouting::UndirectedGraph; + + using pgrouting::functions::edgeColoring; + + hint = edges_sql; + auto edges = get_edges(edges_sql, true, false); + + if (edges.empty()) { + notice << "No edges found"; + log << edges_sql; + return; + } + + hint = ""; + + UndirectedGraph undigraph; + undigraph.insert_edges(edges); + + std::vector results; + + switch (which) { + case EDGECOLORING: + results = edgeColoring(undigraph); + break; + default: + err << "Unknown coloring function" << get_name(which); + return; + } + + auto count = results.size(); + + if (count == 0) { + notice << "No results found"; + return; + } + + return_tuples = pgr_alloc(count, return_tuples); + for (size_t i = 0; i < count; i++) { + return_tuples[i] = results[i]; + } + return_count = count; + + } catch (AssertFailedException &except) { + err << except.what(); + } catch (const std::pair& ex) { + err << ex.first; + log << ex.second; + } catch (const std::string &ex) { + err << ex; + log << hint; + } catch (std::exception &except) { + err << except.what(); + } catch (...) { + err << "Caught unknown exception!"; + } +} + +} // namespace drivers +} // namespace pgrouting diff --git a/src/coloring/coloring_process.cpp b/src/coloring/coloring_process.cpp new file mode 100644 index 0000000000..133ea80b8f --- /dev/null +++ b/src/coloring/coloring_process.cpp @@ -0,0 +1,91 @@ +/*PGR-GNU***************************************************************** +File: allpairs_process.cpp + +Copyright (c) 2025-2026 pgRouting developers +Mail: project@pgrouting.org + +Design of one process & driver file by +Copyright (c) 2025 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +Copying this file (or a derivative) within pgRouting code add the following: + +Generated with Template by: +Copyright (c) 2025-2026 pgRouting developers +Mail: project@pgrouting.org + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "process/coloring_process.h" + +extern "C" { +#include "c_common/postgres_connection.h" +#include "c_common/e_report.h" +#include "c_common/time_msg.h" +} + +#include +#include + +#include "c_types/ii_t_rt.h" + +#include "cpp_common/report_messages.hpp" +#include "cpp_common/utilities.hpp" +#include "cpp_common/assert.hpp" +#include "cpp_common/alloc.hpp" + +#include "drivers/coloring_driver.hpp" + +void pgr_process_coloring( + const char* edges_sql, + + enum Which which, + II_t_rt **result_tuples, + size_t *result_count) { + using pgrouting::to_pg_msg; + using pgrouting::pgr_free; + pgassert(edges_sql); + pgassert(!(*result_tuples)); + pgassert(*result_count == 0); + pgr_SPI_connect(); + + std::ostringstream log; + std::ostringstream err; + std::ostringstream notice; + + clock_t start_t = clock(); + pgrouting::drivers::do_coloring( + edges_sql? edges_sql : "", + + which, + (*result_tuples), (*result_count), + log, notice, err); + + auto name = std::string(" processing ") + pgrouting::get_name(which); + time_msg(name.c_str(), start_t, clock()); + + if (!err.str().empty() && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = nullptr; + (*result_count) = 0; + } + + pgrouting::report_messages(log, notice, err); + pgr_SPI_finish(); +} diff --git a/src/coloring/edgeColoring.c b/src/coloring/edgeColoring.c index f74c5705da..eead4affc9 100644 --- a/src/coloring/edgeColoring.c +++ b/src/coloring/edgeColoring.c @@ -36,11 +36,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/ii_t_rt.h" -#include "drivers/coloring/edgeColoring_driver.h" +#include "process/coloring_process.h" PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_edgecoloring); +#if 0 static void process( @@ -75,6 +76,7 @@ process( pgr_SPI_finish(); } +#endif PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -87,8 +89,10 @@ PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - process( + pgr_process_coloring( text_to_cstring(PG_GETARG_TEXT_P(0)), + + EDGECOLORING, &result_tuples, &result_count); diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp index 791b535c99..5f8193bc0b 100644 --- a/src/coloring/edgeColoring_driver.cpp +++ b/src/coloring/edgeColoring_driver.cpp @@ -65,8 +65,13 @@ void pgr_do_edgeColoring( pgassert(!(*return_tuples)); pgassert(*return_count == 0); + using pgrouting::pgget::get_edges; + using pgrouting::UndirectedGraph; + + using pgrouting::functions::edgeColoring; + hint = edges_sql; - auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + auto edges = get_edges(std::string(edges_sql), true, false); if (edges.empty()) { *notice_msg = to_pg_msg("No edges found"); *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); @@ -74,9 +79,9 @@ void pgr_do_edgeColoring( } hint = nullptr; - pgrouting::UndirectedGraph undigraph; + UndirectedGraph undigraph; undigraph.insert_edges(edges); - auto results = pgrouting::functions::edgeColoring(undigraph); + auto results = edgeColoring(undigraph); auto count = results.size(); From e86281937e6aeb5d8b72768dac1be82887a7bff5 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 21:40:28 -0600 Subject: [PATCH 12/15] The three coloring functions integrated in coloring process and driver --- include/c_common/enums.h | 2 +- src/coloring/CMakeLists.txt | 3 --- src/coloring/bipartite.c | 8 ++++++-- src/coloring/coloring_driver.cpp | 12 +++++++++++- src/coloring/sequentialVertexColoring.c | 8 ++++++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/c_common/enums.h b/include/c_common/enums.h index 60892fc9e6..d217598a6b 100644 --- a/include/c_common/enums.h +++ b/include/c_common/enums.h @@ -50,7 +50,7 @@ enum Which { /* For flow */ MAXFLOW, PUSHRELABEL, BOYKOV, EDMONDSKARP, /* For coloring */ - EDGECOLORING + EDGECOLORING, BIPARTITE, SEQUENTIAL }; #endif // INCLUDE_C_COMMON_ENUMS_H_ diff --git a/src/coloring/CMakeLists.txt b/src/coloring/CMakeLists.txt index 82ae1d65c1..a8a7b37c35 100644 --- a/src/coloring/CMakeLists.txt +++ b/src/coloring/CMakeLists.txt @@ -3,10 +3,7 @@ # License: GPL-2 See https://github.com/pgRouting/pgrouting/blob/main/LICENSE ADD_LIBRARY(coloring OBJECT sequentialVertexColoring.c - sequentialVertexColoring_driver.cpp bipartite.c - bipartite_driver.cpp - edgeColoring.c bipartite.cpp diff --git a/src/coloring/bipartite.c b/src/coloring/bipartite.c index 4dbf494023..dd49056c28 100644 --- a/src/coloring/bipartite.c +++ b/src/coloring/bipartite.c @@ -35,11 +35,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/ii_t_rt.h" -#include "drivers/coloring/bipartite_driver.h" +#include "process/coloring_process.h" PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_bipartite); +#if 0 static void process( @@ -74,6 +75,7 @@ process( pgr_SPI_finish(); } +#endif PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -86,8 +88,10 @@ PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - process( + pgr_process_coloring( text_to_cstring(PG_GETARG_TEXT_P(0)), + + BIPARTITE, &result_tuples, &result_count); diff --git a/src/coloring/coloring_driver.cpp b/src/coloring/coloring_driver.cpp index bef428249c..3b48145422 100644 --- a/src/coloring/coloring_driver.cpp +++ b/src/coloring/coloring_driver.cpp @@ -42,7 +42,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" +#include "coloring/bipartite.hpp" #include "coloring/edgeColoring.hpp" +#include "coloring/sequentialVertexColoring.hpp" namespace pgrouting { namespace drivers { @@ -69,6 +71,8 @@ void do_coloring( using pgrouting::UndirectedGraph; using pgrouting::functions::edgeColoring; + using pgrouting::functions::sequentialVertexColoring; + using pgrouting::functions::pgr_bipartite; hint = edges_sql; auto edges = get_edges(edges_sql, true, false); @@ -90,6 +94,12 @@ void do_coloring( case EDGECOLORING: results = edgeColoring(undigraph); break; + case BIPARTITE: + results = pgr_bipartite(undigraph); + break; + case SEQUENTIAL: + results = sequentialVertexColoring(undigraph); + break; default: err << "Unknown coloring function" << get_name(which); return; @@ -98,7 +108,7 @@ void do_coloring( auto count = results.size(); if (count == 0) { - notice << "No results found"; + log << "No results found"; return; } diff --git a/src/coloring/sequentialVertexColoring.c b/src/coloring/sequentialVertexColoring.c index 8a538b2b72..0269742308 100644 --- a/src/coloring/sequentialVertexColoring.c +++ b/src/coloring/sequentialVertexColoring.c @@ -35,11 +35,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/ii_t_rt.h" -#include "drivers/coloring/sequentialVertexColoring_driver.h" +#include "process/coloring_process.h" PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_sequentialvertexcoloring); +#if 0 static void process( @@ -74,6 +75,7 @@ process( pgr_SPI_finish(); } +#endif PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -86,8 +88,10 @@ PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - process( + pgr_process_coloring( text_to_cstring(PG_GETARG_TEXT_P(0)), + + SEQUENTIAL, &result_tuples, &result_count); From 674a20bd53ee7da6dcc3dfcef7ba3948af46daad Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 21:44:43 -0600 Subject: [PATCH 13/15] Removing unused files --- include/drivers/coloring/bipartite_driver.h | 59 -------- .../drivers/coloring/edgeColoring_driver.h | 57 -------- .../sequentialVertexColoring_driver.h | 57 -------- src/coloring/bipartite_driver.cpp | 128 ------------------ src/coloring/edgeColoring_driver.cpp | 127 ----------------- .../sequentialVertexColoring_driver.cpp | 125 ----------------- 6 files changed, 553 deletions(-) delete mode 100644 include/drivers/coloring/bipartite_driver.h delete mode 100644 include/drivers/coloring/edgeColoring_driver.h delete mode 100644 include/drivers/coloring/sequentialVertexColoring_driver.h delete mode 100644 src/coloring/bipartite_driver.cpp delete mode 100644 src/coloring/edgeColoring_driver.cpp delete mode 100644 src/coloring/sequentialVertexColoring_driver.cpp diff --git a/include/drivers/coloring/bipartite_driver.h b/include/drivers/coloring/bipartite_driver.h deleted file mode 100644 index 4ad01e6fbd..0000000000 --- a/include/drivers/coloring/bipartite_driver.h +++ /dev/null @@ -1,59 +0,0 @@ -/*PGR-GNU***************************************************************** -File: bipartite_driver.h - -Generated with Template by: -Copyright (c) 2013-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2020 Prakash Tiwari -Mail: 85prakash2017@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_DRIVERS_COLORING_BIPARTITE_DRIVER_H_ -#define INCLUDE_DRIVERS_COLORING_BIPARTITE_DRIVER_H_ - - -/* for size-t */ -#ifdef __cplusplus -# include -using II_t_rt = struct II_t_rt; -#else -# include -typedef struct II_t_rt II_t_rt; -#endif - - -#ifdef __cplusplus -extern "C" { -#endif - -void -pgr_do_bipartite( - const char*, - - II_t_rt**, size_t*, - char**, char**, char**); - -#ifdef __cplusplus -} -#endif - -#endif // INCLUDE_DRIVERS_COLORING_BIPARTITE_DRIVER_H_ diff --git a/include/drivers/coloring/edgeColoring_driver.h b/include/drivers/coloring/edgeColoring_driver.h deleted file mode 100644 index 8b1364c847..0000000000 --- a/include/drivers/coloring/edgeColoring_driver.h +++ /dev/null @@ -1,57 +0,0 @@ -/*PGR-GNU***************************************************************** -File: edgeColoring_driver.h - -Generated with Template by: -Copyright (c) 2013-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Veenit Kumar -Mail: 123sveenit@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_DRIVERS_COLORING_EDGECOLORING_DRIVER_H_ -#define INCLUDE_DRIVERS_COLORING_EDGECOLORING_DRIVER_H_ - - -#ifdef __cplusplus -# include -using II_t_rt = struct II_t_rt; -#else -# include -typedef struct II_t_rt II_t_rt; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -void -pgr_do_edgeColoring( - const char*, - - II_t_rt**, size_t*, - char**, char**, char**); - -#ifdef __cplusplus -} -#endif - -#endif // INCLUDE_DRIVERS_COLORING_EDGECOLORING_DRIVER_H_ diff --git a/include/drivers/coloring/sequentialVertexColoring_driver.h b/include/drivers/coloring/sequentialVertexColoring_driver.h deleted file mode 100644 index 3913cdf95c..0000000000 --- a/include/drivers/coloring/sequentialVertexColoring_driver.h +++ /dev/null @@ -1,57 +0,0 @@ -/*PGR-GNU***************************************************************** -File: sequentialVertexColoring_driver.h - -Generated with Template by: -Copyright (c) 2007-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2020 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_DRIVERS_COLORING_SEQUENTIALVERTEXCOLORING_DRIVER_H_ -#define INCLUDE_DRIVERS_COLORING_SEQUENTIALVERTEXCOLORING_DRIVER_H_ -#pragma once - -#ifdef __cplusplus -# include -using II_t_rt = struct II_t_rt; -#else -# include -typedef struct II_t_rt II_t_rt; -#endif - - -#ifdef __cplusplus -extern "C" { -#endif - -void pgr_do_sequentialVertexColoring( - const char*, - - II_t_rt**, size_t*, - char**, char**, char**); - -#ifdef __cplusplus -} -#endif - -#endif // INCLUDE_DRIVERS_COLORING_SEQUENTIALVERTEXCOLORING_DRIVER_H_ diff --git a/src/coloring/bipartite_driver.cpp b/src/coloring/bipartite_driver.cpp deleted file mode 100644 index 3cc3a9591d..0000000000 --- a/src/coloring/bipartite_driver.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/*PGR-GNU***************************************************************** -File: bipartite_driver.cpp - -Generated with Template by: -Copyright (c) 2013-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2020 Prakash Tiwari -Mail: 85prakash2017@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "drivers/coloring/bipartite_driver.h" - -#include -#include -#include - -#include "c_types/ii_t_rt.h" -#include "cpp_common/pgdata_getters.hpp" -#include "cpp_common/alloc.hpp" -#include "cpp_common/assert.hpp" - -#include "coloring/bipartite.hpp" - - -void -pgr_do_bipartite( - const char *edges_sql, - - II_t_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg) { - using pgrouting::pgr_alloc; - using pgrouting::to_pg_msg; - using pgrouting::pgr_free; - - std::ostringstream log; - std::ostringstream notice; - std::ostringstream err; - const char *hint = nullptr; - - try { - // NOLINTBEGIN(clang-analyzer-cplusplus.NewDelete) - pgassert(!(*log_msg)); - pgassert(!(*notice_msg)); - pgassert(!(*err_msg)); - pgassert(!(*return_tuples)); - pgassert(*return_count == 0); - // NOLINTEND(clang-analyzer-cplusplus.NewDelete) - - using pgrouting::functions::pgr_bipartite; - - hint = edges_sql; - auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); - if (edges.empty()) { - *notice_msg = to_pg_msg("No edges found"); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - return; - } - hint = nullptr; - - pgrouting::UndirectedGraph undigraph; - undigraph.insert_edges(edges); - auto results = pgr_bipartite(undigraph); - - auto count = results.size(); - - if (count == 0) { - (*return_tuples) = NULL; - (*return_count) = 0; - notice << "No results found"; - *log_msg = to_pg_msg(notice); - return; - } - - (*return_tuples) = pgr_alloc(count, (*return_tuples)); - for (size_t i = 0; i < count; i++) { - *((*return_tuples) + i) = results[i]; - } - (*return_count) = count; - - pgassert(*err_msg == NULL); - *log_msg = to_pg_msg(log); - *notice_msg = to_pg_msg(notice); - } catch (AssertFailedException &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (const std::string &ex) { - *err_msg = to_pg_msg(ex); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - } catch (std::exception &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (...) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << "Caught unknown exception!"; - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } -} diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp deleted file mode 100644 index 5f8193bc0b..0000000000 --- a/src/coloring/edgeColoring_driver.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/*PGR-GNU***************************************************************** -File: edgeColoring_driver.cpp - -Generated with Template by: -Copyright (c) 2015-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Veenit Kumar -Mail: 123sveenit@gmail.com ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "drivers/coloring/edgeColoring_driver.h" - -#include -#include -#include - -#include "c_types/ii_t_rt.h" -#include "cpp_common/pgdata_getters.hpp" -#include "cpp_common/alloc.hpp" -#include "cpp_common/assert.hpp" - -#include "coloring/edgeColoring.hpp" - - -void pgr_do_edgeColoring( - const char *edges_sql, - - II_t_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg) { - using pgrouting::pgr_alloc; - using pgrouting::to_pg_msg; - using pgrouting::pgr_free; - - std::ostringstream log; - std::ostringstream notice; - std::ostringstream err; - const char *hint = nullptr; - - try { - pgassert(!(*log_msg)); - pgassert(!(*notice_msg)); - pgassert(!(*err_msg)); - pgassert(!(*return_tuples)); - pgassert(*return_count == 0); - - using pgrouting::pgget::get_edges; - using pgrouting::UndirectedGraph; - - using pgrouting::functions::edgeColoring; - - hint = edges_sql; - auto edges = get_edges(std::string(edges_sql), true, false); - if (edges.empty()) { - *notice_msg = to_pg_msg("No edges found"); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - return; - } - hint = nullptr; - - UndirectedGraph undigraph; - undigraph.insert_edges(edges); - auto results = edgeColoring(undigraph); - - auto count = results.size(); - - if (count == 0) { - (*return_tuples) = NULL; - (*return_count) = 0; - notice << "No results found"; - *log_msg = to_pg_msg(notice); - return; - } - - (*return_tuples) = pgr_alloc(count, (*return_tuples)); - for (size_t i = 0; i < count; i++) { - *((*return_tuples) + i) = results[i]; - } - (*return_count) = count; - - pgassert(*err_msg == NULL); - *log_msg = to_pg_msg(log); - *notice_msg = to_pg_msg(notice); - } catch (AssertFailedException &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (const std::string &ex) { - *err_msg = to_pg_msg(ex); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - } catch (std::exception &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (...) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << "Caught unknown exception!"; - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } -} diff --git a/src/coloring/sequentialVertexColoring_driver.cpp b/src/coloring/sequentialVertexColoring_driver.cpp deleted file mode 100644 index db5f48cfa1..0000000000 --- a/src/coloring/sequentialVertexColoring_driver.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/*PGR-GNU***************************************************************** -File: sequentialVertexColoring_driver.cpp - -Generated with Template by: -Copyright (c) 2015-2026 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2020 Ashish Kumar -Mail: ashishkr23438@gmail.com ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "drivers/coloring/sequentialVertexColoring_driver.h" - -#include -#include -#include - -#include "c_types/ii_t_rt.h" -#include "cpp_common/pgdata_getters.hpp" -#include "cpp_common/alloc.hpp" -#include "cpp_common/assert.hpp" - -#include "coloring/sequentialVertexColoring.hpp" - -void -pgr_do_sequentialVertexColoring( - const char *edges_sql, - - II_t_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg) { - using pgrouting::pgr_alloc; - using pgrouting::to_pg_msg; - using pgrouting::pgr_free; - - std::ostringstream log; - std::ostringstream notice; - std::ostringstream err; - const char *hint = nullptr; - - try { - pgassert(!(*log_msg)); - pgassert(!(*notice_msg)); - pgassert(!(*err_msg)); - pgassert(!(*return_tuples)); - pgassert(*return_count == 0); - - using pgrouting::functions::sequentialVertexColoring; - - - hint = edges_sql; - auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); - if (edges.empty()) { - *notice_msg = to_pg_msg("No edges found"); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - return; - } - hint = nullptr; - - pgrouting::UndirectedGraph undigraph; - undigraph.insert_edges(edges); - auto results = sequentialVertexColoring(undigraph); - - auto count = results.size(); - - if (count == 0) { - (*return_tuples) = NULL; - (*return_count) = 0; - notice << "No results found"; - *log_msg = to_pg_msg(notice); - return; - } - - (*return_tuples) = pgr_alloc(count, (*return_tuples)); - for (size_t i = 0; i < count; i++) { - *((*return_tuples) + i) = results[i]; - } - (*return_count) = count; - - pgassert(*err_msg == NULL); - *log_msg = to_pg_msg(log); - *notice_msg = to_pg_msg(notice); - } catch (AssertFailedException &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (const std::string &ex) { - *err_msg = to_pg_msg(ex); - *log_msg = hint? to_pg_msg(hint) : to_pg_msg(log); - } catch (std::exception &except) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << except.what(); - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } catch (...) { - (*return_tuples) = pgr_free(*return_tuples); - (*return_count) = 0; - err << "Caught unknown exception!"; - *err_msg = to_pg_msg(err); - *log_msg = to_pg_msg(log); - } -} From c17455440781347c7f888ec09b80ad4ac5d89c44 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 6 Mar 2026 21:45:46 -0600 Subject: [PATCH 14/15] Cleaning up code --- src/coloring/bipartite.c | 36 ------------------------- src/coloring/edgeColoring.c | 36 ------------------------- src/coloring/sequentialVertexColoring.c | 36 ------------------------- 3 files changed, 108 deletions(-) diff --git a/src/coloring/bipartite.c b/src/coloring/bipartite.c index dd49056c28..dfdd050a1a 100644 --- a/src/coloring/bipartite.c +++ b/src/coloring/bipartite.c @@ -40,42 +40,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_bipartite); -#if 0 -static -void -process( - char* edges_sql, - - II_t_rt **result_tuples, - size_t *result_count) { - pgr_SPI_connect(); - char* log_msg = NULL; - char* notice_msg = NULL; - char* err_msg = NULL; - - (*result_tuples) = NULL; - (*result_count) = 0; - - clock_t start_t = clock(); - pgr_do_bipartite( - edges_sql, - result_tuples, result_count, - &log_msg, - ¬ice_msg, - &err_msg); - time_msg("processing pgr_bipartite()", start_t, clock()); - - if (err_msg && (*result_tuples)) { - pfree(*result_tuples); - (*result_tuples) = NULL; - (*result_count) = 0; - } - - pgr_global_report(&log_msg, ¬ice_msg, &err_msg); - - pgr_SPI_finish(); -} -#endif PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; diff --git a/src/coloring/edgeColoring.c b/src/coloring/edgeColoring.c index eead4affc9..17c16bfcab 100644 --- a/src/coloring/edgeColoring.c +++ b/src/coloring/edgeColoring.c @@ -41,42 +41,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_edgecoloring); -#if 0 -static -void -process( - char* edges_sql, - - II_t_rt **result_tuples, - size_t *result_count) { - pgr_SPI_connect(); - char* log_msg = NULL; - char* notice_msg = NULL; - char* err_msg = NULL; - - (*result_tuples) = NULL; - (*result_count) = 0; - - clock_t start_t = clock(); - pgr_do_edgeColoring( - edges_sql, - result_tuples, result_count, - &log_msg, - ¬ice_msg, - &err_msg); - time_msg("processing pgr_edgeColoring", start_t, clock()); - - if (err_msg && (*result_tuples)) { - pfree(*result_tuples); - (*result_tuples) = NULL; - (*result_count) = 0; - } - - pgr_global_report(&log_msg, ¬ice_msg, &err_msg); - - pgr_SPI_finish(); -} -#endif PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; diff --git a/src/coloring/sequentialVertexColoring.c b/src/coloring/sequentialVertexColoring.c index 0269742308..f0ee97c5fa 100644 --- a/src/coloring/sequentialVertexColoring.c +++ b/src/coloring/sequentialVertexColoring.c @@ -40,42 +40,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_sequentialvertexcoloring); -#if 0 -static -void -process( - char* edges_sql, - - II_t_rt **result_tuples, - size_t *result_count) { - pgr_SPI_connect(); - char* log_msg = NULL; - char* notice_msg = NULL; - char* err_msg = NULL; - - (*result_tuples) = NULL; - (*result_count) = 0; - - clock_t start_t = clock(); - pgr_do_sequentialVertexColoring( - edges_sql, - result_tuples, result_count, - &log_msg, - ¬ice_msg, - &err_msg); - time_msg("processing pgr_sequentialVertexColoring", start_t, clock()); - - if (err_msg && (*result_tuples)) { - pfree(*result_tuples); - (*result_tuples) = NULL; - (*result_count) = 0; - } - - pgr_global_report(&log_msg, ¬ice_msg, &err_msg); - - pgr_SPI_finish(); -} -#endif PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; From a93bb194f84f38d0a28e44effbe95330d2843cdf Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 24 Mar 2026 09:32:05 -0600 Subject: [PATCH 15/15] refining edgeColoring pgtap test --- pgtap/coloring/edgeColoring/edge_cases.pg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgtap/coloring/edgeColoring/edge_cases.pg b/pgtap/coloring/edgeColoring/edge_cases.pg index 8dbee3d413..4dd8deb593 100644 --- a/pgtap/coloring/edgeColoring/edge_cases.pg +++ b/pgtap/coloring/edgeColoring/edge_cases.pg @@ -86,7 +86,7 @@ SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q5')), 'q5: -- {7,12}--{8,11} ; PREPARE q6 AS SELECT id, source, target, cost, reverse_cost -FROM edges WHERE id IN (8, 10, 11, 12); +FROM edges WHERE id IN (8, 10, 11, 12) ORDER BY id ASC; RETURN QUERY SELECT ok((SELECT count(DISTINCT color) <= 3 FROM pgr_edgeColoring('q6')), 'q6: At most three colors are expected');