COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/test/preflow_test.cc @ 874:2195bc090dfe

Last change on this file since 874:2195bc090dfe was 859:2570784896d8, checked in by Mihaly Barasz, 20 years ago

Forgotten std::
Apropos, why does it compile without it?

  • Property exe set to *
File size: 4.3 KB
Line 
1#include <fstream>
2#include <string>
3
4#include "test_tools.h"
5#include <hugo/smart_graph.h>
6#include <hugo/dimacs.h>
7#include <hugo/preflow.h>
8#include <hugo/skeletons/graph.h>
9#include <hugo/skeletons/maps.h>
10
11using namespace hugo;
12
13void check_Preflow()
14{
15  typedef int VType;
16  typedef skeleton::StaticGraphSkeleton Graph;
17
18  typedef Graph::Node Node;
19  typedef Graph::Edge Edge;
20  typedef skeleton::ReadMap<Edge,VType> CapMap;
21  typedef skeleton::ReadWriteMap<Edge,VType> FlowMap;
22  typedef skeleton::ReadWriteMap<Node,bool> CutMap;
23 
24  typedef Preflow<Graph, int, CapMap, FlowMap> PType;
25
26  Graph G;
27  Node n;
28  CapMap cap;
29  FlowMap flow;
30  CutMap cut;
31
32  PType preflow_test(G,n,n,cap,flow);
33
34  preflow_test.run();
35  preflow_test.flowValue();
36  preflow_test.setSource(n);
37  preflow_test.setFlow(flow);
38
39  preflow_test.phase1(PType::NO_FLOW);
40  preflow_test.minCut(cut);
41
42  preflow_test.phase2();
43  preflow_test.setTarget(n);
44  preflow_test.setCap(cap);
45  preflow_test.minMinCut(cut);
46  preflow_test.maxMinCut(cut);
47}
48
49int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut,
50                SmartGraph::EdgeMap<int>& cap) {
51 
52  int c=0;
53  for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) {
54    if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e];
55  }
56  return c;
57}
58
59int main() {
60
61  typedef SmartGraph Graph;
62 
63  typedef Graph::Node Node;
64  typedef Graph::NodeIt NodeIt;
65  typedef Graph::EdgeIt EdgeIt;
66  typedef Graph::EdgeMap<int> CapMap;
67  typedef Graph::EdgeMap<int> FlowMap;
68  typedef Graph::NodeMap<bool> CutMap;
69
70  typedef Preflow<Graph, int> PType;
71
72  std::string f_name;
73  if( getenv("srcdir") ) {
74    f_name = std::string(getenv("srcdir")) + "/preflow_graph.inp";
75  }
76  else {
77    f_name = "preflow_graph.inp";
78  }
79 
80  std::ifstream file(f_name.c_str());
81 
82  check(file, "Input file '" << f_name << "' not found.");
83 
84  Graph G;
85  Node s, t;
86  CapMap cap(G);
87  readDimacs(file, G, cap, s, t);
88
89  FlowMap flow(G,0);
90 
91  PType preflow_test(G, s, t, cap, flow);
92  preflow_test.run(PType::ZERO_FLOW);
93 
94   
95  CutMap mincut(G,false);
96  preflow_test.minCut(mincut);
97  int min_cut_value=cut_value(G,mincut,cap);
98   
99  CutMap minmincut(G,false);
100  preflow_test.minMinCut(minmincut);
101  int min_min_cut_value=cut_value(G,minmincut,cap);
102   
103  CutMap maxmincut(G,false);
104  preflow_test.maxMinCut(maxmincut);
105  int max_min_cut_value=cut_value(G,maxmincut,cap);
106
107  check(preflow_test.flowValue() == min_cut_value &&
108        min_cut_value == min_min_cut_value &&
109        min_min_cut_value == max_min_cut_value,
110        "The max flow value is not equal to the three min cut values.");
111
112  int flow_value=preflow_test.flowValue();
113
114
115  for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e];
116  preflow_test.setCap(cap); 
117
118  NodeIt tmp_node(G,t);
119  ++tmp_node;
120  t=tmp_node;
121 
122  preflow_test.setTarget(t); //the max flow value remains 2*flow_value
123  //warning: ++t must be a valid node. In preflow_graph, it is.
124
125  preflow_test.phase1(PType::PRE_FLOW);
126
127  CutMap mincut1(G,false);
128  preflow_test.minCut(mincut1);
129  min_cut_value=cut_value(G,mincut1,cap);
130   
131  check(preflow_test.flowValue() == min_cut_value &&
132        min_cut_value == 2*flow_value,
133        "The max flow value or the min cut value is wrong.");
134
135  preflow_test.phase2();
136
137  CutMap mincut2(G,false);
138  preflow_test.minCut(mincut2);
139  min_cut_value=cut_value(G,mincut2,cap);
140   
141  CutMap minmincut2(G,false);
142  preflow_test.minMinCut(minmincut2);
143  min_min_cut_value=cut_value(G,minmincut2,cap);
144
145 
146  preflow_test.maxMinCut(maxmincut);
147 
148  max_min_cut_value=cut_value(G,maxmincut,cap);
149
150  check(preflow_test.flowValue() == min_cut_value &&
151        min_cut_value == min_min_cut_value &&
152        min_min_cut_value == max_min_cut_value &&
153        min_cut_value == 2*flow_value,
154        "The max flow value or the three min cut values were not doubled");
155
156  EdgeIt e(G);
157  for( int i=1; i==1000; ++i ) {
158    flow[e]=0;
159    ++e;
160  }
161
162  preflow_test.setFlow(flow);
163  preflow_test.setSource(s);
164
165  preflow_test.run();
166
167  CutMap mincut3(G,false);
168  preflow_test.minCut(mincut3);
169  min_cut_value=cut_value(G,mincut3,cap);
170   
171  CutMap minmincut3(G,false);
172  preflow_test.minMinCut(minmincut3);
173  min_min_cut_value=cut_value(G,minmincut3,cap);
174   
175  preflow_test.maxMinCut(maxmincut);
176  max_min_cut_value=cut_value(G,maxmincut,cap);
177
178  check(preflow_test.flowValue() == min_cut_value &&
179        min_cut_value == min_min_cut_value &&
180        min_min_cut_value == max_min_cut_value,
181        "The max flow value or the three min cut values are incorrect.");
182}
Note: See TracBrowser for help on using the repository browser.