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