src/work/jacint/preflow.cc
author marci
Thu, 29 Apr 2004 15:58:34 +0000
changeset 472 052af4060f3e
parent 451 6b36be4cffa4
child 921 818510fa3d99
permissions -rw-r--r--
preflow, maxflow
jacint@451
     1
#include <iostream>
jacint@451
     2
jacint@451
     3
#include <smart_graph.h>
jacint@451
     4
#include <dimacs.h>
jacint@451
     5
#include <preflow.h>
jacint@451
     6
#include <time_measure.h>
jacint@451
     7
jacint@451
     8
using namespace hugo;
jacint@451
     9
jacint@451
    10
int main(int, char **) {
jacint@451
    11
 
jacint@451
    12
  typedef SmartGraph Graph;
jacint@451
    13
  
jacint@451
    14
  typedef Graph::Node Node;
jacint@451
    15
  typedef Graph::EdgeIt EdgeIt;
jacint@451
    16
jacint@451
    17
  Graph G;
jacint@451
    18
  Node s, t;
jacint@451
    19
  Graph::EdgeMap<int> cap(G);
jacint@451
    20
  readDimacsMaxFlow(std::cin, G, s, t, cap);
jacint@451
    21
  Timer ts;
jacint@470
    22
  bool error=false;
jacint@451
    23
  
jacint@451
    24
  std::cout <<
jacint@451
    25
    "\n  Testing preflow.h on a graph with " << 
jacint@451
    26
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@451
    27
	   << std::endl;
jacint@451
    28
jacint@451
    29
jacint@451
    30
  Graph::EdgeMap<int> flow(G,0);
jacint@451
    31
  Preflow<Graph, int> preflow_test(G, s, t, cap, flow);
jacint@451
    32
  std::cout << "\nCalling run() (flow must be constant zero)..."<<std::endl;
jacint@451
    33
  ts.reset();
jacint@451
    34
  preflow_test.run();
jacint@451
    35
  std::cout << "Elapsed time: " << ts << std::endl;
jacint@451
    36
jacint@451
    37
  Graph::NodeMap<bool> mincut(G);
jacint@451
    38
  preflow_test.minMinCut(mincut); 
jacint@451
    39
  int min_min_cut_value=0;
jacint@451
    40
  Graph::NodeMap<bool> cut(G);
jacint@451
    41
  preflow_test.minCut(cut); 
jacint@451
    42
  int min_cut_value=0;
jacint@451
    43
  Graph::NodeMap<bool> maxcut(G);
jacint@451
    44
  preflow_test.maxMinCut(maxcut); 
jacint@451
    45
  int max_min_cut_value=0;
jacint@451
    46
  EdgeIt e;
jacint@451
    47
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@451
    48
    int c=cap[e];
jacint@451
    49
    if (mincut[G.tail(e)] && !mincut[G.head(e)]) min_min_cut_value+=c;
jacint@451
    50
    if (cut[G.tail(e)] && !cut[G.head(e)]) min_cut_value+=c; 
jacint@451
    51
    if (maxcut[G.tail(e)] && !maxcut[G.head(e)]) max_min_cut_value+=c;
jacint@451
    52
  }
jacint@451
    53
jacint@451
    54
  std::cout << "\nChecking the result: " <<std::endl;  
jacint@451
    55
  std::cout << "Flow value: "<< preflow_test.flowValue() << std::endl;
jacint@451
    56
  std::cout << "Min cut value: "<< min_cut_value << std::endl;
jacint@451
    57
  std::cout << "Min min cut value: "<< min_min_cut_value << std::endl;
jacint@451
    58
  std::cout << "Max min cut value: "<< max_min_cut_value << 
jacint@451
    59
    std::endl;
jacint@451
    60
jacint@451
    61
  if ( preflow_test.flowValue() == min_cut_value &&
jacint@451
    62
       min_cut_value == min_min_cut_value &&
jacint@451
    63
       min_min_cut_value == max_min_cut_value )
jacint@451
    64
    std::cout << "They are equal. " <<std::endl;  
jacint@470
    65
  else {
jacint@470
    66
    std::cout << "ERROR! They are not equal! " <<std::endl;  
jacint@470
    67
    error=true;
jacint@470
    68
  }
