src/work/alpar/dijkstra.h
author alpar
Sun, 06 Feb 2005 14:44:41 +0000
changeset 1128 6a347310d4c2
parent 1126 04e1cb315951
child 1130 47ef467ccf70
permissions -rw-r--r--
Several important changes:
- Named parameters for setting ReachedMap
- run() is separated into initialization and processing phase
- It is possible to run Dijkstra from multiple sources
- It is possible to stop the execution when a destination is reached.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_DIJKSTRA_H
alpar@921
    18
#define LEMON_DIJKSTRA_H
alpar@255
    19
alpar@758
    20
///\ingroup flowalgs
alpar@255
    21
///\file
alpar@255
    22
///\brief Dijkstra algorithm.
alpar@255
    23
alpar@953
    24
#include <lemon/list_graph.h>
alpar@921
    25
#include <lemon/bin_heap.h>
alpar@921
    26
#include <lemon/invalid.h>
alpar@1119
    27
#include <lemon/error.h>
alpar@1119
    28
#include <lemon/maps.h>
alpar@255
    29
alpar@921
    30
namespace lemon {
jacint@385
    31
alpar@1119
    32
alpar@758
    33
/// \addtogroup flowalgs
alpar@430
    34
/// @{
alpar@430
    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@953
    81
    ///\brief The type of the map that stores the last but one
alpar@953
    82
    ///nodes of the shortest paths.
alpar@953
    83
    ///
hegyi@1124
    84
    ///The type of the map that stores the last but one
hegyi@1124
    85
    ///nodes of the shortest paths.
alpar@967
    86
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
    87
    ///
alpar@954
    88
    typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
alpar@954
    89
    ///Instantiates a PredNodeMap.
alpar@1125
    90
    
hegyi@1123
    91
    ///This function instantiates a \ref PredNodeMap. 
hegyi@1123
    92
    ///\param G is the graph, to which we would like to define the \ref PredNodeMap
alpar@954
    93
    static PredNodeMap *createPredNodeMap(const GR &G)
alpar@953
    94
    {
alpar@953
    95
      return new PredNodeMap(G);
alpar@953
    96
    }
alpar@1119
    97
alpar@1119
    98
    ///The type of the map that stores whether a nodes is reached.
alpar@1119
    99
 
hegyi@1124
   100
    ///The type of the map that stores whether a nodes is reached.
alpar@1119
   101
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1119
   102
    ///By default it is a NullMap.
alpar@1119
   103
    ///\todo If it is set to a real map, Dijkstra::reached() should read this.
alpar@1119
   104
    ///\todo named parameter to set this type, function to read and write.
alpar@1119
   105
    typedef NullMap<typename Graph::Node,bool> ReachedMap;
alpar@1119
   106
    ///Instantiates a ReachedMap.
alpar@1119
   107
 
hegyi@1123
   108
    ///This function instantiates a \ref ReachedMap. 
hegyi@1123
   109
    ///\param G is the graph, to which we would like to define the \ref ReachedMap
alpar@1119
   110
    static ReachedMap *createReachedMap(const GR &G)
alpar@1119
   111
    {
alpar@1119
   112
      return new ReachedMap();
alpar@1119
   113
    }
alpar@953
   114
    ///The type of the map that stores the dists of the nodes.
alpar@953
   115
 
hegyi@1124
   116
    ///The type of the map that stores the dists of the nodes.
alpar@967
   117
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
   118
    ///
alpar@987
   119
    typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
alpar@954
   120
    ///Instantiates a DistMap.
alpar@953
   121
 
hegyi@1123
   122
    ///This function instantiates a \ref DistMap. 
hegyi@1123
   123
    ///\param G is the graph, to which we would like to define the \ref DistMap
alpar@954
   124
    static DistMap *createDistMap(const GR &G)
alpar@953
   125
    {
alpar@953
   126
      return new DistMap(G);
alpar@953
   127
    }
alpar@953
   128
  };
alpar@953
   129
  
alpar@255
   130
  ///%Dijkstra algorithm class.
alpar@1125
   131
  
alpar@255
   132
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
   133
  ///The edge lengths are passed to the algorithm using a
klao@959
   134
  ///\ref concept::ReadMap "ReadMap",
alpar@255
   135
  ///so it is easy to change it to any kind of length.
alpar@255
   136
  ///
alpar@880
   137
  ///The type of the length is determined by the
alpar@987
   138
  ///\ref concept::ReadMap::Value "Value" of the length map.
alpar@255
   139
  ///
alpar@255
   140
  ///It is also possible to change the underlying priority heap.
alpar@255
   141
  ///
alpar@953
   142
  ///\param GR The graph type the algorithm runs on. The default value is
alpar@955
   143
  ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
alpar@954
   144
  ///is only passed to \ref DijkstraDefaultTraits.
alpar@584
   145
  ///\param LM This read-only
jacint@385
   146
  ///EdgeMap
jacint@385
   147
  ///determines the
