1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_SUURBALLE_H
20 #define LEMON_SUURBALLE_H
22 ///\ingroup shortest_path
24 ///\brief An algorithm for finding arc-disjoint paths between two
25 /// nodes having minimum total length.
28 #include <lemon/bin_heap.h>
29 #include <lemon/path.h>
30 #include <lemon/list_graph.h>
31 #include <lemon/maps.h>
35 /// \addtogroup shortest_path
38 /// \brief Algorithm for finding arc-disjoint paths between two nodes
39 /// having minimum total length.
41 /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
42 /// finding arc-disjoint paths having minimum total length (cost)
43 /// from a given source node to a given target node in a digraph.
45 /// In fact, this implementation is the specialization of the
46 /// \ref CapacityScaling "successive shortest path" algorithm.
48 /// \tparam GR The digraph type the algorithm runs on.
49 /// The default value is \c ListDigraph.
50 /// \tparam LEN The type of the length (cost) map.
51 /// The default value is <tt>Digraph::ArcMap<int></tt>.
53 /// \warning Length values should be \e non-negative \e integers.
55 /// \note For finding node-disjoint paths this algorithm can be used
56 /// with \ref SplitNodes.
58 template <typename GR, typename LEN>
60 template < typename GR = ListDigraph,
61 typename LEN = typename GR::template ArcMap<int> >
65 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
67 typedef ConstMap<Arc, int> ConstArcMap;
68 typedef typename GR::template NodeMap<Arc> PredMap;
72 /// The type of the digraph the algorithm runs on.
74 /// The type of the length map.
75 typedef LEN LengthMap;
76 /// The type of the lengths.
77 typedef typename LengthMap::Value Length;
78 /// The type of the flow map.
79 typedef typename Digraph::template ArcMap<int> FlowMap;
80 /// The type of the potential map.
81 typedef typename Digraph::template NodeMap<Length> PotentialMap;
82 /// The type of the path structures.
83 typedef SimplePath<Digraph> Path;
87 /// \brief Special implementation of the Dijkstra algorithm
88 /// for finding shortest paths in the residual network.
90 /// \ref ResidualDijkstra is a special implementation of the
91 /// \ref Dijkstra algorithm for finding shortest paths in the
92 /// residual network of the digraph with respect to the reduced arc
93 /// lengths and modifying the node potentials according to the
94 /// distance of the nodes.
95 class ResidualDijkstra
97 typedef typename Digraph::template NodeMap<int> HeapCrossRef;
98 typedef BinHeap<Length, HeapCrossRef> Heap;
102 // The digraph the algorithm runs on
103 const Digraph &_graph;
106 const FlowMap &_flow;
107 const LengthMap &_length;
108 PotentialMap &_potential;
114 // The processed (i.e. permanently labeled) nodes
115 std::vector<Node> _proc_nodes;
123 ResidualDijkstra( const Digraph &digraph,
125 const LengthMap &length,
126 PotentialMap &potential,
129 _graph(digraph), _flow(flow), _length(length), _potential(potential),
130 _dist(digraph), _pred(pred), _s(s), _t(t) {}
132 /// \brief Run the algorithm. It returns \c true if a path is found
133 /// from the source node to the target node.
135 HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
136 Heap heap(heap_cross_ref);
142 while (!heap.empty() && heap.top() != _t) {
143 Node u = heap.top(), v;
144 Length d = heap.prio() + _potential[u], nd;
145 _dist[u] = heap.prio();
147 _proc_nodes.push_back(u);
149 // Traverse outgoing arcs
150 for (OutArcIt e(_graph, u); e != INVALID; ++e) {
152 v = _graph.target(e);
153 switch(heap.state(v)) {
155 heap.push(v, d + _length[e] - _potential[v]);
159 nd = d + _length[e] - _potential[v];
161 heap.decrease(v, nd);
165 case Heap::POST_HEAP:
171 // Traverse incoming arcs
172 for (InArcIt e(_graph, u); e != INVALID; ++e) {
174 v = _graph.source(e);
175 switch(heap.state(v)) {
177 heap.push(v, d - _length[e] - _potential[v]);
181 nd = d - _length[e] - _potential[v];
183 heap.decrease(v, nd);
187 case Heap::POST_HEAP:
193 if (heap.empty()) return false;
195 // Update potentials of processed nodes
196 Length t_dist = heap.prio();
197 for (int i = 0; i < int(_proc_nodes.size()); ++i)
198 _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
202 }; //class ResidualDijkstra
206 // The digraph the algorithm runs on
207 const Digraph &_graph;
209 const LengthMap &_length;
211 // Arc map of the current flow
214 // Node map of the current potentials
215 PotentialMap *_potential;
216 bool _local_potential;
223 // Container to store the found paths
224 std::vector< SimplePath<Digraph> > paths;
229 // Implementation of the Dijkstra algorithm for finding augmenting
230 // shortest paths in the residual network
231 ResidualDijkstra *_dijkstra;
235 /// \brief Constructor.
239 /// \param digraph The digraph the algorithm runs on.
240 /// \param length The length (cost) values of the arcs.
241 /// \param s The source node.
242 /// \param t The target node.
243 Suurballe( const Digraph &digraph,
244 const LengthMap &length,
246 _graph(digraph), _length(length), _flow(0), _local_flow(false),
247 _potential(0), _local_potential(false), _source(s), _target(t),
252 if (_local_flow) delete _flow;
253 if (_local_potential) delete _potential;
257 /// \brief Set the flow map.
259 /// This function sets the flow map.
261 /// The found flow contains only 0 and 1 values. It is the union of
262 /// the found arc-disjoint paths.
264 /// \return <tt>(*this)</tt>
265 Suurballe& flowMap(FlowMap &map) {
274 /// \brief Set the potential map.
276 /// This function sets the potential map.
278 /// The potentials provide the dual solution of the underlying
279 /// minimum cost flow problem.
281 /// \return <tt>(*this)</tt>
282 Suurballe& potentialMap(PotentialMap &map) {
283 if (_local_potential) {
285 _local_potential = false;
291 /// \name Execution control
292 /// The simplest way to execute the algorithm is to call the run()
295 /// If you only need the flow that is the union of the found
296 /// arc-disjoint paths, you may call init() and findFlow().
300 /// \brief Run the algorithm.
302 /// This function runs the algorithm.
304 /// \param k The number of paths to be found.
306 /// \return \c k if there are at least \c k arc-disjoint paths from
307 /// \c s to \c t in the digraph. Otherwise it returns the number of
308 /// arc-disjoint paths found.
310 /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
311 /// shortcut of the following code.
324 /// \brief Initialize the algorithm.
326 /// This function initializes the algorithm.
330 _flow = new FlowMap(_graph);
334 _potential = new PotentialMap(_graph);
335 _local_potential = true;
337 for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
338 for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
340 _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
345 /// \brief Execute the successive shortest path algorithm to find
348 /// This function executes the successive shortest path algorithm to
349 /// find a minimum cost flow, which is the union of \c k or less
350 /// arc-disjoint paths.
352 /// \return \c k if there are at least \c k arc-disjoint paths from
353 /// \c s to \c t in the digraph. Otherwise it returns the number of
354 /// arc-disjoint paths found.
356 /// \pre \ref init() must be called before using this function.
357 int findFlow(int k = 2) {
358 // Find shortest paths
360 while (_path_num < k) {
362 if (!_dijkstra->run()) break;
365 // Set the flow along the found shortest path
368 while ((e = _pred[u]) != INVALID) {
369 if (u == _graph.target(e)) {
371 u = _graph.source(e);
374 u = _graph.target(e);
381 /// \brief Compute the paths from the flow.
383 /// This function computes the paths from the flow.
385 /// \pre \ref init() and \ref findFlow() must be called before using
388 // Create the residual flow map (the union of the paths not found
390 FlowMap res_flow(_graph);
391 for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
394 paths.resize(_path_num);
395 for (int i = 0; i < _path_num; ++i) {
397 while (n != _target) {
398 OutArcIt e(_graph, n);
399 for ( ; res_flow[e] == 0; ++e) ;
400 n = _graph.target(e);
409 /// \name Query Functions
410 /// The results of the algorithm can be obtained using these
412 /// \n The algorithm should be executed before using them.
416 /// \brief Return a const reference to the arc map storing the
419 /// This function returns a const reference to the arc map storing
420 /// the flow that is the union of the found arc-disjoint paths.
422 /// \pre \ref run() or \ref findFlow() must be called before using
424 const FlowMap& flowMap() const {
428 /// \brief Return a const reference to the node map storing the
429 /// found potentials (the dual solution).
431 /// This function returns a const reference to the node map storing
432 /// the found potentials that provide the dual solution of the
433 /// underlying minimum cost flow problem.
435 /// \pre \ref run() or \ref findFlow() must be called before using
437 const PotentialMap& potentialMap() const {
441 /// \brief Return the flow on the given arc.
443 /// This function returns the flow on the given arc.
444 /// It is \c 1 if the arc is involved in one of the found paths,
445 /// otherwise it is \c 0.
447 /// \pre \ref run() or \ref findFlow() must be called before using
449 int flow(const Arc& arc) const {
450 return (*_flow)[arc];
453 /// \brief Return the potential of the given node.
455 /// This function returns the potential of the given node.
457 /// \pre \ref run() or \ref findFlow() must be called before using
459 Length potential(const Node& node) const {
460 return (*_potential)[node];
463 /// \brief Return the total length (cost) of the found paths (flow).
465 /// This function returns the total length (cost) of the found paths
466 /// (flow). The complexity of the function is O(e).
468 /// \pre \ref run() or \ref findFlow() must be called before using
470 Length totalLength() const {
472 for (ArcIt e(_graph); e != INVALID; ++e)
473 c += (*_flow)[e] * _length[e];
477 /// \brief Return the number of the found paths.
479 /// This function returns the number of the found paths.
481 /// \pre \ref run() or \ref findFlow() must be called before using
483 int pathNum() const {
487 /// \brief Return a const reference to the specified path.
489 /// This function returns a const reference to the specified path.
491 /// \param i The function returns the \c i-th path.
492 /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
494 /// \pre \ref run() or \ref findPaths() must be called before using
496 Path path(int i) const {
508 #endif //LEMON_SUURBALLE_H