src/work/alpar/dijkstra.h
author alpar
Wed, 02 Feb 2005 11:54:55 +0000
changeset 1116 f97e1cbbd453
parent 1043 52a2201a88e9
child 1117 5767cc417f62
permissions -rw-r--r--
- More or less follows the new naming convetions
- New implementation for dijkstra();
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@1116
   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@1116
   216
 
alpar@953
   217
    template <class T>
alpar@1116
   218
    struct DefPredMapTraits : 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@1116
   234
    class DefPredMap : public Dijkstra< Graph,
alpar@953
   235
					LengthMap,
alpar@1116
   236
					DefPredMapTraits<T> > { };
alpar@953
   237
    
alpar@953
   238
    template <class T>
alpar@1116
   239
    struct DefPredNodeMapTraits : 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@1116
   255
    class DefPredNodeMap : public Dijkstra< Graph,
alpar@953
   256
					    LengthMap,
alpar@1116
   257
					    DefPredNodeMapTraits<T> > { };
alpar@953
   258
    
alpar@953
   259
    template <class T>
alpar@1116
   260
    struct DefDistMapTraits : 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@1116
   276
    class DefDistMap : public Dijkstra< Graph,
alpar@953
   277
					LengthMap,
alpar@1116
   278
					DefDistMapTraits<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@1116
   303
    Dijkstra &lengthMap(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@1116
   316
    Dijkstra &predMap(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@1116
   333
    Dijkstra &predNodeMap(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@1116
   350
    Dijkstra &distMap(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@1116
   476
  template<class GR,class LM>
alpar@1116
   477
  class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
alpar@1116
   478
  {
alpar@1116
   479
alpar@1116
   480
    typedef DijkstraDefaultTraits<GR,LM> Base;
alpar@1116
   481
  protected:
alpar@1116
   482
    /// Pointer to the underlying graph.
alpar@1116
   483
    void *_g;
alpar@1116
   484
    /// Pointer to the length map
alpar@1116
   485
    void *_length;
alpar@1116
   486
    ///Pointer to the map of predecessors edges.
alpar@1116
   487
    void *_pred;
alpar@1116
   488
    ///Pointer to the map of predecessors nodes.
alpar@1116
   489
    void *_predNode;
alpar@1116
   490
    ///Pointer to the map of distances.
alpar@1116
   491
    void *_dist;
alpar@1116
   492
    ///Pointer to the source node.
alpar@1116
   493
    void *_source;
alpar@1116
   494
alpar@1116
   495
    typedef typename Base::Graph::Node Node;
alpar@1116
   496
alpar@1116
   497
    public:
alpar@1116
   498
    DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
alpar@1116
   499
		       _dist(0), _source(INVALID) {}
alpar@1116
   500
alpar@1116
   501
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
alpar@1116
   502
      _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
alpar@1116
   503
		  _dist(0), _source((void *)&s) {}
alpar@1116
   504
alpar@1116
   505
  };
alpar@1116
   506
  
alpar@953
   507
  ///\e
alpar@953
   508
alpar@953
   509
  ///\e
alpar@953
   510
  ///
alpar@953
   511
  template<class TR>
alpar@1116
   512
  class DijkstraWizard : public TR
alpar@953
   513
  {
alpar@1116
   514
    typedef TR Base;
alpar@953
   515
alpar@953
   516
    ///The type of the underlying graph.
alpar@953
   517
    typedef typename TR::Graph Graph;
alpar@953
   518
    ///\e
alpar@953
   519
    typedef typename Graph::Node Node;
alpar@953
   520
    ///\e
alpar@953
   521
    typedef typename Graph::NodeIt NodeIt;
alpar@953
   522
    ///\e
alpar@953
   523
    typedef typename Graph::Edge Edge;
alpar@953
   524
    ///\e
alpar@953
   525
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
   526
    
alpar@953
   527
    ///The type of the map that stores the edge lengths.
alpar@953
   528
    typedef typename TR::LengthMap LengthMap;
alpar@953
   529
    ///The type of the length of the edges.
alpar@987
   530
    typedef typename LengthMap::Value Value;
alpar@953
   531
    ///\brief The type of the map that stores the last
alpar@953
   532
    ///edges of the shortest paths.
alpar@953
   533
    typedef typename TR::PredMap PredMap;
alpar@953
   534
    ///\brief The type of the map that stores the last but one
alpar@953
   535
    ///nodes of the shortest paths.
alpar@953
   536
    typedef typename TR::PredNodeMap PredNodeMap;
alpar@953
   537
    ///The type of the map that stores the dists of the nodes.
alpar@953
   538
    typedef typename TR::DistMap DistMap;
alpar@953
   539
alpar@953
   540
    ///The heap type used by the dijkstra algorithm.
alpar@953
   541
    typedef typename TR::Heap Heap;
alpar@1116
   542
public:
alpar@1116
   543
    DijkstraWizard() : TR() {}
alpar@953
   544
alpar@1116
   545
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
   546
      TR(g,l,s) {}
alpar@953
   547
alpar@1116
   548
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
   549
alpar@1116
   550
    ~DijkstraWizard() {}
alpar@1116
   551
alpar@1116
   552
    ///\e
alpar@1116
   553
    void run()
alpar@953
   554
    {
alpar@1116
   555
      Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
alpar@1116
   556
      if(_pred) Dij.predMap(*(PredMap*)_pred);
alpar@1116
   557
      if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
alpar@1116
   558
      if(_dist) Dij.distMap(*(DistMap*)_dist);
alpar@1116
   559
      Dij.run(*(Node*)_source);
alpar@1116
   560
    }
alpar@1116
   561
alpar@1116
   562
    ///\e
alpar@1116
   563
    void run(Node s)
alpar@1116
   564
    {
alpar@1116
   565
      _source=(void *)&s;
alpar@1116
   566
      run();
alpar@953
   567
    }
alpar@953
   568
alpar@953
   569
    template<class T>
alpar@1116
   570
    struct DefPredMapBase : public Base {
alpar@1116
   571
      typedef T PredMap;
alpar@1116
   572
      static PredMap *createPredMap(const Graph &G) {};
alpar@1116
   573
    };
alpar@953
   574
    
alpar@953
   575
    ///\e
alpar@953
   576
    template<class T>
alpar@1116
   577
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
   578
    {
alpar@1116
   579
      _pred=(void *)&t;
alpar@1116
   580
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
   581
    }
alpar@953
   582
    
alpar@1116
   583
alpar@953
   584
    template<class T>
alpar@1116
   585
    struct DefPredNodeMapBase : public Base {
alpar@1116
   586
      typedef T PredNodeMap;
alpar@1116
   587
      static PredNodeMap *createPredNodeMap(const Graph &G) {};
alpar@1116
   588
    };
alpar@1116
   589
    
alpar@953
   590
    ///\e
alpar@953
   591
    template<class T>
alpar@1116
   592
    DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
alpar@953
   593
    {
alpar@1116
   594
      _predNode=(void *)&t;
alpar@1116
   595
      return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
alpar@953
   596
    }
alpar@1116
   597
   
alpar@1116
   598
    template<class T>
alpar@1116
   599
    struct DefDistMapBase : public Base {
alpar@1116
   600
      typedef T DistMap;
alpar@1116
   601
      static DistMap *createDistMap(const Graph &G) {};
alpar@1116
   602
    };
alpar@953
   603
    
alpar@953
   604
    ///\e
alpar@953
   605
    template<class T>
alpar@1116
   606
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
   607
    {
alpar@1116
   608
      _dist=(void *)&t;
alpar@1116
   609
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
   610
    }
alpar@1116
   611
alpar@953
   612
    ///\e
alpar@1116
   613
    DijkstraWizard<TR> &setSource(Node s) 
alpar@953
   614
    {
alpar@1116
   615
      source=(void *)&s;
alpar@953
   616
      return *this;
alpar@953
   617
    }
alpar@953
   618
    
alpar@953
   619
  };
alpar@255
   620
  
alpar@953
   621
  ///\e
alpar@953
   622
alpar@954
   623
  ///\todo Please document...
alpar@953
   624
  ///
alpar@953
   625
  template<class GR, class LM>
alpar@1116
   626
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
   627
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
   628
  {
alpar@1116
   629
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
   630
  }
alpar@953
   631
alpar@430
   632
/// @}
alpar@255
   633
  
alpar@921
   634
} //END OF NAMESPACE LEMON
alpar@255
   635
alpar@255
   636
#endif
alpar@255
   637
alpar@255
   638