jacint@385
   148
  ///lengths of the edges. It is read once for each edge, so the map
jacint@385
   149
  ///may involve in relatively time consuming process to compute the edge
jacint@385
   150
  ///length if it is necessary. The default map type is
klao@959
   151
  ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
alpar@955
   152
  ///The value of LM is not used directly by Dijkstra, it
alpar@954
   153
  ///is only passed to \ref DijkstraDefaultTraits.
alpar@954
   154
  ///\param TR Traits class to set various data types used by the algorithm.
alpar@954
   155
  ///The default traits class is
alpar@955
   156
  ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
alpar@954
   157
  ///See \ref DijkstraDefaultTraits for the documentation of
alpar@954
   158
  ///a Dijkstra traits class.
alpar@456
   159
  ///
alpar@689
   160
  ///\author Jacint Szabo and Alpar Juttner
alpar@1128
   161
  ///\todo A compare object would be nice.
alpar@584
   162
alpar@255
   163
#ifdef DOXYGEN
alpar@584
   164
  template <typename GR,
alpar@584
   165
	    typename LM,
alpar@953
   166
	    typename TR>
alpar@255
   167
#else
alpar@953
   168
  template <typename GR=ListGraph,
alpar@584
   169
	    typename LM=typename GR::template EdgeMap<int>,
alpar@953
   170
	    typename TR=DijkstraDefaultTraits<GR,LM> >
alpar@255
   171
#endif
alpar@1116
   172
  class Dijkstra {
alpar@255
   173
  public:
alpar@1125
   174
    /**
alpar@1125
   175
     * \brief \ref Exception for uninitialized parameters.
alpar@1125
   176
     *
alpar@1125
   177
     * This error represents problems in the initialization
alpar@1125
   178
     * of the parameters of the algorithms.
alpar@1125
   179
     */
alpar@1125
   180
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@1125
   181
    public:
alpar@1125
   182
      virtual const char* exceptionName() const {
alpar@1125
   183
	return "lemon::Dijsktra::UninitializedParameter";
alpar@1125
   184
      }
alpar@1125
   185
    };
alpar@1119
   186
alpar@953
   187
    typedef TR Traits;
alpar@584
   188
    ///The type of the underlying graph.
alpar@954
   189
    typedef typename TR::Graph Graph;
alpar@911
   190
    ///\e
alpar@255
   191
    typedef typename Graph::Node Node;
alpar@911
   192
    ///\e
alpar@255
   193
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   194
    ///\e
alpar@255
   195
    typedef typename Graph::Edge Edge;
alpar@911
   196
    ///\e
alpar@255
   197
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
   198
    
alpar@584
   199
    ///The type of the length of the edges.
alpar@987
   200
    typedef typename TR::LengthMap::Value Value;
alpar@693
   201
    ///The type of the map that stores the edge lengths.
alpar@954
   202
    typedef typename TR::LengthMap LengthMap;
alpar@693
   203
    ///\brief The type of the map that stores the last
alpar@584
   204
    ///edges of the shortest paths.
alpar@953
   205
    typedef typename TR::PredMap PredMap;
alpar@693
   206
    ///\brief The type of the map that stores the last but one
alpar@584
   207
    ///nodes of the shortest paths.
alpar@953
   208
    typedef typename TR::PredNodeMap PredNodeMap;
alpar@1119
   209
    ///The type of the map indicating if a node is reached.
alpar@1119
   210
    typedef typename TR::ReachedMap ReachedMap;
alpar@693
   211
    ///The type of the map that stores the dists of the nodes.
alpar@953
   212
    typedef typename TR::DistMap DistMap;
alpar@953
   213
    ///The heap type used by the dijkstra algorithm.
alpar@953
   214
    typedef typename TR::Heap Heap;
alpar@255
   215
  private:
alpar@802
   216
    /// Pointer to the underlying graph.
alpar@688
   217
    const Graph *G;
alpar@802
   218
    /// Pointer to the length map
alpar@954
   219
    const LengthMap *length;
alpar@802
   220
    ///Pointer to the map of predecessors edges.
alpar@1119
   221
    PredMap *_pred;
alpar@1119
   222
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1119
   223
    bool local_pred;
alpar@802
   224
    ///Pointer to the map of predecessors nodes.
alpar@688
   225
    PredNodeMap *pred_node;
alpar@802
   226
    ///Indicates if \ref pred_node is locally allocated (\c true) or not.
alpar@688
   227
    bool local_pred_node;
alpar@802
   228
    ///Pointer to the map of distances.
alpar@688
   229
    DistMap *distance;
alpar@802
   230
    ///Indicates if \ref distance is locally allocated (\c true) or not.
alpar@688
   231
    bool local_distance;
alpar@1119
   232
    ///Pointer to the map of reached status of the nodes.
alpar@1119
   233
    ReachedMap *_reached;
alpar@1119
   234
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@1119
   235
    bool local_reached;
alpar@688
   236
alpar@802
   237
    ///The source node of the last execution.
alpar@774
   238
    Node source;
