src/work/marci/edmonds_karp_demo.cc
author marci
Thu, 11 Mar 2004 14:15:07 +0000
changeset 168 27fbd1559fb7
parent 155 8c6292ec54c6
child 174 44700ed9ffaa
permissions -rw-r--r--
graph wrapper improvements, blocking flow on fly
marci@73
     1
#include <iostream>
marci@73
     2
#include <fstream>
marci@73
     3
marci@73
     4
#include <list_graph.hh>
marci@73
     5
#include <dimacs.hh>
marci@73
     6
#include <edmonds_karp.hh>
marci@73
     7
#include <time_measure.h>
marci@155
     8
#include <graph_wrapper.h>
marci@73
     9
alpar@105
    10
using namespace hugo;
marci@73
    11
marci@73
    12
// Use a DIMACS max flow file as stdin.
marci@73
    13
// read_dimacs_demo < dimacs_max_flow_file
marci@139
    14
marci@139
    15
/*
marci@139
    16
  struct Ize {
marci@139
    17
  };
marci@139
    18
  
marci@139
    19
  struct Mize {
marci@139
    20
    Ize bumm;
marci@139
    21
  };
marci@139
    22
marci@139
    23
  template <typename B>
marci@139
    24
    class Huha {
marci@139
    25
    public:
marci@139
    26
      int u;
marci@139
    27
      B brr;
marci@139
    28
    };
marci@139
    29
*/
marci@139
    30
marci@73
    31
