lemon/dijkstra.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 12 Dec 2008 21:44:37 +0100
changeset 451 09e416d35896
parent 412 62f9787c516c
parent 421 6b9057cdcd8b
child 463 88ed40ad0d4f
permissions -rw-r--r--
Bug fix in variant.h (#196)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@100
     4
 *
alpar@100
     5
 * Copyright (C) 2003-2008
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
alpar@100
    19
#ifndef LEMON_DIJKSTRA_H
alpar@100
    20
#define LEMON_DIJKSTRA_H
alpar@100
    21
alpar@100
    22
///\ingroup shortest_path
alpar@100
    23
///\file
alpar@100
    24
///\brief Dijkstra algorithm.
alpar@100
    25
alpar@184
    26
#include <limits>
kpeter@169
    27
#include <lemon/list_graph.h>
alpar@100
    28
#include <lemon/bin_heap.h>
alpar@100
    29
#include <lemon/bits/path_dump.h>
deba@220
    30
#include <lemon/core.h>
alpar@100
    31
#include <lemon/error.h>
alpar@100
    32
#include <lemon/maps.h>
kpeter@278
    33
#include <lemon/path.h>
alpar@100
    34
alpar@100
    35
namespace lemon {
alpar@100
    36
kpeter@244
    37
  /// \brief Default operation traits for the Dijkstra algorithm class.
alpar@209
    38
  ///
kpeter@244
    39
  /// This operation traits class defines all computational operations and
kpeter@244
    40
  /// constants which are used in the Dijkstra algorithm.
alpar@100
    41
  template <typename Value>
alpar@100
    42
  struct DijkstraDefaultOperationTraits {
alpar@100
    43
    /// \brief Gives back the zero value of the type.
alpar@100
    44
    static Value zero() {
alpar@100
    45
      return static_cast<Value>(0);
alpar@100
    46
    }
alpar@100
    47
    /// \brief Gives back the sum of the given two elements.
alpar@100
    48
    static Value plus(const Value& left, const Value& right) {
alpar@100
    49
      return left + right;
alpar@100
    50
    }
kpeter@244
    51
    /// \brief Gives back true only if the first value is less than the second.
alpar@100
    52
    static bool less(const Value& left, const Value& right) {
alpar@100
    53
      return left < right;
alpar@100
    54
    }
alpar@100
    55
  };
alpar@100
    56
alpar@100
    57
  ///Default traits class of Dijkstra class.
alpar@100
    58
alpar@100
    59
  ///Default traits class of Dijkstra class.
kpeter@244
    60
  ///\tparam GR The type of the digraph.
kpeter@244
    61
  ///\tparam LM The type of the length map.
alpar@100
    62
  template<class GR, class LM>
alpar@100
    63
  struct DijkstraDefaultTraits
alpar@100
    64
  {
kpeter@244
    65
    ///The type of the digraph the algorithm runs on.
alpar@100
    66
    typedef GR Digraph;
kpeter@244
    67
alpar@100
    68
    ///The type of the map that stores the arc lengths.
alpar@100
    69
alpar@100
    70
    ///The type of the map that stores the arc lengths.
alpar@100
    71
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@100
    72
    typedef LM LengthMap;
kpeter@244
    73
    ///The type of the length of the arcs.
alpar@100
    74
    typedef typename LM::Value Value;
kpeter@244
    75
alpar@100
    76
    /// Operation traits for Dijkstra algorithm.
alpar@100
    77
kpeter@244
    78
    /// This class defines the operations that are used in the algorithm.
alpar@100
    79
    /// \see DijkstraDefaultOperationTraits
alpar@100
    80
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
alpar@100
    81
kpeter@244
    82
    /// The cross reference type used by the heap.
alpar@100
    83
kpeter@244
    84
    /// The cross reference type used by the heap.
alpar@100
    85
    /// Usually it is \c Digraph::NodeMap<int>.
alpar@100
    86
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
kpeter@244
    87
    ///Instantiates a \ref HeapCrossRef.
alpar@100
    88
kpeter@244
    89
    ///This function instantiates a \ref HeapCrossRef.
kpeter@244
    90
    /// \param g is the digraph, to which we would like to define the
kpeter@244
    91
    /// \ref HeapCrossRef.
kpeter@244
    92
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
alpar@100
    93
    {
kpeter@244
    94
      return new HeapCrossRef(g);
alpar@100
    95
    }
alpar@209
    96
kpeter@244
    97
    ///The heap type used by the Dijkstra algorithm.
alpar@100
    98
kpeter@244
    99
    ///The heap type used by the Dijkstra algorithm.
alpar@100
   100
    ///
alpar@100
   101
    ///\sa BinHeap
alpar@100
   102
    ///\sa Dijkstra
alpar@100
   103
    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
kpeter@244
   104
    ///Instantiates a \ref Heap.
alpar@100
   105
kpeter@244
   106
    ///This function instantiates a \ref Heap.
kpeter@244
   107
    static Heap *createHeap(HeapCrossRef& r)
alpar@100
   108
    {
kpeter@244
   109
      return new Heap(r);
alpar@100
   110
    }
alpar@100
   111
kpeter@244
   112
    ///\brief The type of the map that stores the predecessor
alpar@100
   113
    ///arcs of the shortest paths.
alpar@209
   114
    ///
kpeter@244
   115
    ///The type of the map that stores the predecessor
alpar@100
   116
    ///arcs of the shortest paths.
alpar@100
   117
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@244
   118
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
   119
    ///Instantiates a PredMap.
alpar@209
   120
kpeter@301
   121
    ///This function instantiates a PredMap.
kpeter@244
   122
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   123
    ///PredMap.
kpeter@244
   124
    static PredMap *createPredMap(const Digraph &g)
alpar@100
   125
    {
kpeter@244
   126
      return new PredMap(g);
alpar@100
   127
    }
alpar@100
   128
kpeter@244
   129
    ///The type of the map that indicates which nodes are processed.
alpar@209
   130
kpeter@244
   131
    ///The type of the map that indicates which nodes are processed.
alpar@100
   132
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   133
    ///By default it is a NullMap.
alpar@100
   134
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
   135
    ///Instantiates a ProcessedMap.
alpar@209
   136
kpeter@301
   137
    ///This function instantiates a ProcessedMap.
alpar@100
   138
    ///\param g is the digraph, to which
kpeter@301
   139
    ///we would like to define the ProcessedMap
alpar@100
   140
#ifdef DOXYGEN
kpeter@244
   141
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   142
#else
kpeter@244
   143
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   144
#endif
alpar@100
   145
    {
alpar@100
   146
      return new ProcessedMap();
alpar@100
   147
    }
alpar@209
   148
kpeter@244
   149
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   150
kpeter@244
   151
    ///The type of the map that stores the distances of the nodes.
alpar@100
   152
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   153
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
kpeter@301
   154
    ///Instantiates a DistMap.
alpar@209
   155
kpeter@301
   156
    ///This function instantiates a DistMap.
kpeter@244
   157
    ///\param g is the digraph, to which we would like to define
kpeter@301
   158
    ///the DistMap
kpeter@244
   159
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   160
    {
kpeter@244
   161
      return new DistMap(g);
alpar@100
   162
    }
alpar@100
   163
  };
alpar@209
   164
alpar@100
   165
  ///%Dijkstra algorithm class.
alpar@209
   166
alpar@100
   167
  /// \ingroup shortest_path
kpeter@244
   168
  ///This class provides an efficient implementation of the %Dijkstra algorithm.
kpeter@244
   169
  ///
alpar@100
   170
  ///The arc lengths are passed to the algorithm using a
alpar@100
   171
  ///\ref concepts::ReadMap "ReadMap",
alpar@100
   172
  ///so it is easy to change it to any kind of length.
alpar@100
   173
  ///The type of the length is determined by the
alpar@100
   174
  ///\ref concepts::ReadMap::Value "Value" of the length map.
alpar@100
   175
  ///It is also possible to change the underlying priority heap.
alpar@100
   176
  ///
kpeter@278
   177
  ///There is also a \ref dijkstra() "function-type interface" for the
kpeter@244
   178
  ///%Dijkstra algorithm, which is convenient in the simplier cases and
kpeter@244
   179
  ///it can be used easier.
kpeter@244
   180
  ///
kpeter@244
   181
  ///\tparam GR The type of the digraph the algorithm runs on.
kpeter@421
   182
  ///The default type is \ref ListDigraph.
kpeter@421
   183
  ///\tparam LM A \ref concepts::ReadMap "readable" arc map that specifies
kpeter@421
   184
  ///the lengths of the arcs.
kpeter@421
   185
  ///It is read once for each arc, so the map may involve in
kpeter@244
   186
  ///relatively time consuming process to compute the arc lengths if
alpar@100
   187
  ///it is necessary. The default map type is \ref
kpeter@421
   188
  ///concepts::Digraph::ArcMap "GR::ArcMap<int>".
alpar@100
   189
#ifdef DOXYGEN
alpar@100
   190
  template <typename GR, typename LM, typename TR>
alpar@100
   191
#else
alpar@100
   192
  template <typename GR=ListDigraph,
alpar@209
   193
            typename LM=typename GR::template ArcMap<int>,
alpar@209
   194
            typename TR=DijkstraDefaultTraits<GR,LM> >
alpar@100
   195
#endif
alpar@100
   196
  class Dijkstra {
alpar@100
   197
  public:
alpar@100
   198
kpeter@244
   199
    ///The type of the digraph the algorithm runs on.
alpar@100
   200
    typedef typename TR::Digraph Digraph;
alpar@209
   201
alpar@100
   202
    ///The type of the length of the arcs.
alpar@100
   203
    typedef typename TR::LengthMap::Value Value;
alpar@100
   204
    ///The type of the map that stores the arc lengths.
alpar@100
   205
    typedef typename TR::LengthMap LengthMap;
kpeter@244
   206
    ///\brief The type of the map that stores the predecessor arcs of the
kpeter@244
   207
    ///shortest paths.
alpar@100
   208
    typedef typename TR::PredMap PredMap;
kpeter@244
   209
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   210
    typedef typename TR::DistMap DistMap;
kpeter@244
   211
    ///The type of the map that indicates which nodes are processed.
alpar@100
   212
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@244
   213
    ///The type of the paths.
kpeter@244
   214
    typedef PredMapPath<Digraph, PredMap> Path;
alpar@100
   215
    ///The cross reference type used for the current heap.
alpar@100
   216
    typedef typename TR::HeapCrossRef HeapCrossRef;
kpeter@244
   217
    ///The heap type used by the algorithm.
alpar@100
   218
    typedef typename TR::Heap Heap;
kpeter@244
   219
    ///The operation traits class.
alpar@100
   220
    typedef typename TR::OperationTraits OperationTraits;
kpeter@244
   221
kpeter@421
   222
    ///The \ref DijkstraDefaultTraits "traits class" of the algorithm.
kpeter@244
   223
    typedef TR Traits;
kpeter@244
   224
alpar@100
   225
  private:
kpeter@244
   226
kpeter@244
   227
    typedef typename Digraph::Node Node;
kpeter@244
   228
    typedef typename Digraph::NodeIt NodeIt;
kpeter@244
   229
    typedef typename Digraph::Arc Arc;
kpeter@244
   230
    typedef typename Digraph::OutArcIt OutArcIt;
kpeter@244
   231
kpeter@244
   232
    //Pointer to the underlying digraph.
alpar@100
   233
    const Digraph *G;
kpeter@244
   234
    //Pointer to the length map.
alpar@100
   235
    const LengthMap *length;
kpeter@244
   236
    //Pointer to the map of predecessors arcs.
alpar@100
   237
    PredMap *_pred;
kpeter@244
   238
    //Indicates if _pred is locally allocated (true) or not.
alpar@100
   239
    bool local_pred;
kpeter@244
   240
    //Pointer to the map of distances.
alpar@100
   241
    DistMap *_dist;
kpeter@244
   242
    //Indicates if _dist is locally allocated (true) or not.
alpar@100
   243
    bool local_dist;
kpeter@244
   244
    //Pointer to the map of processed status of the nodes.
alpar@100
   245
    ProcessedMap *_processed;
kpeter@244
   246
    //Indicates if _processed is locally allocated (true) or not.
alpar@100
   247
    bool local_processed;
kpeter@244
   248
    //Pointer to the heap cross references.
alpar@100
   249
    HeapCrossRef *_heap_cross_ref;
kpeter@244
   250
    //Indicates if _heap_cross_ref is locally allocated (true) or not.
alpar@100
   251
    bool local_heap_cross_ref;
kpeter@244
   252
    //Pointer to the heap.
alpar@100
   253
    Heap *_heap;
kpeter@244
   254
    //Indicates if _heap is locally allocated (true) or not.
alpar@100
   255
    bool local_heap;
alpar@100
   256
alpar@280
   257
    //Creates the maps if necessary.
alpar@209
   258
    void create_maps()
alpar@100
   259
    {
alpar@100
   260
      if(!_pred) {
alpar@209
   261
        local_pred = true;
alpar@209
   262
        _pred = Traits::createPredMap(*G);
alpar@100
   263
      }
alpar@100
   264
      if(!_dist) {
alpar@209
   265
        local_dist = true;
alpar@209
   266
        _dist = Traits::createDistMap(*G);
alpar@100
   267
      }
alpar@100
   268
      if(!_processed) {
alpar@209
   269
        local_processed = true;
alpar@209
   270
        _processed = Traits::createProcessedMap(*G);
alpar@100
   271
      }
alpar@100
   272
      if (!_heap_cross_ref) {
alpar@209
   273
        local_heap_cross_ref = true;
alpar@209
   274
        _heap_cross_ref = Traits::createHeapCrossRef(*G);
alpar@100
   275
      }
alpar@100
   276
      if (!_heap) {
alpar@209
   277
        local_heap = true;
alpar@209
   278
        _heap = Traits::createHeap(*_heap_cross_ref);
alpar@100
   279
      }
alpar@100
   280
    }
alpar@209
   281
kpeter@244
   282
  public:
alpar@100
   283
alpar@100
   284
    typedef Dijkstra Create;
alpar@209
   285
alpar@100
   286
    ///\name Named template parameters
alpar@100
   287
alpar@100
   288
    ///@{
alpar@100
   289
alpar@100
   290
    template <class T>
kpeter@257
   291
    struct SetPredMapTraits : public Traits {
alpar@100
   292
      typedef T PredMap;
alpar@100
   293
      static PredMap *createPredMap(const Digraph &)
alpar@100
   294
      {
deba@290
   295
        LEMON_ASSERT(false, "PredMap is not initialized");
deba@290
   296
        return 0; // ignore warnings
alpar@100
   297
      }
alpar@100
   298
    };
kpeter@244
   299
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   300
    ///PredMap type.
alpar@100
   301
    ///
kpeter@244
   302
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   303
    ///PredMap type.
kpeter@421
   304
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   305
    template <class T>
kpeter@257
   306
    struct SetPredMap
kpeter@257
   307
      : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
kpeter@257
   308
      typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create;
alpar@100
   309
    };
alpar@209
   310
alpar@100
   311
    template <class T>
kpeter@257
   312
    struct SetDistMapTraits : public Traits {
alpar@100
   313
      typedef T DistMap;
alpar@100
   314
      static DistMap *createDistMap(const Digraph &)
alpar@100
   315
      {
deba@290
   316
        LEMON_ASSERT(false, "DistMap is not initialized");
deba@290
   317
        return 0; // ignore warnings
alpar@100
   318
      }
alpar@100
   319
    };
kpeter@244
   320
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   321
    ///DistMap type.
alpar@100
   322
    ///
kpeter@244
   323
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   324
    ///DistMap type.
kpeter@421
   325
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   326
    template <class T>
kpeter@257
   327
    struct SetDistMap
kpeter@257
   328
      : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
kpeter@257
   329
      typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create;
alpar@100
   330
    };
alpar@209
   331
alpar@100
   332
    template <class T>
kpeter@257
   333
    struct SetProcessedMapTraits : public Traits {
alpar@100
   334
      typedef T ProcessedMap;
kpeter@244
   335
      static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   336
      {
deba@290
   337
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
deba@290
   338
        return 0; // ignore warnings
alpar@100
   339
      }
alpar@100
   340
    };
kpeter@244
   341
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   342
    ///ProcessedMap type.
alpar@100
   343
    ///
kpeter@244
   344
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   345
    ///ProcessedMap type.
kpeter@421
   346
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   347
    template <class T>
kpeter@257
   348
    struct SetProcessedMap
kpeter@257
   349
      : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
kpeter@257
   350
      typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create;
alpar@100
   351
    };
