src/work/alpar/dijkstra.h
author klao
Thu, 03 Feb 2005 19:27:10 +0000
changeset 1121 8f066fdf6dc9
parent 1117 5767cc417f62
child 1123 a2e93889a604
permissions -rw-r--r--
Obsolete error.h removed
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@1119
    33
  class UninitializedData : public LogicError {};
alpar@1119
    34
alpar@1119
    35
alpar@758
    36
/// \addtogroup flowalgs
alpar@430
    37
/// @{
alpar@430
    38
alpar@954
    39
  ///Default traits class of Dijkstra class.
alpar@954
    40
alpar@954
    41
  ///Default traits class of Dijkstra class.
alpar@954
    42
  ///\param GR Graph type.
alpar@954
    43
  ///\param LM Type of length map.
alpar@953
    44
  template<class GR, class LM>
alpar@953
    45
  struct DijkstraDefaultTraits
alpar@953
    46
  {
alpar@954
    47
    ///The graph type the algorithm runs on. 
alpar@953
    48
    typedef GR Graph;
alpar@953
    49
    ///The type of the map that stores the edge lengths.
alpar@953
    50
alpar@967
    51
    ///It must meet the \ref concept::ReadMap "ReadMap" concept.
alpar@953
    52
    ///
alpar@953
    53
    typedef LM LengthMap;
alpar@954
    54
    //The type of the length of the edges.
alpar@987
    55
    typedef typename LM::Value Value;
alpar@954
    56
    ///The heap type used by Dijkstra algorithm.
alpar@967
    57
alpar@967
    58
    ///The heap type used by Dijkstra algorithm.
alpar@967
    59
    ///
alpar@967
    60
    ///\sa BinHeap
alpar@967
    61
    ///\sa Dijkstra
alpar@953
    62
    typedef BinHeap<typename Graph::Node,
alpar@987
    63
		    typename LM::Value,
alpar@953
    64
		    typename GR::template NodeMap<int>,
alpar@987
    65
		    std::less<Value> > Heap;
alpar@953
    66
alpar@953
    67
    ///\brief The type of the map that stores the last
alpar@953
    68
    ///edges of the shortest paths.
alpar@953
    69
    /// 
alpar@967
    70
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
    71
    ///
alpar@954
    72
    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
alpar@954
    73
    ///Instantiates a PredMap.
alpar@953
    74
 
alpar@953
    75
    ///\todo Please document...
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
    ///
alpar@967
    84
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
    85
    ///
alpar@954
    86
    typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
alpar@954
    87
    ///Instantiates a PredNodeMap.
alpar@953
    88
 
alpar@953
    89
    ///\todo Please document...
alpar@967
    90
    ///
alpar@954
    91
    static PredNodeMap *createPredNodeMap(const GR &G)
alpar@953
    92
    {
alpar@953
    93
      return new PredNodeMap(G);
alpar@953
    94
    }
alpar@1119
    95
alpar@1119
    96
    ///The type of the map that stores whether a nodes is reached.
alpar@1119
    97
 
alpar@1119
    98
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1119
    99
    ///By default it is a NullMap.
alpar@1119
   100
    ///\todo If it is set to a real map, Dijkstra::reached() should read this.
alpar@1119
   101
    ///\todo named parameter to set this type, function to read and write.
alpar@1119
   102
    typedef NullMap<typename Graph::Node,bool> ReachedMap;
alpar@1119
   103
    ///Instantiates a ReachedMap.
alpar@1119
   104
 
alpar@1119
   105
    ///\todo Please document...
alpar@1119
   106
    ///
alpar@1119
   107
    static ReachedMap *createReachedMap(const GR &G)
alpar@1119
   108
    {
alpar@1119
   109
      return new ReachedMap();
alpar@1119
   110
    }
alpar@953
   111
    ///The type of the map that stores the dists of the nodes.
alpar@953
   112
 
alpar@967
   113
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@953
   114
    ///
alpar@987
   115
    typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
alpar@954
   116
    ///Instantiates a DistMap.
alpar@953
   117
 
alpar@953
   118
    ///\todo Please document...
alpar@953
   119
    ///
alpar@954
   120
    static DistMap *createDistMap(const GR &G)
alpar@953
   121
    {
alpar@953
   122
      return new DistMap(G);
alpar@953
   123
    }
alpar@953
   124
  };
