johnson_all_pairs_shortest.hpp 7.5 KB

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