alpar@209
   352
kpeter@257
   353
    struct SetStandardProcessedMapTraits : public Traits {
alpar@100
   354
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
kpeter@244
   355
      static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   356
      {
kpeter@244
   357
        return new ProcessedMap(g);
alpar@100
   358
      }
alpar@100
   359
    };
kpeter@244
   360
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   361
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
alpar@100
   362
    ///
kpeter@244
   363
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   364
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
kpeter@244
   365
    ///If you don't set it explicitly, it will be automatically allocated.
kpeter@257
   366
    struct SetStandardProcessedMap
kpeter@257
   367
      : public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > {
kpeter@257
   368
      typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits >
alpar@210
   369
      Create;
alpar@100
   370
    };
alpar@100
   371
alpar@100
   372
    template <class H, class CR>
kpeter@257
   373
    struct SetHeapTraits : public Traits {
alpar@100
   374
      typedef CR HeapCrossRef;
alpar@100
   375
      typedef H Heap;
alpar@100
   376
      static HeapCrossRef *createHeapCrossRef(const Digraph &) {
deba@290
   377
        LEMON_ASSERT(false, "HeapCrossRef is not initialized");
deba@290
   378
        return 0; // ignore warnings
alpar@100
   379
      }
alpar@209
   380
      static Heap *createHeap(HeapCrossRef &)
alpar@100
   381
      {
deba@290
   382
        LEMON_ASSERT(false, "Heap is not initialized");
deba@290
   383
        return 0; // ignore warnings
alpar@100
   384
      }
alpar@100
   385
    };
