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