[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> |
---|
[496] | 13 | #include <bipartite_graph_wrapper.h> |
---|
[410] | 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() { |
---|
[499] | 42 | //typedef UndirListGraph Graph; |
---|
| 43 | typedef BipartiteGraph<ListGraph> Graph; |
---|
| 44 | |
---|
[410] | 45 | typedef Graph::Node Node; |
---|
| 46 | typedef Graph::NodeIt NodeIt; |
---|
| 47 | typedef Graph::Edge Edge; |
---|
| 48 | typedef Graph::EdgeIt EdgeIt; |
---|
| 49 | typedef Graph::OutEdgeIt OutEdgeIt; |
---|
| 50 | |
---|
| 51 | Graph g; |
---|
| 52 | |
---|
| 53 | std::vector<Graph::Node> s_nodes; |
---|
| 54 | std::vector<Graph::Node> t_nodes; |
---|
| 55 | |
---|
| 56 | int a; |
---|
| 57 | std::cout << "number of nodes in the first color class="; |
---|
| 58 | std::cin >> a; |
---|
| 59 | int b; |
---|
| 60 | std::cout << "number of nodes in the second color class="; |
---|
| 61 | std::cin >> b; |
---|
| 62 | int m; |
---|
| 63 | std::cout << "number of edges="; |
---|
| 64 | std::cin >> m; |
---|
| 65 | |
---|
[500] | 66 | std::cout << "Generatig a random bipartite graph..." << std::endl; |
---|
[499] | 67 | for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false)); |
---|
| 68 | for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true)); |
---|
[410] | 69 | |
---|
| 70 | random_init(); |
---|
| 71 | for(int i=0; i<m; ++i) { |
---|
| 72 | g.addEdge(s_nodes[random(a)], t_nodes[random(b)]); |
---|
| 73 | } |
---|
| 74 | |
---|
[499] | 75 | std::cout << "Edges of the bipartite graph:" << std::endl; |
---|
| 76 | FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " "; |
---|
| 77 | std::cout << std::endl; |
---|
[410] | 78 | |
---|
[500] | 79 | std::cout << "Nodes in T:" << std::endl; |
---|
| 80 | FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.T_CLASS) std::cout << v << " "; |
---|
| 81 | std::cout << std::endl; |
---|
| 82 | std::cout << "Nodes in S:" << std::endl; |
---|
| 83 | FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.S_CLASS) std::cout << v << " "; |
---|
| 84 | std::cout << std::endl; |
---|
| 85 | |
---|
| 86 | std::cout << "Erasing the first node..." << std::endl; |
---|
| 87 | NodeIt n; |
---|
| 88 | g.first(n); |
---|
| 89 | g.erase(n); |
---|
| 90 | std::cout << "Nodes of the bipartite graph:" << std::endl; |
---|
| 91 | FOR_EACH_GLOB(n, g) std::cout << n << " "; |
---|
| 92 | std::cout << std::endl; |
---|
| 93 | |
---|
| 94 | std::cout << "Nodes in T:" << std::endl; |
---|
| 95 | FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.T_CLASS) std::cout << v << " "; |
---|
| 96 | std::cout << std::endl; |
---|
| 97 | std::cout << "Nodes in S:" << std::endl; |
---|
| 98 | FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.S_CLASS) std::cout << v << " "; |
---|
| 99 | std::cout << std::endl; |
---|
| 100 | |
---|
[499] | 101 | typedef stGraphWrapper<Graph> stGW; |
---|
| 102 | stGW stgw(g); |
---|
| 103 | ConstMap<stGW::Edge, int> const1map(1); |
---|
[410] | 104 | |
---|
| 105 | Timer ts; |
---|
| 106 | ts.reset(); |
---|
[499] | 107 | stGW::EdgeMap<int> flow(stgw); |
---|
[410] | 108 | MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > |
---|
[499] | 109 | max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow); |
---|
[500] | 110 | max_flow_test.run(); |
---|
[410] | 111 | // while (max_flow_test.augmentOnShortestPath()) { } |
---|
[500] | 112 | // typedef ListGraph MutableGraph; |
---|
[410] | 113 | // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { |
---|
[500] | 114 | // while (max_flow_test.augmentOnBlockingFlow2()) { |
---|
| 115 | // std::cout << max_flow_test.flowValue() << std::endl; |
---|
| 116 | // } |
---|
[410] | 117 | std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl; |
---|
| 118 | std::cout << "elapsed time: " << ts << std::endl; |
---|
[499] | 119 | FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { |
---|
| 120 | if (flow[e]) std::cout << e << std::endl; |
---|
| 121 | } |
---|
| 122 | std::cout << std::endl; |
---|
[410] | 123 | |
---|
| 124 | return 0; |
---|
| 125 | } |
---|