src/work/jacint/preflow_excess_test.cc
author alpar
Mon, 19 Jul 2004 13:31:47 +0000
changeset 708 429dfcbbf47d
child 921 818510fa3d99
permissions -rw-r--r--
A new benchmark (hcube)
and other minor changes
jacint@437
     1
/*
jacint@437
     2
The only difference between preflow.h and preflow_res.h is that the latter
jacint@437
     3
uses the ResGraphWrapper, while the first does not. (Bfs is implemented by
jacint@437
     4
hand in both.) This test program runs Preflow and PreflowRes on the same
jacint@437
     5
graph, tests the result of these implementations and writes the running time
jacint@437
     6
of them.  */
jacint@437
     7
#include <iostream>
jacint@437
     8
jacint@437
     9
#include <smart_graph.h>
jacint@437
    10
#include <dimacs.h>
jacint@437
    11
#include <preflow_excess.h>
jacint@437
    12
#include <time_measure.h>
jacint@437
    13
jacint@437
    14
using namespace hugo;
jacint@437
    15
jacint@437
    16
int main(int, char **) {
jacint@437
    17
 
jacint@437
    18
  typedef SmartGraph Graph;
jacint@437
    19
  
jacint@437
    20
  typedef Graph::Node Node;
jacint@437
    21
  typedef Graph::EdgeIt EdgeIt;
jacint@437
    22
jacint@437
    23
  Graph G;
jacint@437
    24
  Node s, t;
jacint@437
    25
  Graph::EdgeMap<int> cap(G);
jacint@437
    26
  readDimacsMaxFlow(std::cin, G, s, t, cap);
jacint@437
    27
  Timer ts;
jacint@437
    28
  
jacint@437
    29
  std::cout <<
jacint@437
    30
    "\n  Are we slower?"
jacint@437
    31
	    <<std::endl;
jacint@437
    32
  std::cout <<
jacint@437
    33
    "\n  Running preflow.h on a graph with " << 
jacint@437
    34
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@437
    35
	   << std::endl<<std::endl;
jacint@437
    36
jacint@437
    37
jacint@437
    38
jacint@437
    39
jacint@437
    40
jacint@437
    41
jacint@437
    42
  
jacint@437
    43
  Graph::EdgeMap<int> flow(G,0);
jacint@437
    44
  Preflow<Graph, int> max_flow_test(G, s, t, cap, flow, 0 , 0);
jacint@437
    45
  ts.reset();
jacint@437
    46
  max_flow_test.run();
jacint@437
    47
  std::cout << "Elapsed time from a preflow: " << std::endl 
jacint@437
    48
	    <<ts << std::endl;
jacint@437
    49
  
jacint@437
    50
  Graph::NodeMap<bool> mincut(G);
jacint@437
    51
  max_flow_test.minMinCut(mincut); 
jacint@437
    52
  int min_min_cut_value=0;
jacint@437
    53
  EdgeIt e;
jacint@437
    54
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
    55
    if (mincut[G.tail(e)] && !mincut[G.head(e)]) min_min_cut_value+=cap[e];
jacint@437
    56
  }
jacint@437
    57
jacint@437
    58
  Graph::NodeMap<bool> cut(G);
jacint@437
    59
  max_flow_test.minCut(cut); 
jacint@437
    60
  int min_cut_value=0;
jacint@437
    61
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
    62
    if (cut[G.tail(e)] && !cut[G.head(e)]) 
jacint@437
    63
      min_cut_value+=cap[e];
jacint@437
    64
  }
jacint@437
    65
jacint@437
    66
  Graph::NodeMap<bool> maxcut(G);
jacint@437
    67
  max_flow_test.maxMinCut(maxcut); 
jacint@437
    68
  int max_min_cut_value=0;
jacint@437
    69
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
    70
    if (maxcut[G.tail(e)] && !maxcut[G.head(e)]) 
jacint@437
    71
      max_min_cut_value+=cap[e];
jacint@437
    72
      }
jacint@437
    73
jacint@437
    74
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@437
    75
  std::cout << "Flow value: "<< max_flow_test.flowValue() << std::endl;
jacint@437
    76
  std::cout << "Min cut value: "<< min_cut_value << std::endl;
jacint@437
    77
  std::cout << "Min min cut value: "<< min_min_cut_value << std::endl;
jacint@437
    78
  std::cout << "Max min cut value: "<< max_min_cut_value << 
jacint@437
    79
    std::endl;
jacint@437
    80
jacint@437
    81
  if ( max_flow_test.flowValue() == min_cut_value &&
jacint@437
    82
       min_cut_value == min_min_cut_value &&
jacint@437
    83
       min_min_cut_value == max_min_cut_value )
jacint@437
    84
    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
jacint@437
    85
jacint@437
    86
jacint@437
    87
jacint@437
    88
jacint@437
    89
jacint@437
    90
  
jacint@437
    91
  Graph::EdgeMap<int> flow2(G,0);
jacint@437
    92
  Preflow<Graph, int> max_flow_test2(G, s, t, cap, flow2, 0 , 1);
jacint@437
    93
  ts.reset();
