[475] | 1 | // -*- c++ -*- |
---|
[777] | 2 | |
---|
| 3 | // Use a DIMACS max flow file as stdin. |
---|
| 4 | // max_flow_demo < dimacs_max_flow_file |
---|
| 5 | |
---|
| 6 | |
---|
[475] | 7 | #include <iostream> |
---|
| 8 | #include <fstream> |
---|
| 9 | |
---|
[642] | 10 | #include <sage_graph.h> |
---|
[555] | 11 | #include <hugo/smart_graph.h> |
---|
| 12 | #include <hugo/dimacs.h> |
---|
| 13 | #include <hugo/time_measure.h> |
---|
[475] | 14 | //#include <graph_wrapper.h> |
---|
[849] | 15 | #include <hugo/preflow.h> |
---|
[762] | 16 | #include <augmenting_flow.h> |
---|
[475] | 17 | //#include <preflow_res.h> |
---|
[762] | 18 | #include <for_each_macros.h> |
---|
[651] | 19 | #include <graph_concept.h> |
---|
[475] | 20 | |
---|
| 21 | using namespace hugo; |
---|
| 22 | |
---|
| 23 | int main(int, char **) { |
---|
| 24 | |
---|
[642] | 25 | typedef SageGraph MutableGraph; |
---|
[475] | 26 | |
---|
[651] | 27 | //typedef FullFeatureGraphConcept Graph; |
---|
[762] | 28 | //typedef SmartGraph Graph; |
---|
| 29 | typedef SageGraph Graph; |
---|
[475] | 30 | typedef Graph::Node Node; |
---|
| 31 | typedef Graph::EdgeIt EdgeIt; |
---|
| 32 | |
---|
[577] | 33 | Graph g; |
---|
[652] | 34 | |
---|
[475] | 35 | Node s, t; |
---|
[577] | 36 | Graph::EdgeMap<int> cap(g); |
---|
| 37 | //readDimacsMaxFlow(std::cin, g, s, t, cap); |
---|
| 38 | readDimacs(std::cin, g, cap, s, t); |
---|
[475] | 39 | Timer ts; |
---|
[577] | 40 | Graph::EdgeMap<int> flow(g); //0 flow |
---|
[849] | 41 | Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > |
---|
[577] | 42 | max_flow_test(g, s, t, cap, flow); |
---|
[762] | 43 | AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > |
---|
| 44 | augmenting_flow_test(g, s, t, cap, flow); |
---|
| 45 | |
---|
[646] | 46 | Graph::NodeMap<bool> cut(g); |
---|
[475] | 47 | |
---|
| 48 | { |
---|
| 49 | std::cout << "preflow ..." << std::endl; |
---|
| 50 | ts.reset(); |
---|
[476] | 51 | max_flow_test.run(); |
---|
[475] | 52 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[476] | 53 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[849] | 54 | max_flow_test.minCut(cut); |
---|
[646] | 55 | |
---|
| 56 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 57 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 58 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 59 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 60 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 61 | } |
---|
[475] | 62 | } |
---|
| 63 | |
---|
| 64 | { |
---|
| 65 | std::cout << "preflow ..." << std::endl; |
---|
[577] | 66 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 67 | ts.reset(); |
---|
[849] | 68 | max_flow_test.preflow(Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW); |
---|
[475] | 69 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[476] | 70 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
[646] | 71 | |
---|
| 72 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 73 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 74 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 75 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 76 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 77 | } |
---|
[475] | 78 | } |
---|
| 79 | |
---|
| 80 | // { |
---|
| 81 | // std::cout << "wrapped preflow ..." << std::endl; |
---|
[577] | 82 | // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 83 | // ts.reset(); |
---|
| 84 | // pre_flow_res.run(); |
---|
| 85 | // std::cout << "elapsed time: " << ts << std::endl; |
---|
| 86 | // std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl; |
---|
| 87 | // } |
---|
| 88 | |
---|
| 89 | { |
---|
| 90 | std::cout << "physical blocking flow augmentation ..." << std::endl; |
---|
[577] | 91 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 92 | ts.reset(); |
---|
| 93 | int i=0; |
---|
[762] | 94 | while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; } |
---|
[475] | 95 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 96 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[762] | 97 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
[646] | 98 | |
---|
| 99 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 100 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 101 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 102 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 103 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 104 | } |
---|
[475] | 105 | } |
---|
| 106 | |
---|
[777] | 107 | { |
---|
| 108 | std::cout << "on-the-fly blocking flow augmentation ..." << std::endl; |
---|
| 109 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 110 | ts.reset(); |
---|
| 111 | int i=0; |
---|
| 112 | while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; } |
---|
| 113 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 114 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 115 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
[775] | 116 | |
---|
[777] | 117 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 118 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 119 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 120 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 121 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 122 | } |
---|
| 123 | } |
---|
[475] | 124 | |
---|
| 125 | { |
---|
| 126 | std::cout << "on-the-fly shortest path augmentation ..." << std::endl; |
---|
[577] | 127 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
[475] | 128 | ts.reset(); |
---|
| 129 | int i=0; |
---|
[762] | 130 | while (augmenting_flow_test.augmentOnShortestPath()) { ++i; } |
---|
[475] | 131 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 132 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[762] | 133 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
[646] | 134 | |
---|
| 135 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 136 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 137 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 138 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 139 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 140 | } |
---|
[475] | 141 | } |
---|
| 142 | |
---|
[646] | 143 | { |
---|
| 144 | std::cout << "on-the-fly shortest path augmentation ..." << std::endl; |
---|
| 145 | FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0); |
---|
| 146 | ts.reset(); |
---|
| 147 | int i=0; |
---|
[762] | 148 | while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; } |
---|
[646] | 149 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 150 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
[762] | 151 | std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl; |
---|
[646] | 152 | |
---|
| 153 | FOR_EACH_LOC(Graph::EdgeIt, e, g) { |
---|
| 154 | if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) |
---|
| 155 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 156 | if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) |
---|
| 157 | std::cout << "Slackness does not hold!" << std::endl; |
---|
| 158 | } |
---|
| 159 | } |
---|
[475] | 160 | |
---|
| 161 | return 0; |
---|
| 162 | } |
---|