lemon/dijkstra.h
author deba
Wed, 05 Oct 2005 16:45:37 +0000
changeset 1709 a323456bf7c8
parent 1694 6d81e6f7a88d
child 1710 f531c16dd923
permissions -rw-r--r--
Template Named Parameter bugfix
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_DIJKSTRA_H
alpar@921
    18
#define LEMON_DIJKSTRA_H
alpar@255
    19
alpar@758
    20
///\ingroup flowalgs
alpar@255
    21
///\file
alpar@255
    22
///\brief Dijkstra algorithm.
alpar@1283
    23
///
alpar@1283
    24
///\todo getPath() should be implemented! (also for BFS and DFS)
alpar@255
    25
alpar@953
    26
#include <lemon/list_graph.h>
alpar@921
    27
#include <lemon/bin_heap.h>
alpar@921
    28
#include <lemon/invalid.h>
alpar@1119
    29
#include <lemon/error.h>
alpar@1119
    30
#include <lemon/maps.h>
alpar@255
    31
alpar@921
    32
namespace lemon {
jacint@385
    33
alpar@1119
    34
alpar@1151
    35
  
alpar@954
    36
  ///Default traits class of Dijkstra class.
alpar@954
    37
alpar@954
    38
  ///Default traits class of Dijkstra class.
alpar@954
    39
  ///\param GR Graph type.
alpar@954
    40
  ///\param LM Type of length map.
alpar@953
    41
  template<class GR, class LM>
alpar@953
    42
  struct DijkstraDefaultTraits
alpar@953
    43
  {
alpar@954
    44
    ///The graph type the algorithm runs on. 
alpar@953
    45
    typedef GR Graph;
alpar@953
    46
    ///The type of the map that stores the edge lengths.
alpar@953
    47
hegyi@1124
    48
    ///The type of the map that stores the edge lengths.
alpar@967
    49
    ///It must meet the \ref concept::ReadMap "ReadMap" concept.
alpar@953
    50
    typedef LM LengthMap;
alpar@954
    51
    //The type of the length of the edges.
alpar@987
    52
    typedef typename LM::Value Value;
alpar@954
    53
    ///The heap type used by Dijkstra algorithm.
alpar@967
    54
alpar@967
    55
    ///The heap type used by Dijkstra algorithm.
alpar@967
    56
    ///
alpar@967
    57
    ///\sa BinHeap
alpar@967
    58
    ///\sa Dijkstra
deba@1709
    59
    typedef BinHeap<typename Graph::Node, typename LM::Value,
alpar@953
    60
		    typename GR::template NodeMap<int>,
alpar@987
    61
		    std::less<Value> > Heap;
alpar@953
    62
alpar@953
    63
    ///\brief The type of the map that stores the last
alpar@953
    64
    ///edges of the shortest paths.
alpar@953
    65
    /// 
hegyi@1124
    66
    ///The type of the map that stores the last
hegyi@1124
    67
    ///edges of the shortest paths.
alpar@967
    68
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
    69
    ///
alpar@954
    70
    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
alpar@954
    71
    ///Instantiates a PredMap.
alpar@953
    72
 
hegyi@1123
    73
    ///This function instantiates a \ref PredMap. 
hegyi@1123
    74
    ///\param G is the graph, to which we would like to define the PredMap.
alpar@1119
    75
    ///\todo The graph alone may be insufficient for the initialization
alpar@954
    76
    static PredMap *createPredMap(const GR &G) 
alpar@953
    77
    {
alpar@953
    78
      return new PredMap(G);
alpar@953
    79
    }
alpar@1119
    80
alpar@1218
    81
    ///The type of the map that stores whether a nodes is processed.
alpar@1119
    82
 
alpar@1218
    83
    ///The type of the map that stores whether a nodes is processed.
alpar@1119
    84
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1119
    85
    ///By default it is a NullMap.
alpar@1218
    86
    ///\todo If it is set to a real map,
alpar@1218
    87
    ///Dijkstra::processed() should read this.
alpar@1119
    88
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
    89
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
    90
    ///Instantiates a ProcessedMap.
alpar@1119
    91
 
alpar@1218
    92
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
    93
    ///\param g is the graph, to which
alpar@1218
    94
    ///we would like to define the \ref ProcessedMap
alpar@1536
    95
#ifdef DOXYGEN
alpar@1536
    96
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
    97
#else
alpar@1366
    98
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
    99
#endif
alpar@1119
   100
    {
alpar@1218
   101
      return new ProcessedMap();
alpar@1119
   102
    }
alpar@953
   103
    ///The type of the map that stores the dists of the nodes.
alpar@953
   104
 
hegyi@1124
   105
    ///The type of the map that stores the dists of the nodes.
alpar@967
   106
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
   107
    ///
alpar@987
   108
    typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
alpar@954
   109
    ///Instantiates a DistMap.
alpar@953
   110
 
hegyi@1123
   111
    ///This function instantiates a \ref DistMap. 
hegyi@1123
   112
    ///\param G is the graph, to which we would like to define the \ref DistMap
alpar@954
   113
    static DistMap *createDistMap(const GR &G)
alpar@953
   114
    {
alpar@953
   115
      return new DistMap(G);
alpar@953
   116
    }
alpar@953
   117
  };
alpar@953
   118
  
alpar@255
   119
  ///%Dijkstra algorithm class.
alpar@1125
   120
  
alpar@1151
   121
  /// \ingroup flowalgs
alpar@255
   122
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
   123
  ///The edge lengths are passed to the algorithm using a
klao@959
   124
  ///\ref concept::ReadMap "ReadMap",
alpar@255
   125
  ///so it is easy to change it to any kind of length.
alpar@255
   126
  ///
alpar@880
   127
  ///The type of the length is determined by the
alpar@987
   128
  ///\ref concept::ReadMap::Value "Value" of the length map.
alpar@255
   129
  ///
alpar@255
   130
  ///It is also possible to change the underlying priority heap.
alpar@255
   131
  ///
alpar@1218
   132
  ///\param GR The graph type the algorithm runs on. The default value
alpar@1218
   133
  ///is \ref ListGraph. The value of GR is not used directly by
alpar@1218
   134
  ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
alpar@1218
   135
  ///\param LM This read-only EdgeMap determines the lengths of the
alpar@1218
   136
  ///edges. It is read once for each edge, so the map may involve in
alpar@1218
   137
  ///relatively time consuming process to compute the edge length if
alpar@1218
   138
  ///it is necessary. The default map type is \ref
alpar@1218
   139
  ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".  The value
alpar@1218
   140
  ///of LM is not used directly by Dijkstra, it is only passed to \ref
alpar@1218
   141
  ///DijkstraDefaultTraits.  \param TR Traits class to set
alpar@1218
   142
  ///various data types used by the algorithm.  The default traits
alpar@1218
   143
  ///class is \ref DijkstraDefaultTraits
alpar@1218
   144
  ///"DijkstraDefaultTraits<GR,LM>".  See \ref
alpar@1218
   145
  ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
alpar@1218
   146
  ///class.
alpar@456
   147
  ///
alpar@689
   148
  ///\author Jacint Szabo and Alpar Juttner
alpar@1128
   149
  ///\todo A compare object would be nice.
alpar@584
   150
alpar@255
   151
#ifdef DOXYGEN
alpar@584
   152
  template <typename GR,
alpar@584
   153
	    typename LM,
alpar@953
   154
	    typename TR>
alpar@255
   155
#else
alpar@953
   156
  template <typename GR=ListGraph,
alpar@584
   157
	    typename LM=typename GR::template EdgeMap<int>,
alpar@953
   158
	    typename TR=DijkstraDefaultTraits<GR,LM> >
alpar@255
   159
#endif
alpar@1116
   160
  class Dijkstra {
alpar@255
   161
  public:
alpar@1125
   162
    /**
alpar@1125
   163
     * \brief \ref Exception for uninitialized parameters.
alpar@1125
   164
     *
alpar@1125
   165
     * This error represents problems in the initialization
alpar@1125
   166
     * of the parameters of the algorithms.
alpar@1125
   167
     */
alpar@1125
   168
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@1125
   169
    public:
alpar@1125
   170
      virtual const char* exceptionName() const {
alpar@1218
   171
	return "lemon::Dijkstra::UninitializedParameter";
alpar@1125
   172
      }
alpar@1125
   173
    };
alpar@1119
   174
alpar@953
   175
    typedef TR Traits;
alpar@584
   176
    ///The type of the underlying graph.
alpar@954
   177
    typedef typename TR::Graph Graph;
alpar@911
   178
    ///\e
alpar@255
   179
    typedef typename Graph::Node Node;
alpar@911
   180
    ///\e
alpar@255
   181
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   182
    ///\e
alpar@255
   183
    typedef typename Graph::Edge Edge;
alpar@911
   184
    ///\e
alpar@255
   185
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
   186
    
alpar@584
   187
    ///The type of the length of the edges.
alpar@987
   188
    typedef typename TR::LengthMap::Value Value;
alpar@693
   189
    ///The type of the map that stores the edge lengths.
alpar@954
   190
    typedef typename TR::LengthMap LengthMap;
alpar@693
   191
    ///\brief The type of the map that stores the last
alpar@584
   192
    ///edges of the shortest paths.
alpar@953
   193
    typedef typename TR::PredMap PredMap;
alpar@1218
   194
    ///The type of the map indicating if a node is processed.
alpar@1218
   195
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@693
   196
    ///The type of the map that stores the dists of the nodes.
alpar@953
   197
    typedef typename TR::DistMap DistMap;
alpar@953
   198
    ///The heap type used by the dijkstra algorithm.
alpar@953
   199
    typedef typename TR::Heap Heap;
alpar@255
   200
  private:
alpar@802
   201
    /// Pointer to the underlying graph.
alpar@688
   202
    const Graph *G;
alpar@802
   203
    /// Pointer to the length map
alpar@954
   204
    const LengthMap *length;
alpar@802
   205
    ///Pointer to the map of predecessors edges.
alpar@1119
   206
    PredMap *_pred;
alpar@1119
   207
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1119
   208
    bool local_pred;
alpar@802
   209
    ///Pointer to the map of distances.
alpar@1130
   210
    DistMap *_dist;
alpar@1130
   211
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@1130
   212
    bool local_dist;
alpar@1218
   213
    ///Pointer to the map of processed status of the nodes.
alpar@1218
   214
    ProcessedMap *_processed;
alpar@1218
   215
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@1218
   216
    bool local_processed;
alpar@688
   217
alpar@1128
   218
    ///Creates the maps if necessary.
alpar@688
   219
    
alpar@694
   220
    ///\todo Error if \c G or are \c NULL. What about \c length?
alpar@688
   221
    ///\todo Better memory allocation (instead of new).
alpar@1128
   222
    void create_maps() 
alpar@688
   223
    {
alpar@1119
   224
      if(!_pred) {
alpar@1119
   225
	local_pred = true;
alpar@1119
   226
	_pred = Traits::createPredMap(*G);
alpar@688
   227
      }
alpar@1130
   228
      if(!_dist) {
alpar@1130
   229
	local_dist = true;
alpar@1130
   230
	_dist = Traits::createDistMap(*G);
alpar@688
   231
      }
alpar@1218
   232
      if(!_processed) {
alpar@1218
   233
	local_processed = true;
alpar@1218
   234
	_processed = Traits::createProcessedMap(*G);
alpar@1119
   235
      }
alpar@688
   236
    }
alpar@255
   237
    
alpar@255
   238
  public :
alpar@1116
   239
 
alpar@1128
   240
    ///\name Named template parameters
alpar@1128
   241
alpar@1128
   242
    ///@{
alpar@1128
   243
alpar@953
   244
    template <class T>
alpar@1116
   245
    struct DefPredMapTraits : public Traits {
alpar@953
   246
      typedef T PredMap;
alpar@953
   247
      static PredMap *createPredMap(const Graph &G) 
alpar@953
   248
      {
alpar@1126
   249
	throw UninitializedParameter();
alpar@953
   250
      }
alpar@953
   251
    };
alpar@954
   252
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@954
   253
alpar@954
   254
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1043
   255
    ///
alpar@953
   256
    template <class T>
deba@1709
   257
    struct DefPredMap 
deba@1709
   258
      : public Dijkstra< Graph,	LengthMap, DefPredMapTraits<T> > {
deba@1709
   259
      typedef Dijkstra< Graph,	LengthMap, DefPredMapTraits<T> > Create;
deba@1709
   260
    };
alpar@953
   261
    
alpar@953
   262
    template <class T>
alpar@1116
   263
    struct DefDistMapTraits : public Traits {
alpar@953
   264
      typedef T DistMap;
alpar@953
   265
      static DistMap *createDistMap(const Graph &G) 
alpar@953
   266
      {
alpar@1126
   267
	throw UninitializedParameter();
alpar@953
   268
      }
alpar@953
   269
    };
alpar@954
   270
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@954
   271
alpar@954
   272
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1043
   273
    ///
alpar@953
   274
    template <class T>
deba@1709
   275
    struct DefDistMap 
deba@1709
   276
      : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > { 
deba@1709
   277
      typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
deba@1709
   278
    };
alpar@953
   279
    
alpar@1128
   280
    template <class T>
alpar@1218
   281
    struct DefProcessedMapTraits : public Traits {
alpar@1218
   282
      typedef T ProcessedMap;
alpar@1218
   283
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1128
   284
      {
alpar@1128
   285
	throw UninitializedParameter();
alpar@1128
   286
      }
alpar@1128
   287
    };
alpar@1218
   288
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1128
   289
alpar@1218
   290
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1128
   291
    ///
alpar@1128
   292
    template <class T>
deba@1709
   293
    struct DefProcessedMap 
deba@1709
   294
      : public Dijkstra< Graph,	LengthMap, DefProcessedMapTraits<T> > { 
deba@1709
   295
      typedef Dijkstra< Graph,	LengthMap, DefProcessedMapTraits<T> > Create;
deba@1709
   296
    };
alpar@1128
   297
    
alpar@1218
   298
    struct DefGraphProcessedMapTraits : public Traits {
alpar@1218
   299
      typedef typename Graph::template NodeMap<bool> ProcessedMap;
alpar@1218
   300
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1128
   301
      {
alpar@1218
   302
	return new ProcessedMap(G);
alpar@1128
   303
      }
alpar@1128
   304
    };
alpar@1128
   305
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   306
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1128
   307
    ///
alpar@1128
   308
    ///\ref named-templ-param "Named parameter"
alpar@1218
   309
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1128
   310
    ///If you don't set it explicitely, it will be automatically allocated.
alpar@1128
   311
    template <class T>
deba@1709
   312
    struct DefProcessedMapToBeDefaultMap 
deba@1709
   313
      : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
deba@1709
   314
      typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
deba@1709
   315
    };
