marci@446: // -*- c++ -*-
marci@446: #include <iostream>
marci@446: #include <fstream>
marci@446: #include <vector>
marci@446: #include <cstdlib>
marci@446: 
marci@446: #include <LEDA/graph.h>
marci@446: #include <LEDA/mcb_matching.h>
marci@446: #include <LEDA/list.h>
marci@446: #include <LEDA/graph_gen.h>
marci@446: 
marci@446: #include <leda_graph_wrapper.h>
marci@648: #include <sage_graph.h>
marci@446: //#include <smart_graph.h>
marci@446: //#include <dimacs.h>
alpar@921: #include <lemon/time_measure.h>
marci@769: #include <for_each_macros.h>
alpar@921: #include <lemon/graph_wrapper.h>
marci@496: #include <bipartite_graph_wrapper.h>
alpar@921: #include <lemon/maps.h>
alpar@921: #include <lemon/max_flow.h>
marci@769: #include <augmenting_flow.h>
marci@446: 
marci@446: /**
marci@446:  * Inicializalja a veletlenszamgeneratort.
marci@446:  * Figyelem, ez nem jo igazi random szamokhoz,
marci@446:  * erre ne bizzad a titkaidat!
marci@446:  */
marci@446: void random_init()
marci@446: {
marci@446: 	unsigned int seed = getpid();
marci@446: 	seed |= seed << 15;
marci@446: 	seed ^= time(0);
marci@446: 
marci@446: 	srand(seed);
marci@446: }
marci@446: 
marci@446: /**
marci@446:  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@446:  */
marci@446: int random(int m)
marci@446: {
marci@446:   return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@446: }
marci@446: 
alpar@921: using namespace lemon;
marci@446: 
marci@446: int main() {
marci@446:   //for leda graph
marci@446:   leda::graph lg;
marci@446:   //lg.make_undirected();
marci@446:   typedef LedaGraphWrapper<leda::graph> Graph;
marci@446:   Graph g(lg);
marci@446: 
marci@648:   //for UndirSageGraph
marci@648:   //typedef UndirSageGraph Graph; 
marci@446:   //Graph g;
marci@446: 
marci@446:   typedef Graph::Node Node;
marci@446:   typedef Graph::NodeIt NodeIt;
marci@446:   typedef Graph::Edge Edge;
marci@446:   typedef Graph::EdgeIt EdgeIt;
marci@446:   typedef Graph::OutEdgeIt OutEdgeIt;
marci@446: 
marci@446:   std::vector<Graph::Node> s_nodes;
marci@446:   std::vector<Graph::Node> t_nodes;
marci@446: 
marci@446:   int a;
marci@446:   std::cout << "number of nodes in the first color class=";
marci@446:   std::cin >> a; 
marci@446:   int b;
marci@446:   std::cout << "number of nodes in the second color class=";
marci@446:   std::cin >> b; 
marci@446:   int m;
marci@446:   std::cout << "number of edges=";
marci@446:   std::cin >> m; 
marci@446:   int k;
marci@447:   std::cout << "A bipartite graph is a random group graph if the color classes \nA and B are partitiones to A_0, A_1, ..., A_{k-1} and B_0, B_1, ..., B_{k-1} \nas equally as possible \nand the edges from A_i goes to A_{i-1 mod k} and A_{i+1 mod k}.\n";
marci@446:   std::cout << "number of groups in LEDA random group graph=";
marci@446:   std::cin >> k; 
marci@482:   std::cout << std::endl;
marci@482:   
marci@446:   leda_list<leda_node> lS;
marci@446:   leda_list<leda_node> lT;
marci@446:   random_bigraph(lg, a, b, m, lS, lT, k);
marci@446: 
marci@482:   Graph::NodeMap<int> ref_map(g, -1);
marci@482:   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@446: 
marci@482:   //generating leda random group graph
marci@446:   leda_node ln;
marci@446:   forall(ln, lS) bipartite_map.insert(ln, false);
marci@446:   forall(ln, lT) bipartite_map.insert(ln, true);
marci@446: 
marci@482:   //making bipartite graph
marci@446:   typedef BipartiteGraphWrapper<Graph> BGW;
marci@446:   BGW bgw(g, bipartite_map);
marci@446: 
marci@446: 
marci@482:   //st-wrapper
marci@768:   typedef stBipartiteGraphWrapper<BGW> stGW;
marci@446:   stGW stgw(bgw);
marci@446:   ConstMap<stGW::Edge, int> const1map(1);
marci@446:   stGW::EdgeMap<int> flow(stgw);
marci@446: 
marci@446:   Timer ts;
marci@482: 
marci@482:   ts.reset();
marci@446:   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@482:   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@482:     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
marci@482:   max_flow_test.run();
alpar@921:   std::cout << "LEMON max matching algorithm based on preflow." << std::endl 
marci@482: 	    << "Size of matching: " 
marci@482: 	    << max_flow_test.flowValue() << std::endl;
marci@482:   std::cout << "elapsed time: " << ts << std::endl << std::endl;
marci@446: 
marci@446:   ts.reset();  
marci@446:   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@482:   std::cout << "LEDA max matching algorithm." << std::endl 
marci@482: 	    << "Size of matching: " 
marci@482: 	    << ml.size() << std::endl;
marci@446:   std::cout << "elapsed time: " << ts << std::endl;
marci@446:   std::cout << "\n";
marci@446: 
marci@482:   ts.reset();
marci@446:   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@648:   typedef SageGraph MutableGraph;
marci@769:   AugmentingFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@769:     max_flow_test_1(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
marci@769:   while (max_flow_test_1.augmentOnBlockingFlow<MutableGraph>()) { }
alpar@921:   std::cout << "LEMON max matching algorithm based on blocking flow augmentation." 
marci@482: 	    << std::endl << "Matching size: " 
marci@769: 	    << max_flow_test_1.flowValue() << std::endl;
marci@446:   std::cout << "elapsed time: " << ts << std::endl;
marci@446: 
marci@446:   return 0;
marci@446: }