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