src/hugo/dijkstra.h
author marci
Thu, 02 Sep 2004 17:56:40 +0000
changeset 792 147eb3a58706
parent 780 e06d0d16595f
child 802 bc0c74eeb151
permissions -rw-r--r--
Nicer and more documented graph_wrapper.h file.
These are only the first steps for making this file more beautiful.
alpar@255
     1
// -*- C++ -*-
alpar@255
     2
#ifndef HUGO_DIJKSTRA_H
alpar@255
     3
#define HUGO_DIJKSTRA_H
alpar@255
     4
alpar@758
     5
///\ingroup flowalgs
alpar@255
     6
///\file
alpar@255
     7
///\brief Dijkstra algorithm.
alpar@255
     8
ladanyi@542
     9
#include <hugo/bin_heap.h>
ladanyi@542
    10
#include <hugo/invalid.h>
alpar@255
    11
alpar@255
    12
namespace hugo {
jacint@385
    13
alpar@758
    14
/// \addtogroup flowalgs
alpar@430
    15
/// @{
alpar@430
    16
alpar@255
    17
  ///%Dijkstra algorithm class.
alpar@255
    18
alpar@255
    19
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
    20
  ///The edge lengths are passed to the algorithm using a
alpar@255
    21
  ///\ref ReadMapSkeleton "readable map",
alpar@255
    22
  ///so it is easy to change it to any kind of length.
alpar@255
    23
  ///
alpar@255
    24
  ///The type of the length is determined by the \c ValueType of the length map.
alpar@255
    25
  ///
alpar@255
    26
  ///It is also possible to change the underlying priority heap.
alpar@255
    27
  ///
alpar@584
    28
  ///\param GR The graph type the algorithm runs on.
alpar@584
    29
  ///\param LM This read-only
jacint@385
    30
  ///EdgeMap
jacint@385
    31
  ///determines the
jacint@385
    32
  ///lengths of the edges. It is read once for each edge, so the map
jacint@385
    33
  ///may involve in relatively time consuming process to compute the edge
jacint@385
    34
  ///length if it is necessary. The default map type is
jacint@385
    35
  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
jacint@385
    36
  ///\param Heap The heap type used by the %Dijkstra
jacint@385
    37
  ///algorithm. The default
jacint@385
    38
  ///is using \ref BinHeap "binary heap".
alpar@456
    39
  ///
alpar@689
    40
  ///\author Jacint Szabo and Alpar Juttner
alpar@693
    41
  ///\todo We need a typedef-names should be standardized. (-:
alpar@734
    42
  ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
alpar@734
    43
  ///should not be fixed. (Problematic to solve).
alpar@584
    44
alpar@255
    45
#ifdef DOXYGEN
alpar@584
    46
  template <typename GR,
alpar@584
    47
	    typename LM,
alpar@255
    48
	    typename Heap>
alpar@255
    49
#else
alpar@584
    50
  template <typename GR,
alpar@584
    51
	    typename LM=typename GR::template EdgeMap<int>,
alpar@532
    52
	    template <class,class,class,class> class Heap = BinHeap >
alpar@255
    53
#endif
alpar@255
    54
  class Dijkstra{
alpar@255
    55
  public:
alpar@584
    56
    ///The type of the underlying graph.
alpar@584
    57
    typedef GR Graph;
alpar@255
    58
    typedef typename Graph::Node Node;
alpar@255
    59
    typedef typename Graph::NodeIt NodeIt;
alpar@255
    60
    typedef typename Graph::Edge Edge;
alpar@255
    61
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
    62
    
alpar@584
    63
    ///The type of the length of the edges.
alpar@584
    64
    typedef typename LM::ValueType ValueType;
alpar@693
    65
    ///The type of the map that stores the edge lengths.
alpar@584
    66
    typedef LM LengthMap;
alpar@693
    67
    ///\brief The type of the map that stores the last
alpar@584
    68
    ///edges of the shortest paths.
marci@433
    69
    typedef typename Graph::template NodeMap<Edge> PredMap;
alpar@693
    70
    ///\brief The type of the map that stores the last but one
alpar@584
    71
    ///nodes of the shortest paths.
marci@433
    72
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
alpar@693
    73
    ///The type of the map that stores the dists of the nodes.
marci@433
    74
    typedef typename Graph::template NodeMap<ValueType> DistMap;
alpar@255
    75
alpar@255
    76
  private:
alpar@688
    77
    const Graph *G;
alpar@688
    78
    const LM *length;
alpar@688
    79
    //    bool local_length;
alpar@688
    80
    PredMap *predecessor;
alpar@688
    81
    bool local_predecessor;
alpar@688
    82
    PredNodeMap *pred_node;
alpar@688
    83
    bool local_pred_node;
alpar@688
    84
    DistMap *distance;
alpar@688
    85
    bool local_distance;
alpar@688
    86
alpar@774
    87
    //The source node of the last execution.
alpar@774
    88
    Node source;
alpar@774
    89
alpar@785
    90
    ///Initializes the maps.
alpar@688
    91
    
alpar@694
    92
    ///\todo Error if \c G or are \c NULL. What about \c length?
alpar@688
    93
    ///\todo Better memory allocation (instead of new).
alpar@688
    94
    void init_maps() 
alpar@688
    95
    {
alpar@688
    96
//       if(!length) {
alpar@688
    97
// 	local_length = true;
alpar@688
    98
// 	length = new LM(G);
alpar@688
    99
//       }
alpar@688
   100
      if(!predecessor) {
alpar@688
   101
	local_predecessor = true;
alpar@688
   102
	predecessor = new PredMap(*G);
alpar@688
   103
      }
alpar@688
   104
      if(!pred_node) {
alpar@688
   105
	local_pred_node = true;
alpar@688
   106
	pred_node = new PredNodeMap(*G);
alpar@688
   107
      }
alpar@688
   108
      if(!distance) {
alpar@688
   109
	local_distance = true;
alpar@688
   110
	distance = new DistMap(*G);
alpar@688
   111
      }
alpar@688
   112
    }
alpar@255
   113
    
alpar@255
   114
  public :
alpar@255
   115
    
alpar@584
   116
    Dijkstra(const Graph& _G, const LM& _length) :
alpar@688
   117
      G(&_G), length(&_length),
alpar@707
   118
      predecessor(NULL), local_predecessor(false),
alpar@707
   119
      pred_node(NULL), local_pred_node(false),
alpar@707
   120
      distance(NULL), local_distance(false)
alpar@688
   121
    { }
alpar@688
   122
    
alpar@688
   123
    ~Dijkstra() 
alpar@688
   124
    {
alpar@688
   125
      //      if(local_length) delete length;
alpar@688
   126
      if(local_predecessor) delete predecessor;
alpar@688
   127
      if(local_pred_node) delete pred_node;
alpar@688
   128
      if(local_distance) delete distance;
alpar@688
   129
    }
alpar@688
   130
alpar@688
   131
    ///Sets the graph the algorithm will run on.
alpar@688
   132
alpar@688
   133
    ///Sets the graph the algorithm will run on.
alpar@688
   134
    ///\return <tt> (*this) </tt>
alpar@785
   135
    ///\bug What about maps?
alpar@785
   136
    ///\todo It may be unnecessary
alpar@688
   137
    Dijkstra &setGraph(const Graph &_G) 
alpar@688
   138
    {
alpar@688
   139
      G = &_G;
alpar@688
   140
      return *this;
alpar@688
   141
    }
alpar@688
   142
    ///Sets the length map.
alpar@688
   143
alpar@688
   144
    ///Sets the length map.
alpar@688
   145
    ///\return <tt> (*this) </tt>
alpar@688
   146
    Dijkstra &setLengthMap(const LM &m) 
alpar@688
   147
    {
alpar@688
   148
//       if(local_length) {
alpar@688
   149
// 	delete length;
alpar@688
   150
// 	local_length=false;
alpar@688
   151
//       }
alpar@688
   152
      length = &m;
alpar@688
   153
      return *this;
alpar@688
   154
    }
alpar@688
   155
alpar@688
   156
    ///Sets the map storing the predecessor edges.
alpar@688
   157
alpar@688
   158
    ///Sets the map storing the predecessor edges.
alpar@688
   159
    ///If you don't use this function before calling \ref run(),
alpar@688
   160
    ///it will allocate one. The destuctor deallocates this
alpar@688
   161
    ///automatically allocated map, of course.
alpar@688
   162
    ///\return <tt> (*this) </tt>
alpar@688
   163
    Dijkstra &setPredMap(PredMap &m) 
alpar@688
   164
    {
alpar@688
   165
      if(local_predecessor) {
alpar@688
   166
	delete predecessor;
alpar@688
   167
	local_predecessor=false;
alpar@688
   168
      }
alpar@688
   169
      predecessor = &m;
alpar@688
   170
      return *this;
alpar@688
   171
    }
alpar@688
   172
alpar@688
   173
    ///Sets the map storing the predecessor nodes.
alpar@688
   174
alpar@688
   175
    ///Sets the map storing the predecessor nodes.
alpar@688
   176
    ///If you don't use this function before calling \ref run(),
alpar@688
   177
    ///it will allocate one. The destuctor deallocates this
alpar@688
   178
    ///automatically allocated map, of course.
alpar@688
   179
    ///\return <tt> (*this) </tt>
alpar@688
   180
    Dijkstra &setPredNodeMap(PredNodeMap &m) 
alpar@688
   181
    {
alpar@688
   182
      if(local_pred_node) {
alpar@688
   183
	delete pred_node;
alpar@688
   184
	local_pred_node=false;
alpar@688
   185
      }
alpar@688
   186
      pred_node = &m;
alpar@688
   187
      return *this;
alpar@688
   188
    }
alpar@688
   189
alpar@688
   190
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   191
alpar@688
   192
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   193
    ///If you don't use this function before calling \ref run(),
alpar@688
   194
    ///it will allocate one. The destuctor deallocates this
alpar@688
   195
    ///automatically allocated map, of course.
alpar@688
   196
    ///\return <tt> (*this) </tt>
alpar@688
   197
    Dijkstra &setDistMap(DistMap &m) 
alpar@688
   198
    {
alpar@688
   199
      if(local_distance) {
alpar@688
   200
	delete distance;
alpar@688
   201
	local_distance=false;
alpar@688
   202
      }
alpar@688
   203
      distance = &m;
alpar@688
   204
      return *this;
alpar@688
   205
    }
alpar@255
   206
    
alpar@694
   207
  ///Runs %Dijkstra algorithm from node \c s.
alpar@694
   208
alpar@694
   209
  ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@694
   210
  ///in order to
alpar@694
   211
  ///compute the
alpar@694
   212
  ///shortest path to each node. The algorithm computes
alpar@694
   213
  ///- The shortest path tree.
alpar@694
   214
  ///- The distance of each node from the root.
alpar@694
   215
    
alpar@694
   216
    void run(Node s) {
alpar@694
   217
      
alpar@694
   218
      init_maps();
alpar@694
   219
      
alpar@774
   220
      source = s;
alpar@774
   221
      
alpar@774
   222
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@694
   223
	predecessor->set(u,INVALID);
alpar@694
   224
	pred_node->set(u,INVALID);
alpar@694
   225
      }
alpar@694
   226
      
alpar@694
   227
      typename GR::template NodeMap<int> heap_map(*G,-1);
alpar@694
   228
      
alpar@694
   229
      typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
alpar@694
   230
      std::less<ValueType> > 
alpar@694
   231
      HeapType;
alpar@694
   232
      
alpar@694
   233
      HeapType heap(heap_map);
alpar@694
   234
      
alpar@694
   235
      heap.push(s,0); 
alpar@694
   236
      
alpar@694
   237
      while ( !heap.empty() ) {
alpar@694
   238
	
alpar@694
   239
	Node v=heap.top(); 
alpar@694
   240
	ValueType oldvalue=heap[v];
alpar@694
   241
	heap.pop();
alpar@694
   242
	distance->set(v, oldvalue);
alpar@694
   243
	
alpar@694
   244
	
alpar@774
   245
	for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@774
   246
	  Node w=G->head(e); 
alpar@694
   247
	  switch(heap.state(w)) {
alpar@694
   248
	  case HeapType::PRE_HEAP:
alpar@694
   249
	    heap.push(w,oldvalue+(*length)[e]); 
alpar@694
   250
	    predecessor->set(w,e);
alpar@694
   251
	    pred_node->set(w,v);
alpar@694
   252
	    break;
alpar@694
   253
	  case HeapType::IN_HEAP:
alpar@694
   254
	    if ( oldvalue+(*length)[e] < heap[w] ) {
alpar@694
   255
	      heap.decrease(w, oldvalue+(*length)[e]); 
alpar@694
   256
	      predecessor->set(w,e);
alpar@694
   257
	      pred_node->set(w,v);
alpar@694
   258
	    }
alpar@694
   259
	    break;
alpar@694
   260
	  case HeapType::POST_HEAP:
alpar@694
   261
	    break;
alpar@694
   262
	  }
alpar@694
   263
	}
alpar@694
   264
      }
alpar@694
   265
    }
