Fix a DANGEROUS bug.
2 #include "test_tools.h"
3 #include <hugo/smart_graph.h>
4 #include <hugo/dimacs.h>
5 #include <hugo/preflow.h>
6 #include <hugo/skeletons/graph.h>
7 #include <hugo/skeletons/maps.h>
13 typedef skeleton::StaticGraphSkeleton Graph;
15 typedef Graph::Node Node;
16 typedef Graph::Edge Edge;
17 typedef skeleton::ReadMap<Edge,VType> CapMap;
18 typedef skeleton::ReadWriteMap<Edge,VType> FlowMap;
19 typedef skeleton::ReadWriteMap<Node,bool> CutMap;
21 typedef Preflow<Graph, int, CapMap, FlowMap> PType;
29 PType preflow_test(G,n,n,cap,flow);
32 preflow_test.flowValue();
33 preflow_test.setSource(n);
34 preflow_test.setFlow(flow);
36 preflow_test.phase1(PType::NO_FLOW);
37 preflow_test.minCut(cut);
39 preflow_test.phase2();
40 preflow_test.setTarget(n);
41 preflow_test.setCap(cap);
42 preflow_test.minMinCut(cut);
43 preflow_test.maxMinCut(cut);
46 int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut,
47 SmartGraph::EdgeMap<int>& cap) {
50 for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) {
51 if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e];
58 typedef SmartGraph Graph;
60 typedef Graph::Node Node;
61 typedef Graph::NodeIt NodeIt;
62 typedef Graph::EdgeIt EdgeIt;
63 typedef Graph::EdgeMap<int> CapMap;
64 typedef Graph::EdgeMap<int> FlowMap;
65 typedef Graph::NodeMap<bool> CutMap;
67 typedef Preflow<Graph, int> PType;
69 std::ifstream file("preflow_graph");
74 readDimacs(file, G, cap, s, t);
78 PType preflow_test(G, s, t, cap, flow);
79 preflow_test.run(PType::ZERO_FLOW);
82 CutMap mincut(G,false);
83 preflow_test.minCut(mincut);
84 int min_cut_value=cut_value(G,mincut,cap);
86 CutMap minmincut(G,false);
87 preflow_test.minMinCut(minmincut);
88 int min_min_cut_value=cut_value(G,minmincut,cap);
90 CutMap maxmincut(G,false);
91 preflow_test.maxMinCut(maxmincut);
92 int max_min_cut_value=cut_value(G,maxmincut,cap);
94 check(preflow_test.flowValue() == min_cut_value &&
95 min_cut_value == min_min_cut_value &&
96 min_min_cut_value == max_min_cut_value,
97 "The max flow value is not equal to the three min cut values.");
99 int flow_value=preflow_test.flowValue();
102 for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e];
103 preflow_test.setCap(cap);
105 NodeIt tmp_node(G,t);
109 preflow_test.setTarget(t); //the max flow value remains 2*flow_value
110 //warning: ++t must be a valid node. In preflow_graph, it is.
112 preflow_test.phase1(PType::PRE_FLOW);
114 CutMap mincut1(G,false);
115 preflow_test.minCut(mincut1);
116 min_cut_value=cut_value(G,mincut1,cap);
118 check(preflow_test.flowValue() == min_cut_value &&
119 min_cut_value == 2*flow_value,
120 "The max flow value or the min cut value is wrong.");
122 preflow_test.phase2();
124 CutMap mincut2(G,false);
125 preflow_test.minCut(mincut2);
126 min_cut_value=cut_value(G,mincut2,cap);
128 CutMap minmincut2(G,false);
129 preflow_test.minMinCut(minmincut2);
130 min_min_cut_value=cut_value(G,minmincut2,cap);
133 preflow_test.maxMinCut(maxmincut);
135 max_min_cut_value=cut_value(G,maxmincut,cap);
137 check(preflow_test.flowValue() == min_cut_value &&
138 min_cut_value == min_min_cut_value &&
139 min_min_cut_value == max_min_cut_value &&
140 min_cut_value == 2*flow_value,
141 "The max flow value or the three min cut values were not doubled");
144 for( int i=1; i==1000; ++i ) {
149 preflow_test.setFlow(flow);
150 preflow_test.setSource(s);
154 CutMap mincut3(G,false);
155 preflow_test.minCut(mincut3);
156 min_cut_value=cut_value(G,mincut3,cap);
158 CutMap minmincut3(G,false);
159 preflow_test.minMinCut(minmincut3);
160 min_min_cut_value=cut_value(G,minmincut3,cap);
162 preflow_test.maxMinCut(maxmincut);
163 max_min_cut_value=cut_value(G,maxmincut,cap);
165 check(preflow_test.flowValue() == min_cut_value &&
166 min_cut_value == min_min_cut_value &&
167 min_min_cut_value == max_min_cut_value,
168 "The max flow value or the three min cut values are incorrect.");