A generic map with value type [0, N) where N is a small integer.
Can enumerate keys with a given value.
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.
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 template <class,class,class> class Heap = BinHeap >
75 typedef typename Graph::Node Node;
76 typedef typename Graph::NodeIt NodeIt;
77 typedef typename Graph::Edge Edge;
78 typedef typename Graph::OutEdgeIt OutEdgeIt;
80 typedef typename LengthMap::ValueType ValueType;
81 typedef typename Graph::NodeMap<Edge> PredMap;
82 typedef typename Graph::NodeMap<Node> PredNodeMap;
83 typedef typename Graph::NodeMap<ValueType> DistMap;
87 const LengthMap& length;
89 PredNodeMap pred_node;
94 Dijkstra(Graph& _G, LengthMap& _length) :
95 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
99 ///The distance of a node from the source.
101 ///Returns the distance of a node from the source.
102 ///\pre \ref run() must be called before using this function.
103 ///\warning If node \c v in unreachable from the source the return value
104 ///of this funcion is undefined.
105 ValueType dist(Node v) const { return distance[v]; }
106 ///Returns the edges of the shortest path tree.
108 ///For a node \c v it returns the last edge of the shortest path
109 ///from the source to \c v or INVALID if \c v is unreachable
111 ///\pre \ref run() must be called before using this function.
112 Edge pred(Node v) const { return predecessor[v]; }
113 ///Returns the nodes of the shortest paths.
115 ///For a node \c v it returns the last but one node of the shortest path
116 ///from the source to \c v or INVALID if \c v is unreachable
118 ///\pre \ref run() must be called before using this function.
119 Node predNode(Node v) const { return pred_node[v]; }
121 ///Returns a reference to the NodeMap of distances.
123 ///\pre \ref run() must be called before using this function.
125 const DistMap &distMap() const { return distance;}
126 ///Returns a reference to the shortest path tree map.
128 ///Returns a reference to the NodeMap of the edges of the
129 ///shortest path tree.
130 ///\pre \ref run() must be called before using this function.
131 const PredMap &predMap() const { return predecessor;}
132 ///Returns a reference to the map of nodes of shortest paths.
134 ///Returns a reference to the NodeMap of the last but one nodes of the
136 ///\pre \ref run() must be called before using this function.
137 const PredNodeMap &predNodeMap() const { return pred_node;}
139 ///Checks if a node is reachable from the source.
141 ///Returns \c true if \c v is reachable from the source.
142 ///\warning the source node is reported to be unreached!
143 ///\todo Is this what we want?
144 ///\pre \ref run() must be called before using this function.
146 bool reached(Node v) { return G.valid(predecessor[v]); }
151 // **********************************************************************
153 // **********************************************************************
155 ///Runs %Dijkstra algorithm from node the source.
157 ///This method runs the %Dijkstra algorithm from a source node \c s
160 ///shortest path to each node. The algorithm computes
161 ///- The shortest path tree.
162 ///- The distance of each node from the source.
163 template <typename Graph, typename LengthMap,
164 template<class,class,class> class Heap >
165 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
168 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
169 predecessor.set(u,INVALID);
170 pred_node.set(u,INVALID);
171 // If a node is unreacheable, then why should be the dist=0?
172 // distance.set(u,0);
173 // reach.set(u,false);
176 typename Graph::NodeMap<int> heap_map(G,-1);
178 Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
182 while ( !heap.empty() ) {
185 ValueType oldvalue=heap[v];
187 distance.set(v, oldvalue);
189 { //FIXME this bracket is for e to be local
192 G.valid(e); G.next(e)) {
195 switch(heap.state(w)) {
197 heap.push(w,oldvalue+length[e]);
198 predecessor.set(w,e);
202 if ( oldvalue+length[e] < heap[w] ) {
203 heap.decrease(w, oldvalue+length[e]);
204 predecessor.set(w,e);
212 } //FIXME tis bracket
216 } //END OF NAMESPACE HUGO