src/hugo/dijkstra.h
author alpar
Wed, 30 Jun 2004 14:50:31 +0000
changeset 688 bdc429a557f2
parent 584 1d4855f5312e
child 689 e7cf90de549a
permissions -rw-r--r--
- Now, it is possible to have Dijkstra store its result directly in given maps.
- More docs.
alpar@255
     1
// -*- C++ -*-
alpar@255
     2
#ifndef HUGO_DIJKSTRA_H
alpar@255
     3
#define HUGO_DIJKSTRA_H
alpar@255
     4
klao@491
     5
///\ingroup galgs
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@430
    14
/// \addtogroup galgs
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@456
    40
  ///\author Jacint Szabo
alpar@584
    41
  ///\todo We need a typedef-names should be standardized.
alpar@584
    42
alpar@255
    43
#ifdef DOXYGEN
alpar@584
    44
  template <typename GR,
alpar@584
    45
	    typename LM,
alpar@255
    46
	    typename Heap>
alpar@255
    47
#else
alpar@584
    48
  template <typename GR,
alpar@584
    49
	    typename LM=typename GR::template EdgeMap<int>,
alpar@532
    50
	    template <class,class,class,class> class Heap = BinHeap >
alpar@255
    51
#endif
alpar@255
    52
  class Dijkstra{
alpar@255
    53
  public:
alpar@584
    54
    ///The type of the underlying graph.
alpar@584
    55
    typedef GR Graph;
alpar@255
    56
    typedef typename Graph::Node Node;
alpar@255
    57
    typedef typename Graph::NodeIt NodeIt;
alpar@255
    58
    typedef typename Graph::Edge Edge;
alpar@255
    59
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
    60
    
alpar@584
    61
    ///The type of the length of the edges.
alpar@584
    62
    typedef typename LM::ValueType ValueType;
alpar@584
    63
    ///The the type of the map that stores the edge lengths.
alpar@584
    64
    typedef LM LengthMap;
alpar@584
    65
    ///\brief The the type of the map that stores the last
alpar@584
    66
    ///edges of the shortest paths.
marci@433
    67
    typedef typename Graph::template NodeMap<Edge> PredMap;
alpar@584
    68
    ///\brief The the type of the map that stores the last but one
alpar@584
    69
    ///nodes of the shortest paths.
marci@433
    70
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
alpar@584
    71
    ///The the type of the map that stores the dists of the nodes.
marci@433
    72
    typedef typename Graph::template NodeMap<ValueType> DistMap;
alpar@255
    73
alpar@255
    74
  private:
alpar@688
    75
    const Graph *G;
alpar@688
    76
    const LM *length;
alpar@688
    77
    //    bool local_length;
alpar@688
    78
    PredMap *predecessor;
alpar@688
    79
    bool local_predecessor;
alpar@688
    80
    PredNodeMap *pred_node;
alpar@688
    81
    bool local_pred_node;
alpar@688
    82
    DistMap *distance;
alpar@688
    83
    bool local_distance;
alpar@688
    84
alpar@688
    85
    ///Initialize maps
alpar@688
    86
    
alpar@688
    87
    ///\todo Error if \c G or are \c NULL. What about \c length
alpar@688
    88
    ///\todo Better memory allocation (instead of new).
alpar@688
    89
    void init_maps() 
alpar@688
    90
    {
alpar@688
    91
//       if(!length) {
alpar@688
    92
// 	local_length = true;
alpar@688
    93
// 	length = new LM(G);
alpar@688
    94
//       }
alpar@688
    95
      if(!predecessor) {
alpar@688
    96
	local_predecessor = true;
alpar@688
    97
	predecessor = new PredMap(*G);
alpar@688
    98
      }
alpar@688
    99
      if(!pred_node) {
alpar@688
   100
	local_pred_node = true;
alpar@688
   101
	pred_node = new PredNodeMap(*G);
alpar@688
   102
      }
alpar@688
   103
      if(!distance) {
alpar@688
   104
	local_distance = true;
alpar@688
   105
	distance = new DistMap(*G);
alpar@688
   106
      }
alpar@688
   107
    }
alpar@255
   108
    
alpar@255
   109
  public :
alpar@255
   110
    
alpar@584
   111
    Dijkstra(const Graph& _G, const LM& _length) :
alpar@688
   112
      G(&_G), length(&_length),
alpar@688
   113
      predecessor(NULL), pred_node(NULL), distance(NULL),
alpar@688
   114
      local_predecessor(false), local_pred_node(false), local_distance(false)
alpar@688
   115
    { }
alpar@688
   116
    
alpar@688
   117
    ~Dijkstra() 
alpar@688
   118
    {
alpar@688
   119
      //      if(local_length) delete length;
alpar@688
   120
      if(local_predecessor) delete predecessor;
alpar@688
   121
      if(local_pred_node) delete pred_node;
alpar@688
   122
      if(local_distance) delete distance;
alpar@688
   123
    }
alpar@688
   124
alpar@688
   125
    ///Sets the graph the algorithm will run on.
alpar@688
   126
alpar@688
   127
    ///Sets the graph the algorithm will run on.
alpar@688
   128
    ///\return <tt> (*this) </tt>
alpar@688
   129
    Dijkstra &setGraph(const Graph &_G) 
alpar@688
   130
    {
alpar@688
   131
      G = &_G;
alpar@688
   132
      return *this;
alpar@688
   133
    }
alpar@688
   134
    ///Sets the length map.
alpar@688
   135
alpar@688
   136
    ///Sets the length map.
alpar@688
   137
    ///\return <tt> (*this) </tt>
alpar@688
   138
    Dijkstra &setLengthMap(const LM &m) 
alpar@688
   139
    {
alpar@688
   140
//       if(local_length) {
alpar@688
   141
// 	delete length;
alpar@688
   142
// 	local_length=false;
alpar@688
   143
//       }
alpar@688
   144
      length = &m;
alpar@688
   145
      return *this;
alpar@688
   146
    }
alpar@688
   147
alpar@688
   148
    ///Sets the map storing the predecessor edges.
alpar@688
   149
alpar@688
   150
    ///Sets the map storing the predecessor edges.
alpar@688
   151
    ///If you don't use this function before calling \ref run(),
alpar@688
   152
    ///it will allocate one. The destuctor deallocates this
alpar@688
   153
    ///automatically allocated map, of course.
alpar@688
   154
    ///\return <tt> (*this) </tt>
alpar@688
   155
    Dijkstra &setPredMap(PredMap &m) 
alpar@688
   156
    {
alpar@688
   157
      if(local_predecessor) {
alpar@688
   158
	delete predecessor;
alpar@688
   159
	local_predecessor=false;
alpar@688
   160
      }
alpar@688
   161
      predecessor = &m;
alpar@688
   162
      return *this;
alpar@688
   163
    }
alpar@688
   164
alpar@688
   165
    ///Sets the map storing the predecessor nodes.
alpar@688
   166
alpar@688
   167
    ///Sets the map storing the predecessor nodes.
alpar@688
   168
    ///If you don't use this function before calling \ref run(),
alpar@688
   169
    ///it will allocate one. The destuctor deallocates this
alpar@688
   170
    ///automatically allocated map, of course.
alpar@688
   171
    ///\return <tt> (*this) </tt>
alpar@688
   172
    Dijkstra &setPredNodeMap(PredNodeMap &m) 
alpar@688
   173
    {
alpar@688
   174
      if(local_pred_node) {
alpar@688
   175
	delete pred_node;
alpar@688
   176
	local_pred_node=false;
alpar@688
   177
      }
alpar@688
   178
      pred_node = &m;
alpar@688
   179
      return *this;
alpar@688
   180
    }
alpar@688
   181
alpar@688
   182
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   183
alpar@688
   184
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   185
    ///If you don't use this function before calling \ref run(),
alpar@688
   186
    ///it will allocate one. The destuctor deallocates this
alpar@688
   187
    ///automatically allocated map, of course.
alpar@688
   188
    ///\return <tt> (*this) </tt>
alpar@688
   189
    Dijkstra &setDistMap(DistMap &m) 
alpar@688
   190
    {
alpar@688
   191
      if(local_distance) {
alpar@688
   192
	delete distance;
alpar@688
   193
	local_distance=false;
alpar@688
   194
      }
alpar@688
   195
      distance = &m;
alpar@688
   196
      return *this;
alpar@688
   197
    }
alpar@255
   198
    
alpar@255
   199
    void run(Node s);
alpar@255
   200
    
jacint@385
   201
    ///The distance of a node from the root.
alpar@255
   202
jacint@385
   203
    ///Returns the distance of a node from the root.
alpar@255
   204
    ///\pre \ref run() must be called before using this function.
jacint@385
   205
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   206
    ///of this funcion is undefined.
alpar@688
   207
    ValueType dist(Node v) const { return (*distance)[v]; }
jacint@373
   208
alpar@584
   209
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   210
alpar@584
   211
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
jacint@385
   212
    ///i.e. it returns the last edge from a shortest path from the root to \c
alpar@688
   213
    ///v. It is \ref INVALID
alpar@688
   214
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   215
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
   216
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
   217
    ///this function.
alpar@688
   218
    Edge pred(Node v) const { return (*predecessor)[v]; }
jacint@373
   219
alpar@584
   220
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   221
alpar@584
   222
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   223
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   224
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   225
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
   226
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
   227
    ///using this function.
alpar@688
   228
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@255
   229
    
alpar@255
   230
    ///Returns a reference to the NodeMap of distances.
alpar@255
   231
jacint@385
   232
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   233
    ///be called before using this function.
alpar@688
   234
    const DistMap &distMap() const { return *distance;}
jacint@385
   235
 
alpar@255
   236
    ///Returns a reference to the shortest path tree map.
alpar@255
   237
alpar@255
   238
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   239
    ///shortest path tree.
alpar@255
   240
    ///\pre \ref run() must be called before using this function.
alpar@688
   241
    const PredMap &predMap() const { return *predecessor;}
jacint@385
   242
 
jacint@385
   243
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   244
alpar@255
   245
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   246
    ///shortest path tree.
alpar@255
   247
    ///\pre \ref run() must be called before using this function.
alpar@688
   248
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@255
   249
jacint@385
   250
    ///Checks if a node is reachable from the root.
alpar@255
   251
jacint@385
   252
    ///Returns \c true if \c v is reachable from the root.
jacint@385
   253
    ///\warning the root node is reported to be unreached!
alpar@255
   254
    ///\todo Is this what we want?
alpar@255
   255
    ///\pre \ref run() must be called before using this function.
jacint@385
   256
    ///
alpar@688
   257
    bool reached(Node v) { return G->valid((*predecessor)[v]); }
alpar@255
   258
    
alpar@255
   259
  };