alpar@1128
   316
    
alpar@1128
   317
    ///@}
alpar@1128
   318
alpar@1128
   319
alpar@1128
   320
  private:
alpar@1128
   321
    typename Graph::template NodeMap<int> _heap_map;
alpar@1128
   322
    Heap _heap;
alpar@1128
   323
  public:      
alpar@1128
   324
    
alpar@802
   325
    ///Constructor.
alpar@255
   326
    
alpar@802
   327
    ///\param _G the graph the algorithm will run on.
alpar@802
   328
    ///\param _length the length map used by the algorithm.
alpar@954
   329
    Dijkstra(const Graph& _G, const LengthMap& _length) :
alpar@688
   330
      G(&_G), length(&_length),
alpar@1119
   331
      _pred(NULL), local_pred(false),
alpar@1130
   332
      _dist(NULL), local_dist(false),
alpar@1218
   333
      _processed(NULL), local_processed(false),
alpar@1128
   334
      _heap_map(*G,-1),_heap(_heap_map)
alpar@688
   335
    { }
alpar@688
   336
    
alpar@802
   337
    ///Destructor.
alpar@688
   338
    ~Dijkstra() 
alpar@688
   339
    {
alpar@1119
   340
      if(local_pred) delete _pred;
alpar@1130
   341
      if(local_dist) delete _dist;
alpar@1218
   342
      if(local_processed) delete _processed;
alpar@688
   343
    }
