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