[410] | 1 | // -*- c++ -*- |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <fstream> |
---|
| 4 | #include <vector> |
---|
| 5 | #include <cstdlib> |
---|
| 6 | |
---|
| 7 | #include <list_graph.h> |
---|
| 8 | //#include <smart_graph.h> |
---|
| 9 | //#include <dimacs.h> |
---|
| 10 | #include <time_measure.h> |
---|
| 11 | #include <for_each_macros.h> |
---|
| 12 | #include <bfs_iterator.h> |
---|
| 13 | #include <graph_wrapper.h> |
---|
| 14 | #include <maps.h> |
---|
[480] | 15 | #include <max_flow.h> |
---|
[410] | 16 | |
---|
| 17 | /** |
---|
| 18 | * Inicializalja a veletlenszamgeneratort. |
---|
| 19 | * Figyelem, ez nem jo igazi random szamokhoz, |
---|
| 20 | * erre ne bizzad a titkaidat! |
---|
| 21 | */ |
---|
| 22 | void random_init() |
---|
| 23 | { |
---|
| 24 | unsigned int seed = getpid(); |
---|
| 25 | seed |= seed << 15; |
---|
| 26 | seed ^= time(0); |
---|
| 27 | |
---|
| 28 | srand(seed); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Egy veletlen int-et ad vissza 0 es m-1 kozott. |
---|
| 33 | */ |
---|
| 34 | int random(int m) |
---|
| 35 | { |
---|
| 36 | return int( double(m) * rand() / (RAND_MAX + 1.0) ); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | using namespace hugo; |
---|
| 40 | |
---|
| 41 | int main() { |
---|
| 42 | typedef UndirListGraph Graph; |
---|
| 43 | typedef Graph::Node Node; |
---|
| 44 | typedef Graph::NodeIt NodeIt; |
---|
| 45 | typedef Graph::Edge Edge; |
---|
| 46 | typedef Graph::EdgeIt EdgeIt; |
---|
| 47 | typedef Graph::OutEdgeIt OutEdgeIt; |
---|
| 48 | |
---|
| 49 | Graph g; |
---|
| 50 | |
---|
| 51 | std::vector<Graph::Node> s_nodes; |
---|
| 52 | std::vector<Graph::Node> t_nodes; |
---|
| 53 | |
---|
| 54 | int a; |
---|
| 55 | std::cout << "number of nodes in the first color class="; |
---|
| 56 | std::cin >> a; |
---|
| 57 | int b; |
---|
| 58 | std::cout << "number of nodes in the second color class="; |
---|
| 59 | std::cin >> b; |
---|
| 60 | int m; |
---|
| 61 | std::cout << "number of edges="; |
---|
| 62 | std::cin >> m; |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode()); |
---|
| 66 | for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode()); |
---|
| 67 | |
---|
| 68 | random_init(); |
---|
| 69 | for(int i=0; i<m; ++i) { |
---|
| 70 | g.addEdge(s_nodes[random(a)], t_nodes[random(b)]); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | Graph::NodeMap<int> ref_map(g, -1); |
---|
| 74 | |
---|
| 75 | IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map); |
---|
| 76 | for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false); |
---|
| 77 | for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true); |
---|
| 78 | |
---|
| 79 | // Graph::Node u; |
---|
| 80 | // std::cout << "These nodes will be in S:\n"; |
---|
| 81 | // //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen |
---|
| 82 | // //irni 1etlen FOR_EACH-csel. |
---|
| 83 | // for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) |
---|
| 84 | // std::cout << u << " "; |
---|
| 85 | // std::cout << "\n"; |
---|
| 86 | // std::cout << "These nodes will be in T:\n"; |
---|
| 87 | // for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) |
---|
| 88 | // std::cout << u << " "; |
---|
| 89 | // std::cout << "\n"; |
---|
| 90 | |
---|
| 91 | typedef BipartiteGraphWrapper<Graph> BGW; |
---|
| 92 | BGW bgw(g, bipartite_map); |
---|
| 93 | |
---|
| 94 | // std::cout << "Nodes by NodeIt:\n"; |
---|
| 95 | // FOR_EACH_LOC(BGW::NodeIt, n, bgw) { |
---|
| 96 | // std::cout << n << " "; |
---|
| 97 | // } |
---|
| 98 | // std::cout << "\n"; |
---|
| 99 | // std::cout << "Nodes in S by ClassNodeIt:\n"; |
---|
| 100 | // FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) { |
---|
| 101 | // std::cout << n << " "; |
---|
| 102 | // } |
---|
| 103 | // std::cout << "\n"; |
---|
| 104 | // std::cout << "Nodes in T by ClassNodeIt:\n"; |
---|
| 105 | // FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) { |
---|
| 106 | // std::cout << n << " "; |
---|
| 107 | // } |
---|
| 108 | // std::cout << "\n"; |
---|
| 109 | // std::cout << "Edges of the bipartite graph:\n"; |
---|
| 110 | // FOR_EACH_LOC(BGW::EdgeIt, e, bgw) { |
---|
| 111 | // std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl; |
---|
| 112 | // } |
---|
| 113 | |
---|
| 114 | BGW::NodeMap<int> dbyj(bgw); |
---|
| 115 | BGW::EdgeMap<int> dbyxcj(bgw); |
---|
| 116 | |
---|
| 117 | typedef stGraphWrapper<BGW> stGW; |
---|
| 118 | stGW stgw(bgw); |
---|
| 119 | ConstMap<stGW::Edge, int> const1map(1); |
---|
| 120 | // stGW::NodeMap<int> ize(stgw); |
---|
| 121 | |
---|
| 122 | // BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw); |
---|
| 123 | // Graph::NodeIt si; |
---|
| 124 | // Graph::Node s; |
---|
| 125 | // s=g.first(si); |
---|
| 126 | // bfs.pushAndSetReached(BGW::Node(s)); |
---|
| 127 | // while (!bfs.finished()) { ++bfs; } |
---|
| 128 | |
---|
| 129 | // FOR_EACH_LOC(stGW::NodeIt, n, stgw) { |
---|
| 130 | // std::cout << "out-edges of " << n << ":\n"; |
---|
| 131 | // FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { |
---|
| 132 | // std::cout << " " << e << "\n"; |
---|
| 133 | // std::cout << " aNode: " << stgw.aNode(e) << "\n"; |
---|
| 134 | // std::cout << " bNode: " << stgw.bNode(e) << "\n"; |
---|
| 135 | // } |
---|
| 136 | // std::cout << "in-edges of " << n << ":\n"; |
---|
| 137 | // FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { |
---|
| 138 | // std::cout << " " << e << "\n"; |
---|
| 139 | // std::cout << " aNode: " << stgw.aNode(e) << "\n"; |
---|
| 140 | // std::cout << " bNode: " << stgw.bNode(e) << "\n"; |
---|
| 141 | // } |
---|
| 142 | // } |
---|
| 143 | // std::cout << "Edges of the stGraphWrapper:\n"; |
---|
| 144 | // FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { |
---|
| 145 | // std::cout << " " << n << "\n"; |
---|
| 146 | // } |
---|
| 147 | |
---|
| 148 | // stGW::NodeMap<bool> b(stgw); |
---|
| 149 | // FOR_EACH_LOC(stGW::NodeIt, n, stgw) { |
---|
| 150 | // std::cout << n << ": " << b[n] <<"\n"; |
---|
| 151 | // } |
---|
| 152 | |
---|
| 153 | // std::cout << "Bfs from s: \n"; |
---|
| 154 | // BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw); |
---|
| 155 | // bfs_stgw.pushAndSetReached(stgw.S_NODE); |
---|
| 156 | // while (!bfs_stgw.finished()) { |
---|
| 157 | // std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n"; |
---|
| 158 | // ++bfs_stgw; |
---|
| 159 | // } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | Timer ts; |
---|
| 163 | ts.reset(); |
---|
| 164 | stGW::EdgeMap<int> max_flow(stgw); |
---|
| 165 | MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > |
---|
| 166 | max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow); |
---|
| 167 | // while (max_flow_test.augmentOnShortestPath()) { } |
---|
| 168 | typedef ListGraph MutableGraph; |
---|
| 169 | // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { |
---|
| 170 | while (max_flow_test.augmentOnBlockingFlow2()) { |
---|
| 171 | std::cout << max_flow_test.flowValue() << std::endl; |
---|
| 172 | } |
---|
| 173 | std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl; |
---|
| 174 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 175 | // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { |
---|
| 176 | // std::cout << e << ": " << max_flow[e] << "\n"; |
---|
| 177 | // } |
---|
| 178 | // std::cout << "\n"; |
---|
| 179 | |
---|
| 180 | ts.reset(); |
---|
| 181 | stGW::EdgeMap<int> pre_flow(stgw); |
---|
[476] | 182 | MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > |
---|
[465] | 183 | pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/); |
---|
[410] | 184 | pre_flow_test.run(); |
---|
| 185 | std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl; |
---|
| 186 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 187 | // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { |
---|
| 188 | // std::cout << e << ": " << pre_flow[e] << "\n"; |
---|
| 189 | // } |
---|
| 190 | // std::cout << "\n"; |
---|
| 191 | |
---|
| 192 | return 0; |
---|
| 193 | } |
---|