alpar@688
   344
alpar@688
   345
    ///Sets the length map.
alpar@688
   346
alpar@688
   347
    ///Sets the length map.
alpar@688
   348
    ///\return <tt> (*this) </tt>
alpar@1116
   349
    Dijkstra &lengthMap(const LengthMap &m) 
alpar@688
   350
    {
alpar@688
   351
      length = &m;
alpar@688
   352
      return *this;
alpar@688
   353
    }
alpar@688
   354
alpar@688
   355
    ///Sets the map storing the predecessor edges.
alpar@688
   356
alpar@688
   357
    ///Sets the map storing the predecessor edges.
alpar@688
   358
    ///If you don't use this function before calling \ref run(),
alpar@688
   359
    ///it will allocate one. The destuctor deallocates this
alpar@688
   360
    ///automatically allocated map, of course.
alpar@688
   361
    ///\return <tt> (*this) </tt>
alpar@1116
   362
    Dijkstra &predMap(PredMap &m) 
alpar@688
   363
    {
alpar@1119
   364
      if(local_pred) {
alpar@1119
   365
	delete _pred;
alpar@1119
   366
	local_pred=false;
alpar@688
   367
      }
alpar@1119
   368
      _pred = &m;
alpar@688
   369
      return *this;
alpar@688
   370
    }
alpar@688
   371
alpar@688
   372
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   373
alpar@688
   374
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   375
    ///If you don't use this function before calling \ref run(),
alpar@688
   376
    ///it will allocate one. The destuctor deallocates this
alpar@688
   377
    ///automatically allocated map, of course.
alpar@688
   378
    ///\return <tt> (*this) </tt>
