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