alpar@100
   386
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@421
   387
    ///heap and cross reference types
alpar@100
   388
    ///
alpar@209
   389
    ///\ref named-templ-param "Named parameter" for setting heap and cross
kpeter@421
   390
    ///reference types. If this named parameter is used, then external
kpeter@421
   391
    ///heap and cross reference objects must be passed to the algorithm
kpeter@421
   392
    ///using the \ref heap() function before calling \ref run(Node) "run()"
kpeter@421
   393
    ///or \ref init().
kpeter@421
   394
    ///\sa SetStandardHeap
alpar@100
   395
    template <class H, class CR = typename Digraph::template NodeMap<int> >
kpeter@257
   396
    struct SetHeap
kpeter@257
   397
      : public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > {
kpeter@257
   398
      typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create;
alpar@100
   399
    };
alpar@100
   400
alpar@100
   401
    template <class H, class CR>
kpeter@257
   402
    struct SetStandardHeapTraits : public Traits {
alpar@100
   403
      typedef CR HeapCrossRef;
alpar@100
   404
      typedef H Heap;
alpar@100
   405
      static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
alpar@209
   406
        return new HeapCrossRef(G);
alpar@100
   407
      }
alpar@209
   408
      static Heap *createHeap(HeapCrossRef &R)
alpar@100
   409
      {
alpar@209
   410
        return new Heap(R);
alpar@100
   411
      }
alpar@100
   412
    };
alpar@100
   413
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@421
   414
    ///heap and cross reference types with automatic allocation
alpar@100
   415
    ///
alpar@209
   416
    ///\ref named-templ-param "Named parameter" for setting heap and cross
kpeter@421
   417
    ///reference types with automatic allocation.
kpeter@421
   418
    ///They should have standard constructor interfaces to be able to
kpeter@421
   419
    ///automatically created by the algorithm (i.e. the digraph should be
kpeter@421
   420
    ///passed to the constructor of the cross reference and the cross
kpeter@421
   421
    ///reference should be passed to the constructor of the heap).
kpeter@421
   422
    ///However external heap and cross reference objects could also be
kpeter@421
   423
    ///passed to the algorithm using the \ref heap() function before
kpeter@421
   424
    ///calling \ref run(Node) "run()" or \ref init().
kpeter@421
   425
    ///\sa SetHeap
alpar@100
   426
    template <class H, class CR = typename Digraph::template NodeMap<int> >
kpeter@257
   427
    struct SetStandardHeap
kpeter@257
   428
      : public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > {
kpeter@257
   429
      typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> >
alpar@100
   430
      Create;
alpar@100
   431
    };
alpar@100
   432
alpar@100
   433
    template <class T>
kpeter@257
   434
    struct SetOperationTraitsTraits : public Traits {
alpar@100
   435
      typedef T OperationTraits;
alpar@100
   436
    };
alpar@209
   437
alpar@209
   438
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@313
   439
    ///\c OperationTraits type
alpar@100
   440
    ///
kpeter@244
   441
    ///\ref named-templ-param "Named parameter" for setting
kpeter@244
   442
    ///\ref OperationTraits type.
alpar@100
   443
    template <class T>
kpeter@257
   444
    struct SetOperationTraits
kpeter@257
   445
      : public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
kpeter@257
   446
      typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> >
alpar@100
   447
      Create;
alpar@100
   448
    };
alpar@209
   449
alpar@100
   450
    ///@}
alpar@100
   451
alpar@100
   452
  protected:
alpar@100
   453
alpar@100
   454
    Dijkstra() {}
alpar@100
   455
alpar@209
   456
  public:
alpar@209
   457
alpar@100
   458
    ///Constructor.
alpar@209
   459
kpeter@244
   460
    ///Constructor.
kpeter@244
   461
    ///\param _g The digraph the algorithm runs on.
kpeter@244
   462
    ///\param _length The length map used by the algorithm.
kpeter@244
   463
    Dijkstra(const Digraph& _g, const LengthMap& _length) :
kpeter@244
   464
      G(&_g), length(&_length),
alpar@100
   465
      _pred(NULL), local_pred(false),
alpar@100
   466
      _dist(NULL), local_dist(false),
alpar@100
   467
      _processed(NULL), local_processed(false),
alpar@100
   468
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
alpar@100
   469
      _heap(NULL), local_heap(false)
alpar@100
   470
    { }
alpar@209
   471
alpar@100
   472
    ///Destructor.
alpar@209
   473
    ~Dijkstra()
alpar@100
   474
    {
alpar@100
   475
      if(local_pred) delete _pred;
alpar@100
   476
      if(local_dist) delete _dist;
alpar@100
   477
      if(local_processed) delete _processed;
alpar@100
   478
      if(local_heap_cross_ref) delete _heap_cross_ref;
alpar@100
   479
      if(local_heap) delete _heap;
alpar@100
   480
    }
alpar@100
   481
alpar@100
   482
    ///Sets the length map.
alpar@100
   483
alpar@100
   484
    ///Sets the length map.
alpar@100
   485
    ///\return <tt> (*this) </tt>
alpar@209
   486
    Dijkstra &lengthMap(const LengthMap &m)
alpar@100
   487
    {
alpar@100
   488
      length = &m;
alpar@100
   489
      return *this;
alpar@100
   490
    }
alpar@100
   491
kpeter@244
   492
    ///Sets the map that stores the predecessor arcs.
alpar@100
   493
kpeter@244
   494
    ///Sets the map that stores the predecessor arcs.
