src/include/dijkstra.h
author klao
Mon, 26 Apr 2004 17:39:15 +0000
changeset 427 a677104e946a
parent 385 d7ebbae96025
child 430 60e4627e8c74
permissions -rw-r--r--
Minor doc corrections
alpar@255
     1
// -*- C++ -*-
alpar@255
     2
#ifndef HUGO_DIJKSTRA_H
alpar@255
     3
#define HUGO_DIJKSTRA_H
alpar@255
     4
alpar@255
     5
///\file
alpar@255
     6
///\brief Dijkstra algorithm.
alpar@255
     7
klao@258
     8
#include <bin_heap.h>
alpar@257
     9
#include <invalid.h>
alpar@255
    10
alpar@255
    11
namespace hugo {
jacint@385
    12
alpar@255
    13
  ///%Dijkstra algorithm class.
alpar@255
    14
alpar@255
    15
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
    16
  ///The edge lengths are passed to the algorithm using a
alpar@255
    17
  ///\ref ReadMapSkeleton "readable map",
alpar@255
    18
  ///so it is easy to change it to any kind of length.
alpar@255
    19
  ///
alpar@255
    20
  ///The type of the length is determined by the \c ValueType of the length map.
alpar@255
    21
  ///
alpar@255
    22
  ///It is also possible to change the underlying priority heap.
alpar@255
    23
  ///
jacint@385
    24
  ///\param Graph The graph type the algorithm runs on.
jacint@385
    25
  ///\param LengthMap This read-only
jacint@385
    26
  ///EdgeMap
jacint@385
    27
  ///determines the
jacint@385
    28
  ///lengths of the edges. It is read once for each edge, so the map
jacint@385
    29
  ///may involve in relatively time consuming process to compute the edge
jacint@385
    30
  ///length if it is necessary. The default map type is
jacint@385
    31
  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
jacint@385
    32
  ///\param Heap The heap type used by the %Dijkstra
jacint@385
    33
  ///algorithm. The default
jacint@385
    34
  ///is using \ref BinHeap "binary heap".
alpar@255
    35
  
alpar@255
    36
#ifdef DOXYGEN
alpar@255
    37
  template <typename Graph,
alpar@255
    38
	    typename LengthMap,
alpar@255
    39
	    typename Heap>
alpar@255
    40
#else
alpar@255
    41
  template <typename Graph,
alpar@255
    42
	    typename LengthMap=typename Graph::EdgeMap<int>,
alpar@255
    43
	    template <class,class,class> class Heap = BinHeap >
alpar@255
    44
#endif
alpar@255
    45
  class Dijkstra{
alpar@255
    46
  public:
alpar@255
    47
    typedef typename Graph::Node Node;
alpar@255
    48
    typedef typename Graph::NodeIt NodeIt;
alpar@255
    49
    typedef typename Graph::Edge Edge;
alpar@255
    50
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
    51
    
alpar@255
    52
    typedef typename LengthMap::ValueType ValueType;
alpar@255
    53
    typedef typename Graph::NodeMap<Edge> PredMap;
alpar@255
    54
    typedef typename Graph::NodeMap<Node> PredNodeMap;
alpar@255
    55
    typedef typename Graph::NodeMap<ValueType> DistMap;
alpar@255
    56
alpar@255
    57
  private:
alpar@255
    58
    const Graph& G;
alpar@255
    59
    const LengthMap& length;
alpar@255
    60
    PredMap predecessor;
alpar@255
    61
    PredNodeMap pred_node;
alpar@255
    62
    DistMap distance;
alpar@255
    63
    
alpar@255
    64
  public :
alpar@255
    65
    
alpar@255
    66
    Dijkstra(Graph& _G, LengthMap& _length) :
alpar@255
    67
      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
alpar@255
    68
    
alpar@255
    69
    void run(Node s);
alpar@255
    70
    
jacint@385
    71
    ///The distance of a node from the root.
alpar@255
    72
jacint@385
    73
    ///Returns the distance of a node from the root.
alpar@255
    74
    ///\pre \ref run() must be called before using this function.
jacint@385
    75
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
    76
    ///of this funcion is undefined.
alpar@255
    77
    ValueType dist(Node v) const { return distance[v]; }
jacint@373
    78
jacint@385
    79
    ///Returns the previous edge of the shortest path tree.
alpar@255
    80
jacint@385
    81
    ///For a node \c v it returns the previous edge of the shortest path tree,
jacint@385
    82
    ///i.e. it returns the last edge from a shortest path from the root to \c
jacint@385
    83
    ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
jacint@385
    84
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
    85
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
    86
    ///this function.
alpar@255
    87
    Edge pred(Node v) const { return predecessor[v]; }
jacint@373
    88
jacint@385
    89
    ///Returns the previous node of the shortest path tree.
alpar@255
    90
jacint@385
    91
    ///For a node \c v it returns the previous node of the shortest path tree,
jacint@385
    92
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
    93
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
    94
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
    95
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
    96
    ///using this function.
alpar@255
    97
    Node predNode(Node v) const { return pred_node[v]; }
alpar@255
    98
    
alpar@255
    99
    ///Returns a reference to the NodeMap of distances.
alpar@255
   100
jacint@385
   101
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   102
    ///be called before using this function.
alpar@255
   103
    const DistMap &distMap() const { return distance;}
jacint@385
   104
 
alpar@255
   105
    ///Returns a reference to the shortest path tree map.
alpar@255
   106
alpar@255
   107
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   108
    ///shortest path tree.
alpar@255
   109
    ///\pre \ref run() must be called before using this function.
alpar@255
   110
    const PredMap &predMap() const { return predecessor;}
jacint@385
   111
 
jacint@385
   112
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   113
alpar@255
   114
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   115
    ///shortest path tree.
alpar@255
   116
    ///\pre \ref run() must be called before using this function.
alpar@255
   117
    const PredNodeMap &predNodeMap() const { return pred_node;}
alpar@255
   118
jacint@385
   119
    ///Checks if a node is reachable from the root.
alpar@255
   120
jacint@385
   121
    ///Returns \c true if \c v is reachable from the root.
jacint@385
   122
    ///\warning the root node is reported to be unreached!
alpar@255
   123
    ///\todo Is this what we want?
alpar@255
   124
    ///\pre \ref run() must be called before using this function.
jacint@385
   125
    ///
alpar@255
   126
    bool reached(Node v) { return G.valid(predecessor[v]); }
alpar@255
   127
    
alpar@255
   128
  };
alpar@255
   129
  
alpar@255
   130
alpar@255
   131
  // **********************************************************************
alpar@255
   132
  //  IMPLEMENTATIONS
alpar@255
   133
  // **********************************************************************
alpar@255
   134
jacint@385
   135
  ///Runs %Dijkstra algorithm from node the root.
alpar@255
   136
jacint@385
   137
  ///This method runs the %Dijkstra algorithm from a root node \c s
jacint@385
   138
  ///in order to
jacint@385
   139
  ///compute the
jacint@385
   140
  ///shortest path to each node. The algorithm computes
jacint@385
   141
  ///- The shortest path tree.
jacint@385
   142
  ///- The distance of each node from the root.
alpar@255
   143
  template <typename Graph, typename LengthMap,
alpar@255
   144
	    template<class,class,class> class Heap >
alpar@255
   145
  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
alpar@255
   146
    
alpar@255
   147
    NodeIt u;
alpar@255
   148
    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
alpar@255
   149
      predecessor.set(u,INVALID);
alpar@255
   150
      pred_node.set(u,INVALID);
alpar@255
   151
    }
