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