dag_shortest_paths.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //=======================================================================
  2. // Copyright 2002 Indiana University.
  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. #ifndef BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
  10. #define BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
  11. #include <boost/graph/topological_sort.hpp>
  12. #include <boost/graph/dijkstra_shortest_paths.hpp>
  13. // single-source shortest paths for a Directed Acyclic Graph (DAG)
  14. namespace boost {
  15. // Initalize distances and call depth first search
  16. template <class VertexListGraph, class DijkstraVisitor,
  17. class DistanceMap, class WeightMap, class ColorMap,
  18. class PredecessorMap,
  19. class Compare, class Combine,
  20. class DistInf, class DistZero>
  21. inline void
  22. dag_shortest_paths
  23. (const VertexListGraph& g,
  24. typename graph_traits<VertexListGraph>::vertex_descriptor s,
  25. DistanceMap distance, WeightMap weight, ColorMap color,
  26. PredecessorMap pred,
  27. DijkstraVisitor vis, Compare compare, Combine combine,
  28. DistInf inf, DistZero zero)
  29. {
  30. typedef typename graph_traits<VertexListGraph>::vertex_descriptor Vertex;
  31. std::vector<Vertex> rev_topo_order;
  32. rev_topo_order.reserve(num_vertices(g));
  33. // Call 'depth_first_visit', not 'topological_sort', because we don't
  34. // want to traverse the entire graph, only vertices reachable from 's',
  35. // and 'topological_sort' will traverse everything. The logic below
  36. // is the same as for 'topological_sort', only we call 'depth_first_visit'
  37. // and 'topological_sort' calls 'depth_first_search'.
  38. topo_sort_visitor<std::back_insert_iterator<std::vector<Vertex> > >
  39. topo_visitor(std::back_inserter(rev_topo_order));
  40. depth_first_visit(g, s, topo_visitor, color);
  41. typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
  42. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
  43. put(distance, *ui, inf);
  44. put(pred, *ui, *ui);
  45. }
  46. put(distance, s, zero);
  47. vis.discover_vertex(s, g);
  48. typename std::vector<Vertex>::reverse_iterator i;
  49. for (i = rev_topo_order.rbegin(); i != rev_topo_order.rend(); ++i) {
  50. Vertex u = *i;
  51. vis.examine_vertex(u, g);
  52. typename graph_traits<VertexListGraph>::out_edge_iterator e, e_end;
  53. for (boost::tie(e, e_end) = out_edges(u, g); e != e_end; ++e) {
  54. vis.discover_vertex(target(*e, g), g);
  55. bool decreased = relax(*e, g, weight, pred, distance,
  56. combine, compare);
  57. if (decreased)
  58. vis.edge_relaxed(*e, g);
  59. else
  60. vis.edge_not_relaxed(*e, g);
  61. }
  62. vis.finish_vertex(u, g);
  63. }
  64. }
  65. namespace detail {
  66. // Defaults are the same as Dijkstra's algorithm
  67. // Handle Distance Compare, Combine, Inf and Zero defaults
  68. template <class VertexListGraph, class DijkstraVisitor,
  69. class DistanceMap, class WeightMap, class ColorMap,
  70. class IndexMap, class Params>
  71. inline void
  72. dag_sp_dispatch2
  73. (const VertexListGraph& g,
  74. typename graph_traits<VertexListGraph>::vertex_descriptor s,
  75. DistanceMap distance, WeightMap weight, ColorMap color, IndexMap /*id*/,
  76. DijkstraVisitor vis, const Params& params)
  77. {
  78. typedef typename property_traits<DistanceMap>::value_type D;
  79. dummy_property_map p_map;
  80. D inf =
  81. choose_param(get_param(params, distance_inf_t()),
  82. (std::numeric_limits<D>::max)());
  83. dag_shortest_paths
  84. (g, s, distance, weight, color,
  85. choose_param(get_param(params, vertex_predecessor), p_map),
  86. vis,
  87. choose_param(get_param(params, distance_compare_t()), std::less<D>()),
  88. choose_param(get_param(params, distance_combine_t()), closed_plus<D>(inf)),
  89. inf,
  90. choose_param(get_param(params, distance_zero_t()),
  91. D()));
  92. }
  93. // Handle DistanceMap and ColorMap defaults
  94. template <class VertexListGraph, class DijkstraVisitor,
  95. class DistanceMap, class WeightMap, class ColorMap,
  96. class IndexMap, class Params>
  97. inline void
  98. dag_sp_dispatch1
  99. (const VertexListGraph& g,
  100. typename graph_traits<VertexListGraph>::vertex_descriptor s,
  101. DistanceMap distance, WeightMap weight, ColorMap color, IndexMap id,
  102. DijkstraVisitor vis, const Params& params)
  103. {
  104. typedef typename property_traits<WeightMap>::value_type T;
  105. typename std::vector<T>::size_type n;
  106. n = is_default_param(distance) ? num_vertices(g) : 1;
  107. std::vector<T> distance_map(n);
  108. n = is_default_param(color) ? num_vertices(g) : 1;
  109. std::vector<default_color_type> color_map(n);
  110. dag_sp_dispatch2
  111. (g, s,
  112. choose_param(distance,
  113. make_iterator_property_map(distance_map.begin(), id,
  114. distance_map[0])),
  115. weight,
  116. choose_param(color,
  117. make_iterator_property_map(color_map.begin(), id,
  118. color_map[0])),
  119. id, vis, params);
  120. }
  121. } // namespace detail
  122. template <class VertexListGraph, class Param, class Tag, class Rest>
  123. inline void
  124. dag_shortest_paths
  125. (const VertexListGraph& g,
  126. typename graph_traits<VertexListGraph>::vertex_descriptor s,
  127. const bgl_named_params<Param,Tag,Rest>& params)
  128. {
  129. // assert that the graph is directed...
  130. null_visitor null_vis;
  131. detail::dag_sp_dispatch1
  132. (g, s,
  133. get_param(params, vertex_distance),
  134. choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
  135. get_param(params, vertex_color),
  136. choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
  137. choose_param(get_param(params, graph_visitor),
  138. make_dijkstra_visitor(null_vis)),
  139. params);
  140. }
  141. } // namespace boost
  142. #endif // BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP