[Lemon-commits] [lemon_svn] marci: r670 - in hugo/trunk/src/work: jacint marci
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:40:52 CET 2006
Author: marci
Date: Mon May 3 12:04:27 2004
New Revision: 670
Added:
hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
Modified:
hugo/trunk/src/work/jacint/max_flow.h
hugo/trunk/src/work/marci/bipartite_graph_wrapper.h
hugo/trunk/src/work/marci/bipartite_matching_try_2.cc
hugo/trunk/src/work/marci/makefile
Log:
matching, flows
Modified: hugo/trunk/src/work/jacint/max_flow.h
==============================================================================
--- hugo/trunk/src/work/jacint/max_flow.h (original)
+++ hugo/trunk/src/work/jacint/max_flow.h Mon May 3 12:04:27 2004
@@ -88,6 +88,20 @@
//In preflow, it shows levels of nodes.
//typename Graph::template NodeMap<int> level;
typename Graph::template NodeMap<Num> excess;
+// protected:
+// MaxFlow() { }
+// void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
+// FlowMap& _flow)
+// {
+// g=&_G;
+// s=_s;
+// t=_t;
+// capacity=&_capacity;
+// flow=&_flow;
+// n=_G.nodeNum;
+// level.set (_G); //kellene vmi ilyesmi fv
+// excess(_G,0); //itt is
+// }
public:
Modified: hugo/trunk/src/work/marci/bipartite_graph_wrapper.h
==============================================================================
--- hugo/trunk/src/work/marci/bipartite_graph_wrapper.h (original)
+++ hugo/trunk/src/work/marci/bipartite_graph_wrapper.h Mon May 3 12:04:27 2004
@@ -310,7 +310,7 @@
//(invalid, 2, vmi)
//invalid, 3, invalid)
template <typename T> class NodeMap;
- template <typename T, typename Parent> class EdgeMap;
+ template <typename T> class EdgeMap;
// template <typename T> friend class NodeMap;
// template <typename T> friend class EdgeMap;
@@ -385,7 +385,7 @@
class Edge : public Graph::Edge {
friend class GraphWrapper<Graph>;
friend class stGraphWrapper<Graph>;
- template <typename T, typename Parent> friend class EdgeMap;
+ template <typename T> friend class EdgeMap;
int spec;
typename Graph::Node n;
public:
@@ -412,6 +412,7 @@
// operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
friend std::ostream& operator<< (std::ostream& os, const Edge& i);
int getSpec() const { return spec; }
+ typename Graph::Node getNode() const { return n; }
};
class OutEdgeIt {
@@ -702,6 +703,7 @@
template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
+ protected:
T s_value, t_value;
public:
NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
@@ -714,14 +716,11 @@
switch (n.spec) {
case 0:
return Parent::operator[](n);
- break;
case 1:
return s_value;
- break;
case 2:
default:
return t_value;
- break;
}
}
void set(const Node& n, T t) {
@@ -740,11 +739,50 @@
}
};
- template<typename T,
- typename Parent=
- typename GraphWrapper<Graph>::template EdgeMap<T> >
- class EdgeMap : public Parent {
- //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
+ /// 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<Graph>.
+ template<typename NM> class NodeMapWrapper {
+ public:
+ typedef Node KeyType;
+ typedef typename NM::ValueType ValueType;
+ protected:
+ NM* nm;
+ ValueType* s_value, t_value;
+ public:
+ NodeMapWrapper(NM& _nm, ValueType& _s_value, ValueType& _t_value) :
+ nm(&_nm), s_value(&_s_value), t_value(&_t_value) { }
+ ValueType operator[](const Node& n) const {
+ switch (n.getSpec()) {
+ case 0:
+ return (*nm)[n];
+ case 1:
+ return *s_value;
+ case 2:
+ default:
+ return *t_value;
+ }
+ }
+ void set(const Node& n, ValueType t) {
+ switch (n.getSpec()) {
+ case 0:
+ nm->set(n, t);
+ break;
+ case 1:
+ *s_value=t;
+ break;
+ case 2:
+ default:
+ *t_value=t;
+ break;
+ }
+ }
+ };
+
+ template<typename T>
+ class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
+ typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
+ protected:
typename GraphWrapper<Graph>::template NodeMap<T> node_value;
public:
EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
@@ -755,14 +793,11 @@
switch (e.spec) {
case 0:
return Parent::operator[](e);
- break;
case 1:
return node_value[e.n];
- break;
case 2:
default:
return node_value[e.n];
- break;
}
}
void set(const Edge& e, T t) {
@@ -781,43 +816,45 @@
}
};
-// template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
-// typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
-// typename GraphWrapper<Graph>::template NodeMap<T> node_value;
-// public:
-// EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
-// node_value(_G) { }
-// EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
-// node_value(_G, a) { }
-// T operator[](const Edge& e) const {
-// switch (e.spec) {
-// case 0:
-// return Parent::operator[](e);
-// break;
-// case 1:
-// return node_value[e.n];
-// break;
-// case 2:
-// default:
-// return node_value[e.n];
-// break;
-// }
-// }
-// void set(const Edge& e, T t) {
-// switch (e.spec) {
-// case 0:
-// GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
-// break;
-// case 1:
-// node_value.set(e.n, t);
-// break;
-// case 2:
-// default:
-// node_value.set(e.n, t);
-// break;
-// }
-// }
-// };
+ /// This class is to wrap an edge-map and a node-map of \c Graph
+ /// to an edge-map of stGraphWrapper<Graph>.
+ template<typename EM, typename NM>
+ class EdgeMapWrapper {
+ public:
+ typedef Edge KeyType;
+ typedef typename EM::ValueType ValueType;
+ protected:
+ EM* em;
+ NM* nm;
+ public:
+ EdgeMapWrapper(EM& _em, NM& _nm) : em(&_em), nm(&_nm) { }
+ ValueType operator[](const Edge& e) const {
+ switch (e.getSpec()) {
+ case 0:
+ return (*em)[e];
+ case 1:
+ return (*nm)[e.getNode()];
+ case 2:
+ default:
+ return (*nm)[e.getNode()];
+ }
+ }
+ void set(const Edge& e, ValueType t) {
+ switch (e.getSpec()) {
+ case 0:
+ em->set(e, t);
+ break;
+ case 1:
+ nm->set(e.getNode(), t);
+ break;
+ case 2:
+ default:
+ nm->set(e.getNode(), t);
+ break;
+ }
+ }
+ };
+
// template<typename G>
friend std::ostream&
@@ -840,5 +877,5 @@
} //namespace hugo
-#endif //HUGO_GRAPH_WRAPPER_H
+#endif //HUGO_BIPARTITE_GRAPH_WRAPPER_H
Modified: hugo/trunk/src/work/marci/bipartite_matching_try_2.cc
==============================================================================
--- hugo/trunk/src/work/marci/bipartite_matching_try_2.cc (original)
+++ hugo/trunk/src/work/marci/bipartite_matching_try_2.cc Mon May 3 12:04:27 2004
@@ -72,34 +72,34 @@
g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
}
- std::cout << "Edges of the bipartite graph:" << std::endl;
- FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
- std::cout << std::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;
-
- std::cout << "Erasing the first node..." << std::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;
-
- 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;
+// std::cout << "Edges of the bipartite graph:" << std::endl;
+// FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
+// std::cout << std::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;
+
+// std::cout << "Erasing the first node..." << std::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;
+
+// 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;
typedef stGraphWrapper<Graph> stGW;
stGW stgw(g);
@@ -119,10 +119,10 @@
// }
std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
std::cout << "elapsed time: " << ts << std::endl;
- FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
- if (flow[e]) std::cout << e << std::endl;
- }
- std::cout << std::endl;
+// FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
+// if (flow[e]) std::cout << e << std::endl;
+// }
+// std::cout << std::endl;
return 0;
}
Added: hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/marci/bipartite_matching_try_3.cc Mon May 3 12:04:27 2004
@@ -0,0 +1,233 @@
+// -*- c++ -*-
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <cstdlib>
+
+#include <list_graph.h>
+//#include <smart_graph.h>
+//#include <dimacs.h>
+#include <time_measure.h>
+#include <for_each_macros.h>
+#include <bfs_iterator.h>
+#include <bipartite_graph_wrapper.h>
+#include <maps.h>
+#include <max_flow.h>
+
+using namespace hugo;
+
+// template <typename Graph, typename EdgeCap, typename NodeCap,
+// typename EdgeFlow, typename NodeFlow>
+// class MaxMatching : public MaxFlow<stGraphWrapper<Graph>,
+// stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
+// typedef MaxFlow<stGraphWrapper<Graph>,
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>,
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
+// Parent;
+// protected:
+// stGraphWrapper<Graph> gw;
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
+// //graph* g;
+// //EdgeCap* edge_cap;
+// //EdgeFlow* edge_flow;
+// public:
+// MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
+// EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
+// MaxFlow(), gw(_g),
+// cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
+// Parent::set(gw, cap, flow);
+// }
+// };
+
+template <typename Graph, typename EdgeCap, typename NodeCap,
+ typename EdgeFlow, typename NodeFlow>
+class MaxMatching {
+// : public MaxFlow<stGraphWrapper<Graph>,
+// stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
+// typedef MaxFlow<stGraphWrapper<Graph>,
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>,
+// stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
+// Parent;
+protected:
+// EdgeCap* edge_cap;
+// NodeCap* node_cap;
+// EdgeFlow* edge_flow;
+// NodeFlow* node_flow;
+ typedef stGraphWrapper<Graph> stGW;
+ stGW stgw;
+ typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
+ CapMap cap;
+ typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
+ FlowMap flow;
+ MaxFlow<stGW, int, CapMap, FlowMap> mf;
+ //graph* g;
+ //EdgeCap* edge_cap;
+ //EdgeFlow* edge_flow;
+public:
+ MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
+ EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
+ stgw(_g),
+ cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow),
+ mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
+ void run() { mf.run(); }
+ int matchingValue() { return mf.flowValue(); }
+};
+
+/**
+ * Inicializalja a veletlenszamgeneratort.
+ * Figyelem, ez nem jo igazi random szamokhoz,
+ * erre ne bizzad a titkaidat!
+ */
+void random_init()
+{
+ unsigned int seed = getpid();
+ seed |= seed << 15;
+ seed ^= time(0);
+
+ srand(seed);
+}
+
+/**
+ * Egy veletlen int-et ad vissza 0 es m-1 kozott.
+ */
+int random(int m)
+{
+ return int( double(m) * rand() / (RAND_MAX + 1.0) );
+}
+
+int main() {
+ //typedef UndirListGraph Graph;
+ typedef BipartiteGraph<ListGraph> Graph;
+
+ typedef Graph::Node Node;
+ typedef Graph::NodeIt NodeIt;
+ typedef Graph::Edge Edge;
+ typedef Graph::EdgeIt EdgeIt;
+ typedef Graph::OutEdgeIt OutEdgeIt;
+
+ Graph g;
+
+ std::vector<Graph::Node> s_nodes;
+ std::vector<Graph::Node> t_nodes;
+
+ int a;
+ std::cout << "number of nodes in the first color class=";
+ std::cin >> a;
+ int b;
+ std::cout << "number of nodes in the second color class=";
+ std::cin >> b;
+ int m;
+ std::cout << "number of edges=";
+ std::cin >> m;
+
+ std::cout << "Generatig a random bipartite graph..." << std::endl;
+ for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
+ for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
+
+ random_init();
+ for(int i=0; i<m; ++i) {
+ g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
+ }
+
+ std::cout << "Edges of the bipartite graph:" << std::endl;
+ FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
+ std::cout << std::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;
+
+ std::cout << "Erasing the first node..." << std::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;
+
+ 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;
+
+ typedef stGraphWrapper<Graph> stGW;
+ stGW stgw(g);
+ ConstMap<stGW::Edge, int> const1map(1);
+
+ Timer ts;
+ ts.reset();
+ stGW::EdgeMap<int> flow(stgw);
+ MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> >
+ max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
+ max_flow_test.run();
+// while (max_flow_test.augmentOnShortestPath()) { }
+// typedef ListGraph MutableGraph;
+// while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
+// while (max_flow_test.augmentOnBlockingFlow2()) {
+// std::cout << max_flow_test.flowValue() << std::endl;
+// }
+ std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
+ std::cout << "elapsed time: " << ts << std::endl;
+ FOR_EACH_LOC(stGW::EdgeIt, e, stgw) {
+ if (flow[e]) std::cout << e << std::endl;
+ }
+ std::cout << std::endl;
+
+ typedef ConstMap<Graph::Edge, int> EdgeCap;
+ EdgeCap ge1(1);
+ typedef ConstMap<Graph::Node, int> NodeCap;
+ NodeCap gn1(1);
+ typedef Graph::EdgeMap<int> EdgeFlow;
+ EdgeFlow gef(g); //0
+ typedef Graph::NodeMap<int> NodeFlow;
+ NodeFlow gnf(g); //0
+
+ typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> CapMap;
+ typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
+ CapMap cm(ge1, gn1);
+ FlowMap fm(gef, gnf);
+
+ //Timer ts;
+ ts.reset();
+ //stGW::EdgeMap<int> flow(stgw);
+ MaxFlow<stGW, int, CapMap, FlowMap>
+ max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
+ max_flow_test1.run();
+// while (max_flow_test.augmentOnShortestPath()) { }
+// typedef ListGraph MutableGraph;
+// while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
+// while (max_flow_test.augmentOnBlockingFlow2()) {
+// std::cout << max_flow_test.flowValue() << std::endl;
+// }
+ std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl;
+ std::cout << "elapsed time: " << ts << std::endl;
+// FOR_EACH_LOC(Graph::EdgeIt, e, g) {
+// if (gef[e]) std::cout << e << std::endl;
+// }
+ std::cout << std::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);
+ MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>,
+ Graph::EdgeMap<int>, Graph::NodeMap<int> >
+ 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;
+// FOR_EACH_LOC(Graph::EdgeIt, e, g) {
+// if (gef[e]) std::cout << e << std::endl;
+// }
+ std::cout << std::endl;
+ return 0;
+}
Modified: hugo/trunk/src/work/marci/makefile
==============================================================================
--- hugo/trunk/src/work/marci/makefile (original)
+++ hugo/trunk/src/work/marci/makefile Mon May 3 12:04:27 2004
@@ -4,7 +4,7 @@
INCLUDEDIRS ?= -I../../include -I.. -I../{marci,jacint,alpar,klao,akos,athos} -I$(BOOSTROOT)
LEDABINARIES = leda_graph_demo leda_bfs_dfs max_bipartite_matching_demo
-BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try bipartite_matching_try_2
+BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try bipartite_matching_try_2 bipartite_matching_try_3
#gw_vs_not preflow_demo_boost edmonds_karp_demo_boost preflow_demo_jacint preflow_demo_athos edmonds_karp_demo_alpar preflow_demo_leda
include ../makefile
More information about the Lemon-commits
mailing list