| 1 | // -*- c++ -*- |
|---|
| 2 | #include <iostream> |
|---|
| 3 | #include <fstream> |
|---|
| 4 | |
|---|
| 5 | #include <list_graph.h> |
|---|
| 6 | #include <smart_graph.h> |
|---|
| 7 | #include <dimacs.h> |
|---|
| 8 | #include <edmonds_karp.h> |
|---|
| 9 | #include <time_measure.h> |
|---|
| 10 | #include <graph_wrapper.h> |
|---|
| 11 | |
|---|
| 12 | class CM { |
|---|
| 13 | public: |
|---|
| 14 | template<typename T> int get(T) const {return 1;} |
|---|
| 15 | }; |
|---|
| 16 | |
|---|
| 17 | using namespace hugo; |
|---|
| 18 | |
|---|
| 19 | // Use a DIMACS max flow file as stdin. |
|---|
| 20 | // read_dimacs_demo < dimacs_max_flow_file |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | // struct Ize { |
|---|
| 24 | // }; |
|---|
| 25 | |
|---|
| 26 | // struct Mize { |
|---|
| 27 | // Ize bumm; |
|---|
| 28 | // }; |
|---|
| 29 | |
|---|
| 30 | // template <typename B> |
|---|
| 31 | // class Huha { |
|---|
| 32 | // public: |
|---|
| 33 | // int u; |
|---|
| 34 | // B brr; |
|---|
| 35 | // }; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | int main(int, char **) { |
|---|
| 39 | |
|---|
| 40 | typedef ListGraph MutableGraph; |
|---|
| 41 | |
|---|
| 42 | //typedef SmartGraph Graph; |
|---|
| 43 | typedef ListGraph Graph; |
|---|
| 44 | typedef Graph::Node Node; |
|---|
| 45 | typedef Graph::EdgeIt EdgeIt; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | // Mize mize[10]; |
|---|
| 49 | // Mize bize[0]; |
|---|
| 50 | // Mize zize; |
|---|
| 51 | // typedef Mize Tize[0]; |
|---|
| 52 | |
|---|
| 53 | // std::cout << &zize << " " << sizeof(mize) << sizeof(Tize) << std::endl; |
|---|
| 54 | // std::cout << sizeof(bize) << std::endl; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | // Huha<Tize> k; |
|---|
| 58 | // std::cout << sizeof(k) << std::endl; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | // struct Bumm { |
|---|
| 62 | // //int a; |
|---|
| 63 | // bool b; |
|---|
| 64 | // }; |
|---|
| 65 | |
|---|
| 66 | // std::cout << sizeof(Bumm) << std::endl; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | Graph G; |
|---|
| 70 | Node s, t; |
|---|
| 71 | Graph::EdgeMap<int> cap(G); |
|---|
| 72 | readDimacsMaxFlow(std::cin, G, s, t, cap); |
|---|
| 73 | |
|---|
| 74 | // typedef TrivGraphWrapper<Graph> TGW; |
|---|
| 75 | // TGW gw(G); |
|---|
| 76 | // TGW::NodeIt sw; |
|---|
| 77 | // gw./*getF*/first(sw); |
|---|
| 78 | // std::cout << "p1:" << gw.nodeNum() << std::endl; |
|---|
| 79 | // gw.erase(sw); |
|---|
| 80 | // std::cout << "p2:" << gw.nodeNum() << std::endl; |
|---|
| 81 | |
|---|
| 82 | // typedef const Graph cLG; |
|---|
| 83 | // typedef TrivGraphWrapper<const cLG> CTGW; |
|---|
| 84 | // CTGW cgw(G); |
|---|
| 85 | // CTGW::NodeIt csw; |
|---|
| 86 | // cgw./*getF*/first(csw); |
|---|
| 87 | // std::cout << "p1:" << cgw.nodeNum() << std::endl; |
|---|
| 88 | // //cgw.erase(csw); |
|---|
| 89 | // std::cout << "p2:" << cgw.nodeNum() << std::endl; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | { |
|---|
| 93 | //std::cout << "SmartGraph..." << std::endl; |
|---|
| 94 | typedef TrivGraphWrapper<const Graph> GW; |
|---|
| 95 | GW gw(G); |
|---|
| 96 | std::cout << "edmonds karp demo (physical blocking flow augmentation)..." << std::endl; |
|---|
| 97 | GW::EdgeMap<int> flow(G); //0 flow |
|---|
| 98 | |
|---|
| 99 | Timer ts; |
|---|
| 100 | ts.reset(); |
|---|
| 101 | |
|---|
| 102 | typedef GW::EdgeMapWrapper< Graph::EdgeMap<int>, int > EMW; |
|---|
| 103 | EMW cw(cap); |
|---|
| 104 | MaxFlow<GW, int, GW::EdgeMap<int>, EMW > max_flow_test(gw, s, t, flow, cw); |
|---|
| 105 | int i=0; |
|---|
| 106 | while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { |
|---|
| 107 | // for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 108 | // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 109 | // } |
|---|
| 110 | // std::cout<<std::endl; |
|---|
| 111 | ++i; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // std::cout << "maximum flow: "<< std::endl; |
|---|
| 115 | // for(EdgeIt e=G.first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 116 | // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 117 | // } |
|---|
| 118 | // std::cout<<std::endl; |
|---|
| 119 | std::cout << "elapsed time: " << ts << std::endl; |
|---|
| 120 | std::cout << "number of augmentation phases: " << i << std::endl; |
|---|
| 121 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // { |
|---|
| 125 | // //std::cout << "SmartGraph..." << std::endl; |
|---|
| 126 | // std::cout << "edmonds karp demo (physical blocking flow 1 augmentation)..." << std::endl; |
|---|
| 127 | // Graph::EdgeMap<int> flow(G); //0 flow |
|---|
| 128 | |
|---|
| 129 | // Timer ts; |
|---|
| 130 | // ts.reset(); |
|---|
| 131 | |
|---|
| 132 | // MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s, t, flow, cap); |
|---|
| 133 | // int i=0; |
|---|
| 134 | // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { |
|---|
| 135 | // // for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 136 | // // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 137 | // // } |
|---|
| 138 | // // std::cout<<std::endl; |
|---|
| 139 | // ++i; |
|---|
| 140 | // } |
|---|
| 141 | |
|---|
| 142 | // // std::cout << "maximum flow: "<< std::endl; |
|---|
| 143 | // // for(EdgeIt e=G.first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 144 | // // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 145 | // // } |
|---|
| 146 | // // std::cout<<std::endl; |
|---|
| 147 | // std::cout << "elapsed time: " << ts << std::endl; |
|---|
| 148 | // std::cout << "number of augmentation phases: " << i << std::endl; |
|---|
| 149 | // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
|---|
| 150 | // } |
|---|
| 151 | |
|---|
| 152 | // { |
|---|
| 153 | // std::cout << "edmonds karp demo (on-the-fly blocking flow augmentation)..." << std::endl; |
|---|
| 154 | // Graph::EdgeMap<int> flow(G); //0 flow |
|---|
| 155 | |
|---|
| 156 | // Timer ts; |
|---|
| 157 | // ts.reset(); |
|---|
| 158 | |
|---|
| 159 | // MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s, t, flow, cap); |
|---|
| 160 | // int i=0; |
|---|
| 161 | // while (max_flow_test.augmentOnBlockingFlow2()) { |
|---|
| 162 | // // for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 163 | // // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 164 | // // } |
|---|
| 165 | // // std::cout<<std::endl; |
|---|
| 166 | // ++i; |
|---|
| 167 | // } |
|---|
| 168 | |
|---|
| 169 | // // std::cout << "maximum flow: "<< std::endl; |
|---|
| 170 | // // for(EdgeIt e=G.first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 171 | // // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 172 | // // } |
|---|
| 173 | // // std::cout<<std::endl; |
|---|
| 174 | // std::cout << "elapsed time: " << ts << std::endl; |
|---|
| 175 | // std::cout << "number of augmentation phases: " << i << std::endl; |
|---|
| 176 | // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
|---|
| 177 | // } |
|---|
| 178 | |
|---|
| 179 | { |
|---|
| 180 | typedef TrivGraphWrapper<const Graph> GW; |
|---|
| 181 | GW gw(G); |
|---|
| 182 | std::cout << "edmonds karp demo (on-the-fly shortest path augmentation)..." << std::endl; |
|---|
| 183 | GW::EdgeMap<int> flow(gw); //0 flow |
|---|
| 184 | |
|---|
| 185 | Timer ts; |
|---|
| 186 | ts.reset(); |
|---|
| 187 | |
|---|
| 188 | //CM cm; |
|---|
| 189 | typedef GW::EdgeMapWrapper< Graph::EdgeMap<int>, int > EMW; |
|---|
| 190 | EMW cw(cap); |
|---|
| 191 | MaxFlow<GW, int, GW::EdgeMap<int>, EMW> max_flow_test(gw, s, t, flow, cw); |
|---|
| 192 | int i=0; |
|---|
| 193 | while (max_flow_test.augmentOnShortestPath()) { |
|---|
| 194 | // for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 195 | // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 196 | // } |
|---|
| 197 | // std::cout<<std::endl; |
|---|
| 198 | ++i; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | // std::cout << "maximum flow: "<< std::endl; |
|---|
| 202 | // for(EdgeIt e=G.first<EdgeIt>(); e.valid(); ++e) { |
|---|
| 203 | // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
|---|
| 204 | // } |
|---|
| 205 | // std::cout<<std::endl; |
|---|
| 206 | std::cout << "elapsed time: " << ts << std::endl; |
|---|
| 207 | std::cout << "number of augmentation phases: " << i << std::endl; |
|---|
| 208 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | return 0; |
|---|
| 213 | } |
|---|