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