dijkstra_shortest_paths.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. .. Copyright (C) 2004-2008 The Trustees of Indiana University.
  2. Use, modification and distribution is subject to the Boost Software
  3. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================
  6. |Logo| Dijkstra's Single-Source Shortest Paths
  7. ==============================================
  8. ::
  9. // named parameter version
  10. template <typename Graph, typename P, typename T, typename R>
  11. void
  12. dijkstra_shortest_paths(Graph& g,
  13. typename graph_traits<Graph>::vertex_descriptor s,
  14. const bgl_named_params<P, T, R>& params);
  15. // non-named parameter version
  16. template <typename Graph, typename DijkstraVisitor,
  17. typename PredecessorMap, typename DistanceMap,
  18. typename WeightMap, typename VertexIndexMap, typename CompareFunction, typename CombineFunction,
  19. typename DistInf, typename DistZero>
  20. void dijkstra_shortest_paths
  21. (const Graph& g,
  22. typename graph_traits<Graph>::vertex_descriptor s,
  23. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  24. VertexIndexMap index_map,
  25. CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,
  26. DijkstraVisitor vis);
  27. The ``dijkstra_shortest_paths()`` function solves the single-source
  28. shortest paths problem on a weighted, undirected or directed
  29. distributed graph. There are two implementations of distributed
  30. Dijkstra's algorithm, which offer different performance
  31. tradeoffs. Both are accessible via the ``dijkstra_shortest_paths()``
  32. function (for compatibility with sequential BGL programs). The
  33. distributed Dijkstra algorithms are very similar to their sequential
  34. counterparts. Only the differences are highlighted here; please refer
  35. to the `sequential Dijkstra implementation`_ for additional
  36. details. The best-performing implementation for most cases is the
  37. `Delta-Stepping algorithm`_; however, one can also employ the more
  38. conservative `Crauser et al.'s algorithm`_ or the simlistic `Eager
  39. Dijkstra's algorithm`_.
  40. .. contents::
  41. Where Defined
  42. -------------
  43. <``boost/graph/dijkstra_shortest_paths.hpp``>
  44. Parameters
  45. ----------
  46. All parameters of the `sequential Dijkstra implementation`_ are
  47. supported and have essentially the same meaning. The distributed
  48. Dijkstra implementations introduce a new parameter that allows one to
  49. select `Eager Dijkstra's algorithm`_ and control the amount of work it
  50. performs. Only differences and new parameters are documented here.
  51. IN: ``Graph& g``
  52. The graph type must be a model of `Distributed Graph`_.
  53. IN: ``vertex_descriptor s``
  54. The start vertex must be the same in every process.
  55. OUT: ``predecessor_map(PredecessorMap p_map)``
  56. The predecessor map must be a `Distributed Property Map`_ or
  57. ``dummy_property_map``, although only the local portions of the map
  58. will be written.
  59. **Default:** ``dummy_property_map``
  60. UTIL/OUT: ``distance_map(DistanceMap d_map)``
  61. The distance map must be either a `Distributed Property Map`_ or
  62. ``dummy_property_map``. It will be given the ``vertex_distance``
  63. role.
  64. IN: ``visitor(DijkstraVisitor vis)``
  65. The visitor must be a distributed Dijkstra visitor. The suble differences
  66. between sequential and distributed Dijkstra visitors are discussed in the
  67. section `Visitor Event Points`_.
  68. UTIL/OUT: ``color_map(ColorMap color)``
  69. The color map must be a `Distributed Property Map`_ with the same
  70. process group as the graph ``g`` whose colors must monotonically
  71. darken (white -> gray -> black). The default value is a distributed
  72. ``iterator_property_map`` created from a ``std::vector`` of
  73. ``default_color_type``.
  74. IN: ``lookahead(distance_type look)``
  75. When this parameter is supplied, the implementation will use the
  76. `Eager Dijkstra's algorithm`_ with the given lookahead value.
  77. Lookahead permits distributed Dijkstra's algorithm to speculatively
  78. process vertices whose shortest distance from the source may not
  79. have been found yet. When the distance found is the shortest
  80. distance, parallelism is improved and the algorithm may terminate
  81. more quickly. However, if the distance is not the shortest distance,
  82. the vertex will need to be reprocessed later, resulting in more
  83. work.
  84. The type ``distance_type`` is the value type of the ``DistanceMap``
  85. property map. It is a nonnegative value specifying how far ahead
  86. Dijkstra's algorithm may process values.
  87. **Default:** no value (lookahead is not employed; uses `Crauser et
  88. al.'s algorithm`_).
  89. Visitor Event Points
  90. --------------------
  91. The `Dijkstra Visitor`_ concept defines 7 event points that will be
  92. triggered by the `sequential Dijkstra implementation`_. The distributed
  93. Dijkstra retains these event points, but the sequence of events
  94. triggered and the process in which each event occurs will change
  95. depending on the distribution of the graph, lookahead, and edge
  96. weights.
  97. ``initialize_vertex(s, g)``
  98. This will be invoked by every process for each local vertex.
  99. ``discover_vertex(u, g)``
  100. This will be invoked each type a process discovers a new vertex
  101. ``u``. Due to incomplete information in distributed property maps,
  102. this event may be triggered many times for the same vertex ``u``.
  103. ``examine_vertex(u, g)``
  104. This will be invoked by the process owning the vertex ``u``. This
  105. event may be invoked multiple times for the same vertex when the
  106. graph contains negative edges or lookahead is employed.
  107. ``examine_edge(e, g)``
  108. This will be invoked by the process owning the source vertex of
  109. ``e``. As with ``examine_vertex``, this event may be invoked
  110. multiple times for the same edge.
  111. ``edge_relaxed(e, g)``
  112. Similar to ``examine_edge``, this will be invoked by the process
  113. owning the source vertex and may be invoked multiple times (even
  114. without lookahead or negative edges).
  115. ``edge_not_relaxed(e, g)``
  116. Similar to ``edge_relaxed``. Some ``edge_not_relaxed`` events that
  117. would be triggered by sequential Dijkstra's will become
  118. ``edge_relaxed`` events in distributed Dijkstra's algorithm.
  119. ``finish_vertex(e, g)``
  120. See documentation for ``examine_vertex``. Note that a "finished"
  121. vertex is not necessarily finished if lookahead is permitted or
  122. negative edges exist in the graph.
  123. Crauser et al.'s algorithm
  124. --------------------------
  125. ::
  126. namespace graph {
  127. template<typename DistributedGraph, typename DijkstraVisitor,
  128. typename PredecessorMap, typename DistanceMap, typename WeightMap,
  129. typename IndexMap, typename ColorMap, typename Compare,
  130. typename Combine, typename DistInf, typename DistZero>
  131. void
  132. crauser_et_al_shortest_paths
  133. (const DistributedGraph& g,
  134. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  135. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  136. IndexMap index_map, ColorMap color_map,
  137. Compare compare, Combine combine, DistInf inf, DistZero zero,
  138. DijkstraVisitor vis);
  139. template<typename DistributedGraph, typename DijkstraVisitor,
  140. typename PredecessorMap, typename DistanceMap, typename WeightMap>
  141. void
  142. crauser_et_al_shortest_paths
  143. (const DistributedGraph& g,
  144. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  145. PredecessorMap predecessor, DistanceMap distance, WeightMap weight);
  146. template<typename DistributedGraph, typename DijkstraVisitor,
  147. typename PredecessorMap, typename DistanceMap>
  148. void
  149. crauser_et_al_shortest_paths
  150. (const DistributedGraph& g,
  151. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  152. PredecessorMap predecessor, DistanceMap distance);
  153. }
  154. The formulation of Dijkstra's algorithm by Crauser, Mehlhorn, Meyer,
  155. and Sanders [CMMS98a]_ improves the scalability of parallel Dijkstra's
  156. algorithm by increasing the number of vertices that can be processed
  157. in a given superstep. This algorithm adapts well to various graph
  158. types, and is a simple algorithm to use, requiring no additional user
  159. input to achieve reasonable performance. The disadvantage of this
  160. algorithm is that the implementation is required to manage three
  161. priority queues, which creates a large amount of work at each node.
  162. This algorithm is used by default in distributed
  163. ``dijkstra_shortest_paths()``.
  164. Where Defined
  165. ~~~~~~~~~~~~~
  166. <``boost/graph/distributed/crauser_et_al_shortest_paths.hpp``>
  167. Complexity
  168. ~~~~~~~~~~
  169. This algorithm performs *O(V log V)* work in *d + 1* BSP supersteps,
  170. where *d* is at most *O(V)* but is generally much smaller. On directed
  171. Erdos-Renyi graphs with edge weights in [0, 1), the expected number of
  172. supersteps *d* is *O(n^(1/3))* with high probability.
  173. Performance
  174. ~~~~~~~~~~~
  175. The following charts illustrate the performance of the Parallel BGL implementation of Crauser et al.'s
  176. algorithm on graphs with edge weights uniformly selected from the
  177. range *[0, 1)*.
  178. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeSparse&columns=4
  179. :align: left
  180. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeSparse&columns=4&speedup=1
  181. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeDense&columns=4
  182. :align: left
  183. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeDense&columns=4&speedup=1
  184. Eager Dijkstra's algorithm
  185. --------------------------
  186. ::
  187. namespace graph {
  188. template<typename DistributedGraph, typename DijkstraVisitor,
  189. typename PredecessorMap, typename DistanceMap, typename WeightMap,
  190. typename IndexMap, typename ColorMap, typename Compare,
  191. typename Combine, typename DistInf, typename DistZero>
  192. void
  193. eager_dijkstra_shortest_paths
  194. (const DistributedGraph& g,
  195. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  196. PredecessorMap predecessor, DistanceMap distance,
  197. typename property_traits<DistanceMap>::value_type lookahead,
  198. WeightMap weight, IndexMap index_map, ColorMap color_map,
  199. Compare compare, Combine combine, DistInf inf, DistZero zero,
  200. DijkstraVisitor vis);
  201. template<typename DistributedGraph, typename DijkstraVisitor,
  202. typename PredecessorMap, typename DistanceMap, typename WeightMap>
  203. void
  204. eager_dijkstra_shortest_paths
  205. (const DistributedGraph& g,
  206. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  207. PredecessorMap predecessor, DistanceMap distance,
  208. typename property_traits<DistanceMap>::value_type lookahead,
  209. WeightMap weight);
  210. template<typename DistributedGraph, typename DijkstraVisitor,
  211. typename PredecessorMap, typename DistanceMap>
  212. void
  213. eager_dijkstra_shortest_paths
  214. (const DistributedGraph& g,
  215. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  216. PredecessorMap predecessor, DistanceMap distance,
  217. typename property_traits<DistanceMap>::value_type lookahead);
  218. }
  219. In each superstep, parallel Dijkstra's algorithm typically only
  220. processes nodes whose distances equivalent to the global minimum
  221. distance, because these distances are guaranteed to be correct. This
  222. variation on the algorithm allows the algorithm to process all
  223. vertices whose distances are within some constant value of the
  224. minimum distance. The value is called the "lookahead" value and is
  225. provided by the user as the fifth parameter to the function. Small
  226. values of the lookahead parameter will likely result in limited
  227. parallelization opportunities, whereas large values will expose more
  228. parallelism but may introduce (non-infinite) looping and result in
  229. extra work. The optimal value for the lookahead parameter depends on
  230. the input graph; see [CMMS98b]_ and [MS98]_.
  231. This algorithm will be used by ``dijkstra_shortest_paths()`` when it
  232. is provided with a lookahead value.
  233. Where Defined
  234. ~~~~~~~~~~~~~
  235. <``boost/graph/distributed/eager_dijkstra_shortest_paths.hpp``>
  236. Complexity
  237. ~~~~~~~~~~
  238. This algorithm performs *O(V log V)* work in *d
  239. + 1* BSP supersteps, where *d* is at most *O(V)* but may be smaller
  240. depending on the lookahead value. the algorithm may perform more work
  241. when a large lookahead is provided, because vertices will be
  242. reprocessed.
  243. Performance
  244. ~~~~~~~~~~~
  245. The performance of the eager Dijkstra's algorithm varies greatly
  246. depending on the lookahead value. The following charts illustrate the
  247. performance of the Parallel BGL on graphs with edge weights uniformly
  248. selected from the range *[0, 1)* and a constant lookahead of 0.1.
  249. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeSparse&columns=5
  250. :align: left
  251. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeSparse&columns=5&speedup=1
  252. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeDense&columns=5
  253. :align: left
  254. .. image:: http://www.osl.iu.edu/research/pbgl/performance/chart.php?cluster=Odin&generator=ER,SF,SW&dataset=TimeDense&columns=5&speedup=1
  255. Delta-Stepping algorithm
  256. --------------------------
  257. ::
  258. namespace boost { namespace graph { namespace distributed {
  259. template <typename Graph, typename PredecessorMap,
  260. typename DistanceMap, typename WeightMap>
  261. void delta_stepping_shortest_paths
  262. (const Graph& g,
  263. typename graph_traits<Graph>::vertex_descriptor s,
  264. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  265. typename property_traits<WeightMap>::value_type delta)
  266. template <typename Graph, typename PredecessorMap,
  267. typename DistanceMap, typename WeightMap>
  268. void delta_stepping_shortest_paths
  269. (const Graph& g,
  270. typename graph_traits<Graph>::vertex_descriptor s,
  271. PredecessorMap predecessor, DistanceMap distance, WeightMap weight)
  272. }
  273. } } }
  274. The delta-stepping algorithm [MS98]_ is another variant of the parallel
  275. Dijkstra algorithm. Like the eager Dijkstra algorithm, it employs a
  276. lookahead (``delta``) value that allows processors to process vertices
  277. before we are guaranteed to find their minimum distances, permitting
  278. more parallelism than a conservative strategy. Delta-stepping also
  279. introduces a multi-level bucket data structure that provides more
  280. relaxed ordering constraints than the priority queues employed by the
  281. other Dijkstra variants, reducing the complexity of insertions,
  282. relaxations, and removals from the central data structure. The
  283. delta-stepping algorithm is the best-performing of the Dijkstra
  284. variants.
  285. The lookahead value ``delta`` determines how large each of the
  286. "buckets" within the delta-stepping queue will be, where the ith
  287. bucket contains edges within tentative distances between ``delta``*i
  288. and ``delta``*(i+1). ``delta`` must be a positive value. When omitted,
  289. ``delta`` will be set to the maximum edge weight divided by the
  290. maximum degree.
  291. Where Defined
  292. ~~~~~~~~~~~~~
  293. <``boost/graph/distributed/delta_stepping_shortest_paths.hpp``>
  294. Example
  295. -------
  296. See the separate `Dijkstra example`_.
  297. Bibliography
  298. ------------
  299. .. [CMMS98a] Andreas Crauser, Kurt Mehlhorn, Ulrich Meyer, and Peter Sanders. A
  300. Parallelization of Dijkstra's Shortest Path Algorithm. In
  301. *Mathematical Foundations of Computer Science (MFCS)*, volume 1450 of
  302. Lecture Notes in Computer Science, pages 722--731, 1998. Springer.
  303. .. [CMMS98b] Andreas Crauser, Kurt Mehlhorn, Ulrich Meyer, and Peter
  304. Sanders. Parallelizing Dijkstra's shortest path algorithm. Technical
  305. report, MPI-Informatik, 1998.
  306. .. [MS98] Ulrich Meyer and Peter Sanders. Delta-stepping: A parallel
  307. shortest path algorithm. In *6th ESA*, LNCS. Springer, 1998.
  308. -----------------------------------------------------------------------------
  309. Copyright (C) 2004, 2005, 2006, 2007, 2008 The Trustees of Indiana University.
  310. Authors: Douglas Gregor and Andrew Lumsdaine
  311. .. |Logo| image:: pbgl-logo.png
  312. :align: middle
  313. :alt: Parallel BGL
  314. :target: http://www.osl.iu.edu/research/pbgl
  315. .. _sequential Dijkstra implementation: http://www.boost.org/libs/graph/doc/dijkstra_shortest_paths.html
  316. .. _distributed breadth-first search: breadth_first_search.html
  317. .. _Distributed Graph: DistributedGraph.html
  318. .. _Distributed Property Map: distributed_property_map.html
  319. .. _Dijkstra Visitor: http://www.boost.org/libs/graph/doc/DijkstraVisitor.html
  320. .. _Dijkstra example: dijkstra_example.html