Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgr_sequentialVertexColoring GSoC-2020 Week 4 #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d46ccb4
[sequentialVertexColoring] Added pgr_sequentialVertexColoring.hpp file
krashish8 Jun 22, 2020
38711e0
[sequentialVertexColoring] Added basic code in hpp file
krashish8 Jun 22, 2020
5b19676
[sequentialVertexColoring] Trying to apply the boost algorithm
krashish8 Jun 23, 2020
cd134d2
[sequentialVertexColoring] Trying to use color map with boost
krashish8 Jun 24, 2020
94a9fa0
[sequentialVertexColoring] Boost algorithm is working
krashish8 Jun 24, 2020
7dbd0a0
[depthFirstSearch] Modified log_msg and notice_msg in case of empty rows
krashish8 Jun 24, 2020
4b30ff5
[sequentialVertexColoring] Removed unnecessary code
krashish8 Jun 24, 2020
351b3af
[sequentialVertexColoring] Removed boost namespace
krashish8 Jun 24, 2020
3c86e9d
[sequentialVertexColoring] Changed log_msg and notice_msg in case of …
krashish8 Jun 24, 2020
4e09b22
[depthFirstSearch] Changed log_msg in case of empty rows
krashish8 Jun 24, 2020
4bebff2
[sequentialVertexColoring] Changed log_msg in case of empty rows
krashish8 Jun 24, 2020
c3134cf
[sequentialVertexColoring] Calling the boost function in try-catch block
krashish8 Jun 25, 2020
d83f3bc
[sequentialVertexColoring] Added comments
krashish8 Jun 25, 2020
885413e
[sequentialVertexColoring] Completed boost implementation
krashish8 Jun 25, 2020
9b85427
[sequentialVertexColoring] Removed unnecessary code
krashish8 Jun 25, 2020
9b0d610
[sequentialVertexColoring] Added comments
krashish8 Jun 25, 2020
c0d464c
[depthFirstSearch] Fixed error in the docstring
krashish8 Jun 25, 2020
f6351c6
[sequentialVertexColoring] Modified docstring
krashish8 Jun 25, 2020
450fb1e
[sequentialVertexColoring] Removed boost namespace
krashish8 Jun 25, 2020
bc581a5
[sequentialVertexColoring] Restored all the signatures of the functions
krashish8 Jun 25, 2020
1e607ab
[sequentialVertexColoring] Modified the docqueries
krashish8 Jun 25, 2020
39d99f6
[sequentialVertexColoring] Fixed indentation
krashish8 Jun 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docqueries/graphColoring/doc-pgr_sequentialVertexColoring.result
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/depthFirstSearch/pgr_depthFirstSearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
std::vector<pgr_mst_rt> get_results(
Expand Down
147 changes: 147 additions & 0 deletions include/graphColoring/pgr_sequentialVertexColoring.hpp
Original file line number Diff line number Diff line change
@@ -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 <boost/property_map/property_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map/vector_property_map.hpp>
#include <boost/type_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/sequential_vertex_coloring.hpp>

#include <vector>
#include <map>

#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 G>
class Pgr_sequentialVertexColoring : public pgrouting::Pgr_messages {
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/doc/libs/1_73_0/libs/graph/doc/sequential_vertex_coloring.html)
*/
std::vector<pgr_vertex_color_rt> sequentialVertexColoring(
G &graph) {
std::vector<pgr_vertex_color_rt> results;

// 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 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<pgr_vertex_color_rt> get_results(
std::vector<vertices_size_type> &colors,
const G &graph) {
std::vector<pgr_vertex_color_rt> results;

typename boost::graph_traits<Graph>::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_
13 changes: 9 additions & 4 deletions src/graphColoring/sequentialVertexColoring_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <class G>
std::vector<pgr_vertex_color_rt>
pgr_sequentialVertexColoring(
Expand All @@ -73,7 +70,6 @@ pgr_sequentialVertexColoring(
log += fn_sequentialVertexColoring.get_log();
return results;
}
#endif

/** @brief Performs exception handling and converts the results to postgres.
*
Expand Down Expand Up @@ -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
Expand Down