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