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