# HG changeset patch # User marci # Date 1093259160 0 # Node ID a5e9303a551114312b919b6f462fe2dcdb0afbe0 # Parent 1d3a11622365efc2502598ba0521b7ae0221938e stGraphWrapper modifications diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/bipartite_graph_wrapper.h --- a/src/work/marci/bipartite_graph_wrapper.h Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/bipartite_graph_wrapper.h Mon Aug 23 11:06:00 2004 +0000 @@ -30,9 +30,10 @@ /// \author Marton Makai template class BipartiteGraphWrapper : public GraphWrapper { - protected: + public: typedef IterableBoolMap< typename Graph::template NodeMap > SFalseTTrueMap; + protected: SFalseTTrueMap* s_false_t_true_map; BipartiteGraphWrapper() : GraphWrapper()/*, @@ -46,6 +47,12 @@ //FIXME vhogy igy kellene, csak az en forditom nem eszi meg static const bool S_CLASS; static const bool T_CLASS; + + /// This method is to reach the iterable maps of the bipartite graph or + /// bipartite graph wrapper. + const SFalseTTrueMap& sFalseTTrueMap() const { + return *s_false_t_true_map; + } //bool S_CLASS; //bool T_CLASS; @@ -211,13 +218,13 @@ /// does not work well. template class BipartiteGraph : public BipartiteGraphWrapper { - typedef IterableBoolMap< typename Graph::template NodeMap > - SFalseTTrueMap; +// typedef IterableBoolMap< typename Graph::template NodeMap > +// SFalseTTrueMap; typedef BipartiteGraphWrapper Parent; protected: Graph gr; typename Graph::template NodeMap bipartite_map; - SFalseTTrueMap s_false_t_true_map; + typename Parent::SFalseTTrueMap s_false_t_true_map; public: typedef typename Parent::Node Node; typedef typename Parent::Edge Edge; @@ -258,9 +265,20 @@ } }; + template + class stGraphWrapper; + /// Easier stuff for bipartite graphs. template - class stGraphWrapper; + class stBipartiteGraphWrapper : public + stGraphWrapper { + public: + typedef stGraphWrapper Parent; + stBipartiteGraphWrapper(Graph& _graph) : + Parent(_graph, _graph.sFalseTTrueMap(), _graph.sFalseTTrueMap()) { } + }; // template // std::ostream& @@ -287,13 +305,16 @@ /// graph have to be oriented from S to T. /// /// \author Marton Makai - template + template class stGraphWrapper : public GraphWrapper { + protected: + const sIterableMap* s_iterable_map; + const tIterableMap* t_iterable_map; public: class Node; friend class Node; //GN, int -//0 normalis, 1 s, 2, true, ez az iteralasi sorrend, +//0 normalis, 1 s, 2 t, ez az iteralasi sorrend, //es a vege a false azaz (invalid, 3) class NodeIt; friend class NodeIt; @@ -334,9 +355,15 @@ static const bool S_CLASS=false; static const bool T_CLASS=true; - stGraphWrapper(Graph& _graph) : GraphWrapper(_graph) , - S_NODE(INVALID, 1), - T_NODE(INVALID, 2) { } + // \bug not too nice constructor. + stGraphWrapper(Graph& _graph, + const sIterableMap& _s_iterable_map, + const tIterableMap& _t_iterable_map) : + GraphWrapper(_graph), + s_iterable_map(&_s_iterable_map), + t_iterable_map(&_t_iterable_map), + S_NODE(INVALID, 1), + T_NODE(INVALID, 2) { } // std::ostream& @@ -349,7 +376,7 @@ class Node : public Graph::Node { protected: friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; template friend class NodeMap; friend class Edge; friend class OutEdgeIt; @@ -380,7 +407,7 @@ class NodeIt { friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; typename Graph::NodeIt n; int spec; public: @@ -388,7 +415,8 @@ NodeIt(const typename Graph::NodeIt& _n, int _spec) : n(_n), spec(_spec) { } NodeIt(const Invalid& i) : n(i), spec(3) { } - NodeIt(const stGraphWrapper& _G) : n(*(_G.graph)), spec(0) { + NodeIt(const stGraphWrapper& _G) + : n(*(_G.graph)), spec(0) { if (!_G.graph->valid(n)) spec=1; } operator Node() const { return Node(n, spec); } @@ -396,7 +424,7 @@ class Edge : public Graph::Edge { friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; template friend class EdgeMap; int spec; typename Graph::Node n; @@ -429,7 +457,7 @@ class OutEdgeIt { friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; typename Graph::OutEdgeIt e; int spec; typename Graph::ClassNodeIt n; @@ -440,7 +468,8 @@ e(_e), spec(_spec), n(_n) { } OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } - OutEdgeIt(const stGraphWrapper& _G, const Node& _n) { + OutEdgeIt(const stGraphWrapper& _G, + const Node& _n) { switch (_n.spec) { case 0 : if (_G.graph->inSClass(_n)) { //S, van normalis kiel @@ -473,7 +502,7 @@ class InEdgeIt { friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; typename Graph::InEdgeIt e; int spec; typename Graph::ClassNodeIt n; @@ -484,7 +513,8 @@ e(_e), spec(_spec), n(_n) { } InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } - InEdgeIt(const stGraphWrapper& _G, const Node& _n) { + InEdgeIt(const stGraphWrapper& _G, + const Node& _n) { switch (_n.spec) { case 0 : if (_G.graph->inTClass(_n)) { //T, van normalis beel @@ -517,7 +547,7 @@ class EdgeIt { friend class GraphWrapper; - friend class stGraphWrapper; + friend class stGraphWrapper; typename Graph::EdgeIt e; int spec; typename Graph::ClassNodeIt n; @@ -527,7 +557,7 @@ const typename Graph::ClassNodeIt& _n) : e(_e), spec(_spec), n(_n) { } EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { } - EdgeIt(const stGraphWrapper& _G) : + EdgeIt(const stGraphWrapper& _G) : e(*(_G.graph)), spec(0), n(INVALID) { if (!_G.graph->valid(e)) { spec=1; @@ -718,12 +748,14 @@ protected: T s_value, t_value; public: - NodeMap(const stGraphWrapper& _G) : Parent(_G), - s_value(), - t_value() { } - NodeMap(const stGraphWrapper& _G, T a) : Parent(_G, a), - s_value(a), - t_value(a) { } + NodeMap(const stGraphWrapper& _G) : + Parent(_G), + s_value(), + t_value() { } + NodeMap(const stGraphWrapper& _G, T a) + : Parent(_G, a), + s_value(a), + t_value(a) { } T operator[](const Node& n) const { switch (n.spec) { case 0: @@ -753,7 +785,7 @@ /// This class is to wrap a node-map of \c Graph and two variables /// storing values for \c S_NODE and \c T_NODE to a node-map of - /// stGraphWrapper. + /// stGraphWrapper. template class NodeMapWrapper { public: typedef Node KeyType; @@ -797,10 +829,12 @@ protected: typename GraphWrapper::template NodeMap node_value; public: - EdgeMap(const stGraphWrapper& _G) : Parent(_G), - node_value(_G) { } - EdgeMap(const stGraphWrapper& _G, T a) : Parent(_G, a), - node_value(_G, a) { } + EdgeMap(const stGraphWrapper& _G) + : Parent(_G), + node_value(_G) { } + EdgeMap(const stGraphWrapper& _G, T a) + : Parent(_G, a), + node_value(_G, a) { } T operator[](const Edge& e) const { switch (e.spec) { case 0: @@ -829,7 +863,7 @@ }; /// This class is to wrap an edge-map and a node-map of \c Graph - /// to an edge-map of stGraphWrapper. + /// to an edge-map of stGraphWrapper. template class EdgeMapWrapper { public: diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/bipartite_graph_wrapper_test.cc --- a/src/work/marci/bipartite_graph_wrapper_test.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/bipartite_graph_wrapper_test.cc Mon Aug 23 11:06:00 2004 +0000 @@ -89,7 +89,7 @@ BGW::NodeMap dbyj(bgw); BGW::EdgeMap dbyxcj(bgw); - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw(bgw); ConstMap const1map(1); stGW::NodeMap ize(stgw); diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/bipartite_matching_try.cc --- a/src/work/marci/bipartite_matching_try.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/bipartite_matching_try.cc Mon Aug 23 11:06:00 2004 +0000 @@ -116,7 +116,7 @@ // BGW::NodeMap dbyj(bgw); // BGW::EdgeMap dbyxcj(bgw); - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw(bgw); ConstMap const1map(1); // stGW::NodeMap ize(stgw); diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/bipartite_matching_try_3.cc --- a/src/work/marci/bipartite_matching_try_3.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/bipartite_matching_try_3.cc Mon Aug 23 11:06:00 2004 +0000 @@ -17,6 +17,9 @@ using namespace hugo; +using std::cout; +using std::endl; + int main() { //typedef UndirListGraph Graph; typedef BipartiteGraph Graph; @@ -30,53 +33,54 @@ Graph g; int a; - std::cout << "number of nodes in the first color class="; + cout << "number of nodes in the first color class="; std::cin >> a; int b; - std::cout << "number of nodes in the second color class="; + cout << "number of nodes in the second color class="; std::cin >> b; int m; - std::cout << "number of edges="; + cout << "number of edges="; std::cin >> m; - std::cout << "Generatig a random bipartite graph..." << std::endl; + cout << "Generatig a random bipartite graph..." << endl; random_init(); randomBipartiteGraph(g, a, b, m); -// std::cout << "Edges of the bipartite graph:" << std::endl; -// FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " "; -// std::cout << std::endl; +// cout << "Edges of the bipartite graph:" << endl; +// FOR_EACH_LOC(EdgeIt, e, g) cout << e << " "; +// cout << endl; -// std::cout << "Nodes:" << std::endl; -// FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " "; -// std::cout << std::endl; -// std::cout << "Nodes in T:" << std::endl; -// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; -// std::cout << std::endl; -// std::cout << "Nodes in S:" << std::endl; -// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; -// std::cout << std::endl; +// cout << "Nodes:" << endl; +// FOR_EACH_LOC(Graph::NodeIt, v, g) cout << v << " "; +// cout << endl; +// cout << "Nodes in T:" << endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " "; +// cout << endl; +// cout << "Nodes in S:" << endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " "; +// cout << endl; -// std::cout << "Erasing the first node..." << std::endl; +// cout << "Erasing the first node..." << endl; // NodeIt n; // g.first(n); // g.erase(n); -// std::cout << "Nodes of the bipartite graph:" << std::endl; -// FOR_EACH_GLOB(n, g) std::cout << n << " "; -// std::cout << std::endl; +// cout << "Nodes of the bipartite graph:" << endl; +// FOR_EACH_GLOB(n, g) cout << n << " "; +// cout << endl; -// std::cout << "Nodes in T:" << std::endl; -// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; -// std::cout << std::endl; -// std::cout << "Nodes in S:" << std::endl; -// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; -// std::cout << std::endl; +// cout << "Nodes in T:" << endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " "; +// cout << endl; +// cout << "Nodes in S:" << endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " "; +// cout << endl; - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw(g); ConstMap const1map(1); Timer ts; + cout << "max bipartite matching with stGraphWrapper..." << endl; ts.reset(); stGW::EdgeMap flow(stgw); MaxFlow, stGW::EdgeMap > @@ -86,14 +90,14 @@ // typedef ListGraph MutableGraph; // while (max_flow_test.augmentOnBlockingFlow1()) { // while (max_flow_test.augmentOnBlockingFlow2()) { -// std::cout << max_flow_test.flowValue() << std::endl; +// cout << max_flow_test.flowValue() << endl; // } - std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl; - std::cout << "elapsed time: " << ts << std::endl; + cout << "matching value: " << max_flow_test.flowValue() << endl; + cout << "elapsed time: " << ts << endl; // FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { -// if (flow[e]) std::cout << e << std::endl; +// if (flow[e]) cout << e << endl; // } - std::cout << std::endl; + cout << endl; typedef ConstMap EdgeCap; EdgeCap ge1(1); @@ -104,12 +108,13 @@ typedef Graph::NodeMap NodeFlow; NodeFlow gnf(g); //0 - typedef stGraphWrapper::EdgeMapWrapper CapMap; - typedef stGraphWrapper::EdgeMapWrapper FlowMap; + typedef stGW::EdgeMapWrapper CapMap; + typedef stGW::EdgeMapWrapper FlowMap; CapMap cm(ge1, gn1); FlowMap fm(gef, gnf); //Timer ts; + cout << "max bipartite matching with stGraphWrapper..." << endl; ts.reset(); //stGW::EdgeMap flow(stgw); MaxFlow @@ -119,15 +124,16 @@ // typedef ListGraph MutableGraph; // while (max_flow_test.augmentOnBlockingFlow1()) { // while (max_flow_test.augmentOnBlockingFlow2()) { -// std::cout << max_flow_test.flowValue() << std::endl; +// cout << max_flow_test.flowValue() << endl; // } - std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl; - std::cout << "elapsed time: " << ts << std::endl; + cout << "matching value: " << max_flow_test1.flowValue() << endl; + cout << "elapsed time: " << ts << endl; // FOR_EACH_LOC(Graph::EdgeIt, e, g) { -// if (gef[e]) std::cout << e << std::endl; +// if (gef[e]) cout << e << endl; // } - std::cout << std::endl; + cout << endl; + cout << "max bipartite matching with stGraphWrapper..." << endl; ts.reset(); FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); @@ -136,27 +142,41 @@ matching_test(g, ge1, gn1, gef, gnf); matching_test.run(); - std::cout << "max flow value: " << matching_test.matchingValue() << std::endl; - std::cout << "elapsed time: " << ts << std::endl; + cout << "matching value: " << matching_test.matchingValue() << endl; + cout << "elapsed time: " << ts << endl; // FOR_EACH_LOC(Graph::EdgeIt, e, g) { -// if (gef[e]) std::cout << e << std::endl; +// if (gef[e]) cout << e << endl; // } - std::cout << std::endl; + cout << endl; + cout << "max bipartite matching with MaxBipartiteMatching..." << endl; ts.reset(); FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); - MaxBipartiteMatching, ConstMap, - Graph::EdgeMap, Graph::NodeMap > - matching_test_1(g, ge1, gn1, gef/*, gnf*/); + typedef MaxBipartiteMatching, + ConstMap, + Graph::EdgeMap, Graph::NodeMap > MaxBipartiteMatching; + MaxBipartiteMatching matching_test_1(g, ge1, gn1, gef/*, gnf*/); matching_test_1.run(); - std::cout << "max flow value: " << matching_test_1.matchingValue() << std::endl; - std::cout << "elapsed time: " << ts << std::endl; + cout << "matching value: " << matching_test_1.matchingValue() << endl; + cout << "elapsed time: " << ts << endl; // FOR_EACH_LOC(Graph::EdgeIt, e, g) { -// if (gef[e]) std::cout << e << std::endl; +// if (gef[e]) cout << e << endl; // } - std::cout << std::endl; + cout << endl; + + cout << "testing optimality with MaxBipartiteMatching..." << endl; + ts.reset(); + matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING); + cout << "matching value: " << matching_test_1.matchingValue() << endl; + cout << "elapsed time: " << ts << endl; + + cout << "testing optimality with MaxBipartiteMatching..." << endl; + ts.reset(); + matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING_WITH_GOOD_NODE_FLOW); + cout << "matching value: " << matching_test_1.matchingValue() << endl; + cout << "elapsed time: " << ts << endl; return 0; } diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/leda/bipartite_matching_leda_gen.cc --- a/src/work/marci/leda/bipartite_matching_leda_gen.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/leda/bipartite_matching_leda_gen.cc Mon Aug 23 11:06:00 2004 +0000 @@ -97,7 +97,7 @@ //st-wrapper - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw(bgw); ConstMap const1map(1); stGW::EdgeMap flow(stgw); diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/leda/comparison.cc --- a/src/work/marci/leda/comparison.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/leda/comparison.cc Mon Aug 23 11:06:00 2004 +0000 @@ -97,7 +97,7 @@ //st-wrapper - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw(bgw); ConstMap const1map(1); stGW::EdgeMap flow(stgw); diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/lp/lp_solver_wrapper.h --- a/src/work/marci/lp/lp_solver_wrapper.h Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/lp/lp_solver_wrapper.h Mon Aug 23 11:06:00 2004 +0000 @@ -331,8 +331,8 @@ lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up); } ///. - void setObjCoef(const ColIt& col_it, double obj_coef) { - lpx_set_obj_coef(lp, col_iter_map[col_it], obj_coef); + double getObjCoef(const ColIt& col_it) { + return lpx_get_obj_coef(lp, col_iter_map[col_it]); } ///. void setRowBounds(const RowIt& row_it, int bound_type, diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/max_bipartite_matching.h --- a/src/work/marci/max_bipartite_matching.h Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/max_bipartite_matching.h Mon Aug 23 11:06:00 2004 +0000 @@ -59,18 +59,25 @@ // NodeCap* node_cap; // EdgeFlow* edge_flow; // NodeFlow* node_flow; - typedef stGraphWrapper stGW; + typedef stBipartiteGraphWrapper stGW; stGW stgw; typedef typename stGW::template EdgeMapWrapper CapMap; CapMap cap; NodeFlow* node_flow; typedef typename stGW::template EdgeMapWrapper FlowMap; FlowMap flow; - MaxFlow mf; + typedef MaxFlow MaxFlow; + MaxFlow mf; //graph* g; //EdgeCap* edge_cap; //EdgeFlow* edge_flow; public: + enum MatchingEnum{ + ZERO_MATCHING, + GEN_MATCHING, + GEN_MATCHING_WITH_GOOD_NODE_FLOW, + NO_MATCHING + }; /// For capacitated b-matchings, edge-caoacities and node-capacities /// have to be given. After running \c run the matching is is given /// back in the edge-map \c _edge_flow and \c _node_map can be used @@ -98,9 +105,34 @@ /// The class have a nontrivial destructor. ~MaxBipartiteMatching() { if (node_flow) delete node_flow; } /// run computes the max matching. - void run() { mf.run(); } + void run(MatchingEnum me=ZERO_MATCHING) { + switch (me) { + case ZERO_MATCHING: + mf.run(MaxFlow::ZERO_FLOW); + break; + case GEN_MATCHING: + { + typename stGW::OutEdgeIt e; + for (stgw.first(e, stgw.S_NODE); stgw.valid(e); stgw.next(e)) + flow.set(e, cap[e]); + } + { + typename stGW::InEdgeIt e; + for (stgw.first(e, stgw.T_NODE); stgw.valid(e); stgw.next(e)) + flow.set(e, 0); + } + mf.run(MaxFlow::PRE_FLOW); + break; + case GEN_MATCHING_WITH_GOOD_NODE_FLOW: + mf.run(MaxFlow::GEN_FLOW); + break; + case NO_MATCHING: + mf.run(MaxFlow::NO_FLOW); + break; + } + } /// The matching value after running \c run. - int matchingValue() { return mf.flowValue(); } + int matchingValue() const { return mf.flowValue(); } }; } //namespace hugo diff -r 1d3a11622365 -r a5e9303a5511 src/work/marci/max_bipartite_matching_demo.cc --- a/src/work/marci/max_bipartite_matching_demo.cc Thu Aug 19 11:34:48 2004 +0000 +++ b/src/work/marci/max_bipartite_matching_demo.cc Mon Aug 23 11:06:00 2004 +0000 @@ -70,8 +70,7 @@ cin >> b; int m; cout << "number of edges="; - cin >> m; - + cin >> m; for(int i=0; i