alpar@774
   239
alpar@1128
   240
    ///Creates the maps if necessary.
alpar@688
   241
    
alpar@694
   242
    ///\todo Error if \c G or are \c NULL. What about \c length?
alpar@688
   243
    ///\todo Better memory allocation (instead of new).
alpar@1128
   244
    void create_maps() 
alpar@688
   245
    {
alpar@1119
   246
      if(!_pred) {
alpar@1119
   247
	local_pred = true;
alpar@1119
   248
	_pred = Traits::createPredMap(*G);
alpar@688
   249
      }
alpar@688
   250
      if(!pred_node) {
alpar@688
   251
	local_pred_node = true;
alpar@953
   252
	pred_node = Traits::createPredNodeMap(*G);
alpar@688
   253
      }
alpar@688
   254
      if(!distance) {
alpar@688
   255
	local_distance = true;
alpar@953
   256
	distance = Traits::createDistMap(*G);
alpar@688
   257
      }
alpar@1119
   258
      if(!_reached) {
alpar@1119
   259
	local_reached = true;
alpar@1119
   260
	_reached = Traits::createReachedMap(*G);
alpar@1119
   261
      }
alpar@688
   262
    }
alpar@255
   263
    
alpar@255
   264
  public :
alpar@1116
   265
 
alpar@1128
   266
    ///\name Named template parameters
alpar@1128
   267
alpar@1128
   268
    ///@{
alpar@1128
   269
alpar@953
   270
    template <class T>
alpar@1116
   271
    struct DefPredMapTraits : public Traits {
alpar@953
   272
      typedef T PredMap;
alpar@953
   273
      static PredMap *createPredMap(const Graph &G) 
alpar@953
   274
      {
alpar@1126
   275
	throw UninitializedParameter();
alpar@953
   276
      }
alpar@953
   277
    };
alpar@954
   278
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@954
   279
alpar@954
   280
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1043
   281
    ///
alpar@953
   282
    template <class T>
alpar@1116
   283
    class DefPredMap : public Dijkstra< Graph,
alpar@953
   284
					LengthMap,
alpar@1116
   285
					DefPredMapTraits<T> > { };
alpar@953
   286
    
alpar@953
   287
    template <class T>
alpar@1116
   288
    struct DefPredNodeMapTraits : public Traits {
alpar@953
   289
      typedef T PredNodeMap;
alpar@953
   290
      static PredNodeMap *createPredNodeMap(const Graph &G) 
alpar@953
   291
      {
alpar@1126
   292
	throw UninitializedParameter();
alpar@953
   293
      }
alpar@953
   294
    };
alpar@954
   295
    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
alpar@954
   296
alpar@954
   297
    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
alpar@1043
   298
    ///
alpar@953
   299
    template <class T>
alpar@1116
   300
    class DefPredNodeMap : public Dijkstra< Graph,
alpar@953
   301
					    LengthMap,
alpar@1116
   302
					    DefPredNodeMapTraits<T> > { };
alpar@953
   303
    
alpar@953
   304
    template <class T>
alpar@1116
   305
    struct DefDistMapTraits : public Traits {
alpar@953
   306
      typedef T DistMap;
alpar@953
   307
      static DistMap *createDistMap(const Graph &G) 
alpar@953
   308
      {
alpar@1126
   309
	throw UninitializedParameter();
alpar@953
   310
      }
alpar@953
   311
    };
alpar@954
   312
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@954
   313
alpar@954
   314
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1043
   315
    ///
alpar@953
   316
    template <class T>
alpar@1116
   317
    class DefDistMap : public Dijkstra< Graph,
alpar@953
   318
					LengthMap,
alpar@1116
   319
					DefDistMapTraits<T> > { };
alpar@953
   320
    
alpar@1128
   321
    template <class T>
alpar@1128
   322
    struct DefReachedMapTraits : public Traits {
alpar@1128
   323
      typedef T ReachedMap;
alpar@1128
   324
      static ReachedMap *createReachedMap(const Graph &G) 
alpar@1128
   325
      {
alpar@1128
   326
	throw UninitializedParameter();
alpar@1128
   327
      }
alpar@1128
   328
    };
alpar@1128
   329
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1128
   330
alpar@1128
   331
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1128
   332
    ///
alpar@1128
   333
    template <class T>
alpar@1128
   334
    class DefReachedMap : public Dijkstra< Graph,
alpar@1128
   335
					LengthMap,
alpar@1128
   336
					DefReachedMapTraits<T> > { };
alpar@1128
   337
    
alpar@1128
   338
    struct DefGraphReachedMapTraits : public Traits {
alpar@1128
   339
      typedef typename Graph::NodeMap<bool> ReachedMap;
alpar@1128
   340
      static ReachedMap *createReachedMap(const Graph &G) 
alpar@1128
   341
      {
alpar@1128
   342
	return new ReachedMap(G);
alpar@1128
   343
      }
alpar@1128
   344
    };
alpar@1128
   345
    ///\brief \ref named-templ-param "Named parameter"
