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