alpar@953
   125
  
alpar@255
   126
  ///%Dijkstra algorithm class.
alpar@255
   127
alpar@255
   128
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
   129
  ///The edge lengths are passed to the algorithm using a
klao@959
   130
  ///\ref concept::ReadMap "ReadMap",
alpar@255
   131
  ///so it is easy to change it to any kind of length.
alpar@255
   132
  ///
alpar@880
   133
  ///The type of the length is determined by the
alpar@987
   134
  ///\ref concept::ReadMap::Value "Value" of the length map.
alpar@255
   135
  ///
alpar@255
   136
  ///It is also possible to change the underlying priority heap.
alpar@255
   137
  ///
alpar@953
   138
  ///\param GR The graph type the algorithm runs on. The default value is
alpar@955
   139
  ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
alpar@954
   140
  ///is only passed to \ref DijkstraDefaultTraits.
alpar@584
   141
  ///\param LM This read-only
jacint@385
   142
  ///EdgeMap
jacint@385
   143
  ///determines the
jacint@385
   144
  ///lengths of the edges. It is read once for each edge, so the map
jacint@385
   145
  ///may involve in relatively time consuming process to compute the edge
jacint@385
   146
  ///length if it is necessary. The default map type is
klao@959
   147
  ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
alpar@955
   148
  ///The value of LM is not used directly by Dijkstra, it
alpar@954
   149
  ///is only passed to \ref DijkstraDefaultTraits.
alpar@954
   150
  ///\param TR Traits class to set various data types used by the algorithm.
alpar@954
   151
  ///The default traits class is
alpar@955
   152
  ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
alpar@954
   153
  ///See \ref DijkstraDefaultTraits for the documentation of
alpar@954
   154
  ///a Dijkstra traits class.
alpar@456
   155
  ///
alpar@689
   156
  ///\author Jacint Szabo and Alpar Juttner
alpar@693
   157
  ///\todo We need a typedef-names should be standardized. (-:
alpar@584
   158
alpar@255
   159
#ifdef DOXYGEN
alpar@584
   160
  template <typename GR,
alpar@584
   161
	    typename LM,
alpar@953
   162
	    typename TR>
alpar@255
   163
#else
alpar@953
   164
  template <typename GR=ListGraph,
alpar@584
   165
	    typename LM=typename GR::template EdgeMap<int>,
alpar@953
   166
	    typename TR=DijkstraDefaultTraits<GR,LM> >
alpar@255
   167