alpar@1128
   346
    ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
alpar@1128
   347
    ///
alpar@1128
   348
    ///\ref named-templ-param "Named parameter"
alpar@1128
   349
    ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
alpar@1128
   350
    ///If you don't set it explicitely, it will be automatically allocated.
alpar@1128
   351
    template <class T>
alpar@1128
   352
    class DefReachedMapToBeDefaultMap :
alpar@1128
   353
      public Dijkstra< Graph,
alpar@1128
   354
		       LengthMap,
alpar@1128
   355
		       DefGraphReachedMapTraits> { };
alpar@1128
   356
    
alpar@1128
   357
    ///@}
alpar@1128
   358
alpar@1128
   359
alpar@1128
   360
  private:
alpar@1128
   361
    typename Graph::template NodeMap<int> _heap_map;
alpar@1128
   362
    Heap _heap;
alpar@1128
   363
  public:      
alpar@1128
   364
    
alpar@802
   365
    ///Constructor.
alpar@255
   366
    
alpar@802
   367
    ///\param _G the graph the algorithm will run on.
alpar@802
   368
    ///\param _length the length map used by the algorithm.
alpar@954
   369
    Dijkstra(const Graph& _G, const LengthMap& _length) :
alpar@688
   370
      G(&_G), length(&_length),
alpar@1119
   371
      _pred(NULL), local_pred(false),
alpar@707
   372
      pred_node(NULL), local_pred_node(false),
alpar@1119
   373
      distance(NULL), local_distance(false),
alpar@1128
   374
      _reached(NULL), local_reached(false),
alpar@1128
   375
      _heap_map(*G,-1),_heap(_heap_map)
alpar@688
   376
    { }
alpar@688
   377
    
alpar@802
   378
    ///Destructor.
alpar@688
   379
    ~Dijkstra() 
alpar@688
   380
    {
alpar@1119
   381
      if(local_pred) delete _pred;
alpar@688
   382
      if(local_pred_node) delete pred_node;
alpar@688
   383
      if(local_distance) delete distance;
alpar@1119
   384
      if(local_reached) delete _reached;
alpar@688
   385
    }
alpar@688
   386
alpar@688
   387
    ///Sets the length map.
alpar@688
   388
alpar@688
   389
    ///Sets the length map.
alpar@688
   390
    ///\return <tt> (*this) </tt>
alpar@1116
   391
    Dijkstra &lengthMap(const LengthMap &m) 
alpar@688
   392
    {
alpar@688
   393
      length = &m;
alpar@688
   394
      return *this;
alpar@688
   395
    }
alpar@688
   396
alpar@688
   397
    ///Sets the map storing the predecessor edges.
alpar@688
   398
alpar@688
   399
    ///Sets the map storing the predecessor edges.
alpar@688
   400
    ///If you don't use this function before calling \ref run(),
alpar@688
   401
    ///it will allocate one. The destuctor deallocates this
alpar@688
   402
    ///automatically allocated map, of course.
alpar@688
   403
    ///\return <tt> (*this) </tt>
alpar@1116
   404
    Dijkstra &predMap(PredMap &m) 
alpar@688
   405
    {
alpar@1119
   406
      if(local_pred) {
alpar@1119
   407
	delete _pred;
alpar@1119
   408
	local_pred=false;
alpar@688
   409
      }
alpar@1119
   410
      _pred = &m;
alpar@688
   411
      return *this;
alpar@688
   412
    }
alpar@688
   413
alpar@688
   414
    ///Sets the map storing the predecessor nodes.
alpar@688
   415
alpar@688
   416
    ///Sets the map storing the predecessor nodes.
alpar@688
   417
    ///If you don't use this function before calling \ref run(),
alpar@688
   418
    ///it will allocate one. The destuctor deallocates this
alpar@688
   419
    ///automatically allocated map, of course.
alpar@688
   420
    ///\return <tt> (*this) </tt>
alpar@1116
   421
    Dijkstra &predNodeMap(PredNodeMap &m) 
alpar@688
   422
    {
alpar@688
   423
      if(local_pred_node) {
alpar@688
   424
	delete pred_node;
alpar@688
   425
	local_pred_node=false;
alpar@688
   426
      }
alpar@688
   427
      pred_node = &m;
alpar@688
   428
      return *this;
alpar@688
   429
    }
alpar@688
   430
alpar@688
   431
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   432
alpar@688
   433
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   434
    ///If you don't use this function before calling \ref run(),
alpar@688
   435
    ///it will allocate one. The destuctor deallocates this
alpar@688
   436
    ///automatically allocated map, of course.
alpar@688
   437
    ///\return <tt> (*this) </tt>
alpar@1116
   438
    Dijkstra &distMap(DistMap &m) 
alpar@688
   439
    {
alpar@688
   440
      if(local_distance) {
alpar@688
   441
	delete distance;
alpar@688
   442
	local_distance=false;
alpar@688
   443
      }
alpar@688
   444
      distance = &m;
alpar@688
   445
      return *this;
alpar@688
   446
    }
