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