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 Digraph The digraph type the algorithm runs on.
49 /// The default value is \c ListDigraph.
50 /// \tparam LengthMap 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 Digraph, typename LengthMap>
60 template < typename Digraph = ListDigraph,
61 typename LengthMap = typename Digraph::template ArcMap<int> >
65 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
67 typedef typename LengthMap::Value Length;
68 typedef ConstMap<Arc, int> ConstArcMap;
69 typedef typename Digraph::template NodeMap<Arc> PredMap;
73 /// The type of the flow map.
74 typedef typename Digraph::template ArcMap<int> FlowMap;
75 /// The type of the potential map.
76 typedef typename Digraph::template NodeMap<Length> PotentialMap;
77 /// The type of the path structures.
78 typedef SimplePath<Digraph> Path;
82 /// \brief Special implementation of the Dijkstra algorithm
83 /// for finding shortest paths in the residual network.
85 /// \ref ResidualDijkstra is a special implementation of the
86 /// \ref Dijkstra algorithm for finding shortest paths in the
87 /// residual network of the digraph with respect to the reduced arc
88 /// lengths and modifying the node potentials according to the
89 /// distance of the nodes.
90 class ResidualDijkstra
92 typedef typename Digraph::template NodeMap<int> HeapCrossRef;
93 typedef BinHeap<Length, HeapCrossRef> Heap;
97 // The digraph the algorithm runs on
98 const Digraph &_graph;
101 const FlowMap &_flow;
102 const LengthMap &_length;
103 PotentialMap &_potential;
109 // The processed (i.e. permanently labeled) nodes
110 std::vector<Node> _proc_nodes;
118 ResidualDijkstra( const Digraph &digraph,
120 const LengthMap &length,
121 PotentialMap &potential,
124 _graph(digraph), _flow(flow), _length(length), _potential(potential),
125 _dist(digraph), _pred(pred), _s(s), _t(t) {}
127 /// \brief Run the algorithm. It returns \c true if a path is found
128 /// from the source node to the target node.
130 HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
131 Heap heap(heap_cross_ref);
137 while (!heap.empty() && heap.top() != _t) {
138 Node u = heap.top(), v;
139 Length d = heap.prio() + _potential[u], nd;
140 _dist[u] = heap.prio();
142 _proc_nodes.push_back(u);
144 // Traverse outgoing arcs
145 for (OutArcIt e(_graph, u); e != INVALID; ++e) {
147 v = _graph.target(e);
148 switch(heap.state(v)) {
150 heap.push(v, d + _length[e] - _potential[v]);
154 nd = d + _length[e] - _potential[v];
156 heap.decrease(v, nd);
160 case Heap::POST_HEAP:
166 // Traverse incoming arcs
167 for (InArcIt e(_graph, u); e != INVALID; ++e) {
169 v = _graph.source(e);
170 switch(heap.state(v)) {
172 heap.push(v, d - _length[e] - _potential[v]);
176 nd = d - _length[e] - _potential[v];
178 heap.decrease(v, nd);
182 case Heap::POST_HEAP:
188 if (heap.empty()) return false;
190 // Update potentials of processed nodes
191 Length t_dist = heap.prio();
192 for (int i = 0; i < int(_proc_nodes.size()); ++i)
193 _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
197 }; //class ResidualDijkstra
201 // The digraph the algorithm runs on
202 const Digraph &_graph;
204 const LengthMap &_length;
206 // Arc map of the current flow
209 // Node map of the current potentials
210 PotentialMap *_potential;
211 bool _local_potential;
218 // Container to store the found paths
219 std::vector< SimplePath<Digraph> > paths;
224 // Implementation of the Dijkstra algorithm for finding augmenting
225 // shortest paths in the residual network
226 ResidualDijkstra *_dijkstra;
230 /// \brief Constructor.
234 /// \param digraph The digraph the algorithm runs on.
235 /// \param length The length (cost) values of the arcs.
236 /// \param s The source node.
237 /// \param t The target node.
238 Suurballe( const Digraph &digraph,
239 const LengthMap &length,
241 _graph(digraph), _length(length), _flow(0), _local_flow(false),
242 _potential(0), _local_potential(false), _source(s), _target(t),
247 if (_local_flow) delete _flow;
248 if (_local_potential) delete _potential;
252 /// \brief Set the flow map.
254 /// This function sets the flow map.
256 /// The found flow contains only 0 and 1 values. It is the union of
257 /// the found arc-disjoint paths.
259 /// \return \c (*this)
260 Suurballe& flowMap(FlowMap &map) {
269 /// \brief Set the potential map.
271 /// This function sets the potential map.
273 /// The potentials provide the dual solution of the underlying
274 /// minimum cost flow problem.
276 /// \return \c (*this)
277 Suurballe& potentialMap(PotentialMap &map) {
278 if (_local_potential) {
280 _local_potential = false;
286 /// \name Execution control
287 /// The simplest way to execute the algorithm is to call the run()
290 /// If you only need the flow that is the union of the found
291 /// arc-disjoint paths, you may call init() and findFlow().
295 /// \brief Run the algorithm.
297 /// This function runs the algorithm.
299 /// \param k The number of paths to be found.
301 /// \return \c k if there are at least \c k arc-disjoint paths from
302 /// \c s to \c t in the digraph. Otherwise it returns the number of
303 /// arc-disjoint paths found.
305 /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
306 /// shortcut of the following code.
319 /// \brief Initialize the algorithm.
321 /// This function initializes the algorithm.
325 _flow = new FlowMap(_graph);
329 _potential = new PotentialMap(_graph);
330 _local_potential = true;
332 for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
333 for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
335 _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
340 /// \brief Execute the successive shortest path algorithm to find
343 /// This function executes the successive shortest path algorithm to
344 /// find a minimum cost flow, which is the union of \c k or less
345 /// arc-disjoint paths.
347 /// \return \c k if there are at least \c k arc-disjoint paths from
348 /// \c s to \c t in the digraph. Otherwise it returns the number of
349 /// arc-disjoint paths found.
351 /// \pre \ref init() must be called before using this function.
352 int findFlow(int k = 2) {
353 // Find shortest paths
355 while (_path_num < k) {
357 if (!_dijkstra->run()) break;
360 // Set the flow along the found shortest path
363 while ((e = _pred[u]) != INVALID) {
364 if (u == _graph.target(e)) {
366 u = _graph.source(e);
369 u = _graph.target(e);
376 /// \brief Compute the paths from the flow.
378 /// This function computes the paths from the flow.
380 /// \pre \ref init() and \ref findFlow() must be called before using
383 // Create the residual flow map (the union of the paths not found
385 FlowMap res_flow(_graph);
386 for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
389 paths.resize(_path_num);
390 for (int i = 0; i < _path_num; ++i) {
392 while (n != _target) {
393 OutArcIt e(_graph, n);
394 for ( ; res_flow[e] == 0; ++e) ;
395 n = _graph.target(e);
404 /// \name Query Functions
405 /// The results of the algorithm can be obtained using these
407 /// \n The algorithm should be executed before using them.
411 /// \brief Return a const reference to the arc map storing the
414 /// This function returns a const reference to the arc map storing
415 /// the flow that is the union of the found arc-disjoint paths.
417 /// \pre \ref run() or \ref findFlow() must be called before using
419 const FlowMap& flowMap() const {
423 /// \brief Return a const reference to the node map storing the
424 /// found potentials (the dual solution).
426 /// This function returns a const reference to the node map storing
427 /// the found potentials that provide the dual solution of the
428 /// underlying minimum cost flow problem.
430 /// \pre \ref run() or \ref findFlow() must be called before using
432 const PotentialMap& potentialMap() const {
436 /// \brief Return the flow on the given arc.
438 /// This function returns the flow on the given arc.
439 /// It is \c 1 if the arc is involved in one of the found paths,
440 /// otherwise it is \c 0.
442 /// \pre \ref run() or \ref findFlow() must be called before using
444 int flow(const Arc& arc) const {
445 return (*_flow)[arc];
448 /// \brief Return the potential of the given node.
450 /// This function returns the potential of the given node.
452 /// \pre \ref run() or \ref findFlow() must be called before using
454 Length potential(const Node& node) const {
455 return (*_potential)[node];
458 /// \brief Return the total length (cost) of the found paths (flow).
460 /// This function returns the total length (cost) of the found paths
461 /// (flow). The complexity of the function is \f$ O(e) \f$.
463 /// \pre \ref run() or \ref findFlow() must be called before using
465 Length totalLength() const {
467 for (ArcIt e(_graph); e != INVALID; ++e)
468 c += (*_flow)[e] * _length[e];
472 /// \brief Return the number of the found paths.
474 /// This function returns the number of the found paths.
476 /// \pre \ref run() or \ref findFlow() must be called before using
478 int pathNum() const {
482 /// \brief Return a const reference to the specified path.
484 /// This function returns a const reference to the specified path.
486 /// \param i The function returns the \c i-th path.
487 /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
489 /// \pre \ref run() or \ref findPaths() must be called before using
491 Path path(int i) const {
503 #endif //LEMON_SUURBALLE_H