alpar@255
   260
  
alpar@255
   261
alpar@255
   262
  // **********************************************************************
alpar@255
   263
  //  IMPLEMENTATIONS
alpar@255
   264
  // **********************************************************************
alpar@255
   265
jacint@385
   266
  ///Runs %Dijkstra algorithm from node the root.
alpar@255
   267
jacint@385
   268
  ///This method runs the %Dijkstra algorithm from a root node \c s
jacint@385
   269
  ///in order to
jacint@385
   270
  ///compute the
jacint@385
   271
  ///shortest path to each node. The algorithm computes
jacint@385
   272
  ///- The shortest path tree.
jacint@385
   273
  ///- The distance of each node from the root.
alpar@584
   274
  template <typename GR, typename LM,
alpar@532
   275
	    template<class,class,class,class> class Heap >
alpar@584
   276
  void Dijkstra<GR,LM,Heap>::run(Node s) {
alpar@688
   277
alpar@688
   278
    init_maps();
alpar@688
   279
alpar@688
   280
    for ( NodeIt u(*G) ; G->valid(u) ; G->next(u) ) {
alpar@688
   281
      predecessor->set(u,INVALID);
alpar@688
   282
      pred_node->set(u,INVALID);
alpar@255
   283
    }
alpar@255
   284
    
alpar@688
   285
    typename GR::template NodeMap<int> heap_map(*G,-1);
alpar@255
   286
    
alpar@584
   287
    typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
alpar@532
   288
      std::less<ValueType> > 
alpar@532
   289
      HeapType;
alpar@532
   290
    
alpar@532
   291
    HeapType heap(heap_map);
jacint@385
   292
    
alpar@255
   293
    heap.push(s,0); 
alpar@255
   294
    
jacint@385
   295
      while ( !heap.empty() ) {
alpar@255
   296
	
jacint@385
   297
	Node v=heap.top(); 
jacint@385
   298
	ValueType oldvalue=heap[v];
jacint@385
   299
	heap.pop();
alpar@688
   300
	distance->set(v, oldvalue);
jacint@385
   301
	
alpar@688
   302
	  
alpar@688
   303
	for(OutEdgeIt e(*G,v); G->valid(e); G->next(e)) {
alpar@688
   304
	  Node w=G->bNode(e); 
alpar@255
   305
	  
alpar@255
   306
	  switch(heap.state(w)) {
alpar@532
   307
	  case HeapType::PRE_HEAP:
alpar@688
   308
	    heap.push(w,oldvalue+(*length)[e]); 
alpar@688
   309
	    predecessor->set(w,e);
alpar@688
   310
	    pred_node->set(w,v);
alpar@255
   311
	    break;
alpar@532
   312
	  case HeapType::IN_HEAP:
alpar@688
   313
	    if ( oldvalue+(*length)[e] < heap[w] ) {
alpar@688
   314
	      heap.decrease(w, oldvalue+(*length)[e]); 
alpar@688
   315
	      predecessor->set(w,e);
alpar@688
   316
	      pred_node->set(w,v);
alpar@255
   317
	    }
alpar@255
   318
	    break;
alpar@532
   319
	  case HeapType::POST_HEAP:
alpar@255
   320
	    break;
alpar@255
   321
	  }
alpar@255
   322
	}
jacint@385
   323
      }
alpar@255
   324
  }
alpar@430
   325
alpar@430
   326
/// @}
alpar@255
   327
  
alpar@255
   328
} //END OF NAMESPACE HUGO
alpar@255
   329
alpar@255
   330
#endif
alpar@255
   331
alpar@255
   332