dag_shortest_paths.hpp 5.9 KB

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