src/work/marci/preflow_bug.cc
author alpar
Wed, 16 Feb 2005 16:17:30 +0000
changeset 1151 b217fc69f913
parent 921 818510fa3d99
permissions -rw-r--r--
Several changes in the docs.
marci@747
     1
// -*- c++ -*-
marci@747
     2
#include <iostream>
marci@747
     3
#include <fstream>
marci@747
     4
marci@747
     5
#include <sage_graph.h>
alpar@921
     6
#include <lemon/smart_graph.h>
alpar@921
     7
#include <lemon/dimacs.h>
alpar@921
     8
//#include <lemon/time_measure.h>
marci@747
     9
//#include <graph_wrapper.h>
alpar@921
    10
#include <lemon/max_flow.h>
marci@747
    11
//#include <preflow_res.h>
marci@747
    12
#include <for_each_macros.h>
marci@747
    13
#include <graph_concept.h>
marci@747
    14
marci@747
    15
using std::cout;
marci@747
    16
using std::endl;
marci@747
    17
alpar@921
    18
using namespace lemon;
marci@747
    19
marci@747
    20
// Use a DIMACS min cost flow file as stdin.
marci@747
    21
// read_dimacs_demo < dimacs_max_flow_file
marci@747
    22
marci@747
    23
int main(int, char **) {
marci@747
    24
marci@747
    25
  //typedef SageGraph MutableGraph;
marci@747
    26
  //typedef FullFeatureGraphConcept Graph;
marci@747
    27
  //typedef SmartGraph Graph;
marci@747
    28
  typedef SageGraph Graph;
marci@747
    29
  typedef Graph::Node Node;
marci@747
    30
  typedef Graph::EdgeIt EdgeIt;
marci@747
    31
marci@747
    32
  Graph g;
marci@747
    33
marci@747
    34
  Node s, t;
marci@747
    35
  Graph::EdgeMap<int> cap(g);
marci@747
    36
  Graph::EdgeMap<int> flow(g); //0 flow
marci@747
    37
  //readDimacs(std::cin, g, cap, s, t);
marci@747
    38
  readDimacs(std::cin, g, cap, s, t, flow);
marci@747
    39
//  Timer ts;
marci@747
    40
  MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@747
    41
    max_flow_test(g, s, t, cap, flow);
marci@747
    42
  
marci@747
    43
  Graph::NodeMap<bool> cut(g);
marci@747
    44
marci@747
    45
  {
marci@747
    46
    Graph::EdgeIt e;
marci@747
    47
    for (g.first(e); g.valid(e); g.next(e))
alpar@986
    48
      cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) << " cap: " << cap[e] << " preflow: " << flow[e] << endl;
marci@747
    49
  }
marci@747
    50
  {
marci@747
    51
    Graph::NodeIt n;
marci@747
    52
    for (g.first(n); g.valid(n); g.next(n)) {
marci@747
    53
      int a=0;
marci@747
    54
      {
marci@747
    55
	Graph::InEdgeIt e;
marci@747
    56
	for (g.first(e, n); g.valid(e); g.next(e)) a+=flow[e];
marci@747
    57
      }
marci@747
    58
      {
marci@747
    59
	Graph::OutEdgeIt e;
marci@747
    60
	for (g.first(e, n); g.valid(e); g.next(e)) a-=flow[e];
marci@747
    61
      }
marci@747
    62
      cout << 1+g.id(n) << " excess: " << a << endl;
marci@747
    63
    }
marci@747
    64
  }
marci@747
    65
marci@747
    66
