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