[192] | 1 | // -*- c++ -*- |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <fstream> |
---|
| 4 | #include <vector> |
---|
| 5 | #include <cstdlib> |
---|
| 6 | |
---|
| 7 | #include <LEDA/graph.h> |
---|
| 8 | #include <leda_graph_wrapper.h> |
---|
| 9 | #include <dimacs.h> |
---|
| 10 | #include <time_measure.h> |
---|
| 11 | #include <edmonds_karp.h> |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Inicializalja a veletlenszamgeneratort. |
---|
| 15 | * Figyelem, ez nem jo igazi random szamokhoz, |
---|
| 16 | * erre ne bizzad a titkaidat! |
---|
| 17 | */ |
---|
| 18 | void random_init() |
---|
| 19 | { |
---|
| 20 | unsigned int seed = getpid(); |
---|
| 21 | seed |= seed << 15; |
---|
| 22 | seed ^= time(0); |
---|
| 23 | |
---|
| 24 | srand(seed); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Egy veletlen int-et ad vissza 0 es m-1 kozott. |
---|
| 29 | */ |
---|
| 30 | int random(int m) |
---|
| 31 | { |
---|
| 32 | return int( double(m) * rand() / (RAND_MAX + 1.0) ); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | using namespace hugo; |
---|
| 36 | |
---|
| 37 | using std::cout; |
---|
| 38 | using std::endl; |
---|
| 39 | |
---|
| 40 | int main() { |
---|
| 41 | leda::graph g; |
---|
| 42 | typedef LedaGraphWrapper<leda::graph> Graph; |
---|
| 43 | Graph G(g); |
---|
| 44 | |
---|
| 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 | typedef Graph::InEdgeIt InEdgeIt; |
---|
| 51 | |
---|
[194] | 52 | //Node s, t; |
---|
[192] | 53 | //Graph::EdgeMap<int> cap(G); |
---|
| 54 | //readDimacsMaxFlow(std::cin, G, s, t, cap); |
---|
| 55 | std::vector<Node> s_nodes; |
---|
| 56 | std::vector<Node> t_nodes; |
---|
| 57 | |
---|
[195] | 58 | for(int i=0; i<4; ++i) { |
---|
[192] | 59 | s_nodes.push_back(G.addNode()); |
---|
| 60 | } |
---|
[195] | 61 | for(int i=0; i<4; ++i) { |
---|
[192] | 62 | t_nodes.push_back(G.addNode()); |
---|
| 63 | } |
---|
[195] | 64 | // random_init(); |
---|
| 65 | // for(int i=0; i<6; ++i) { |
---|
| 66 | // G.addEdge(s_nodes[random(4)], t_nodes[random(4)]); |
---|
| 67 | // } |
---|
| 68 | |
---|
| 69 | G.addEdge(s_nodes[2], t_nodes[4-4]); |
---|
| 70 | G.addEdge(s_nodes[2], t_nodes[7-4]); |
---|
| 71 | G.addEdge(s_nodes[2], t_nodes[4-4]); |
---|
| 72 | G.addEdge(s_nodes[3], t_nodes[6-4]); |
---|
| 73 | G.addEdge(s_nodes[3], t_nodes[5-4]); |
---|
| 74 | G.addEdge(s_nodes[3], t_nodes[5-4]); |
---|
| 75 | |
---|
| 76 | |
---|
[194] | 77 | Graph::NodeMap<bool> s_map(G); //false |
---|
| 78 | Graph::NodeMap<bool> t_map(G); //false |
---|
[192] | 79 | |
---|
[195] | 80 | for(int i=0; i<4; ++i) { |
---|
[192] | 81 | s_map.set(s_nodes[i], true); |
---|
| 82 | t_map.set(t_nodes[i], true); |
---|
| 83 | } |
---|
| 84 | |
---|
[195] | 85 | cout << "bfs and dfs iterator demo on the directed graph" << endl; |
---|
| 86 | for(NodeIt n=G.first<NodeIt>(); G.valid(n); G.next(n)) { |
---|
| 87 | cout << G.id(n) << ": "; |
---|
| 88 | cout << "out edges: "; |
---|
| 89 | for(OutEdgeIt e=G.first<OutEdgeIt>(n); G.valid(e); G.next(e)) |
---|
| 90 | cout << G.id(G.tail(e)) << "->" << G.id(G.head(e)) << " "; |
---|
| 91 | cout << "in edges: "; |
---|
| 92 | for(InEdgeIt e=G.first<InEdgeIt>(n); G.valid(e); G.next(e)) |
---|
| 93 | cout << G.id(G.tail(e)) << "->" << G.id(G.head(e)) << " "; |
---|
| 94 | cout << endl; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[192] | 98 | { |
---|
| 99 | std::cout << "on-the-fly max bipartite matching demo on wrapped leda graph..." << std::endl; |
---|
| 100 | Graph::EdgeMap<int> flow(G); //0 flow |
---|
[194] | 101 | Graph::EdgeMap<int> cap(G, 1); |
---|
[192] | 102 | |
---|
| 103 | Timer ts; |
---|
| 104 | ts.reset(); |
---|
| 105 | |
---|
| 106 | MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap); |
---|
| 107 | //max_flow_test.augmentWithBlockingFlow<Graph>(); |
---|
| 108 | int i=0; |
---|
| 109 | while (max_flow_test.augmentOnShortestPath()) { |
---|
| 110 | // for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { |
---|
| 111 | // std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") "; |
---|
| 112 | // } |
---|
| 113 | // std::cout<<std::endl; |
---|
| 114 | ++i; |
---|
| 115 | } |
---|
| 116 | |
---|
[195] | 117 | std::cout << "maximum matching: "<< std::endl; |
---|
| 118 | for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) |
---|
| 119 | if (flow.get(e)) |
---|
| 120 | std::cout << G.id(G.tail(e)) << "-" << flow.get(e) << "->" << G.id(G.head(e)) << " "; |
---|
| 121 | std::cout<<std::endl; |
---|
| 122 | std::cout << "edges which are not in this maximum matching: "<< std::endl; |
---|
| 123 | for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) |
---|
| 124 | if (!flow.get(e)) |
---|
| 125 | std::cout << G.id(G.tail(e)) << "-" << flow.get(e) << "->" << G.id(G.head(e)) << " "; |
---|
| 126 | std::cout<<std::endl; |
---|
| 127 | |
---|
[192] | 128 | std::cout << "elapsed time: " << ts << std::endl; |
---|
| 129 | std::cout << "number of augmentation phases: " << i << std::endl; |
---|
| 130 | std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | return 0; |
---|
| 135 | } |
---|