int main(int, char **) {
marci@73
    32
  typedef ListGraph::NodeIt NodeIt;
marci@73
    33
  typedef ListGraph::EachEdgeIt EachEdgeIt;
marci@73
    34
marci@139
    35
/*
marci@139
    36
  Mize mize[10];
marci@139
    37
  Mize bize[0];
marci@139
    38
  Mize zize;
marci@139
    39
  typedef Mize Tize[0];
marci@139
    40
marci@139
    41
  std::cout << &zize << " " << sizeof(mize) << sizeof(Tize) << std::endl;
marci@139
    42
  std::cout << sizeof(bize) << std::endl;
marci@139
    43
marci@139
    44
marci@139
    45
  Huha<Tize> k;
marci@139
    46
  std::cout << sizeof(k) << std::endl;
marci@146
    47
marci@146
    48
marci@146
    49
  struct Bumm {
marci@146
    50
    //int a;
marci@146
    51
    bool b;
marci@146
    52
  };
marci@146
    53
marci@146
    54
  std::cout << sizeof(Bumm) << std::endl;
marci@139
    55
*/
marci@139
    56
marci@73
    57
  ListGraph G;
marci@73
    58
  NodeIt s, t;
marci@73
    59
  ListGraph::EdgeMap<int> cap(G);
marci@73
    60
  readDimacsMaxFlow(std::cin, G, s, t, cap);
marci@155
    61
/*
marci@155
    62
  typedef TrivGraphWrapper<ListGraph> TGW;
marci@155
    63
  TGW gw(G);
marci@155
    64
  TGW::EachNodeIt sw;
marci@155
    65
  gw.getFirst(sw);
marci@155
    66
  std::cout << "p1:" << gw.nodeNum() << std::endl;
marci@155
    67
  gw.erase(sw);
marci@155
    68
  std::cout << "p2:" << gw.nodeNum() << std::endl;
marci@155
    69
marci@155
    70
  typedef const ListGraph cLG;
marci@155
    71
  typedef TrivGraphWrapper<const cLG> CTGW;
marci@155
    72
  CTGW cgw(G);
marci@155
    73
  CTGW::EachNodeIt csw;
marci@155
    74
  cgw.getFirst(csw);
marci@155
    75
  std::cout << "p1:" << cgw.nodeNum() << std::endl;
marci@155
    76
  //cgw.erase(csw);
marci@155
    77
  std::cout << "p2:" << cgw.nodeNum() << std::endl;
marci@155
    78
*/
marci@73
    79
marci@133
    80
  {
marci@133
    81
  std::cout << "edmonds karp demo (blocking flow augmentation)..." << std::endl;
marci@73
    82
  ListGraph::EdgeMap<int> flow(G); //0 flow
marci@73
    83
marci@144
    84
  Timer ts;
marci@144
    85
  ts.reset();
marci@144
    86
  //double pre_time=currTime();
marci@73
    87
  MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(G, s, t, flow, cap);
marci@133
    88
  //max_flow_test.augmentWithBlockingFlow<ListGraph>();
marci@138
    89
  int i=0;
marci@168
    90
  while (max_flow_test.augmentOnBlockingFlow<ListGraph>()) { 
marci@168
    91
//     for(EachEdgeIt e=G.template first<EachEdgeIt>(); e.valid(); ++e) { 
marci@168
    92
//       std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@168
    93
//     }
marci@168
    94
//     std::cout<<std::endl;
marci@168
    95
    ++i; 
marci@168
    96
  }
marci@168
    97
  //double post_time=currTime();
marci@168
    98
marci@168
    99
  //std::cout << "maximum flow: "<< std::endl;
marci@168
   100
  //for(EachEdgeIt e=G.first<EachEdgeIt>(); e.valid(); ++e) { 
marci@168
   101
  //  std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@168
   102
  //}
marci@168
   103
  //std::cout<<std::endl;
marci@168
   104
  std::cout << "elapsed time: " << ts << std::endl;
marci@168
   105
  std::cout << "number of augmentation phases: " << i << std::endl; 
marci@168
   106
  std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@168
   107
  }
marci@168
   108
marci@168
   109
  {
marci@168
   110
  std::cout << "edmonds karp demo (blocking flow augmentation)..." << std::endl;
marci@168
   111
  ListGraph::EdgeMap<int> flow(G); //0 flow
marci@168
   112
marci@168
   113
  Timer ts;
marci@168
   114
  ts.reset();
marci@168
   115
  //double pre_time=currTime();
marci@168
   116
  MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(G, s, t, flow, cap);
marci@168
   117
  //max_flow_test.augmentWithBlockingFlow<ListGraph>();
marci@168
   118
  int i=0;
marci@168
   119
  while (max_flow_test.augmentOnBlockingFlow2()) { 
marci@168
   120
//     for(EachEdgeIt e=G.template first<EachEdgeIt>(); e.valid(); ++e) { 
marci@168
   121
//       std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@168
   122
//     }
marci@168
   123
//     std::cout<<std::endl;
marci@168
   124
    ++i; 
marci@168
   125
  }
marci@144
   126
  //double post_time=currTime();
marci@133
   127
marci@73
   128
  //std::cout << "maximum flow: "<< std::endl;
marci@73
   129
  //for(EachEdgeIt e=G.first<EachEdgeIt>(); e.valid(); ++e) { 
marci@73
   130
  //  std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@73
   131
  //}
marci@73
   132
  //std::cout<<std::endl;
marci@144
   133
  std::cout << "elapsed time: " << ts << std::endl;
marci@138
   134
  std::cout << "number of augmentation phases: " << i << std::endl; 
marci@73
   135
  std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@133
   136
  }
marci@133
   137
marci@133
   138
  {
marci@133
   139
  std::cout << "edmonds karp demo (shortest path augmentation)..." << std::endl;
marci@133
   140
  ListGraph::EdgeMap<int> flow(G); //0 flow
marci@133
   141
marci@144
   142
  Timer ts;
marci@144
   143
  ts.reset();
marci@144
   144
  //double pre_time=currTime();
marci@133
   145
  MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(G, s, t, flow, cap);
marci@133
   146
  //max_flow_test.augmentWithBlockingFlow<ListGraph>();
marci@138
   147
  int i=0;
marci@168
   148
  while (max_flow_test.augmentOnShortestPath()) { 
marci@168
   149
//     for(EachEdgeIt e=G.template first<EachEdgeIt>(); e.valid(); ++e) { 
marci@168
   150
//       std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@168
   151
//     }
marci@168
   152
//     std::cout<<std::endl;
marci@168
   153
    ++i; 
marci@168
   154
  }
marci@144
   155
  //double post_time=currTime();
marci@133
   156
marci@133
   157
  //std::cout << "maximum flow: "<< std::endl;
marci@133
   158
  //for(EachEdgeIt e=G.first<EachEdgeIt>(); e.valid(); ++e) { 
marci@133
   159
  //  std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@133
   160
  //}
marci@133
   161
  //std::cout<<std::endl;
marci@144
   162
  std::cout << "elapsed time: " << ts << std::endl;
marci@138
   163
  std::cout << "number of augmentation phases: " << i << std::endl; 
marci@133
   164
  std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@133
   165
  }
marci@73
   166
marci@73
   167
  return 0;
marci@73
   168
}