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