7 #include <list_graph.h>
8 //#include <smart_graph.h>
10 #include <time_measure.h>
11 #include <for_each_macros.h>
12 #include <bfs_iterator.h>
13 #include <bipartite_graph_wrapper.h>
19 // template <typename Graph, typename EdgeCap, typename NodeCap,
20 // typename EdgeFlow, typename NodeFlow>
21 // class MaxMatching : public MaxFlow<stGraphWrapper<Graph>,
22 // stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
23 // typedef MaxFlow<stGraphWrapper<Graph>,
24 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>,
25 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
28 // stGraphWrapper<Graph> gw;
29 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
30 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
32 // //EdgeCap* edge_cap;
33 // //EdgeFlow* edge_flow;
35 // MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
36 // EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
38 // cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
39 // Parent::set(gw, cap, flow);
43 template <typename Graph, typename EdgeCap, typename NodeCap,
44 typename EdgeFlow, typename NodeFlow>
49 // EdgeFlow* edge_flow;
50 // NodeFlow* node_flow;
51 typedef stGraphWrapper<Graph> stGW;
53 typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
56 typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
58 MaxFlow<stGW, int, CapMap, FlowMap> mf;
61 //EdgeFlow* edge_flow;
63 MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
64 EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
66 cap(_edge_cap, _node_cap),
68 flow(_edge_flow, _node_flow),
69 mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
70 MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
71 EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) :
73 cap(_edge_cap, _node_cap),
74 node_flow(new NodeFlow(_g)),
75 flow(_edge_flow, *node_flow),
76 mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
77 ~MaxMatching() { if (node_flow) delete node_flow; }
78 void run() { mf.run(); }
79 int matchingValue() { return mf.flowValue(); }
83 * Inicializalja a veletlenszamgeneratort.
84 * Figyelem, ez nem jo igazi random szamokhoz,
85 * erre ne bizzad a titkaidat!
89 unsigned int seed = getpid();
97 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
101 return int( double(m) * rand() / (RAND_MAX + 1.0) );
105 //typedef UndirListGraph Graph;
106 typedef BipartiteGraph<ListGraph> Graph;
108 typedef Graph::Node Node;
109 typedef Graph::NodeIt NodeIt;
110 typedef Graph::Edge Edge;
111 typedef Graph::EdgeIt EdgeIt;
112 typedef Graph::OutEdgeIt OutEdgeIt;
116 std::vector<Graph::Node> s_nodes;
117 std::vector<Graph::Node> t_nodes;
120 std::cout << "number of nodes in the first color class=";
123 std::cout << "number of nodes in the second color class=";
126 std::cout << "number of edges=";
129 std::cout << "Generatig a random bipartite graph..." << std::endl;
130 for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
131 for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
134 for(int i=0; i<m; ++i) {
135 g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
138 // std::cout << "Edges of the bipartite graph:" << std::endl;
139 // FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
140 // std::cout << std::endl;
142 // std::cout << "Nodes:" << std::endl;
143 // FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
144 // std::cout << std::endl;
145 // std::cout << "Nodes in T:" << std::endl;
146 // FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
147 // std::cout << std::endl;
148 // std::cout << "Nodes in S:" << std::endl;
149 // FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
150 // std::cout << std::endl;
152 // std::cout << "Erasing the first node..." << std::endl;
156 // std::cout << "Nodes of the bipartite graph:" << std::endl;
157 // FOR_EACH_GLOB(n, g) std::cout << n << " ";
158 // std::cout << std::endl;
160 // std::cout << "Nodes in T:" << std::endl;
161 // FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
162 // std::cout << std::endl;
163 // std::cout << "Nodes in S:" << std::endl;
164 // FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
165 // std::cout << std::endl;
167 typedef stGraphWrapper<Graph> stGW;
169 ConstMap<stGW::Edge, int> const1map(1);
173 stGW::EdgeMap<int> flow(stgw);
174 MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
175 max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
177 // while (max_flow_test.augmentOnShortestPath()) { }
178 // typedef ListGraph MutableGraph;
179 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
180 // while (max_flow_test.augmentOnBlockingFlow2()) {
181 // std::cout << max_flow_test.flowValue() << std::endl;
183 std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
184 std::cout << "elapsed time: " << ts << std::endl;
185 // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
186 // if (flow[e]) std::cout << e << std::endl;
188 std::cout << std::endl;
190 typedef ConstMap<Graph::Edge, int> EdgeCap;
192 typedef ConstMap<Graph::Node, int> NodeCap;
194 typedef Graph::EdgeMap<int> EdgeFlow;
196 typedef Graph::NodeMap<int> NodeFlow;
199 typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
200 typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
202 FlowMap fm(gef, gnf);
206 //stGW::EdgeMap<int> flow(stgw);
207 MaxFlow<stGW, int, CapMap, FlowMap>
208 max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
209 max_flow_test1.run();
210 // while (max_flow_test.augmentOnShortestPath()) { }
211 // typedef ListGraph MutableGraph;
212 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
213 // while (max_flow_test.augmentOnBlockingFlow2()) {
214 // std::cout << max_flow_test.flowValue() << std::endl;
216 std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl;
217 std::cout << "elapsed time: " << ts << std::endl;
218 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
219 // if (gef[e]) std::cout << e << std::endl;
221 std::cout << std::endl;
224 FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0);
225 FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0);
226 MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>,
227 Graph::EdgeMap<int>, Graph::NodeMap<int> >
228 matching_test(g, ge1, gn1, gef, gnf);
231 std::cout << "max flow value: " << matching_test.matchingValue() << std::endl;
232 std::cout << "elapsed time: " << ts << std::endl;
233 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
234 // if (gef[e]) std::cout << e << std::endl;
236 std::cout << std::endl;
239 FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0);
240 //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0);
241 MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>,
242 Graph::EdgeMap<int>, Graph::NodeMap<int> >
243 matching_test_1(g, ge1, gn1, gef/*, gnf*/);
244 matching_test_1.run();
246 std::cout << "max flow value: " << matching_test_1.matchingValue() << std::endl;
247 std::cout << "elapsed time: " << ts << std::endl;
248 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
249 // if (gef[e]) std::cout << e << std::endl;
251 std::cout << std::endl;