alpar@255
   152
    
alpar@255
   153
    typename Graph::NodeMap<int> heap_map(G,-1);
alpar@255
   154
    
alpar@255
   155
    Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
jacint@385
   156
    
alpar@255
   157
    heap.push(s,0); 
alpar@255
   158
    
jacint@385
   159
      while ( !heap.empty() ) {
alpar@255
   160
	
jacint@385
   161
	Node v=heap.top(); 
jacint@385
   162
	ValueType oldvalue=heap[v];
jacint@385
   163
	heap.pop();
jacint@385
   164
	distance.set(v, oldvalue);
jacint@385
   165
	
jacint@385
   166
	{ //FIXME this bracket is for e to be local
jacint@385
   167
	  OutEdgeIt e;
jacint@385
   168
	for(G.first(e, v);
jacint@385
   169
	    G.valid(e); G.next(e)) {
marci@421
   170
	  Node w=G.bNode(e); 
alpar@255
   171
	  
alpar@255
   172
	  switch(heap.state(w)) {
alpar@255
   173
	  case heap.PRE_HEAP:
alpar@255
   174
	    heap.push(w,oldvalue+length[e]); 
alpar@255
   175
	    predecessor.set(w,e);
alpar@255
   176
	    pred_node.set(w,v);
alpar@255
   177
	    break;
alpar@255
   178
	  case heap.IN_HEAP:
alpar@255
   179
	    if ( oldvalue+length[e] < heap[w] ) {
alpar@255
   180
	      heap.decrease(w, oldvalue+length[e]); 
alpar@255
   181
	      predecessor.set(w,e);
alpar@255
   182
	      pred_node.set(w,v);
alpar@255
   183
	    }
alpar@255
   184
	    break;
alpar@255
   185
	  case heap.POST_HEAP:
alpar@255
   186
	    break;
alpar@255
   187
	  }
alpar@255
   188
	}
jacint@385
   189
      } //FIXME tis bracket
jacint@385
   190
      }
alpar@255
   191
  }
alpar@255
   192
  
alpar@255
   193
} //END OF NAMESPACE HUGO
alpar@255
   194
alpar@255
   195
#endif
alpar@255
   196
alpar@255
   197