/Src/Dependencies/Boost/boost/graph/johnson_all_pairs_shortest.hpp

http://hadesmem.googlecode.com/ · C++ Header · 204 lines · 158 code · 22 blank · 24 comment · 20 complexity · 098324f7f67ccd12000fbc9f2a6c2e68 MD5 · raw file

  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. /*
  10. This file implements the function
  11. template <class VertexAndEdgeListGraph, class DistanceMatrix,
  12. class P, class T, class R>
  13. bool
  14. johnson_all_pairs_shortest_paths
  15. (VertexAndEdgeListGraph& g,
  16. DistanceMatrix& D,
  17. const bgl_named_params<P, T, R>& params)
  18. */
  19. #ifndef BOOST_GRAPH_JOHNSON_HPP
  20. #define BOOST_GRAPH_JOHNSON_HPP
  21. #include <boost/graph/graph_traits.hpp>
  22. #include <boost/property_map/property_map.hpp>
  23. #include <boost/property_map/shared_array_property_map.hpp>
  24. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  25. #include <boost/graph/dijkstra_shortest_paths.hpp>
  26. #include <boost/graph/adjacency_list.hpp>
  27. #include <boost/type_traits/same_traits.hpp>
  28. namespace boost {
  29. template <class VertexAndEdgeListGraph, class DistanceMatrix,
  30. class VertexID, class Weight, typename BinaryPredicate,
  31. typename BinaryFunction, typename Infinity, class DistanceZero>
  32. bool
  33. johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
  34. DistanceMatrix& D,
  35. VertexID id1, Weight w1, const BinaryPredicate& compare,
  36. const BinaryFunction& combine, const Infinity& inf,
  37. DistanceZero zero)
  38. {
  39. typedef graph_traits<VertexAndEdgeListGraph> Traits1;
  40. typedef typename property_traits<Weight>::value_type DT;
  41. function_requires< BasicMatrixConcept<DistanceMatrix,
  42. typename Traits1::vertices_size_type, DT> >();
  43. typedef typename Traits1::directed_category DirCat;
  44. bool is_undirected = is_same<DirCat, undirected_tag>::value;
  45. typedef adjacency_list<vecS, vecS, directedS,
  46. property< vertex_distance_t, DT>,
  47. property< edge_weight_t, DT,
  48. property< edge_weight2_t, DT > > > Graph2;
  49. typedef graph_traits<Graph2> Traits2;
  50. Graph2 g2(num_vertices(g1) + 1);
  51. typename property_map<Graph2, edge_weight_t>::type
  52. w = get(edge_weight, g2);
  53. typename property_map<Graph2, edge_weight2_t>::type
  54. w_hat = get(edge_weight2, g2);
  55. typename property_map<Graph2, vertex_distance_t>::type
  56. d = get(vertex_distance, g2);
  57. typedef typename property_map<Graph2, vertex_index_t>::type VertexID2;
  58. VertexID2 id2 = get(vertex_index, g2);
  59. // Construct g2 where V[g2] = V[g1] U {s}
  60. // and E[g2] = E[g1] U {(s,v)| v in V[g1]}
  61. std::vector<typename Traits1::vertex_descriptor>
  62. verts1(num_vertices(g1) + 1);
  63. typename Traits2::vertex_descriptor s = *vertices(g2).first;
  64. {
  65. typename Traits1::vertex_iterator v, v_end;
  66. int i = 1;
  67. for (boost::tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
  68. typename Traits2::edge_descriptor e; bool z;
  69. boost::tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
  70. put(w, e, zero);
  71. verts1[i] = *v;
  72. }
  73. typename Traits1::edge_iterator e, e_end;
  74. for (boost::tie(e, e_end) = edges(g1); e != e_end; ++e) {
  75. typename Traits2::edge_descriptor e2; bool z;
  76. boost::tie(e2, z) = add_edge(get(id1, source(*e, g1)) + 1,
  77. get(id1, target(*e, g1)) + 1, g2);
  78. put(w, e2, get(w1, *e));
  79. if (is_undirected) {
  80. boost::tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
  81. get(id1, source(*e, g1)) + 1, g2);
  82. put(w, e2, get(w1, *e));
  83. }
  84. }
  85. }
  86. typename Traits2::vertex_iterator v, v_end, u, u_end;
  87. typename Traits2::edge_iterator e, e_end;
  88. shared_array_property_map<DT,VertexID2> h(num_vertices(g2), id2);
  89. for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
  90. put(d, *v, inf);
  91. put(d, s, zero);
  92. // Using the non-named parameter versions of bellman_ford and
  93. // dijkstra for portability reasons.
  94. dummy_property_map pred; bellman_visitor<> bvis;
  95. if (bellman_ford_shortest_paths
  96. (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
  97. for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
  98. put(h, *v, get(d, *v));
  99. // Reweight the edges to remove negatives
  100. for (boost::tie(e, e_end) = edges(g2); e != e_end; ++e) {
  101. typename Traits2::vertex_descriptor a = source(*e, g2),
  102. b = target(*e, g2);
  103. put(w_hat, *e, combine(get(w, *e), (get(h, a) - get(h, b))));
  104. }
  105. for (boost::tie(u, u_end) = vertices(g2); u != u_end; ++u) {
  106. dijkstra_visitor<> dvis;
  107. dijkstra_shortest_paths
  108. (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
  109. for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v) {
  110. if (*u != s && *v != s) {
  111. typename Traits1::vertex_descriptor u1, v1;
  112. u1 = verts1[get(id2, *u)]; v1 = verts1[get(id2, *v)];
  113. D[get(id2, *u)-1][get(id2, *v)-1] = combine(get(d, *v), (get(h, *v) - get(h, *u)));
  114. }
  115. }
  116. }
  117. return true;
  118. } else
  119. return false;
  120. }
  121. template <class VertexAndEdgeListGraph, class DistanceMatrix,
  122. class VertexID, class Weight, class DistanceZero>
  123. bool
  124. johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
  125. DistanceMatrix& D,
  126. VertexID id1, Weight w1, DistanceZero zero)
  127. {
  128. typedef typename property_traits<Weight>::value_type WT;
  129. return johnson_all_pairs_shortest_paths(g1, D, id1, w1,
  130. std::less<WT>(),
  131. closed_plus<WT>(),
  132. (std::numeric_limits<WT>::max)(),
  133. zero);
  134. }
  135. namespace detail {
  136. template <class VertexAndEdgeListGraph, class DistanceMatrix,
  137. class P, class T, class R, class Weight,
  138. class VertexID>
  139. bool
  140. johnson_dispatch(VertexAndEdgeListGraph& g,
  141. DistanceMatrix& D,
  142. const bgl_named_params<P, T, R>& params,
  143. Weight w, VertexID id)
  144. {
  145. typedef typename property_traits<Weight>::value_type WT;
  146. return johnson_all_pairs_shortest_paths
  147. (g, D, id, w,
  148. choose_param(get_param(params, distance_compare_t()),
  149. std::less<WT>()),
  150. choose_param(get_param(params, distance_combine_t()),
  151. closed_plus<WT>()),
  152. choose_param(get_param(params, distance_inf_t()),
  153. std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
  154. choose_param(get_param(params, distance_zero_t()), WT()) );
  155. }
  156. } // namespace detail
  157. template <class VertexAndEdgeListGraph, class DistanceMatrix,
  158. class P, class T, class R>
  159. bool
  160. johnson_all_pairs_shortest_paths
  161. (VertexAndEdgeListGraph& g,
  162. DistanceMatrix& D,
  163. const bgl_named_params<P, T, R>& params)
  164. {
  165. return detail::johnson_dispatch
  166. (g, D, params,
  167. choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
  168. choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
  169. );
  170. }
  171. template <class VertexAndEdgeListGraph, class DistanceMatrix>
  172. bool
  173. johnson_all_pairs_shortest_paths
  174. (VertexAndEdgeListGraph& g, DistanceMatrix& D)
  175. {
  176. bgl_named_params<int,int> params(1);
  177. return detail::johnson_dispatch
  178. (g, D, params, get(edge_weight, g), get(vertex_index, g));
  179. }
  180. } // namespace boost
  181. #endif // BOOST_GRAPH_JOHNSON_HPP