jacint@451
    69
jacint@451
    70
jacint@451
    71
jacint@451
    72
  Preflow<Graph, int> preflow_test2(G, s, t, cap, flow);
jacint@451
    73
  std::cout << "\n\nCalling preflow(GEN_FLOW) with the given maximum flow..."<<std::endl;
jacint@451
    74
  ts.reset();
jacint@451
    75
  preflow_test2.preflow(preflow_test2.GEN_FLOW);
jacint@451
    76
  std::cout << "Elapsed time: " << ts << std::endl;
jacint@451
    77
jacint@451
    78
  Graph::NodeMap<bool> mincut2(G);
jacint@451
    79
  preflow_test.minMinCut(mincut2); 
jacint@451
    80
  int min_min_cut2_value=0;
jacint@451
    81
  Graph::NodeMap<bool> cut2(G);
jacint@451
    82
  preflow_test.minCut(cut2); 
jacint@451
    83
  int min_cut2_value=0;
jacint@451
    84
  Graph::NodeMap<bool> maxcut2(G);
jacint@451
    85
  preflow_test.maxMinCut(maxcut2); 
jacint@451
    86
  int max_min_cut2_value=0;
jacint@451
    87
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@451
    88
    int c=cap[e];
jacint@451
    89
    if (mincut2[G.tail(e)] && !mincut2[G.head(e)]) min_min_cut2_value+=c;
jacint@451
    90
    if (cut2[G.tail(e)] && !cut2[G.head(e)]) min_cut2_value+=c; 
jacint@451
    91
    if (maxcut2[G.tail(e)] && !maxcut2[G.head(e)]) max_min_cut2_value+=c;
jacint@451
    92
  }
jacint@451
    93
jacint@451
    94
  std::cout << "\nThe given flow value is "
jacint@451
    95
	    << preflow_test2.flowValue();
jacint@451
    96
jacint@451
    97
  if ( preflow_test2.flowValue() == min_cut2_value &&
jacint@451
    98
       min_cut2_value == min_min_cut2_value &&
jacint@451
    99
       min_min_cut2_value == max_min_cut2_value )
jacint@451
   100
    std::cout <<", which is equal to all three min cut values." 
jacint@451
   101
	      <<std::endl;  
jacint@470
   102
  else {
jacint@470
   103
    std::cout << "ERROR! It is not equal to all three min cut values! " 
jacint@470
   104
	      <<std::endl;  
jacint@470
   105
    error=true;
jacint@470
   106
  }
jacint@470
   107
  
jacint@451
   108
jacint@451
   109
jacint@451
   110
jacint@451
   111
  Graph::EdgeMap<int> flow3(G,0);
jacint@451
   112
  Preflow<Graph, int> preflow_test3(G, s, t, cap, flow3);
jacint@451
   113
  std::cout << "\n\nCalling preflowPhase0(PREFLOW) on the constant zero flow..."<<std::endl;
jacint@451
   114
  ts.reset();
jacint@451
   115
  preflow_test3.preflowPhase0(preflow_test3.PREFLOW);
jacint@451
   116
  std::cout << "Elapsed time: " << ts << std::endl;
jacint@451
   117
  Graph::NodeMap<bool> actcut3(G);
jacint@451
   118
  std::cout << "\nCalling actMinCut()..."<<std::endl;
jacint@451
   119
  preflow_test3.actMinCut(actcut3); 
jacint@451
   120
  std::cout << "Calling preflowPhase1() on the given flow..."<<std::endl;
jacint@451
   121
  ts.reset();
jacint@451
   122
  preflow_test3.preflowPhase1();
jacint@451
   123
  std::cout << "Elapsed time: " << ts << std::endl;
jacint@451
   124
  
jacint@451
   125
  int act_min_cut3_value=0;
jacint@451
   126
  
jacint@451
   127
  Graph::NodeMap<bool> mincut3(G);
jacint@451
   128
  preflow_test.minMinCut(mincut3); 
jacint@451
   129
  int min_min_cut3_value=0;
jacint@451
   130
  
jacint@451
   131
  Graph::NodeMap<bool> cut3(G);
jacint@451
   132
  preflow_test.minCut(cut3); 