kpeter@421
   495
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@421
   496
    ///or \ref init(), an instance will be allocated automatically.
kpeter@421
   497
    ///The destructor deallocates this automatically allocated map,
kpeter@421
   498
    ///of course.
alpar@100
   499
    ///\return <tt> (*this) </tt>
alpar@209
   500
    Dijkstra &predMap(PredMap &m)
alpar@100
   501
    {
alpar@100
   502
      if(local_pred) {
alpar@209
   503
        delete _pred;
alpar@209
   504
        local_pred=false;
alpar@100
   505
      }
alpar@100
   506
      _pred = &m;
alpar@100
   507
      return *this;
alpar@100
   508
    }
alpar@100
   509
kpeter@244
   510
    ///Sets the map that indicates which nodes are processed.
alpar@100
   511
kpeter@244
   512
    ///Sets the map that indicates which nodes are processed.
kpeter@421
   513
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@421
   514
    ///or \ref init(), an instance will be allocated automatically.
kpeter@421
   515
    ///The destructor deallocates this automatically allocated map,
kpeter@421
   516
    ///of course.
kpeter@244
   517
    ///\return <tt> (*this) </tt>
kpeter@244
   518
    Dijkstra &processedMap(ProcessedMap &m)
kpeter@244
   519
    {
kpeter@244
   520
      if(local_processed) {
kpeter@244
   521
        delete _processed;
kpeter@244
   522
        local_processed=false;
kpeter@244
   523
      }
kpeter@244
   524
      _processed = &m;
kpeter@244
   525
      return *this;
kpeter@244
   526
    }
kpeter@244
   527
kpeter@244
   528
    ///Sets the map that stores the distances of the nodes.
kpeter@244
   529
kpeter@244
   530
    ///Sets the map that stores the distances of the nodes calculated by the
kpeter@244
   531
    ///algorithm.
kpeter@421
   532
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@421
   533
    ///or \ref init(), an instance will be allocated automatically.
kpeter@421
   534
    ///The destructor deallocates this automatically allocated map,
kpeter@421
   535
    ///of course.
alpar@100
   536
    ///\return <tt> (*this) </tt>
alpar@209
   537
    Dijkstra &distMap(DistMap &m)
alpar@100
   538
    {
alpar@100
   539
      if(local_dist) {
alpar@209
   540
        delete _dist;
alpar@209
   541
        local_dist=false;
alpar@100
   542
      }
alpar@100
   543
      _dist = &m;
alpar@100
   544
      return *this;
alpar@100
   545
    }
alpar@100
   546
alpar@100
   547
    ///Sets the heap and the cross reference used by algorithm.
alpar@100
   548
alpar@100
   549
    ///Sets the heap and the cross reference used by algorithm.
kpeter@421
   550
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@421
   551
    ///or \ref init(), heap and cross reference instances will be
kpeter@421
   552
    ///allocated automatically.
kpeter@421
   553
    ///The destructor deallocates these automatically allocated objects,
kpeter@421
   554
    ///of course.
alpar@100
   555
    ///\return <tt> (*this) </tt>
alpar@100
   556
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
alpar@100
   557
    {
alpar@100
   558
      if(local_heap_cross_ref) {
alpar@209
   559
        delete _heap_cross_ref;
alpar@209
   560
        local_heap_cross_ref=false;
alpar@100
   561
      }
alpar@100
   562
      _heap_cross_ref = &cr;
alpar@100
   563
      if(local_heap) {
alpar@209
   564
        delete _heap;
alpar@209
   565
        local_heap=false;
alpar@100
   566
      }
alpar@100
   567
      _heap = &hp;
alpar@100
   568
      return *this;
alpar@100
   569
    }
alpar@100
   570
alpar@100
   571
  private:
kpeter@244
   572
alpar@100
   573
    void finalizeNodeData(Node v,Value dst)
alpar@100
   574
    {
alpar@100
   575
      _processed->set(v,true);
alpar@100
   576
      _dist->set(v, dst);
alpar@100
   577
    }
alpar@100
   578
alpar@100
   579
  public:
alpar@100
   580
kpeter@421
   581
    ///\name Execution Control
kpeter@421
   582
    ///The simplest way to execute the %Dijkstra algorithm is to use
kpeter@421
   583
    ///one of the member functions called \ref run(Node) "run()".\n
kpeter@421
   584
    ///If you need more control on the execution, first you have to call
kpeter@421
   585
    ///\ref init(), then you can add several source nodes with
kpeter@421
   586
    ///\ref addSource(). Finally the actual path computation can be
kpeter@421
   587
    ///performed with one of the \ref start() functions.
alpar@100
   588
alpar@100
   589
    ///@{
alpar@100
   590
kpeter@421
   591
    ///\brief Initializes the internal data structures.
kpeter@421
   592
    ///
alpar@100
   593
    ///Initializes the internal data structures.
alpar@100
   594
    void init()
alpar@100
   595
    {
alpar@100
   596
      create_maps();
alpar@100
   597
      _heap->clear();
alpar@100
   598
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@209
   599
        _pred->set(u,INVALID);
alpar@209
   600
        _processed->set(u,false);
alpar@209
   601
        _heap_cross_ref->set(u,Heap::PRE_HEAP);
alpar@100
   602
      }
alpar@100
   603
    }
alpar@209
   604
alpar@100
   605
    ///Adds a new source node.
alpar@100
   606
alpar@100
   607
    ///Adds a new source node to the priority heap.
alpar@100
   608
    ///The optional second parameter is the initial distance of the node.
alpar@100
   609
    ///
kpeter@244
   610
    ///The function checks if the node has already been added to the heap and
alpar@100
   611
    ///it is pushed to the heap only if either it was not in the heap
alpar@100
   612
    ///or the shortest path found till then is shorter than \c dst.
alpar@100
   613
    void addSource(Node s,Value dst=OperationTraits::zero())
alpar@100
   614
    {
alpar@100
   615
      if(_heap->state(s) != Heap::IN_HEAP) {
alpar@209
   616
        _heap->push(s,dst);
alpar@100
   617
      } else if(OperationTraits::less((*_heap)[s], dst)) {
alpar@209
   618
        _heap->set(s,dst);
alpar@209
   619
        _pred->set(s,INVALID);
alpar@100
   620
      }
alpar@100
   621
    }
alpar@209
   622
alpar@100
   623
    ///Processes the next node in the priority heap
alpar@100
   624
alpar@100
   625
    ///Processes the next node in the priority heap.
alpar@100
   626
    ///
alpar@100
   627
    ///\return The processed node.
alpar@100
   628
    ///
kpeter@244
   629
    ///\warning The priority heap must not be empty.
alpar@100
   630
    Node processNextNode()
alpar@100
   631
    {
alpar@209
   632
      Node v=_heap->top();
alpar@100
   633
      Value oldvalue=_heap->prio();
alpar@100
   634
      _heap->pop();
alpar@100
   635
      finalizeNodeData(v,oldvalue);
alpar@209
   636
alpar@100
   637
      for(OutArcIt e(*G,v); e!=INVALID; ++e) {
alpar@209
   638
        Node w=G->target(e);
alpar@209
   639
        switch(_heap->state(w)) {
alpar@209
   640
        case Heap::PRE_HEAP:
alpar@209
   641
          _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
alpar@209
   642
          _pred->set(w,e);
alpar@209
   643
          break;
alpar@209
   644
        case Heap::IN_HEAP:
alpar@209
   645
          {
alpar@209
   646
            Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
alpar@209
   647
            if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
alpar@209
   648
              _heap->decrease(w, newvalue);
alpar@209
   649
              _pred->set(w,e);
alpar@209
   650
            }
alpar@209
   651
          }
alpar@209
   652
          break;
alpar@209
   653
        case Heap::POST_HEAP:
alpar@209
   654
          break;
alpar@209
   655
        }
alpar@100
   656
      }
alpar@100
   657
      return v;
alpar@100
   658
    }
alpar@100
   659
