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