src/work/jacint/max_flow_test.cc
author alpar
Thu, 31 Mar 2005 13:30:27 +0000
changeset 1282 81e89e2b90d1
parent 921 818510fa3d99
permissions -rw-r--r--
length() returns int istead of size_t
jacint@588
     1
#include <iostream>
jacint@588
     2
alpar@921
     3
#include <lemon/list_graph.h>
alpar@921
     4
#include <lemon/dimacs.h>
jacint@588
     5
#include <max_flow.h>
jacint@715
     6
#include <max_flow_no_stack.h>
alpar@921
     7
#include <lemon/time_measure.h>
jacint@588
     8
alpar@921
     9
using namespace lemon;
jacint@588
    10
jacint@588
    11
int main(int, char **) {
jacint@588
    12
 
jacint@588
    13
  typedef ListGraph Graph;
jacint@588
    14
  
jacint@588
    15
  typedef Graph::Node Node;
jacint@588
    16
  typedef Graph::EdgeIt EdgeIt;
jacint@588
    17
jacint@588
    18
  Graph G;
jacint@588
    19
  Node s, t;
jacint@588
    20
  Graph::EdgeMap<int> cap(G);
jacint@588
    21
  readDimacs(std::cin, G, cap, s, t);
jacint@588
    22
  Timer ts;
jacint@588
    23
  
jacint@588
    24
  std::cout <<
jacint@588
    25
    "\n  Running max_flow.h on a graph with " << 
jacint@588
    26
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@588
    27
	   << std::endl<<std::endl;
jacint@588
    28
jacint@715
    29
  Graph::EdgeMap<int> flowstack(G,0);
jacint@715
    30
  MaxFlow<Graph, int> max_flow_test(G, s, t, cap, flowstack);
jacint@588
    31
  ts.reset();
jacint@588
    32
  max_flow_test.run();
jacint@715
    33
  std::cout << "Elapsed time of run() with stl stack: " << std::endl 
jacint@715
    34
	    <<ts << std::endl;
jacint@715
    35
jacint@715
    36
  Graph::EdgeMap<int> flow(G,0);
jacint@715
    37
  MaxFlowNoStack<Graph, int> max_flow_test_no_stack(G, s, t, cap, flow);
jacint@715
    38
  ts.reset();
jacint@715
    39
  max_flow_test_no_stack.run();
jacint@715
    40
  std::cout << "Elapsed time of run() without stack: " << std::endl 
jacint@588
    41
	    <<ts << std::endl;
jacint@588
    42
  
jacint@588
    43
  Graph::NodeMap<bool> mincut(G);
jacint@715
    44
  max_flow_test_no_stack.minMinCut(mincut); 
jacint@588
    45
  int min_min_cut_value=0;
jacint@588
    46
  EdgeIt e;
jacint@588
    47
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
    48
    if (mincut[G.source(e)] && !mincut[G.target(e)]) min_min_cut_value+=cap[e];
jacint@588
    49
  }
jacint@588
    50
jacint@588
    51
  Graph::NodeMap<bool> cut(G);
jacint@715
    52
  max_flow_test_no_stack.minCut(cut); 
jacint@588
    53
  int min_cut_value=0;
jacint@588
    54
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
    55
    if (cut[G.source(e)] && !cut[G.target(e)]) 
jacint@588
    56
      min_cut_value+=cap[e];
jacint@588
    57
  }
jacint@588
    58
jacint@588
    59
  Graph::NodeMap<bool> maxcut(G);
jacint@715
    60
  max_flow_test_no_stack.maxMinCut(maxcut); 
jacint@588
    61
  int max_min_cut_value=0;
jacint@588
    62
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
    63
    if (maxcut[G.source(e)] && !maxcut[G.target(e)]) 
jacint@588
    64
      max_min_cut_value+=cap[e];
jacint@588
    65
      }
jacint@588
    66
jacint@715
    67
  std::cout << "\n Checking the result without stack: " <<std::endl;  
jacint@715
    68
  std::cout << "Flow value: "<< max_flow_test_no_stack.flowValue() << std::endl;
jacint@588
    69
  std::cout << "Min cut value: "<< min_cut_value << std::endl;
jacint@588
    70
  std::cout << "Min min cut value: "<< min_min_cut_value << std::endl;
jacint@588
    71
  std::cout << "Max min cut value: "<< max_min_cut_value << 
jacint@588
    72
    std::endl;
jacint@588
    73
jacint@715
    74
  if ( max_flow_test_no_stack.flowValue() == min_cut_value &&
jacint@588
    75
       min_cut_value == min_min_cut_value &&
jacint@588
    76
       min_min_cut_value == max_min_cut_value )
jacint@588
    77
    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
jacint@588
    78