#endif
alpar@1116
   168
  class Dijkstra {
alpar@255
   169
  public:
alpar@1119
   170
    ///Exception thrown by dijkstra.
alpar@1119
   171
    class UninitializedData : public lemon::UninitializedData {};
alpar@1119
   172
alpar@953
   173
    typedef TR Traits;
alpar@584
   174
    ///The type of the underlying graph.
alpar@954
   175
    typedef typename TR::Graph Graph;
alpar@911
   176
    ///\e
alpar@255
   177
    typedef typename Graph::Node Node;
alpar@911
   178
    ///\e
alpar@255
   179
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   180
    ///\e
alpar@255
   181
    typedef typename Graph::Edge Edge;
alpar@911
   182
    ///\e
alpar@255
   183
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
   184
    
alpar@584
   185
    ///The type of the length of the edges.
alpar@987
   186
    typedef typename TR::LengthMap::Value Value;
alpar@693
   187
    ///The type of the map that stores the edge lengths.
alpar@954
   188
    typedef typename TR::LengthMap LengthMap;
alpar@693
   189
    ///\brief The type of the map that stores the last
alpar@584
   190
    ///edges of the shortest paths.
alpar@953
   191
    typedef typename TR::PredMap PredMap;
alpar@693
   192
    ///\brief The type of the map that stores the last but one
alpar@584
   193
    ///nodes of the shortest paths.
alpar@953
   194
    typedef typename TR::PredNodeMap PredNodeMap;
alpar@1119
   195
    ///The type of the map indicating if a node is reached.
alpar@1119
   196
    typedef typename TR::ReachedMap ReachedMap;
alpar@693
   197
    ///The type of the map that stores the dists of the nodes.
alpar@953
   198
    typedef typename TR::DistMap DistMap;
alpar@953
   199
    ///The heap type used by the dijkstra algorithm.
alpar@953
   200
    typedef typename TR::Heap Heap;
alpar@255
   201
  private:
alpar@802
   202
    /// Pointer to the underlying graph.
alpar@688
   203
    const Graph *G;
alpar@802
   204
    /// Pointer to the length map
alpar@954
   205
    const LengthMap *length;
alpar@802
   206
    ///Pointer to the map of predecessors edges.
alpar@1119
   207
    PredMap *_pred;
alpar@1119
   208
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1119
   209
    bool local_pred;
alpar@802
   210
    ///Pointer to the map of predecessors nodes.
alpar@688
   211
    PredNodeMap *pred_node;
alpar@802
   212
    ///Indicates if \ref pred_node is locally allocated (\c true) or not.
alpar@688
   213
    bool local_pred_node;
alpar@802
   214
    ///Pointer to the map of distances.
alpar@688
   215
    DistMap *distance;
alpar@802
   216
    ///Indicates if \ref distance is locally allocated (\c true) or not.
alpar@688
   217
    bool local_distance;
alpar@1119
   218
    ///Pointer to the map of reached status of the nodes.
alpar@1119
   219
    ReachedMap *_reached;
alpar@1119
   220
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@1119
   221
    bool local_reached;
alpar@688
   222
alpar@802
   223
    ///The source node of the last execution.
alpar@774
   224
    Node source;
alpar@774
   225
alpar@785
   226
    ///Initializes the maps.
alpar@688
   227
    
alpar@694
   228
    ///\todo Error if \c G or are \c NULL. What about \c length?
alpar@688
   229
    ///\todo Better memory allocation (instead of new).
alpar@688
   230
    void init_maps() 
alpar@688
   231
    {
alpar@1119
   232
      if(!_pred) {
alpar@1119
   233
	local_pred = true;
alpar@1119
   234
	_pred = Traits::createPredMap(*G);
alpar@688
   235
      }
alpar@688
   236
      if(!pred_node) {
alpar@688
   237
	local_pred_node = true;
alpar@953
   238
	pred_node = Traits::createPredNodeMap(*G);
alpar@688
   239
      }
alpar@688
   240
      if(!distance) {
alpar@688
   241
	local_distance = true;
alpar@953
   242
	distance = Traits::createDistMap(*G);
alpar@688
   243
      }
alpar@1119
   244
      if(!_reached) {
alpar@1119
   245
	local_reached = true;
alpar@1119
   246
	_reached = Traits::createReachedMap(*G);
alpar@1119
   247
      }
alpar@688
   248
    }
alpar@255
   249
    
alpar@255
   250
  public :
alpar@1116
   251
 
alpar@953
   252
    template <class T>
alpar@1116
   253
    struct DefPredMapTraits : public Traits {
alpar@953
   254
      typedef T PredMap;
alpar@953
   255
      ///\todo An exception should be thrown.
alpar@953
   256
      ///
alpar@953
   257
      static PredMap *createPredMap(const Graph &G) 
alpar@953
   258
      {
alpar@1119
   259
	throw UninitializedData();
alpar@953
   260
      }
alpar@953
   261
    };
alpar@954
   262
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@954
   263
alpar@954
   264
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1043
   265
    ///
alpar@953
   266
    template <class T>
alpar@1116
   267
    class DefPredMap : public Dijkstra< Graph,
alpar@953
   268
					LengthMap,
alpar@1116
   269
					DefPredMapTraits<T> > { };
alpar@953
   270
    
alpar@953
   271
    template <class T>
alpar@1116
   272
    struct DefPredNodeMapTraits : public Traits {
alpar@953
   273
      typedef T PredNodeMap;
alpar@953
   274
      ///\todo An exception should be thrown.
alpar@953
   275
      ///
alpar@953
   276
      static PredNodeMap *createPredNodeMap(const Graph &G) 
alpar@953
   277
      {
alpar@1119
   278
	throw UninitializedData();
alpar@953
   279
      }
alpar@953
   280
    };
alpar@954
   281
    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
alpar@954
   282
alpar@954
   283
    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
alpar@1043
   284
    ///
alpar@953
   285
    template <class T>
alpar@1116
   286
    class DefPredNodeMap : public Dijkstra< Graph,
alpar@953
   287
					    LengthMap,
alpar@1116
   288
					    DefPredNodeMapTraits<T> > { };
alpar@953
   289
    
alpar@953
   290
    template <class T>
alpar@1116
   291
    struct DefDistMapTraits : public Traits {
alpar@953
   292
      typedef T DistMap;
alpar@953
   293
      ///\todo An exception should be thrown.
alpar@953
   294
      ///
alpar@953
   295
      static DistMap *createDistMap(const Graph &G) 
alpar@953
   296
      {
alpar@1119
   297
	throw UninitializedData();
alpar@953
   298
      }
alpar@953
   299
    };
alpar@954
   300
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@954
   301
alpar@954
   302
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1043
   303
    ///
alpar@953
   304
    template <class T>
alpar@1116
   305
    class DefDistMap : public Dijkstra< Graph,
alpar@953
   306
					LengthMap,
alpar@1116
   307
					DefDistMapTraits<T> > { };
alpar@953
   308
    
alpar@802
   309
    ///Constructor.
alpar@255
   310
    
alpar@802
   311
    ///\param _G the graph the algorithm will run on.
alpar@802
   312
    ///\param _length the length map used by the algorithm.
alpar@954
   313
    Dijkstra(const Graph& _G, const LengthMap& _length) :
alpar@688
   314
      G(&_G), length(&_length),
alpar@1119
   315
      _pred(NULL), local_pred(false),
alpar@707
   316
      pred_node(NULL), local_pred_node(false),
alpar@1119
   317
      distance(NULL), local_distance(false),
alpar@1119
   318
      _reached(NULL), local_reached(false)
alpar@688
   319
    { }
alpar@688
   320
    
alpar@802
   321
    ///Destructor.
alpar@688
   322
    ~Dijkstra() 
alpar@688
   323
    {
alpar@1119
   324
      if(local_pred) delete _pred;
alpar@688
   325
      if(local_pred_node) delete pred_node;
alpar@688
   326
      if(local_distance) delete distance;
alpar@1119
   327
      if(local_reached) delete _reached;
alpar@688
   328
    }
alpar@688
   329
alpar@688
   330
    ///Sets the length map.
alpar@688
   331
alpar@688
   332
    ///Sets the length map.
alpar@688
   333
    ///\return <tt> (*this) </tt>
alpar@1116
   334
    Dijkstra &lengthMap(const LengthMap &m) 
alpar@688
   335
    {
alpar@688
   336
      length = &m;
alpar@688
   337
      return *this;
alpar@688
   338
    }
alpar@688
   339
alpar@688
   340
    ///Sets the map storing the predecessor edges.
alpar@688
   341
alpar@688
   342
    ///Sets the map storing the predecessor edges.
alpar@688
   343
    ///If you don't use this function before calling \ref run(),
alpar@688
   344
    ///it will allocate one. The destuctor deallocates this
alpar@688
   345
    ///automatically allocated map, of course.
alpar@688
   346
    ///\return <tt> (*this) </tt>
alpar@1116
   347
    Dijkstra &predMap(PredMap &m) 
alpar@688
   348
    {
alpar@1119
   349
      if(local_pred) {
alpar@1119
   350
	delete _pred;
alpar@1119
   351
	local_pred=false;
alpar@688
   352
      }
alpar@1119
   353
      _pred = &m;
alpar@688
   354
      return *this;
alpar@688
   355
    }
alpar@688
   356
alpar@688
   357
    ///Sets the map storing the predecessor nodes.
alpar@688
   358
alpar@688
   359
    ///Sets the map storing the predecessor nodes.
alpar@688
   360
    ///If you don't use this function before calling \ref run(),
alpar@688
   361
    ///it will allocate one. The destuctor deallocates this
alpar@688
   362
    ///automatically allocated map, of course.
alpar@688
   363
    ///\return <tt> (*this) </tt>
alpar@1116
   364
    Dijkstra &predNodeMap(PredNodeMap &m) 
alpar@688
   365
    {
alpar@688
   366
      if(local_pred_node) {
alpar@688
   367
	delete pred_node;
alpar@688
   368
	local_pred_node=false;
alpar@688
   369
      }
alpar@688
   370
      pred_node = &m;
alpar@688
   371
      return *this;
alpar@688
   372
    }
alpar@688
   373
alpar@688
   374
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   375
alpar@688
   376
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   377
    ///If you don't use this function before calling \ref run(),
alpar@688
   378
    ///it will allocate one. The destuctor deallocates this
alpar@688
   379
    ///automatically allocated map, of course.
alpar@688
   380
    ///\return <tt> (*this) </tt>
alpar@1116
   381
    Dijkstra &distMap(DistMap &m) 
alpar@688
   382
    {
alpar@688
   383
      if(local_distance) {
alpar@688
   384
	delete distance;
alpar@688
   385
	local_distance=false;
alpar@688
   386
      }
alpar@688
   387
      distance = &m;
alpar@688
   388
      return *this;
alpar@688
   389
    }
alpar@255
   390
    
alpar@694
   391
  ///Runs %Dijkstra algorithm from node \c s.
alpar@694
   392
alpar@694
   393
  ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@694
   394
  ///in order to
alpar@694
   395
  ///compute the
alpar@694
   396
  ///shortest path to each node. The algorithm computes
alpar@694
   397
  ///- The shortest path tree.
alpar@694
   398
  ///- The distance of each node from the root.
alpar@954
   399
  ///\todo heap_map's type could also be in the traits class.
alpar@694
   400
    void run(Node s) {
alpar@694
   401
      
alpar@694
   402
      init_maps();
alpar@694
   403
      
alpar@774
   404
      source = s;
alpar@774
   405
      
alpar@774
   406
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1119
   407
	_pred->set(u,INVALID);
alpar@694
   408
	pred_node->set(u,INVALID);
alpar@1119
   409
	///\todo *_reached is not set to false.
alpar@694
   410
      }
alpar@694
   411
      
alpar@954
   412
      typename Graph::template NodeMap<int> heap_map(*G,-1);
alpar@694
   413
      
alpar@953
   414
      Heap heap(heap_map);
alpar@694
   415
      
alpar@694
   416
      heap.push(s,0); 
alpar@694
   417
      
alpar@694
   418
      while ( !heap.empty() ) {
alpar@694
   419
	
alpar@694
   420
	Node v=heap.top(); 
alpar@1119
   421
	_reached->set(v,true);
alpar@987
   422
	Value oldvalue=heap[v];
alpar@694
   423
	heap.pop();
alpar@694
   424
	distance->set(v, oldvalue);
alpar@694
   425
	
alpar@694
   426
	
alpar@774
   427
	for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@986
   428
	  Node w=G->target(e); 
alpar@694
   429
	  switch(heap.state(w)) {
alpar@953
   430
	  case Heap::PRE_HEAP:
alpar@694
   431
	    heap.push(w,oldvalue+(*length)[e]); 
alpar@1119
   432
	    _pred->set(w,e);
alpar@694
   433
	    pred_node->set(w,v);
alpar@694
   434
	    break;
alpar@953
   435
	  case Heap::IN_HEAP:
alpar@694
   436
	    if ( oldvalue+(*length)[e] < heap[w] ) {
alpar@694
   437
	      heap.decrease(w, oldvalue+(*length)[e]); 
alpar@1119
   438
	      _pred->set(w,e);
alpar@694
   439
	      pred_node->set(w,v);
alpar@694
   440
	    }
alpar@694
   441
	    break;
alpar@953
   442
	  case Heap::POST_HEAP:
alpar@694
   443
	    break;
alpar@694
   444
	  }
alpar@694
   445
	}
alpar@694
   446
      }
