4 *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> >
8 *Dijkstra(Graph G, LengthMap length)
15 *T dist(Node v) : After run(s) was run, it returns the distance from s to v.
16 * Returns T() if v is not reachable from s.
18 *Edge pred(Node v) : After run(s) was run, it returns the last
19 * edge of a shortest s-v path. It is INVALID for s and for
20 * the nodes not reachable from s.
22 *bool reached(Node v) : After run(s) was run, it is true iff v is
27 #ifndef HUGO_DIJKSTRA_H
28 #define HUGO_DIJKSTRA_H
31 ///\brief Dijkstra algorithm.
34 #include <bin_heap.hh>
39 //Alpar: Changed the order of the parameters
41 ///%Dijkstra algorithm class.
43 ///This class provides an efficient implementation of %Dijkstra algorithm.
44 ///The edge lengths are passed to the algorithm using a
45 ///\ref ReadMapSkeleton "readable map",
46 ///so it is easy to change it to any kind of length.
48 ///The type of the length is determined by the \c ValueType of the length map.
50 ///It is also possible to change the underlying priority heap.
52 ///\param Graph The graph type the algorithm runs on.
53 ///\param LengthMap This read-only
56 ///lengths of the edges. It is read once for each edge, so the map
57 ///may involve in relatively time consuming process to compute the edge
58 ///length if it is necessary. The default map type is
59 ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
60 ///\param Heap The heap type used by the %Dijkstra
61 ///algorithm. The default
62 ///is using \ref BinHeap "binary heap".
65 template <typename Graph,
69 template <typename Graph,
70 typename LengthMap=typename Graph::EdgeMap<int>,
71 typename Heap=BinHeap <typename Graph::Node,
72 typename LengthMap::ValueType,
73 typename Graph::NodeMap<int> > >
77 typedef typename Graph::Node Node;
78 typedef typename Graph::NodeIt NodeIt;
79 typedef typename Graph::Edge Edge;
80 typedef typename Graph::OutEdgeIt OutEdgeIt;
82 typedef typename LengthMap::ValueType ValueType;
83 typedef typename Graph::NodeMap<Edge> PredMap;
84 typedef typename Graph::NodeMap<Node> PredNodeMap;
85 typedef typename Graph::NodeMap<ValueType> DistMap;
89 const LengthMap& length;
92 PredNodeMap pred_node;
96 // typename Graph::NodeMap<bool> reach;
97 // //typename Graph::NodeMap<int> reach;
102 The distance of the nodes is 0.
104 Dijkstra(Graph& _G, LengthMap& _length) :
105 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
110 ///The distance of a node from the source.
112 ///Returns the distance of a node from the source.
113 ///\pre \ref run() must be called before using this function.
114 ///\warning If node \c v in unreachable from the source the return value
115 ///of this funcion is undefined.
116 ValueType dist(Node v) const { return distance[v]; }
117 ///Returns the edges of the shortest path tree.
119 ///For a node \c v it returns the last edge of the shortest path
120 ///from the source to \c v or INVALID if \c v is unreachable
122 ///\pre \ref run() must be called before using this function.
123 Edge pred(Node v) const { return predecessor[v]; }
124 ///Returns the nodes of the shortest paths.
126 ///For a node \c v it returns the last but one node of the shortest path
127 ///from the source to \c v or INVALID if \c v is unreachable
129 ///\pre \ref run() must be called before using this function.
130 Node predNode(Node v) const { return pred_node[v]; }
132 ///Returns a reference to the NodeMap of distances.
134 ///\pre \ref run() must be called before using this function.
136 const DistMap &distMap() const { return distance;}
137 ///Returns a reference to the shortest path tree map.
139 ///Returns a reference to the NodeMap of the edges of the
140 ///shortest path tree.
141 ///\pre \ref run() must be called before using this function.
142 const PredMap &predMap() const { return predecessor;}
143 ///Returns a reference to the map of nodes of shortest paths.
145 ///Returns a reference to the NodeMap of the last but one nodes of the
147 ///\pre \ref run() must be called before using this function.
148 const PredNodeMap &predNodeMap() const { return pred_node;}
150 // bool reached(Node v) { return reach[v]; }
152 ///Checks if a node is reachable from the source.
154 ///Returns \c true if \c v is reachable from the source.
155 ///\warning the source node is reported to be unreached!
156 ///\todo Is this what we want?
157 ///\pre \ref run() must be called before using this function.
159 bool reached(Node v) { return G.valid(predecessor[v]); }
164 // **********************************************************************
166 // **********************************************************************
168 ///Runs %Dijkstra algorithm from node the source.
170 ///This method runs the %Dijkstra algorithm from a source node \c s
173 ///shortest path to each node. The algorithm computes
174 ///- The shortest path tree.
175 ///- The distance of each node from the source.
176 template <typename Graph, typename LengthMap, typename Heap >
177 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
180 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
181 predecessor.set(u,INVALID);
182 pred_node.set(u,INVALID);
183 // If a node is unreacheable, then why should be the dist=0?
184 // distance.set(u,0);
185 // reach.set(u,false);
188 //We don't need it at all.
190 // typename Graph::NodeMap<bool> scanned(G,false);
191 // //typename Graph::NodeMap<int> scanned(G,false);
192 typename Graph::NodeMap<int> heap_map(G,-1);
197 // reach.set(s, true);
199 while ( !heap.empty() ) {
202 ValueType oldvalue=heap[v];
204 distance.set(v, oldvalue);
206 for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) {
209 switch(heap.state(w)) {
211 // reach.set(w,true);
212 heap.push(w,oldvalue+length[e]);
213 predecessor.set(w,e);
217 if ( oldvalue+length[e] < heap[w] ) {
218 heap.decrease(w, oldvalue+length[e]);
219 predecessor.set(w,e);
223 case Heap::POST_HEAP:
230 } //END OF NAMESPACE HUGO