marci@411: // -*- c++ -*-
marci@411: #include <iostream>
marci@411: #include <fstream>
marci@411: #include <vector>
marci@411: #include <cstdlib>
marci@411: 
marci@411: #include <LEDA/graph.h>
marci@411: #include <LEDA/mcb_matching.h>
marci@411: #include <LEDA/list.h>
marci@411: 
marci@411: #include <leda_graph_wrapper.h>
marci@411: #include <list_graph.h>
marci@411: //#include <smart_graph.h>
marci@411: //#include <dimacs.h>
marci@411: #include <time_measure.h>
marci@411: #include <for_each_macros.h>
marci@413: //#include <bfs_iterator.h>
marci@411: #include <graph_wrapper.h>
marci@411: #include <maps.h>
marci@411: #include <edmonds_karp.h>
marci@411: #include <preflow.h>
marci@411: 
marci@411: /**
marci@411:  * Inicializalja a veletlenszamgeneratort.
marci@411:  * Figyelem, ez nem jo igazi random szamokhoz,
marci@411:  * erre ne bizzad a titkaidat!
marci@411:  */
marci@411: void random_init()
marci@411: {
marci@411: 	unsigned int seed = getpid();
marci@411: 	seed |= seed << 15;
marci@411: 	seed ^= time(0);
marci@411: 
marci@411: 	srand(seed);
marci@411: }
marci@411: 
marci@411: /**
marci@411:  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@411:  */
marci@411: int random(int m)
marci@411: {
marci@411:   return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@411: }
marci@411: 
marci@411: using namespace hugo;
marci@411: 
marci@411: int main() {
marci@411:   //for leda graph
marci@411:   leda::graph lg;
marci@413:   //lg.make_undirected();
marci@411:   typedef LedaGraphWrapper<leda::graph> Graph;
marci@411:   Graph g(lg);
marci@411: 
marci@411:   //for UndirListGraph
marci@411:   //typedef UndirListGraph Graph; 
marci@411:   //Graph g;
marci@411: 
marci@411:   typedef Graph::Node Node;
marci@411:   typedef Graph::NodeIt NodeIt;
marci@411:   typedef Graph::Edge Edge;
marci@411:   typedef Graph::EdgeIt EdgeIt;
marci@411:   typedef Graph::OutEdgeIt OutEdgeIt;
marci@411: 
marci@411:   std::vector<Graph::Node> s_nodes;
marci@411:   std::vector<Graph::Node> t_nodes;
marci@411: 
marci@411:   int a;
marci@411:   std::cout << "number of nodes in the first color class=";
marci@411:   std::cin >> a; 
marci@411:   int b;
marci@411:   std::cout << "number of nodes in the second color class=";
marci@411:   std::cin >> b; 
marci@411:   int m;
marci@411:   std::cout << "number of edges=";
marci@411:   std::cin >> m; 
marci@411:   
marci@411: 
marci@411:   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
marci@411:   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
marci@411: 
marci@411:   random_init();
marci@411:   for(int i=0; i<m; ++i) {
marci@411:     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@411:   }
marci@411: 
marci@411:   Graph::NodeMap<int> ref_map(g, -1);
marci@411: 
marci@411:   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@411:   for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
marci@411:   for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
marci@411: 
marci@411: //   Graph::Node u;
marci@411: //   std::cout << "These nodes will be in S:\n";
marci@411: //   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
marci@411: //   //irni 1etlen FOR_EACH-csel.
marci@411: //   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
marci@411: //     std::cout << u << " ";
marci@411: //   std::cout << "\n";
marci@411: //   std::cout << "These nodes will be in T:\n";
marci@411: //   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
marci@411: //     std::cout << u << " ";
marci@411: //   std::cout << "\n";
marci@411: 
marci@411:   typedef BipartiteGraphWrapper<Graph> BGW;
marci@411:   BGW bgw(g, bipartite_map);
marci@411: 
marci@411: //   std::cout << "Nodes by NodeIt:\n";
marci@411: //   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
marci@411: //     std::cout << n << " ";
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: //   std::cout << "Nodes in S by ClassNodeIt:\n";
marci@411: //   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
marci@411: //     std::cout << n << " ";
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: //   std::cout << "Nodes in T by ClassNodeIt:\n";
marci@411: //   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
marci@411: //     std::cout << n << " ";
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: //   std::cout << "Edges of the bipartite graph:\n";
marci@411: //   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
marci@411: //     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
marci@411: //   }
marci@411: 
marci@411:   BGW::NodeMap<int> dbyj(bgw);
marci@411:   BGW::EdgeMap<int> dbyxcj(bgw);
marci@411: 
marci@411:   typedef stGraphWrapper<BGW> stGW;
marci@411:   stGW stgw(bgw);
marci@411:   ConstMap<stGW::Edge, int> const1map(1);
marci@411: //  stGW::NodeMap<int> ize(stgw);
marci@411: 
marci@411: //   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
marci@411: //   Graph::NodeIt si;
marci@411: //   Graph::Node s; 
marci@411: //   s=g.first(si);
marci@411: //   bfs.pushAndSetReached(BGW::Node(s));
marci@411: //   while (!bfs.finished()) { ++bfs; }
marci@411: 
marci@411: //   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@411: //     std::cout << "out-edges of " << n << ":\n"; 
marci@411: //     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
marci@411: //       std::cout << " " << e << "\n";
marci@411: //       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@411: //       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
marci@411: //     }
marci@411: //     std::cout << "in-edges of " << n << ":\n"; 
marci@411: //     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
marci@411: //       std::cout << " " << e << "\n";
marci@411: //       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@411: //       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
marci@411: //     }
marci@411: //   }
marci@411: //   std::cout << "Edges of the stGraphWrapper:\n"; 
marci@411: //   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
marci@411: //     std::cout << " " << n << "\n";
marci@411: //   }
marci@411: 
marci@411: //   stGW::NodeMap<bool> b(stgw);
marci@411: //   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@411: //     std::cout << n << ": " << b[n] <<"\n";
marci@411: //   }
marci@411: 
marci@411: //   std::cout << "Bfs from s: \n";
marci@411: //   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
marci@411: //   bfs_stgw.pushAndSetReached(stgw.S_NODE);
marci@411: //   while (!bfs_stgw.finished()) { 
marci@411: //     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
marci@411: //     ++bfs_stgw; 
marci@411: //   }
marci@411: 
marci@411: 
marci@411:   Timer ts;
marci@411:   ts.reset();
marci@411:   stGW::EdgeMap<int> max_flow(stgw);
marci@411:   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@411:     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
marci@411: //  while (max_flow_test.augmentOnShortestPath()) { }
marci@411:   typedef ListGraph MutableGraph;
marci@411: //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@411:   while (max_flow_test.augmentOnBlockingFlow2()) {
marci@411:    std::cout << max_flow_test.flowValue() << std::endl;
marci@411:   }
marci@411:   std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@411:   std::cout << "elapsed time: " << ts << std::endl;
marci@411: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@411: //     std::cout << e << ": " << max_flow[e] << "\n"; 
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: 
marci@411:   ts.reset();
marci@411:   stGW::EdgeMap<int> pre_flow(stgw);
marci@411:   Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@411:     pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
marci@411:   pre_flow_test.run();
marci@411:   std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
marci@411:   std::cout << "elapsed time: " << ts << std::endl;
marci@411: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@411: //     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: 
marci@411:   ts.reset();  
marci@413:   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@411:   //  stGW::EdgeMap<int> pre_flow(stgw);
marci@411:   //Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@411:   //  pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
marci@411:   //pre_flow_test.run();
marci@413:   std::cout << "leda matching value: " << ml.size() << std::endl;
marci@411:   std::cout << "elapsed time: " << ts << std::endl;
marci@411: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@411: //     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@411: //   }
marci@411: //   std::cout << "\n";
marci@411: 
marci@411:   return 0;
marci@411: }