marci@617: // -*- c++ -*-
marci@617: #include <iostream>
marci@617: #include <fstream>
marci@617: #include <vector>
marci@617: #include <cstdlib>
marci@617: 
marci@617: #include <LEDA/graph.h>
marci@617: #include <LEDA/mcb_matching.h>
marci@617: #include <LEDA/list.h>
marci@617: #include <LEDA/graph_gen.h>
marci@617: 
marci@617: #include <leda_graph_wrapper.h>
marci@648: #include <sage_graph.h>
marci@617: //#include <smart_graph.h>
marci@617: //#include <dimacs.h>
marci@617: #include <hugo/time_measure.h>
marci@769: #include <for_each_macros.h>
marci@617: #include <hugo/graph_wrapper.h>
marci@617: #include <bipartite_graph_wrapper.h>
marci@617: #include <hugo/maps.h>
marci@769: #include <hugo/max_flow.h>
marci@617: 
marci@770: using std::cin;
marci@769: using std::cout;
marci@769: using std::endl;
marci@617: 
marci@617: using namespace hugo;
marci@617: 
marci@617: int main() {
marci@617:   //for leda graph
marci@617:   leda::graph lg;
marci@617:   //lg.make_undirected();
marci@617:   typedef LedaGraphWrapper<leda::graph> Graph;
marci@617:   Graph g(lg);
marci@617: 
marci@648:   //for UndirSageGraph
marci@648:   //typedef UndirSageGraph Graph; 
marci@617:   //Graph g;
marci@617: 
marci@617:   typedef Graph::Node Node;
marci@617:   typedef Graph::NodeIt NodeIt;
marci@617:   typedef Graph::Edge Edge;
marci@617:   typedef Graph::EdgeIt EdgeIt;
marci@617:   typedef Graph::OutEdgeIt OutEdgeIt;
marci@617: 
marci@617:   std::vector<Graph::Node> s_nodes;
marci@617:   std::vector<Graph::Node> t_nodes;
marci@617: 
marci@617:   int a;
marci@769:   cout << "number of nodes in the first color class=";
marci@770:   cin >> a; 
marci@617:   int b;
marci@769:   cout << "number of nodes in the second color class=";
marci@770:   cin >> b; 
marci@617:   int m;
marci@769:   cout << "number of edges=";
marci@770:   cin >> m; 
marci@617:   int k;
marci@769:   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@769:   cout << "number of groups in LEDA random group graph=";
marci@770:   cin >> k; 
marci@769:   cout << endl;
marci@617:   
marci@617:   leda_list<leda_node> lS;
marci@617:   leda_list<leda_node> lT;
marci@617:   random_bigraph(lg, a, b, m, lS, lT, k);
marci@617: 
marci@617:   Graph::NodeMap<int> ref_map(g, -1);
marci@617:   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@617: 
marci@617:   //generating leda random group graph
marci@617:   leda_node ln;
marci@617:   forall(ln, lS) bipartite_map.insert(ln, false);
marci@617:   forall(ln, lT) bipartite_map.insert(ln, true);
marci@617: 
marci@617:   //making bipartite graph
marci@617:   typedef BipartiteGraphWrapper<Graph> BGW;
marci@617:   BGW bgw(g, bipartite_map);
marci@617: 
marci@617: 
marci@617:   //st-wrapper
marci@768:   typedef stBipartiteGraphWrapper<BGW> stGW;
marci@617:   stGW stgw(bgw);
marci@617:   ConstMap<stGW::Edge, int> const1map(1);
marci@617:   stGW::EdgeMap<int> flow(stgw);
marci@617: 
marci@617:   Timer ts;
marci@617: 
marci@617:   ts.reset();
marci@617:   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@617:   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@617:     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
marci@617:   max_flow_test.run();
marci@769:   cout << "HUGO max matching algorithm based on preflow." << endl 
marci@617: 	    << "Size of matching: " 
marci@769: 	    << max_flow_test.flowValue() << endl;
marci@769:   cout << "elapsed time: " << ts << endl << endl;
marci@617: 
marci@617:   ts.reset();  
marci@617:   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@769:   cout << "LEDA max matching algorithm." << endl 
marci@617: 	    << "Size of matching: " 
marci@769: 	    << ml.size() << endl;
marci@769:   cout << "elapsed time: " << ts << endl << endl;
marci@617: 
marci@617: //   ts.reset();
marci@617: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@648: //   typedef SageGraph MutableGraph;
marci@617: //   while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { }
marci@769: //   cout << "HUGO max matching algorithm based on blocking flow augmentation." 
marci@769: // 	    << endl << "Matching size: " 
marci@769: // 	    << max_flow_test.flowValue() << endl;
marci@769: //   cout << "elapsed time: " << ts << endl << endl;
marci@617: 
marci@617:   {
marci@648:   SageGraph hg;
marci@648:   SageGraph::Node s=hg.addNode();  
marci@648:   SageGraph::Node t=hg.addNode();
marci@648:   BGW::NodeMap<SageGraph::Node> b_s_nodes(bgw);  
marci@648:   BGW::NodeMap<SageGraph::Node> b_t_nodes(bgw);
marci@617:   
marci@617:   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::S_CLASS) {
marci@617:     b_s_nodes.set(n, hg.addNode());
marci@617:     hg.addEdge(s, b_s_nodes[n]);
marci@617:   }
marci@617:   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::T_CLASS) {
marci@617:     b_t_nodes.set(n, hg.addNode());
marci@617:     hg.addEdge(b_t_nodes[n], t);
marci@617:   }
marci@617: 
marci@617:   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) 
marci@617:     hg.addEdge(b_s_nodes[bgw.tail(e)], b_t_nodes[bgw.head(e)]);
marci@617: 
marci@648:   ConstMap<SageGraph::Edge, int> cm(1);
marci@648:   SageGraph::EdgeMap<int> flow(hg); //0
marci@617:   
marci@617:   Timer ts;
marci@617: 
marci@617:   ts.reset();
marci@648:   MaxFlow<SageGraph, int, ConstMap<SageGraph::Edge, int>, 
marci@648:     SageGraph::EdgeMap<int> > 
marci@617:     max_flow_test(hg, s, t, cm, flow);
marci@617:   max_flow_test.run();
marci@769:   cout << "HUGO max matching algorithm on SageGraph by copying the graph, based on preflow." 
marci@769: 	    << endl 
marci@617: 	    << "Size of matching: " 
marci@769: 	    << max_flow_test.flowValue() << endl;
marci@769:   cout << "elapsed time: " << ts << endl << endl;
marci@617:   }
marci@617: 
marci@617:   return 0;
marci@617: }