An experimental LPSolverWrapper class which uses glpk. For a short
demo, max flow problems are solved with it. This demo does not
demonstrates, but the main aims of this class are row and column
generation capabilities, i.e. to be a core for easily
implementable branch-and-cut a column generetion algorithms.
2 #ifndef HUGO_DIJKSTRA_H
3 #define HUGO_DIJKSTRA_H
7 ///\brief Dijkstra algorithm.
9 #include <hugo/bin_heap.h>
10 #include <hugo/invalid.h>
14 /// \addtogroup flowalgs
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 GR The graph type the algorithm runs on.
29 ///\param LM 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".
40 ///\author Jacint Szabo and Alpar Juttner
41 ///\todo We need a typedef-names should be standardized. (-:
42 ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
43 ///should not be fixed. (Problematic to solve).
46 template <typename GR,
50 template <typename GR,
51 typename LM=typename GR::template EdgeMap<int>,
52 template <class,class,class,class> class Heap = BinHeap >
56 ///The type of the underlying graph.
58 typedef typename Graph::Node Node;
59 typedef typename Graph::NodeIt NodeIt;
60 typedef typename Graph::Edge Edge;
61 typedef typename Graph::OutEdgeIt OutEdgeIt;
63 ///The type of the length of the edges.
64 typedef typename LM::ValueType ValueType;
65 ///The type of the map that stores the edge lengths.
67 ///\brief The type of the map that stores the last
68 ///edges of the shortest paths.
69 typedef typename Graph::template NodeMap<Edge> PredMap;
70 ///\brief The type of the map that stores the last but one
71 ///nodes of the shortest paths.
72 typedef typename Graph::template NodeMap<Node> PredNodeMap;
73 ///The type of the map that stores the dists of the nodes.
74 typedef typename Graph::template NodeMap<ValueType> DistMap;
81 bool local_predecessor;
82 PredNodeMap *pred_node;
89 ///\todo Error if \c G or are \c NULL. What about \c length?
90 ///\todo Better memory allocation (instead of new).
94 // local_length = true;
95 // length = new LM(G);
98 local_predecessor = true;
99 predecessor = new PredMap(*G);
102 local_pred_node = true;
103 pred_node = new PredNodeMap(*G);
106 local_distance = true;
107 distance = new DistMap(*G);
113 Dijkstra(const Graph& _G, const LM& _length) :
114 G(&_G), length(&_length),
115 predecessor(NULL), local_predecessor(false),
116 pred_node(NULL), local_pred_node(false),
117 distance(NULL), local_distance(false)
122 // if(local_length) delete length;
123 if(local_predecessor) delete predecessor;
124 if(local_pred_node) delete pred_node;
125 if(local_distance) delete distance;
128 ///Sets the graph the algorithm will run on.
130 ///Sets the graph the algorithm will run on.
131 ///\return <tt> (*this) </tt>
132 Dijkstra &setGraph(const Graph &_G)
137 ///Sets the length map.
139 ///Sets the length map.
140 ///\return <tt> (*this) </tt>
141 Dijkstra &setLengthMap(const LM &m)
143 // if(local_length) {
145 // local_length=false;
151 ///Sets the map storing the predecessor edges.
153 ///Sets the map storing the predecessor edges.
154 ///If you don't use this function before calling \ref run(),
155 ///it will allocate one. The destuctor deallocates this
156 ///automatically allocated map, of course.
157 ///\return <tt> (*this) </tt>
158 Dijkstra &setPredMap(PredMap &m)
160 if(local_predecessor) {
162 local_predecessor=false;
168 ///Sets the map storing the predecessor nodes.
170 ///Sets the map storing the predecessor nodes.
171 ///If you don't use this function before calling \ref run(),
172 ///it will allocate one. The destuctor deallocates this
173 ///automatically allocated map, of course.
174 ///\return <tt> (*this) </tt>
175 Dijkstra &setPredNodeMap(PredNodeMap &m)
177 if(local_pred_node) {
179 local_pred_node=false;
185 ///Sets the map storing the distances calculated by the algorithm.
187 ///Sets the map storing the distances calculated by the algorithm.
188 ///If you don't use this function before calling \ref run(),
189 ///it will allocate one. The destuctor deallocates this
190 ///automatically allocated map, of course.
191 ///\return <tt> (*this) </tt>
192 Dijkstra &setDistMap(DistMap &m)
196 local_distance=false;
202 ///Runs %Dijkstra algorithm from node \c s.
204 ///This method runs the %Dijkstra algorithm from a root node \c s
207 ///shortest path to each node. The algorithm computes
208 ///- The shortest path tree.
209 ///- The distance of each node from the root.
215 for ( NodeIt u(*G) ; G->valid(u) ; G->next(u) ) {
216 predecessor->set(u,INVALID);
217 pred_node->set(u,INVALID);
220 typename GR::template NodeMap<int> heap_map(*G,-1);
222 typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
223 std::less<ValueType> >
226 HeapType heap(heap_map);
230 while ( !heap.empty() ) {
233 ValueType oldvalue=heap[v];
235 distance->set(v, oldvalue);
238 for(OutEdgeIt e(*G,v); G->valid(e); G->next(e)) {
241 switch(heap.state(w)) {
242 case HeapType::PRE_HEAP:
243 heap.push(w,oldvalue+(*length)[e]);
244 predecessor->set(w,e);
247 case HeapType::IN_HEAP:
248 if ( oldvalue+(*length)[e] < heap[w] ) {
249 heap.decrease(w, oldvalue+(*length)[e]);
250 predecessor->set(w,e);
254 case HeapType::POST_HEAP:
261 ///The distance of a node from the root.
263 ///Returns the distance of a node from the root.
264 ///\pre \ref run() must be called before using this function.
265 ///\warning If node \c v in unreachable from the root the return value
266 ///of this funcion is undefined.
267 ValueType dist(Node v) const { return (*distance)[v]; }
269 ///Returns the 'previous edge' of the shortest path tree.
271 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
272 ///i.e. it returns the last edge from a shortest path from the root to \c
273 ///v. It is \ref INVALID
274 ///if \c v is unreachable from the root or if \c v=s. The
275 ///shortest path tree used here is equal to the shortest path tree used in
276 ///\ref predNode(Node v). \pre \ref run() must be called before using
278 Edge pred(Node v) const { return (*predecessor)[v]; }
280 ///Returns the 'previous node' of the shortest path tree.
282 ///For a node \c v it returns the 'previous node' of the shortest path tree,
283 ///i.e. it returns the last but one node from a shortest path from the
284 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
285 ///\c v=s. The shortest path tree used here is equal to the shortest path
286 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
287 ///using this function.
288 Node predNode(Node v) const { return (*pred_node)[v]; }
290 ///Returns a reference to the NodeMap of distances.
292 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
293 ///be called before using this function.
294 const DistMap &distMap() const { return *distance;}
296 ///Returns a reference to the shortest path tree map.
298 ///Returns a reference to the NodeMap of the edges of the
299 ///shortest path tree.
300 ///\pre \ref run() must be called before using this function.
301 const PredMap &predMap() const { return *predecessor;}
303 ///Returns a reference to the map of nodes of shortest paths.
305 ///Returns a reference to the NodeMap of the last but one nodes of the
306 ///shortest path tree.
307 ///\pre \ref run() must be called before using this function.
308 const PredNodeMap &predNodeMap() const { return *pred_node;}
310 ///Checks if a node is reachable from the root.
312 ///Returns \c true if \c v is reachable from the root.
313 ///\warning the root node is reported to be unreached!
314 ///\todo Is this what we want?
315 ///\pre \ref run() must be called before using this function.
317 bool reached(Node v) { return G->valid((*predecessor)[v]); }
322 // **********************************************************************
324 // **********************************************************************
328 } //END OF NAMESPACE HUGO