marci@771: // -*- c++ -*- marci@771: #include <iostream> marci@771: #include <fstream> marci@771: #include <vector> marci@771: #include <cstdlib> marci@771: marci@771: #include <LEDA/graph.h> marci@771: #include <LEDA/mcb_matching.h> marci@771: #include <LEDA/list.h> marci@771: marci@771: #include <leda_graph_wrapper.h> marci@771: #include <sage_graph.h> marci@771: #include <dimacs.h> marci@771: #include <time_measure.h> marci@771: #include <edmonds_karp.h> marci@771: marci@771: /** marci@771: * Inicializalja a veletlenszamgeneratort. marci@771: * Figyelem, ez nem jo igazi random szamokhoz, marci@771: * erre ne bizzad a titkaidat! marci@771: */ marci@771: void random_init() marci@771: { marci@771: unsigned int seed = getpid(); marci@771: seed |= seed << 15; marci@771: seed ^= time(0); marci@771: marci@771: srand(seed); marci@771: } marci@771: marci@771: /** marci@771: * Egy veletlen int-et ad vissza 0 es m-1 kozott. marci@771: */ marci@771: int random(int m) marci@771: { marci@771: return int( double(m) * rand() / (RAND_MAX + 1.0) ); marci@771: } marci@771: alpar@921: using namespace lemon; marci@771: marci@771: using std::cout; marci@771: using std::cin; marci@771: using std::endl; marci@771: marci@771: int main() { marci@771: leda::graph g; marci@771: typedef LedaGraphWrapper<leda::graph> Graph; marci@771: Graph G(g); marci@771: // typedef ListGraph Graph; marci@771: // Graph G; marci@771: marci@771: typedef Graph::Node Node; marci@771: typedef Graph::NodeIt NodeIt; marci@771: typedef Graph::Edge Edge; marci@771: typedef Graph::EdgeIt EdgeIt; marci@771: typedef Graph::OutEdgeIt OutEdgeIt; marci@771: typedef Graph::InEdgeIt InEdgeIt; marci@771: marci@771: //Node s, t; marci@771: //Graph::EdgeMap<int> cap(G); marci@771: //readDimacsMaxFlow(std::cin, G, s, t, cap); marci@771: std::vector<Node> s_nodes; marci@771: std::vector<Node> t_nodes; marci@771: marci@771: int a; marci@771: cout << "number of nodes in the first color class="; marci@771: cin >> a; marci@771: int b; marci@771: cout << "number of nodes in the second color class="; marci@771: cin >> b; marci@771: int m; marci@771: cout << "number of edges="; marci@771: cin >> m; marci@771: marci@771: for(int i=0; i<a; ++i) { marci@771: s_nodes.push_back(G.addNode()); marci@771: } marci@771: for(int i=0; i<a; ++i) { marci@771: t_nodes.push_back(G.addNode()); marci@771: } marci@771: random_init(); marci@771: for(int i=0; i<m; ++i) { marci@771: G.addEdge(s_nodes[random(a)], t_nodes[random(b)]); marci@771: } marci@771: marci@771: // G.addEdge(s_nodes[1], t_nodes[5-4]); marci@771: // G.addEdge(s_nodes[1], t_nodes[5-4]); marci@771: // G.addEdge(s_nodes[1], t_nodes[4-4]); marci@771: // G.addEdge(s_nodes[1], t_nodes[4-4]); marci@771: // G.addEdge(s_nodes[2], t_nodes[4-4]); marci@771: // G.addEdge(s_nodes[3], t_nodes[4-4]); marci@771: marci@771: leda_list<leda_node> A; marci@771: leda_list<leda_node> B; marci@771: Graph::NodeMap<bool> s_map(G); //false marci@771: Graph::NodeMap<bool> t_map(G); //false marci@771: marci@771: for(int i=0; i<a; ++i) { s_map.set(s_nodes[i], true); A+=s_nodes[i]; } marci@771: for(int i=0; i<b; ++i) { t_map.set(t_nodes[i], true); B+=t_nodes[i]; } marci@771: marci@771: // cout << "bfs and dfs iterator demo on the directed graph" << endl; marci@771: // for(NodeIt n=G.first<NodeIt>(); G.valid(n); G.next(n)) { marci@771: // cout << G.id(n) << ": "; marci@771: // cout << "out edges: "; marci@771: // for(OutEdgeIt e=G.first<OutEdgeIt>(n); G.valid(e); G.next(e)) alpar@986: // cout << G.id(G.source(e)) << "->" << G.id(G.target(e)) << " "; marci@771: // cout << "in edges: "; marci@771: // for(InEdgeIt e=G.first<InEdgeIt>(n); G.valid(e); G.next(e)) alpar@986: // cout << G.id(G.source(e)) << "->" << G.id(G.target(e)) << " "; marci@771: // cout << endl; marci@771: // } marci@771: marci@771: marci@771: { marci@771: std::cout << "on-the-fly max bipartite matching (Edmonds-Karp) demo on wrapped leda graph..." << std::endl; marci@771: Graph::EdgeMap<int> flow(G); //0 flow marci@771: Graph::EdgeMap<int> cap(G, 1); marci@771: marci@771: Timer ts; marci@771: ts.reset(); marci@771: marci@771: MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap); marci@771: int i=0; marci@771: while (max_flow_test.augmentOnShortestPath()) { marci@771: // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) alpar@986: // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // std::cout<<std::endl; marci@771: ++i; marci@771: } marci@771: marci@771: // std::cout << "maximum matching: "<< std::endl; marci@771: // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // if (flow.get(e)) alpar@986: // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // std::cout<<std::endl; marci@771: // std::cout << "edges which are not in this maximum matching: "<< std::endl; marci@771: // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // if (!flow.get(e)) alpar@986: // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // std::cout<<std::endl; marci@771: marci@771: std::cout << "elapsed time: " << ts << std::endl; marci@771: std::cout << "number of augmentation phases: " << i << std::endl; marci@771: std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; marci@771: } marci@771: marci@771: // { marci@771: // std::cout << "on-the-fly max bipartite matching demo (Hopcroft-Karp) on wrapped leda graph..." << std::endl; marci@771: // Graph::EdgeMap<int> flow(G); //0 flow marci@771: // Graph::EdgeMap<int> cap(G, 1); marci@771: marci@771: // Timer ts; marci@771: // ts.reset(); marci@771: marci@771: // MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap); marci@771: // int i=0; marci@771: // while (max_flow_test.augmentOnBlockingFlow2()) { marci@771: // // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) alpar@986: // // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // // std::cout<<std::endl; marci@771: // ++i; marci@771: // } marci@771: marci@771: // // std::cout << "maximum matching: "<< std::endl; marci@771: // // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // // if (flow.get(e)) alpar@986: // // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // // std::cout<<std::endl; marci@771: // // std::cout << "edges which are not in this maximum matching: "<< std::endl; marci@771: // // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // // if (!flow.get(e)) alpar@986: // // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // // std::cout<<std::endl; marci@771: marci@771: // std::cout << "elapsed time: " << ts << std::endl; marci@771: // std::cout << "number of augmentation phases: " << i << std::endl; marci@771: // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl; marci@771: // } marci@771: marci@771: { marci@771: std::cout << "max bipartite matching (LEDA)..." << std::endl; marci@771: //Graph::EdgeMap<int> flow(G); //0 flow marci@771: //Graph::EdgeMap<int> cap(G, 1); marci@771: marci@771: leda_node_array<bool> NC(g); marci@771: marci@771: Timer ts; marci@771: ts.reset(); marci@771: marci@771: //MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap); marci@771: //int i=0; marci@771: //while (max_flow_test.augmentOnShortestPath()) { ++i; } marci@771: marci@771: //leda_list<leda_edge> l=MAX_CARD_BIPARTITE_MATCHING_HK(g, A, B, NC, false); marci@771: leda_list<leda_edge> l=MAX_CARD_BIPARTITE_MATCHING(g); marci@771: marci@771: marci@771: // std::cout << "maximum matching: "<< std::endl; marci@771: // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // if (flow.get(e)) alpar@986: // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // std::cout<<std::endl; marci@771: // std::cout << "edges which are not in this maximum matching: "<< std::endl; marci@771: // for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) marci@771: // if (!flow.get(e)) alpar@986: // std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " "; marci@771: // std::cout<<std::endl; marci@771: marci@771: marci@771: std::cout << "elapsed time: " << ts << std::endl; marci@771: //std::cout << "number of augmentation phases: " << i << std::endl; marci@771: std::cout << "flow value: "<< l.size() << std::endl; marci@771: } marci@771: marci@771: marci@771: marci@771: return 0; marci@771: }