src/work/alpar/dijkstra/dijkstra.h
author alpar
Wed, 24 Mar 2004 13:06:06 +0000
changeset 242 b255f25ad394
parent 229 ae5f9ca94be7
child 247 fefccf1bdc23
permissions -rw-r--r--
DocFixes
alpar@222
     1
// -*- C++ -*-
alpar@242
     2
alpar@222
     3
/* 
alpar@222
     4
 *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> >
alpar@222
     5
 *
alpar@222
     6
 *Constructor: 
alpar@222
     7
 *
alpar@222
     8
 *Dijkstra(Graph G, LengthMap length)
alpar@222
     9
 *
alpar@222
    10
 *
alpar@222
    11
 *Methods:
alpar@222
    12
 *
alpar@222
    13
 *void run(Node s)
alpar@222
    14
 *
alpar@222
    15
 *T dist(Node v) : After run(s) was run, it returns the distance from s to v. 
alpar@222
    16
 *   Returns T() if v is not reachable from s.
alpar@222
    17
 *
alpar@222
    18
 *Edge pred(Node v) : After run(s) was run, it returns the last 
alpar@222
    19
 *   edge of a shortest s-v path. It is INVALID for s and for 
alpar@222
    20
 *   the nodes not reachable from s.
alpar@222
    21
 *
alpar@222
    22
 *bool reached(Node v) : After run(s) was run, it is true iff v is 
alpar@222
    23
 *   reachable from s
alpar@222
    24
 *
alpar@222
    25
 */
alpar@222
    26
alpar@222
    27
#ifndef HUGO_DIJKSTRA_H
alpar@222
    28
#define HUGO_DIJKSTRA_H
alpar@222
    29
alpar@242
    30
///\file
alpar@242
    31
///\brief Dijkstra algorithm.
alpar@242
    32
alpar@222
    33
#include <fib_heap.h>
alpar@228
    34
#include <bin_heap.hh>
alpar@222
    35
#include <invalid.h>
alpar@222
    36
alpar@222
    37
