[475] | 1 | // -*- c++ -*- |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <fstream> |
---|
| 4 | |
---|
[642] | 5 | #include <sage_graph.h> |
---|
[555] | 6 | #include <hugo/smart_graph.h> |
---|
| 7 | #include <hugo/dimacs.h> |
---|
| 8 | #include <hugo/time_measure.h> |
---|
[475] | 9 | //#include <graph_wrapper.h> |
---|
[480] | 10 | #include <max_flow.h> |
---|
[475] | 11 | //#include <preflow_res.h> |
---|
[640] | 12 | #include <hugo/for_each_macros.h> |
---|
[651] | 13 | #include <graph_concept.h> |
---|
[475] | 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 | |
---|
[642] | 38 | typedef SageGraph MutableGraph; |
---|
[475] | 39 | |
---|
[651] | 40 | //typedef FullFeatureGraphConcept Graph; |
---|
[475] | 41 | typedef SmartGraph Graph; |
---|
[642] | 42 | // typedef SageGraph Graph; |
---|
[475] | 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 | |
---|
[577] | 68 | Graph g; |
---|
[475] | 69 | Node s, t; |
---|
[577] | 70 | Graph::EdgeMap<int> cap(g); |
---|
| 71 | //readDimacsMaxFlow(std::cin, g, s, t, cap); |
---|
| 72 | readDimacs(std::cin, g, cap, s, t); |
---|
[475] | 73 | Timer ts; |
---|
[577] | 74 | Graph::EdgeMap<int> flow(g); //0 flow |
---|
[476] | 75 | MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > |
---|
[577] | 76 | max_flow_test(g, s, t, cap, flow); |
---|
[646] | 77 | Graph::NodeMap<bool> cut(g); |
---|
[475] | 78 | |
---|
| 79 | { |
---|
| 80 | std::cout << "preflow ..." << std::endl; |
---|
| 81 | ts.reset(); |
---|
[476] | 82 | max_flow_test.run(); |
---|
[475] | 83 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[476] | 84 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 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 | } |
---|
[475] | 93 | } |
---|
| 94 | |
---|
| 95 | { |
---|
| 96 | std::cout << "preflow ..." << std::endl; |
---|
[577] | 97 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 98 | ts.reset(); |
---|
[476] | 99 | max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW); |
---|
[475] | 100 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[476] | 101 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 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 | } |
---|
[475] | 109 | } |
---|
| 110 | |
---|
| 111 | // { |
---|
| 112 | // std::cout << "wrapped preflow ..." << std::endl; |
---|
[577] | 113 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 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; |
---|
[577] | 122 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 123 | ts.reset(); |
---|
| 124 | int i=0; |
---|
[476] | 125 | while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; } |
---|
[475] | 126 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 127 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[476] | 128 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 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 | } |
---|
[475] | 136 | } |
---|
| 137 | |
---|
| 138 | // { |
---|
| 139 | // std::cout << "faster physical blocking flow augmentation ..." << std::endl; |
---|
[577] | 140 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 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; |
---|
[577] | 151 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 152 | ts.reset(); |
---|
| 153 | int i=0; |
---|
[476] | 154 | while (max_flow_test.augmentOnBlockingFlow2()) { ++i; } |
---|
[475] | 155 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 156 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[476] | 157 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 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 | } |
---|
[475] | 165 | } |
---|
| 166 | |
---|
| 167 | { |
---|
| 168 | std::cout << "on-the-fly shortest path augmentation ..." << std::endl; |
---|
[577] | 169 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 170 | ts.reset(); |
---|
| 171 | int i=0; |
---|
[476] | 172 | while (max_flow_test.augmentOnShortestPath()) { ++i; } |
---|
[475] | 173 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 174 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[476] | 175 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 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 | } |
---|
[475] | 183 | } |
---|
| 184 | |
---|
[646] | 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 | } |
---|
[475] | 202 | |
---|
| 203 | return 0; |
---|
| 204 | } |
---|