src/work/marci/max_flow_demo.cc
author marci
Tue, 31 Aug 2004 11:26:59 +0000
changeset 775 e46a1f0623a0
parent 762 511200bdb71f
child 777 a82713ed19f3
permissions -rw-r--r--
ResGraphWrapper<Graph> is done, so does dimacs.h.
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@762
    10
#include <hugo/max_flow.h>
marci@762
    11
#include <augmenting_flow.h>
marci@475
    12
//#include <preflow_res.h>
marci@762
    13
#include <for_each_macros.h>
marci@651
    14
#include <graph_concept.h>
marci@475
    15
marci@475
    16
using namespace hugo;
marci@475
    17
marci@475
    18
// Use a DIMACS max flow file as stdin.
marci@775
    19
// max_flow_demo < dimacs_max_flow_file
marci@475
    20
marci@475
    21
int main(int, char **) {
marci@475
    22
marci@642
    23
  typedef SageGraph MutableGraph;
marci@475
    24
marci@651
    25
  //typedef FullFeatureGraphConcept Graph;
marci@762
    26
  //typedef SmartGraph Graph;
marci@762
    27
  typedef SageGraph Graph;
marci@475
    28
  typedef Graph::Node Node;
marci@475
    29
  typedef Graph::EdgeIt EdgeIt;
marci@475
    30
marci@577
    31
  Graph g;
marci@652
    32
marci@475
    33
  Node s, t;
marci@577
    34
  Graph::EdgeMap<int> cap(g);
marci@577
    35
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    36
  readDimacs(std::cin, g, cap, s, t);
marci@475
    37
  Timer ts;
marci@577
    38
  Graph::EdgeMap<int> flow(g); //0 flow
marci@476
    39
  MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@577
    40
    max_flow_test(g, s, t, cap, flow);
marci@762
    41
  AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@762
    42
    augmenting_flow_test(g, s, t, cap, flow);
marci@762
    43
  
marci@646
    44
  Graph::NodeMap<bool> cut(g);
marci@475
    45
marci@475
    46
  {
marci@475
    47
    std::cout << "preflow ..." << std::endl;
marci@475
    48
    ts.reset();
marci@476
    49
    max_flow_test.run();
marci@475
    50
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    51
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
    52
    max_flow_test.actMinCut(cut);
marci@646
    53
marci@646
    54
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
    55
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
    56
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    57
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
    58
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    59
    }
marci@475
    60
  }
marci@475
    61
marci@475
    62
  {
marci@475
    63
    std::cout << "preflow ..." << std::endl;
marci@577
    64
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
    65
    ts.reset();
marci@476
    66
    max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@475
    67
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    68
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
    69
marci@646
    70
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
    71
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
    72
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    73
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
    74
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    75
    }
marci@475
    76
  }
marci@475
    77
marci@475
    78
//   {
marci@475
    79
//     std::cout << "wrapped preflow ..." << std::endl;
marci@577
    80
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
    81
//     ts.reset();
marci@475
    82
//     pre_flow_res.run();
marci@475
    83
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
    84
//     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@475
    85
//   }
marci@475
    86
marci@475
    87
  {
marci@475
    88
    std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@577
    89
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
    90
    ts.reset();
marci@475
    91
    int i=0;
marci@762
    92
    while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@475
    93
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
    94
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
    95
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
    96
marci@646
    97
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
    98
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
    99
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   100
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   101
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   102
    }
marci@475
   103
  }
marci@475
   104
marci@475
   105
//   {
marci@775
   106
//     std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@577
   107
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   108
//     ts.reset();
marci@475
   109
//     int i=0;
marci@775
   110
//     while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@475
   111
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
   112
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@775
   113
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@775
   114
marci@775
   115
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@775
   116
//       if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@775
   117
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@775
   118
//       if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@775
   119
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@775
   120
//     }
marci@475
   121
//   }
marci@475
   122
marci@475
   123
  {
marci@475
   124
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@577
   125
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   126
    ts.reset();
marci@475
   127
    int i=0;
marci@762
   128
    while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@475
   129
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   130
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   131
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   132
marci@646
   133
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   134
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   135
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   136
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   137
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   138
    }
marci@475
   139
  }
marci@475
   140
marci@646
   141
  {
marci@646
   142
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@646
   143
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@646
   144
    ts.reset();
marci@646
   145
    int i=0;
marci@762
   146
    while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@646
   147
    std::cout << "elapsed time: " << ts << std::endl;
marci@646
   148
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   149
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   150
marci@646
   151
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   152
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   153
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   154
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   155
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   156
    }
marci@646
   157
  }
marci@475
   158
marci@475
   159
  return 0;
marci@475
   160
}