[833] | 1 | #include <fstream> |
---|
[858] | 2 | #include <string> |
---|
| 3 | |
---|
[833] | 4 | #include "test_tools.h" |
---|
| 5 | #include <hugo/smart_graph.h> |
---|
| 6 | #include <hugo/dimacs.h> |
---|
| 7 | #include <hugo/preflow.h> |
---|
| 8 | #include <hugo/skeletons/graph.h> |
---|
| 9 | #include <hugo/skeletons/maps.h> |
---|
[855] | 10 | |
---|
[833] | 11 | using namespace hugo; |
---|
| 12 | |
---|
| 13 | void check_Preflow() |
---|
| 14 | { |
---|
| 15 | typedef int VType; |
---|
[880] | 16 | typedef skeleton::StaticGraph Graph; |
---|
[833] | 17 | |
---|
| 18 | typedef Graph::Node Node; |
---|
| 19 | typedef Graph::Edge Edge; |
---|
| 20 | typedef skeleton::ReadMap<Edge,VType> CapMap; |
---|
| 21 | typedef skeleton::ReadWriteMap<Edge,VType> FlowMap; |
---|
| 22 | typedef skeleton::ReadWriteMap<Node,bool> CutMap; |
---|
| 23 | |
---|
| 24 | typedef Preflow<Graph, int, CapMap, FlowMap> PType; |
---|
| 25 | |
---|
| 26 | Graph G; |
---|
| 27 | Node n; |
---|
| 28 | CapMap cap; |
---|
| 29 | FlowMap flow; |
---|
| 30 | CutMap cut; |
---|
| 31 | |
---|
| 32 | PType preflow_test(G,n,n,cap,flow); |
---|
| 33 | |
---|
| 34 | preflow_test.run(); |
---|
| 35 | preflow_test.flowValue(); |
---|
| 36 | preflow_test.setSource(n); |
---|
| 37 | preflow_test.setFlow(flow); |
---|
| 38 | |
---|
| 39 | preflow_test.phase1(PType::NO_FLOW); |
---|
| 40 | preflow_test.minCut(cut); |
---|
| 41 | |
---|
| 42 | preflow_test.phase2(); |
---|
| 43 | preflow_test.setTarget(n); |
---|
| 44 | preflow_test.setCap(cap); |
---|
| 45 | preflow_test.minMinCut(cut); |
---|
| 46 | preflow_test.maxMinCut(cut); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut, |
---|
| 50 | SmartGraph::EdgeMap<int>& cap) { |
---|
| 51 | |
---|
| 52 | int c=0; |
---|
| 53 | for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) { |
---|
| 54 | if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e]; |
---|
| 55 | } |
---|
| 56 | return c; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int main() { |
---|
| 60 | |
---|
| 61 | typedef SmartGraph Graph; |
---|
| 62 | |
---|
[842] | 63 | typedef Graph::Node Node; |
---|
[833] | 64 | typedef Graph::NodeIt NodeIt; |
---|
| 65 | typedef Graph::EdgeIt EdgeIt; |
---|
| 66 | typedef Graph::EdgeMap<int> CapMap; |
---|
| 67 | typedef Graph::EdgeMap<int> FlowMap; |
---|
| 68 | typedef Graph::NodeMap<bool> CutMap; |
---|
| 69 | |
---|
| 70 | typedef Preflow<Graph, int> PType; |
---|
| 71 | |
---|
[859] | 72 | std::string f_name; |
---|
[858] | 73 | if( getenv("srcdir") ) { |
---|
[887] | 74 | f_name = std::string(getenv("srcdir")) + "/preflow_graph.dim"; |
---|
[858] | 75 | } |
---|
| 76 | else { |
---|
[887] | 77 | f_name = "preflow_graph.dim"; |
---|
[858] | 78 | } |
---|
[855] | 79 | |
---|
[858] | 80 | std::ifstream file(f_name.c_str()); |
---|
[855] | 81 | |
---|
[858] | 82 | check(file, "Input file '" << f_name << "' not found."); |
---|
[833] | 83 | |
---|
| 84 | Graph G; |
---|
[842] | 85 | Node s, t; |
---|
[833] | 86 | CapMap cap(G); |
---|
| 87 | readDimacs(file, G, cap, s, t); |
---|
| 88 | |
---|
| 89 | FlowMap flow(G,0); |
---|
[887] | 90 | |
---|
[833] | 91 | |
---|
[887] | 92 | |
---|
[833] | 93 | PType preflow_test(G, s, t, cap, flow); |
---|
| 94 | preflow_test.run(PType::ZERO_FLOW); |
---|
[887] | 95 | |
---|
[833] | 96 | CutMap mincut(G,false); |
---|
| 97 | preflow_test.minCut(mincut); |
---|
| 98 | int min_cut_value=cut_value(G,mincut,cap); |
---|
| 99 | |
---|
| 100 | CutMap minmincut(G,false); |
---|
| 101 | preflow_test.minMinCut(minmincut); |
---|
| 102 | int min_min_cut_value=cut_value(G,minmincut,cap); |
---|
| 103 | |
---|
| 104 | CutMap maxmincut(G,false); |
---|
| 105 | preflow_test.maxMinCut(maxmincut); |
---|
| 106 | int max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
| 107 | |
---|
| 108 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 109 | min_cut_value == min_min_cut_value && |
---|
| 110 | min_min_cut_value == max_min_cut_value, |
---|
| 111 | "The max flow value is not equal to the three min cut values."); |
---|
| 112 | |
---|
| 113 | int flow_value=preflow_test.flowValue(); |
---|
| 114 | |
---|
| 115 | |
---|
[887] | 116 | |
---|
[833] | 117 | for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; |
---|
| 118 | preflow_test.setCap(cap); |
---|
[842] | 119 | |
---|
[833] | 120 | preflow_test.phase1(PType::PRE_FLOW); |
---|
| 121 | |
---|
| 122 | CutMap mincut1(G,false); |
---|
| 123 | preflow_test.minCut(mincut1); |
---|
| 124 | min_cut_value=cut_value(G,mincut1,cap); |
---|
| 125 | |
---|
| 126 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 127 | min_cut_value == 2*flow_value, |
---|
| 128 | "The max flow value or the min cut value is wrong."); |
---|
| 129 | |
---|
| 130 | preflow_test.phase2(); |
---|
| 131 | |
---|
| 132 | CutMap mincut2(G,false); |
---|
| 133 | preflow_test.minCut(mincut2); |
---|
| 134 | min_cut_value=cut_value(G,mincut2,cap); |
---|
| 135 | |
---|
| 136 | CutMap minmincut2(G,false); |
---|
| 137 | preflow_test.minMinCut(minmincut2); |
---|
| 138 | min_min_cut_value=cut_value(G,minmincut2,cap); |
---|
| 139 | |
---|
| 140 | preflow_test.maxMinCut(maxmincut); |
---|
| 141 | max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
| 142 | |
---|
| 143 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 144 | min_cut_value == min_min_cut_value && |
---|
| 145 | min_min_cut_value == max_min_cut_value && |
---|
| 146 | min_cut_value == 2*flow_value, |
---|
| 147 | "The max flow value or the three min cut values were not doubled"); |
---|
| 148 | |
---|
[887] | 149 | |
---|
| 150 | |
---|
[833] | 151 | EdgeIt e(G); |
---|
[887] | 152 | for( int i=1; i==10; ++i ) { |
---|
| 153 | flow.set(e,0); |
---|
[833] | 154 | ++e; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | preflow_test.setFlow(flow); |
---|
[887] | 158 | |
---|
| 159 | NodeIt tmp1(G,s); |
---|
| 160 | ++tmp1; |
---|
| 161 | if ( tmp1 != INVALID ) s=tmp1; |
---|
| 162 | |
---|
| 163 | NodeIt tmp2(G,t); |
---|
| 164 | ++tmp2; |
---|
| 165 | if ( tmp2 != INVALID ) t=tmp2; |
---|
| 166 | |
---|
[833] | 167 | preflow_test.setSource(s); |
---|
[887] | 168 | preflow_test.setTarget(t); |
---|
| 169 | |
---|
[833] | 170 | preflow_test.run(); |
---|
| 171 | |
---|
| 172 | CutMap mincut3(G,false); |
---|
| 173 | preflow_test.minCut(mincut3); |
---|
| 174 | min_cut_value=cut_value(G,mincut3,cap); |
---|
| 175 | |
---|
| 176 | CutMap minmincut3(G,false); |
---|
| 177 | preflow_test.minMinCut(minmincut3); |
---|
| 178 | min_min_cut_value=cut_value(G,minmincut3,cap); |
---|
| 179 | |
---|
| 180 | preflow_test.maxMinCut(maxmincut); |
---|
| 181 | max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
| 182 | |
---|
| 183 | check(preflow_test.flowValue() == min_cut_value && |
---|
| 184 | min_cut_value == min_min_cut_value && |
---|
| 185 | min_min_cut_value == max_min_cut_value, |
---|
| 186 | "The max flow value or the three min cut values are incorrect."); |
---|
| 187 | } |
---|