src/work/marci/max_flow_demo.cc
author alpar
Thu, 03 Feb 2005 16:08:56 +0000
changeset 1119 d3504fc075dc
parent 921 818510fa3d99
permissions -rw-r--r--
Two incomplete additions:
- Exceptions
- bool map indication reached nodes (NullMap by default)
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@475
     6
#include <iostream>
marci@475
     7
#include <fstream>
marci@475
     8
alpar@921
     9
#include <lemon/smart_graph.h>
alpar@921
    10
#include <lemon/list_graph.h>
alpar@921
    11
#include <lemon/dimacs.h>
alpar@921
    12
#include <lemon/time_measure.h>
alpar@921
    13
#include <lemon/preflow.h>
marci@762
    14
#include <augmenting_flow.h>
marci@651
    15
#include <graph_concept.h>
marci@475
    16
alpar@921
    17
using namespace lemon;
marci@475
    18
marci@475
    19
int main(int, char **) {
marci@475
    20
marci@854
    21
  typedef ListGraph MutableGraph;
marci@854
    22
  typedef SmartGraph Graph;
marci@475
    23
  typedef Graph::Node Node;
marci@475
    24
  typedef Graph::EdgeIt EdgeIt;
marci@475
    25
marci@577
    26
  Graph g;
marci@652
    27
marci@475
    28
  Node s, t;
marci@577
    29
  Graph::EdgeMap<int> cap(g);
marci@577
    30
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    31
  readDimacs(std::cin, g, cap, s, t);
marci@475
    32
  Timer ts;
marci@577
    33
  Graph::EdgeMap<int> flow(g); //0 flow
marci@849
    34
  Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@577
    35
    max_flow_test(g, s, t, cap, flow);
marci@762
    36
  AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@762
    37
    augmenting_flow_test(g, s, t, cap, flow);
marci@762
    38
  
marci@646
    39
  Graph::NodeMap<bool> cut(g);
marci@475
    40
marci@475
    41
  {
marci@475
    42
    std::cout << "preflow ..." << std::endl;
marci@475
    43
    ts.reset();
marci@476
    44
    max_flow_test.run();
marci@475
    45
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    46
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@849
    47
    max_flow_test.minCut(cut);
marci@646
    48
marci@854
    49
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    50
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@646
    51
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    52
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@646
    53
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    54
    }
marci@475
    55
  }
marci@475
    56
marci@475
    57
  {
marci@475
    58
    std::cout << "preflow ..." << std::endl;
marci@854
    59
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@475
    60
    ts.reset();
marci@854
    61
    max_flow_test.run(Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@475
    62
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    63
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
    64
marci@854
    65
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    66
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@646
    67
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    68
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@646
    69
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    70
    }
marci@475
    71
  }
marci@475
    72
marci@475
    73
//   {
marci@475
    74
//     std::cout << "wrapped preflow ..." << std::endl;
marci@577
    75
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
    76
//     ts.reset();
marci@475
    77
//     pre_flow_res.run();
marci@475
    78
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
    79
//     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@475
    80
//   }
marci@475
    81
marci@475
    82
  {
marci@475
    83
    std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@854
    84
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@475
    85
    ts.reset();
marci@475
    86
    int i=0;
marci@762
    87
    while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@475
    88
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
    89
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
    90
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
    91
marci@854
    92
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    93
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@646
    94
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    95
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@646
    96
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    97
    }
marci@475
    98
  }
marci@475
    99
marci@777
   100
  {
marci@777
   101
    std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@854
   102
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@777
   103
    ts.reset();
marci@777
   104
    int i=0;
marci@777
   105
    while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@777
   106
    std::cout << "elapsed time: " << ts << std::endl;
marci@777
   107
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@777
   108
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@775
   109
marci@854
   110
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
   111
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@777
   112
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   113
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@777
   114
	std::cout << "Slackness does not hold!" << std::endl;
marci@777
   115
    }
marci@777
   116
  }
marci@475
   117
marci@475
   118
  {
marci@475
   119
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@854
   120
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@475
   121
    ts.reset();
marci@475
   122
    int i=0;
marci@762
   123
    while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@475
   124
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   125
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   126
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   127
marci@854
   128
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
   129
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@646
   130
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   131
      if (!cut[g.source(e)] && cut[g.target(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@646
   136
  {
marci@646
   137
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@854
   138
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@646
   139
    ts.reset();
marci@646
   140
    int i=0;
marci@762
   141
    while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@646
   142
    std::cout << "elapsed time: " << ts << std::endl;
marci@646
   143
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   144
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   145
marci@854
   146
    for(Graph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
   147
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@646
   148
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   149
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@646
   150
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   151
    }
marci@646
   152
  }
marci@475
   153
marci@475
   154
  return 0;
marci@475
   155
}