[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[833] | 19 | #include <fstream> |
---|
[858] | 20 | #include <string> |
---|
| 21 | |
---|
[833] | 22 | #include "test_tools.h" |
---|
[921] | 23 | #include <lemon/smart_graph.h> |
---|
| 24 | #include <lemon/dimacs.h> |
---|
| 25 | #include <lemon/preflow.h> |
---|
[959] | 26 | #include <lemon/concept/graph.h> |
---|
| 27 | #include <lemon/concept/maps.h> |
---|
[855] | 28 | |
---|
[921] | 29 | using namespace lemon; |
---|
[833] | 30 | |
---|
| 31 | void check_Preflow() |
---|
| 32 | { |
---|
| 33 | typedef int VType; |
---|
[959] | 34 | typedef concept::StaticGraph Graph; |
---|
[833] | 35 | |
---|
| 36 | typedef Graph::Node Node; |
---|
| 37 | typedef Graph::Edge Edge; |
---|
[959] | 38 | typedef concept::ReadMap<Edge,VType> CapMap; |
---|
| 39 | typedef concept::ReadWriteMap<Edge,VType> FlowMap; |
---|
| 40 | typedef concept::ReadWriteMap<Node,bool> CutMap; |
---|
[833] | 41 | |
---|
| 42 | typedef Preflow<Graph, int, CapMap, FlowMap> PType; |
---|
| 43 | |
---|
[940] | 44 | Graph g; |
---|
[833] | 45 | Node n; |
---|
| 46 | CapMap cap; |
---|
| 47 | FlowMap flow; |
---|
| 48 | CutMap cut; |
---|
| 49 | |
---|
[940] | 50 | PType preflow_test(g,n,n,cap,flow); |
---|
[833] | 51 | |
---|
| 52 | preflow_test.run(); |
---|
| 53 | preflow_test.flowValue(); |
---|
[1222] | 54 | preflow_test.source(n); |
---|
| 55 | preflow_test.flowMap(flow); |
---|
[833] | 56 | |
---|
| 57 | preflow_test.phase1(PType::NO_FLOW); |
---|
| 58 | preflow_test.minCut(cut); |
---|
| 59 | |
---|
| 60 | preflow_test.phase2(); |
---|
[1222] | 61 | preflow_test.target(n); |
---|
| 62 | preflow_test.capacityMap(cap); |
---|
[833] | 63 | preflow_test.minMinCut(cut); |
---|
| 64 | preflow_test.maxMinCut(cut); |
---|
| 65 | } |
---|
| 66 | |
---|
[940] | 67 | int cut_value ( SmartGraph& g, SmartGraph::NodeMap<bool>& cut, |
---|
[833] | 68 | SmartGraph::EdgeMap<int>& cap) { |
---|
| 69 | |
---|
| 70 | int c=0; |
---|
[940] | 71 | for(SmartGraph::EdgeIt e(g); e!=INVALID; ++e) { |
---|
[986] | 72 | if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
---|
[833] | 73 | } |
---|
| 74 | return c; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | int main() { |
---|
| 78 | |
---|
| 79 | typedef SmartGraph Graph; |
---|
| 80 | |
---|
[842] | 81 | typedef Graph::Node Node; |
---|
[833] | 82 | typedef Graph::NodeIt NodeIt; |
---|
| 83 | typedef Graph::EdgeIt EdgeIt; |
---|
| 84 | typedef Graph::EdgeMap<int> CapMap; |
---|
| 85 | typedef Graph::EdgeMap<int> FlowMap; |
---|
| 86 | typedef Graph::NodeMap<bool> CutMap; |
---|
| 87 | |
---|
| 88 | typedef Preflow<Graph, int> PType; |
---|
| 89 | |
---|
[859] | 90 | std::string f_name; |
---|
[1215] | 91 | if( getenv("srcdir") ) |
---|
| 92 | f_name = std::string(getenv("srcdir")); |
---|
| 93 | else f_name = "."; |
---|
| 94 | f_name += "/preflow_graph.dim"; |
---|
[855] | 95 | |
---|
[858] | 96 | std::ifstream file(f_name.c_str()); |
---|
[855] | 97 | |
---|
[858] | 98 | check(file, "Input file '" << f_name << "' not found."); |
---|
[833] | 99 | |
---|
[940] | 100 | Graph g; |
---|
[842] | 101 | Node s, t; |
---|
[940] | 102 | CapMap cap(g); |
---|
| 103 | readDimacs(file, g, cap, s, t); |
---|
[833] | 104 | |
---|
[940] | 105 | FlowMap flow(g,0); |
---|
[887] | 106 | |
---|
[833] | 107 | |
---|
[887] | 108 | |
---|
[940] | 109 | PType preflow_test(g, s, t, cap, flow); |
---|
[833] | 110 | preflow_test.run(PType::ZERO_FLOW); |
---|
[887] | 111 | |
---|
[940] | 112 | CutMap min_cut(g,false); |
---|
| 113 | preflow_test.minCut(min_cut); |
---|
| 114 | int min_cut_value=cut_value(g,min_cut,cap); |
---|
[833] | 115 | |
---|
[940] | 116 | CutMap min_min_cut(g,false); |
---|
| 117 | preflow_test.minMinCut(min_min_cut); |
---|
| 118 | int min_min_cut_value=cut_value(g,min_min_cut,cap); |
---|
[833] | 119 | |
---|
[940] | 120 | CutMap max_min_cut(g,false); |
---|
| 121 | preflow_test.maxMinCut(max_min_cut); |
---|
| 122 | int max_min_cut_value=cut_value(g,max_min_cut,cap); |
---|
[833] | 123 | |
---|
| 124 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 125 | min_cut_value == min_min_cut_value && |
---|
| 126 | min_min_cut_value == max_min_cut_value, |
---|
| 127 | "The max flow value is not equal to the three min cut values."); |
---|
| 128 | |
---|
| 129 | int flow_value=preflow_test.flowValue(); |
---|
| 130 | |
---|
| 131 | |
---|
[887] | 132 | |
---|
[940] | 133 | for(EdgeIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; |
---|
[1222] | 134 | preflow_test.capacityMap(cap); |
---|
[842] | 135 | |
---|
[833] | 136 | preflow_test.phase1(PType::PRE_FLOW); |
---|
| 137 | |
---|
[940] | 138 | CutMap min_cut1(g,false); |
---|
| 139 | preflow_test.minCut(min_cut1); |
---|
| 140 | min_cut_value=cut_value(g,min_cut1,cap); |
---|
[833] | 141 | |
---|
| 142 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 143 | min_cut_value == 2*flow_value, |
---|
| 144 | "The max flow value or the min cut value is wrong."); |
---|
| 145 | |
---|
| 146 | preflow_test.phase2(); |
---|
| 147 | |
---|
[940] | 148 | CutMap min_cut2(g,false); |
---|
| 149 | preflow_test.minCut(min_cut2); |
---|
| 150 | min_cut_value=cut_value(g,min_cut2,cap); |
---|
[833] | 151 | |
---|
[940] | 152 | CutMap min_min_cut2(g,false); |
---|
| 153 | preflow_test.minMinCut(min_min_cut2); |
---|
| 154 | min_min_cut_value=cut_value(g,min_min_cut2,cap); |
---|
[833] | 155 | |
---|
[940] | 156 | preflow_test.maxMinCut(max_min_cut); |
---|
| 157 | max_min_cut_value=cut_value(g,max_min_cut,cap); |
---|
[833] | 158 | |
---|
| 159 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 160 | min_cut_value == min_min_cut_value && |
---|
| 161 | min_min_cut_value == max_min_cut_value && |
---|
| 162 | min_cut_value == 2*flow_value, |
---|
| 163 | "The max flow value or the three min cut values were not doubled"); |
---|
| 164 | |
---|
[887] | 165 | |
---|
| 166 | |
---|
[940] | 167 | EdgeIt e(g); |
---|
[887] | 168 | for( int i=1; i==10; ++i ) { |
---|
| 169 | flow.set(e,0); |
---|
[833] | 170 | ++e; |
---|
| 171 | } |
---|
| 172 | |
---|
[1222] | 173 | preflow_test.flowMap(flow); |
---|
[887] | 174 | |
---|
[940] | 175 | NodeIt tmp1(g,s); |
---|
[887] | 176 | ++tmp1; |
---|
| 177 | if ( tmp1 != INVALID ) s=tmp1; |
---|
| 178 | |
---|
[940] | 179 | NodeIt tmp2(g,t); |
---|
[887] | 180 | ++tmp2; |
---|
| 181 | if ( tmp2 != INVALID ) t=tmp2; |
---|
| 182 | |
---|
[1222] | 183 | preflow_test.source(s); |
---|
| 184 | preflow_test.target(t); |
---|
[887] | 185 | |
---|
[833] | 186 | preflow_test.run(); |
---|
| 187 | |
---|
[940] | 188 | CutMap min_cut3(g,false); |
---|
| 189 | preflow_test.minCut(min_cut3); |
---|
| 190 | min_cut_value=cut_value(g,min_cut3,cap); |
---|
[833] | 191 | |
---|
[940] | 192 | CutMap min_min_cut3(g,false); |
---|
| 193 | preflow_test.minMinCut(min_min_cut3); |
---|
| 194 | min_min_cut_value=cut_value(g,min_min_cut3,cap); |
---|
[833] | 195 | |
---|
[940] | 196 | preflow_test.maxMinCut(max_min_cut); |
---|
| 197 | max_min_cut_value=cut_value(g,max_min_cut,cap); |
---|
[833] | 198 | |
---|
| 199 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 200 | min_cut_value == min_min_cut_value && |
---|
| 201 | min_min_cut_value == max_min_cut_value, |
---|
| 202 | "The max flow value or the three min cut values are incorrect."); |
---|
| 203 | } |
---|