/* -*- 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/concepts/digraph.h>
#include <lemon/smart_graph.h>
#include <lemon/list_graph.h>
#include <lemon/lgf_reader.h>
#include <lemon/bellman_ford.h>
void checkBellmanFordCompile()
typedef concepts::Digraph Digraph;
typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
typedef BellmanFord<Digraph, LengthMap> BF;
typedef Digraph::Node Node;
typedef Digraph::Arc Arc;
concepts::Path<Digraph> pp;
const BF& const_bf_test = bf_test;
b = bf_test.processNextRound();
b = bf_test.processNextWeakRound();
l = const_bf_test.dist(t);
e = const_bf_test.predArc(t);
s = const_bf_test.predNode(t);
b = const_bf_test.reached(t);
d = const_bf_test.distMap();
p = const_bf_test.predMap();
pp = const_bf_test.path(t);
pp = const_bf_test.negativeCycle();
for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
::SetDistMap<concepts::ReadWriteMap<Node,Value> >
::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
::Create bf_test(gr,length);
concepts::ReadWriteMap<Node,Arc> pred_map;
concepts::ReadWriteMap<Node,Value> dist_map;
b = bf_test.processNextRound();
b = bf_test.processNextWeakRound();
pp = bf_test.negativeCycle();
void checkBellmanFordFunctionCompile()
typedef concepts::Digraph Digraph;
typedef Digraph::Arc Arc;
typedef Digraph::Node Node;
typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
bellmanFord(g,LengthMap()).run(Node());
b = bellmanFord(g,LengthMap()).run(Node(),Node());
bellmanFord(g,LengthMap())
.predMap(concepts::ReadWriteMap<Node,Arc>())
.distMap(concepts::ReadWriteMap<Node,Value>())
b=bellmanFord(g,LengthMap())
.predMap(concepts::ReadWriteMap<Node,Arc>())
.distMap(concepts::ReadWriteMap<Node,Value>())
.path(concepts::Path<Digraph>())
template <typename Digraph, typename Value>
void checkBellmanFord() {
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
typedef typename Digraph::template ArcMap<Value> LengthMap;
std::istringstream input(test_lgf);
digraphReader(gr, input).
arcMap("length", length).
BellmanFord<Digraph, LengthMap>
Path<Digraph> p = bf.path(t);
check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
check(p.length() == 3, "path() found a wrong path.");
check(checkPath(gr, p), "path() found a wrong path.");
check(pathSource(gr, p) == s, "path() found a wrong path.");
check(pathTarget(gr, p) == t, "path() found a wrong path.");
bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
check(reached && dist == -1, "Bellman-Ford found a wrong path.");
check(path.length() == 3, "path() found a wrong path.");
check(checkPath(gr, path), "path() found a wrong path.");
check(pathSource(gr, path) == s, "path() found a wrong path.");
check(pathTarget(gr, path) == t, "path() found a wrong path.");
for(ArcIt e(gr); e!=INVALID; ++e) {
check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
"Wrong output. dist(target)-dist(source)-arc_length=" <<
bf.dist(v) - bf.dist(u) - length[e]);
for(NodeIt v(gr); v!=INVALID; ++v) {
check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
if (bf.predArc(v)!=INVALID ) {
check(u==bf.predNode(v),"Wrong tree.");
check(bf.dist(v) - bf.dist(u) == length[e],
"Wrong distance! Difference: " <<
bf.dist(v) - bf.dist(u) - length[e]);
void checkBellmanFordNegativeCycle() {
DIGRAPH_TYPEDEFS(SmartDigraph);
Arc a1 = gr.addArc(n1, n2);
Arc a2 = gr.addArc(n2, n2);
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
StaticPath<SmartDigraph> p = bf.negativeCycle();
check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
"Wrong negative cycle.");
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
check(bf.negativeCycle().empty(),
"Negative cycle should not be found.");
length[gr.addArc(n1, n3)] = 5;
length[gr.addArc(n4, n3)] = 1;
length[gr.addArc(n2, n4)] = 2;
length[gr.addArc(n3, n2)] = -4;
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
for (int i = 0; i < 4; ++i) {
check(bf.negativeCycle().empty(),
"Negative cycle should not be found.");
StaticPath<SmartDigraph> p = bf.negativeCycle();
check(p.length() == 3, "Wrong negative cycle.");
check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
"Wrong negative cycle.");
checkBellmanFord<ListDigraph, int>();
checkBellmanFord<SmartDigraph, double>();
checkBellmanFordNegativeCycle();