* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#ifndef LEMON_SUURBALLE_H
#define LEMON_SUURBALLE_H
///\ingroup shortest_path
///\brief An algorithm for finding arc-disjoint paths between two
/// nodes having minimum total length.
#include <lemon/bin_heap.h>
/// \addtogroup shortest_path
/// \brief Algorithm for finding arc-disjoint paths between two nodes
/// having minimum total length.
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for
/// finding arc-disjoint paths having minimum total length (cost)
/// from a given source node to a given target node in a digraph.
/// In fact, this implementation is the specialization of the
/// \ref CapacityScaling "successive shortest path" algorithm.
/// \tparam Digraph The digraph type the algorithm runs on.
/// The default value is \c ListDigraph.
/// \tparam LengthMap The type of the length (cost) map.
/// The default value is <tt>Digraph::ArcMap<int></tt>.
/// \warning Length values should be \e non-negative \e integers.
/// \note For finding node-disjoint paths this algorithm can be used
/// with \ref SplitDigraphAdaptor.
template <typename Digraph, typename LengthMap>
template < typename Digraph = ListDigraph,
typename LengthMap = typename Digraph::template ArcMap<int> >
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
typedef typename LengthMap::Value Length;
typedef ConstMap<Arc, int> ConstArcMap;
typedef typename Digraph::template NodeMap<Arc> PredMap;
/// The type of the flow map.
typedef typename Digraph::template ArcMap<int> FlowMap;
/// The type of the potential map.
typedef typename Digraph::template NodeMap<Length> PotentialMap;
/// The type of the path structures.
typedef SimplePath<Digraph> Path;
/// \brief Special implementation of the Dijkstra algorithm
/// for finding shortest paths in the residual network.
/// \ref ResidualDijkstra is a special implementation of the
/// \ref Dijkstra algorithm for finding shortest paths in the
/// residual network of the digraph with respect to the reduced arc
/// lengths and modifying the node potentials according to the
/// distance of the nodes.
typedef typename Digraph::template NodeMap<int> HeapCrossRef;
typedef BinHeap<Length, HeapCrossRef> Heap;
// The digraph the algorithm runs on
const LengthMap &_length;
PotentialMap &_potential;
// The processed (i.e. permanently labeled) nodes
std::vector<Node> _proc_nodes;
ResidualDijkstra( const Digraph &digraph,
_graph(digraph), _flow(flow), _length(length), _potential(potential),
_dist(digraph), _pred(pred), _s(s), _t(t) {}
/// \brief Run the algorithm. It returns \c true if a path is found
/// from the source node to the target node.
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
Heap heap(heap_cross_ref);
while (!heap.empty() && heap.top() != _t) {
Length d = heap.prio() + _potential[u], nd;
_proc_nodes.push_back(u);
// Traverse outgoing arcs
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
heap.push(v, d + _length[e] - _potential[v]);
nd = d + _length[e] - _potential[v];
// Traverse incoming arcs
for (InArcIt e(_graph, u); e != INVALID; ++e) {
heap.push(v, d - _length[e] - _potential[v]);
nd = d - _length[e] - _potential[v];
if (heap.empty()) return false;
// Update potentials of processed nodes
Length t_dist = heap.prio();
for (int i = 0; i < int(_proc_nodes.size()); ++i)
_potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
}; //class ResidualDijkstra
// The digraph the algorithm runs on
const LengthMap &_length;
// Arc map of the current flow
// Node map of the current potentials
PotentialMap *_potential;
// Container to store the found paths
std::vector< SimplePath<Digraph> > paths;
// Implementation of the Dijkstra algorithm for finding augmenting
// shortest paths in the residual network
ResidualDijkstra *_dijkstra;
/// \param digraph The digraph the algorithm runs on.
/// \param length The length (cost) values of the arcs.
/// \param s The source node.
/// \param t The target node.
Suurballe( const Digraph &digraph,
_graph(digraph), _length(length), _flow(0), _local_flow(false),
_potential(0), _local_potential(false), _source(s), _target(t),
if (_local_flow) delete _flow;
if (_local_potential) delete _potential;
/// \brief Set the flow map.
/// This function sets the flow map.
/// The found flow contains only 0 and 1 values. It is the union of
/// the found arc-disjoint paths.
Suurballe& flowMap(FlowMap &map) {
/// \brief Set the potential map.
/// This function sets the potential map.
/// The potentials provide the dual solution of the underlying
/// minimum cost flow problem.
Suurballe& potentialMap(PotentialMap &map) {
_local_potential = false;
/// \name Execution control
/// The simplest way to execute the algorithm is to call the run()
/// If you only need the flow that is the union of the found
/// arc-disjoint paths, you may call init() and findFlow().
/// \brief Run the algorithm.
/// This function runs the algorithm.
/// \param k The number of paths to be found.
/// \return \c k if there are at least \c k arc-disjoint paths from
/// \c s to \c t in the digraph. Otherwise it returns the number of
/// arc-disjoint paths found.
/// \note Apart from the return value, <tt>s.run(k)</tt> is just a
/// shortcut of the following code.
/// \brief Initialize the algorithm.
/// This function initializes the algorithm.
_flow = new FlowMap(_graph);
_potential = new PotentialMap(_graph);
for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
_dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
/// \brief Execute the successive shortest path algorithm to find
/// This function executes the successive shortest path algorithm to
/// find a minimum cost flow, which is the union of \c k or less
/// \return \c k if there are at least \c k arc-disjoint paths from
/// \c s to \c t in the digraph. Otherwise it returns the number of
/// arc-disjoint paths found.
/// \pre \ref init() must be called before using this function.
int findFlow(int k = 2) {
if (!_dijkstra->run()) break;
// Set the flow along the found shortest path
while ((e = _pred[u]) != INVALID) {
if (u == _graph.target(e)) {
/// \brief Compute the paths from the flow.
/// This function computes the paths from the flow.
/// \pre \ref init() and \ref findFlow() must be called before using
// Create the residual flow map (the union of the paths not found
FlowMap res_flow(_graph);
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
for (int i = 0; i < _path_num; ++i) {
for ( ; res_flow[e] == 0; ++e) ;
/// \name Query Functions
/// The results of the algorithm can be obtained using these
/// \n The algorithm should be executed before using them.
/// \brief Return a const reference to the arc map storing the
/// This function returns a const reference to the arc map storing
/// the flow that is the union of the found arc-disjoint paths.
/// \pre \ref run() or \ref findFlow() must be called before using
const FlowMap& flowMap() const {
/// \brief Return a const reference to the node map storing the
/// found potentials (the dual solution).
/// This function returns a const reference to the node map storing
/// the found potentials that provide the dual solution of the
/// underlying minimum cost flow problem.
/// \pre \ref run() or \ref findFlow() must be called before using
const PotentialMap& potentialMap() const {
/// \brief Return the flow on the given arc.
/// This function returns the flow on the given arc.
/// It is \c 1 if the arc is involved in one of the found paths,
/// otherwise it is \c 0.
/// \pre \ref run() or \ref findFlow() must be called before using
int flow(const Arc& arc) const {
/// \brief Return the potential of the given node.
/// This function returns the potential of the given node.
/// \pre \ref run() or \ref findFlow() must be called before using
Length potential(const Node& node) const {
return (*_potential)[node];
/// \brief Return the total length (cost) of the found paths (flow).
/// This function returns the total length (cost) of the found paths
/// (flow). The complexity of the function is \f$ O(e) \f$.
/// \pre \ref run() or \ref findFlow() must be called before using
Length totalLength() const {
for (ArcIt e(_graph); e != INVALID; ++e)
c += (*_flow)[e] * _length[e];
/// \brief Return the number of the found paths.
/// This function returns the number of the found paths.
/// \pre \ref run() or \ref findFlow() must be called before using
/// \brief Return a const reference to the specified path.
/// This function returns a const reference to the specified path.
/// \param i The function returns the \c i-th path.
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
/// \pre \ref run() or \ref findPaths() must be called before using
#endif //LEMON_SUURBALLE_H