alpar@694
   447
    }
alpar@255
   448
    
jacint@385
   449
    ///The distance of a node from the root.
alpar@255
   450
jacint@385
   451
    ///Returns the distance of a node from the root.
alpar@255
   452
    ///\pre \ref run() must be called before using this function.
jacint@385
   453
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   454
    ///of this funcion is undefined.
alpar@987
   455
    Value dist(Node v) const { return (*distance)[v]; }
jacint@373
   456
alpar@584
   457
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   458
alpar@584
   459
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@785
   460
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@688
   461
    ///v. It is \ref INVALID
alpar@688
   462
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   463
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
   464
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
   465
    ///this function.
alpar@780
   466
    ///\todo predEdge could be a better name.
alpar@1119
   467
    Edge pred(Node v) const { return (*_pred)[v]; }
jacint@373
   468
alpar@584
   469
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   470
alpar@584
   471
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   472
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   473
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   474
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
   475
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
   476
    ///using this function.
alpar@688
   477
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@255
   478
    
alpar@255
   479
    ///Returns a reference to the NodeMap of distances.
alpar@255
   480
jacint@385
   481
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   482
    ///be called before using this function.
alpar@688
   483
    const DistMap &distMap() const { return *distance;}
jacint@385
   484
 
alpar@255
   485
    ///Returns a reference to the shortest path tree map.
