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