jacint@451
   133
  int min_cut3_value=0;
jacint@451
   134
  
jacint@451
   135
  Graph::NodeMap<bool> maxcut3(G);
jacint@451
   136
  preflow_test.maxMinCut(maxcut3); 
jacint@451
   137
  int max_min_cut3_value=0;
jacint@451
   138
  
jacint@451
   139
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@451
   140
    int c=cap[e];
jacint@451
   141
    if (mincut3[G.tail(e)] && !mincut3[G.head(e)]) min_min_cut3_value+=c;
jacint@451
   142
    if (cut3[G.tail(e)] && !cut3[G.head(e)]) min_cut3_value+=c; 
jacint@451
   143
    if (maxcut3[G.tail(e)] && !maxcut3[G.head(e)]) max_min_cut3_value+=c;
jacint@451
   144
    if (actcut3[G.tail(e)] && !actcut3[G.head(e)]) act_min_cut3_value+=c;
jacint@451
   145
  }
jacint@451
   146
jacint@451
   147
 std::cout << "\nThe min cut value given by actMinCut() after phase 0 is "<<
jacint@451
   148
   act_min_cut3_value;
jacint@451
   149
jacint@451
   150
  if ( preflow_test3.flowValue() == min_cut3_value &&
jacint@451
   151
       min_cut3_value == min_min_cut3_value &&
jacint@451
   152
       min_min_cut3_value == max_min_cut3_value &&
jacint@451
   153
       max_min_cut3_value == act_min_cut3_value ) {
jacint@451
   154
    std::cout << 
jacint@451
   155
      ", which is equal to the given flow value and to all three min cut values after phase 1." 
jacint@451
   156
	      <<std::endl;  
jacint@451
   157
  }
jacint@470
   158
  else {
jacint@470
   159
    std::cout << 
jacint@470
   160
      "ERROR! It is not equal to the given flow value and to all three min cut values after phase 1! " 
jacint@470
   161
	      <<std::endl;  
jacint@470
   162
    error=true;
jacint@470
   163
  }
jacint@470
   164
  
jacint@451
   165
jacint@451
   166
jacint@451
   167
jacint@451
   168
jacint@451
   169
  Graph::EdgeMap<int> flow4(G,0);
jacint@451
   170
  Preflow<Graph, int> preflow_test4(G, s, t, cap, flow4);
jacint@451
   171
  std::cout << 
jacint@451
   172
    "\n\nCalling preflow(PREFLOW) with the constant 0 flow, the result is f..."
jacint@451
   173
	    <<std::endl;
jacint@451
   174
  preflow_test4.preflow(preflow_test4.PREFLOW);
jacint@451
   175
jacint@451
   176
  std::cout << "Swapping the source and the target, "<<std::endl;
jacint@451
   177
  std::cout << "by calling resetSource(t) and resetTarget(s)..."
jacint@451
   178
	    <<std::endl;
jacint@451
   179
  preflow_test4.resetSource(t);
jacint@451
   180
  preflow_test4.resetTarget(s);
jacint@451
   181
jacint@451
   182
  std::cout << 
jacint@451
   183
    "Calling preflow(PREFLOW) to find a maximum t-s flow starting with flow f..."
jacint@451
   184
	    <<std::endl;
jacint@451
   185
  preflow_test4.preflow(preflow_test4.PREFLOW);
jacint@451
   186
jacint@451
   187
  Graph::NodeMap<bool> mincut4(G);
jacint@451
   188
  preflow_test4.minMinCut(mincut4); 
jacint@451
   189
  int min_min_cut4_value=0;
jacint@451
   190
  Graph::NodeMap<bool> cut4(G);
jacint@451
   191
  preflow_test4.minCut(cut4); 
jacint@451
   192
  int min_cut4_value=0;
jacint@451
   193
  Graph::NodeMap<bool> maxcut4(G);
jacint@451
   194
  preflow_test4.maxMinCut(maxcut4); 
jacint@451
   195
  int max_min_cut4_value=0;
jacint@451
   196
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@451
   197
    int c=cap[e];
jacint@451
   198
    if (mincut4[G.tail(e)] && !mincut4[G.head(e)]) min_min_cut4_value+=c;
