7 #include <LEDA/graph.h>
8 #include <LEDA/mcb_matching.h>
11 #include <leda_graph_wrapper.h>
12 #include <list_graph.h>
13 //#include <smart_graph.h>
15 #include <time_measure.h>
16 #include <for_each_macros.h>
17 //#include <bfs_iterator.h>
18 #include <graph_wrapper.h>
20 #include <edmonds_karp.h>
24 * Inicializalja a veletlenszamgeneratort.
25 * Figyelem, ez nem jo igazi random szamokhoz,
26 * erre ne bizzad a titkaidat!
30 unsigned int seed = getpid();
38 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
42 return int( double(m) * rand() / (RAND_MAX + 1.0) );
50 //lg.make_undirected();
51 typedef LedaGraphWrapper<leda::graph> Graph;
55 //typedef UndirListGraph Graph;
58 typedef Graph::Node Node;
59 typedef Graph::NodeIt NodeIt;
60 typedef Graph::Edge Edge;
61 typedef Graph::EdgeIt EdgeIt;
62 typedef Graph::OutEdgeIt OutEdgeIt;
64 std::vector<Graph::Node> s_nodes;
65 std::vector<Graph::Node> t_nodes;
68 std::cout << "number of nodes in the first color class=";
71 std::cout << "number of nodes in the second color class=";
74 std::cout << "number of edges=";
78 for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
79 for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
82 for(int i=0; i<m; ++i) {
83 g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
86 Graph::NodeMap<int> ref_map(g, -1);
88 IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
89 for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
90 for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
93 // std::cout << "These nodes will be in S:\n";
94 // //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen
95 // //irni 1etlen FOR_EACH-csel.
96 // for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u))
97 // std::cout << u << " ";
99 // std::cout << "These nodes will be in T:\n";
100 // for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u))
101 // std::cout << u << " ";
102 // std::cout << "\n";
104 typedef BipartiteGraphWrapper<Graph> BGW;
105 BGW bgw(g, bipartite_map);
107 // std::cout << "Nodes by NodeIt:\n";
108 // FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
109 // std::cout << n << " ";
111 // std::cout << "\n";
112 // std::cout << "Nodes in S by ClassNodeIt:\n";
113 // FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
114 // std::cout << n << " ";
116 // std::cout << "\n";
117 // std::cout << "Nodes in T by ClassNodeIt:\n";
118 // FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
119 // std::cout << n << " ";
121 // std::cout << "\n";
122 // std::cout << "Edges of the bipartite graph:\n";
123 // FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
124 // std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
127 BGW::NodeMap<int> dbyj(bgw);
128 BGW::EdgeMap<int> dbyxcj(bgw);
130 typedef stGraphWrapper<BGW> stGW;
132 ConstMap<stGW::Edge, int> const1map(1);
133 // stGW::NodeMap<int> ize(stgw);
135 // BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
139 // bfs.pushAndSetReached(BGW::Node(s));
140 // while (!bfs.finished()) { ++bfs; }
142 // FOR_EACH_LOC(stGW::NodeIt, n, stgw) {
143 // std::cout << "out-edges of " << n << ":\n";
144 // FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) {
145 // std::cout << " " << e << "\n";
146 // std::cout << " aNode: " << stgw.aNode(e) << "\n";
147 // std::cout << " bNode: " << stgw.bNode(e) << "\n";
149 // std::cout << "in-edges of " << n << ":\n";
150 // FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) {
151 // std::cout << " " << e << "\n";
152 // std::cout << " aNode: " << stgw.aNode(e) << "\n";
153 // std::cout << " bNode: " << stgw.bNode(e) << "\n";
156 // std::cout << "Edges of the stGraphWrapper:\n";
157 // FOR_EACH_LOC(stGW::EdgeIt, n, stgw) {
158 // std::cout << " " << n << "\n";
161 // stGW::NodeMap<bool> b(stgw);
162 // FOR_EACH_LOC(stGW::NodeIt, n, stgw) {
163 // std::cout << n << ": " << b[n] <<"\n";
166 // std::cout << "Bfs from s: \n";
167 // BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
168 // bfs_stgw.pushAndSetReached(stgw.S_NODE);
169 // while (!bfs_stgw.finished()) {
170 // std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
177 stGW::EdgeMap<int> max_flow(stgw);
178 MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
179 max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
180 // while (max_flow_test.augmentOnShortestPath()) { }
181 typedef ListGraph MutableGraph;
182 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
183 while (max_flow_test.augmentOnBlockingFlow2()) {
184 std::cout << max_flow_test.flowValue() << std::endl;
186 std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
187 std::cout << "elapsed time: " << ts << std::endl;
188 // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
189 // std::cout << e << ": " << max_flow[e] << "\n";
191 // std::cout << "\n";
194 stGW::EdgeMap<int> pre_flow(stgw);
195 Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
196 pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
198 std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
199 std::cout << "elapsed time: " << ts << std::endl;
200 // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
201 // std::cout << e << ": " << pre_flow[e] << "\n";
203 // std::cout << "\n";
206 leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
207 // stGW::EdgeMap<int> pre_flow(stgw);
208 //Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
209 // pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
210 //pre_flow_test.run();
211 std::cout << "leda matching value: " << ml.size() << std::endl;
212 std::cout << "elapsed time: " << ts << std::endl;
213 // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
214 // std::cout << e << ": " << pre_flow[e] << "\n";
216 // std::cout << "\n";