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