alpar@1116
   379
    Dijkstra &distMap(DistMap &m) 
alpar@688
   380
    {
alpar@1130
   381
      if(local_dist) {
alpar@1130
   382
	delete _dist;
alpar@1130
   383
	local_dist=false;
alpar@688
   384
      }
alpar@1130
   385
      _dist = &m;
alpar@688
   386
      return *this;
alpar@688
   387
    }
alpar@694
   388
alpar@1130
   389
  private:
alpar@1130
   390
    void finalizeNodeData(Node v,Value dst)
alpar@1130
   391
    {
alpar@1218
   392
      _processed->set(v,true);
alpar@1130
   393
      _dist->set(v, dst);
alpar@1130
   394
    }
alpar@1130
   395
alpar@1130
   396
  public:
alpar@1218
   397
    ///\name Execution control
alpar@1128
   398
    ///The simplest way to execute the algorithm is to use
alpar@1156
   399
    ///one of the member functions called \c run(...).
alpar@1128
   400
    ///\n
alpar@1218
   401
    ///If you need more control on the execution,
alpar@1128
   402
    ///first you must call \ref init(), then you can add several source nodes
alpar@1218
   403
    ///with \ref addSource().
alpar@1218
   404
    ///Finally \ref start() will perform the actual path
alpar@1128
   405
    ///computation.
alpar@1128
   406
alpar@1128
   407
    ///@{
alpar@1128
   408
alpar@1128
   409
    ///Initializes the internal data structures.
alpar@1128
   410
alpar@1128
   411
    ///Initializes the internal data structures.
alpar@1128
   412
    ///
alpar@1128
   413
    ///\todo _heap_map's type could also be in the traits class.
alpar@1229
   414
    ///\todo The heaps should be able to make themselves empty directly.
alpar@1128
   415
    void init()
alpar@1128
   416
    {
alpar@1128
   417
      create_maps();
alpar@1229
   418
      while(!_heap.empty()) _heap.pop();
alpar@774
   419
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1119
   420
	_pred->set(u,INVALID);
alpar@1218
   421
	_processed->set(u,false);
alpar@1128
   422
	_heap_map.set(u,Heap::PRE_HEAP);
alpar@694
   423
      }
alpar@1128
   424
    }
alpar@1128
   425
    
alpar@1128
   426
    ///Adds a new source node.
alpar@1128
   427
alpar@1155
   428
    ///Adds a new source node to the priority heap.
alpar@1128
   429
    ///
alpar@1128
   430
    ///The optional second parameter is the initial distance of the node.
alpar@1128
   431
    ///
alpar@1155
   432
    ///It checks if the node has already been added to the heap and
alpar@1155
   433
    ///It is pushed to the heap only if either it was not in the heap
alpar@1155
   434
    ///or the shortest path found till then is longer then \c dst.
alpar@1128
   435
    void addSource(Node s,Value dst=0)
alpar@1128
   436
    {
alpar@1128
   437
      if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
alpar@1155
   438
      else if(_heap[s]<dst) {
alpar@1155
   439
	_heap.push(s,dst);
alpar@1155
   440
	_pred->set(s,INVALID);
alpar@1155
   441
      }
alpar@1128
   442
    }
alpar@1128
   443
    
alpar@1155
   444
    ///Processes the next node in the priority heap
alpar@1155
   445
alpar@1155
   446
    ///Processes the next node in the priority heap.
alpar@1155
   447
    ///
alpar@1516
   448
    ///\return The processed node.
alpar@1516
   449
    ///
alpar@1155
   450
    ///\warning The priority heap must not be empty!
alpar@1516
   451
    Node processNextNode()
alpar@1128
   452
    {
alpar@1128
   453
      Node v=_heap.top(); 
alpar@1128
   454
      Value oldvalue=_heap[v];
alpar@1128
   455
      _heap.pop();
alpar@1130
   456
      finalizeNodeData(v,oldvalue);
alpar@694
   457
      
alpar@1128
   458
      for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@1128
   459
	Node w=G->target(e); 
alpar@1128
   460
	switch(_heap.state(w)) {
alpar@1128
   461
	case Heap::PRE_HEAP:
alpar@1128
   462
	  _heap.push(w,oldvalue+(*length)[e]); 
alpar@1128
   463
	  _pred->set(w,e);
alpar@1128
   464
	  break;
alpar@1128
   465
	case Heap::IN_HEAP:
alpar@1128
   466
	  if ( oldvalue+(*length)[e] < _heap[w] ) {
alpar@1128
   467
	    _heap.decrease(w, oldvalue+(*length)[e]); 
alpar@1119
   468
	    _pred->set(w,e);
alpar@694
   469
	  }
alpar@1128
   470
	  break;
alpar@1128
   471
	case Heap::POST_HEAP:
alpar@1128
   472
	  break;
alpar@694
   473
	}
alpar@694
   474
      }
alpar@1516
   475
      return v;
alpar@694
   476
    }
alpar@1128
   477
alpar@1665
   478
    ///Next node to be processed.
alpar@1665
   479
    
alpar@1665
   480
    ///Next node to be processed.
alpar@1665
   481
    ///
alpar@1665
   482
    ///\return The next node to be processed or INVALID if the priority heap
alpar@1665
   483
    /// is empty.
deba@1694
   484
    Node nextNode()
alpar@1665
   485
    { 
alpar@1665
   486
      return _heap.empty()?_heap.top():INVALID;
alpar@1665
   487
    }
alpar@1665
   488
 
alpar@1218
   489
    ///\brief Returns \c false if there are nodes
alpar@1218
   490
    ///to be processed in the priority heap
alpar@1155
   491
    ///
alpar@1218
   492
    ///Returns \c false if there are nodes
alpar@1218
   493
    ///to be processed in the priority heap
alpar@1218
   494
    bool emptyQueue() { return _heap.empty(); }
alpar@1155
   495
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   496
alpar@1155
   497
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   498
    ///
alpar@1218
   499
    int queueSize() { return _heap.size(); }
alpar@1155
   500
    
alpar@1130
   501
    ///Executes the algorithm.
