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