src/hugo/dijkstra.h
author alpar
Fri, 07 May 2004 16:04:39 +0000
changeset 580 a00f9f1cfab8
parent 542 69bde1d90c04
child 584 1d4855f5312e
permissions -rw-r--r--
Property changes (some files was ignored).
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
  ///
jacint@385
    28
  ///\param Graph The graph type the algorithm runs on.
jacint@385
    29
  ///\param LengthMap 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@570
    41
  ///\todo We need a LengthMap typedef
alpar@255
    42
#ifdef DOXYGEN
alpar@255
    43
  template <typename Graph,
alpar@255
    44
	    typename LengthMap,
alpar@255
    45
	    typename Heap>
alpar@255
    46
#else
alpar@255
    47
  template <typename Graph,
marci@433
    48
	    typename LengthMap=typename Graph::template EdgeMap<int>,
alpar@532
    49
	    template <class,class,class,class> class Heap = BinHeap >
alpar@255
    50
#endif
alpar@255
    51
  class Dijkstra{
alpar@255
    52
  public:
alpar@255
    53
    typedef typename Graph::Node Node;
alpar@255
    54
    typedef typename Graph::NodeIt NodeIt;
alpar@255
    55
    typedef typename Graph::Edge Edge;
alpar@255
    56
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
    57
    
alpar@255
    58
    typedef typename LengthMap::ValueType ValueType;
marci@433
    59
    typedef typename Graph::template NodeMap<Edge> PredMap;
marci@433
    60
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
marci@433
    61
    typedef typename Graph::template NodeMap<ValueType> DistMap;
alpar@255
    62
alpar@255
    63
  private:
alpar@255
    64
    const Graph& G;
alpar@255
    65
    const LengthMap& length;
alpar@255
    66
    PredMap predecessor;
alpar@255
    67
    PredNodeMap pred_node;
alpar@255
    68
    DistMap distance;
alpar@255
    69
    
alpar@255
    70
  public :
alpar@255
    71
    
marci@459
    72
    Dijkstra(const Graph& _G, const LengthMap& _length) :
alpar@255
    73
      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
alpar@255
    74
    
alpar@255
    75
    void run(Node s);
alpar@255
    76
    
jacint@385
    77
    ///The distance of a node from the root.
alpar@255
    78
jacint@385
    79
    ///Returns the distance of a node from the root.
alpar@255
    80
    ///\pre \ref run() must be called before using this function.
jacint@385
    81
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
    82
    ///of this funcion is undefined.
alpar@255
    83
    ValueType dist(Node v) const { return distance[v]; }
jacint@373
    84
jacint@385
    85
    ///Returns the previous edge of the shortest path tree.
alpar@255
    86
jacint@385
    87
    ///For a node \c v it returns the previous edge of the shortest path tree,
jacint@385
    88
    ///i.e. it returns the last edge from a shortest path from the root to \c
jacint@385
    89
    ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
jacint@385
    90
    ///shortest path tree used here is equal to the shortest path tree used in
jacint@385
    91
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
jacint@385
    92
    ///this function.
alpar@255
    93
    Edge pred(Node v) const { return predecessor[v]; }
jacint@373
    94
jacint@385
    95
    ///Returns the previous node of the shortest path tree.
alpar@255
    96
jacint@385
    97
    ///For a node \c v it returns the previous node of the shortest path tree,
jacint@385
    98
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
    99
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   100
    ///\c v=s. The shortest path tree used here is equal to the shortest path
jacint@385
   101
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
jacint@385
   102
    ///using this function.
alpar@255
   103
    Node predNode(Node v) const { return pred_node[v]; }
alpar@255
   104
    
alpar@255
   105
    ///Returns a reference to the NodeMap of distances.
alpar@255
   106
jacint@385
   107
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   108
    ///be called before using this function.
alpar@255
   109
    const DistMap &distMap() const { return distance;}
jacint@385
   110
 
alpar@255
   111
    ///Returns a reference to the shortest path tree map.
alpar@255
   112
alpar@255
   113
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   114
    ///shortest path tree.
alpar@255
   115
    ///\pre \ref run() must be called before using this function.
alpar@255
   116
    const PredMap &predMap() const { return predecessor;}
jacint@385
   117
 
jacint@385
   118
    ///Returns a reference to the map of nodes of shortest paths.
alpar@255
   119
alpar@255
   120
    ///Returns a reference to the NodeMap of the last but one nodes of the
jacint@385
   121
    ///shortest path tree.
alpar@255
   122
    ///\pre \ref run() must be called before using this function.
alpar@255
   123
    const PredNodeMap &predNodeMap() const { return pred_node;}
alpar@255
   124
jacint@385
   125
    ///Checks if a node is reachable from the root.
alpar@255
   126
jacint@385
   127
    ///Returns \c true if \c v is reachable from the root.
jacint@385
   128
    ///\warning the root node is reported to be unreached!
alpar@255
   129
    ///\todo Is this what we want?
alpar@255
   130
    ///\pre \ref run() must be called before using this function.
jacint@385
   131
    ///
alpar@255
   132
    bool reached(Node v) { return G.valid(predecessor[v]); }
alpar@255
   133
    
alpar@255
   134
  };
