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