alpar@255
   486
alpar@255
   487
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   488
    ///shortest path tree.
alpar@255
   489
    ///\pre \ref run() must be called before using this function.
alpar@1119
   490
    const PredMap &predMap() const { return *_pred;}
jacint@385
   491
 
jacint@385
   492
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   493
alpar@255
   494
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   495
    ///shortest path tree.
alpar@255
   496
    ///\pre \ref run() must be called before using this function.
alpar@688
   497
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@255
   498
jacint@385
   499
    ///Checks if a node is reachable from the root.
alpar@255
   500
jacint@385
   501
    ///Returns \c true if \c v is reachable from the root.
alpar@802
   502
    ///\note The root node is reported to be reached!
alpar@255
   503
    ///\pre \ref run() must be called before using this function.
jacint@385
   504
    ///
alpar@1119
   505
    bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
alpar@255
   506
    
alpar@255
   507
  };
alpar@953
   508
alpar@1116
   509
  template<class GR,class LM>
alpar@1116
   510
  class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
alpar@1116
   511
  {
alpar@1116
   512
alpar@1116
   513
    typedef DijkstraDefaultTraits<GR,LM> Base;
alpar@1116
   514
  protected:
alpar@1116
   515
    /// Pointer to the underlying graph.
alpar@1116
   516
    void *_g;
alpar@1116
   517
    /// Pointer to the length map
alpar@1116
   518
    void *_length;
alpar@1116
   519
    ///Pointer to the map of predecessors edges.
alpar@1116
   520
    void *_pred;
alpar@1116
   521
    ///Pointer to the map of predecessors nodes.
alpar@1116
   522
    void *_predNode;
alpar@1116
   523
    ///Pointer to the map of distances.
alpar@1116
   524
    void *_dist;
alpar@1116
   525
    ///Pointer to the source node.
alpar@1116
   526
    void *_source;
alpar@1116
   527
alpar@1116
   528
    typedef typename Base::Graph::Node Node;
alpar@1116
   529
alpar@1116
   530
    public:
alpar@1116
   531
    DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
alpar@1116
   532
		       _dist(0), _source(INVALID) {}
alpar@1116
   533
alpar@1116
   534
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
alpar@1116
   535
      _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
alpar@1116
   536
		  _dist(0), _source((void *)&s) {}
alpar@1116
   537
alpar@1116
   538
  };
