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