alpar@694
   447
alpar@1128
   448
    ///\name Excetution control
alpar@1128
   449
    ///The simplest way to execute the algorithm is to use
alpar@1128
   450
    ///\ref run().
alpar@1128
   451
    ///\n
alpar@1128
   452
    ///It you need more control on the execution,
alpar@1128
   453
    ///first you must call \ref init(), then you can add several source nodes
alpar@1128
   454
    ///with \ref addSource(). Finally \ref start() will perform the actual path
alpar@1128
   455
    ///computation.
alpar@1128
   456
alpar@1128
   457
    ///@{
alpar@1128
   458
alpar@1128
   459
    ///Initializes the internal data structures.
alpar@1128
   460
alpar@1128
   461
    ///Initializes the internal data structures.
alpar@1128
   462
    ///
alpar@1128
   463
    ///\todo _heap_map's type could also be in the traits class.
alpar@1128
   464
    void init()
alpar@1128
   465
    {
alpar@1128
   466
      create_maps();
alpar@774
   467
      
alpar@774
   468
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1119
   469
	_pred->set(u,INVALID);
alpar@694
   470
	pred_node->set(u,INVALID);
alpar@1119
   471
	///\todo *_reached is not set to false.
alpar@1128
   472
	_heap_map.set(u,Heap::PRE_HEAP);
alpar@694
   473
      }
alpar@1128
   474
    }
alpar@1128
   475
    
alpar@1128
   476
    ///Adds a new source node.
alpar@1128
   477
alpar@1128
   478
    ///Adds a new source node the the priority heap.
alpar@1128
   479
    ///It checks if the node has already been added to the heap.
alpar@1128
   480
    ///
alpar@1128
   481
    ///The optional second parameter is the initial distance of the node.
alpar@1128
   482
    ///
alpar@1128
   483
    ///\todo Do we really want to check it?
alpar@1128
   484
    void addSource(Node s,Value dst=0)
alpar@1128
   485
    {
alpar@1128
   486
      source = s;
alpar@1128
   487
      if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
alpar@1128
   488
    }
alpar@1128
   489
    
alpar@1128
   490
    void processNode()
alpar@1128
   491
    {
alpar@1128
   492
      Node v=_heap.top(); 
alpar@1128
   493
      _reached->set(v,true);
alpar@1128
   494
      Value oldvalue=_heap[v];
alpar@1128
   495
      _heap.pop();
alpar@1128
   496
      distance->set(v, oldvalue);
alpar@694
   497
      
alpar@1128
   498
      for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@1128
   499
	Node w=G->target(e); 
alpar@1128
   500
	switch(_heap.state(w)) {
alpar@1128
   501
	case Heap::PRE_HEAP:
alpar@1128
   502
	  _heap.push(w,oldvalue+(*length)[e]); 
alpar@1128
   503
	  _pred->set(w,e);
alpar@1128
   504
	  pred_node->set(w,v);
alpar@1128
   505
	  break;
alpar@1128
   506
	case Heap::IN_HEAP:
alpar@1128
   507
	  if ( oldvalue+(*length)[e] < _heap[w] ) {
alpar@1128
   508
	    _heap.decrease(w, oldvalue+(*length)[e]); 
alpar@1119
   509
	    _pred->set(w,e);
alpar@694
   510
	    pred_node->set(w,v);
alpar@694
   511
	  }
alpar@1128
   512
	  break;
alpar@1128
   513
	case Heap::POST_HEAP:
alpar@1128
   514
	  break;
alpar@694
   515
	}
alpar@694
   516
      }
alpar@694
   517
    }
alpar@1128
   518
alpar@1128
   519
    ///Starts the execution of the algorithm.
alpar@1128
   520
alpar@1128
   521
    ///Starts the execution of the algorithm.
alpar@1128
   522
    ///
alpar@1128
   523
    ///\pre init() must be called before and at least one node should be added
alpar@1128
   524
    ///with addSource().
alpar@1128
   525
    ///
alpar@1128
   526
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   527
    ///in order to
alpar@1128
   528
    ///compute the
alpar@1128
   529
    ///shortest path to each node. The algorithm computes
alpar@1128
   530
    ///- The shortest path tree.
alpar@1128
   531
    ///- The distance of each node from the root(s).
alpar@1128
   532
    ///
alpar@1128
   533
    void start()
alpar@1128
   534
    {
alpar@1128
   535
      while ( !_heap.empty() ) processNode();
alpar@1128
   536
    }
alpar@255
   537
    
alpar@1128
   538
    ///Starts the execution of the algorithm until \c dest is reached.
alpar@1128
   539
alpar@1128
   540
    ///Starts the execution of the algorithm until \c dest is reached.
alpar@1128
   541
    ///
alpar@1128
   542
    ///\pre init() must be called before and at least one node should be added
alpar@1128
   543
    ///with addSource().
alpar@1128
   544
    ///
alpar@1128
   545
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   546
    ///in order to
