getPath() function implemented.
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>
46 // : public MaxFlow<stGraphWrapper<Graph>,
47 // stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
48 // typedef MaxFlow<stGraphWrapper<Graph>,
49 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>,
50 // stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
55 // EdgeFlow* edge_flow;
56 // NodeFlow* node_flow;
57 typedef stGraphWrapper<Graph> stGW;
59 typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
61 typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
63 MaxFlow<stGW, int, CapMap, FlowMap> mf;
66 //EdgeFlow* edge_flow;
68 MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
69 EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
71 cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow),
72 mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
73 void run() { mf.run(); }
74 int matchingValue() { return mf.flowValue(); }
78 * Inicializalja a veletlenszamgeneratort.
79 * Figyelem, ez nem jo igazi random szamokhoz,
80 * erre ne bizzad a titkaidat!
84 unsigned int seed = getpid();
92 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
96 return int( double(m) * rand() / (RAND_MAX + 1.0) );
100 //typedef UndirListGraph Graph;
101 typedef BipartiteGraph<ListGraph> Graph;
103 typedef Graph::Node Node;
104 typedef Graph::NodeIt NodeIt;
105 typedef Graph::Edge Edge;
106 typedef Graph::EdgeIt EdgeIt;
107 typedef Graph::OutEdgeIt OutEdgeIt;
111 std::vector<Graph::Node> s_nodes;
112 std::vector<Graph::Node> t_nodes;
115 std::cout << "number of nodes in the first color class=";
118 std::cout << "number of nodes in the second color class=";
121 std::cout << "number of edges=";
124 std::cout << "Generatig a random bipartite graph..." << std::endl;
125 for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
126 for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
129 for(int i=0; i<m; ++i) {
130 g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
133 std::cout << "Edges of the bipartite graph:" << std::endl;
134 FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
135 std::cout << std::endl;
137 std::cout << "Nodes:" << std::endl;
138 FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
139 std::cout << std::endl;
140 std::cout << "Nodes in T:" << std::endl;
141 FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
142 std::cout << std::endl;
143 std::cout << "Nodes in S:" << std::endl;
144 FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
145 std::cout << std::endl;
147 std::cout << "Erasing the first node..." << std::endl;
151 std::cout << "Nodes of the bipartite graph:" << std::endl;
152 FOR_EACH_GLOB(n, g) std::cout << n << " ";
153 std::cout << std::endl;
155 std::cout << "Nodes in T:" << std::endl;
156 FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
157 std::cout << std::endl;
158 std::cout << "Nodes in S:" << std::endl;
159 FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
160 std::cout << std::endl;
162 typedef stGraphWrapper<Graph> stGW;
164 ConstMap<stGW::Edge, int> const1map(1);
168 stGW::EdgeMap<int> flow(stgw);
169 MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
170 max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
172 // while (max_flow_test.augmentOnShortestPath()) { }
173 // typedef ListGraph MutableGraph;
174 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
175 // while (max_flow_test.augmentOnBlockingFlow2()) {
176 // std::cout << max_flow_test.flowValue() << std::endl;
178 std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
179 std::cout << "elapsed time: " << ts << std::endl;
180 FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
181 if (flow[e]) std::cout << e << std::endl;
183 std::cout << std::endl;
185 typedef ConstMap<Graph::Edge, int> EdgeCap;
187 typedef ConstMap<Graph::Node, int> NodeCap;
189 typedef Graph::EdgeMap<int> EdgeFlow;
191 typedef Graph::NodeMap<int> NodeFlow;
194 typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
195 typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
197 FlowMap fm(gef, gnf);
201 //stGW::EdgeMap<int> flow(stgw);
202 MaxFlow<stGW, int, CapMap, FlowMap>
203 max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
204 max_flow_test1.run();
205 // while (max_flow_test.augmentOnShortestPath()) { }
206 // typedef ListGraph MutableGraph;
207 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
208 // while (max_flow_test.augmentOnBlockingFlow2()) {
209 // std::cout << max_flow_test.flowValue() << std::endl;
211 std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl;
212 std::cout << "elapsed time: " << ts << std::endl;
213 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
214 // if (gef[e]) std::cout << e << std::endl;
216 std::cout << std::endl;
219 FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0);
220 FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0);
221 MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>,
222 Graph::EdgeMap<int>, Graph::NodeMap<int> >
223 matching_test(g, ge1, gn1, gef, gnf);
226 std::cout << "max flow value: " << matching_test.matchingValue() << std::endl;
227 std::cout << "elapsed time: " << ts << std::endl;
228 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
229 // if (gef[e]) std::cout << e << std::endl;
231 std::cout << std::endl;