kpeter@244
   660
    ///The next node to be processed.
alpar@209
   661
kpeter@244
   662
    ///Returns the next node to be processed or \c INVALID if the
kpeter@244
   663
    ///priority heap is empty.
kpeter@244
   664
    Node nextNode() const
alpar@209
   665
    {
alpar@100
   666
      return !_heap->empty()?_heap->top():INVALID;
alpar@100
   667
    }
alpar@209
   668
kpeter@421
   669
    ///Returns \c false if there are nodes to be processed.
kpeter@421
   670
kpeter@421
   671
    ///Returns \c false if there are nodes to be processed
kpeter@421
   672
    ///in the priority heap.
kpeter@244
   673
    bool emptyQueue() const { return _heap->empty(); }
kpeter@244
   674
kpeter@421
   675
    ///Returns the number of the nodes to be processed.
alpar@100
   676
kpeter@421
   677
    ///Returns the number of the nodes to be processed
kpeter@421
   678
    ///in the priority heap.
kpeter@244
   679
    int queueSize() const { return _heap->size(); }
alpar@209
   680
alpar@100
   681
    ///Executes the algorithm.
alpar@100
   682
alpar@100
   683
    ///Executes the algorithm.
alpar@100
   684
    ///
kpeter@244
   685
    ///This method runs the %Dijkstra algorithm from the root node(s)
kpeter@244
   686
    ///in order to compute the shortest path to each node.
kpeter@244
   687
    ///
kpeter@244
   688
    ///The algorithm computes
kpeter@244
   689
    ///- the shortest path tree (forest),
kpeter@244
   690
    ///- the distance of each node from the root(s).
kpeter@244
   691
    ///
kpeter@244
   692
    ///\pre init() must be called and at least one root node should be
kpeter@244
   693
    ///added with addSource() before using this function.
kpeter@244
   694
    ///
kpeter@244
   695
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
   696
    ///\code
kpeter@244
   697
    ///  while ( !d.emptyQueue() ) {
kpeter@244
   698
    ///    d.processNextNode();
kpeter@244
   699
    ///  }
kpeter@244
   700
    ///\endcode
kpeter@244
   701
    void start()
kpeter@244
   702
    {
kpeter@244
   703
      while ( !emptyQueue() ) processNextNode();
kpeter@244
   704
    }
kpeter@244
   705
kpeter@286
   706
    ///Executes the algorithm until the given target node is processed.
kpeter@244
   707
kpeter@286
   708
    ///Executes the algorithm until the given target node is processed.
alpar@100
   709
    ///
alpar@100
   710
    ///This method runs the %Dijkstra algorithm from the root node(s)
kpeter@286
   711
    ///in order to compute the shortest path to \c t.
alpar@100
   712
    ///
kpeter@244
   713
    ///The algorithm computes
kpeter@286
   714
    ///- the shortest path to \c t,
kpeter@286
   715
    ///- the distance of \c t from the root(s).
alpar@100
   716
    ///
kpeter@244
   717
    ///\pre init() must be called and at least one root node should be
kpeter@244
   718
    ///added with addSource() before using this function.
kpeter@286
   719
    void start(Node t)
alpar@100
   720
    {
kpeter@286
   721
      while ( !_heap->empty() && _heap->top()!=t ) processNextNode();
kpeter@286
   722
      if ( !_heap->empty() ) {
kpeter@286
   723
        finalizeNodeData(_heap->top(),_heap->prio());
kpeter@286
   724
        _heap->pop();
kpeter@286
   725
      }
alpar@100
   726
    }
alpar@209
   727
alpar@100
   728
    ///Executes the algorithm until a condition is met.
alpar@100
   729
alpar@100
   730
    ///Executes the algorithm until a condition is met.
alpar@100
   731
    ///
kpeter@244
   732
    ///This method runs the %Dijkstra algorithm from the root node(s) in
kpeter@244
   733
    ///order to compute the shortest path to a node \c v with
kpeter@244
   734
    /// <tt>nm[v]</tt> true, if such a node can be found.
alpar@100
   735
    ///
kpeter@244
   736
    ///\param nm A \c bool (or convertible) node map. The algorithm
alpar@100
   737
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
alpar@100
   738
    ///
alpar@100
   739
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
alpar@100
   740
    ///\c INVALID if no such node was found.
kpeter@244
   741
    ///
kpeter@244
   742
    ///\pre init() must be called and at least one root node should be
kpeter@244
   743
    ///added with addSource() before using this function.
alpar@100
   744
    template<class NodeBoolMap>
alpar@100
   745
    Node start(const NodeBoolMap &nm)
alpar@100
   746
    {
alpar@100
   747
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
alpar@100
   748
      if ( _heap->empty() ) return INVALID;
alpar@100
   749
      finalizeNodeData(_heap->top(),_heap->prio());
alpar@100
   750
      return _heap->top();
alpar@100
   751
    }
alpar@209
   752
kpeter@286
   753
    ///Runs the algorithm from the given source node.
alpar@209
   754
kpeter@244
   755
    ///This method runs the %Dijkstra algorithm from node \c s
kpeter@244
   756
    ///in order to compute the shortest path to each node.
alpar@100
   757
    ///
kpeter@244
   758
    ///The algorithm computes
kpeter@244
   759
    ///- the shortest path tree,
kpeter@244
   760
    ///- the distance of each node from the root.
kpeter@244
   761
    ///
kpeter@244
   762
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
   763
    ///\code
alpar@100
   764
    ///  d.init();
alpar@100
   765
    ///  d.addSource(s);
alpar@100
   766
    ///  d.start();
alpar@100
   767
    ///\endcode
alpar@100
   768
    void run(Node s) {
alpar@100
   769
      init();
alpar@100
   770
      addSource(s);
alpar@100
   771
      start();
alpar@100
   772
    }
alpar@209
   773
alpar@100
   774
    ///Finds the shortest path between \c s and \c t.
alpar@209
   775
kpeter@244
   776
    ///This method runs the %Dijkstra algorithm from node \c s
kpeter@286
   777
    ///in order to compute the shortest path to node \c t
kpeter@286
   778
    ///(it stops searching when \c t is processed).
alpar@100
   779
    ///
kpeter@286
   780
    ///\return \c true if \c t is reachable form \c s.
kpeter@244
   781
    ///
kpeter@244
   782
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
kpeter@244
   783
    ///shortcut of the following code.
alpar@100
   784
    ///\code
alpar@100
   785
    ///  d.init();
alpar@100
   786
    ///  d.addSource(s);
alpar@100
   787
    ///  d.start(t);
alpar@100
   788
    ///\endcode
kpeter@286
   789
    bool run(Node s,Node t) {
alpar@100
   790
      init();
alpar@100
   791
      addSource(s);
alpar@100
   792
      start(t);
kpeter@286
   793
      return (*_heap_cross_ref)[t] == Heap::POST_HEAP;
alpar@100
   794
    }
alpar@209
   795
alpar@100
   796
    ///@}
alpar@100
   797
alpar@100
   798
    ///\name Query Functions
kpeter@421
   799
    ///The results of the %Dijkstra algorithm can be obtained using these
alpar@100
   800
    ///functions.\n
kpeter@421
   801
    ///Either \ref run(Node) "run()" or \ref start() should be called
kpeter@421
   802
    ///before using them.
alpar@209
   803
alpar@100
   804
    ///@{
alpar@100
   805
kpeter@244
   806
    ///The shortest path to a node.
alpar@209
   807
kpeter@244
   808
    ///Returns the shortest path to a node.
kpeter@244
   809
    ///
kpeter@421
   810
    ///\warning \c t should be reached from the root(s).
kpeter@244
   811
    ///
kpeter@421
   812
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@421
   813
    ///must be called before using this function.
kpeter@244
   814
    Path path(Node t) const { return Path(*G, *_pred, t); }
alpar@100
   815
kpeter@244
   816
    ///The distance of a node from the root(s).
alpar@100
   817
