COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/bipartite_matching_try.cc @ 555:995bc1f1a3ce

Last change on this file since 555:995bc1f1a3ce was 555:995bc1f1a3ce, checked in by marci, 16 years ago

#include <hugo/ > modifications

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