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