alpar@1128
   547
    ///compute the
alpar@1128
   548
    ///shortest path to \c dest. The algorithm computes
alpar@1128
   549
    ///- The shortest path to \c  dest.
alpar@1128
   550
    ///- The distance of \c dest from the root(s).
alpar@1128
   551
    ///
alpar@1128
   552
    void start(Node dest)
alpar@1128
   553
    {
alpar@1128
   554
      while ( !_heap.empty() && _heap.top()!=dest) processNode();
alpar@1128
   555
    }
alpar@1128
   556
    
alpar@1128
   557
    ///Runs %Dijkstra algorithm from node \c s.
alpar@1128
   558
    
alpar@1128
   559
    ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@1128
   560
    ///in order to
alpar@1128
   561
    ///compute the
alpar@1128
   562
    ///shortest path to each node. The algorithm computes
alpar@1128
   563
    ///- The shortest path tree.
alpar@1128
   564
    ///- The distance of each node from the root.
alpar@1128
   565
    ///
alpar@1128
   566
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1128
   567
    ///\code
alpar@1128
   568
    ///  d.init();
alpar@1128
   569
    ///  d.addSource(s);
alpar@1128
   570
    ///  d.start();
alpar@1128
   571
    ///\endcode
alpar@1128
   572
    void run(Node s) {
alpar@1128
   573
      init();
alpar@1128
   574
      addSource(s);
alpar@1128
   575
      start();
alpar@1128
   576
    }
alpar@1128
   577
    
alpar@1128
   578
    ///@}
alpar@1128
   579
alpar@1128
   580
    ///\name Query Functions
alpar@1128
   581
    ///The result of the %Dijkstra algorithm can be obtained using these
alpar@1128
   582
    ///functions.\n
alpar@1128
   583
    ///Before the use of these functions,
alpar@1128
   584
    ///either run() or start() must be called.
alpar@1128
   585
    
alpar@1128
   586
    ///@{
alpar@1128
   587
jacint@385
   588
    ///The distance of a node from the root.
alpar@255
   589
jacint@385
   590
    ///Returns the distance of a node from the root.
alpar@255
   591
    ///\pre \ref run() must be called before using this function.
jacint@385
   592
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   593
    ///of this funcion is undefined.
alpar@987
   594
    Value dist(Node v) const { return (*distance)[v]; }
jacint@373
   595
alpar@584
   596
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   597
alpar@584
   598
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@785
   599
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@688
   600
    ///v. It is \ref INVALID
alpar@688
   601
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   602
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
   603
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
   604
    ///this function.
alpar@780
   605
    ///\todo predEdge could be a better name.
alpar@1119
   606
    Edge pred(Node v) const { return (*_pred)[v]; }
jacint@373
   607
alpar@584
   608
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   609
alpar@584
   610
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   611
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   612
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   613
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
   614
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
   615
    ///using this function.
alpar@688
   616
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@255
   617
    
alpar@255
   618
    ///Returns a reference to the NodeMap of distances.
alpar@255
   619
jacint@385
   620
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   621
    ///be called before using this function.
alpar@688
   622
    const DistMap &distMap() const { return *distance;}
jacint@385
   623
 
alpar@255
   624
    ///Returns a reference to the shortest path tree map.
alpar@255
   625
alpar@255
   626
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   627
    ///shortest path tree.
alpar@255
   628
    ///\pre \ref run() must be called before using this function.
alpar@1119
   629
    const PredMap &predMap() const { return *_pred;}
jacint@385
   630
 
jacint@385
   631
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   632
alpar@255
   633
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   634
    ///shortest path tree.
alpar@255
   635
    ///\pre \ref run() must be called before using this function.
alpar@688
   636
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@255
   637
jacint@385
   638
    ///Checks if a node is reachable from the root.
alpar@255
   639
jacint@385
   640
    ///Returns \c true if \c v is reachable from the root.
alpar@1128
   641
    ///\warning If the algorithm is started from multiple nodes,
alpar@1128
   642
    ///this function may give false result for the source nodes.
alpar@255
   643
    ///\pre \ref run() must be called before using this function.
jacint@385
   644
    ///
alpar@1119
   645
    bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
alpar@255
   646
    
alpar@1128
   647
    ///@}
alpar@255
   648
  };
alpar@953
   649
hegyi@1123
   650
  /// Default traits used by \ref DijkstraWizard
hegyi@1123
   651
hegyi@1124
   652
  /// To make it easier to use Dijkstra algorithm we have created a wizard class.
hegyi@1124
   653
  /// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class.
hegyi@1123
   654
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
hegyi@1123
   655
  /// \ref DijkstraWizard class.
alpar@1116
   656
  template<class GR,class LM>
alpar@1116
   657
  class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
