src/work/marci/max_flow_demo.cc
author marci
Wed, 19 May 2004 16:06:57 +0000
changeset 646 bd7a69231cf8
parent 642 e812963087f0
child 651 a56e043aeab1
permissions -rw-r--r--
max_flow.h: status flags for actMinCut
leda_graph_wrapper.h: NodeMapWrapper, EdgeMapWrapper
marci@475
     1
// -*- c++ -*-
marci@475
     2
#include <iostream>
marci@475
     3
#include <fstream>
marci@475
     4
marci@642
     5
#include <sage_graph.h>
marci@555
     6
#include <hugo/smart_graph.h>
marci@555
     7
#include <hugo/dimacs.h>
marci@555
     8
#include <hugo/time_measure.h>
marci@475
     9
//#include <graph_wrapper.h>
marci@480
    10
#include <max_flow.h>
marci@475
    11
//#include <preflow_res.h>
marci@640
    12
#include <hugo/for_each_macros.h>
marci@475
    13
marci@475
    14
using namespace hugo;
marci@475
    15
marci@475
    16
// Use a DIMACS max flow file as stdin.
marci@475
    17
// read_dimacs_demo < dimacs_max_flow_file
marci@475
    18
marci@475
    19
marci@475
    20
//   struct Ize {
marci@475
    21
//   };
marci@475
    22
  
marci@475
    23
//   struct Mize {
marci@475
    24
//     Ize bumm;
marci@475
    25
//   };
marci@475
    26
marci@475
    27
//   template <typename B>
marci@475
    28
//     class Huha {
marci@475
    29
//     public:
marci@475
    30
//       int u;
marci@475
    31
//       B brr;
marci@475
    32
//     };
marci@475
    33
marci@475
    34
marci@475
    35
int main(int, char **) {
marci@475
    36
marci@642
    37
  typedef SageGraph MutableGraph;
marci@475
    38
marci@475
    39
  typedef SmartGraph Graph;
marci@642
    40
  //  typedef SageGraph Graph;
marci@475
    41
  typedef Graph::Node Node;
marci@475
    42
  typedef Graph::EdgeIt EdgeIt;
marci@475
    43
marci@475
    44
marci@475
    45
//   Mize mize[10];
marci@475
    46
//   Mize bize[0];
marci@475
    47
//   Mize zize;
marci@475
    48
//   typedef Mize Tize[0];
marci@475
    49
marci@475
    50
//   std::cout << &zize << " " << sizeof(mize) << sizeof(Tize) << std::endl;
marci@475
    51
//   std::cout << sizeof(bize) << std::endl;
marci@475
    52
marci@475
    53
marci@475
    54
//   Huha<Tize> k;
marci@475
    55
//   std::cout << sizeof(k) << std::endl;
marci@475
    56
marci@475
    57
marci@475
    58
//   struct Bumm {
marci@475
    59
//     //int a;
marci@475
    60
//     bool b;
marci@475
    61
//   };
marci@475
    62
marci@475
    63
//   std::cout << sizeof(Bumm) << std::endl;
marci@475
    64
marci@475
    65
marci@577
    66
  Graph g;
marci@475
    67
  Node s, t;
marci@577
    68
  Graph::EdgeMap<int> cap(g);
marci@577
    69
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    70
  readDimacs(std::cin, g, cap, s, t);
marci@475
    71
  Timer ts;
marci@577
    72
  Graph::EdgeMap<int> flow(g); //0 flow
marci@476
    73
  MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@577
    74
    max_flow_test(g, s, t, cap, flow);
marci@646
    75
  Graph::NodeMap<bool> cut(g);
marci@475
    76
marci@475
    77
  {
marci@475
    78
    std::cout << "preflow ..." << std::endl;
marci@475
    79
    ts.reset();
marci@476
    80
    max_flow_test.run();
marci@475
    81
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    82
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
    83
    max_flow_test.actMinCut(cut);
marci@646
    84
marci@646
    85
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
    86
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
    87
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    88
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
    89
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    90
    }
marci@475
    91
  }
marci@475
    92
marci@475
    93
  {
marci@475
    94
    std::cout << "preflow ..." << std::endl;
marci@577
    95
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
    96
    ts.reset();
marci@476
    97
    max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@475
    98
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    99
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   100
marci@646
   101
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   102
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   103
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   104
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   105
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   106
    }
marci@475
   107
  }
marci@475
   108
marci@475
   109
//   {
marci@475
   110
//     std::cout << "wrapped preflow ..." << std::endl;
marci@577
   111
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   112
//     ts.reset();
marci@475
   113
//     pre_flow_res.run();
marci@475
   114
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
   115
//     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@475
   116
//   }
marci@475
   117
marci@475
   118
  {
marci@475
   119
    std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@577
   120
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   121
    ts.reset();
marci@475
   122
    int i=0;
marci@476
   123
    while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@475
   124
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   125
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@476
   126
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   127
marci@646
   128
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   129
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   130
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   131
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   132
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   133
    }
marci@475
   134
  }
marci@475
   135
marci@475
   136
//   {
marci@475
   137
//     std::cout << "faster physical blocking flow augmentation ..." << std::endl;
marci@577
   138
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   139
//     ts.reset();
marci@475
   140
//     int i=0;
marci@475
   141
//     while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
marci@475
   142
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
   143
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@475
   144
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@475
   145
//   }
marci@475
   146
marci@475
   147
  {
marci@475
   148
    std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@577
   149
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   150
    ts.reset();
marci@475
   151
    int i=0;
marci@476
   152
    while (max_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@475
   153
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   154
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@476
   155
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   156
marci@646
   157
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   158
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   159
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   160
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   161
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   162
    }
marci@475
   163
  }
marci@475
   164
marci@475
   165
  {
marci@475
   166
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@577
   167
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   168
    ts.reset();
marci@475
   169
    int i=0;
marci@476
   170
    while (max_flow_test.augmentOnShortestPath()) { ++i; }
marci@475
   171
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   172
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@476
   173
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   174
marci@646
   175
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   176
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   177
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   178
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   179
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   180
    }
marci@475
   181
  }
marci@475
   182
marci@646
   183
  {
marci@646
   184
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@646
   185
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@646
   186
    ts.reset();
marci@646
   187
    int i=0;
marci@646
   188
    while (max_flow_test.augmentOnShortestPath2()) { ++i; }
marci@646
   189
    std::cout << "elapsed time: " << ts << std::endl;
marci@646
   190
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@646
   191
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   192
marci@646
   193
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   194
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   195
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   196
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   197
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   198
    }
marci@646
   199
  }
marci@475
   200
marci@475
   201
  return 0;
marci@475
   202
}