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