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