1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/preflow_test.cc Mon May 23 04:48:14 2005 +0000
1.3 @@ -0,0 +1,201 @@
1.4 +/* -*- C++ -*-
1.5 + * test/preflow_test.cc - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#include <fstream>
1.21 +#include <string>
1.22 +
1.23 +#include "test_tools.h"
1.24 +#include <lemon/smart_graph.h>
1.25 +#include <lemon/dimacs.h>
1.26 +#include <lemon/preflow.h>
1.27 +#include <lemon/concept/graph.h>
1.28 +#include <lemon/concept/maps.h>
1.29 +
1.30 +using namespace lemon;
1.31 +
1.32 +void check_Preflow()
1.33 +{
1.34 + typedef int VType;
1.35 + typedef concept::StaticGraph Graph;
1.36 +
1.37 + typedef Graph::Node Node;
1.38 + typedef Graph::Edge Edge;
1.39 + typedef concept::ReadMap<Edge,VType> CapMap;
1.40 + typedef concept::ReadWriteMap<Edge,VType> FlowMap;
1.41 + typedef concept::ReadWriteMap<Node,bool> CutMap;
1.42 +
1.43 + typedef Preflow<Graph, int, CapMap, FlowMap> PType;
1.44 +
1.45 + Graph g;
1.46 + Node n;
1.47 + CapMap cap;
1.48 + FlowMap flow;
1.49 + CutMap cut;
1.50 +
1.51 + PType preflow_test(g,n,n,cap,flow);
1.52 +
1.53 + preflow_test.run();
1.54 + preflow_test.flowValue();
1.55 + preflow_test.source(n);
1.56 + preflow_test.flowMap(flow);
1.57 +
1.58 + preflow_test.phase1(PType::NO_FLOW);
1.59 + preflow_test.minCut(cut);
1.60 +
1.61 + preflow_test.phase2();
1.62 + preflow_test.target(n);
1.63 + preflow_test.capacityMap(cap);
1.64 + preflow_test.minMinCut(cut);
1.65 + preflow_test.maxMinCut(cut);
1.66 +}
1.67 +
1.68 +int cut_value ( SmartGraph& g, SmartGraph::NodeMap<bool>& cut,
1.69 + SmartGraph::EdgeMap<int>& cap) {
1.70 +
1.71 + int c=0;
1.72 + for(SmartGraph::EdgeIt e(g); e!=INVALID; ++e) {
1.73 + if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
1.74 + }
1.75 + return c;
1.76 +}
1.77 +
1.78 +int main() {
1.79 +
1.80 + typedef SmartGraph Graph;
1.81 +
1.82 + typedef Graph::Node Node;
1.83 + typedef Graph::NodeIt NodeIt;
1.84 + typedef Graph::EdgeIt EdgeIt;
1.85 + typedef Graph::EdgeMap<int> CapMap;
1.86 + typedef Graph::EdgeMap<int> FlowMap;
1.87 + typedef Graph::NodeMap<bool> CutMap;
1.88 +
1.89 + typedef Preflow<Graph, int> PType;
1.90 +
1.91 + std::string f_name;
1.92 + if( getenv("srcdir") )
1.93 + f_name = std::string(getenv("srcdir"));
1.94 + else f_name = ".";
1.95 + f_name += "/preflow_graph.dim";
1.96 +
1.97 + std::ifstream file(f_name.c_str());
1.98 +
1.99 + check(file, "Input file '" << f_name << "' not found.");
1.100 +
1.101 + Graph g;
1.102 + Node s, t;
1.103 + CapMap cap(g);
1.104 + readDimacs(file, g, cap, s, t);
1.105 +
1.106 + FlowMap flow(g,0);
1.107 +
1.108 +
1.109 +
1.110 + PType preflow_test(g, s, t, cap, flow);
1.111 + preflow_test.run(PType::ZERO_FLOW);
1.112 +
1.113 + CutMap min_cut(g,false);
1.114 + preflow_test.minCut(min_cut);
1.115 + int min_cut_value=cut_value(g,min_cut,cap);
1.116 +
1.117 + CutMap min_min_cut(g,false);
1.118 + preflow_test.minMinCut(min_min_cut);
1.119 + int min_min_cut_value=cut_value(g,min_min_cut,cap);
1.120 +
1.121 + CutMap max_min_cut(g,false);
1.122 + preflow_test.maxMinCut(max_min_cut);
1.123 + int max_min_cut_value=cut_value(g,max_min_cut,cap);
1.124 +
1.125 + check(preflow_test.flowValue() == min_cut_value &&
1.126 + min_cut_value == min_min_cut_value &&
1.127 + min_min_cut_value == max_min_cut_value,
1.128 + "The max flow value is not equal to the three min cut values.");
1.129 +
1.130 + int flow_value=preflow_test.flowValue();
1.131 +
1.132 +
1.133 +
1.134 + for(EdgeIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
1.135 + preflow_test.capacityMap(cap);
1.136 +
1.137 + preflow_test.phase1(PType::PRE_FLOW);
1.138 +
1.139 + CutMap min_cut1(g,false);
1.140 + preflow_test.minCut(min_cut1);
1.141 + min_cut_value=cut_value(g,min_cut1,cap);
1.142 +
1.143 + check(preflow_test.flowValue() == min_cut_value &&
1.144 + min_cut_value == 2*flow_value,
1.145 + "The max flow value or the min cut value is wrong.");
1.146 +
1.147 + preflow_test.phase2();
1.148 +
1.149 + CutMap min_cut2(g,false);
1.150 + preflow_test.minCut(min_cut2);
1.151 + min_cut_value=cut_value(g,min_cut2,cap);
1.152 +
1.153 + CutMap min_min_cut2(g,false);
1.154 + preflow_test.minMinCut(min_min_cut2);
1.155 + min_min_cut_value=cut_value(g,min_min_cut2,cap);
1.156 +
1.157 + preflow_test.maxMinCut(max_min_cut);
1.158 + max_min_cut_value=cut_value(g,max_min_cut,cap);
1.159 +
1.160 + check(preflow_test.flowValue() == min_cut_value &&
1.161 + min_cut_value == min_min_cut_value &&
1.162 + min_min_cut_value == max_min_cut_value &&
1.163 + min_cut_value == 2*flow_value,
1.164 + "The max flow value or the three min cut values were not doubled");
1.165 +
1.166 +
1.167 +
1.168 + EdgeIt e(g);
1.169 + for( int i=1; i==10; ++i ) {
1.170 + flow.set(e,0);
1.171 + ++e;
1.172 + }
1.173 +
1.174 + preflow_test.flowMap(flow);
1.175 +
1.176 + NodeIt tmp1(g,s);
1.177 + ++tmp1;
1.178 + if ( tmp1 != INVALID ) s=tmp1;
1.179 +
1.180 + NodeIt tmp2(g,t);
1.181 + ++tmp2;
1.182 + if ( tmp2 != INVALID ) t=tmp2;
1.183 +
1.184 + preflow_test.source(s);
1.185 + preflow_test.target(t);
1.186 +
1.187 + preflow_test.run();
1.188 +
1.189 + CutMap min_cut3(g,false);
1.190 + preflow_test.minCut(min_cut3);
1.191 + min_cut_value=cut_value(g,min_cut3,cap);
1.192 +
1.193 + CutMap min_min_cut3(g,false);
1.194 + preflow_test.minMinCut(min_min_cut3);
1.195 + min_min_cut_value=cut_value(g,min_min_cut3,cap);
1.196 +
1.197 + preflow_test.maxMinCut(max_min_cut);
1.198 + max_min_cut_value=cut_value(g,max_min_cut,cap);
1.199 +
1.200 + check(preflow_test.flowValue() == min_cut_value &&
1.201 + min_cut_value == min_min_cut_value &&
1.202 + min_min_cut_value == max_min_cut_value,
1.203 + "The max flow value or the three min cut values are incorrect.");
1.204 +}