1 | /* -*- C++ -*- |
---|
2 | * lemon/johnson.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_JOHNSON_H |
---|
18 | #define LEMON_JOHNSON_H |
---|
19 | |
---|
20 | ///\ingroup flowalgs |
---|
21 | /// \file |
---|
22 | /// \brief Johnson algorithm. |
---|
23 | /// |
---|
24 | |
---|
25 | #include <lemon/list_graph.h> |
---|
26 | #include <lemon/graph_utils.h> |
---|
27 | #include <lemon/dijkstra.h> |
---|
28 | #include <lemon/bellman_ford.h> |
---|
29 | #include <lemon/invalid.h> |
---|
30 | #include <lemon/error.h> |
---|
31 | #include <lemon/maps.h> |
---|
32 | #include <lemon/matrix_maps.h> |
---|
33 | |
---|
34 | #include <limits> |
---|
35 | |
---|
36 | namespace lemon { |
---|
37 | |
---|
38 | /// \brief Default OperationTraits for the Johnson algorithm class. |
---|
39 | /// |
---|
40 | /// It defines all computational operations and constants which are |
---|
41 | /// used in the Floyd-Warshall algorithm. The default implementation |
---|
42 | /// is based on the numeric_limits class. If the numeric type does not |
---|
43 | /// have infinity value then the maximum value is used as extremal |
---|
44 | /// infinity value. |
---|
45 | template < |
---|
46 | typename Value, |
---|
47 | bool has_infinity = std::numeric_limits<Value>::has_infinity> |
---|
48 | struct JohnsonDefaultOperationTraits { |
---|
49 | /// \brief Gives back the zero value of the type. |
---|
50 | static Value zero() { |
---|
51 | return static_cast<Value>(0); |
---|
52 | } |
---|
53 | /// \brief Gives back the positive infinity value of the type. |
---|
54 | static Value infinity() { |
---|
55 | return std::numeric_limits<Value>::infinity(); |
---|
56 | } |
---|
57 | /// \brief Gives back the sum of the given two elements. |
---|
58 | static Value plus(const Value& left, const Value& right) { |
---|
59 | return left + right; |
---|
60 | } |
---|
61 | /// \brief Gives back true only if the first value less than the second. |
---|
62 | static bool less(const Value& left, const Value& right) { |
---|
63 | return left < right; |
---|
64 | } |
---|
65 | }; |
---|
66 | |
---|
67 | template <typename Value> |
---|
68 | struct JohnsonDefaultOperationTraits<Value, false> { |
---|
69 | static Value zero() { |
---|
70 | return static_cast<Value>(0); |
---|
71 | } |
---|
72 | static Value infinity() { |
---|
73 | return std::numeric_limits<Value>::max(); |
---|
74 | } |
---|
75 | static Value plus(const Value& left, const Value& right) { |
---|
76 | if (left == infinity() || right == infinity()) return infinity(); |
---|
77 | return left + right; |
---|
78 | } |
---|
79 | static bool less(const Value& left, const Value& right) { |
---|
80 | return left < right; |
---|
81 | } |
---|
82 | }; |
---|
83 | |
---|
84 | /// \brief Default traits class of Johnson class. |
---|
85 | /// |
---|
86 | /// Default traits class of Johnson class. |
---|
87 | /// \param _Graph Graph type. |
---|
88 | /// \param _LegthMap Type of length map. |
---|
89 | template<class _Graph, class _LengthMap> |
---|
90 | struct JohnsonDefaultTraits { |
---|
91 | /// The graph type the algorithm runs on. |
---|
92 | typedef _Graph Graph; |
---|
93 | |
---|
94 | /// \brief The type of the map that stores the edge lengths. |
---|
95 | /// |
---|
96 | /// The type of the map that stores the edge lengths. |
---|
97 | /// It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
98 | typedef _LengthMap LengthMap; |
---|
99 | |
---|
100 | // The type of the length of the edges. |
---|
101 | typedef typename _LengthMap::Value Value; |
---|
102 | |
---|
103 | /// \brief Operation traits for bellman-ford algorithm. |
---|
104 | /// |
---|
105 | /// It defines the infinity type on the given Value type |
---|
106 | /// and the used operation. |
---|
107 | /// \see JohnsonDefaultOperationTraits |
---|
108 | typedef JohnsonDefaultOperationTraits<Value> OperationTraits; |
---|
109 | |
---|
110 | /// The cross reference type used by heap. |
---|
111 | |
---|
112 | /// The cross reference type used by heap. |
---|
113 | /// Usually it is \c Graph::NodeMap<int>. |
---|
114 | typedef typename Graph::template NodeMap<int> HeapCrossRef; |
---|
115 | |
---|
116 | ///Instantiates a HeapCrossRef. |
---|
117 | |
---|
118 | ///This function instantiates a \ref HeapCrossRef. |
---|
119 | /// \param graph is the graph, to which we would like to define the |
---|
120 | /// HeapCrossRef. |
---|
121 | static HeapCrossRef *createHeapCrossRef(const Graph& graph) { |
---|
122 | return new HeapCrossRef(graph); |
---|
123 | } |
---|
124 | |
---|
125 | ///The heap type used by Dijkstra algorithm. |
---|
126 | |
---|
127 | ///The heap type used by Dijkstra algorithm. |
---|
128 | /// |
---|
129 | ///\sa BinHeap |
---|
130 | ///\sa Dijkstra |
---|
131 | typedef BinHeap<typename Graph::Node, typename LengthMap::Value, |
---|
132 | HeapCrossRef, std::less<Value> > Heap; |
---|
133 | |
---|
134 | ///Instantiates a Heap. |
---|
135 | |
---|
136 | ///This function instantiates a \ref Heap. |
---|
137 | /// \param crossRef The cross reference for the heap. |
---|
138 | static Heap *createHeap(HeapCrossRef& crossRef) { |
---|
139 | return new Heap(crossRef); |
---|
140 | } |
---|
141 | |
---|
142 | /// \brief The type of the matrix map that stores the last edges of the |
---|
143 | /// shortest paths. |
---|
144 | /// |
---|
145 | /// The type of the map that stores the last edges of the shortest paths. |
---|
146 | /// It must be a matrix map with \c Graph::Edge value type. |
---|
147 | /// |
---|
148 | typedef DynamicMatrixMap<Graph, typename Graph::Node, |
---|
149 | typename Graph::Edge> PredMap; |
---|
150 | |
---|
151 | /// \brief Instantiates a PredMap. |
---|
152 | /// |
---|
153 | /// This function instantiates a \ref PredMap. |
---|
154 | /// \param G is the graph, to which we would like to define the PredMap. |
---|
155 | /// \todo The graph alone may be insufficient for the initialization |
---|
156 | static PredMap *createPredMap(const Graph& graph) { |
---|
157 | return new PredMap(graph); |
---|
158 | } |
---|
159 | |
---|
160 | /// \brief The type of the matrix map that stores the dists of the nodes. |
---|
161 | /// |
---|
162 | /// The type of the matrix map that stores the dists of the nodes. |
---|
163 | /// It must meet the \ref concept::WriteMatrixMap "WriteMatrixMap" concept. |
---|
164 | /// |
---|
165 | typedef DynamicMatrixMap<Graph, typename Graph::Node, Value> DistMap; |
---|
166 | |
---|
167 | /// \brief Instantiates a DistMap. |
---|
168 | /// |
---|
169 | /// This function instantiates a \ref DistMap. |
---|
170 | /// \param G is the graph, to which we would like to define the |
---|
171 | /// \ref DistMap |
---|
172 | static DistMap *createDistMap(const _Graph& graph) { |
---|
173 | return new DistMap(graph); |
---|
174 | } |
---|
175 | |
---|
176 | }; |
---|
177 | |
---|
178 | /// \brief %Johnson algorithm class. |
---|
179 | /// |
---|
180 | /// \ingroup flowalgs |
---|
181 | /// This class provides an efficient implementation of \c %Johnson |
---|
182 | /// algorithm. The edge lengths are passed to the algorithm using a |
---|
183 | /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any |
---|
184 | /// kind of length. |
---|
185 | /// |
---|
186 | /// The algorithm solves the shortest path problem for each pair |
---|
187 | /// of node when the edges can have negative length but the graph should |
---|
188 | /// not contain cycles with negative sum of length. If we can assume |
---|
189 | /// that all edge is non-negative in the graph then the dijkstra algorithm |
---|
190 | /// should be used from each node. |
---|
191 | /// |
---|
192 | /// The complexity of this algorithm is $O(n^2 * log(n) + n * log(n) * e)$ or |
---|
193 | /// with fibonacci heap O(n^2 * log(n) + n * e). Usually the fibonacci heap |
---|
194 | /// implementation is slower than either binary heap implementation or the |
---|
195 | /// Floyd-Warshall algorithm. |
---|
196 | /// |
---|
197 | /// The type of the length is determined by the |
---|
198 | /// \ref concept::ReadMap::Value "Value" of the length map. |
---|
199 | /// |
---|
200 | /// \param _Graph The graph type the algorithm runs on. The default value |
---|
201 | /// is \ref ListGraph. The value of _Graph is not used directly by |
---|
202 | /// Johnson, it is only passed to \ref JohnsonDefaultTraits. |
---|
203 | /// \param _LengthMap This read-only EdgeMap determines the lengths of the |
---|
204 | /// edges. It is read once for each edge, so the map may involve in |
---|
205 | /// relatively time consuming process to compute the edge length if |
---|
206 | /// it is necessary. The default map type is \ref |
---|
207 | /// concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value |
---|
208 | /// of _LengthMap is not used directly by Johnson, it is only passed |
---|
209 | /// to \ref JohnsonDefaultTraits. \param _Traits Traits class to set |
---|
210 | /// various data types used by the algorithm. The default traits |
---|
211 | /// class is \ref JohnsonDefaultTraits |
---|
212 | /// "JohnsonDefaultTraits<_Graph,_LengthMap>". See \ref |
---|
213 | /// JohnsonDefaultTraits for the documentation of a Johnson traits |
---|
214 | /// class. |
---|
215 | /// |
---|
216 | /// \author Balazs Dezso |
---|
217 | |
---|
218 | #ifdef DOXYGEN |
---|
219 | template <typename _Graph, typename _LengthMap, typename _Traits> |
---|
220 | #else |
---|
221 | template <typename _Graph=ListGraph, |
---|
222 | typename _LengthMap=typename _Graph::template EdgeMap<int>, |
---|
223 | typename _Traits=JohnsonDefaultTraits<_Graph,_LengthMap> > |
---|
224 | #endif |
---|
225 | class Johnson { |
---|
226 | public: |
---|
227 | |
---|
228 | /// \brief \ref Exception for uninitialized parameters. |
---|
229 | /// |
---|
230 | /// This error represents problems in the initialization |
---|
231 | /// of the parameters of the algorithms. |
---|
232 | |
---|
233 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
234 | public: |
---|
235 | virtual const char* exceptionName() const { |
---|
236 | return "lemon::Johnson::UninitializedParameter"; |
---|
237 | } |
---|
238 | }; |
---|
239 | |
---|
240 | typedef _Traits Traits; |
---|
241 | ///The type of the underlying graph. |
---|
242 | typedef typename _Traits::Graph Graph; |
---|
243 | |
---|
244 | typedef typename Graph::Node Node; |
---|
245 | typedef typename Graph::NodeIt NodeIt; |
---|
246 | typedef typename Graph::Edge Edge; |
---|
247 | typedef typename Graph::EdgeIt EdgeIt; |
---|
248 | |
---|
249 | /// \brief The type of the length of the edges. |
---|
250 | typedef typename _Traits::LengthMap::Value Value; |
---|
251 | /// \brief The type of the map that stores the edge lengths. |
---|
252 | typedef typename _Traits::LengthMap LengthMap; |
---|
253 | /// \brief The type of the map that stores the last |
---|
254 | /// edges of the shortest paths. The type of the PredMap |
---|
255 | /// is a matrix map for Edges |
---|
256 | typedef typename _Traits::PredMap PredMap; |
---|
257 | /// \brief The type of the map that stores the dists of the nodes. |
---|
258 | /// The type of the DistMap is a matrix map for Values |
---|
259 | typedef typename _Traits::DistMap DistMap; |
---|
260 | /// \brief The operation traits. |
---|
261 | typedef typename _Traits::OperationTraits OperationTraits; |
---|
262 | ///The cross reference type used for the current heap. |
---|
263 | typedef typename _Traits::HeapCrossRef HeapCrossRef; |
---|
264 | ///The heap type used by the dijkstra algorithm. |
---|
265 | typedef typename _Traits::Heap Heap; |
---|
266 | private: |
---|
267 | /// Pointer to the underlying graph. |
---|
268 | const Graph *graph; |
---|
269 | /// Pointer to the length map |
---|
270 | const LengthMap *length; |
---|
271 | ///Pointer to the map of predecessors edges. |
---|
272 | PredMap *_pred; |
---|
273 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
274 | bool local_pred; |
---|
275 | ///Pointer to the map of distances. |
---|
276 | DistMap *_dist; |
---|
277 | ///Indicates if \ref _dist is locally allocated (\c true) or not. |
---|
278 | bool local_dist; |
---|
279 | ///Pointer to the heap cross references. |
---|
280 | HeapCrossRef *_heap_cross_ref; |
---|
281 | ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. |
---|
282 | bool local_heap_cross_ref; |
---|
283 | ///Pointer to the heap. |
---|
284 | Heap *_heap; |
---|
285 | ///Indicates if \ref _heap is locally allocated (\c true) or not. |
---|
286 | bool local_heap; |
---|
287 | |
---|
288 | /// Creates the maps if necessary. |
---|
289 | void create_maps() { |
---|
290 | if(!_pred) { |
---|
291 | local_pred = true; |
---|
292 | _pred = Traits::createPredMap(*graph); |
---|
293 | } |
---|
294 | if(!_dist) { |
---|
295 | local_dist = true; |
---|
296 | _dist = Traits::createDistMap(*graph); |
---|
297 | } |
---|
298 | if (!_heap_cross_ref) { |
---|
299 | local_heap_cross_ref = true; |
---|
300 | _heap_cross_ref = Traits::createHeapCrossRef(*graph); |
---|
301 | } |
---|
302 | if (!_heap) { |
---|
303 | local_heap = true; |
---|
304 | _heap = Traits::createHeap(*_heap_cross_ref); |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | public : |
---|
309 | |
---|
310 | /// \name Named template parameters |
---|
311 | |
---|
312 | ///@{ |
---|
313 | |
---|
314 | template <class T> |
---|
315 | struct DefPredMapTraits : public Traits { |
---|
316 | typedef T PredMap; |
---|
317 | static PredMap *createPredMap(const Graph& graph) { |
---|
318 | throw UninitializedParameter(); |
---|
319 | } |
---|
320 | }; |
---|
321 | |
---|
322 | /// \brief \ref named-templ-param "Named parameter" for setting PredMap |
---|
323 | /// type |
---|
324 | /// \ref named-templ-param "Named parameter" for setting PredMap type |
---|
325 | /// |
---|
326 | template <class T> |
---|
327 | struct DefPredMap |
---|
328 | : public Johnson< Graph, LengthMap, DefPredMapTraits<T> > { |
---|
329 | typedef Johnson< Graph, LengthMap, DefPredMapTraits<T> > Create; |
---|
330 | }; |
---|
331 | |
---|
332 | template <class T> |
---|
333 | struct DefDistMapTraits : public Traits { |
---|
334 | typedef T DistMap; |
---|
335 | static DistMap *createDistMap(const Graph& graph) { |
---|
336 | throw UninitializedParameter(); |
---|
337 | } |
---|
338 | }; |
---|
339 | /// \brief \ref named-templ-param "Named parameter" for setting DistMap |
---|
340 | /// type |
---|
341 | /// |
---|
342 | /// \ref named-templ-param "Named parameter" for setting DistMap type |
---|
343 | /// |
---|
344 | template <class T> |
---|
345 | struct DefDistMap |
---|
346 | : public Johnson< Graph, LengthMap, DefDistMapTraits<T> > { |
---|
347 | typedef Johnson< Graph, LengthMap, DefDistMapTraits<T> > Create; |
---|
348 | }; |
---|
349 | |
---|
350 | template <class T> |
---|
351 | struct DefOperationTraitsTraits : public Traits { |
---|
352 | typedef T OperationTraits; |
---|
353 | }; |
---|
354 | |
---|
355 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
356 | /// OperationTraits type |
---|
357 | /// |
---|
358 | /// \ref named-templ-param "Named parameter" for setting |
---|
359 | /// OperationTraits type |
---|
360 | template <class T> |
---|
361 | struct DefOperationTraits |
---|
362 | : public Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > { |
---|
363 | typedef Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > Create; |
---|
364 | }; |
---|
365 | |
---|
366 | template <class H, class CR> |
---|
367 | struct DefHeapTraits : public Traits { |
---|
368 | typedef CR HeapCrossRef; |
---|
369 | typedef H Heap; |
---|
370 | static HeapCrossRef *createHeapCrossRef(const Graph &) { |
---|
371 | throw UninitializedParameter(); |
---|
372 | } |
---|
373 | static Heap *createHeap(HeapCrossRef &) |
---|
374 | { |
---|
375 | throw UninitializedParameter(); |
---|
376 | } |
---|
377 | }; |
---|
378 | ///\brief \ref named-templ-param "Named parameter" for setting heap and |
---|
379 | ///cross reference type |
---|
380 | |
---|
381 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
382 | ///reference type |
---|
383 | /// |
---|
384 | template <class H, class CR = typename Graph::template NodeMap<int> > |
---|
385 | struct DefHeap |
---|
386 | : public Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > { |
---|
387 | typedef Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > Create; |
---|
388 | }; |
---|
389 | |
---|
390 | template <class H, class CR> |
---|
391 | struct DefStandardHeapTraits : public Traits { |
---|
392 | typedef CR HeapCrossRef; |
---|
393 | typedef H Heap; |
---|
394 | static HeapCrossRef *createHeapCrossRef(const Graph &G) { |
---|
395 | return new HeapCrossRef(G); |
---|
396 | } |
---|
397 | static Heap *createHeap(HeapCrossRef &R) |
---|
398 | { |
---|
399 | return new Heap(R); |
---|
400 | } |
---|
401 | }; |
---|
402 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
403 | ///reference type with automatic allocation |
---|
404 | |
---|
405 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
406 | ///reference type. It can allocate the heap and the cross reference |
---|
407 | ///object if the cross reference's constructor waits for the graph as |
---|
408 | ///parameter and the heap's constructor waits for the cross reference. |
---|
409 | template <class H, class CR = typename Graph::template NodeMap<int> > |
---|
410 | struct DefStandardHeap |
---|
411 | : public Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > { |
---|
412 | typedef Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > |
---|
413 | Create; |
---|
414 | }; |
---|
415 | |
---|
416 | ///@} |
---|
417 | |
---|
418 | protected: |
---|
419 | |
---|
420 | Johnson() {} |
---|
421 | |
---|
422 | public: |
---|
423 | |
---|
424 | typedef Johnson Create; |
---|
425 | |
---|
426 | /// \brief Constructor. |
---|
427 | /// |
---|
428 | /// \param _graph the graph the algorithm will run on. |
---|
429 | /// \param _length the length map used by the algorithm. |
---|
430 | Johnson(const Graph& _graph, const LengthMap& _length) : |
---|
431 | graph(&_graph), length(&_length), |
---|
432 | _pred(0), local_pred(false), |
---|
433 | _dist(0), local_dist(false), |
---|
434 | _heap_cross_ref(0), local_heap_cross_ref(false), |
---|
435 | _heap(0), local_heap(false) {} |
---|
436 | |
---|
437 | ///Destructor. |
---|
438 | ~Johnson() { |
---|
439 | if (local_pred) delete _pred; |
---|
440 | if (local_dist) delete _dist; |
---|
441 | if (local_heap_cross_ref) delete _heap_cross_ref; |
---|
442 | if (local_heap) delete _heap; |
---|
443 | } |
---|
444 | |
---|
445 | /// \brief Sets the length map. |
---|
446 | /// |
---|
447 | /// Sets the length map. |
---|
448 | /// \return \c (*this) |
---|
449 | Johnson &lengthMap(const LengthMap &m) { |
---|
450 | length = &m; |
---|
451 | return *this; |
---|
452 | } |
---|
453 | |
---|
454 | /// \brief Sets the map storing the predecessor edges. |
---|
455 | /// |
---|
456 | /// Sets the map storing the predecessor edges. |
---|
457 | /// If you don't use this function before calling \ref run(), |
---|
458 | /// it will allocate one. The destuctor deallocates this |
---|
459 | /// automatically allocated map, of course. |
---|
460 | /// \return \c (*this) |
---|
461 | Johnson &predMap(PredMap &m) { |
---|
462 | if(local_pred) { |
---|
463 | delete _pred; |
---|
464 | local_pred=false; |
---|
465 | } |
---|
466 | _pred = &m; |
---|
467 | return *this; |
---|
468 | } |
---|
469 | |
---|
470 | /// \brief Sets the map storing the distances calculated by the algorithm. |
---|
471 | /// |
---|
472 | /// Sets the map storing the distances calculated by the algorithm. |
---|
473 | /// If you don't use this function before calling \ref run(), |
---|
474 | /// it will allocate one. The destuctor deallocates this |
---|
475 | /// automatically allocated map, of course. |
---|
476 | /// \return \c (*this) |
---|
477 | Johnson &distMap(DistMap &m) { |
---|
478 | if(local_dist) { |
---|
479 | delete _dist; |
---|
480 | local_dist=false; |
---|
481 | } |
---|
482 | _dist = &m; |
---|
483 | return *this; |
---|
484 | } |
---|
485 | |
---|
486 | public: |
---|
487 | |
---|
488 | ///\name Execution control |
---|
489 | /// The simplest way to execute the algorithm is to use |
---|
490 | /// one of the member functions called \c run(...). |
---|
491 | /// \n |
---|
492 | /// If you need more control on the execution, |
---|
493 | /// Finally \ref start() will perform the actual path |
---|
494 | /// computation. |
---|
495 | |
---|
496 | ///@{ |
---|
497 | |
---|
498 | /// \brief Initializes the internal data structures. |
---|
499 | /// |
---|
500 | /// Initializes the internal data structures. |
---|
501 | void init() { |
---|
502 | create_maps(); |
---|
503 | } |
---|
504 | |
---|
505 | /// \brief Executes the algorithm with own potential map. |
---|
506 | /// |
---|
507 | /// This method runs the %Johnson algorithm in order to compute |
---|
508 | /// the shortest path to each node pairs. The potential map |
---|
509 | /// can be given for this algorithm which usually calculated |
---|
510 | /// by the Bellman-Ford algorithm. If the graph does not have |
---|
511 | /// negative length edge then this start function can be used |
---|
512 | /// with constMap<Node, int>(0) parameter to omit the running time of |
---|
513 | /// the Bellman-Ford. |
---|
514 | /// The algorithm computes |
---|
515 | /// - The shortest path tree for each node. |
---|
516 | /// - The distance between each node pairs. |
---|
517 | template <typename PotentialMap> |
---|
518 | void shiftedStart(const PotentialMap& potential) { |
---|
519 | typename Graph::template EdgeMap<Value> shiftlen(*graph); |
---|
520 | for (EdgeIt it(*graph); it != INVALID; ++it) { |
---|
521 | shiftlen[it] = (*length)[it] |
---|
522 | + potential[graph->source(it)] |
---|
523 | - potential[graph->target(it)]; |
---|
524 | } |
---|
525 | |
---|
526 | typename Dijkstra<Graph, typename Graph::template EdgeMap<Value> >:: |
---|
527 | template DefHeap<Heap, HeapCrossRef>:: |
---|
528 | Create dijkstra(*graph, shiftlen); |
---|
529 | |
---|
530 | dijkstra.heap(*_heap, *_heap_cross_ref); |
---|
531 | |
---|
532 | for (NodeIt it(*graph); it != INVALID; ++it) { |
---|
533 | dijkstra.run(it); |
---|
534 | for (NodeIt jt(*graph); jt != INVALID; ++jt) { |
---|
535 | if (dijkstra.reached(jt)) { |
---|
536 | _dist->set(it, jt, dijkstra.dist(jt) + |
---|
537 | potential[jt] - potential[it]); |
---|
538 | _pred->set(it, jt, dijkstra.predEdge(jt)); |
---|
539 | } else { |
---|
540 | _dist->set(it, jt, OperationTraits::infinity()); |
---|
541 | _pred->set(it, jt, INVALID); |
---|
542 | } |
---|
543 | } |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | /// \brief Executes the algorithm. |
---|
548 | /// |
---|
549 | /// This method runs the %Johnson algorithm in order to compute |
---|
550 | /// the shortest path to each node pairs. The algorithm |
---|
551 | /// computes |
---|
552 | /// - The shortest path tree for each node. |
---|
553 | /// - The distance between each node pairs. |
---|
554 | void start() { |
---|
555 | |
---|
556 | typedef typename BellmanFord<Graph, LengthMap>:: |
---|
557 | template DefOperationTraits<OperationTraits>:: |
---|
558 | template DefPredMap<NullMap<Node, Edge> >:: |
---|
559 | Create BellmanFordType; |
---|
560 | |
---|
561 | BellmanFordType bellmanford(*graph, *length); |
---|
562 | |
---|
563 | NullMap<Node, Edge> predMap; |
---|
564 | |
---|
565 | bellmanford.predMap(predMap); |
---|
566 | |
---|
567 | bellmanford.init(OperationTraits::zero()); |
---|
568 | bellmanford.start(); |
---|
569 | |
---|
570 | shiftedStart(bellmanford.distMap()); |
---|
571 | } |
---|
572 | |
---|
573 | /// \brief Executes the algorithm and checks the negatvie cycles. |
---|
574 | /// |
---|
575 | /// This method runs the %Johnson algorithm in order to compute |
---|
576 | /// the shortest path to each node pairs. If the graph contains |
---|
577 | /// negative cycle it gives back false. The algorithm |
---|
578 | /// computes |
---|
579 | /// - The shortest path tree for each node. |
---|
580 | /// - The distance between each node pairs. |
---|
581 | bool checkedStart() { |
---|
582 | |
---|
583 | typedef typename BellmanFord<Graph, LengthMap>:: |
---|
584 | template DefOperationTraits<OperationTraits>:: |
---|
585 | template DefPredMap<NullMap<Node, Edge> >:: |
---|
586 | Create BellmanFordType; |
---|
587 | |
---|
588 | BellmanFordType bellmanford(*graph, *length); |
---|
589 | |
---|
590 | NullMap<Node, Edge> predMap; |
---|
591 | |
---|
592 | bellmanford.predMap(predMap); |
---|
593 | |
---|
594 | bellmanford.init(OperationTraits::zero()); |
---|
595 | if (!bellmanford.checkedStart()) return false; |
---|
596 | |
---|
597 | shiftedStart(bellmanford.distMap()); |
---|
598 | return true; |
---|
599 | } |
---|
600 | |
---|
601 | |
---|
602 | /// \brief Runs %Johnson algorithm. |
---|
603 | /// |
---|
604 | /// This method runs the %Johnson algorithm from a each node |
---|
605 | /// in order to compute the shortest path to each node pairs. |
---|
606 | /// The algorithm computes |
---|
607 | /// - The shortest path tree for each node. |
---|
608 | /// - The distance between each node pairs. |
---|
609 | /// |
---|
610 | /// \note d.run(s) is just a shortcut of the following code. |
---|
611 | /// \code |
---|
612 | /// d.init(); |
---|
613 | /// d.start(); |
---|
614 | /// \endcode |
---|
615 | void run() { |
---|
616 | init(); |
---|
617 | start(); |
---|
618 | } |
---|
619 | |
---|
620 | ///@} |
---|
621 | |
---|
622 | /// \name Query Functions |
---|
623 | /// The result of the %Johnson algorithm can be obtained using these |
---|
624 | /// functions.\n |
---|
625 | /// Before the use of these functions, |
---|
626 | /// either run() or start() must be called. |
---|
627 | |
---|
628 | ///@{ |
---|
629 | |
---|
630 | /// \brief Copies the shortest path to \c t into \c p |
---|
631 | /// |
---|
632 | /// This function copies the shortest path to \c t into \c p. |
---|
633 | /// If it \c t is a source itself or unreachable, then it does not |
---|
634 | /// alter \c p. |
---|
635 | /// \return Returns \c true if a path to \c t was actually copied to \c p, |
---|
636 | /// \c false otherwise. |
---|
637 | /// \sa DirPath |
---|
638 | template <typename Path> |
---|
639 | bool getPath(Path &p, Node source, Node target) { |
---|
640 | if (connected(source, target)) { |
---|
641 | p.clear(); |
---|
642 | typename Path::Builder b(target); |
---|
643 | for(b.setStartNode(target); predEdge(source, target) != INVALID; |
---|
644 | target = predNode(target)) { |
---|
645 | b.pushFront(predEdge(source, target)); |
---|
646 | } |
---|
647 | b.commit(); |
---|
648 | return true; |
---|
649 | } |
---|
650 | return false; |
---|
651 | } |
---|
652 | |
---|
653 | /// \brief The distance between two nodes. |
---|
654 | /// |
---|
655 | /// Returns the distance between two nodes. |
---|
656 | /// \pre \ref run() must be called before using this function. |
---|
657 | /// \warning If node \c v in unreachable from the root the return value |
---|
658 | /// of this funcion is undefined. |
---|
659 | Value dist(Node source, Node target) const { |
---|
660 | return (*_dist)(source, target); |
---|
661 | } |
---|
662 | |
---|
663 | /// \brief Returns the 'previous edge' of the shortest path tree. |
---|
664 | /// |
---|
665 | /// For the node \c node it returns the 'previous edge' of the shortest |
---|
666 | /// path tree to direction of the node \c root |
---|
667 | /// i.e. it returns the last edge of a shortest path from the node \c root |
---|
668 | /// to \c node. It is \ref INVALID if \c node is unreachable from the root |
---|
669 | /// or if \c node=root. The shortest path tree used here is equal to the |
---|
670 | /// shortest path tree used in \ref predNode(). |
---|
671 | /// \pre \ref run() must be called before using this function. |
---|
672 | Edge predEdge(Node root, Node node) const { |
---|
673 | return (*_pred)(root, node); |
---|
674 | } |
---|
675 | |
---|
676 | /// \brief Returns the 'previous node' of the shortest path tree. |
---|
677 | /// |
---|
678 | /// For a node \c node it returns the 'previous node' of the shortest path |
---|
679 | /// tree to direction of the node \c root, i.e. it returns the last but |
---|
680 | /// one node from a shortest path from the \c root to \c node. It is |
---|
681 | /// INVALID if \c node is unreachable from the root or if \c node=root. |
---|
682 | /// The shortest path tree used here is equal to the |
---|
683 | /// shortest path tree used in \ref predEdge(). |
---|
684 | /// \pre \ref run() must be called before using this function. |
---|
685 | Node predNode(Node root, Node node) const { |
---|
686 | return (*_pred)(root, node) == INVALID ? |
---|
687 | INVALID : graph->source((*_pred)(root, node)); |
---|
688 | } |
---|
689 | |
---|
690 | /// \brief Returns a reference to the matrix node map of distances. |
---|
691 | /// |
---|
692 | /// Returns a reference to the matrix node map of distances. |
---|
693 | /// |
---|
694 | /// \pre \ref run() must be called before using this function. |
---|
695 | const DistMap &distMap() const { return *_dist;} |
---|
696 | |
---|
697 | /// \brief Returns a reference to the shortest path tree map. |
---|
698 | /// |
---|
699 | /// Returns a reference to the matrix node map of the edges of the |
---|
700 | /// shortest path tree. |
---|
701 | /// \pre \ref run() must be called before using this function. |
---|
702 | const PredMap &predMap() const { return *_pred;} |
---|
703 | |
---|
704 | /// \brief Checks if a node is reachable from the root. |
---|
705 | /// |
---|
706 | /// Returns \c true if \c v is reachable from the root. |
---|
707 | /// \pre \ref run() must be called before using this function. |
---|
708 | /// |
---|
709 | bool connected(Node source, Node target) { |
---|
710 | return (*_dist)(source, target) != OperationTraits::infinity(); |
---|
711 | } |
---|
712 | |
---|
713 | ///@} |
---|
714 | }; |
---|
715 | |
---|
716 | } //END OF NAMESPACE LEMON |
---|
717 | |
---|
718 | #endif |
---|