jacint@451
   199
    if (cut4[G.tail(e)] && !cut4[G.head(e)]) min_cut4_value+=c; 
jacint@451
   200
    if (maxcut4[G.tail(e)] && !maxcut4[G.head(e)]) max_min_cut4_value+=c;
jacint@451
   201
  }
jacint@451
   202
jacint@451
   203
  std::cout << "\nThe given flow value is "
jacint@451
   204
	    << preflow_test4.flowValue();
jacint@451
   205
  
jacint@451
   206
  if ( preflow_test4.flowValue() == min_cut4_value &&
jacint@451
   207
       min_cut4_value == min_min_cut4_value &&
jacint@451
   208
       min_min_cut4_value == max_min_cut4_value )
jacint@451
   209
    std::cout <<", which is equal to all three min cut values." 
jacint@451
   210
	      <<std::endl;  
jacint@470
   211
  else {
jacint@470
   212
    std::cout << "ERROR! It is not equal to all three min cut values! " 
jacint@470
   213
	      <<std::endl;  
jacint@470
   214
    error=true;
jacint@470
   215
  }
jacint@451
   216
jacint@451
   217
jacint@451
   218
jacint@451
   219
jacint@451
   220
  Graph::EdgeMap<int> flow5(G,0);
jacint@451
   221
  std::cout << "Resetting the stored flow to constant zero, by calling resetFlow..."
jacint@451
   222
	    <<std::endl;
jacint@451
   223
  preflow_test4.resetFlow(flow5);
jacint@451
   224
  std::cout << 
jacint@451
   225
    "Calling preflow(GEN_FLOW) to find a maximum t-s flow "<<std::endl;
jacint@451
   226
  std::cout << 
jacint@451
   227
    "starting with this constant zero flow..." <<std::endl;
jacint@451
   228
  preflow_test4.preflow(preflow_test4.GEN_FLOW);
jacint@451
   229
jacint@451
   230
  Graph::NodeMap<bool> mincut5(G);
jacint@451
   231
  preflow_test4.minMinCut(mincut5); 
jacint@451
   232
  int min_min_cut5_value=0;
jacint@451
   233
  Graph::NodeMap<bool> cut5(G);
jacint@451
   234
  preflow_test4.minCut(cut5); 
jacint@451
   235
  int min_cut5_value=0;
jacint@451
   236
  Graph::NodeMap<bool> maxcut5(G);
jacint@451
   237
  preflow_test4.maxMinCut(maxcut5); 
jacint@451
   238
  int max_min_cut5_value=0;
jacint@451
   239
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@451
   240
    int c=cap[e];
jacint@451
   241
    if (mincut5[G.tail(e)] && !mincut5[G.head(e)]) min_min_cut5_value+=c;
jacint@451
   242
    if (cut5[G.tail(e)] && !cut5[G.head(e)]) min_cut5_value+=c; 
jacint@451
   243
    if (maxcut5[G.tail(e)] && !maxcut5[G.head(e)]) max_min_cut5_value+=c;
jacint@451
   244
  }
jacint@451
   245
jacint@451
   246
  std::cout << "\nThe given flow value is "
jacint@451
   247
	    << preflow_test4.flowValue();
jacint@451
   248
  
jacint@451
   249
  if ( preflow_test4.flowValue() == min_cut5_value &&
jacint@451
   250
       min_cut5_value == min_min_cut5_value &&
jacint@451
   251
       min_min_cut5_value == max_min_cut5_value )
jacint@451
   252
    std::cout <<", which is equal to all three min cut values." 
jacint@451
   253
	      <<std::endl<<std::endl;  
jacint@470
   254
  else {
jacint@470
   255
    std::cout << "ERROR! It is not equal to all three min cut values! " 
jacint@470
   256
	      <<std::endl;  
jacint@470
   257
    error=true;
jacint@470
   258
  }
jacint@470
   259
  
jacint@470
   260
  if (error) std::cout <<"\nThere was some error in the results!\n"<<std::endl; 
jacint@470
   261
  else std::cout <<"\nThere was no error in the results.\n"<<std::endl; 
jacint@451
   262
jacint@451
   263
  return 0;
jacint@451
   264
}