dijkstra_shortest_paths.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. //
  11. // Revision History:
  12. // 04 April 2001: Added named parameter variant. (Jeremy Siek)
  13. // 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock)
  14. //
  15. #ifndef BOOST_GRAPH_DIJKSTRA_HPP
  16. #define BOOST_GRAPH_DIJKSTRA_HPP
  17. #include <functional>
  18. #include <boost/limits.hpp>
  19. #include <boost/graph/named_function_params.hpp>
  20. #include <boost/graph/breadth_first_search.hpp>
  21. #include <boost/graph/relax.hpp>
  22. #include <boost/pending/indirect_cmp.hpp>
  23. #include <boost/graph/exception.hpp>
  24. #include <boost/graph/overloading.hpp>
  25. #include <boost/smart_ptr.hpp>
  26. #include <boost/graph/detail/d_ary_heap.hpp>
  27. #include <boost/graph/two_bit_color_map.hpp>
  28. #include <boost/graph/detail/mpi_include.hpp>
  29. #include <boost/property_map/property_map.hpp>
  30. #include <boost/property_map/vector_property_map.hpp>
  31. #include <boost/type_traits.hpp>
  32. #include <boost/concept/assert.hpp>
  33. #ifdef BOOST_GRAPH_DIJKSTRA_TESTING
  34. #include <boost/pending/mutable_queue.hpp>
  35. #endif // BOOST_GRAPH_DIJKSTRA_TESTING
  36. namespace boost
  37. {
  38. /**
  39. * @brief Updates a particular value in a queue used by Dijkstra's
  40. * algorithm.
  41. *
  42. * This routine is called by Dijkstra's algorithm after it has
  43. * decreased the distance from the source vertex to the given @p
  44. * vertex. By default, this routine will just call @c
  45. * Q.update(vertex). However, other queues may provide more
  46. * specialized versions of this routine.
  47. *
  48. * @param Q the queue that will be updated.
  49. * @param vertex the vertex whose distance has been updated
  50. * @param old_distance the previous distance to @p vertex
  51. */
  52. template < typename Buffer, typename Vertex, typename DistanceType >
  53. inline void dijkstra_queue_update(
  54. Buffer& Q, Vertex vertex, DistanceType old_distance)
  55. {
  56. (void)old_distance;
  57. Q.update(vertex);
  58. }
  59. template < class Visitor, class Graph > struct DijkstraVisitorConcept
  60. {
  61. void constraints()
  62. {
  63. BOOST_CONCEPT_ASSERT((CopyConstructibleConcept< Visitor >));
  64. vis.initialize_vertex(u, g);
  65. vis.discover_vertex(u, g);
  66. vis.examine_vertex(u, g);
  67. vis.examine_edge(e, g);
  68. vis.edge_relaxed(e, g);
  69. vis.edge_not_relaxed(e, g);
  70. vis.finish_vertex(u, g);
  71. }
  72. Visitor vis;
  73. Graph g;
  74. typename graph_traits< Graph >::vertex_descriptor u;
  75. typename graph_traits< Graph >::edge_descriptor e;
  76. };
  77. template < class Visitors = null_visitor >
  78. class dijkstra_visitor : public bfs_visitor< Visitors >
  79. {
  80. public:
  81. dijkstra_visitor() {}
  82. dijkstra_visitor(Visitors vis) : bfs_visitor< Visitors >(vis) {}
  83. template < class Edge, class Graph > void edge_relaxed(Edge e, Graph& g)
  84. {
  85. invoke_visitors(this->m_vis, e, g, on_edge_relaxed());
  86. }
  87. template < class Edge, class Graph > void edge_not_relaxed(Edge e, Graph& g)
  88. {
  89. invoke_visitors(this->m_vis, e, g, on_edge_not_relaxed());
  90. }
  91. private:
  92. template < class Edge, class Graph > void tree_edge(Edge u, Graph& g) {}
  93. };
  94. template < class Visitors >
  95. dijkstra_visitor< Visitors > make_dijkstra_visitor(Visitors vis)
  96. {
  97. return dijkstra_visitor< Visitors >(vis);
  98. }
  99. typedef dijkstra_visitor<> default_dijkstra_visitor;
  100. namespace detail
  101. {
  102. template < class UniformCostVisitor, class UpdatableQueue, class WeightMap,
  103. class PredecessorMap, class DistanceMap, class BinaryFunction,
  104. class BinaryPredicate >
  105. struct dijkstra_bfs_visitor
  106. {
  107. typedef typename property_traits< DistanceMap >::value_type D;
  108. typedef typename property_traits< WeightMap >::value_type W;
  109. dijkstra_bfs_visitor(UniformCostVisitor vis, UpdatableQueue& Q,
  110. WeightMap w, PredecessorMap p, DistanceMap d,
  111. BinaryFunction combine, BinaryPredicate compare, D zero)
  112. : m_vis(vis)
  113. , m_Q(Q)
  114. , m_weight(w)
  115. , m_predecessor(p)
  116. , m_distance(d)
  117. , m_combine(combine)
  118. , m_compare(compare)
  119. , m_zero(zero)
  120. {
  121. }
  122. template < class Edge, class Graph > void tree_edge(Edge e, Graph& g)
  123. {
  124. bool decreased = relax_target(e, g, m_weight, m_predecessor,
  125. m_distance, m_combine, m_compare);
  126. if (decreased)
  127. m_vis.edge_relaxed(e, g);
  128. else
  129. m_vis.edge_not_relaxed(e, g);
  130. }
  131. template < class Edge, class Graph > void gray_target(Edge e, Graph& g)
  132. {
  133. D old_distance = get(m_distance, target(e, g));
  134. bool decreased = relax_target(e, g, m_weight, m_predecessor,
  135. m_distance, m_combine, m_compare);
  136. if (decreased)
  137. {
  138. dijkstra_queue_update(m_Q, target(e, g), old_distance);
  139. m_vis.edge_relaxed(e, g);
  140. }
  141. else
  142. m_vis.edge_not_relaxed(e, g);
  143. }
  144. template < class Vertex, class Graph >
  145. void initialize_vertex(Vertex u, Graph& g)
  146. {
  147. m_vis.initialize_vertex(u, g);
  148. }
  149. template < class Edge, class Graph > void non_tree_edge(Edge, Graph&) {}
  150. template < class Vertex, class Graph >
  151. void discover_vertex(Vertex u, Graph& g)
  152. {
  153. m_vis.discover_vertex(u, g);
  154. }
  155. template < class Vertex, class Graph >
  156. void examine_vertex(Vertex u, Graph& g)
  157. {
  158. m_vis.examine_vertex(u, g);
  159. }
  160. template < class Edge, class Graph > void examine_edge(Edge e, Graph& g)
  161. {
  162. // Test for negative-weight edges:
  163. //
  164. // Reasons that other comparisons do not work:
  165. //
  166. // m_compare(e_weight, D(0)):
  167. // m_compare only needs to work on distances, not weights, and
  168. // those types do not need to be the same (bug 8398,
  169. // https://svn.boost.org/trac/boost/ticket/8398).
  170. // m_compare(m_combine(source_dist, e_weight), source_dist):
  171. // if m_combine is project2nd (as in prim_minimum_spanning_tree),
  172. // this test will claim that the edge weight is negative whenever
  173. // the edge weight is less than source_dist, even if both of
  174. // those are positive (bug 9012,
  175. // https://svn.boost.org/trac/boost/ticket/9012).
  176. // m_compare(m_combine(e_weight, source_dist), source_dist):
  177. // would fix project2nd issue, but documentation only requires
  178. // that m_combine be able to take a distance and a weight (in
  179. // that order) and return a distance.
  180. // W e_weight = get(m_weight, e);
  181. // sd_plus_ew = source_dist + e_weight.
  182. // D sd_plus_ew = m_combine(source_dist, e_weight);
  183. // sd_plus_2ew = source_dist + 2 * e_weight.
  184. // D sd_plus_2ew = m_combine(sd_plus_ew, e_weight);
  185. // The test here is equivalent to e_weight < 0 if m_combine has a
  186. // cancellation law, but always returns false when m_combine is a
  187. // projection operator.
  188. if (m_compare(m_combine(m_zero, get(m_weight, e)), m_zero))
  189. boost::throw_exception(negative_edge());
  190. // End of test for negative-weight edges.
  191. m_vis.examine_edge(e, g);
  192. }
  193. template < class Edge, class Graph > void black_target(Edge, Graph&) {}
  194. template < class Vertex, class Graph >
  195. void finish_vertex(Vertex u, Graph& g)
  196. {
  197. m_vis.finish_vertex(u, g);
  198. }
  199. UniformCostVisitor m_vis;
  200. UpdatableQueue& m_Q;
  201. WeightMap m_weight;
  202. PredecessorMap m_predecessor;
  203. DistanceMap m_distance;
  204. BinaryFunction m_combine;
  205. BinaryPredicate m_compare;
  206. D m_zero;
  207. };
  208. } // namespace detail
  209. namespace detail
  210. {
  211. template < class Graph, class IndexMap, class Value, bool KnownNumVertices >
  212. struct vertex_property_map_generator_helper
  213. {
  214. };
  215. template < class Graph, class IndexMap, class Value >
  216. struct vertex_property_map_generator_helper< Graph, IndexMap, Value, true >
  217. {
  218. typedef boost::iterator_property_map< Value*, IndexMap > type;
  219. static type build(const Graph& g, const IndexMap& index,
  220. boost::scoped_array< Value >& array_holder)
  221. {
  222. array_holder.reset(new Value[num_vertices(g)]);
  223. std::fill(array_holder.get(), array_holder.get() + num_vertices(g),
  224. Value());
  225. return make_iterator_property_map(array_holder.get(), index);
  226. }
  227. };
  228. template < class Graph, class IndexMap, class Value >
  229. struct vertex_property_map_generator_helper< Graph, IndexMap, Value, false >
  230. {
  231. typedef boost::vector_property_map< Value, IndexMap > type;
  232. static type build(const Graph& g, const IndexMap& index,
  233. boost::scoped_array< Value >& array_holder)
  234. {
  235. return boost::make_vector_property_map< Value >(index);
  236. }
  237. };
  238. template < class Graph, class IndexMap, class Value >
  239. struct vertex_property_map_generator
  240. {
  241. typedef boost::is_base_and_derived< boost::vertex_list_graph_tag,
  242. typename boost::graph_traits< Graph >::traversal_category >
  243. known_num_vertices;
  244. typedef vertex_property_map_generator_helper< Graph, IndexMap, Value,
  245. known_num_vertices::value >
  246. helper;
  247. typedef typename helper::type type;
  248. static type build(const Graph& g, const IndexMap& index,
  249. boost::scoped_array< Value >& array_holder)
  250. {
  251. return helper::build(g, index, array_holder);
  252. }
  253. };
  254. }
  255. namespace detail
  256. {
  257. template < class Graph, class IndexMap, bool KnownNumVertices >
  258. struct default_color_map_generator_helper
  259. {
  260. };
  261. template < class Graph, class IndexMap >
  262. struct default_color_map_generator_helper< Graph, IndexMap, true >
  263. {
  264. typedef boost::two_bit_color_map< IndexMap > type;
  265. static type build(const Graph& g, const IndexMap& index)
  266. {
  267. size_t nv = num_vertices(g);
  268. return boost::two_bit_color_map< IndexMap >(nv, index);
  269. }
  270. };
  271. template < class Graph, class IndexMap >
  272. struct default_color_map_generator_helper< Graph, IndexMap, false >
  273. {
  274. typedef boost::vector_property_map< boost::two_bit_color_type,
  275. IndexMap >
  276. type;
  277. static type build(const Graph& g, const IndexMap& index)
  278. {
  279. return boost::make_vector_property_map< boost::two_bit_color_type >(
  280. index);
  281. }
  282. };
  283. template < class Graph, class IndexMap > struct default_color_map_generator
  284. {
  285. typedef boost::is_base_and_derived< boost::vertex_list_graph_tag,
  286. typename boost::graph_traits< Graph >::traversal_category >
  287. known_num_vertices;
  288. typedef default_color_map_generator_helper< Graph, IndexMap,
  289. known_num_vertices::value >
  290. helper;
  291. typedef typename helper::type type;
  292. static type build(const Graph& g, const IndexMap& index)
  293. {
  294. return helper::build(g, index);
  295. }
  296. };
  297. }
  298. // Call breadth first search with default color map.
  299. template < class Graph, class SourceInputIter, class DijkstraVisitor,
  300. class PredecessorMap, class DistanceMap, class WeightMap, class IndexMap,
  301. class Compare, class Combine, class DistZero >
  302. inline void dijkstra_shortest_paths_no_init(const Graph& g,
  303. SourceInputIter s_begin, SourceInputIter s_end, PredecessorMap predecessor,
  304. DistanceMap distance, WeightMap weight, IndexMap index_map, Compare compare,
  305. Combine combine, DistZero zero, DijkstraVisitor vis)
  306. {
  307. typedef detail::default_color_map_generator< Graph, IndexMap >
  308. ColorMapHelper;
  309. typedef typename ColorMapHelper::type ColorMap;
  310. ColorMap color = ColorMapHelper::build(g, index_map);
  311. dijkstra_shortest_paths_no_init(g, s_begin, s_end, predecessor, distance,
  312. weight, index_map, compare, combine, zero, vis, color);
  313. }
  314. // Call breadth first search with default color map.
  315. template < class Graph, class DijkstraVisitor, class PredecessorMap,
  316. class DistanceMap, class WeightMap, class IndexMap, class Compare,
  317. class Combine, class DistZero >
  318. inline void dijkstra_shortest_paths_no_init(const Graph& g,
  319. typename graph_traits< Graph >::vertex_descriptor s,
  320. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  321. IndexMap index_map, Compare compare, Combine combine, DistZero zero,
  322. DijkstraVisitor vis)
  323. {
  324. dijkstra_shortest_paths_no_init(g, &s, &s + 1, predecessor, distance,
  325. weight, index_map, compare, combine, zero, vis);
  326. }
  327. // Call breadth first search
  328. template < class Graph, class SourceInputIter, class DijkstraVisitor,
  329. class PredecessorMap, class DistanceMap, class WeightMap, class IndexMap,
  330. class Compare, class Combine, class DistZero, class ColorMap >
  331. inline void dijkstra_shortest_paths_no_init(const Graph& g,
  332. SourceInputIter s_begin, SourceInputIter s_end, PredecessorMap predecessor,
  333. DistanceMap distance, WeightMap weight, IndexMap index_map, Compare compare,
  334. Combine combine, DistZero zero, DijkstraVisitor vis, ColorMap color)
  335. {
  336. typedef indirect_cmp< DistanceMap, Compare > IndirectCmp;
  337. IndirectCmp icmp(distance, compare);
  338. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  339. // Now the default: use a d-ary heap
  340. boost::scoped_array< std::size_t > index_in_heap_map_holder;
  341. typedef detail::vertex_property_map_generator< Graph, IndexMap,
  342. std::size_t >
  343. IndexInHeapMapHelper;
  344. typedef typename IndexInHeapMapHelper::type IndexInHeapMap;
  345. IndexInHeapMap index_in_heap
  346. = IndexInHeapMapHelper::build(g, index_map, index_in_heap_map_holder);
  347. typedef d_ary_heap_indirect< Vertex, 4, IndexInHeapMap, DistanceMap,
  348. Compare >
  349. MutableQueue;
  350. MutableQueue Q(distance, index_in_heap, compare);
  351. detail::dijkstra_bfs_visitor< DijkstraVisitor, MutableQueue, WeightMap,
  352. PredecessorMap, DistanceMap, Combine, Compare >
  353. bfs_vis(vis, Q, weight, predecessor, distance, combine, compare, zero);
  354. breadth_first_visit(g, s_begin, s_end, Q, bfs_vis, color);
  355. }
  356. // Call breadth first search
  357. template < class Graph, class DijkstraVisitor, class PredecessorMap,
  358. class DistanceMap, class WeightMap, class IndexMap, class Compare,
  359. class Combine, class DistZero, class ColorMap >
  360. inline void dijkstra_shortest_paths_no_init(const Graph& g,
  361. typename graph_traits< Graph >::vertex_descriptor s,
  362. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  363. IndexMap index_map, Compare compare, Combine combine, DistZero zero,
  364. DijkstraVisitor vis, ColorMap color)
  365. {
  366. dijkstra_shortest_paths_no_init(g, &s, &s + 1, predecessor, distance,
  367. weight, index_map, compare, combine, zero, vis, color);
  368. }
  369. // Initialize distances and call breadth first search with default color map
  370. template < class VertexListGraph, class SourceInputIter, class DijkstraVisitor,
  371. class PredecessorMap, class DistanceMap, class WeightMap, class IndexMap,
  372. class Compare, class Combine, class DistInf, class DistZero, typename T,
  373. typename Tag, typename Base >
  374. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  375. SourceInputIter s_begin, SourceInputIter s_end, PredecessorMap predecessor,
  376. DistanceMap distance, WeightMap weight, IndexMap index_map, Compare compare,
  377. Combine combine, DistInf inf, DistZero zero, DijkstraVisitor vis,
  378. const bgl_named_params< T, Tag, Base >& BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  379. VertexListGraph, vertex_list_graph_tag))
  380. {
  381. boost::two_bit_color_map< IndexMap > color(num_vertices(g), index_map);
  382. dijkstra_shortest_paths(g, s_begin, s_end, predecessor, distance, weight,
  383. index_map, compare, combine, inf, zero, vis, color);
  384. }
  385. // Initialize distances and call breadth first search with default color map
  386. template < class VertexListGraph, class DijkstraVisitor, class PredecessorMap,
  387. class DistanceMap, class WeightMap, class IndexMap, class Compare,
  388. class Combine, class DistInf, class DistZero, typename T, typename Tag,
  389. typename Base >
  390. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  391. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  392. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  393. IndexMap index_map, Compare compare, Combine combine, DistInf inf,
  394. DistZero zero, DijkstraVisitor vis,
  395. const bgl_named_params< T, Tag, Base >& BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  396. VertexListGraph, vertex_list_graph_tag))
  397. {
  398. dijkstra_shortest_paths(g, &s, &s + 1, predecessor, distance, weight,
  399. index_map, compare, combine, inf, zero, vis);
  400. }
  401. // Initialize distances and call breadth first search
  402. template < class VertexListGraph, class SourceInputIter, class DijkstraVisitor,
  403. class PredecessorMap, class DistanceMap, class WeightMap, class IndexMap,
  404. class Compare, class Combine, class DistInf, class DistZero,
  405. class ColorMap >
  406. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  407. SourceInputIter s_begin, SourceInputIter s_end, PredecessorMap predecessor,
  408. DistanceMap distance, WeightMap weight, IndexMap index_map, Compare compare,
  409. Combine combine, DistInf inf, DistZero zero, DijkstraVisitor vis,
  410. ColorMap color)
  411. {
  412. typedef typename property_traits< ColorMap >::value_type ColorValue;
  413. typedef color_traits< ColorValue > Color;
  414. typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
  415. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
  416. {
  417. vis.initialize_vertex(*ui, g);
  418. put(distance, *ui, inf);
  419. put(predecessor, *ui, *ui);
  420. put(color, *ui, Color::white());
  421. }
  422. for (SourceInputIter it = s_begin; it != s_end; ++it)
  423. {
  424. put(distance, *it, zero);
  425. }
  426. dijkstra_shortest_paths_no_init(g, s_begin, s_end, predecessor, distance,
  427. weight, index_map, compare, combine, zero, vis, color);
  428. }
  429. // Initialize distances and call breadth first search
  430. template < class VertexListGraph, class DijkstraVisitor, class PredecessorMap,
  431. class DistanceMap, class WeightMap, class IndexMap, class Compare,
  432. class Combine, class DistInf, class DistZero, class ColorMap >
  433. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  434. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  435. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  436. IndexMap index_map, Compare compare, Combine combine, DistInf inf,
  437. DistZero zero, DijkstraVisitor vis, ColorMap color)
  438. {
  439. dijkstra_shortest_paths(g, &s, &s + 1, predecessor, distance, weight,
  440. index_map, compare, combine, inf, zero, vis, color);
  441. }
  442. // Initialize distances and call breadth first search
  443. template < class VertexListGraph, class SourceInputIter, class DijkstraVisitor,
  444. class PredecessorMap, class DistanceMap, class WeightMap, class IndexMap,
  445. class Compare, class Combine, class DistInf, class DistZero >
  446. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  447. SourceInputIter s_begin, SourceInputIter s_end, PredecessorMap predecessor,
  448. DistanceMap distance, WeightMap weight, IndexMap index_map, Compare compare,
  449. Combine combine, DistInf inf, DistZero zero, DijkstraVisitor vis)
  450. {
  451. dijkstra_shortest_paths(g, s_begin, s_end, predecessor, distance, weight,
  452. index_map, compare, combine, inf, zero, vis, no_named_parameters());
  453. }
  454. // Initialize distances and call breadth first search
  455. template < class VertexListGraph, class DijkstraVisitor, class PredecessorMap,
  456. class DistanceMap, class WeightMap, class IndexMap, class Compare,
  457. class Combine, class DistInf, class DistZero >
  458. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  459. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  460. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  461. IndexMap index_map, Compare compare, Combine combine, DistInf inf,
  462. DistZero zero, DijkstraVisitor vis)
  463. {
  464. dijkstra_shortest_paths(g, &s, &s + 1, predecessor, distance, weight,
  465. index_map, compare, combine, inf, zero, vis);
  466. }
  467. namespace detail
  468. {
  469. // Handle defaults for PredecessorMap and
  470. // Distance Compare, Combine, Inf and Zero
  471. template < class VertexListGraph, class DistanceMap, class WeightMap,
  472. class IndexMap, class Params >
  473. inline void dijkstra_dispatch2(const VertexListGraph& g,
  474. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  475. DistanceMap distance, WeightMap weight, IndexMap index_map,
  476. const Params& params)
  477. {
  478. // Default for predecessor map
  479. dummy_property_map p_map;
  480. typedef typename property_traits< DistanceMap >::value_type D;
  481. D inf = choose_param(get_param(params, distance_inf_t()),
  482. (std::numeric_limits< D >::max)());
  483. dijkstra_shortest_paths(g, s,
  484. choose_param(get_param(params, vertex_predecessor), p_map),
  485. distance, weight, index_map,
  486. choose_param(
  487. get_param(params, distance_compare_t()), std::less< D >()),
  488. choose_param(
  489. get_param(params, distance_combine_t()), std::plus< D >()),
  490. inf, choose_param(get_param(params, distance_zero_t()), D()),
  491. choose_param(get_param(params, graph_visitor),
  492. make_dijkstra_visitor(null_visitor())),
  493. params);
  494. }
  495. template < class VertexListGraph, class DistanceMap, class WeightMap,
  496. class IndexMap, class Params >
  497. inline void dijkstra_dispatch1(const VertexListGraph& g,
  498. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  499. DistanceMap distance, WeightMap weight, IndexMap index_map,
  500. const Params& params)
  501. {
  502. // Default for distance map
  503. typedef typename property_traits< WeightMap >::value_type D;
  504. typename std::vector< D >::size_type n
  505. = is_default_param(distance) ? num_vertices(g) : 1;
  506. std::vector< D > distance_map(n);
  507. detail::dijkstra_dispatch2(g, s,
  508. choose_param(distance,
  509. make_iterator_property_map(
  510. distance_map.begin(), index_map, distance_map[0])),
  511. weight, index_map, params);
  512. }
  513. } // namespace detail
  514. // Named Parameter Variant
  515. template < class VertexListGraph, class Param, class Tag, class Rest >
  516. inline void dijkstra_shortest_paths(const VertexListGraph& g,
  517. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  518. const bgl_named_params< Param, Tag, Rest >& params)
  519. {
  520. // Default for edge weight and vertex index map is to ask for them
  521. // from the graph. Default for the visitor is null_visitor.
  522. detail::dijkstra_dispatch1(g, s, get_param(params, vertex_distance),
  523. choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
  524. choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
  525. params);
  526. }
  527. } // namespace boost
  528. #include BOOST_GRAPH_MPI_INCLUDE(< boost / graph / distributed / dijkstra_shortest_paths.hpp >)
  529. #endif // BOOST_GRAPH_DIJKSTRA_HPP