namespace hugo {
alpar@222
    38
  
alpar@222
    39
  //Alpar: Changed the order of the parameters
alpar@224
    40
  
alpar@229
    41
  ///%Dijkstra algorithm class.
alpar@224
    42
alpar@229
    43
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@224
    44
  ///The edge lengths are passed to the algorithm using a
alpar@224
    45
  ///\ref ReadMapSkeleton "readable map",
alpar@224
    46
  ///so it is easy to change it to any kind of length.
alpar@224
    47
  ///
alpar@224
    48
  ///The type of the length is determined by the \c ValueType of the length map.
alpar@224
    49
  ///
alpar@242
    50
  ///It is also possible to change the underlying priority heap.
alpar@224
    51
  ///
alpar@224
    52
  ///\param Graph The graph type the algorithm runs on.
alpar@242
    53
  ///\param LengthMap This read-only
alpar@242
    54
  ///EdgeMap
alpar@242
    55
  ///determines the
alpar@224
    56
  ///lengths of the edges. It is read once for each edge, so the map
alpar@224
    57
  ///may involve in relatively time consuming process to compute the edge
alpar@242
    58
  ///length if it is necessary. The default map type is
alpar@242
    59
  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
alpar@229
    60
  ///\param Heap The heap type used by the %Dijkstra
alpar@224
    61
  ///algorithm. The default
alpar@224
    62
  ///is using \ref BinHeap "binary heap".
alpar@242
    63
  
alpar@242
    64
#ifdef DOXYGEN
alpar@242
    65
  template <typename Graph,
alpar@242
    66
	    typename LengthMap,
alpar@242
    67
	    typename Heap>
alpar@242
    68
#else
alpar@222
    69
  template <typename Graph,
alpar@222
    70
	    typename LengthMap=typename Graph::EdgeMap<int>,
alpar@228
    71
	    typename Heap=BinHeap <typename Graph::Node,
alpar@228
    72
				   typename LengthMap::ValueType, 
alpar@228
    73
				   typename Graph::NodeMap<int> > >
alpar@242
    74
#endif
alpar@222
    75
  class Dijkstra{
alpar@222
    76
  public:
alpar@228
    77
    typedef typename Graph::Node Node;
alpar@228
    78
    typedef typename Graph::NodeIt NodeIt;
alpar@228
    79
    typedef typename Graph::Edge Edge;
alpar@228
    80
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@228
    81
    
alpar@222
    82
    typedef typename LengthMap::ValueType ValueType;
alpar@224
    83
    typedef typename Graph::NodeMap<Edge> PredMap;
alpar@224
    84
    typedef typename Graph::NodeMap<Node> PredNodeMap;
alpar@224
    85
    typedef typename Graph::NodeMap<ValueType> DistMap;
alpar@222
    86
alpar@222
    87
  private:
alpar@222
    88
    const Graph& G;
alpar@222
    89
    const LengthMap& length;
alpar@222
    90
    PredMap predecessor;
alpar@222
    91
    //In place of reach:
alpar@222
    92
    PredNodeMap pred_node;
alpar@222
    93
    DistMap distance;
alpar@222
    94
    //I don't like this:
alpar@222
    95
    //     //FIXME:
alpar@222
    96
    //     typename Graph::NodeMap<bool> reach;
alpar@222
    97
    //     //typename Graph::NodeMap<int> reach;
alpar@222
    98
    
alpar@222
    99
  public :
alpar@222
   100
    
alpar@222
   101
    /*
alpar@222
   102
      The distance of the nodes is 0.
alpar@222
   103
    */
alpar@222
   104
    Dijkstra(Graph& _G, LengthMap& _length) :
alpar@222
   105
      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
alpar@222
   106
    
alpar@222
   107
alpar@222
   108
    void run(Node s);
alpar@222
   109
    
alpar@224
   110
    ///The distance of a node from the source.
alpar@224
   111
alpar@224
   112
    ///Returns the distance of a node from the source.
alpar@224
   113
    ///\pre \ref run() must be called before using this function.
alpar@229
   114
    ///\warning If node \c v in unreachable from the source the return value
alpar@224
   115
    ///of this funcion is undefined.
alpar@222
   116
    ValueType dist(Node v) const { return distance[v]; }
alpar@224
   117
    ///Returns the edges of the shortest path tree.
alpar@224
   118
alpar@224
   119
    ///For a node \c v it returns the last edge of the shortest path
alpar@229
   120
    ///from the source to \c v or INVALID if \c v is unreachable
alpar@229
   121
    ///from the source.
alpar@224
   122
    ///\pre \ref run() must be called before using this function.
alpar@222
   123
    Edge pred(Node v) const { return predecessor[v]; }
alpar@224
   124
    ///Returns the nodes of the shortest paths.
alpar@224
   125
alpar@224
   126
    ///For a node \c v it returns the last but one node of the shortest path
alpar@229
   127
    ///from the source to \c v or INVALID if \c v is unreachable
alpar@229
   128
    ///from the source.
alpar@224
   129
    ///\pre \ref run() must be called before using this function.
alpar@222
   130
    Node predNode(Node v) const { return pred_node[v]; }
alpar@222
   131
    
alpar@224
   132
    ///Returns a reference to the NodeMap of distances.
alpar@224
   133
alpar@224
   134
    ///\pre \ref run() must be called before using this function.
alpar@224
   135
    ///
alpar@222
   136
    const DistMap &distMap() const { return distance;}
alpar@224
   137
    ///Returns a reference to the shortest path tree map.
alpar@224
   138
alpar@224
   139
    ///Returns a reference to the NodeMap of the edges of the
alpar@224
   140
    ///shortest path tree.
alpar@224
   141
    ///\pre \ref run() must be called before using this function.
alpar@222
   142
    const PredMap &predMap() const { return predecessor;}
alpar@224
   143
    ///Returns a reference to the map of nodes of  shortest paths.
alpar@224
   144
alpar@224
   145
    ///Returns a reference to the NodeMap of the last but one nodes of the
alpar@224
   146
    ///shortest paths.
alpar@224
   147
    ///\pre \ref run() must be called before using this function.
alpar@222
   148
    const PredNodeMap &predNodeMap() const { return pred_node;}
alpar@222
   149
alpar@222
   150
    //    bool reached(Node v) { return reach[v]; }
alpar@224
   151
alpar@242
   152
    ///Checks if a node is reachable from the source.
alpar@224
   153
alpar@229
   154
    ///Returns \c true if \c v is reachable from the source.
alpar@229
   155
    ///\warning the source node is reported to be unreached!
alpar@224
   156
    ///\todo Is this what we want?
alpar@224
   157
    ///\pre \ref run() must be called before using this function.
alpar@222
   158
    ///
alpar@222
   159
    bool reached(Node v) { return G.valid(predecessor[v]); }
alpar@222
   160
    
alpar@222
   161
  };
alpar@222
   162
  
alpar@222
   163
alpar@224
   164
  // **********************************************************************
alpar@224
   165
  //  IMPLEMENTATIONS
alpar@224
   166
  // **********************************************************************
alpar@222
   167
alpar@229
   168
  ///Runs %Dijkstra algorithm from node the source.
alpar@224
   169
alpar@229
   170
  ///This method runs the %Dijkstra algorithm from a source node \c s
alpar@229
   171
  ///in order to
alpar@224
   172
  ///compute the
alpar@224
   173
  ///shortest path to each node. The algorithm computes
alpar@224
   174
  ///- The shortest path tree.
alpar@229
   175
  ///- The distance of each node from the source.
alpar@222
   176
  template <typename Graph, typename LengthMap, typename Heap >
alpar@222
   177
  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
alpar@222
   178
    
alpar@222
   179
    NodeIt u;
alpar@222
   180
    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
alpar@222
   181
      predecessor.set(u,INVALID);
alpar@224
   182
      pred_node.set(u,INVALID);
alpar@222
   183
      // If a node is unreacheable, then why should be the dist=0?
alpar@222
   184
      // distance.set(u,0);
alpar@222
   185
      //      reach.set(u,false);
alpar@222
   186
    }