kpeter@244
   818
    ///Returns the distance of a node from the root(s).
kpeter@244
   819
    ///
kpeter@421
   820
    ///\warning If node \c v is not reached from the root(s), then
kpeter@244
   821
    ///the return value of this function is undefined.
kpeter@244
   822
    ///
kpeter@421
   823
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@421
   824
    ///must be called before using this function.
alpar@100
   825
    Value dist(Node v) const { return (*_dist)[v]; }
alpar@100
   826
kpeter@244
   827
    ///Returns the 'previous arc' of the shortest path tree for a node.
alpar@100
   828
kpeter@244
   829
    ///This function returns the 'previous arc' of the shortest path
kpeter@244
   830
    ///tree for the node \c v, i.e. it returns the last arc of a
kpeter@421
   831
    ///shortest path from a root to \c v. It is \c INVALID if \c v
kpeter@421
   832
    ///is not reached from the root(s) or if \c v is a root.
kpeter@244
   833
    ///
kpeter@244
   834
    ///The shortest path tree used here is equal to the shortest path
kpeter@244
   835
    ///tree used in \ref predNode().
kpeter@244
   836
    ///
kpeter@421
   837
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@421
   838
    ///must be called before using this function.
alpar@100
   839
    Arc predArc(Node v) const { return (*_pred)[v]; }
alpar@100
   840
kpeter@244
   841
    ///Returns the 'previous node' of the shortest path tree for a node.
alpar@100
   842
kpeter@244
   843
    ///This function returns the 'previous node' of the shortest path
kpeter@244
   844
    ///tree for the node \c v, i.e. it returns the last but one node
kpeter@421
   845
    ///from a shortest path from a root to \c v. It is \c INVALID
kpeter@421
   846
    ///if \c v is not reached from the root(s) or if \c v is a root.
kpeter@244
   847
    ///
kpeter@244
   848
    ///The shortest path tree used here is equal to the shortest path
kpeter@244
   849
    ///tree used in \ref predArc().
kpeter@244
   850
    ///
kpeter@421
   851
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@421
   852
    ///must be called before using this function.
alpar@100
   853
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@209
   854
                                  G->source((*_pred)[v]); }
alpar@209
   855
kpeter@244
   856
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   857
    ///distances of the nodes.
kpeter@244
   858
    ///
kpeter@244
   859
    ///Returns a const reference to the node map that stores the distances
kpeter@244
   860
    ///of the nodes calculated by the algorithm.
kpeter@244
   861
    ///
kpeter@421
   862
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@244
   863
    ///must be called before using this function.
alpar@100
   864
    const DistMap &distMap() const { return *_dist;}
alpar@209
   865
kpeter@244
   866
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   867
    ///predecessor arcs.
kpeter@244
   868
    ///
kpeter@244
   869
    ///Returns a const reference to the node map that stores the predecessor
kpeter@244
   870
    ///arcs, which form the shortest path tree.
kpeter@244
   871
    ///
kpeter@421
   872
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@244
   873
    ///must be called before using this function.
alpar@100
   874
    const PredMap &predMap() const { return *_pred;}
alpar@209
   875
kpeter@421
   876
    ///Checks if a node is reached from the root(s).
alpar@100
   877
kpeter@421
   878
    ///Returns \c true if \c v is reached from the root(s).
kpeter@421
   879
    ///
kpeter@421
   880
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@244
   881
    ///must be called before using this function.
kpeter@244
   882
    bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
kpeter@244
   883
                                        Heap::PRE_HEAP; }
alpar@100
   884
alpar@100
   885
    ///Checks if a node is processed.
alpar@100
   886
alpar@100
   887
    ///Returns \c true if \c v is processed, i.e. the shortest
alpar@100
   888
    ///path to \c v has already found.
kpeter@421
   889
    ///
kpeter@421
   890
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@244
   891
    ///must be called before using this function.
kpeter@244
   892
    bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
kpeter@244
   893
                                          Heap::POST_HEAP; }
kpeter@244
   894
kpeter@244
   895
    ///The current distance of a node from the root(s).
kpeter@244
   896
kpeter@244
   897
    ///Returns the current distance of a node from the root(s).
kpeter@244
   898
    ///It may be decreased in the following processes.
kpeter@421
   899
    ///
kpeter@421
   900
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@286
   901
    ///must be called before using this function and
kpeter@286
   902
    ///node \c v must be reached but not necessarily processed.
kpeter@286
   903
    Value currentDist(Node v) const {
kpeter@286
   904
      return processed(v) ? (*_dist)[v] : (*_heap)[v];
kpeter@286
   905
    }
alpar@209
   906
alpar@100
   907
    ///@}
alpar@100
   908
  };
alpar@100
   909
alpar@100
   910
kpeter@244
   911
  ///Default traits class of dijkstra() function.
alpar@100
   912
kpeter@244
   913
  ///Default traits class of dijkstra() function.
kpeter@244
   914
  ///\tparam GR The type of the digraph.
kpeter@244
   915
  ///\tparam LM The type of the length map.
alpar@100
   916
  template<class GR, class LM>
alpar@100
   917
  struct DijkstraWizardDefaultTraits
alpar@100
   918
  {
kpeter@244
   919
    ///The type of the digraph the algorithm runs on.
alpar@100
   920
    typedef GR Digraph;
alpar@100
   921
    ///The type of the map that stores the arc lengths.
alpar@100
   922
alpar@100
   923
    ///The type of the map that stores the arc lengths.
alpar@100
   924
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@100
   925
    typedef LM LengthMap;
kpeter@244
   926
    ///The type of the length of the arcs.
alpar@100
   927
    typedef typename LM::Value Value;
kpeter@244
   928
alpar@100
   929
    /// Operation traits for Dijkstra algorithm.
alpar@100
   930
kpeter@244
   931
    /// This class defines the operations that are used in the algorithm.
alpar@100
   932
    /// \see DijkstraDefaultOperationTraits
alpar@100
   933
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
alpar@100
   934
kpeter@244
   935
    /// The cross reference type used by the heap.
alpar@100
   936
kpeter@244
   937
    /// The cross reference type used by the heap.
alpar@100
   938
    /// Usually it is \c Digraph::NodeMap<int>.
alpar@100
   939
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
kpeter@244
   940
    ///Instantiates a \ref HeapCrossRef.
alpar@100
   941
alpar@209
   942
    ///This function instantiates a \ref HeapCrossRef.
kpeter@244
   943
    /// \param g is the digraph, to which we would like to define the
alpar@100
   944
    /// HeapCrossRef.
kpeter@244
   945
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
alpar@100
   946
    {
kpeter@244
   947
      return new HeapCrossRef(g);
alpar@100
   948
    }
alpar@209
   949
kpeter@244
   950
    ///The heap type used by the Dijkstra algorithm.
alpar@100
   951
kpeter@244
   952
    ///The heap type used by the Dijkstra algorithm.
alpar@100
   953
    ///
alpar@100
   954
    ///\sa BinHeap
alpar@100
   955
    ///\sa Dijkstra
kpeter@244
   956
    typedef BinHeap<Value, typename Digraph::template NodeMap<int>,
alpar@209
   957
                    std::less<Value> > Heap;
alpar@100
   958
kpeter@244
   959
    ///Instantiates a \ref Heap.
kpeter@244
   960
kpeter@244
   961
    ///This function instantiates a \ref Heap.
kpeter@244
   962
    /// \param r is the HeapCrossRef which is used.
kpeter@244
   963
    static Heap *createHeap(HeapCrossRef& r)
alpar@100
   964
    {
kpeter@244
   965
      return new Heap(r);
alpar@100
   966
    }
alpar@100
   967
kpeter@244
   968
    ///\brief The type of the map that stores the predecessor
alpar@100
   969
    ///arcs of the shortest paths.
alpar@209
   970
    ///
kpeter@244
   971
    ///The type of the map that stores the predecessor
alpar@100
   972
    ///arcs of the shortest paths.
alpar@100
   973
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   974
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
   975
    ///Instantiates a PredMap.
alpar@209
   976
kpeter@301
   977
    ///This function instantiates a PredMap.
kpeter@244
   978
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   979
    ///PredMap.
kpeter@244
   980
    static PredMap *createPredMap(const Digraph &g)
alpar@100
   981
    {
kpeter@278
   982
      return new PredMap(g);
alpar@100
   983
    }
alpar@209
   984
kpeter@244
   985
    ///The type of the map that indicates which nodes are processed.
kpeter@244
   986
kpeter@244
   987
    ///The type of the map that indicates which nodes are processed.
alpar@100
   988
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   989
    ///By default it is a NullMap.
alpar@100
   990
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
   991
    ///Instantiates a ProcessedMap.
alpar@209
   992
kpeter@301
   993
    ///This function instantiates a ProcessedMap.
alpar@100
   994
    ///\param g is the digraph, to which
kpeter@301
   995
    ///we would like to define the ProcessedMap.
alpar@100
   996
#ifdef DOXYGEN
kpeter@244
   997
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   998
#else
kpeter@244
   999
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
  1000
#endif
alpar@100
  1001
    {
alpar@100
  1002
      return new ProcessedMap();
alpar@100
  1003
    }
alpar@209
  1004
kpeter@244
  1005
    ///The type of the map that stores the distances of the nodes.
kpeter@244
  1006
kpeter@244
  1007
    ///The type of the map that stores the distances of the nodes.
alpar@100
  1008
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
  1009
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
kpeter@301
  1010
    ///Instantiates a DistMap.
alpar@209
  1011
kpeter@301
  1012
    ///This function instantiates a DistMap.
alpar@210
  1013
    ///\param g is the digraph, to which we would like to define
kpeter@301
  1014
    ///the DistMap
kpeter@244
  1015
    static DistMap *createDistMap(const Digraph &g)
alpar@100
  1016
    {
kpeter@278
  1017
      return new DistMap(g);
alpar@100
  1018
    }
kpeter@278
  1019
kpeter@278
  1020
    ///The type of the shortest paths.
kpeter@278
  1021
kpeter@278
  1022
    ///The type of the shortest paths.
kpeter@278
  1023
    ///It must meet the \ref concepts::Path "Path" concept.
kpeter@278
  1024
    typedef lemon::Path<Digraph> Path;
alpar@100
  1025
  };
