# HG changeset patch # User marci # Date 1082973264 0 # Node ID 7ab7f083760a4d7f0ab3145182e1dfd09dbba77b # Parent cc8629dc293551ffc481889aa9c0289c65172cc7 stGraphWrapper is almost working diff -r cc8629dc2935 -r 7ab7f083760a src/work/list_graph.h --- a/src/work/list_graph.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/list_graph.h Mon Apr 26 09:54:24 2004 +0000 @@ -369,10 +369,17 @@ /* stream operations, for testing purpose */ friend std::ostream& operator<<(std::ostream& os, const Node& i) { - os << i.node->id; return os; + if (i.valid()) + os << i.node->id; + else + os << "invalid"; + return os; } friend std::ostream& operator<<(std::ostream& os, const Edge& i) { - os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; + if (i.valid()) + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; + else + os << "invalid"; return os; } diff -r cc8629dc2935 -r 7ab7f083760a src/work/makefile --- a/src/work/makefile Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/makefile Mon Apr 26 09:54:24 2004 +0000 @@ -1,12 +1,12 @@ INCLUDEDIRS ?= -I../include -I. -I./{marci,jacint,alpar,klao,akos} -CXXFLAGS = -g -O0 -W -Wall $(INCLUDEDIRS) -ansi -pedantic +CXXFLAGS = -g -O3 -W -Wall $(INCLUDEDIRS) -ansi -pedantic BINARIES ?= bin_heap_demo # Hat, ez elismerem, hogy nagyon ronda, de mukodik minden altalam # ismert rendszeren :-) (Misi) -#CXX := $(shell type -p g++-3.4 || type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++) -CXX := $(shell type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++) +CXX := $(shell type -p g++-3.4 || type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++) +#CXX := $(shell type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++) CC := $(CXX) diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/bfs_iterator.h --- a/src/work/marci/bfs_iterator.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/bfs_iterator.h Mon Apr 26 09:54:24 2004 +0000 @@ -28,6 +28,9 @@ graph(&_graph), reached(*(new ReachedMap(*graph /*, false*/))), own_reached_map(true) { } ~BfsIterator() { if (own_reached_map) delete &reached; } + //s is marcked reached. + //if the queue is empty, then the its is pushed ant the first OutEdgeIt is processe. + //is the queue is not empty, then is it pushed. void pushAndSetReached(Node s) { reached.set(s, true); if (bfs_queue.empty()) { @@ -80,7 +83,7 @@ return *this; } bool finished() const { return bfs_queue.empty(); } - operator OutEdgeIt () const { return actual_edge; } + operator OutEdgeIt() const { return actual_edge; } bool isBNodeNewlyReached() const { return b_node_newly_reached; } bool isANodeExamined() const { return !(graph->valid(actual_edge)); } Node aNode() const { return bfs_queue.front(); } @@ -89,6 +92,51 @@ const std::queue& getBfsQueue() const { return bfs_queue; } }; + /// Bfs searches from s for the nodes wich are not marked in + /// reachedmap + template , + typename PredMap + =typename Graph::template NodeMap, + typename DistMap=typename Graph::template NodeMap > + class Bfs : public BfsIterator { + typedef BfsIterator Parent; + protected: + typedef typename Parent::Node Node; + PredMap& pred; + DistMap& dist; + public: + Bfs(const Graph& _graph, ReachedMap& _reached, PredMap& _pred, DistMap& _dist) : BfsIterator(_graph, _reached), pred(&_pred), dist(&_dist) { } + //s is marked to be reached and pushed in the bfs queue. + //if the queue is empty, then the first out-edge is processed + //If s was not marked previously, then + //in addition its pred is set to be INVALID, and dist to 0. + //if s was marked previuosly, then it is simply pushed. + void push(Node s) { + if (this->reached[s]) { + Parent::pushAndSetReached(s); + } else { + Parent::pushAndSetReached(s); + pred.set(s, INVALID); + dist.set(s, 0); + } + } + void run(Node s) { + push(s); + while (!this->finished()) this->operator++(); + } + Bfs operator++() { + Parent::operator++(); + if (this->graph->valid(actual_edge) && this->b_node_newly_reached) { + pred.set(s, actual_edge); + dist.set(s, dist[this->aNode()]); + } + return *this; + } + const PredMap& getPredMap() const { return pred; } + const DistMap& getDistMap() const { return dist; } + }; + template */ > class DfsIterator { @@ -142,7 +190,7 @@ return *this; } bool finished() const { return dfs_stack.empty(); } - operator OutEdgeIt () const { return actual_edge; } + operator OutEdgeIt() const { return actual_edge; } bool isBNodeNewlyReached() const { return b_node_newly_reached; } bool isANodeExamined() const { return !(graph->valid(actual_edge)); } Node aNode() const { return actual_node; /*FIXME*/} diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/bipartite_graph_wrapper_test.cc --- a/src/work/marci/bipartite_graph_wrapper_test.cc Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/bipartite_graph_wrapper_test.cc Mon Apr 26 09:54:24 2004 +0000 @@ -24,22 +24,62 @@ typedef Graph::OutEdgeIt OutEdgeIt; Graph g; - //Node s, t; - //Graph::EdgeMap cap(g); - //readDimacsMaxFlow(std::cin, g, s, t, cap); - std::vector s_nodes; - std::vector t_nodes; - for (int i=0; i<3; ++i) s_nodes.push_back(g.addNode()); - for (int i=0; i<3; ++i) t_nodes.push_back(g.addNode()); - g.addEdge(s_nodes[0], t_nodes[2]); - g.addEdge(t_nodes[1], s_nodes[2]); +// std::vector s_nodes; +// std::vector t_nodes; +// for (int i=0; i<3; ++i) s_nodes.push_back(g.addNode()); +// for (int i=0; i<3; ++i) t_nodes.push_back(g.addNode()); +// g.addEdge(s_nodes[0], t_nodes[2]); +// g.addEdge(t_nodes[1], s_nodes[2]); +// g.addEdge(s_nodes[0], t_nodes[1]); + +// Graph::NodeMap ref_map(g, -1); +// IterableBoolMap< Graph::NodeMap > bipartite_map(ref_map); +// for (int i=0; i<3; ++i) bipartite_map.insert(s_nodes[i], false); +// for (int i=0; i<3; ++i) bipartite_map.insert(t_nodes[i], true); + + std::vector nodes; + for (int i=0; i<3; ++i) nodes.push_back(g.addNode()); + for (int i=3; i<6; ++i) nodes.push_back(g.addNode()); + g.addEdge(nodes[0], nodes[3+2]); + g.addEdge(nodes[3+1], nodes[2]); + g.addEdge(nodes[0], nodes[3+1]); Graph::NodeMap ref_map(g, -1); IterableBoolMap< Graph::NodeMap > bipartite_map(ref_map); - for (int i=0; i<3; ++i) bipartite_map.insert(s_nodes[i], false); - for (int i=0; i<3; ++i) bipartite_map.insert(t_nodes[i], true); + for (int i=0; i<3; ++i) bipartite_map.insert(nodes[i], false); + for (int i=3; i<6; ++i) bipartite_map.insert(nodes[i], true); + + Graph::Node u; + std::cout << "These nodes will be in S:\n"; + //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen + //irni 1etlen FOR_EACH-csel. + for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) + std::cout << u << " "; + std::cout << "\n"; + std::cout << "These nodes will be in T:\n"; + for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) + std::cout << u << " "; + std::cout << "\n"; + typedef BipartiteGraphWrapper BGW; BGW bgw(g, bipartite_map); + + std::cout << "Nodes by NodeIt:\n"; + FOR_EACH_LOC(BGW::NodeIt, n, bgw) { + std::cout << n << " "; + } + std::cout << "\n"; + std::cout << "Nodes in S by ClassNodeIt:\n"; + FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) { + std::cout << n << " "; + } + std::cout << "\n"; + std::cout << "Nodes in T by ClassNodeIt:\n"; + FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) { + std::cout << n << " "; + } + std::cout << "\n"; + std::cout << "Edges of the bipartite graph:\n"; FOR_EACH_LOC(BGW::EdgeIt, e, bgw) { std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl; } @@ -58,21 +98,43 @@ Graph::Node s; s=g.first(si); bfs.pushAndSetReached(BGW::Node(s)); - while (!bfs.finished()) ++bfs; + while (!bfs.finished()) { ++bfs; } - BGW::EdgeMap cap(bgw); - BGW::EdgeMap flow1(bgw); + FOR_EACH_LOC(stGW::NodeIt, n, stgw) { + std::cout << "out-edges of " << n << ":\n"; + FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { + std::cout << " " << e << "\n"; + std::cout << " aNode: " << stgw.aNode(e) << "\n"; + std::cout << " bNode: " << stgw.bNode(e) << "\n"; + } + std::cout << "in-edges of " << n << ":\n"; + FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { + std::cout << " " << e << "\n"; + std::cout << " aNode: " << stgw.aNode(e) << "\n"; + std::cout << " bNode: " << stgw.bNode(e) << "\n"; + } + } + std::cout << "Edges of the stGraphWrapper:\n"; + FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { + std::cout << " " << n << "\n"; + } - typedef ResGraphWrapper< BGW, int, BGW::EdgeMap, BGW::EdgeMap > - RBGW; - RBGW rbgw(bgw, cap, flow1); - RBGW::NodeMap u(rbgw); + stGW::NodeMap b(stgw); + FOR_EACH_LOC(stGW::NodeIt, n, stgw) { + std::cout << n << ": " << b[n] <<"\n"; + } + + std::cout << "Bfs from s: \n"; + BfsIterator< stGW, stGW::NodeMap > bfs_stgw(stgw); + bfs_stgw.pushAndSetReached(stgw.S_NODE); + while (!bfs_stgw.finished()) { + std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n"; + ++bfs_stgw; + } - MaxFlow, stGW::EdgeMap > max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow); - max_flow_test.augmentOnShortestPath(); - max_flow_test.augmentOnShortestPath(); + while (max_flow_test.augmentOnShortestPath()) { } std::cout << max_flow_test.flowValue() << std::endl; diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/edmonds_karp.h --- a/src/work/marci/edmonds_karp.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/edmonds_karp.h Mon Apr 26 09:54:24 2004 +0000 @@ -322,7 +322,9 @@ typename MapGraphWrapper::template NodeMap dist; public: DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { } - void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; } + void set(const typename MapGraphWrapper::Node& n, int a) { + dist.set(n, a); + } int operator[](const typename MapGraphWrapper::Node& n) { return dist[n]; } // int get(const typename MapGraphWrapper::Node& n) const { diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/for_each_macros.h --- a/src/work/marci/for_each_macros.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/for_each_macros.h Mon Apr 26 09:54:24 2004 +0000 @@ -4,13 +4,13 @@ namespace hugo { -#define FOR_EACH(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) -#define FOR_EACH_INC(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_GLOB(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_INC_GLOB(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) -#define FOR_EACH_EDGE(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) -#define FOR_EACH_NODE(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) -#define FOR_EACH_INEDGE(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) -#define FOR_EACH_OUTEDGE(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_EDGE_GLOB(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_NODE_GLOB(e, g) for((g).first((e)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_INEDGE_GLOB(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) +#define FOR_EACH_OUTEDGE_GLOB(e, g, v) for((g).first((e), (v)); (g).valid((e)); (g).next((e))) // template // It loopFirst(const Graph& g) const { diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/graph_wrapper.h --- a/src/work/marci/graph_wrapper.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/graph_wrapper.h Mon Apr 26 09:54:24 2004 +0000 @@ -923,12 +923,17 @@ SFalseTTrueMap* s_false_t_true_map; public: - static const bool S_CLASS=false; - static const bool T_CLASS=true; + //marci + //FIXME vhogy igy kellene, csak az en forditom nem eszi meg + //static const bool S_CLASS=false; + //static const bool T_CLASS=true; + bool S_CLASS; + bool T_CLASS; + BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map) - : GraphWrapper(_graph), s_false_t_true_map(&_s_false_t_true_map) { - } + : GraphWrapper(_graph), s_false_t_true_map(&_s_false_t_true_map), + S_CLASS(false), T_CLASS(true) { } typedef typename GraphWrapper::Node Node; //using GraphWrapper::NodeIt; typedef typename GraphWrapper::Edge Edge; @@ -1102,7 +1107,7 @@ //(invalid, 2, vmi) //invalid, 3, invalid) template class NodeMap; - template class EdgeMap; + template class EdgeMap; // template friend class NodeMap; // template friend class EdgeMap; @@ -1141,9 +1146,11 @@ return (v.spec!=u.spec || static_cast(u)!= static_cast(v)); - } + } + friend std::ostream& operator<<(std::ostream& os, const Node& i); int getSpec() const { return spec; } }; + class NodeIt { friend class GraphWrapper; friend class stGraphWrapper; @@ -1155,14 +1162,15 @@ n(_n), spec(_spec) { } NodeIt(const Invalid& i) : n(i), spec(3) { } NodeIt(const stGraphWrapper& _G) : n(*(_G.graph)), spec(0) { - if (!_G->valid(n)) spec=1; + if (!_G.graph->valid(n)) spec=1; } operator Node() const { return Node(n, spec); } }; + class Edge : public Graph::Edge { friend class GraphWrapper; friend class stGraphWrapper; - template friend class EdgeMap; + template friend class EdgeMap; int spec; typename Graph::Node n; public: @@ -1184,8 +1192,10 @@ static_cast(v) || u.n!=v.n); } + friend std::ostream& operator<<(std::ostream& os, const Edge& i); int getSpec() const { return spec; } }; + class OutEdgeIt { friend class GraphWrapper; friend class stGraphWrapper; @@ -1229,6 +1239,7 @@ } operator Edge() const { return Edge(e, spec, n); } }; + class InEdgeIt { friend class GraphWrapper; friend class stGraphWrapper; @@ -1261,9 +1272,10 @@ e=INVALID; spec=3; n=INVALID; + break; case 2: e=INVALID; - spec=1; + spec=2; _G.graph->first(n, T_CLASS); //vmi->t; if (!_G.graph->valid(n)) spec=3; //Ha T ures break; @@ -1271,6 +1283,7 @@ } operator Edge() const { return Edge(e, spec, n); } }; + class EdgeIt { friend class GraphWrapper; friend class stGraphWrapper; @@ -1334,7 +1347,7 @@ typename Graph::Node v; switch (i.spec) { case 0: //normal edge - this->graph->aNode(i.e); + v=this->graph->aNode(i.e); this->graph->next(i.e); if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk if (this->graph->inSClass(v)) { //S, nincs kiel @@ -1447,14 +1460,19 @@ bool valid(const Node& n) const { return (n.spec<3); } bool valid(const Edge& e) const { return (e.spec<3); } -// int nodeNum() const { return this->graph->nodeNum(); } -// int edgeNum() const { return this->graph->edgeNum(); } + int nodeNum() const { return this->graph->nodeNum()+2; } + int edgeNum() const { + return this->graph->edgeNum()+this->graph->nodeNum(); + } Node aNode(const OutEdgeIt& e) const { return tail(e); } Node aNode(const InEdgeIt& e) const { return head(e); } Node bNode(const OutEdgeIt& e) const { return head(e); } Node bNode(const InEdgeIt& e) const { return tail(e); } - + + void addNode() const { } + void addEdge() const { } + // Node addNode() const { return Node(this->graph->addNode()); } // Edge addEdge(const Node& tail, const Node& head) const { // return Edge(this->graph->addEdge(tail, head)); } @@ -1468,7 +1486,9 @@ typedef typename GraphWrapper::template NodeMap Parent; T s_value, t_value; public: - NodeMap(const stGraphWrapper& _G) : Parent(_G) { } + 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) { } @@ -1502,8 +1522,11 @@ } }; - template class EdgeMap : public GraphWrapper::template EdgeMap { - typedef typename GraphWrapper::template EdgeMap Parent; + template::template EdgeMap > + class EdgeMap : public Parent { + //typedef typename GraphWrapper::template EdgeMap Parent; typename GraphWrapper::template NodeMap node_value; public: EdgeMap(const stGraphWrapper& _G) : Parent(_G), @@ -1527,7 +1550,7 @@ void set(const Edge& e, T t) { switch (e.spec) { case 0: - GraphWrapper::template EdgeMap::set(e, t); + Parent::set(e, t); break; case 1: node_value.set(e.n, t); @@ -1539,6 +1562,55 @@ } } }; + +// template class EdgeMap : public GraphWrapper::template EdgeMap { +// typedef typename GraphWrapper::template EdgeMap Parent; +// 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) { } +// 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::template EdgeMap::set(e, t); +// break; +// case 1: +// node_value.set(e.n, t); +// break; +// case 2: +// default: +// node_value.set(e.n, t); +// break; +// } +// } +// }; + + friend std::ostream& operator<<(std::ostream& os, const Node& i) { + os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")"; + return os; + } + friend std::ostream& operator<<(std::ostream& os, const Edge& i) { + os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec << + " node: " << i.n << ")"; + return os; + } + }; ///@} diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/leda_graph_wrapper.h --- a/src/work/marci/leda_graph_wrapper.h Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/leda_graph_wrapper.h Mon Apr 26 09:54:24 2004 +0000 @@ -22,8 +22,11 @@ // @defgroup empty_graph The LedaGraphWrapper class // @{ - /// An empty graph class. + /// A graph wrapperstructure for wrapping LEDA graphs in HUGO. + /// This graph wrapper class wraps LEDA graph and LEDA parametrized graph + /// and then the generic algorithms and wrappers of HUGO can be used + /// with LEDA graphs. /// This class provides all the common features of a grapf structure, /// however completely without implementations or real data structures /// behind the interface. @@ -194,19 +197,19 @@ return i; } - template< typename It > - It first() const { - It e; - first(e); - return e; - } +// template< typename It > +// It first() const { +// It e; +// first(e); +// return e; +// } - template< typename It > - It first(Node v) const { - It e; - first(e, v); - return e; - } +// template< typename It > +// It first(Node v) const { +// It e; +// first(e, v); +// return e; +// } ///Gives back the head node of an edge. Node head(Edge e) const { diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/macro_test.cc --- a/src/work/marci/macro_test.cc Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/macro_test.cc Mon Apr 26 09:54:24 2004 +0000 @@ -14,7 +14,7 @@ Graph::Node n1=g.addNode(); Graph::Node n2=g.addNode(); Graph::NodeIt n; - FOR_EACH(n, g) { + FOR_EACH_GLOB(n, g) { std::cout << g.id(n) << " "; } std::cout << std::endl; diff -r cc8629dc2935 -r 7ab7f083760a src/work/marci/makefile --- a/src/work/marci/makefile Mon Apr 26 09:21:27 2004 +0000 +++ b/src/work/marci/makefile Mon Apr 26 09:54:24 2004 +0000 @@ -9,7 +9,7 @@ CXXFLAGS = -g -O3 -W -Wall $(INCLUDEDIRS) -ansi -pedantic -ftemplate-depth-30 LEDABINARIES = leda_graph_demo leda_bfs_dfs max_bipartite_matching_demo -BINARIES = edmonds_karp_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test +BINARIES = edmonds_karp_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try #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