alpar@255
   135
  
alpar@255
   136
alpar@255
   137
  // **********************************************************************
alpar@255
   138
  //  IMPLEMENTATIONS
alpar@255
   139
  // **********************************************************************
alpar@255
   140
jacint@385
   141
  ///Runs %Dijkstra algorithm from node the root.
alpar@255
   142
jacint@385
   143
  ///This method runs the %Dijkstra algorithm from a root node \c s
jacint@385
   144
  ///in order to
jacint@385
   145
  ///compute the
jacint@385
   146
  ///shortest path to each node. The algorithm computes
jacint@385
   147
  ///- The shortest path tree.
jacint@385
   148
  ///- The distance of each node from the root.
alpar@255
   149
  template <typename Graph, typename LengthMap,
alpar@532
   150
	    template<class,class,class,class> class Heap >
alpar@255
   151
  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
alpar@255
   152
    
alpar@255
   153
    NodeIt u;
alpar@255
   154
    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
alpar@255
   155
      predecessor.set(u,INVALID);
alpar@255
   156
      pred_node.set(u,INVALID);
alpar@255
   157
    }
alpar@255
   158
    
marci@433
   159
    typename Graph::template NodeMap<int> heap_map(G,-1);
alpar@255
   160
    
alpar@532
   161
    typedef Heap<Node, ValueType, typename Graph::template NodeMap<int>,
alpar@532
   162
      std::less<ValueType> > 
alpar@532
   163
      HeapType;
alpar@532
   164
    
alpar@532
   165
    HeapType heap(heap_map);
jacint@385
   166
    
alpar@255
   167
    heap.push(s,0); 
alpar@255
   168
    
jacint@385
   169
      while ( !heap.empty() ) {
alpar@255
   170
	
jacint@385
   171
	Node v=heap.top(); 
jacint@385
   172
	ValueType oldvalue=heap[v];
jacint@385
   173
	heap.pop();
jacint@385
   174
	distance.set(v, oldvalue);
jacint@385
   175
	
jacint@385
   176
	{ //FIXME this bracket is for e to be local
jacint@385
   177
	  OutEdgeIt e;
jacint@385
   178
	for(G.first(e, v);
jacint@385
   179
	    G.valid(e); G.next(e)) {
marci@421
   180
	  Node w=G.bNode(e); 
alpar@255
   181
	  
alpar@255
   182
	  switch(heap.state(w)) {
alpar@532
   183
	  case HeapType::PRE_HEAP:
alpar@255
   184
	    heap.push(w,oldvalue+length[e]); 
alpar@255
   185
	    predecessor.set(w,e);
alpar@255
   186
	    pred_node.set(w,v);
alpar@255
   187
	    break;
alpar@532
   188
	  case HeapType::IN_HEAP:
alpar@255
   189
	    if ( oldvalue+length[e] < heap[w] ) {
alpar@255
   190
	      heap.decrease(w, oldvalue+length[e]); 
alpar@255
   191
	      predecessor.set(w,e);
alpar@255
   192
	      pred_node.set(w,v);
alpar@255
   193
	    }
alpar@255
   194
	    break;
alpar@532
   195
	  case HeapType::POST_HEAP:
alpar@255
   196
	    break;
alpar@255
   197
	  }
alpar@255
   198
	}
jacint@385
   199
      } //FIXME tis bracket
jacint@385
   200
      }
alpar@255
   201
  }
alpar@430
   202
alpar@430
   203
/// @}
alpar@255
   204
  
alpar@255
   205
} //END OF NAMESPACE HUGO
alpar@255
   206
alpar@255
   207
#endif
alpar@255
   208
alpar@255
   209