alpar@255
   266
    
jacint@385
   267
    ///The distance of a node from the root.
alpar@255
   268
jacint@385
   269
    ///Returns the distance of a node from the root.
alpar@255
   270
    ///\pre \ref run() must be called before using this function.
jacint@385
   271
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   272
    ///of this funcion is undefined.
alpar@688
   273
    ValueType dist(Node v) const { return (*distance)[v]; }
jacint@373
   274
alpar@584
   275
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   276
alpar@584
   277
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@785
   278
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@688
   279
    ///v. It is \ref INVALID
alpar@688
   280
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   281
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
   282
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
   283
    ///this function.
alpar@780
   284
    ///\todo predEdge could be a better name.
alpar@688
   285
    Edge pred(Node v) const { return (*predecessor)[v]; }
jacint@373
   286
alpar@584
   287
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   288
alpar@584
   289
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   290
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   291
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   292
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
   293
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
   294
    ///using this function.
alpar@688
   295
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@255
   296
    
alpar@255
   297
    ///Returns a reference to the NodeMap of distances.
alpar@255
   298
jacint@385
   299
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   300
    ///be called before using this function.
alpar@688
   301
    const DistMap &distMap() const { return *distance;}
jacint@385
   302
 
alpar@255
   303
    ///Returns a reference to the shortest path tree map.
alpar@255
   304
alpar@255
   305
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   306
    ///shortest path tree.
alpar@255
   307
    ///\pre \ref run() must be called before using this function.
alpar@688
   308
    const PredMap &predMap() const { return *predecessor;}
jacint@385
   309
 
jacint@385
   310
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   311
alpar@255
   312
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   313
    ///shortest path tree.
alpar@255
   314
    ///\pre \ref run() must be called before using this function.
alpar@688
   315
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@255
   316
jacint@385
   317
    ///Checks if a node is reachable from the root.
alpar@255
   318
jacint@385
   319
    ///Returns \c true if \c v is reachable from the root.
alpar@774
   320
    ///\warning the root node is reported to be reached!
alpar@255
   321
    ///\pre \ref run() must be called before using this function.
jacint@385
   322
    ///
alpar@780
   323
    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
alpar@255
   324
    
alpar@255
   325
  };
alpar@255
   326
  
alpar@430
   327
/// @}
alpar@255
   328
  
alpar@255
   329
} //END OF NAMESPACE HUGO
alpar@255
   330
alpar@255
   331
#endif
alpar@255
   332
alpar@255
   333