Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 4 additions & 25 deletions include/coloring/edgeColoring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#define INCLUDE_COLORING_EDGECOLORING_HPP_
#pragma once

#include <iostream>
#include <map>
#include <vector>
#include <cstdint>
#include <iosfwd>

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/version.hpp>

#include "cpp_common/base_graph.hpp"
#include "cpp_common/edge_t.hpp"
#include "c_types/ii_t_rt.h"
#include "cpp_common/assert.hpp"
#include "cpp_common/messages.hpp"

namespace pgrouting {
namespace functions {

class Pgr_edgeColoring : public Pgr_messages {
public:
using EdgeColoring_Graph =
boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
int64_t, boost::no_property>;

using V = boost::graph_traits<EdgeColoring_Graph>::vertex_descriptor;
using E = boost::graph_traits<EdgeColoring_Graph>::edge_descriptor;
using V_it = boost::graph_traits<EdgeColoring_Graph>::vertex_iterator;
using E_it = boost::graph_traits<EdgeColoring_Graph>::edge_iterator;

public:
public:
std::vector<II_t_rt> edgeColoring();

explicit Pgr_edgeColoring(const std::vector<Edge_t>&);
Expand All @@ -69,16 +57,7 @@ class Pgr_edgeColoring : public Pgr_messages {
#endif

private:
V get_boost_vertex(int64_t id) const;
int64_t get_vertex_id(V v) const;
int64_t get_edge_id(E e) const;


private:
EdgeColoring_Graph graph;
std::map<int64_t, V> id_to_V;
std::map<V, int64_t> V_to_id;
std::map<E, int64_t> E_to_id;
pgrouting::UndirectedGraph graph;
};

} // namespace functions
Expand Down
94 changes: 18 additions & 76 deletions src/coloring/edgeColoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include "coloring/edgeColoring.hpp"

#include <map>

#include <vector>
#include <utility>
#include <string>

#include "cpp_common/identifiers.hpp"
#include <boost/graph/edge_coloring.hpp>
#include <boost/graph/graph_utility.hpp>

#include "cpp_common/base_graph.hpp"
#include "cpp_common/assert.hpp"
#include "cpp_common/interruption.hpp"

Expand All @@ -50,91 +52,31 @@ Pgr_edgeColoring::edgeColoring() {
CHECK_FOR_INTERRUPTS();

try {
boost::edge_coloring(graph, boost::get(boost::edge_bundle, graph));
} catch (...) {
throw std::make_pair(
std::string("INTERNAL: something went wrong while calling boost::edge_coloring"),
std::string(__PGR_PRETTY_FUNCTION__));
}

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<Edge_t> &edges) {
/*
* Inserting vertices
*/
Identifiers<int64_t> ids;
for (const auto &e : edges) {
ids += e.source;
ids += e.target;
}
using E = pgrouting::UndirectedGraph::E;

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;
std::map<E, int64_t> color_storage;
auto color_map = boost::make_assoc_property_map(color_storage);

if (edge.source == edge.target) continue;
boost::edge_coloring(graph.graph, color_map);

if (edge.cost < 0 && edge.reverse_cost < 0) continue;
for (auto e_i : boost::make_iterator_range(boost::edges(graph.graph))) {
int64_t edge_id = graph.graph[e_i].id;
int64_t color = color_map[e_i];

E e;
// NOLINTNEXTLINE
boost::tie(e, added) = boost::add_edge(v1, v2, graph);

E_to_id.insert(std::make_pair(e, edge.id));
}
}

Pgr_edgeColoring::V
Pgr_edgeColoring::get_boost_vertex(int64_t id) const {
try {
return id_to_V.at(id);
results.push_back({{edge_id}, {(color + 1)}});
}
} catch (...) {
throw std::make_pair(
std::string("INTERNAL: something went wrong when getting the vertex descriptor"),
throw std::make_pair
(
std::string("INTERNAL: something went wrong while calling boost::edge_coloring"),
std::string(__PGR_PRETTY_FUNCTION__));
}
}

int64_t
Pgr_edgeColoring::get_vertex_id(V v) const {
try {
return V_to_id.at(v);
} catch (...) {
throw std::make_pair(
std::string("INTERNAL: something went wrong when getting the vertex id"),
std::string(__PGR_PRETTY_FUNCTION__));
}
return results;
}

int64_t
Pgr_edgeColoring::get_edge_id(E e) const {
try {
return E_to_id.at(e);
} catch (...) {
throw std::make_pair(
std::string("INTERNAL: something went wrong when getting the edge id"),
std::string(__PGR_PRETTY_FUNCTION__));
}
Pgr_edgeColoring::Pgr_edgeColoring(const std::vector<Edge_t> &edges) {
graph.insert_edges(edges);
}

} // namespace functions
Expand Down