Docs are now divided into modules.
2 #ifndef HUGO_DIJKSTRA_H
3 #define HUGO_DIJKSTRA_H
6 ///\brief Dijkstra algorithm.
13 ///%Dijkstra algorithm class.
15 ///This class provides an efficient implementation of %Dijkstra algorithm.
16 ///The edge lengths are passed to the algorithm using a
17 ///\ref ReadMapSkeleton "readable map",
18 ///so it is easy to change it to any kind of length.
20 ///The type of the length is determined by the \c ValueType of the length map.
22 ///It is also possible to change the underlying priority heap.
24 ///\param Graph The graph type the algorithm runs on.
25 ///\param LengthMap This read-only
28 ///lengths of the edges. It is read once for each edge, so the map
29 ///may involve in relatively time consuming process to compute the edge
30 ///length if it is necessary. The default map type is
31 ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
32 ///\param Heap The heap type used by the %Dijkstra
33 ///algorithm. The default
34 ///is using \ref BinHeap "binary heap".
37 template <typename Graph,
41 template <typename Graph,
42 typename LengthMap=typename Graph::EdgeMap<int>,
43 template <class,class,class> class Heap = BinHeap >
47 typedef typename Graph::Node Node;
48 typedef typename Graph::NodeIt NodeIt;
49 typedef typename Graph::Edge Edge;
50 typedef typename Graph::OutEdgeIt OutEdgeIt;
52 typedef typename LengthMap::ValueType ValueType;
53 typedef typename Graph::NodeMap<Edge> PredMap;
54 typedef typename Graph::NodeMap<Node> PredNodeMap;
55 typedef typename Graph::NodeMap<ValueType> DistMap;
59 const LengthMap& length;
61 PredNodeMap pred_node;
66 Dijkstra(Graph& _G, LengthMap& _length) :
67 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
71 ///The distance of a node from the root.
73 ///Returns the distance of a node from the root.
74 ///\pre \ref run() must be called before using this function.
75 ///\warning If node \c v in unreachable from the root the return value
76 ///of this funcion is undefined.
77 ValueType dist(Node v) const { return distance[v]; }
79 ///Returns the previous edge of the shortest path tree.
81 ///For a node \c v it returns the previous edge of the shortest path tree,
82 ///i.e. it returns the last edge from a shortest path from the root to \c
83 ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
84 ///shortest path tree used here is equal to the shortest path tree used in
85 ///\ref predNode(Node v). \pre \ref run() must be called before using
87 Edge pred(Node v) const { return predecessor[v]; }
89 ///Returns the previous node of the shortest path tree.
91 ///For a node \c v it returns the previous node of the shortest path tree,
92 ///i.e. it returns the last but one node from a shortest path from the
93 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
94 ///\c v=s. The shortest path tree used here is equal to the shortest path
95 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
96 ///using this function.
97 Node predNode(Node v) const { return pred_node[v]; }
99 ///Returns a reference to the NodeMap of distances.
101 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
102 ///be called before using this function.
103 const DistMap &distMap() const { return distance;}
105 ///Returns a reference to the shortest path tree map.
107 ///Returns a reference to the NodeMap of the edges of the
108 ///shortest path tree.
109 ///\pre \ref run() must be called before using this function.
110 const PredMap &predMap() const { return predecessor;}
112 ///Returns a reference to the map of nodes of shortest paths.
114 ///Returns a reference to the NodeMap of the last but one nodes of the
115 ///shortest path tree.
116 ///\pre \ref run() must be called before using this function.
117 const PredNodeMap &predNodeMap() const { return pred_node;}
119 ///Checks if a node is reachable from the root.
121 ///Returns \c true if \c v is reachable from the root.
122 ///\warning the root node is reported to be unreached!
123 ///\todo Is this what we want?
124 ///\pre \ref run() must be called before using this function.
126 bool reached(Node v) { return G.valid(predecessor[v]); }
131 // **********************************************************************
133 // **********************************************************************
135 ///Runs %Dijkstra algorithm from node the root.
137 ///This method runs the %Dijkstra algorithm from a root node \c s
140 ///shortest path to each node. The algorithm computes
141 ///- The shortest path tree.
142 ///- The distance of each node from the root.
143 template <typename Graph, typename LengthMap,
144 template<class,class,class> class Heap >
145 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
148 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
149 predecessor.set(u,INVALID);
150 pred_node.set(u,INVALID);
153 typename Graph::NodeMap<int> heap_map(G,-1);
155 Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
159 while ( !heap.empty() ) {
162 ValueType oldvalue=heap[v];
164 distance.set(v, oldvalue);
166 { //FIXME this bracket is for e to be local
169 G.valid(e); G.next(e)) {
172 switch(heap.state(w)) {
174 heap.push(w,oldvalue+length[e]);
175 predecessor.set(w,e);
179 if ( oldvalue+length[e] < heap[w] ) {
180 heap.decrease(w, oldvalue+length[e]);
181 predecessor.set(w,e);
189 } //FIXME tis bracket
193 } //END OF NAMESPACE HUGO