alpar@1116
   658
  {
alpar@1116
   659
alpar@1116
   660
    typedef DijkstraDefaultTraits<GR,LM> Base;
alpar@1116
   661
  protected:
alpar@1116
   662
    /// Pointer to the underlying graph.
alpar@1116
   663
    void *_g;
alpar@1116
   664
    /// Pointer to the length map
alpar@1116
   665
    void *_length;
alpar@1116
   666
    ///Pointer to the map of predecessors edges.
alpar@1116
   667
    void *_pred;
alpar@1116
   668
    ///Pointer to the map of predecessors nodes.
alpar@1116
   669
    void *_predNode;
alpar@1116
   670
    ///Pointer to the map of distances.
alpar@1116
   671
    void *_dist;
alpar@1116
   672
    ///Pointer to the source node.
alpar@1116
   673
    void *_source;
alpar@1116
   674
hegyi@1123
   675
    /// Type of the nodes in the graph.
alpar@1116
   676
    typedef typename Base::Graph::Node Node;
alpar@1116
   677
alpar@1116
   678
    public:
hegyi@1123
   679
    /// Constructor.
hegyi@1123
   680
    
hegyi@1123
   681
    /// This constructor does not require parameters, therefore it initiates
hegyi@1123
   682
    /// all of the attributes to default values (0, INVALID).
alpar@1116
   683
    DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
alpar@1116
   684
		       _dist(0), _source(INVALID) {}
alpar@1116
   685
hegyi@1123
   686
    /// Constructor.
hegyi@1123
   687
    
hegyi@1123
   688
    /// This constructor requires some parameters, listed in the parameters list.
hegyi@1123
   689
    /// Others are initiated to 0.
hegyi@1123
   690
    /// \param g is the initial value of  \ref _g
hegyi@1123
   691
    /// \param l is the initial value of  \ref _length
hegyi@1123
   692
    /// \param s is the initial value of  \ref _source
alpar@1116
   693
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
alpar@1116
   694
      _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
alpar@1116
   695
		  _dist(0), _source((void *)&s) {}
alpar@1116
   696
alpar@1116
   697
  };
alpar@1116
   698
  
hegyi@1123
   699
  /// A class to make easier the usage of Dijkstra algorithm
alpar@953
   700
hegyi@1123
   701
  /// This class is created to make it easier to use Dijkstra algorithm.
hegyi@1123
   702
  /// It uses the functions and features of the plain \ref Dijkstra,
hegyi@1123
   703
  /// but it is much more simple to use it.
alpar@953
   704
  ///
hegyi@1123
   705
  /// Simplicity means that the way to change the types defined
hegyi@1123
   706
  /// in the traits class is based on functions that returns the new class
hegyi@1124
   707
  /// and not on templatable built-in classes. When using the plain \ref Dijkstra
hegyi@1124
   708
  /// the new class with the modified type comes from the original class by using the ::
hegyi@1124
   709
  /// operator. In the case of \ref DijkstraWizard only a function have to be called and it will
hegyi@1123
   710
  /// return the needed class.
hegyi@1123
   711
  ///
hegyi@1123
   712
  /// It does not have own \ref run method. When its \ref run method is called
hegyi@1123
   713
  /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
hegyi@1123
   714
  /// method of it.
alpar@953
   715
  template<class TR>
alpar@1116
   716
  class DijkstraWizard : public TR
