/* -*- 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
#include <lemon/list_graph.h>
#include <lemon/lgf_reader.h>
#include <lemon/suurballe.h>
#include <lemon/concepts/digraph.h>
// Check the interface of Suurballe
void checkSuurballeCompile()
typedef concepts::Digraph Digraph;
typedef Digraph::Node Node;
typedef Digraph::Arc Arc;
typedef concepts::ReadMap<Arc, VType> LengthMap;
typedef Suurballe<Digraph, LengthMap> SuurballeType;
SuurballeType::FlowMap flow(g);
SuurballeType::PotentialMap pi(g);
SuurballeType suurb_test(g, len);
const SuurballeType& const_suurb_test = suurb_test;
k = suurb_test.run(n, n);
k = suurb_test.run(n, n, k);
k = suurb_test.findFlow(n);
k = suurb_test.findFlow(n, k);
c = const_suurb_test.totalLength();
f = const_suurb_test.flow(e);
const SuurballeType::FlowMap& fm =
const_suurb_test.flowMap();
c = const_suurb_test.potential(n);
const SuurballeType::PotentialMap& pm =
const_suurb_test.potentialMap();
k = const_suurb_test.pathNum();
Path<Digraph> p = const_suurb_test.path(k);
ignore_unused_variable_warning(fm);
ignore_unused_variable_warning(pm);
// Check the feasibility of the flow
template <typename Digraph, typename FlowMap>
bool checkFlow( const Digraph& gr, const FlowMap& flow,
typename Digraph::Node s, typename Digraph::Node t,
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
for (ArcIt e(gr); e != INVALID; ++e)
if (!(flow[e] == 0 || flow[e] == 1)) return false;
for (NodeIt n(gr); n != INVALID; ++n) {
for (OutArcIt e(gr, n); e != INVALID; ++e)
for (InArcIt e(gr, n); e != INVALID; ++e)
if (n == s && sum != value) return false;
if (n == t && sum != -value) return false;
if (n != s && n != t && sum != 0) return false;
// Check the optimalitiy of the flow
template < typename Digraph, typename CostMap,
typename FlowMap, typename PotentialMap >
bool checkOptimality( const Digraph& gr, const CostMap& cost,
const FlowMap& flow, const PotentialMap& pi )
// Check the "Complementary Slackness" optimality condition
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
for (ArcIt e(gr); e != INVALID; ++e) {
typename CostMap::Value red_cost =
cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
opt = (flow[e] == 0 && red_cost >= 0) ||
(flow[e] == 1 && red_cost <= 0);
template <typename Digraph, typename Path>
bool checkPath( const Digraph& gr, const Path& path,
typename Digraph::Node s, typename Digraph::Node t)
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
for (int i = 0; i < path.length(); ++i) {
if (gr.source(path.nth(i)) != n) return false;
n = gr.target(path.nth(i));
DIGRAPH_TYPEDEFS(ListDigraph);
ListDigraph::ArcMap<int> length(digraph);
std::istringstream input(test_lgf);
DigraphReader<ListDigraph>(digraph, input).
arcMap("length", length).
Suurballe<ListDigraph> suurballe(digraph, length);
check(suurballe.run(s, t) == 2, "Wrong number of paths");
check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
"The flow is not feasible");
check(suurballe.totalLength() == 510, "The flow is not optimal");
check(checkOptimality(digraph, length, suurballe.flowMap(),
suurballe.potentialMap()),
for (int i = 0; i < suurballe.pathNum(); ++i)
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
Suurballe<ListDigraph> suurballe(digraph, length);
check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
"The flow is not feasible");
check(suurballe.totalLength() == 1040, "The flow is not optimal");
check(checkOptimality(digraph, length, suurballe.flowMap(),
suurballe.potentialMap()),
for (int i = 0; i < suurballe.pathNum(); ++i)
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
// Find 5 paths (only 3 can be found)
Suurballe<ListDigraph> suurballe(digraph, length);
check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
"The flow is not feasible");
check(suurballe.totalLength() == 1040, "The flow is not optimal");
check(checkOptimality(digraph, length, suurballe.flowMap(),
suurballe.potentialMap()),
for (int i = 0; i < suurballe.pathNum(); ++i)
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");