alpar@209
  1026
kpeter@313
  1027
  /// Default traits class used by DijkstraWizard
alpar@100
  1028
alpar@100
  1029
  /// To make it easier to use Dijkstra algorithm
kpeter@244
  1030
  /// we have created a wizard class.
alpar@100
  1031
  /// This \ref DijkstraWizard class needs default traits,
kpeter@244
  1032
  /// as well as the \ref Dijkstra class.
alpar@100
  1033
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
alpar@100
  1034
  /// \ref DijkstraWizard class.
alpar@100
  1035
  template<class GR,class LM>
alpar@100
  1036
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
alpar@100
  1037
  {
alpar@100
  1038
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
alpar@100
  1039
  protected:
kpeter@244
  1040
    //The type of the nodes in the digraph.
alpar@100
  1041
    typedef typename Base::Digraph::Node Node;
alpar@100
  1042
kpeter@244
  1043
    //Pointer to the digraph the algorithm runs on.
alpar@100
  1044
    void *_g;
kpeter@278
  1045
    //Pointer to the length map.
alpar@100
  1046
    void *_length;
kpeter@251
  1047
    //Pointer to the map of processed nodes.
kpeter@251
  1048
    void *_processed;
kpeter@244
  1049
    //Pointer to the map of predecessors arcs.
alpar@100
  1050
    void *_pred;
kpeter@244
  1051
    //Pointer to the map of distances.
alpar@100
  1052
    void *_dist;
kpeter@278
  1053
    //Pointer to the shortest path to the target node.
kpeter@278
  1054
    void *_path;
kpeter@278
  1055
    //Pointer to the distance of the target node.
kpeter@278
  1056
    void *_di;
alpar@100
  1057
kpeter@244
  1058
  public:
alpar@100
  1059
    /// Constructor.
alpar@209
  1060
alpar@100
  1061
    /// This constructor does not require parameters, therefore it initiates
kpeter@278
  1062
    /// all of the attributes to \c 0.
kpeter@251
  1063
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
kpeter@278
  1064
                           _dist(0), _path(0), _di(0) {}
alpar@100
  1065
alpar@100
  1066
    /// Constructor.
alpar@209
  1067
kpeter@278
  1068
    /// This constructor requires two parameters,
kpeter@278
  1069
    /// others are initiated to \c 0.
kpeter@244
  1070
    /// \param g The digraph the algorithm runs on.
kpeter@244
  1071
    /// \param l The length map.
kpeter@278
  1072
    DijkstraWizardBase(const GR &g,const LM &l) :
alpar@209
  1073
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
alpar@209
  1074
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
kpeter@278
  1075
      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
alpar@100
  1076
alpar@100
  1077
  };
alpar@209
  1078
kpeter@278
  1079
  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
alpar@100
  1080
kpeter@278
  1081
  /// This auxiliary class is created to implement the
kpeter@278
  1082
  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
kpeter@421
  1083
  /// It does not have own \ref run(Node) "run()" method, it uses the
kpeter@421
  1084
  /// functions and features of the plain \ref Dijkstra.
alpar@100
  1085
  ///
kpeter@278
  1086
  /// This class should only be used through the \ref dijkstra() function,
kpeter@278
  1087
  /// which makes it easier to use the algorithm.
alpar@100
  1088
  template<class TR>
alpar@100
  1089
  class DijkstraWizard : public TR
alpar@100
  1090
  {
alpar@100
  1091
    typedef TR Base;
alpar@100
  1092
kpeter@244
  1093
    ///The type of the digraph the algorithm runs on.
alpar@100
  1094
    typedef typename TR::Digraph Digraph;
kpeter@244
  1095
alpar@100
  1096
    typedef typename Digraph::Node Node;
alpar@100
  1097
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1098
    typedef typename Digraph::Arc Arc;
alpar@100
  1099
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
  1100
alpar@100
  1101
    ///The type of the map that stores the arc lengths.
alpar@100
  1102
    typedef typename TR::LengthMap LengthMap;
alpar@100
  1103
    ///The type of the length of the arcs.
alpar@100
  1104
    typedef typename LengthMap::Value Value;
kpeter@244
  1105
    ///\brief The type of the map that stores the predecessor
alpar@100
  1106
    ///arcs of the shortest paths.
alpar@100
  1107
    typedef typename TR::PredMap PredMap;
kpeter@244
  1108
    ///The type of the map that stores the distances of the nodes.
alpar@100
  1109
    typedef typename TR::DistMap DistMap;
kpeter@244
  1110
    ///The type of the map that indicates which nodes are processed.
kpeter@244
  1111
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@278
  1112
    ///The type of the shortest paths
kpeter@278
  1113
    typedef typename TR::Path Path;
alpar@100
  1114
    ///The heap type used by the dijkstra algorithm.
alpar@100
  1115
    typedef typename TR::Heap Heap;
kpeter@244
  1116
alpar@100
  1117
  public:
kpeter@244
  1118
alpar@100
  1119
    /// Constructor.
alpar@100
  1120
    DijkstraWizard() : TR() {}
alpar@100
  1121
alpar@100
  1122
    /// Constructor that requires parameters.
alpar@100
  1123
alpar@100
  1124
    /// Constructor that requires parameters.
alpar@100
  1125
    /// These parameters will be the default values for the traits class.
kpeter@278
  1126
    /// \param g The digraph the algorithm runs on.
kpeter@278
  1127
    /// \param l The length map.
kpeter@278
  1128
    DijkstraWizard(const Digraph &g, const LengthMap &l) :
kpeter@278
  1129
      TR(g,l) {}
alpar@100
  1130
alpar@100
  1131
    ///Copy constructor
alpar@100
  1132
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@100
  1133
alpar@100
  1134
    ~DijkstraWizard() {}
alpar@100
  1135
kpeter@278
  1136
    ///Runs Dijkstra algorithm from the given source node.
alpar@209
  1137
kpeter@278
  1138
    ///This method runs %Dijkstra algorithm from the given source node
kpeter@278
  1139
    ///in order to compute the shortest path to each node.
kpeter@278
  1140
    void run(Node s)
alpar@100
  1141
    {
alpar@209
  1142
      Dijkstra<Digraph,LengthMap,TR>
kpeter@278
  1143
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
kpeter@278
  1144
             *reinterpret_cast<const LengthMap*>(Base::_length));
kpeter@278
  1145
      if (Base::_pred)
kpeter@278
  1146
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
  1147
      if (Base::_dist)
kpeter@278
  1148
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
  1149
      if (Base::_processed)
kpeter@278
  1150
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
  1151
      dijk.run(s);
alpar@100
  1152
    }
