johnson_all_pairs_shortest.hpp 7.5 KB

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