alpar@953
   717
  {
alpar@1116
   718
    typedef TR Base;
alpar@953
   719
hegyi@1123
   720
    ///The type of the underlying graph.
alpar@953
   721
    typedef typename TR::Graph Graph;
alpar@1119
   722
    //\e
alpar@953
   723
    typedef typename Graph::Node Node;
alpar@1119
   724
    //\e
alpar@953
   725
    typedef typename Graph::NodeIt NodeIt;
alpar@1119
   726
    //\e
alpar@953
   727
    typedef typename Graph::Edge Edge;
alpar@1119
   728
    //\e
alpar@953
   729
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
   730
    
hegyi@1123
   731
    ///The type of the map that stores the edge lengths.
alpar@953
   732
    typedef typename TR::LengthMap LengthMap;
hegyi@1123
   733
    ///The type of the length of the edges.
alpar@987
   734
    typedef typename LengthMap::Value Value;
hegyi@1123
   735
    ///\brief The type of the map that stores the last
hegyi@1123
   736
    ///edges of the shortest paths.
alpar@953
   737
    typedef typename TR::PredMap PredMap;
hegyi@1123
   738
    ///\brief The type of the map that stores the last but one
hegyi@1123
   739
    ///nodes of the shortest paths.
alpar@953
   740
    typedef typename TR::PredNodeMap PredNodeMap;
hegyi@1123
   741
    ///The type of the map that stores the dists of the nodes.
alpar@953
   742
    typedef typename TR::DistMap DistMap;
alpar@953
   743
hegyi@1123
   744
    ///The heap type used by the dijkstra algorithm.
alpar@953
   745
    typedef typename TR::Heap Heap;
alpar@1116
   746
public:
hegyi@1123
   747
    /// Constructor.
alpar@1116
   748
    DijkstraWizard() : TR() {}
alpar@953
   749
hegyi@1123
   750
    /// Constructor that requires parameters.
hegyi@1124
   751
hegyi@1124
   752
    /// Constructor that requires parameters.
hegyi@1123
   753
    /// These parameters will be the default values for the traits class.
alpar@1116
   754
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
   755
      TR(g,l,s) {}
alpar@953
   756
hegyi@1123
   757
    ///Copy constructor
alpar@1116
   758
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
   759
alpar@1116
   760
    ~DijkstraWizard() {}
alpar@1116
   761
hegyi@1123
   762
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
   763
    
hegyi@1123
   764
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
   765
    ///The node can be given by the \ref source function.
alpar@1116
   766
    void run()
alpar@953
   767
    {
alpar@1126
   768
      if(_source==0) throw UninitializedParameter();
alpar@1116
   769
      Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
alpar@1116
   770
      if(_pred) Dij.predMap(*(PredMap*)_pred);
alpar@1116
   771
      if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
alpar@1116
   772
      if(_dist) Dij.distMap(*(DistMap*)_dist);
alpar@1116
   773
      Dij.run(*(Node*)_source);
alpar@1116
   774
    }
alpar@1116
   775
hegyi@1124
   776
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
   777
hegyi@1124
   778
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
   779
    ///\param s is the given source.
alpar@1116
   780
    void run(Node s)
alpar@1116
   781
    {
alpar@1116
   782
      _source=(void *)&s;
alpar@1116
   783
      run();
alpar@953
   784
    }
alpar@953
   785
alpar@953
   786
    template<class T>
alpar@1116
   787
    struct DefPredMapBase : public Base {
alpar@1116
   788
      typedef T PredMap;
alpar@1117
   789
      static PredMap *createPredMap(const Graph &G) { return 0; };
alpar@1117
   790
      DefPredMapBase(const Base &b) : Base(b) {}
alpar@1116
   791
    };
alpar@953
   792
    
hegyi@1123
   793
    /// \ref named-templ-param "Named parameter" function for setting PredMap type
hegyi@1123
   794
hegyi@1123
   795
    /// \ref named-templ-param "Named parameter" function for setting PredMap type
hegyi@1124
   796
    ///
alpar@953
   797
    template<class T>
alpar@1116
   798
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
   799
    {
alpar@1116
   800
      _pred=(void *)&t;
alpar@1116
   801
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
   802
    }
alpar@953
   803
    
alpar@1116
   804
alpar@953
   805
    template<class T>
alpar@1116
   806
    struct DefPredNodeMapBase : public Base {
alpar@1116
   807
      typedef T PredNodeMap;
alpar@1117
   808
      static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
alpar@1117
   809
      DefPredNodeMapBase(const Base &b) : Base(b) {}
alpar@1116
   810
    };
alpar@1116
   811
    
hegyi@1123
   812
    /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
hegyi@1123
   813
hegyi@1123
   814
    /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
hegyi@1124
   815
    ///
alpar@953
   816
    template<class T>
alpar@1116
   817
    DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
alpar@953
   818
    {
alpar@1116
   819
      _predNode=(void *)&t;
alpar@1116
   820
      return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
alpar@953
   821
    }
alpar@1116
   822
   
alpar@1116
   823
    template<class T>
alpar@1116
   824
    struct DefDistMapBase : public Base {
alpar@1116
   825
      typedef T DistMap;
alpar@1117
   826
      static DistMap *createDistMap(const Graph &G) { return 0; };
alpar@1117
   827
      DefDistMapBase(const Base &b) : Base(b) {}
alpar@1116
   828
    };
alpar@953
   829
    
hegyi@1123
   830
    /// \ref named-templ-param "Named parameter" function for setting DistMap type
hegyi@1123
   831
hegyi@1123
   832
    /// \ref named-templ-param "Named parameter" function for setting DistMap type
hegyi@1124
   833
    ///
alpar@953
   834
    template<class T>
alpar@1116
   835
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
   836
    {
alpar@1116
   837
      _dist=(void *)&t;
alpar@1116
   838
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
   839
    }
alpar@1117
   840
    
hegyi@1123
   841
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
   842
hegyi@1123
   843
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
   844
    /// \param s is the source node.
alpar@1117
   845
    DijkstraWizard<TR> &source(Node s) 
alpar@953
   846
    {
alpar@1116
   847
      source=(void *)&s;
alpar@953
   848
      return *this;
alpar@953
   849
    }
alpar@953
   850
    
alpar@953
   851
  };
alpar@255
   852
  
alpar@953
   853
  ///\e
alpar@953
   854
alpar@954
   855
  ///\todo Please document...
alpar@953
   856
  ///
alpar@953
   857
  template<class GR, class LM>
alpar@1116
   858
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
   859
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
   860
  {
alpar@1116
   861
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
   862
  }
alpar@953
   863
alpar@430
   864
/// @}
alpar@255
   865
  
alpar@921
   866
} //END OF NAMESPACE LEMON
alpar@255
   867
alpar@255
   868
#endif
alpar@255
   869