alpar@1128
   502
alpar@1130
   503
    ///Executes the algorithm.
alpar@1128
   504
    ///
alpar@1130
   505
    ///\pre init() must be called and at least one node should be added
alpar@1130
   506
    ///with addSource() before using this function.
alpar@1128
   507
    ///
alpar@1128
   508
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   509
    ///in order to
alpar@1128
   510
    ///compute the
alpar@1128
   511
    ///shortest path to each node. The algorithm computes
alpar@1128
   512
    ///- The shortest path tree.
alpar@1128
   513
    ///- The distance of each node from the root(s).
alpar@1128
   514
    ///
alpar@1128
   515
    void start()
alpar@1128
   516
    {
alpar@1151
   517
      while ( !_heap.empty() ) processNextNode();
alpar@1128
   518
    }
alpar@255
   519
    
alpar@1130
   520
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   521
alpar@1130
   522
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   523
    ///
alpar@1130
   524
    ///\pre init() must be called and at least one node should be added
alpar@1130
   525
    ///with addSource() before using this function.
alpar@1128
   526
    ///
alpar@1128
   527
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   528
    ///in order to
alpar@1128
   529
    ///compute the
alpar@1128
   530
    ///shortest path to \c dest. The algorithm computes
alpar@1128
   531
    ///- The shortest path to \c  dest.
alpar@1128
   532
    ///- The distance of \c dest from the root(s).
alpar@1128
   533
    ///
alpar@1128
   534
    void start(Node dest)
alpar@1128
   535
    {
alpar@1151
   536
      while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
alpar@1229
   537
      if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
alpar@1130
   538
    }
alpar@1130
   539
    
alpar@1130
   540
    ///Executes the algorithm until a condition is met.
alpar@1130
   541
alpar@1130
   542
    ///Executes the algorithm until a condition is met.
alpar@1130
   543
    ///
alpar@1130
   544
    ///\pre init() must be called and at least one node should be added
alpar@1130
   545
    ///with addSource() before using this function.
alpar@1130
   546
    ///
alpar@1130
   547
    ///\param nm must be a bool (or convertible) node map. The algorithm
alpar@1130
   548
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@1345
   549
    template<class NodeBoolMap>
deba@1345
   550
    void start(const NodeBoolMap &nm)
alpar@1130
   551
    {
deba@1193
   552
      while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
alpar@1229
   553
      if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
alpar@1128
   554
    }
alpar@1128
   555
    
alpar@1128
   556
    ///Runs %Dijkstra algorithm from node \c s.
alpar@1128
   557
    
alpar@1128
   558
    ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@1128
   559
    ///in order to
alpar@1128
   560
    ///compute the
alpar@1128
   561
    ///shortest path to each node. The algorithm computes
alpar@1128
   562
    ///- The shortest path tree.
alpar@1128
   563
    ///- The distance of each node from the root.
alpar@1128
   564
    ///
alpar@1128
   565
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1128
   566
    ///\code
alpar@1128
   567
    ///  d.init();
alpar@1128
   568
    ///  d.addSource(s);
alpar@1128
   569
    ///  d.start();
alpar@1128
   570
    ///\endcode
alpar@1128
   571
    void run(Node s) {
alpar@1128
   572
      init();
alpar@1128
   573
      addSource(s);
alpar@1128
   574
      start();
alpar@1128
   575
    }
alpar@1128
   576
    
alpar@1130
   577
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   578
    
alpar@1130
   579
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   580
    ///
alpar@1130
   581
    ///\return The length of the shortest s---t path if there exists one,
alpar@1130
   582
    ///0 otherwise.
alpar@1130
   583
    ///\note Apart from the return value, d.run(s) is
alpar@1130
   584
    ///just a shortcut of the following code.
alpar@1130
   585
    ///\code
alpar@1130
   586
    ///  d.init();
alpar@1130
   587
    ///  d.addSource(s);
alpar@1130
   588
    ///  d.start(t);
alpar@1130
   589
    ///\endcode
alpar@1130
   590
    Value run(Node s,Node t) {
alpar@1130
   591
      init();
alpar@1130
   592
      addSource(s);
alpar@1130
   593
      start(t);
alpar@1130
   594
      return (*_pred)[t]==INVALID?0:(*_dist)[t];
alpar@1130
   595
    }
alpar@1130
   596
    
alpar@1128
   597
    ///@}
alpar@1128
   598
alpar@1128
   599
    ///\name Query Functions
alpar@1128
   600
    ///The result of the %Dijkstra algorithm can be obtained using these
alpar@1128
   601
    ///functions.\n
alpar@1128
   602
    ///Before the use of these functions,
alpar@1128
   603
    ///either run() or start() must be called.
alpar@1128
   604
    
alpar@1128
   605
    ///@{
alpar@1128
   606
alpar@1283
   607
    ///Copies the shortest path to \c t into \c p
alpar@1283
   608
    
alpar@1283
   609
    ///This function copies the shortest path to \c t into \c p.
alpar@1536
   610
    ///If it \c t is a source itself or unreachable, then it does not
alpar@1283
   611
    ///alter \c p.
alpar@1283
   612
    ///\todo Is it the right way to handle unreachable nodes?
alpar@1283
   613
    ///\return Returns \c true if a path to \c t was actually copied to \c p,
alpar@1283
   614
    ///\c false otherwise.
alpar@1283
   615
    ///\sa DirPath
alpar@1283
   616
    template<class P>
alpar@1283
   617
    bool getPath(P &p,Node t) 
alpar@1283
   618
    {
alpar@1283
   619
      if(reached(t)) {
alpar@1283
   620
	p.clear();
alpar@1283
   621
	typename P::Builder b(p);
alpar@1283
   622
	for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
alpar@1283
   623
	  b.pushFront(pred(t));
alpar@1283
   624
	b.commit();
alpar@1283
   625
	return true;
alpar@1283
   626
      }
alpar@1283
   627
      return false;
alpar@1283
   628
    }
alpar@1283
   629
	  
jacint@385
   630
    ///The distance of a node from the root.