jacint@437
    94
  max_flow_test2.run();
jacint@437
    95
  std::cout << "Elapsed time from a flow: " << std::endl 
jacint@437
    96
	    << ts << std::endl;
jacint@437
    97
  
jacint@437
    98
  Graph::NodeMap<bool> mincut2(G);
jacint@437
    99
  max_flow_test2.minMinCut(mincut2); 
jacint@437
   100
  int min_min_cut_value2=0;
jacint@437
   101
    for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   102
    if (mincut2[G.tail(e)] && !mincut2[G.head(e)]) min_min_cut_value2+=cap[e];
jacint@437
   103
  }
jacint@437
   104
jacint@437
   105
  Graph::NodeMap<bool> cut2(G);
jacint@437
   106
  max_flow_test2.minCut(cut2); 
jacint@437
   107
  int min_cut_value2=0;
jacint@437
   108
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   109
    if (cut2[G.tail(e)] && !cut2[G.head(e)]) 
jacint@437
   110
      min_cut_value2+=cap[e];
jacint@437
   111
  }
jacint@437
   112
jacint@437
   113
  Graph::NodeMap<bool> maxcut2(G);
jacint@437
   114
  max_flow_test2.maxMinCut(maxcut2); 
jacint@437
   115
  int max_min_cut_value2=0;
jacint@437
   116
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   117
    if (maxcut2[G.tail(e)] && !maxcut2[G.head(e)]) 
jacint@437
   118
      max_min_cut_value2+=cap[e];
jacint@437
   119
      }
jacint@437
   120
  
jacint@437
   121
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@437
   122
  std::cout << "Flow value: "<< max_flow_test2.flowValue() << std::endl;
jacint@437
   123
  std::cout << "Min cut value: "<< min_cut_value2 << std::endl;
jacint@437
   124
  std::cout << "Min min cut value: "<< min_min_cut_value2 << std::endl;
jacint@437
   125
  std::cout << "Max min cut value: "<< max_min_cut_value2 << 
jacint@437
   126
    std::endl;  
jacint@437
   127
  if ( max_flow_test.flowValue() == min_cut_value &&
jacint@437
   128
       min_cut_value == min_min_cut_value &&
jacint@437
   129
       min_min_cut_value == max_min_cut_value )
jacint@437
   130
    std::cout << "They are equal! " <<std::endl;  
jacint@437
   131
jacint@437
   132
jacint@437
   133
jacint@437
   134
jacint@437
   135
jacint@437
   136
  Graph::EdgeMap<int> flow3(G,0);
jacint@437
   137
  Preflow<Graph, int> max_flow_test3(G, s, t, cap, flow3, 1 , 1);
jacint@437
   138
  ts.reset();
jacint@437
   139
  max_flow_test3.run();
jacint@437
   140
  std::cout << "Elapsed time from a const zero flow: " << std::endl 
jacint@437
   141
	    <<ts << std::endl;
jacint@437
   142
  
jacint@437
   143
  Graph::NodeMap<bool> mincut3(G);
jacint@437
   144
  max_flow_test3.minMinCut(mincut3); 
jacint@437
   145
  int min_min_cut_value3=0;
jacint@437
   146
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   147
    if (mincut3[G.tail(e)] && !mincut3[G.head(e)]) min_min_cut_value3+=cap[e];
jacint@437
   148
  }
jacint@437
   149
jacint@437
   150
  Graph::NodeMap<bool> cut3(G);
jacint@437
   151
  max_flow_test3.minCut(cut3); 
jacint@437
   152
  int min_cut_value3=0;
jacint@437
   153
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   154
    if (cut3[G.tail(e)] && !cut3[G.head(e)]) 
jacint@437
   155
      min_cut_value3+=cap[e];
jacint@437
   156
  }
jacint@437
   157
jacint@437
   158
  Graph::NodeMap<bool> maxcut3(G);
jacint@437
   159
  max_flow_test3.maxMinCut(maxcut3); 
jacint@437
   160
  int max_min_cut_value3=0;
jacint@437
   161
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@437
   162
    if (maxcut3[G.tail(e)] && !maxcut3[G.head(e)]) 
jacint@437
   163
      max_min_cut_value3+=cap[e];
jacint@437
   164
      }
jacint@437
   165
jacint@437
   166
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@437
   167
  std::cout << "Flow value: "<< max_flow_test3.flowValue() << std::endl;
jacint@437
   168
  std::cout << "Min cut value: "<< min_cut_value3 << std::endl;
jacint@437
   169
  std::cout << "Min min cut value: "<< min_min_cut_value3 << std::endl;
jacint@437
   170
  std::cout << "Max min cut value: "<< max_min_cut_value3 << 
jacint@437
   171
    std::endl;
jacint@437
   172
jacint@437
   173
  if ( max_flow_test3.flowValue() == min_cut_value3 &&
jacint@437
   174
       min_cut_value3 == min_min_cut_value3 &&
jacint@437
   175
       min_min_cut_value3 == max_min_cut_value3 )
jacint@437
   176
    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
jacint@437
   177
  
jacint@437
   178
  return 0;
jacint@437
   179
}