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