alpar@1116
   539
  
alpar@953
   540
  ///\e
alpar@953
   541
alpar@953
   542
  ///\e
alpar@953
   543
  ///
alpar@953
   544
  template<class TR>
alpar@1116
   545
  class DijkstraWizard : public TR
alpar@953
   546
  {
alpar@1116
   547
    typedef TR Base;
alpar@953
   548
alpar@1119
   549
    //The type of the underlying graph.
alpar@953
   550
    typedef typename TR::Graph Graph;
alpar@1119
   551
    //\e
alpar@953
   552
    typedef typename Graph::Node Node;
alpar@1119
   553
    //\e
alpar@953
   554
    typedef typename Graph::NodeIt NodeIt;
alpar@1119
   555
    //\e
alpar@953
   556
    typedef typename Graph::Edge Edge;
alpar@1119
   557
    //\e
alpar@953
   558
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
   559
    
alpar@1119
   560
    //The type of the map that stores the edge lengths.
alpar@953
   561
    typedef typename TR::LengthMap LengthMap;
alpar@1119
   562
    //The type of the length of the edges.
alpar@987
   563
    typedef typename LengthMap::Value Value;
alpar@1119
   564
    //\brief The type of the map that stores the last
alpar@1119
   565
    //edges of the shortest paths.
alpar@953
   566
    typedef typename TR::PredMap PredMap;
alpar@1119
   567
    //\brief The type of the map that stores the last but one
alpar@1119
   568
    //nodes of the shortest paths.
alpar@953
   569
    typedef typename TR::PredNodeMap PredNodeMap;
alpar@1119
   570
    //The type of the map that stores the dists of the nodes.
alpar@953
   571
    typedef typename TR::DistMap DistMap;
alpar@953
   572
alpar@1119
   573
    //The heap type used by the dijkstra algorithm.
alpar@953
   574
    typedef typename TR::Heap Heap;
alpar@1116
   575
public:
alpar@1116
   576
    DijkstraWizard() : TR() {}
alpar@953
   577
alpar@1116
   578
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
   579
      TR(g,l,s) {}
alpar@953
   580
alpar@1116
   581
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
   582
alpar@1116
   583
    ~DijkstraWizard() {}
alpar@1116
   584
alpar@1116
   585
    ///\e
alpar@1116
   586
    void run()
alpar@953
   587
    {
alpar@1119
   588
      if(_source==0) throw UninitializedData();
alpar@1116
   589
      Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
alpar@1116
   590
      if(_pred) Dij.predMap(*(PredMap*)_pred);
alpar@1116
   591
      if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
alpar@1116
   592
      if(_dist) Dij.distMap(*(DistMap*)_dist);
alpar@1116
   593
      Dij.run(*(Node*)_source);
alpar@1116
   594
    }
alpar@1116
   595
alpar@1116
   596
    ///\e
alpar@1116
   597
    void run(Node s)
alpar@1116
   598
    {
alpar@1116
   599
      _source=(void *)&s;
alpar@1116
   600
      run();
alpar@953
   601
    }
alpar@953
   602
alpar@953
   603
    template<class T>
alpar@1116
   604
    struct DefPredMapBase : public Base {
alpar@1116
   605
      typedef T PredMap;
alpar@1117
   606
      static PredMap *createPredMap(const Graph &G) { return 0; };
alpar@1117
   607
      DefPredMapBase(const Base &b) : Base(b) {}
alpar@1116
   608
    };
alpar@953
   609
    
alpar@953
   610
    ///\e
alpar@953
   611
    template<class T>
alpar@1116
   612
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
   613
    {
alpar@1116
   614
      _pred=(void *)&t;
alpar@1116
   615
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
   616
    }
alpar@953
   617
    
alpar@1116
   618
alpar@953
   619
    template<class T>
alpar@1116
   620
    struct DefPredNodeMapBase : public Base {
alpar@1116
   621
      typedef T PredNodeMap;
alpar@1117
   622
      static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
alpar@1117
   623
      DefPredNodeMapBase(const Base &b) : Base(b) {}
alpar@1116
   624
    };
alpar@1116
   625
    
alpar@953
   626
    ///\e
alpar@953
   627
    template<class T>
alpar@1116
   628
    DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
alpar@953
   629
    {
alpar@1116
   630
      _predNode=(void *)&t;
alpar@1116
   631
      return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
alpar@953
   632
    }
alpar@1116
   633
   
alpar@1116
   634
    template<class T>
alpar@1116
   635
    struct DefDistMapBase : public Base {
alpar@1116
   636
      typedef T DistMap;
alpar@1117
   637
      static DistMap *createDistMap(const Graph &G) { return 0; };
alpar@1117
   638
      DefDistMapBase(const Base &b) : Base(b) {}
alpar@1116
   639
    };
alpar@953
   640
    
alpar@953
   641
    ///\e
alpar@953
   642
    template<class T>
alpar@1116
   643
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
   644
    {
alpar@1116
   645
      _dist=(void *)&t;
alpar@1116
   646
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
   647
    }
alpar@1117
   648
    
alpar@953
   649
    ///\e
alpar@1117
   650
    DijkstraWizard<TR> &source(Node s) 
alpar@953
   651
    {
alpar@1116
   652
      source=(void *)&s;
alpar@953
   653
      return *this;
alpar@953
   654
    }
alpar@953
   655
    
alpar@953
   656
  };
alpar@255
   657
  
alpar@953
   658
  ///\e
alpar@953
   659
alpar@954
   660
  ///\todo Please document...
alpar@953
   661
  ///
alpar@953
   662
  template<class GR, class LM>
alpar@1116
   663
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
   664
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
   665
  {
alpar@1116
   666
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
   667
  }
alpar@953
   668
alpar@430
   669
/// @}
alpar@255
   670
  
alpar@921
   671
} //END OF NAMESPACE LEMON
alpar@255
   672
alpar@255
   673
#endif
alpar@255
   674