marci@747
    67
  {
marci@747
    68
//    std::cout << "preflow ..." << std::endl;
marci@747
    69
//    ts.reset();
marci@747
    70
    max_flow_test.preflowPhase1(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::PRE_FLOW);
marci@747
    71
//    std::cout << "elapsed time: " << ts << std::endl;
marci@747
    72
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747
    73
    std::cout << "flow value 2: "<< max_flow_test.flowValue2() << std::endl;
marci@747
    74
    max_flow_test.actMinCut(cut);
marci@747
    75
    
marci@747
    76
    Graph::EdgeIt e;
marci@747
    77
    for (g.first(e); g.valid(e); g.next(e)) {
alpar@986
    78
      if (cut[g.source(e)] && !cut[g.target(e)]) {
alpar@986
    79
	cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) 
marci@747
    80
	     << "(forward edge) flow: " << flow[e] 
marci@747
    81
	     << " cap: " << cap[e]<< endl;
marci@747
    82
	if (flow[e]!=cap[e]) 
marci@747
    83
	std::cout << "Slackness does not hold!" << std::endl;
marci@747
    84
      }
alpar@986
    85
      if (!cut[g.source(e)] && cut[g.target(e)]) {
alpar@986
    86
	cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) 
marci@747
    87
	     << "(backward edge) flow: " << flow[e] << endl;
marci@747
    88
	if (flow[e]!=0) 
marci@747
    89
	std::cout << "Slackness does not hold!" << std::endl;
marci@747
    90
      }
marci@747
    91
    }
marci@747
    92
    Graph::NodeIt n;
marci@747
    93
    for (g.first(n); g.valid(n); g.next(n)) {
marci@747
    94
      if (cut[n]) 
marci@747
    95
	cout << "in cut: " << 1+g.id(n) << endl;
marci@747
    96
    }
marci@747
    97
  }
marci@747
    98
marci@747
    99
//   {
marci@747
   100
//     std::cout << "preflow ..." << std::endl;
marci@747
   101
//     ts.reset();
marci@747
   102
//     max_flow_test.run();
marci@747
   103
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   104
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747
   105
//     max_flow_test.actMinCut(cut);
marci@747
   106
marci@747
   107
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   108
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   109
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   110
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   111
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   112
//     }
marci@747
   113
//   }
marci@747
   114
marci@747
   115
//   {
marci@747
   116
//     std::cout << "preflow ..." << std::endl;
marci@747
   117
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   118
//     ts.reset();
marci@747
   119
//     max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@747
   120
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   121
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747
   122
marci@747
   123
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   124
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   125
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   126
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   127
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   128
//     }
marci@747
   129
//   }
marci@747
   130
marci@747
   131
// //   {
marci@747
   132
// //     std::cout << "wrapped preflow ..." << std::endl;
marci@747
   133
// //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   134
// //     ts.reset();
marci@747
   135
// //     pre_flow_res.run();
marci@747
   136
// //     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   137
// //     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@747
   138
// //   }
marci@747
   139
marci@747
   140
//   {
marci@747
   141
//     std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@747
   142
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   143
//     ts.reset();
marci@747
   144
//     int i=0;
marci@747
   145
//     while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@747
   146
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   147
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747
   148
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747
   149
marci@747
   150
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   151
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   152
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   153
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   154
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   155
//     }
marci@747
   156
//   }
marci@747
   157
marci@747
   158
// //   {
marci@747
   159
// //     std::cout << "faster physical blocking flow augmentation ..." << std::endl;
marci@747
   160
// //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   161
// //     ts.reset();
marci@747
   162
// //     int i=0;
marci@747
   163
// //     while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
marci@747
   164
// //     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   165
// //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747
   166
// //     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747
   167
// //   }
marci@747
   168
marci@747
   169
//   {
marci@747
   170
//     std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@747
   171
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   172
//     ts.reset();
marci@747
   173
//     int i=0;
marci@747
   174
//     while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@747
   175
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   176
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747
   177
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747
   178
marci@747
   179
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   180
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   181
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   182
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   183
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   184
//     }
marci@747
   185
//   }
marci@747
   186
marci@747
   187
//   {
marci@747
   188
//     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@747
   189
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   190
//     ts.reset();
marci@747
   191
//     int i=0;
marci@747
   192
//     while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@747
   193
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   194
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747
   195
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747
   196
marci@747
   197
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   198
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   199
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   200
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   201
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   202
//     }
marci@747
   203
//   }
marci@747
   204
marci@747
   205
//   {
marci@747
   206
//     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@747
   207
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747
   208
//     ts.reset();
marci@747
   209
//     int i=0;
marci@747
   210
//     while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@747
   211
//     std::cout << "elapsed time: " << ts << std::endl;
marci@747
   212
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747
   213
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747
   214
marci@747
   215
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   216
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747
   217
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   218
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747
   219
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@747
   220
//     }
marci@747
   221
//   }
marci@747
   222
marci@747
   223
  return 0;
marci@747
   224
}