2 #ifndef HUGO_DIJKSTRA_H
3 #define HUGO_DIJKSTRA_H
7 ///\brief Dijkstra algorithm.
17 ///%Dijkstra algorithm class.
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.
24 ///The type of the length is determined by the \c ValueType of the length map.
26 ///It is also possible to change the underlying priority heap.
28 ///\param Graph The graph type the algorithm runs on.
29 ///\param LengthMap This read-only
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".
41 template <typename Graph,
45 template <typename Graph,
46 typename LengthMap=typename Graph::template EdgeMap<int>,
47 template <class,class,class> class Heap = BinHeap >
51 typedef typename Graph::Node Node;
52 typedef typename Graph::NodeIt NodeIt;
53 typedef typename Graph::Edge Edge;
54 typedef typename Graph::OutEdgeIt OutEdgeIt;
56 typedef typename LengthMap::ValueType ValueType;
57 typedef typename Graph::template NodeMap<Edge> PredMap;
58 typedef typename Graph::template NodeMap<Node> PredNodeMap;
59 typedef typename Graph::template NodeMap<ValueType> DistMap;
63 const LengthMap& length;
65 PredNodeMap pred_node;
70 Dijkstra(Graph& _G, LengthMap& _length) :
71 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
75 ///The distance of a node from the root.
77 ///Returns the distance of a node from the root.
78 ///\pre \ref run() must be called before using this function.
79 ///\warning If node \c v in unreachable from the root the return value
80 ///of this funcion is undefined.
81 ValueType dist(Node v) const { return distance[v]; }
83 ///Returns the previous edge of the shortest path tree.
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
91 Edge pred(Node v) const { return predecessor[v]; }
93 ///Returns the previous node of the shortest path tree.
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.
101 Node predNode(Node v) const { return pred_node[v]; }
103 ///Returns a reference to the NodeMap of distances.
105 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
106 ///be called before using this function.
107 const DistMap &distMap() const { return distance;}
109 ///Returns a reference to the shortest path tree map.
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;}
116 ///Returns a reference to the map of nodes of shortest paths.
118 ///Returns a reference to the NodeMap of the last but one nodes of the
119 ///shortest path tree.
120 ///\pre \ref run() must be called before using this function.
121 const PredNodeMap &predNodeMap() const { return pred_node;}
123 ///Checks if a node is reachable from the root.
125 ///Returns \c true if \c v is reachable from the root.
126 ///\warning the root node is reported to be unreached!
127 ///\todo Is this what we want?
128 ///\pre \ref run() must be called before using this function.
130 bool reached(Node v) { return G.valid(predecessor[v]); }
135 // **********************************************************************
137 // **********************************************************************
139 ///Runs %Dijkstra algorithm from node the root.
141 ///This method runs the %Dijkstra algorithm from a root node \c s
144 ///shortest path to each node. The algorithm computes
145 ///- The shortest path tree.
146 ///- The distance of each node from the root.
147 template <typename Graph, typename LengthMap,
148 template<class,class,class> class Heap >
149 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
152 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
153 predecessor.set(u,INVALID);
154 pred_node.set(u,INVALID);
157 typename Graph::template NodeMap<int> heap_map(G,-1);
159 Heap<Node, ValueType, typename Graph::template NodeMap<int> >
164 while ( !heap.empty() ) {
167 ValueType oldvalue=heap[v];
169 distance.set(v, oldvalue);
171 { //FIXME this bracket is for e to be local
174 G.valid(e); G.next(e)) {
177 switch(heap.state(w)) {
179 heap.push(w,oldvalue+length[e]);
180 predecessor.set(w,e);
184 if ( oldvalue+length[e] < heap[w] ) {
185 heap.decrease(w, oldvalue+length[e]);
186 predecessor.set(w,e);
194 } //FIXME tis bracket
200 } //END OF NAMESPACE HUGO