alpar@255
   631
jacint@385
   632
    ///Returns the distance of a node from the root.
alpar@255
   633
    ///\pre \ref run() must be called before using this function.
jacint@385
   634
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   635
    ///of this funcion is undefined.
alpar@1130
   636
    Value dist(Node v) const { return (*_dist)[v]; }
jacint@373
   637
alpar@584
   638
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   639
alpar@584
   640
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@785
   641
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@688
   642
    ///v. It is \ref INVALID
alpar@688
   643
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   644
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@1631
   645
    ///\ref predNode().  \pre \ref run() must be called before using
jacint@385
   646
    ///this function.
alpar@780
   647
    ///\todo predEdge could be a better name.
alpar@1119
   648
    Edge pred(Node v) const { return (*_pred)[v]; }
jacint@373
   649
alpar@584
   650
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   651
alpar@584
   652
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   653
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   654
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   655
    ///\c v=s. The shortest path tree used here is equal to the shortest path
alpar@1631
   656
    ///tree used in \ref pred().  \pre \ref run() must be called before
jacint@385
   657
    ///using this function.
alpar@1130
   658
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1130
   659
				  G->source((*_pred)[v]); }
alpar@255
   660
    
alpar@255
   661
    ///Returns a reference to the NodeMap of distances.
alpar@255
   662
jacint@385
   663
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   664
    ///be called before using this function.
alpar@1130
   665
    const DistMap &distMap() const { return *_dist;}
jacint@385
   666
 
alpar@255
   667
    ///Returns a reference to the shortest path tree map.
alpar@255
   668
alpar@255
   669
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   670
    ///shortest path tree.
alpar@255
   671
    ///\pre \ref run() must be called before using this function.
alpar@1119
   672
    const PredMap &predMap() const { return *_pred;}
jacint@385
   673
 
jacint@385
   674
    ///Checks if a node is reachable from the root.
alpar@255
   675
jacint@385
   676
    ///Returns \c true if \c v is reachable from the root.
alpar@1218
   677
    ///\warning The source nodes are inditated as unreached.
alpar@255
   678
    ///\pre \ref run() must be called before using this function.
jacint@385
   679
    ///
alpar@1218
   680
    bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
alpar@255
   681
    
alpar@1128
   682
    ///@}
alpar@255
   683
  };
alpar@953
   684
alpar@1218
   685
alpar@1218
   686
alpar@1218
   687
alpar@1218
   688
 
alpar@1218
   689
  ///Default traits class of Dijkstra function.
alpar@1218
   690
alpar@1218
   691
  ///Default traits class of Dijkstra function.
alpar@1218
   692
  ///\param GR Graph type.
alpar@1218
   693
  ///\param LM Type of length map.
alpar@1218
   694
  template<class GR, class LM>
alpar@1218
   695
  struct DijkstraWizardDefaultTraits
alpar@1218
   696
  {
alpar@1218
   697
    ///The graph type the algorithm runs on. 
alpar@1218
   698
    typedef GR Graph;
alpar@1218
   699
    ///The type of the map that stores the edge lengths.
alpar@1218
   700
alpar@1218
   701
    ///The type of the map that stores the edge lengths.
alpar@1218
   702
    ///It must meet the \ref concept::ReadMap "ReadMap" concept.
alpar@1218
   703
    typedef LM LengthMap;
alpar@1218
   704
    //The type of the length of the edges.
alpar@1218
   705
    typedef typename LM::Value Value;
alpar@1218
   706
    ///The heap type used by Dijkstra algorithm.
alpar@1218
   707
alpar@1218
   708
    ///The heap type used by Dijkstra algorithm.
alpar@1218
   709
    ///
alpar@1218
   710
    ///\sa BinHeap
alpar@1218
   711
    ///\sa Dijkstra
alpar@1218
   712
    typedef BinHeap<typename Graph::Node,
alpar@1218
   713
		    typename LM::Value,
alpar@1218
   714
		    typename GR::template NodeMap<int>,
alpar@1218
   715
		    std::less<Value> > Heap;
alpar@1218
   716
alpar@1218
   717
    ///\brief The type of the map that stores the last
alpar@1218
   718
    ///edges of the shortest paths.
alpar@1218
   719
    /// 
alpar@1218
   720
    ///The type of the map that stores the last
alpar@1218
   721
    ///edges of the shortest paths.
alpar@1218
   722
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   723
    ///
alpar@1218
   724
    typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
alpar@1218
   725
    ///Instantiates a PredMap.
alpar@1218
   726
 
alpar@1218
   727
    ///This function instantiates a \ref PredMap. 
alpar@1536
   728
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   729
    ///\todo The graph alone may be insufficient for the initialization
alpar@1536
   730
#ifdef DOXYGEN
alpar@1536
   731
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   732
#else
alpar@1367
   733
    static PredMap *createPredMap(const GR &) 
alpar@1536
   734
#endif
alpar@1218
   735
    {
alpar@1218
   736
      return new PredMap();
alpar@1218
   737
    }
alpar@1218
   738
    ///The type of the map that stores whether a nodes is processed.
alpar@1218
   739
 
alpar@1218
   740
    ///The type of the map that stores whether a nodes is processed.
alpar@1218
   741
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   742
    ///By default it is a NullMap.
alpar@1218
   743
    ///\todo If it is set to a real map,
alpar@1218
   744
    ///Dijkstra::processed() should read this.
alpar@1218
   745
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   746
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   747
    ///Instantiates a ProcessedMap.
alpar@1218
   748
 
alpar@1218
   749
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   750
    ///\param g is the graph, to which
alpar@1218
   751
    ///we would like to define the \ref ProcessedMap
alpar@1536
   752
#ifdef DOXYGEN
alpar@1536
   753
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   754
#else
alpar@1367
   755
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   756
#endif
alpar@1218
   757
    {
alpar@1218
   758
      return new ProcessedMap();
alpar@1218
   759
    }
alpar@1218
   760
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   761
 
alpar@1218
   762
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   763
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   764
    ///
alpar@1218
   765
    typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
alpar@1218
   766
    ///Instantiates a DistMap.
alpar@1218
   767
 
alpar@1218
   768
    ///This function instantiates a \ref DistMap. 
alpar@1536
   769
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   770
#ifdef DOXYGEN
alpar@1536
   771
    static DistMap *createDistMap(const GR &g)
alpar@1536
   772
#else
alpar@1367
   773
    static DistMap *createDistMap(const GR &)
alpar@1536
   774
#endif
alpar@1218
   775
    {
alpar@1218
   776
      return new DistMap();
alpar@1218
   777
    }
alpar@1218
   778
  };
