[764] | 1 | // -*- c++ -*- |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <fstream> |
---|
| 4 | |
---|
[921] | 5 | #include <lemon/smart_graph.h> |
---|
[1014] | 6 | #include <lemon/list_graph.h> |
---|
[921] | 7 | #include <lemon/dimacs.h> |
---|
| 8 | #include <lemon/time_measure.h> |
---|
[764] | 9 | //#include <graph_wrapper.h> |
---|
[1014] | 10 | #include <lemon/preflow.h> |
---|
[764] | 11 | #include <augmenting_flow.h> |
---|
| 12 | //#include <preflow_res.h> |
---|
[1031] | 13 | //#include <lp_solver_wrapper_2.h> |
---|
[1017] | 14 | #include <min_cost_gen_flow.h> |
---|
[764] | 15 | |
---|
| 16 | // Use a DIMACS max flow file as stdin. |
---|
| 17 | // max_flow_demo < dimacs_max_flow_file |
---|
| 18 | |
---|
[1015] | 19 | using namespace lemon; |
---|
| 20 | |
---|
[764] | 21 | int main(int, char **) { |
---|
| 22 | |
---|
[1014] | 23 | typedef ListGraph MutableGraph; |
---|
[1025] | 24 | typedef ListGraph Graph; |
---|
[764] | 25 | typedef Graph::Node Node; |
---|
[1015] | 26 | typedef Graph::Edge Edge; |
---|
[764] | 27 | typedef Graph::EdgeIt EdgeIt; |
---|
| 28 | |
---|
| 29 | Graph g; |
---|
| 30 | |
---|
| 31 | Node s, t; |
---|
| 32 | Graph::EdgeMap<int> cap(g); |
---|
| 33 | //readDimacsMaxFlow(std::cin, g, s, t, cap); |
---|
| 34 | readDimacs(std::cin, g, cap, s, t); |
---|
| 35 | Timer ts; |
---|
| 36 | Graph::EdgeMap<int> flow(g); //0 flow |
---|
[1014] | 37 | Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > |
---|
[764] | 38 | max_flow_test(g, s, t, cap, flow); |
---|
| 39 | AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > |
---|
| 40 | augmenting_flow_test(g, s, t, cap, flow); |
---|
| 41 | |
---|
| 42 | Graph::NodeMap<bool> cut(g); |
---|
| 43 | |
---|
| 44 | { |
---|
| 45 | std::cout << "preflow ..." << std::endl; |
---|
| 46 | ts.reset(); |
---|
| 47 | max_flow_test.run(); |
---|
| 48 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 49 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[1014] | 50 | max_flow_test.minCut(cut); |
---|
[764] | 51 | |
---|
[1015] | 52 | for (EdgeIt e(g); e!=INVALID; ++e) { |
---|
[986] | 53 | if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 54 | std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 55 | if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 56 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // { |
---|
| 61 | // std::cout << "preflow ..." << std::endl; |
---|
| 62 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 63 | // ts.reset(); |
---|
[1014] | 64 | // max_flow_test.preflow(Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW); |
---|
[764] | 65 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 66 | // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
| 67 | |
---|
| 68 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
[986] | 69 | // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 70 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 71 | // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 72 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
| 73 | // } |
---|
| 74 | // } |
---|
| 75 | |
---|
| 76 | // { |
---|
| 77 | // std::cout << "wrapped preflow ..." << std::endl; |
---|
| 78 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 79 | // ts.reset(); |
---|
| 80 | // pre_flow_res.run(); |
---|
| 81 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 82 | // std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl; |
---|
| 83 | // } |
---|
| 84 | |
---|
| 85 | { |
---|
| 86 | std::cout << "physical blocking flow augmentation ..." << std::endl; |
---|
[1015] | 87 | for (EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0); |
---|
[764] | 88 | ts.reset(); |
---|
| 89 | int i=0; |
---|
| 90 | while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; } |
---|
| 91 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 92 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 93 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
| 94 | |
---|
[1015] | 95 | for (EdgeIt e(g); e!=INVALID; ++e) { |
---|
[986] | 96 | if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 97 | std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 98 | if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 99 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | // { |
---|
| 104 | // std::cout << "faster physical blocking flow augmentation ..." << std::endl; |
---|
| 105 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 106 | // ts.reset(); |
---|
| 107 | // int i=0; |
---|
| 108 | // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; } |
---|
| 109 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 110 | // std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 111 | // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
| 112 | // } |
---|
| 113 | |
---|
| 114 | { |
---|
| 115 | std::cout << "on-the-fly blocking flow augmentation ..." << std::endl; |
---|
[1015] | 116 | for (EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0); |
---|
[764] | 117 | ts.reset(); |
---|
| 118 | int i=0; |
---|
| 119 | while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; } |
---|
| 120 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 121 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 122 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
| 123 | |
---|
[1015] | 124 | for (EdgeIt e(g); e!=INVALID; ++e) { |
---|
[986] | 125 | if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 126 | std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 127 | if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 128 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // { |
---|
| 133 | // std::cout << "on-the-fly shortest path augmentation ..." << std::endl; |
---|
| 134 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 135 | // ts.reset(); |
---|
| 136 | // int i=0; |
---|
| 137 | // while (augmenting_flow_test.augmentOnShortestPath()) { ++i; } |
---|
| 138 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 139 | // std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 140 | // std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
| 141 | |
---|
| 142 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
[986] | 143 | // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 144 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 145 | // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 146 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
| 147 | // } |
---|
| 148 | // } |
---|
| 149 | |
---|
| 150 | // { |
---|
| 151 | // std::cout << "on-the-fly shortest path augmentation ..." << std::endl; |
---|
| 152 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 153 | // ts.reset(); |
---|
| 154 | // int i=0; |
---|
| 155 | // while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; } |
---|
| 156 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 157 | // std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 158 | // std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
| 159 | |
---|
| 160 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
[986] | 161 | // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) |
---|
[764] | 162 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
[986] | 163 | // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) |
---|
[764] | 164 | // std::cout << "Slackness does not hold!" << std::endl; |
---|
| 165 | // } |
---|
| 166 | // } |
---|
| 167 | |
---|
| 168 | ts.reset(); |
---|
[1015] | 169 | |
---|
| 170 | Edge e=g.addEdge(t, s); |
---|
| 171 | Graph::EdgeMap<int> cost(g, 0); |
---|
| 172 | cost.set(e, -1); |
---|
| 173 | cap.set(e, 10000); |
---|
| 174 | typedef ConstMap<Node, int> Excess; |
---|
| 175 | Excess excess(0); |
---|
| 176 | typedef ConstMap<Edge, int> LCap; |
---|
| 177 | LCap lcap(0); |
---|
| 178 | |
---|
| 179 | MinCostGenFlow<Graph, int, Excess, LCap> |
---|
| 180 | min_cost(g, excess, lcap, cap, flow, cost); |
---|
[1025] | 181 | min_cost.feasible(); |
---|
[1031] | 182 | min_cost.runByLP(); |
---|
[1015] | 183 | |
---|
[764] | 184 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[1015] | 185 | std::cout << "flow value: "<< flow[e] << std::endl; |
---|
[764] | 186 | } |
---|