/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2011
* 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/smart_graph.h>
#include <lemon/preflow.h>
#include <lemon/concepts/digraph.h>
#include <lemon/concepts/maps.h>
#include <lemon/lgf_reader.h>
#include <lemon/elevator.h>
void checkPreflowCompile()
typedef concepts::Digraph Digraph;
typedef Digraph::Node Node;
typedef Digraph::Arc Arc;
typedef concepts::ReadMap<Arc,VType> CapMap;
typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
typedef concepts::WriteMap<Node,bool> CutMap;
typedef Elevator<Digraph, Digraph::Node> Elev;
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
typedef Preflow<Digraph, CapMap>
::SetStandardElevator<LinkedElev>
PreflowType preflow_test(g, cap, n, n);
const PreflowType& const_preflow_test = preflow_test;
const PreflowType::Elevator& elev = const_preflow_test.elevator();
preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
PreflowType::Tolerance tol = const_preflow_test.tolerance();
preflow_test.tolerance(tol);
preflow_test.startFirstPhase();
preflow_test.startSecondPhase();
preflow_test.runMinCut();
v = const_preflow_test.flowValue();
v = const_preflow_test.flow(e);
const FlowMap& fm = const_preflow_test.flowMap();
b = const_preflow_test.minCut(n);
const_preflow_test.minCutMap(cut);
ignore_unused_variable_warning(fm);
int cutValue (const SmartDigraph& g,
const SmartDigraph::NodeMap<bool>& cut,
const SmartDigraph::ArcMap<int>& cap) {
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
bool checkFlow(const SmartDigraph& g,
const SmartDigraph::ArcMap<int>& flow,
const SmartDigraph::ArcMap<int>& cap,
SmartDigraph::Node s, SmartDigraph::Node t) {
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
if (flow[e] < 0 || flow[e] > cap[e]) return false;
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
if (n == s || n == t) continue;
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
if (sum != 0) return false;
DIGRAPH_TYPEDEFS(SmartDigraph);
SmartDigraph::ArcMap<int> cap(g),iflow(g);
Node s=g.addNode(); Node t=g.addNode();
Node n1=g.addNode(); Node n2=g.addNode();
a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
Preflow<SmartDigraph> pre(g,cap,s,t);
check(pre.flowValue() == 10, "The incorrect max flow value.");
check(pre.minCut(s), "Wrong min cut (Node s).");
check(pre.minCut(n1), "Wrong min cut (Node n1).");
check(!pre.minCut(n2), "Wrong min cut (Node n2).");
check(!pre.minCut(t), "Wrong min cut (Node t).");
typedef SmartDigraph Digraph;
typedef Digraph::Node Node;
typedef Digraph::NodeIt NodeIt;
typedef Digraph::ArcIt ArcIt;
typedef Digraph::ArcMap<int> CapMap;
typedef Digraph::ArcMap<int> FlowMap;
typedef Digraph::NodeMap<bool> CutMap;
typedef Preflow<Digraph, CapMap> PType;
std::istringstream input(test_lgf);
DigraphReader<Digraph>(g,input).
PType preflow_test(g, cap, s, t);
check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
"The flow is not feasible.");
preflow_test.minCutMap(min_cut);
int min_cut_value=cutValue(g,min_cut,cap);
check(preflow_test.flowValue() == min_cut_value,
"The max flow value is not equal to the three min cut values.");
for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
int flow_value=preflow_test.flowValue();
for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
preflow_test.startFirstPhase();
preflow_test.minCutMap(min_cut1);
min_cut_value=cutValue(g,min_cut1,cap);
check(preflow_test.flowValue() == min_cut_value &&
min_cut_value == 2*flow_value,
"The max flow value or the min cut value is wrong.");
preflow_test.startSecondPhase();
check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
"The flow is not feasible.");
preflow_test.minCutMap(min_cut2);
min_cut_value=cutValue(g,min_cut2,cap);
check(preflow_test.flowValue() == min_cut_value &&
min_cut_value == 2*flow_value,
"The max flow value or the three min cut values were not doubled");
preflow_test.flowMap(flow);
if ( tmp1 != INVALID ) s=tmp1;
if ( tmp2 != INVALID ) t=tmp2;
preflow_test.minCutMap(min_cut3);
min_cut_value=cutValue(g,min_cut3,cap);
check(preflow_test.flowValue() == min_cut_value,
"The max flow value or the three min cut values are incorrect.");