alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2553: * Copyright (C) 2003-2008 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: jacint@833: #include klao@858: #include klao@858: jacint@833: #include "test_tools.h" alpar@921: #include alpar@921: #include alpar@921: #include alpar@2260: #include alpar@2260: #include alpar@855: alpar@921: using namespace lemon; jacint@833: deba@2514: void checkPreflow() jacint@833: { jacint@833: typedef int VType; alpar@2260: typedef concepts::Graph Graph; jacint@833: jacint@833: typedef Graph::Node Node; jacint@833: typedef Graph::Edge Edge; alpar@2260: typedef concepts::ReadMap CapMap; alpar@2260: typedef concepts::ReadWriteMap FlowMap; deba@2514: typedef concepts::WriteMap CutMap; jacint@833: marci@940: Graph g; jacint@833: Node n; deba@2514: Edge e; jacint@833: CapMap cap; jacint@833: FlowMap flow; jacint@833: CutMap cut; jacint@833: deba@2514: Preflow::DefFlowMap::Create preflow_test(g,cap,n,n); jacint@833: deba@2514: preflow_test.capacityMap(cap); deba@2514: flow = preflow_test.flowMap(); deba@2514: preflow_test.flowMap(flow); deba@2514: preflow_test.source(n); deba@2514: preflow_test.target(n); deba@2514: deba@2514: preflow_test.init(); deba@2514: preflow_test.flowInit(cap); deba@2514: preflow_test.startFirstPhase(); deba@2514: preflow_test.startSecondPhase(); jacint@833: preflow_test.run(); deba@2514: preflow_test.runMinCut(); deba@2514: jacint@833: preflow_test.flowValue(); deba@2514: preflow_test.minCut(n); deba@2514: preflow_test.minCutMap(cut); deba@2514: preflow_test.flow(e); jacint@833: jacint@833: } jacint@833: deba@2514: int cutValue (const SmartGraph& g, deba@2514: const SmartGraph::NodeMap& cut, deba@2514: const SmartGraph::EdgeMap& cap) { jacint@833: jacint@833: int c=0; marci@940: for(SmartGraph::EdgeIt e(g); e!=INVALID; ++e) { alpar@986: if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; jacint@833: } jacint@833: return c; jacint@833: } jacint@833: deba@2514: bool checkFlow(const SmartGraph& g, deba@2514: const SmartGraph::EdgeMap& flow, deba@2514: const SmartGraph::EdgeMap& cap, deba@2514: SmartGraph::Node s, SmartGraph::Node t) { deba@2514: deba@2514: for (SmartGraph::EdgeIt e(g); e != INVALID; ++e) { deba@2514: if (flow[e] < 0 || flow[e] > cap[e]) return false; deba@2514: } deba@2514: deba@2514: for (SmartGraph::NodeIt n(g); n != INVALID; ++n) { deba@2514: if (n == s || n == t) continue; deba@2514: int sum = 0; deba@2514: for (SmartGraph::OutEdgeIt e(g, n); e != INVALID; ++e) { deba@2514: sum += flow[e]; deba@2514: } deba@2514: for (SmartGraph::InEdgeIt e(g, n); e != INVALID; ++e) { deba@2514: sum -= flow[e]; deba@2514: } deba@2514: if (sum != 0) return false; deba@2514: } deba@2514: return true; deba@2514: } deba@2514: jacint@833: int main() { jacint@833: jacint@833: typedef SmartGraph Graph; jacint@833: alpar@842: typedef Graph::Node Node; jacint@833: typedef Graph::NodeIt NodeIt; jacint@833: typedef Graph::EdgeIt EdgeIt; jacint@833: typedef Graph::EdgeMap CapMap; jacint@833: typedef Graph::EdgeMap FlowMap; jacint@833: typedef Graph::NodeMap CutMap; jacint@833: deba@2514: typedef Preflow PType; jacint@833: klao@859: std::string f_name; alpar@1215: if( getenv("srcdir") ) alpar@1215: f_name = std::string(getenv("srcdir")); alpar@1215: else f_name = "."; ladanyi@2108: f_name += "/test/preflow_graph.dim"; alpar@855: klao@858: std::ifstream file(f_name.c_str()); alpar@855: klao@858: check(file, "Input file '" << f_name << "' not found."); jacint@833: marci@940: Graph g; alpar@842: Node s, t; marci@940: CapMap cap(g); marci@940: readDimacs(file, g, cap, s, t); jacint@833: deba@2514: PType preflow_test(g, cap, s, t); deba@2514: preflow_test.run(); deba@2514: deba@2514: check(checkFlow(g, preflow_test.flowMap(), cap, s, t), deba@2514: "The flow is not feasible."); jacint@887: deba@2514: CutMap min_cut(g); deba@2514: preflow_test.minCutMap(min_cut); deba@2514: int min_cut_value=cutValue(g,min_cut,cap); deba@2514: deba@2514: check(preflow_test.flowValue() == min_cut_value, deba@2514: "The max flow value is not equal to the three min cut values."); jacint@887: deba@2514: FlowMap flow(g); deba@2514: flow = preflow_test.flowMap(); jacint@833: jacint@833: int flow_value=preflow_test.flowValue(); jacint@833: deba@2514: for(EdgeIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; deba@2514: preflow_test.flowInit(flow); deba@2514: preflow_test.startFirstPhase(); jacint@833: deba@2514: CutMap min_cut1(g); deba@2514: preflow_test.minCutMap(min_cut1); deba@2514: min_cut_value=cutValue(g,min_cut1,cap); jacint@833: jacint@833: check(preflow_test.flowValue() == min_cut_value && jacint@833: min_cut_value == 2*flow_value, jacint@833: "The max flow value or the min cut value is wrong."); jacint@833: deba@2514: preflow_test.startSecondPhase(); jacint@833: deba@2514: check(checkFlow(g, preflow_test.flowMap(), cap, s, t), deba@2514: "The flow is not feasible."); deba@2514: deba@2514: CutMap min_cut2(g); deba@2514: preflow_test.minCutMap(min_cut2); deba@2514: min_cut_value=cutValue(g,min_cut2,cap); jacint@833: jacint@833: check(preflow_test.flowValue() == min_cut_value && jacint@833: min_cut_value == 2*flow_value, jacint@833: "The max flow value or the three min cut values were not doubled"); jacint@833: jacint@887: alpar@1222: preflow_test.flowMap(flow); jacint@887: marci@940: NodeIt tmp1(g,s); jacint@887: ++tmp1; jacint@887: if ( tmp1 != INVALID ) s=tmp1; jacint@887: marci@940: NodeIt tmp2(g,t); jacint@887: ++tmp2; jacint@887: if ( tmp2 != INVALID ) t=tmp2; jacint@887: alpar@1222: preflow_test.source(s); alpar@1222: preflow_test.target(t); jacint@887: jacint@833: preflow_test.run(); jacint@833: deba@2514: CutMap min_cut3(g); deba@2514: preflow_test.minCutMap(min_cut3); deba@2514: min_cut_value=cutValue(g,min_cut3,cap); jacint@833: jacint@833: deba@2514: check(preflow_test.flowValue() == min_cut_value, jacint@833: "The max flow value or the three min cut values are incorrect."); deba@2514: deba@2514: return 0; jacint@833: }