marci@410: // -*- c++ -*-
marci@410: #include <iostream>
marci@410: #include <fstream>
marci@410: #include <vector>
marci@410: #include <cstdlib>
marci@410: 
marci@410: #include <list_graph.h>
marci@410: //#include <smart_graph.h>
marci@410: //#include <dimacs.h>
marci@555: #include <hugo/time_measure.h>
marci@410: #include <for_each_macros.h>
marci@410: #include <bfs_iterator.h>
marci@496: #include <bipartite_graph_wrapper.h>
marci@555: #include <hugo/maps.h>
marci@480: #include <max_flow.h>
marci@410: 
marci@410: /**
marci@410:  * Inicializalja a veletlenszamgeneratort.
marci@410:  * Figyelem, ez nem jo igazi random szamokhoz,
marci@410:  * erre ne bizzad a titkaidat!
marci@410:  */
marci@410: void random_init()
marci@410: {
marci@410: 	unsigned int seed = getpid();
marci@410: 	seed |= seed << 15;
marci@410: 	seed ^= time(0);
marci@410: 
marci@410: 	srand(seed);
marci@410: }
marci@410: 
marci@410: /**
marci@410:  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@410:  */
marci@410: int random(int m)
marci@410: {
marci@410:   return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@410: }
marci@410: 
marci@410: using namespace hugo;
marci@410: 
marci@410: int main() {
marci@499:   //typedef UndirListGraph Graph; 
marci@499:   typedef BipartiteGraph<ListGraph> Graph;
marci@499:   
marci@410:   typedef Graph::Node Node;
marci@410:   typedef Graph::NodeIt NodeIt;
marci@410:   typedef Graph::Edge Edge;
marci@410:   typedef Graph::EdgeIt EdgeIt;
marci@410:   typedef Graph::OutEdgeIt OutEdgeIt;
marci@410: 
marci@410:   Graph g;
marci@410: 
marci@410:   std::vector<Graph::Node> s_nodes;
marci@410:   std::vector<Graph::Node> t_nodes;
marci@410: 
marci@410:   int a;
marci@410:   std::cout << "number of nodes in the first color class=";
marci@410:   std::cin >> a; 
marci@410:   int b;
marci@410:   std::cout << "number of nodes in the second color class=";
marci@410:   std::cin >> b; 
marci@410:   int m;
marci@410:   std::cout << "number of edges=";
marci@410:   std::cin >> m; 
marci@410:   
marci@500:   std::cout << "Generatig a random bipartite graph..." << std::endl;
marci@499:   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
marci@499:   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
marci@410: 
marci@410:   random_init();
marci@410:   for(int i=0; i<m; ++i) {
marci@410:     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@410:   }
marci@410: 
marci@510: //   std::cout << "Edges of the bipartite graph:" << std::endl;
marci@510: //   FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
marci@510: //   std::cout << std::endl;
marci@410: 
marci@510: //   std::cout << "Nodes:" << std::endl;
marci@510: //   FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
marci@510: //   std::cout << std::endl;
marci@510: //   std::cout << "Nodes in T:" << std::endl;
marci@510: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
marci@510: //   std::cout << std::endl;
marci@510: //   std::cout << "Nodes in S:" << std::endl;
marci@510: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
marci@510: //   std::cout << std::endl;
marci@500: 
marci@510: //   std::cout << "Erasing the first node..." << std::endl;
marci@510: //   NodeIt n;
marci@510: //   g.first(n);
marci@510: //   g.erase(n);
marci@510: //   std::cout << "Nodes of the bipartite graph:" << std::endl;
marci@510: //   FOR_EACH_GLOB(n, g) std::cout << n << " ";
marci@510: //   std::cout << std::endl;
marci@500: 
marci@510: //   std::cout << "Nodes in T:" << std::endl;
marci@510: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
marci@510: //   std::cout << std::endl;
marci@510: //   std::cout << "Nodes in S:" << std::endl;
marci@510: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
marci@510: //   std::cout << std::endl;
marci@500: 
marci@499:   typedef stGraphWrapper<Graph> stGW;
marci@499:   stGW stgw(g);
marci@499:   ConstMap<stGW::Edge, int> const1map(1);
marci@410: 
marci@410:   Timer ts;
marci@410:   ts.reset();
marci@499:   stGW::EdgeMap<int> flow(stgw);
marci@410:   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@499:     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@500:   max_flow_test.run();
marci@410: //  while (max_flow_test.augmentOnShortestPath()) { }
marci@500: //  typedef ListGraph MutableGraph;
marci@410: //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@500: //  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@500: //   std::cout << max_flow_test.flowValue() << std::endl;
marci@500: //  }
marci@410:   std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@410:   std::cout << "elapsed time: " << ts << std::endl;
marci@510: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@510: //     if (flow[e]) std::cout << e << std::endl; 
marci@510: //   }
marci@510: //   std::cout << std::endl;
marci@410: 
marci@410:   return 0;
marci@410: }