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