jacint@715
    79
  /*
jacint@588
    80
jacint@588
    81
  Graph::EdgeMap<int> flow2(G,0);
jacint@588
    82
  std::cout << "Calling resetFlow() " << std::endl 
jacint@588
    83
	    << ts << std::endl;
alpar@757
    84
  max_flow_test.setFlow(flow2);  
jacint@588
    85
  ts.reset();
jacint@588
    86
  max_flow_test.preflow(max_flow_test.PRE_FLOW);
jacint@588
    87
  std::cout << "Elapsed time of preflow(PRE_FLOW) starting from the zero flow: " << std::endl 
jacint@588
    88
	    << ts << std::endl;
jacint@588
    89
  
jacint@588
    90
  Graph::NodeMap<bool> mincut2(G);
jacint@588
    91
  max_flow_test.minMinCut(mincut2); 
jacint@588
    92
  int min_min_cut_value2=0;
jacint@588
    93
    for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
    94
    if (mincut2[G.source(e)] && !mincut2[G.target(e)]) min_min_cut_value2+=cap[e];
jacint@588
    95
  }
jacint@588
    96
jacint@588
    97
  Graph::NodeMap<bool> cut2(G);
jacint@588
    98
  max_flow_test.minCut(cut2); 
jacint@588
    99
  int min_cut_value2=0;
jacint@588
   100
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
   101
    if (cut2[G.source(e)] && !cut2[G.target(e)]) 
jacint@588
   102
      min_cut_value2+=cap[e];
jacint@588
   103
  }
jacint@588
   104
jacint@588
   105
  Graph::NodeMap<bool> maxcut2(G);
jacint@588
   106
  max_flow_test.maxMinCut(maxcut2); 
jacint@588
   107
  int max_min_cut_value2=0;
jacint@588
   108
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
   109
    if (maxcut2[G.source(e)] && !maxcut2[G.target(e)]) 
jacint@588
   110
      max_min_cut_value2+=cap[e];
jacint@588
   111
      }
jacint@588
   112
  
jacint@588
   113
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@588
   114
  std::cout << "Flow value: "<< max_flow_test.flowValue() << std::endl;
jacint@588
   115
  std::cout << "Min cut value: "<< min_cut_value2 << std::endl;
jacint@588
   116
  std::cout << "Min min cut value: "<< min_min_cut_value2 << std::endl;
jacint@588
   117
  std::cout << "Max min cut value: "<< max_min_cut_value2 << 
jacint@588
   118
    std::endl;  
jacint@588
   119
  if ( max_flow_test.flowValue() == min_cut_value &&
jacint@588
   120
       min_cut_value == min_min_cut_value &&
jacint@588
   121
       min_min_cut_value == max_min_cut_value )
jacint@588
   122
    std::cout << "They are equal! " <<std::endl;  
jacint@588
   123
jacint@588
   124
jacint@588
   125
  MaxFlow<Graph, int> max_flow_test3(G, s, t, cap, flow2);
jacint@588
   126
  max_flow_test3.run(max_flow_test3.GEN_FLOW);
jacint@588
   127
  std::cout << "Calling run(GEN_FLOW) from the max flow found before. " <<std::endl;  
jacint@588
   128
  
jacint@588
   129
  Graph::NodeMap<bool> mincut3(G);
jacint@588
   130
  max_flow_test3.minMinCut(mincut3); 
jacint@588
   131
  int min_min_cut_value3=0;
jacint@588
   132
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
   133
    if (mincut3[G.source(e)] && !mincut3[G.target(e)]) min_min_cut_value3+=cap[e];
jacint@588
   134
  }
jacint@588
   135
jacint@588
   136
  Graph::NodeMap<bool> cut3(G);
jacint@588
   137
  max_flow_test3.minCut(cut3); 
jacint@588
   138
  int min_cut_value3=0;
jacint@588
   139
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
   140
    if (cut3[G.source(e)] && !cut3[G.target(e)]) 
jacint@588
   141
      min_cut_value3+=cap[e];
jacint@588
   142
  }
jacint@588
   143
jacint@588
   144
  Graph::NodeMap<bool> maxcut3(G);
jacint@588
   145
  max_flow_test3.maxMinCut(maxcut3); 
jacint@588
   146
  int max_min_cut_value3=0;
jacint@588
   147
  for(G.first(e); G.valid(e); G.next(e)) {
alpar@986
   148
    if (maxcut3[G.source(e)] && !maxcut3[G.target(e)]) 
jacint@588
   149
      max_min_cut_value3+=cap[e];
jacint@588
   150
  }
jacint@588
   151
jacint@588
   152
  std::cout << "\n Checking the result: " <<std::endl;  
jacint@588
   153
  std::cout << "Flow value: "<< max_flow_test3.flowValue() << std::endl;
jacint@588
   154
  std::cout << "Min cut value: "<< min_cut_value3 << std::endl;
jacint@588
   155
  std::cout << "Min min cut value: "<< min_min_cut_value3 << std::endl;
jacint@588
   156
  std::cout << "Max min cut value: "<< max_min_cut_value3 << 
jacint@588
   157
    std::endl;
jacint@588
   158
jacint@588
   159
  if ( max_flow_test3.flowValue() == min_cut_value3 &&
jacint@588
   160
       min_cut_value3 == min_min_cut_value3 &&
jacint@588
   161
       min_min_cut_value3 == max_min_cut_value3 )
jacint@588
   162
    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
jacint@715
   163
  */
jacint@588
   164
  
jacint@588
   165
  return 0;
jacint@588
   166
}