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