25 #ifndef INCLUDE_CPP_COMMON_PGR_BASE_GRAPH_HPP_
26 #define INCLUDE_CPP_COMMON_PGR_BASE_GRAPH_HPP_
29 #include <boost/graph/iteration_macros.hpp>
30 #include <boost/config.hpp>
31 #include <boost/graph/adjacency_list.hpp>
32 #include <boost/graph/graph_utility.hpp>
167 template <
class G,
typename Vertex,
typename Edge>
183 boost::adjacency_list < boost::vecS, boost::vecS,
189 boost::adjacency_list < boost::vecS, boost::vecS,
190 boost::bidirectionalS,
195 boost::adjacency_list < boost::listS, boost::vecS,
201 boost::adjacency_list < boost::listS, boost::vecS,
202 boost::bidirectionalS,
211 template <
typename G,
typename T_V,
typename T_E>
212 class Pgr_base_graph {
229 typedef typename boost::graph_traits < G >::vertex_descriptor
V;
230 typedef typename boost::graph_traits < G >::edge_descriptor
E;
231 typedef typename boost::graph_traits < G >::vertex_iterator
V_i;
232 typedef typename boost::graph_traits < G >::edge_iterator
E_i;
233 typedef typename boost::graph_traits < G >::out_edge_iterator
EO_i;
234 typedef typename boost::graph_traits < G >::in_edge_iterator
EI_i;
236 typedef typename boost::graph_traits < G >::vertices_size_type
238 typedef typename boost::graph_traits < G >::edges_size_type
240 typedef typename boost::graph_traits < G >::degree_size_type
253 typedef typename std::map< int64_t, V >
id_to_V;
254 typedef typename id_to_V::const_iterator
LI;
269 typename boost::property_map<G, boost::vertex_index_t>::type
vertIndex;
296 const std::vector< T_V > &vertices,
graphType gtype)
297 :
graph(vertices.size()),
299 m_num_vertices(vertices.size()),
310 for (
auto vi = boost::vertices(
graph).first;
311 vi != boost::vertices(
graph).second; ++vi) {
313 graph[(*vi)].cp_members(vertices[i]);
319 std::ostringstream log;
324 << iter->first <<
"\tValue:" << iter->second <<
"\n";
326 for (
const auto vertex : vertices) {
356 template <
typename T >
361 template <
typename T >
363 insert_edges(std::vector < T >(edges, edges + count),
false);
366 template <
typename T>
368 for (
size_t i = 0; i < count; ++i) {
375 template <
typename T >
393 template <
typename T>
404 for (
const auto edge : edges) {
409 template <
typename T>
414 template <
typename T>
417 for (
const auto edge : edges) {
422 template <
typename T >
424 const std::vector<T> &edges,
425 bool normal =
true) {
426 for (
const auto edge : edges) {
463 std::vector< T_V > vertices) {
465 for (
const auto vertex : vertices) {
468 auto v = add_vertex(
graph);
470 graph[v].cp_members(vertex);
493 auto v =
get_V(vertex_id);
515 auto v = add_vertex(
graph);
516 graph[v].cp_members(vertex);
578 boost::in_degree(v,
graph) :
579 boost::out_degree(v,
graph);
588 return boost::out_degree(v,
graph);
649 for (
auto vi = vertices(g.graph).first;
650 vi != vertices(g.graph).second; ++vi) {
651 if ((*vi) >= g.num_vertices())
break;
652 log << (*vi) <<
": " <<
" out_edges_of(" << g.graph[(*vi)] <<
"):";
653 for (boost::tie(out, out_end) = out_edges(*vi, g.graph);
654 out != out_end; ++out) {
656 << g.graph[*out].id <<
"=("
657 << g[g.source(*out)].id <<
", "
658 << g[g.target(*out)].id <<
") = "
659 << g.graph[*out].cost <<
"\t";
674 double &distance)
const {
677 V v_source, v_target;
678 double minCost = (std::numeric_limits<double>::max)();
681 for (boost::tie(out_i, out_end) = boost::out_edges(from,
graph);
682 out_i != out_end; ++out_i) {
690 if ((from == v_source) && (to == v_target)
691 && (distance ==
graph[e].cost)) {
694 if ((from == v_source) && (to == v_target)
695 && (minCost >
graph[e].cost)) {
696 minCost =
graph[e].cost;
709 template <
typename T >
712 template <
typename T >
715 template <
typename T >
720 template <
typename T>
728 std::ostringstream log;
732 log <<
"Key: " << iter->first <<
"\tValue:" << iter->second <<
"\n";
743 boost::tie(e, inserted) =
744 boost::add_edge(vm_s, vm_t,
graph);
752 boost::tie(e, inserted) =
753 boost::add_edge(vm_t, vm_s,
graph);
763 template <
class G,
typename T_V,
typename T_E >
769 if (!has_vertex(p_from) || !has_vertex(p_to))
return;
772 V g_from(get_V(p_from));
776 for (boost::tie(out, out_end) = out_edges(g_from, graph);
777 out != out_end; ++out) {
778 if (target(*out) == g_to) {
779 d_edge.id = graph[*out].id;
780 d_edge.source = graph[source(*out)].id;
781 d_edge.target = graph[target(*out)].id;
782 d_edge.cost = graph[*out].cost;
783 removed_edges.push_back(d_edge);
787 boost::remove_edge(g_from, g_to, graph);
792 template <
class G,
typename T_V,
typename T_E >
795 int64_t vertex_id, int64_t edge_id) {
799 if (!has_vertex(vertex_id))
return;
800 auto v_from(get_V(vertex_id));
807 for (boost::tie(out, out_end) = out_edges(v_from, graph);
808 out != out_end; ++out) {
809 if (graph[*out].
id == edge_id) {
810 d_edge.id = graph[*out].id;
811 d_edge.source = graph[source(*out)].id;
812 d_edge.target = graph[target(*out)].id;
813 d_edge.cost = graph[*out].cost;
814 removed_edges.push_back(d_edge);
815 boost::remove_edge((*out), graph);
824 template <
class G,
typename T_V,
typename T_E >
827 if (!has_vertex(vertex))
return;
828 disconnect_vertex(get_V(vertex));
831 template <
class G,
typename T_V,
typename T_E >
838 for (boost::tie(out, out_end) = out_edges(vertex, graph);
839 out != out_end; ++out) {
840 d_edge.id = graph[*out].id;
841 d_edge.source = graph[source(*out)].id;
842 d_edge.target = graph[target(*out)].id;
843 d_edge.cost = graph[*out].cost;
844 removed_edges.push_back(d_edge);
850 for (boost::tie(in, in_end) = in_edges(vertex, graph);
851 in != in_end; ++in) {
852 d_edge.id = graph[*in].id;
853 d_edge.source = graph[source(*in)].id;
854 d_edge.target = graph[target(*in)].id;
855 d_edge.cost = graph[*in].cost;
856 removed_edges.push_back(d_edge);
861 boost::clear_vertex(vertex, graph);
864 template <
class G,
typename T_V,
typename T_E >
867 while (removed_edges.size() != 0) {
868 graph_add_edge(removed_edges[0]);
869 removed_edges.pop_front();
876 template <
class G,
typename T_V,
typename T_E >
881 double &distance)
const {
884 V v_source, v_target;
885 double minCost = (std::numeric_limits<double>::max)();
886 int64_t minEdge = -1;
887 for (boost::tie(out_i, out_end) = boost::out_edges(from, graph);
888 out_i != out_end; ++out_i) {
890 v_target = target(e);
891 v_source = source(e);
892 if ((from == v_source) && (to == v_target)
893 && (distance == graph[e].cost))
895 if ((from == v_source) && (to == v_target)
896 && (minCost > graph[e].cost)) {
897 minCost = graph[e].cost;
898 minEdge = graph[e].id;
901 distance = minEdge == -1? 0: minCost;
906 template <
class G,
typename T_V,
typename T_E >
913 if (vm_s == vertices_map.end()) {
919 if (vm_t == vertices_map.end()) {
926 boost::tie(e, inserted) =
927 boost::add_edge(vm_s->second, vm_t->second, graph);
928 graph[e].cp_members(
edge);
933 template <
class G,
typename T_V,
typename T_E >
934 template <
typename T>
946 auto vm_s = get_V(T_V(
edge,
true));
947 auto vm_t = get_V(T_V(
edge,
false));
952 boost::tie(e, inserted) =
953 boost::add_edge(vm_s, vm_t, graph);
961 boost::tie(e, inserted) =
962 boost::add_edge(vm_t, vm_s, graph);
969 template <
class G,
typename T_V,
typename T_E >
970 template <
typename T>
982 auto vm_s = get_V(T_V(
edge,
true));
983 auto vm_t = get_V(T_V(
edge,
false));
990 boost::tie(e1, found) =
edge(vm_s, vm_t, graph);
997 boost::tie(e, inserted) =
998 boost::add_edge(vm_s, vm_t, graph);
1009 boost::tie(e1, found) =
edge(vm_t, vm_s, graph);
1016 boost::tie(e, inserted) =
1017 boost::add_edge(vm_t, vm_s, graph);
1032 template <
class G,
typename T_V,
typename T_E >
1033 template <
typename T>
1039 auto vm_s = get_V(T_V(
edge,
true));
1040 auto vm_t = get_V(T_V(
edge,
false));
1045 boost::tie(e, inserted) =
1046 boost::add_edge(vm_s, vm_t, graph);
1057 boost::tie(e, inserted) =
1058 boost::add_edge(vm_t, vm_s, graph);
1075 #endif // INCLUDE_CPP_COMMON_PGR_BASE_GRAPH_HPP_