src/work/jacint/preflow_res_comp.cc
author marci
Sat, 24 Apr 2004 14:25:03 +0000
changeset 392 b8d635e1672d
permissions -rw-r--r--
g++-4.0
jacint@388
     1
/*
jacint@388
     2
The only difference between preflow.h and preflow_res.h is that the latter
jacint@388
     3
uses the ResGraphWrapper, while the first does not. (Bfs is implemented by
jacint@388
     4
hand in both.) This test program runs Preflow and PreflowRes on the same
jacint@388
     5
graph, tests the result of these implementations and writes the running time
jacint@388
     6
of them.  */
jacint@388
     7
#include <iostream>
jacint@388
     8
jacint@388
     9
#include <smart_graph.h>
jacint@388
    10
#include <dimacs.h>
jacint@388
    11
#include <preflow.h>
jacint@388
    12
#include <preflow_res.h>
jacint@388
    13
#include <time_measure.h>
jacint@388
    14
jacint@388
    15
using namespace hugo;
jacint@388
    16
jacint@388
    17
int main(int, char **) {
jacint@388
    18
 
jacint@388
    19
  typedef SmartGraph Graph;
jacint@388
    20
  
jacint@388
    21
  typedef Graph::Node Node;
jacint@388
    22
  typedef Graph::EdgeIt EdgeIt;
jacint@388
    23
jacint@388
    24
  Graph G;
jacint@388
    25
  Node s, t;
jacint@388
    26
  Graph::EdgeMap<int> cap(G);
jacint@388
    27
  readDimacsMaxFlow(std::cin, G, s, t, cap);
jacint@388
    28
  Timer ts;
jacint@388
    29
  
jacint@388
    30
  std::cout <<
jacint@388
    31
    "\n  In which way are we faster: using ResGraphWrapper or not?"
jacint@388
    32
	    <<std::endl;
jacint@388
    33
  std::cout <<
jacint@388
    34
    "\n  Running preflow.h on a graph with " << 
jacint@388
    35
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@388
    36
	   << std::endl<<std::endl;
jacint@388
    37
jacint@388
    38
jacint@388
    39
  
jacint@388
    40
  Graph::EdgeMap<int> flow(G,0);
jacint@388
    41
  Preflow<Graph, int> max_flow_test(G, s, t, cap, flow, 1);
jacint@388
    42
  ts.reset();
jacint@388
    43
  max_flow_test.run();
jacint@388
    44
  std::cout << "Elapsed time NOT using the ResGraphWrapper: " << std::endl 
jacint@388
    45
	    <<ts << std::endl;
jacint@388
    46
  
jacint@388
    47
  Graph::NodeMap<bool> mincut(G);
jacint@388
    48
  max_flow_test.minMinCut(mincut); 
jacint@388
    49
  int min_min_cut_value=0;
jacint@388
    50
  EdgeIt e;
jacint@388
    51
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
    52
    if (mincut[G.tail(e)] && !mincut[G.head(e)]) min_min_cut_value+=cap[e];
jacint@388
    53
  }
jacint@388
    54
jacint@388
    55
  Graph::NodeMap<bool> cut(G);
jacint@388
    56
  max_flow_test.minCut(cut); 
jacint@388
    57
  int min_cut_value=0;
jacint@388
    58
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
    59
    if (cut[G.tail(e)] && !cut[G.head(e)]) 
jacint@388
    60
      min_cut_value+=cap[e];
jacint@388
    61
  }
jacint@388
    62
jacint@388
    63
  Graph::NodeMap<bool> maxcut(G);
jacint@388
    64
  max_flow_test.maxMinCut(maxcut); 
jacint@388
    65
  int max_min_cut_value=0;
jacint@388
    66
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
    67
    if (maxcut[G.tail(e)] && !maxcut[G.head(e)]) 
jacint@388
    68
      max_min_cut_value+=cap[e];
jacint@388
    69
      }
jacint@388
    70
jacint@388
    71
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@388
    72
  std::cout << "Flow value: "<< max_flow_test.flowValue() << std::endl;
jacint@388
    73
  std::cout << "Min cut value: "<< min_cut_value << std::endl;
jacint@388
    74
  std::cout << "Min min cut value: "<< min_min_cut_value << std::endl;
jacint@388
    75
  std::cout << "Max min cut value: "<< max_min_cut_value << 
jacint@388
    76
    std::endl;
jacint@388
    77
jacint@388
    78
  if ( max_flow_test.flowValue() == min_cut_value &&
jacint@388
    79
       min_cut_value == min_min_cut_value &&
jacint@388
    80
       min_min_cut_value == max_min_cut_value )
jacint@388
    81
    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
jacint@388
    82
  
jacint@388
    83
  Graph::EdgeMap<int> flow2(G,0);
jacint@388
    84
  PreflowRes<Graph, int> max_flow_test2(G, s, t, cap, flow2, 1);
jacint@388
    85
  ts.reset();
jacint@388
    86
  max_flow_test2.run();
jacint@388
    87
  std::cout << "Elapsed time using the ResGraphWrapper: " << std::endl 
jacint@388
    88
	    << ts << std::endl;
jacint@388
    89
  
jacint@388
    90
  Graph::NodeMap<bool> mincut2(G);
jacint@388
    91
  max_flow_test2.minMinCut(mincut2); 
jacint@388
    92
  int min_min_cut_value2=0;
jacint@388
    93
    for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
    94
    if (mincut2[G.tail(e)] && !mincut2[G.head(e)]) min_min_cut_value2+=cap[e];
jacint@388
    95
  }
jacint@388
    96
jacint@388
    97
  Graph::NodeMap<bool> cut2(G);
jacint@388
    98
  max_flow_test2.minCut(cut2); 
jacint@388
    99
  int min_cut_value2=0;
jacint@388
   100
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
   101
    if (cut2[G.tail(e)] && !cut2[G.head(e)]) 
jacint@388
   102
      min_cut_value2+=cap[e];
jacint@388
   103
  }
jacint@388
   104
jacint@388
   105
  Graph::NodeMap<bool> maxcut2(G);
jacint@388
   106
  max_flow_test2.maxMinCut(maxcut2); 
jacint@388
   107
  int max_min_cut_value2=0;
jacint@388
   108
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@388
   109
    if (maxcut2[G.tail(e)] && !maxcut2[G.head(e)]) 
jacint@388
   110
      max_min_cut_value2+=cap[e];
jacint@388
   111
      }
jacint@388
   112
  
jacint@388
   113
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@388
   114
  std::cout << "Flow value: "<< max_flow_test2.flowValue() << std::endl;
jacint@388
   115
  std::cout << "Min cut value: "<< min_cut_value2 << std::endl;
jacint@388
   116
  std::cout << "Min min cut value: "<< min_min_cut_value2 << std::endl;
jacint@388
   117
  std::cout << "Max min cut value: "<< max_min_cut_value2 << 
jacint@388
   118
    std::endl;  
jacint@388
   119
  if ( max_flow_test.flowValue() == min_cut_value &&
jacint@388
   120
       min_cut_value == min_min_cut_value &&
jacint@388
   121
       min_min_cut_value == max_min_cut_value )
jacint@388
   122
    std::cout << "They are equal! " <<std::endl<<"/n";  
jacint@388
   123
  
jacint@388
   124
  return 0;
jacint@388
   125
}