diff --git a/docqueries/graphColoring/doc-pgr_sequentialVertexColoring.result b/docqueries/graphColoring/doc-pgr_sequentialVertexColoring.result index 68b78e3c81..51c4f729ba 100644 --- a/docqueries/graphColoring/doc-pgr_sequentialVertexColoring.result +++ b/docqueries/graphColoring/doc-pgr_sequentialVertexColoring.result @@ -8,7 +8,24 @@ SELECT * FROM pgr_sequentialVertexColoring( ); seq | node | color -----+------+------- -(0 rows) + 1 | 1 | 0 + 2 | 2 | 1 + 3 | 3 | 0 + 4 | 4 | 1 + 5 | 5 | 0 + 6 | 6 | 1 + 7 | 7 | 0 + 8 | 8 | 1 + 9 | 9 | 0 + 10 | 10 | 1 + 11 | 11 | 0 + 12 | 12 | 1 + 13 | 13 | 0 + 14 | 14 | 0 + 15 | 15 | 1 + 16 | 16 | 0 + 17 | 17 | 1 +(17 rows) -- q2 ROLLBACK; diff --git a/include/depthFirstSearch/pgr_depthFirstSearch.hpp b/include/depthFirstSearch/pgr_depthFirstSearch.hpp index 3f970d7aa9..ef14a5be28 100644 --- a/include/depthFirstSearch/pgr_depthFirstSearch.hpp +++ b/include/depthFirstSearch/pgr_depthFirstSearch.hpp @@ -165,7 +165,7 @@ class Pgr_depthFirstSearch : public pgrouting::Pgr_messages { * @param max_depth the maximum depth of traversal * @param graph the graph containing the edges * - * @returns bool @b True, when results are found + * @returns `results` vector */ template std::vector get_results( diff --git a/include/graphColoring/pgr_sequentialVertexColoring.hpp b/include/graphColoring/pgr_sequentialVertexColoring.hpp new file mode 100644 index 0000000000..4e98c8ced5 --- /dev/null +++ b/include/graphColoring/pgr_sequentialVertexColoring.hpp @@ -0,0 +1,147 @@ +/*PGR-GNU***************************************************************** +File: pgr_sequentialVertexColoring.hpp + +Copyright (c) 2020 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*/ + +#ifndef INCLUDE_GRAPHCOLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_ +#define INCLUDE_GRAPHCOLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_ +#pragma once + + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "cpp_common/pgr_base_graph.hpp" +#include "cpp_common/pgr_messages.h" + + +/** @file pgr_sequentialVertexColoring.hpp + * @brief The main file which calls the respective boost function. + * + * Contains actual implementation of the function and the calling + * of the respective boost function. + */ + + +namespace pgrouting { +namespace functions { + +//************************************************************* + +template +class Pgr_sequentialVertexColoring : public pgrouting::Pgr_messages { + public: + typedef typename G::V V; + typedef typename G::E E; + typedef boost::adjacency_list Graph; + typedef boost::graph_traits::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/doc/libs/1_73_0/libs/graph/doc/sequential_vertex_coloring.html) + */ + std::vector sequentialVertexColoring( + G &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)); + + // An iterator property map which records the colors of each vertex + auto color_map = boost::make_iterator_property_map(colors.begin(), + boost::get(boost::vertex_index, graph.graph)); + + try { + // calling the boost function + 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 &colors, + const G &graph) { + std::vector results; + + typename boost::graph_traits::vertex_iterator v, vend; + + // iterate through every vertex in the graph + for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) { + int64_t node = graph[*v].id; + int64_t color = colors[*v]; + + // push the vertex id and the color of the vertex in the `results` vector + results.push_back({ + node, + color + }); + } + + return results; + } +}; +} // namespace functions +} // namespace pgrouting + +#endif // INCLUDE_GRAPHCOLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_ diff --git a/src/graphColoring/sequentialVertexColoring_driver.cpp b/src/graphColoring/sequentialVertexColoring_driver.cpp index b377d385f8..efce3cd32f 100644 --- a/src/graphColoring/sequentialVertexColoring_driver.cpp +++ b/src/graphColoring/sequentialVertexColoring_driver.cpp @@ -36,9 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -#if 0 #include "graphColoring/pgr_sequentialVertexColoring.hpp" -#endif /** @file sequentialVertexColoring_driver.cpp * @brief Handles actual calling of function in the `pgr_sequentialVertexColoring.hpp` file. @@ -61,7 +59,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * @returns results, when results are found */ -#if 0 template std::vector pgr_sequentialVertexColoring( @@ -73,7 +70,6 @@ pgr_sequentialVertexColoring( log += fn_sequentialVertexColoring.get_log(); return results; } -#endif /** @brief Performs exception handling and converts the results to postgres. * @@ -124,6 +120,15 @@ do_pgr_sequentialVertexColoring( // string variable to store the log messages std::string logstr; + graphType gType = UNDIRECTED; + pgrouting::UndirectedGraph undigraph(gType); + undigraph.insert_edges(data_edges, total_edges); + + // calls the template function + results = pgr_sequentialVertexColoring( + undigraph, + logstr); + log << logstr; // the count of rows in the result