alpar@100
  1153
kpeter@278
  1154
    ///Finds the shortest path between \c s and \c t.
alpar@100
  1155
kpeter@278
  1156
    ///This method runs the %Dijkstra algorithm from node \c s
kpeter@278
  1157
    ///in order to compute the shortest path to node \c t
kpeter@278
  1158
    ///(it stops searching when \c t is processed).
kpeter@278
  1159
    ///
kpeter@278
  1160
    ///\return \c true if \c t is reachable form \c s.
kpeter@278
  1161
    bool run(Node s, Node t)
alpar@100
  1162
    {
kpeter@278
  1163
      Dijkstra<Digraph,LengthMap,TR>
kpeter@278
  1164
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
kpeter@278
  1165
             *reinterpret_cast<const LengthMap*>(Base::_length));
kpeter@278
  1166
      if (Base::_pred)
kpeter@278
  1167
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
  1168
      if (Base::_dist)
kpeter@278
  1169
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
  1170
      if (Base::_processed)
kpeter@278
  1171
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
  1172
      dijk.run(s,t);
kpeter@278
  1173
      if (Base::_path)
kpeter@278
  1174
        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
kpeter@278
  1175
      if (Base::_di)
kpeter@278
  1176
        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
kpeter@278
  1177
      return dijk.reached(t);
kpeter@244
  1178
    }
kpeter@244
  1179
alpar@100
  1180
    template<class T>
kpeter@257
  1181
    struct SetPredMapBase : public Base {
alpar@100
  1182
      typedef T PredMap;
alpar@100
  1183
      static PredMap *createPredMap(const Digraph &) { return 0; };
kpeter@257
  1184
      SetPredMapBase(const TR &b) : TR(b) {}
alpar@100
  1185
    };
kpeter@278
  1186
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1187
    ///for setting PredMap object.
alpar@100
  1188
    ///
kpeter@278
  1189
    ///\ref named-func-param "Named parameter"
kpeter@301
  1190
    ///for setting PredMap object.
alpar@100
  1191
    template<class T>
kpeter@257
  1192
    DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
alpar@100
  1193
    {
alpar@100
  1194
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1195
      return DijkstraWizard<SetPredMapBase<T> >(*this);
alpar@100
  1196
    }
alpar@209
  1197
alpar@100
  1198
    template<class T>
kpeter@278
  1199
    struct SetDistMapBase : public Base {
kpeter@278
  1200
      typedef T DistMap;
kpeter@278
  1201
      static DistMap *createDistMap(const Digraph &) { return 0; };
kpeter@278
  1202
      SetDistMapBase(const TR &b) : TR(b) {}
kpeter@278
  1203
    };
kpeter@278
  1204
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1205
    ///for setting DistMap object.
kpeter@278
  1206
    ///
kpeter@278
  1207
    ///\ref named-func-param "Named parameter"
kpeter@301
  1208
    ///for setting DistMap object.
kpeter@278
  1209
    template<class T>
kpeter@278
  1210
    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
kpeter@278
  1211
    {
kpeter@278
  1212
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1213
      return DijkstraWizard<SetDistMapBase<T> >(*this);
kpeter@278
  1214
    }
kpeter@278
  1215
kpeter@278
  1216
    template<class T>
kpeter@257
  1217
    struct SetProcessedMapBase : public Base {
kpeter@244
  1218
      typedef T ProcessedMap;
kpeter@244
  1219
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
kpeter@257
  1220
      SetProcessedMapBase(const TR &b) : TR(b) {}
kpeter@244
  1221
    };
kpeter@278
  1222
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1223
    ///for setting ProcessedMap object.
kpeter@244
  1224
    ///
kpeter@278
  1225
    /// \ref named-func-param "Named parameter"
kpeter@301
  1226
    ///for setting ProcessedMap object.
kpeter@244
  1227
    template<class T>
kpeter@257
  1228
    DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
kpeter@244
  1229
    {
kpeter@244
  1230
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1231
      return DijkstraWizard<SetProcessedMapBase<T> >(*this);
kpeter@244
  1232
    }
kpeter@244
  1233
kpeter@244
  1234
    template<class T>
kpeter@278
  1235
    struct SetPathBase : public Base {
kpeter@278
  1236
      typedef T Path;
kpeter@278
  1237
      SetPathBase(const TR &b) : TR(b) {}
alpar@100
  1238
    };
kpeter@278
  1239
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1240
    ///for getting the shortest path to the target node.
alpar@100
  1241
    ///
kpeter@278
  1242
    ///\ref named-func-param "Named parameter"
kpeter@278
  1243
    ///for getting the shortest path to the target node.
alpar@100
  1244
    template<class T>
kpeter@278
  1245
    DijkstraWizard<SetPathBase<T> > path(const T &t)
alpar@100
  1246
    {
kpeter@278
  1247
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1248
      return DijkstraWizard<SetPathBase<T> >(*this);
kpeter@278
  1249
    }
kpeter@278
  1250
kpeter@278
  1251
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1252
    ///for getting the distance of the target node.
kpeter@278
  1253
    ///
kpeter@278
  1254
    ///\ref named-func-param "Named parameter"
kpeter@278
  1255
    ///for getting the distance of the target node.
kpeter@278
  1256
    DijkstraWizard dist(const Value &d)
kpeter@278
  1257
    {
kpeter@278
  1258
      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
kpeter@278
  1259
      return *this;
alpar@100
  1260
    }
alpar@209
  1261
alpar@100
  1262
  };
alpar@209
  1263
kpeter@278
  1264
  ///Function-type interface for Dijkstra algorithm.
alpar@100
  1265
alpar@100
  1266
  /// \ingroup shortest_path
kpeter@278
  1267
  ///Function-type interface for Dijkstra algorithm.
alpar@100
  1268
  ///
kpeter@278
  1269
  ///This function also has several \ref named-func-param "named parameters",
alpar@100
  1270
  ///they are declared as the members of class \ref DijkstraWizard.
kpeter@278
  1271
  ///The following examples show how to use these parameters.
alpar@100
  1272
  ///\code
kpeter@278
  1273
  ///  // Compute shortest path from node s to each node
kpeter@278
  1274
  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
kpeter@278
  1275
  ///
kpeter@278
  1276
  ///  // Compute shortest path from s to t
kpeter@278
  1277
  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
alpar@100
  1278
  ///\endcode
kpeter@421
  1279
  ///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()"
alpar@100
  1280
  ///to the end of the parameter list.
alpar@100
  1281
  ///\sa DijkstraWizard
alpar@100
  1282
  ///\sa Dijkstra
alpar@100
  1283
  template<class GR, class LM>
alpar@100
  1284
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
kpeter@278
  1285
  dijkstra(const GR &digraph, const LM &length)
alpar@100
  1286
  {
kpeter@278
  1287
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
alpar@100
  1288
  }
alpar@100
  1289
alpar@100
  1290
} //END OF NAMESPACE LEMON
alpar@100
  1291
alpar@100
  1292
#endif