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