marci@617: // -*- c++ -*- marci@617: #include marci@617: #include marci@617: #include marci@617: #include marci@617: marci@617: #include marci@617: #include marci@617: #include marci@617: #include marci@617: marci@617: #include marci@648: #include marci@617: //#include marci@617: //#include marci@617: #include marci@648: #include marci@617: #include marci@617: #include marci@617: #include marci@617: #include marci@617: marci@617: /** marci@617: * Inicializalja a veletlenszamgeneratort. marci@617: * Figyelem, ez nem jo igazi random szamokhoz, marci@617: * erre ne bizzad a titkaidat! marci@617: */ marci@617: void random_init() marci@617: { marci@617: unsigned int seed = getpid(); marci@617: seed |= seed << 15; marci@617: seed ^= time(0); marci@617: marci@617: srand(seed); marci@617: } marci@617: marci@617: /** marci@617: * Egy veletlen int-et ad vissza 0 es m-1 kozott. marci@617: */ marci@617: int random(int m) marci@617: { marci@617: return int( double(m) * rand() / (RAND_MAX + 1.0) ); marci@617: } 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 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 s_nodes; marci@617: std::vector t_nodes; marci@617: marci@617: int a; marci@617: std::cout << "number of nodes in the first color class="; marci@617: std::cin >> a; marci@617: int b; marci@617: std::cout << "number of nodes in the second color class="; marci@617: std::cin >> b; marci@617: int m; marci@617: std::cout << "number of edges="; marci@617: std::cin >> m; marci@617: int k; marci@617: 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@617: std::cout << "number of groups in LEDA random group graph="; marci@617: std::cin >> k; marci@617: std::cout << std::endl; marci@617: marci@617: leda_list lS; marci@617: leda_list lT; marci@617: random_bigraph(lg, a, b, m, lS, lT, k); marci@617: marci@617: Graph::NodeMap ref_map(g, -1); marci@617: IterableBoolMap< Graph::NodeMap > 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 BGW; marci@617: BGW bgw(g, bipartite_map); marci@617: marci@617: marci@617: //st-wrapper marci@617: typedef stGraphWrapper stGW; marci@617: stGW stgw(bgw); marci@617: ConstMap const1map(1); marci@617: stGW::EdgeMap 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::EdgeMap > marci@617: max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/); marci@617: max_flow_test.run(); marci@617: std::cout << "HUGO max matching algorithm based on preflow." << std::endl marci@617: << "Size of matching: " marci@617: << max_flow_test.flowValue() << std::endl; marci@617: std::cout << "elapsed time: " << ts << std::endl << std::endl; marci@617: marci@617: ts.reset(); marci@617: leda_list ml=MAX_CARD_BIPARTITE_MATCHING(lg); marci@617: std::cout << "LEDA max matching algorithm." << std::endl marci@617: << "Size of matching: " marci@617: << ml.size() << std::endl; marci@617: std::cout << "elapsed time: " << ts << std::endl << std::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()) { } marci@617: // std::cout << "HUGO max matching algorithm based on blocking flow augmentation." marci@617: // << std::endl << "Matching size: " marci@617: // << max_flow_test.flowValue() << std::endl; marci@617: // std::cout << "elapsed time: " << ts << std::endl << std::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 b_s_nodes(bgw); marci@648: BGW::NodeMap 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 cm(1); marci@648: SageGraph::EdgeMap flow(hg); //0 marci@617: marci@617: Timer ts; marci@617: marci@617: ts.reset(); marci@648: MaxFlow, marci@648: SageGraph::EdgeMap > marci@617: max_flow_test(hg, s, t, cm, flow); marci@617: max_flow_test.run(); marci@648: std::cout << "HUGO max matching algorithm on SageGraph by copying the graph, based on preflow." marci@617: << std::endl marci@617: << "Size of matching: " marci@617: << max_flow_test.flowValue() << std::endl; marci@617: std::cout << "elapsed time: " << ts << std::endl << std::endl; marci@617: } marci@617: marci@617: return 0; marci@617: }