alpar@222
   187
    
alpar@222
   188
    //We don't need it at all.
alpar@222
   189
    //     //FIXME:
alpar@222
   190
    //     typename Graph::NodeMap<bool> scanned(G,false);
alpar@222
   191
    //     //typename Graph::NodeMap<int> scanned(G,false);
alpar@222
   192
    typename Graph::NodeMap<int> heap_map(G,-1);
alpar@222
   193
    
alpar@222
   194
    Heap heap(heap_map);
alpar@222
   195
    
alpar@222
   196
    heap.push(s,0); 
alpar@222
   197
    //    reach.set(s, true);
alpar@222
   198
    
alpar@222
   199
      while ( !heap.empty() ) {
alpar@222
   200
	
alpar@222
   201
	Node v=heap.top(); 
alpar@222
   202
	ValueType oldvalue=heap[v];
alpar@222
   203
	heap.pop();
alpar@222
   204
	distance.set(v, oldvalue);
alpar@222
   205
	
alpar@222
   206
	for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) {
alpar@222
   207
	  Node w=G.head(e); 
alpar@222
   208
	  
alpar@222
   209
	  switch(heap.state(w)) {
alpar@222
   210
	  case Heap::PRE_HEAP:
alpar@222
   211
	    //	    reach.set(w,true);
alpar@222
   212
	    heap.push(w,oldvalue+length[e]); 
alpar@222
   213
	    predecessor.set(w,e);
alpar@222
   214
	    pred_node.set(w,v);
alpar@222
   215
	    break;
alpar@222
   216
	  case Heap::IN_HEAP:
alpar@222
   217
	    if ( oldvalue+length[e] < heap[w] ) {
alpar@222
   218
	      heap.decrease(w, oldvalue+length[e]); 
alpar@222
   219
	      predecessor.set(w,e);
alpar@222
   220
	      pred_node.set(w,v);
alpar@222
   221
	    }
alpar@222
   222
	    break;
alpar@222
   223
	  case Heap::POST_HEAP:
alpar@222
   224
	    break;
alpar@222
   225
	  }
alpar@222
   226
	}
alpar@222
   227
      }
alpar@222
   228
  }
alpar@222
   229
  
alpar@222
   230
} //END OF NAMESPACE HUGO
alpar@222
   231
alpar@222
   232
#endif
alpar@222
   233
alpar@222
   234