alpar@1218
   779
  
hegyi@1123
   780
  /// Default traits used by \ref DijkstraWizard
hegyi@1123
   781
alpar@1151
   782
  /// To make it easier to use Dijkstra algorithm
alpar@1151
   783
  ///we have created a wizard class.
alpar@1151
   784
  /// This \ref DijkstraWizard class needs default traits,
alpar@1151
   785
  ///as well as the \ref Dijkstra class.
hegyi@1123
   786
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
hegyi@1123
   787
  /// \ref DijkstraWizard class.
alpar@1220
   788
  /// \todo More named parameters are required...
alpar@1116
   789
  template<class GR,class LM>
alpar@1218
   790
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
alpar@1116
   791
  {
alpar@1116
   792
alpar@1218
   793
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
alpar@1116
   794
  protected:
alpar@1201
   795
    /// Type of the nodes in the graph.
alpar@1201
   796
    typedef typename Base::Graph::Node Node;
alpar@1201
   797
alpar@1116
   798
    /// Pointer to the underlying graph.
alpar@1116
   799
    void *_g;
alpar@1116
   800
    /// Pointer to the length map
alpar@1116
   801
    void *_length;
alpar@1116
   802
    ///Pointer to the map of predecessors edges.
alpar@1116
   803
    void *_pred;
alpar@1218
   804
//     ///Pointer to the map of predecessors nodes.
alpar@1218
   805
//     void *_predNode;
alpar@1116
   806
    ///Pointer to the map of distances.
alpar@1116
   807
    void *_dist;
alpar@1116
   808
    ///Pointer to the source node.
alpar@1201
   809
    Node _source;
alpar@1116
   810
alpar@1116
   811
    public:
hegyi@1123
   812
    /// Constructor.
hegyi@1123
   813
    
hegyi@1123
   814
    /// This constructor does not require parameters, therefore it initiates
hegyi@1123
   815
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   816
    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
alpar@1218
   817
// 			   _predNode(0),
alpar@1218
   818
			   _dist(0), _source(INVALID) {}
alpar@1116
   819
hegyi@1123
   820
    /// Constructor.
hegyi@1123
   821
    
alpar@1156
   822
    /// This constructor requires some parameters,
alpar@1156
   823
    /// listed in the parameters list.
hegyi@1123
   824
    /// Others are initiated to 0.
hegyi@1123
   825
    /// \param g is the initial value of  \ref _g
hegyi@1123
   826
    /// \param l is the initial value of  \ref _length
hegyi@1123
   827
    /// \param s is the initial value of  \ref _source
alpar@1116
   828
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
alpar@1218
   829
      _g((void *)&g), _length((void *)&l), _pred(0),
alpar@1218
   830
//       _predNode(0),
alpar@1218
   831
      _dist(0), _source(s) {}
alpar@1116
   832
alpar@1116
   833
  };
alpar@1116
   834
  
alpar@1229
   835
  /// A class to make the usage of Dijkstra algorithm easier
alpar@953
   836
hegyi@1123
   837
  /// This class is created to make it easier to use Dijkstra algorithm.
hegyi@1123
   838
  /// It uses the functions and features of the plain \ref Dijkstra,
alpar@1151
   839
  /// but it is much simpler to use it.
alpar@953
   840
  ///
hegyi@1123
   841
  /// Simplicity means that the way to change the types defined
hegyi@1123
   842
  /// in the traits class is based on functions that returns the new class
alpar@1151
   843
  /// and not on templatable built-in classes.
alpar@1151
   844
  /// When using the plain \ref Dijkstra
alpar@1151
   845
  /// the new class with the modified type comes from
alpar@1151
   846
  /// the original class by using the ::
alpar@1151
   847
  /// operator. In the case of \ref DijkstraWizard only
alpar@1151
   848
  /// a function have to be called and it will
hegyi@1123
   849
  /// return the needed class.
hegyi@1123
   850
  ///
hegyi@1123
   851
  /// It does not have own \ref run method. When its \ref run method is called
hegyi@1123
   852
  /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
hegyi@1123
   853
  /// method of it.
alpar@953
   854
  template<class TR>
alpar@1116
   855
  class DijkstraWizard : public TR
