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