/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* 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>
#include <lemon/list_graph.h>
#include <lemon/dijkstra.h>
/// \brief Default traits class of Suurballe algorithm.
/// Default traits class of Suurballe algorithm.
/// \tparam GR The digraph type the algorithm runs on.
/// \tparam LEN The type of the length map.
/// The default value is <tt>GR::ArcMap<int></tt>.
template <typename GR, typename LEN>
typename LEN = typename GR::template ArcMap<int> >
struct SuurballeDefaultTraits
/// The type of the digraph.
/// The type of the length map.
/// The type of the lengths.
typedef typename LEN::Value Length;
/// The type of the flow map.
typedef typename GR::template ArcMap<int> FlowMap;
/// The type of the potential map.
typedef typename GR::template NodeMap<Length> PotentialMap;
/// The type used for storing the found arc-disjoint paths.
/// It must conform to the \ref lemon::concepts::Path "Path" concept
/// and it must have an \c addBack() function.
typedef lemon::Path<Digraph> Path;
/// The cross reference type used for the heap.
typedef typename GR::template NodeMap<int> HeapCrossRef;
/// \brief The heap type used for internal Dijkstra computations.
/// The type of the heap used for internal Dijkstra computations.
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept
/// and its priority type must be \c Length.
typedef BinHeap<Length, HeapCrossRef> Heap;
/// \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.
/// Note that this problem is a special case of the \ref min_cost_flow
/// "minimum cost flow problem". This implementation is actually an
/// efficient specialized version of the \ref CapacityScaling
/// "successive shortest path" algorithm directly for this problem.
/// Therefore this class provides query functions for flow values and
/// node potentials (the dual solution) just like the minimum cost flow
/// \tparam GR The digraph type the algorithm runs on.
/// \tparam LEN The type of the length map.
/// The default value is <tt>GR::ArcMap<int></tt>.
/// \warning Length values should be \e non-negative.
/// \note For finding \e node-disjoint paths, this algorithm can be used
/// along with the \ref SplitNodes adaptor.
template <typename GR, typename LEN, typename TR>
typename LEN = typename GR::template ArcMap<int>,
typename TR = SuurballeDefaultTraits<GR, LEN> >
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
typedef ConstMap<Arc, int> ConstArcMap;
typedef typename GR::template NodeMap<Arc> PredMap;
/// The type of the digraph.
typedef typename TR::Digraph Digraph;
/// The type of the length map.
typedef typename TR::LengthMap LengthMap;
/// The type of the lengths.
typedef typename TR::Length Length;
/// The type of the flow map.
typedef typename TR::FlowMap FlowMap;
/// The type of the potential map.
typedef typename TR::PotentialMap PotentialMap;
/// The type of the path structures.
typedef typename TR::Path Path;
/// The cross reference type used for the heap.
typedef typename TR::HeapCrossRef HeapCrossRef;
/// The heap type used for internal Dijkstra computations.
typedef typename TR::Heap Heap;
/// The \ref SuurballeDefaultTraits "traits class" of the algorithm.
// ResidualDijkstra is a special implementation of the
// Dijkstra algorithm for finding shortest paths in the
// residual network with respect to the reduced arc lengths
// and modifying the node potentials according to the
// distance of the nodes.
const LengthMap &_length;
std::vector<Node> _proc_nodes;
ResidualDijkstra(Suurballe &srb) :
_graph(srb._graph), _length(srb._length),
_flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred),
_s(srb._s), _t(srb._t), _dist(_graph) {}
// Run the algorithm and return true if a path is found
// from the source node to the target node.
return cnt == 0 ? startFirst() : start();
// Execute the algorithm for the first time (the flow and potential
// functions have to be identically zero).
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
Heap heap(heap_cross_ref);
while (!heap.empty() && heap.top() != _t) {
Length d = heap.prio(), dn;
_proc_nodes.push_back(u);
// Traverse outgoing arcs
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
heap.push(v, d + _length[e]);
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)
_pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist;
// Execute the algorithm.
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
Heap heap(heap_cross_ref);
while (!heap.empty() && heap.top() != _t) {
Length d = heap.prio() + _pi[u], dn;
_proc_nodes.push_back(u);
// Traverse outgoing arcs
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
heap.push(v, d + _length[e] - _pi[v]);
dn = d + _length[e] - _pi[v];
// Traverse incoming arcs
for (InArcIt e(_graph, u); e != INVALID; ++e) {
heap.push(v, d - _length[e] - _pi[v]);
dn = d - _length[e] - _pi[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)
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
}; //class ResidualDijkstra
/// \name Named Template Parameters
struct SetFlowMapTraits : public Traits {
/// \brief \ref named-templ-param "Named parameter" for setting
/// \ref named-templ-param "Named parameter" for setting
: public Suurballe<GR, LEN, SetFlowMapTraits<T> > {
typedef Suurballe<GR, LEN, SetFlowMapTraits<T> > Create;
struct SetPotentialMapTraits : public Traits {
/// \brief \ref named-templ-param "Named parameter" for setting
/// \c PotentialMap type.
/// \ref named-templ-param "Named parameter" for setting
/// \c PotentialMap type.
: public Suurballe<GR, LEN, SetPotentialMapTraits<T> > {
typedef Suurballe<GR, LEN, SetPotentialMapTraits<T> > Create;
struct SetPathTraits : public Traits {
/// \brief \ref named-templ-param "Named parameter" for setting
/// \ref named-templ-param "Named parameter" for setting \c %Path type.
/// It must conform to the \ref lemon::concepts::Path "Path" concept
/// and it must have an \c addBack() function.
: public Suurballe<GR, LEN, SetPathTraits<T> > {
typedef Suurballe<GR, LEN, SetPathTraits<T> > Create;
template <typename H, typename CR>
struct SetHeapTraits : public Traits {
/// \brief \ref named-templ-param "Named parameter" for setting
/// \c Heap and \c HeapCrossRef types.
/// \ref named-templ-param "Named parameter" for setting \c Heap
/// and \c HeapCrossRef types with automatic allocation.
/// They will be used for internal Dijkstra computations.
/// The heap type must conform to the \ref lemon::concepts::Heap "Heap"
/// concept and its priority type must be \c Length.
typename CR = typename Digraph::template NodeMap<int> >
: public Suurballe<GR, LEN, SetHeapTraits<H, CR> > {
typedef Suurballe<GR, LEN, SetHeapTraits<H, CR> > Create;
// 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<Path> _paths;
PotentialMap *_init_dist;
/// \param graph The digraph the algorithm runs on.
/// \param length The length (cost) values of the arcs.
Suurballe( const Digraph &graph,
const LengthMap &length ) :
_graph(graph), _length(length), _flow(0), _local_flow(false),
_potential(0), _local_potential(false), _pred(graph),
_init_dist(0), _init_pred(0)
if (_local_flow) delete _flow;
if (_local_potential) delete _potential;
/// \brief Set the flow map.
/// This function sets the flow map.
/// If it is not used before calling \ref run() or \ref init(),
/// an instance will be allocated automatically. The destructor
/// deallocates this automatically allocated map, of course.
/// The found flow contains only 0 and 1 values, since it is the
/// union of the found arc-disjoint paths.
/// \return <tt>(*this)</tt>
Suurballe& flowMap(FlowMap &map) {
/// \brief Set the potential map.
/// This function sets the potential map.
/// If it is not used before calling \ref run() or \ref init(),
/// an instance will be allocated automatically. The destructor
/// deallocates this automatically allocated map, of course.
/// The node potentials provide the dual solution of the underlying
/// \ref min_cost_flow "minimum cost flow problem".
/// \return <tt>(*this)</tt>
Suurballe& potentialMap(PotentialMap &map) {
_local_potential = false;
/// \name Execution Control
/// The simplest way to execute the algorithm is to call the run()
/// If you need to execute the algorithm many times using the same
/// source node, then you may call fullInit() once and start()
/// for each target node.\n
/// If you only need the flow that is the union of the found
/// arc-disjoint paths, then you may call findFlow() instead of
/// \brief Run the algorithm.
/// This function runs the algorithm.
/// \param s The source node.
/// \param t The target node.
/// \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(s, t, k)</tt> is
/// just a shortcut of the following code.
int run(const Node& s, const Node& t, int k = 2) {
/// \brief Initialize the algorithm.
/// This function initializes the algorithm with the given source node.
/// \param s The source node.
void init(const Node& s) {
_flow = new FlowMap(_graph);
_potential = new PotentialMap(_graph);
/// \brief Initialize the algorithm and perform Dijkstra.
/// This function initializes the algorithm and performs a full
/// Dijkstra search from the given source node. It makes consecutive
/// executions of \ref start() "start(t, k)" faster, since they
/// have to perform %Dijkstra only k-1 times.
/// This initialization is usually worth using instead of \ref init()
/// if the algorithm is executed many times using the same source node.
/// \param s The source node.
void fullInit(const Node& s) {
_init_dist = new PotentialMap(_graph);
_init_pred = new PredMap(_graph);
typename Dijkstra<Digraph, LengthMap>
::template SetStandardHeap<Heap>
::template SetDistMap<PotentialMap>
::template SetPredMap<PredMap>
::Create dijk(_graph, _length);
dijk.distMap(*_init_dist).predMap(*_init_pred);
/// \brief Execute the algorithm.
/// This function executes the algorithm.
/// \param t The target node.
/// \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.start(t, k)</tt> is
/// just a shortcut of the following code.
int start(const Node& t, int k = 2) {
/// \brief Execute the algorithm to find an optimal flow.
/// This function executes the successive shortest path algorithm to
/// find a minimum cost flow, which is the union of \c k (or less)
/// \param t The target node.
/// \param k The number of paths to be found.
/// \return \c k if there are at least \c k arc-disjoint paths from
/// the source node to the given node \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(const Node& t, int k = 2) {
ResidualDijkstra dijkstra(*this);
for (ArcIt e(_graph); e != INVALID; ++e) {
for (NodeIt n(_graph); n != INVALID; ++n) {
(*_potential)[n] = (*_init_dist)[n];
while ((e = (*_init_pred)[u]) != INVALID) {
for (NodeIt n(_graph); n != INVALID; ++n) {
if (!dijkstra.run(_path_num)) 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 arc-disjoint paths from the found minimum
/// cost flow, which is the union of them.
/// \pre \ref init() and \ref findFlow() must be called before using
FlowMap res_flow(_graph);
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
_paths.resize(_path_num);
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 the total length of the found paths.
/// This function returns the total length of the found paths, i.e.
/// the total cost of the found flow.
/// The complexity of the function is O(e).
/// \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 flow value on the given arc.
/// This function returns the flow value on the given arc.
/// It is \c 1 if the arc is involved in one of the found arc-disjoint
/// 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 a const reference to an arc map storing the
/// This function returns a const reference to an 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 the potential of the given node.
/// This function returns the potential of the given node.
/// The node potentials provide the dual solution of the
/// underlying \ref min_cost_flow "minimum cost flow problem".
/// \pre \ref run() or \ref findFlow() must be called before using
Length potential(const Node& node) const {
return (*_potential)[node];
/// \brief Return a const reference to a node map storing the
/// found potentials (the dual solution).
/// This function returns a const reference to a node map storing
/// the found potentials that provide the dual solution of the
/// underlying \ref min_cost_flow "minimum cost flow problem".
/// \pre \ref run() or \ref findFlow() must be called before using
const PotentialMap& potentialMap() const {
/// \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 <tt>i</tt>-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
const Path& path(int i) const {
#endif //LEMON_SUURBALLE_H