alpar@953
   856
  {
alpar@1116
   857
    typedef TR Base;
alpar@953
   858
hegyi@1123
   859
    ///The type of the underlying graph.
alpar@953
   860
    typedef typename TR::Graph Graph;
alpar@1119
   861
    //\e
alpar@953
   862
    typedef typename Graph::Node Node;
alpar@1119
   863
    //\e
alpar@953
   864
    typedef typename Graph::NodeIt NodeIt;
alpar@1119
   865
    //\e
alpar@953
   866
    typedef typename Graph::Edge Edge;
alpar@1119
   867
    //\e
alpar@953
   868
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
   869
    
hegyi@1123
   870
    ///The type of the map that stores the edge lengths.
alpar@953
   871
    typedef typename TR::LengthMap LengthMap;
hegyi@1123
   872
    ///The type of the length of the edges.
alpar@987
   873
    typedef typename LengthMap::Value Value;
hegyi@1123
   874
    ///\brief The type of the map that stores the last
hegyi@1123
   875
    ///edges of the shortest paths.
alpar@953
   876
    typedef typename TR::PredMap PredMap;
alpar@1218
   877
//     ///\brief The type of the map that stores the last but one
alpar@1218
   878
//     ///nodes of the shortest paths.
alpar@1218
   879
//     typedef typename TR::PredNodeMap PredNodeMap;
hegyi@1123
   880
    ///The type of the map that stores the dists of the nodes.
alpar@953
   881
    typedef typename TR::DistMap DistMap;
alpar@953
   882
hegyi@1123
   883
    ///The heap type used by the dijkstra algorithm.
alpar@953
   884
    typedef typename TR::Heap Heap;
alpar@1116
   885
public:
hegyi@1123
   886
    /// Constructor.
alpar@1116
   887
    DijkstraWizard() : TR() {}
alpar@953
   888
hegyi@1123
   889
    /// Constructor that requires parameters.
hegyi@1124
   890
hegyi@1124
   891
    /// Constructor that requires parameters.
hegyi@1123
   892
    /// These parameters will be the default values for the traits class.
alpar@1116
   893
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
   894
      TR(g,l,s) {}
alpar@953
   895
hegyi@1123
   896
    ///Copy constructor
alpar@1116
   897
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
   898
alpar@1116
   899
    ~DijkstraWizard() {}
alpar@1116
   900
hegyi@1123
   901
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
   902
    
hegyi@1123
   903
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
   904
    ///The node can be given by the \ref source function.
alpar@1116
   905
    void run()
alpar@953
   906
    {
alpar@1201
   907
      if(Base::_source==INVALID) throw UninitializedParameter();
deba@1193
   908
      Dijkstra<Graph,LengthMap,TR> 
deba@1345
   909
	dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
deba@1345
   910
      if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
alpar@1218
   911
//       if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
deba@1345
   912
      if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
deba@1345
   913
      dij.run(Base::_source);
alpar@1116
   914
    }
alpar@1116
   915
hegyi@1124
   916
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
   917
hegyi@1124
   918
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
   919
    ///\param s is the given source.
alpar@1116
   920
    void run(Node s)
alpar@1116
   921
    {
alpar@1201
   922
      Base::_source=s;
alpar@1116
   923
      run();
alpar@953
   924
    }
alpar@953
   925
alpar@953
   926
    template<class T>
alpar@1116
   927
    struct DefPredMapBase : public Base {
alpar@1116
   928
      typedef T PredMap;
alpar@1367
   929
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   930
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1116
   931
    };
alpar@953
   932
    
alpar@1156
   933
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
   934
    ///function for setting PredMap type
alpar@1156
   935
    ///
alpar@1156
   936
    /// \ref named-templ-param "Named parameter"
alpar@1156
   937
    ///function for setting PredMap type
hegyi@1124
   938
    ///
alpar@953
   939
    template<class T>
alpar@1116
   940
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
   941
    {
deba@1193
   942
      Base::_pred=(void *)&t;
alpar@1116
   943
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
   944
    }
alpar@953
   945
    
alpar@1116
   946
alpar@1218
   947
//     template<class T>
alpar@1218
   948
//     struct DefPredNodeMapBase : public Base {
alpar@1218
   949
//       typedef T PredNodeMap;
alpar@1218
   950
//       static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
alpar@1236
   951
//       DefPredNodeMapBase(const TR &b) : TR(b) {}
alpar@1218
   952
//     };
alpar@1116
   953
    
alpar@1218
   954
//     ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   955
//     ///function for setting PredNodeMap type
alpar@1218
   956
//     ///
alpar@1218
   957
//     /// \ref named-templ-param "Named parameter"
alpar@1218
   958
//     ///function for setting PredNodeMap type
alpar@1218
   959
//     ///
alpar@1218
   960
//     template<class T>
alpar@1218
   961
//     DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
alpar@1218
   962
//     {
alpar@1218
   963
//       Base::_predNode=(void *)&t;
alpar@1218
   964
//       return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
alpar@1218
   965
//     }
alpar@1116
   966
   
alpar@1116
   967
    template<class T>
alpar@1116
   968
    struct DefDistMapBase : public Base {
alpar@1116
   969
      typedef T DistMap;
alpar@1367
   970
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
   971
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1116
   972
    };
alpar@953
   973
    
alpar@1156
   974
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
   975
    ///function for setting DistMap type
alpar@1156
   976
    ///
alpar@1156
   977
    /// \ref named-templ-param "Named parameter"
alpar@1156
   978
    ///function for setting DistMap type
hegyi@1124
   979
    ///
alpar@953
   980
    template<class T>
alpar@1116
   981
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
   982
    {
deba@1193
   983
      Base::_dist=(void *)&t;
alpar@1116
   984
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
   985
    }
alpar@1117
   986
    
hegyi@1123
   987
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
   988
hegyi@1123
   989
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
   990
    /// \param s is the source node.
alpar@1117
   991
    DijkstraWizard<TR> &source(Node s) 
alpar@953
   992
    {
alpar@1201
   993
      Base::_source=s;
alpar@953
   994
      return *this;
alpar@953
   995
    }
alpar@953
   996
    
alpar@953
   997
  };
alpar@255
   998
  
alpar@1218
   999
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1000
alpar@1151
  1001
  /// \ingroup flowalgs
alpar@1218
  1002
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1003
  ///
alpar@1218
  1004
  ///This function also has several
alpar@1218
  1005
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1006
  ///they are declared as the members of class \ref DijkstraWizard.
alpar@1218
  1007
  ///The following
alpar@1218
  1008
  ///example shows how to use these parameters.
alpar@1218
  1009
  ///\code
alpar@1218
  1010
  ///  dijkstra(g,length,source).predMap(preds).run();
alpar@1218
  1011
  ///\endcode
alpar@1218
  1012
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
alpar@1218
  1013
  ///to the end of the parameter list.
alpar@1218
  1014
  ///\sa DijkstraWizard
alpar@1218
  1015
  ///\sa Dijkstra
alpar@953
  1016
  template<class GR, class LM>
alpar@1116
  1017
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
  1018
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
  1019
  {
alpar@1116
  1020
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
  1021
  }
alpar@953
  1022
alpar@921
  1023
} //END OF NAMESPACE LEMON
alpar@255
  1024
alpar@255
  1025
#endif