[1699] | 1 | /* -*- C++ -*- |
---|
| 2 | * lemon/johnson.h - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2005 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/belmann_ford.h> |
---|
| 29 | #include <lemon/invalid.h> |
---|
| 30 | #include <lemon/error.h> |
---|
| 31 | #include <lemon/maps.h> |
---|
[1723] | 32 | #include <lemon/matrix_maps.h> |
---|
[1699] | 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 belmann-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; |
---|
[1741] | 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 | } |
---|
[1699] | 141 | |
---|
[1723] | 142 | /// \brief The type of the matrix map that stores the last edges of the |
---|
[1699] | 143 | /// shortest paths. |
---|
| 144 | /// |
---|
[1723] | 145 | /// The type of the map that stores the last edges of the shortest paths. |
---|
[1699] | 146 | /// It must be a matrix map with \c Graph::Edge value type. |
---|
| 147 | /// |
---|
[1723] | 148 | typedef DynamicMatrixMap<Graph, typename Graph::Node, |
---|
| 149 | typename Graph::Edge> PredMap; |
---|
[1699] | 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 |
---|
[1741] | 156 | static PredMap *createPredMap(const Graph& graph) { |
---|
[1699] | 157 | return new PredMap(graph); |
---|
| 158 | } |
---|
| 159 | |
---|
[1723] | 160 | /// \brief The type of the matrix map that stores the dists of the nodes. |
---|
[1699] | 161 | /// |
---|
[1723] | 162 | /// The type of the matrix map that stores the dists of the nodes. |
---|
| 163 | /// It must meet the \ref concept::WriteMatrixMap "WriteMatrixMap" concept. |
---|
[1699] | 164 | /// |
---|
[1723] | 165 | typedef DynamicMatrixMap<Graph, typename Graph::Node, Value> DistMap; |
---|
| 166 | |
---|
[1699] | 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 | |
---|
[1754] | 178 | /// \brief %Johnson algorithm class. |
---|
[1699] | 179 | /// |
---|
| 180 | /// \ingroup flowalgs |
---|
[1754] | 181 | /// This class provides an efficient implementation of \c %Johnson |
---|
[1699] | 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 | /// |
---|
[1723] | 186 | /// The algorithm solves the shortest path problem for each pairs |
---|
| 187 | /// of node when the edges can have negative length but the graph should |
---|
[1754] | 188 | /// not contain cycles with negative sum of length. If we can assume |
---|
[1723] | 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 |
---|
[1741] | 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. |
---|
[1723] | 196 | /// |
---|
[1699] | 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 | |
---|
[1710] | 218 | #ifdef DOXYGEN |
---|
| 219 | template <typename _Graph, typename _LengthMap, typename _Traits> |
---|
| 220 | #else |
---|
[1699] | 221 | template <typename _Graph=ListGraph, |
---|
| 222 | typename _LengthMap=typename _Graph::template EdgeMap<int>, |
---|
| 223 | typename _Traits=JohnsonDefaultTraits<_Graph,_LengthMap> > |
---|
[1710] | 224 | #endif |
---|
[1699] | 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; |
---|
[1741] | 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; |
---|
[1699] | 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; |
---|
[1741] | 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; |
---|
[1699] | 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 | } |
---|
[1741] | 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 | } |
---|
[1699] | 306 | } |
---|
[1741] | 307 | |
---|
[1699] | 308 | public : |
---|
[1741] | 309 | |
---|
| 310 | typedef Johnson Create; |
---|
[1699] | 311 | |
---|
| 312 | /// \name Named template parameters |
---|
| 313 | |
---|
| 314 | ///@{ |
---|
| 315 | |
---|
| 316 | template <class T> |
---|
| 317 | struct DefPredMapTraits : public Traits { |
---|
| 318 | typedef T PredMap; |
---|
| 319 | static PredMap *createPredMap(const Graph& graph) { |
---|
| 320 | throw UninitializedParameter(); |
---|
| 321 | } |
---|
| 322 | }; |
---|
| 323 | |
---|
| 324 | /// \brief \ref named-templ-param "Named parameter" for setting PredMap |
---|
| 325 | /// type |
---|
| 326 | /// \ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 327 | /// |
---|
| 328 | template <class T> |
---|
[1710] | 329 | struct DefPredMap |
---|
| 330 | : public Johnson< Graph, LengthMap, DefPredMapTraits<T> > { |
---|
| 331 | typedef Johnson< Graph, LengthMap, DefPredMapTraits<T> > Create; |
---|
| 332 | }; |
---|
[1699] | 333 | |
---|
| 334 | template <class T> |
---|
| 335 | struct DefDistMapTraits : public Traits { |
---|
| 336 | typedef T DistMap; |
---|
| 337 | static DistMap *createDistMap(const Graph& graph) { |
---|
| 338 | throw UninitializedParameter(); |
---|
| 339 | } |
---|
| 340 | }; |
---|
| 341 | /// \brief \ref named-templ-param "Named parameter" for setting DistMap |
---|
| 342 | /// type |
---|
| 343 | /// |
---|
| 344 | /// \ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 345 | /// |
---|
| 346 | template <class T> |
---|
[1710] | 347 | struct DefDistMap |
---|
| 348 | : public Johnson< Graph, LengthMap, DefDistMapTraits<T> > { |
---|
| 349 | typedef Johnson< Graph, LengthMap, DefDistMapTraits<T> > Create; |
---|
| 350 | }; |
---|
[1699] | 351 | |
---|
| 352 | template <class T> |
---|
| 353 | struct DefOperationTraitsTraits : public Traits { |
---|
| 354 | typedef T OperationTraits; |
---|
| 355 | }; |
---|
| 356 | |
---|
| 357 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
| 358 | /// OperationTraits type |
---|
| 359 | /// |
---|
[1710] | 360 | /// \ref named-templ-param "Named parameter" for setting |
---|
| 361 | /// OperationTraits type |
---|
[1699] | 362 | template <class T> |
---|
[1710] | 363 | struct DefOperationTraits |
---|
| 364 | : public Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > { |
---|
| 365 | typedef Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > Create; |
---|
| 366 | }; |
---|
[1741] | 367 | |
---|
| 368 | template <class H, class CR> |
---|
| 369 | struct DefHeapTraits : public Traits { |
---|
| 370 | typedef CR HeapCrossRef; |
---|
| 371 | typedef H Heap; |
---|
| 372 | static HeapCrossRef *createHeapCrossRef(const Graph &) { |
---|
| 373 | throw UninitializedParameter(); |
---|
| 374 | } |
---|
| 375 | static Heap *createHeap(HeapCrossRef &) |
---|
| 376 | { |
---|
| 377 | throw UninitializedParameter(); |
---|
| 378 | } |
---|
| 379 | }; |
---|
[1754] | 380 | ///\brief \ref named-templ-param "Named parameter" for setting heap and |
---|
| 381 | ///cross reference type |
---|
[1741] | 382 | |
---|
| 383 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
| 384 | ///reference type |
---|
| 385 | /// |
---|
| 386 | template <class H, class CR = typename Graph::template NodeMap<int> > |
---|
| 387 | struct DefHeap |
---|
| 388 | : public Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > { |
---|
| 389 | typedef Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > Create; |
---|
| 390 | }; |
---|
| 391 | |
---|
| 392 | template <class H, class CR> |
---|
| 393 | struct DefStandardHeapTraits : public Traits { |
---|
| 394 | typedef CR HeapCrossRef; |
---|
| 395 | typedef H Heap; |
---|
| 396 | static HeapCrossRef *createHeapCrossRef(const Graph &G) { |
---|
| 397 | return new HeapCrossRef(G); |
---|
| 398 | } |
---|
| 399 | static Heap *createHeap(HeapCrossRef &R) |
---|
| 400 | { |
---|
| 401 | return new Heap(R); |
---|
| 402 | } |
---|
| 403 | }; |
---|
| 404 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
| 405 | ///reference type with automatic allocation |
---|
| 406 | |
---|
| 407 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
| 408 | ///reference type. It can allocate the heap and the cross reference |
---|
| 409 | ///object if the cross reference's constructor waits for the graph as |
---|
| 410 | ///parameter and the heap's constructor waits for the cross reference. |
---|
| 411 | template <class H, class CR = typename Graph::template NodeMap<int> > |
---|
| 412 | struct DefStandardHeap |
---|
| 413 | : public Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > { |
---|
| 414 | typedef Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > |
---|
| 415 | Create; |
---|
| 416 | }; |
---|
[1699] | 417 | |
---|
| 418 | ///@} |
---|
| 419 | |
---|
[1710] | 420 | protected: |
---|
| 421 | |
---|
| 422 | Johnson() {} |
---|
| 423 | |
---|
[1699] | 424 | public: |
---|
[1741] | 425 | |
---|
| 426 | typedef Johnson Create; |
---|
[1699] | 427 | |
---|
| 428 | /// \brief Constructor. |
---|
| 429 | /// |
---|
| 430 | /// \param _graph the graph the algorithm will run on. |
---|
| 431 | /// \param _length the length map used by the algorithm. |
---|
| 432 | Johnson(const Graph& _graph, const LengthMap& _length) : |
---|
| 433 | graph(&_graph), length(&_length), |
---|
| 434 | _pred(0), local_pred(false), |
---|
[1741] | 435 | _dist(0), local_dist(false), |
---|
| 436 | _heap_cross_ref(0), local_heap_cross_ref(false), |
---|
| 437 | _heap(0), local_heap(false) {} |
---|
[1699] | 438 | |
---|
| 439 | ///Destructor. |
---|
| 440 | ~Johnson() { |
---|
[1741] | 441 | if (local_pred) delete _pred; |
---|
| 442 | if (local_dist) delete _dist; |
---|
| 443 | if (local_heap_cross_ref) delete _heap_cross_ref; |
---|
| 444 | if (local_heap) delete _heap; |
---|
[1699] | 445 | } |
---|
| 446 | |
---|
| 447 | /// \brief Sets the length map. |
---|
| 448 | /// |
---|
| 449 | /// Sets the length map. |
---|
| 450 | /// \return \c (*this) |
---|
| 451 | Johnson &lengthMap(const LengthMap &m) { |
---|
| 452 | length = &m; |
---|
| 453 | return *this; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | /// \brief Sets the map storing the predecessor edges. |
---|
| 457 | /// |
---|
| 458 | /// Sets the map storing the predecessor edges. |
---|
| 459 | /// If you don't use this function before calling \ref run(), |
---|
| 460 | /// it will allocate one. The destuctor deallocates this |
---|
| 461 | /// automatically allocated map, of course. |
---|
| 462 | /// \return \c (*this) |
---|
| 463 | Johnson &predMap(PredMap &m) { |
---|
| 464 | if(local_pred) { |
---|
| 465 | delete _pred; |
---|
| 466 | local_pred=false; |
---|
| 467 | } |
---|
| 468 | _pred = &m; |
---|
| 469 | return *this; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | /// \brief Sets the map storing the distances calculated by the algorithm. |
---|
| 473 | /// |
---|
| 474 | /// Sets the map storing the distances calculated by the algorithm. |
---|
| 475 | /// If you don't use this function before calling \ref run(), |
---|
| 476 | /// it will allocate one. The destuctor deallocates this |
---|
| 477 | /// automatically allocated map, of course. |
---|
| 478 | /// \return \c (*this) |
---|
| 479 | Johnson &distMap(DistMap &m) { |
---|
| 480 | if(local_dist) { |
---|
| 481 | delete _dist; |
---|
| 482 | local_dist=false; |
---|
| 483 | } |
---|
| 484 | _dist = &m; |
---|
| 485 | return *this; |
---|
| 486 | } |
---|
| 487 | |
---|
[1741] | 488 | protected: |
---|
| 489 | |
---|
[1754] | 490 | template <typename PotentialMap> |
---|
| 491 | void shiftedRun(const PotentialMap& potential) { |
---|
[1741] | 492 | |
---|
[1747] | 493 | typename Graph::template EdgeMap<Value> shiftlen(*graph); |
---|
| 494 | for (EdgeIt it(*graph); it != INVALID; ++it) { |
---|
| 495 | shiftlen[it] = (*length)[it] |
---|
[1754] | 496 | + potential[graph->source(it)] |
---|
| 497 | - potential[graph->target(it)]; |
---|
[1747] | 498 | } |
---|
| 499 | |
---|
| 500 | typename Dijkstra<Graph, typename Graph::template EdgeMap<Value> >:: |
---|
| 501 | template DefHeap<Heap, HeapCrossRef>:: |
---|
| 502 | Create dijkstra(*graph, shiftlen); |
---|
[1741] | 503 | |
---|
| 504 | dijkstra.heap(*_heap, *_heap_cross_ref); |
---|
| 505 | |
---|
| 506 | for (NodeIt it(*graph); it != INVALID; ++it) { |
---|
| 507 | dijkstra.run(it); |
---|
| 508 | for (NodeIt jt(*graph); jt != INVALID; ++jt) { |
---|
| 509 | if (dijkstra.reached(jt)) { |
---|
| 510 | _dist->set(it, jt, dijkstra.dist(jt) + |
---|
[1754] | 511 | potential[jt] - potential[it]); |
---|
[1741] | 512 | _pred->set(it, jt, dijkstra.pred(jt)); |
---|
| 513 | } else { |
---|
| 514 | _dist->set(it, jt, OperationTraits::infinity()); |
---|
| 515 | _pred->set(it, jt, INVALID); |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | public: |
---|
| 522 | |
---|
[1699] | 523 | ///\name Execution control |
---|
| 524 | /// The simplest way to execute the algorithm is to use |
---|
| 525 | /// one of the member functions called \c run(...). |
---|
| 526 | /// \n |
---|
| 527 | /// If you need more control on the execution, |
---|
| 528 | /// Finally \ref start() will perform the actual path |
---|
| 529 | /// computation. |
---|
| 530 | |
---|
| 531 | ///@{ |
---|
| 532 | |
---|
| 533 | /// \brief Initializes the internal data structures. |
---|
| 534 | /// |
---|
| 535 | /// Initializes the internal data structures. |
---|
| 536 | void init() { |
---|
| 537 | create_maps(); |
---|
| 538 | } |
---|
[1741] | 539 | |
---|
[1699] | 540 | /// \brief Executes the algorithm. |
---|
| 541 | /// |
---|
| 542 | /// This method runs the %Johnson algorithm in order to compute |
---|
| 543 | /// the shortest path to each node pairs. The algorithm |
---|
| 544 | /// computes |
---|
| 545 | /// - The shortest path tree for each node. |
---|
| 546 | /// - The distance between each node pairs. |
---|
| 547 | void start() { |
---|
[1710] | 548 | |
---|
[1754] | 549 | typedef typename BelmannFord<Graph, LengthMap>:: |
---|
| 550 | template DefOperationTraits<OperationTraits>:: |
---|
| 551 | template DefPredMap<NullMap<Node, Edge> >:: |
---|
| 552 | Create BelmannFordType; |
---|
| 553 | |
---|
[1710] | 554 | BelmannFordType belmannford(*graph, *length); |
---|
| 555 | |
---|
| 556 | NullMap<Node, Edge> predMap; |
---|
| 557 | |
---|
| 558 | belmannford.predMap(predMap); |
---|
[1699] | 559 | |
---|
[1710] | 560 | belmannford.init(OperationTraits::zero()); |
---|
[1699] | 561 | belmannford.start(); |
---|
| 562 | |
---|
[1754] | 563 | shiftedRun(belmannford.distMap()); |
---|
[1699] | 564 | } |
---|
[1741] | 565 | |
---|
[1754] | 566 | /// \brief Executes the algorithm and checks the negatvie cycles. |
---|
[1741] | 567 | /// |
---|
| 568 | /// This method runs the %Johnson algorithm in order to compute |
---|
| 569 | /// the shortest path to each node pairs. If the graph contains |
---|
[1754] | 570 | /// negative cycle it gives back false. The algorithm |
---|
[1741] | 571 | /// computes |
---|
| 572 | /// - The shortest path tree for each node. |
---|
| 573 | /// - The distance between each node pairs. |
---|
| 574 | bool checkedStart() { |
---|
[1754] | 575 | |
---|
| 576 | typedef typename BelmannFord<Graph, LengthMap>:: |
---|
| 577 | template DefOperationTraits<OperationTraits>:: |
---|
| 578 | template DefPredMap<NullMap<Node, Edge> >:: |
---|
| 579 | Create BelmannFordType; |
---|
[1741] | 580 | |
---|
| 581 | BelmannFordType belmannford(*graph, *length); |
---|
| 582 | |
---|
| 583 | NullMap<Node, Edge> predMap; |
---|
| 584 | |
---|
| 585 | belmannford.predMap(predMap); |
---|
| 586 | |
---|
| 587 | belmannford.init(OperationTraits::zero()); |
---|
| 588 | if (!belmannford.checkedStart()) return false; |
---|
| 589 | |
---|
[1754] | 590 | shiftedRun(belmannford.distMap()); |
---|
[1741] | 591 | return true; |
---|
| 592 | } |
---|
| 593 | |
---|
[1699] | 594 | |
---|
| 595 | /// \brief Runs %Johnson algorithm. |
---|
| 596 | /// |
---|
| 597 | /// This method runs the %Johnson algorithm from a each node |
---|
| 598 | /// in order to compute the shortest path to each node pairs. |
---|
| 599 | /// The algorithm computes |
---|
| 600 | /// - The shortest path tree for each node. |
---|
| 601 | /// - The distance between each node pairs. |
---|
| 602 | /// |
---|
| 603 | /// \note d.run(s) is just a shortcut of the following code. |
---|
| 604 | /// \code |
---|
| 605 | /// d.init(); |
---|
| 606 | /// d.start(); |
---|
| 607 | /// \endcode |
---|
| 608 | void run() { |
---|
| 609 | init(); |
---|
| 610 | start(); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | ///@} |
---|
| 614 | |
---|
| 615 | /// \name Query Functions |
---|
| 616 | /// The result of the %Johnson algorithm can be obtained using these |
---|
| 617 | /// functions.\n |
---|
| 618 | /// Before the use of these functions, |
---|
| 619 | /// either run() or start() must be called. |
---|
| 620 | |
---|
| 621 | ///@{ |
---|
| 622 | |
---|
| 623 | /// \brief Copies the shortest path to \c t into \c p |
---|
| 624 | /// |
---|
| 625 | /// This function copies the shortest path to \c t into \c p. |
---|
| 626 | /// If it \c t is a source itself or unreachable, then it does not |
---|
| 627 | /// alter \c p. |
---|
| 628 | /// \todo Is it the right way to handle unreachable nodes? |
---|
| 629 | /// \return Returns \c true if a path to \c t was actually copied to \c p, |
---|
| 630 | /// \c false otherwise. |
---|
| 631 | /// \sa DirPath |
---|
| 632 | template <typename Path> |
---|
| 633 | bool getPath(Path &p, Node source, Node target) { |
---|
| 634 | if (connected(source, target)) { |
---|
| 635 | p.clear(); |
---|
| 636 | typename Path::Builder b(target); |
---|
| 637 | for(b.setStartNode(target); pred(source, target) != INVALID; |
---|
| 638 | target = predNode(target)) { |
---|
| 639 | b.pushFront(pred(source, target)); |
---|
| 640 | } |
---|
| 641 | b.commit(); |
---|
| 642 | return true; |
---|
| 643 | } |
---|
| 644 | return false; |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | /// \brief The distance between two nodes. |
---|
| 648 | /// |
---|
| 649 | /// Returns the distance between two nodes. |
---|
| 650 | /// \pre \ref run() must be called before using this function. |
---|
| 651 | /// \warning If node \c v in unreachable from the root the return value |
---|
| 652 | /// of this funcion is undefined. |
---|
| 653 | Value dist(Node source, Node target) const { |
---|
| 654 | return (*_dist)(source, target); |
---|
| 655 | } |
---|
| 656 | |
---|
| 657 | /// \brief Returns the 'previous edge' of the shortest path tree. |
---|
| 658 | /// |
---|
| 659 | /// For the node \c node it returns the 'previous edge' of the shortest |
---|
| 660 | /// path tree to direction of the node \c root |
---|
| 661 | /// i.e. it returns the last edge of a shortest path from the node \c root |
---|
| 662 | /// to \c node. It is \ref INVALID if \c node is unreachable from the root |
---|
| 663 | /// or if \c node=root. The shortest path tree used here is equal to the |
---|
| 664 | /// shortest path tree used in \ref predNode(). |
---|
| 665 | /// \pre \ref run() must be called before using this function. |
---|
| 666 | /// \todo predEdge could be a better name. |
---|
| 667 | Edge pred(Node root, Node node) const { |
---|
| 668 | return (*_pred)(root, node); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | /// \brief Returns the 'previous node' of the shortest path tree. |
---|
| 672 | /// |
---|
| 673 | /// For a node \c node it returns the 'previous node' of the shortest path |
---|
| 674 | /// tree to direction of the node \c root, i.e. it returns the last but |
---|
| 675 | /// one node from a shortest path from the \c root to \c node. It is |
---|
| 676 | /// INVALID if \c node is unreachable from the root or if \c node=root. |
---|
| 677 | /// The shortest path tree used here is equal to the |
---|
| 678 | /// shortest path tree used in \ref pred(). |
---|
| 679 | /// \pre \ref run() must be called before using this function. |
---|
| 680 | Node predNode(Node root, Node node) const { |
---|
| 681 | return (*_pred)(root, node) == INVALID ? |
---|
| 682 | INVALID : graph->source((*_pred)(root, node)); |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | /// \brief Returns a reference to the matrix node map of distances. |
---|
| 686 | /// |
---|
| 687 | /// Returns a reference to the matrix node map of distances. |
---|
| 688 | /// |
---|
| 689 | /// \pre \ref run() must be called before using this function. |
---|
| 690 | const DistMap &distMap() const { return *_dist;} |
---|
| 691 | |
---|
| 692 | /// \brief Returns a reference to the shortest path tree map. |
---|
| 693 | /// |
---|
| 694 | /// Returns a reference to the matrix node map of the edges of the |
---|
| 695 | /// shortest path tree. |
---|
| 696 | /// \pre \ref run() must be called before using this function. |
---|
| 697 | const PredMap &predMap() const { return *_pred;} |
---|
| 698 | |
---|
| 699 | /// \brief Checks if a node is reachable from the root. |
---|
| 700 | /// |
---|
| 701 | /// Returns \c true if \c v is reachable from the root. |
---|
| 702 | /// \pre \ref run() must be called before using this function. |
---|
| 703 | /// |
---|
| 704 | bool connected(Node source, Node target) { |
---|
| 705 | return (*_dist)(source, target) != OperationTraits::infinity(); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | ///@} |
---|
| 709 | }; |
---|
| 710 | |
---|
| 711 | } //END OF NAMESPACE LEMON |
---|
| 712 | |
---|
| 713 | #endif |
---|