Add lp_solver_wrapper.h to work doxygen.
2 #ifndef HUGO_GRAPH_WRAPPER_H
3 #define HUGO_GRAPH_WRAPPER_H
7 ///\brief Several graph wrappers.
9 ///This file contains several useful graph wrapper functions.
11 ///\author Marton Makai
13 #include <hugo/invalid.h>
14 #include <hugo/maps.h>
15 //#include <iter_map.h>
21 /// \addtogroup gwrappers
22 /// A main parts of HUGOlib are the different graph structures,
23 /// generic graph algorithms, graph concepts which couple these, and
24 /// graph wrappers. While the previous ones are more or less clear, the
25 /// latter notion needs further explanation.
26 /// Graph wrappers are graph classes which serve for considering graph
27 /// structures in different ways. A short example makes the notion much
29 /// Suppose that we have an instance \c g of a directed graph
30 /// type say \c ListGraph and an algorithm
31 /// \code template<typename Graph> int algorithm(const Graph&); \endcode
32 /// is needed to run on the reversely oriented graph.
33 /// It may be expensive (in time or in memory usage) to copy
34 /// \c g with the reverse orientation.
35 /// Thus, a wrapper class
36 /// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
37 /// The code looks as follows
40 /// RevGraphWrapper<ListGraph> rgw(g);
41 /// int result=algorithm(rgw);
43 /// After running the algorithm, the original graph \c g
44 /// remains untouched. Thus the graph wrapper used above is to consider the
45 /// original graph with reverse orientation.
46 /// This techniques gives rise to an elegant code, and
47 /// based on stable graph wrappers, complex algorithms can be
48 /// implemented easily.
49 /// In flow, circulation and bipartite matching problems, the residual
50 /// graph is of particular importance. Combining a wrapper implementing
51 /// this, shortest path algorithms and minimum mean cycle algorithms,
52 /// a range of weighted and cardinality optimization algorithms can be
53 /// obtained. For lack of space, for other examples,
54 /// the interested user is referred to the detailed documentation of graph
56 /// The behavior of graph wrappers can be very different. Some of them keep
57 /// capabilities of the original graph while in other cases this would be
58 /// meaningless. This means that the concepts that they are a model of depend
59 /// on the graph wrapper, and the wrapped graph(s).
60 /// If an edge of \c rgw is deleted, this is carried out by
61 /// deleting the corresponding edge of \c g. But for a residual
62 /// graph, this operation has no sense.
63 /// Let we stand one more example here to simplify your work.
65 /// \code template<typename Graph> class RevGraphWrapper; \endcode
67 /// <tt> RevGraphWrapper(Graph& _g)</tt>.
68 /// This means that in a situation,
69 /// when a <tt> const ListGraph& </tt> reference to a graph is given,
70 /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
72 /// int algorithm1(const ListGraph& g) {
73 /// RevGraphWrapper<const ListGraph> rgw(g);
74 /// return algorithm2(rgw);
78 /// \addtogroup gwrappers
81 ///Base type for the Graph Wrappers
83 ///This is the base type for the Graph Wrappers.
84 ///\todo Some more docs...
86 ///\author Marton Makai
87 template<typename Graph>
91 GraphWrapper() : graph(0) { }
92 void setGraph(Graph& _graph) { graph=&_graph; }
95 typedef Graph BaseGraph;
96 typedef Graph ParentGraph;
98 GraphWrapper(Graph& _graph) : graph(&_graph) { }
99 // Graph& getGraph() const { return *graph; }
101 // typedef typename Graph::Node Node;
102 class Node : public Graph::Node {
103 friend class GraphWrapper<Graph>;
106 Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
107 // /// \bug construction throughrthr multiple levels should be
108 // /// handled better
109 // Node(const typename ParentGraph::ParentGraph::Node& _n) :
110 // Graph::Node(_n) { }
111 Node(const Invalid& i) : Graph::Node(i) { }
114 friend class GraphWrapper<Graph>;
115 typename Graph::NodeIt n;
118 NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
119 NodeIt(const Invalid& i) : n(i) { }
120 NodeIt(const GraphWrapper<Graph>& _G) : n(*(_G.graph)) { }
121 operator Node() const { return Node(typename Graph::Node(n)); }
123 // typedef typename Graph::Edge Edge;
124 class Edge : public Graph::Edge {
125 friend class GraphWrapper<Graph>;
128 Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
129 Edge(const Invalid& i) : Graph::Edge(i) { }
132 friend class GraphWrapper<Graph>;
133 typename Graph::OutEdgeIt e;
136 OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
137 OutEdgeIt(const Invalid& i) : e(i) { }
138 OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
139 e(*(_G.graph), typename Graph::Node(_n)) { }
140 operator Edge() const { return Edge(typename Graph::Edge(e)); }
143 friend class GraphWrapper<Graph>;
144 typename Graph::InEdgeIt e;
147 InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
148 InEdgeIt(const Invalid& i) : e(i) { }
149 InEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
150 e(*(_G.graph), typename Graph::Node(_n)) { }
151 operator Edge() const { return Edge(typename Graph::Edge(e)); }
153 //typedef typename Graph::SymEdgeIt SymEdgeIt;
155 friend class GraphWrapper<Graph>;
156 typename Graph::EdgeIt e;
159 EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
160 EdgeIt(const Invalid& i) : e(i) { }
161 EdgeIt(const GraphWrapper<Graph>& _G) : e(*(_G.graph)) { }
162 operator Edge() const { return Edge(typename Graph::Edge(e)); }
165 NodeIt& first(NodeIt& i) const {
166 i=NodeIt(*this); return i;
168 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
169 i=OutEdgeIt(*this, p); return i;
171 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
172 i=InEdgeIt(*this, p); return i;
174 EdgeIt& first(EdgeIt& i) const {
175 i=EdgeIt(*this); return i;
178 NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
179 OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
180 InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
181 EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }
183 Node tail(const Edge& e) const {
184 return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
185 Node head(const Edge& e) const {
186 return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
188 bool valid(const Node& n) const {
189 return graph->valid(static_cast<typename Graph::Node>(n)); }
190 bool valid(const Edge& e) const {
191 return graph->valid(static_cast<typename Graph::Edge>(e)); }
193 int nodeNum() const { return graph->nodeNum(); }
194 int edgeNum() const { return graph->edgeNum(); }
196 Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
197 Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
198 Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
199 Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
201 Node addNode() const { return Node(graph->addNode()); }
202 Edge addEdge(const Node& tail, const Node& head) const {
203 return Edge(graph->addEdge(tail, head)); }
205 void erase(const Node& i) const { graph->erase(i); }
206 void erase(const Edge& i) const { graph->erase(i); }
208 void clear() const { graph->clear(); }
210 bool forward(const Edge& e) const { return graph->forward(e); }
211 bool backward(const Edge& e) const { return graph->backward(e); }
213 int id(const Node& v) const { return graph->id(v); }
214 int id(const Edge& e) const { return graph->id(e); }
216 Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
218 template<typename T> class NodeMap : public Graph::template NodeMap<T> {
219 typedef typename Graph::template NodeMap<T> Parent;
221 NodeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
222 NodeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
225 template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
226 typedef typename Graph::template EdgeMap<T> Parent;
228 EdgeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
229 EdgeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
235 /// A graph wrapper which reverses the orientation of the edges.
237 /// A graph wrapper which reverses the orientation of the edges.
238 /// Thus \c Graph have to be a directed graph type.
240 ///\author Marton Makai
241 template<typename Graph>
242 class RevGraphWrapper : public GraphWrapper<Graph> {
244 typedef GraphWrapper<Graph> Parent;
246 RevGraphWrapper() : GraphWrapper<Graph>() { }
248 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
250 typedef typename GraphWrapper<Graph>::Node Node;
251 typedef typename GraphWrapper<Graph>::Edge Edge;
252 //If Graph::OutEdgeIt is not defined
253 //and we do not want to use RevGraphWrapper::InEdgeIt,
254 //the typdef techinque does not work.
255 //Unfortunately all the typedefs are instantiated in templates.
256 //typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
257 //typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
260 friend class GraphWrapper<Graph>;
261 friend class RevGraphWrapper<Graph>;
262 typename Graph::InEdgeIt e;
265 OutEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
266 OutEdgeIt(const Invalid& i) : e(i) { }
267 OutEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
268 e(*(_G.graph), typename Graph::Node(_n)) { }
269 operator Edge() const { return Edge(typename Graph::Edge(e)); }
272 friend class GraphWrapper<Graph>;
273 friend class RevGraphWrapper<Graph>;
274 typename Graph::OutEdgeIt e;
277 InEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
278 InEdgeIt(const Invalid& i) : e(i) { }
279 InEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
280 e(*(_G.graph), typename Graph::Node(_n)) { }
281 operator Edge() const { return Edge(typename Graph::Edge(e)); }
284 using GraphWrapper<Graph>::first;
285 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
286 i=OutEdgeIt(*this, p); return i;
288 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
289 i=InEdgeIt(*this, p); return i;
292 using GraphWrapper<Graph>::next;
293 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
294 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
296 Node aNode(const OutEdgeIt& e) const {
297 return Node(this->graph->aNode(e.e)); }
298 Node aNode(const InEdgeIt& e) const {
299 return Node(this->graph->aNode(e.e)); }
300 Node bNode(const OutEdgeIt& e) const {
301 return Node(this->graph->bNode(e.e)); }
302 Node bNode(const InEdgeIt& e) const {
303 return Node(this->graph->bNode(e.e)); }
305 Node tail(const Edge& e) const {
306 return GraphWrapper<Graph>::head(e); }
307 Node head(const Edge& e) const {
308 return GraphWrapper<Graph>::tail(e); }
314 /// A graph wrapper for hiding nodes and edges from a graph.
316 /// This wrapper shows a graph with filtered node-set and
317 /// edge-set. The quick brown fox iterator jumps over
318 /// the lazy dog nodes or edges if the values for them are false
319 /// in the bool maps.
321 ///\author Marton Makai
322 template<typename Graph, typename NodeFilterMap,
323 typename EdgeFilterMap>
324 class SubGraphWrapper : public GraphWrapper<Graph> {
326 typedef GraphWrapper<Graph> Parent;
328 NodeFilterMap* node_filter_map;
329 EdgeFilterMap* edge_filter_map;
331 SubGraphWrapper() : GraphWrapper<Graph>(),
332 node_filter_map(0), edge_filter_map(0) { }
333 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
334 node_filter_map=&_node_filter_map;
336 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
337 edge_filter_map=&_edge_filter_map;
341 SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
342 EdgeFilterMap& _edge_filter_map) :
343 GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
344 edge_filter_map(&_edge_filter_map) { }
346 typedef typename GraphWrapper<Graph>::Node Node;
348 friend class GraphWrapper<Graph>;
349 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
350 typename Graph::NodeIt n;
353 NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
354 NodeIt(const Invalid& i) : n(i) { }
355 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
357 while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n])
360 operator Node() const { return Node(typename Graph::Node(n)); }
362 typedef typename GraphWrapper<Graph>::Edge Edge;
364 friend class GraphWrapper<Graph>;
365 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
366 typename Graph::OutEdgeIt e;
369 OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
370 OutEdgeIt(const Invalid& i) : e(i) { }
371 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
373 e(*(_G.graph), typename Graph::Node(_n)) {
374 while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
377 operator Edge() const { return Edge(typename Graph::Edge(e)); }
380 friend class GraphWrapper<Graph>;
381 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
382 typename Graph::InEdgeIt e;
385 InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
386 InEdgeIt(const Invalid& i) : e(i) { }
387 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
389 e(*(_G.graph), typename Graph::Node(_n)) {
390 while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
393 operator Edge() const { return Edge(typename Graph::Edge(e)); }
395 //typedef typename Graph::SymEdgeIt SymEdgeIt;
397 friend class GraphWrapper<Graph>;
398 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
399 typename Graph::EdgeIt e;
402 EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
403 EdgeIt(const Invalid& i) : e(i) { }
404 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
406 while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
409 operator Edge() const { return Edge(typename Graph::Edge(e)); }
412 NodeIt& first(NodeIt& i) const {
413 i=NodeIt(*this); return i;
415 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
416 i=OutEdgeIt(*this, p); return i;
418 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
419 i=InEdgeIt(*this, p); return i;
421 EdgeIt& first(EdgeIt& i) const {
422 i=EdgeIt(*this); return i;
425 NodeIt& next(NodeIt& i) const {
426 this->graph->next(i.n);
427 while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
428 this->graph->next(i.n); }
431 OutEdgeIt& next(OutEdgeIt& i) const {
432 this->graph->next(i.e);
433 while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
434 this->graph->next(i.e); }
437 InEdgeIt& next(InEdgeIt& i) const {
438 this->graph->next(i.e);
439 while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
440 this->graph->next(i.e); }
443 EdgeIt& next(EdgeIt& i) const {
444 this->graph->next(i.e);
445 while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
446 this->graph->next(i.e); }
450 Node aNode(const OutEdgeIt& e) const {
451 return Node(this->graph->aNode(e.e)); }
452 Node aNode(const InEdgeIt& e) const {
453 return Node(this->graph->aNode(e.e)); }
454 Node bNode(const OutEdgeIt& e) const {
455 return Node(this->graph->bNode(e.e)); }
456 Node bNode(const InEdgeIt& e) const {
457 return Node(this->graph->bNode(e.e)); }
459 /// This function hides \c n in the graph, i.e. the iteration
460 /// jumps over it. This is done by simply setting the value of \c n
461 /// to be false in the corresponding node-map.
462 void hide(const Node& n) const { node_filter_map->set(n, false); }
464 /// This function hides \c e in the graph, i.e. the iteration
465 /// jumps over it. This is done by simply setting the value of \c e
466 /// to be false in the corresponding edge-map.
467 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
469 /// The value of \c n is set to be true in the node-map which stores
470 /// hide information. If \c n was hidden previuosly, then it is shown
472 void unHide(const Node& n) const { node_filter_map->set(n, true); }
474 /// The value of \c e is set to be true in the edge-map which stores
475 /// hide information. If \c e was hidden previuosly, then it is shown
477 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
479 /// Returns true if \c n is hidden.
480 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
482 /// Returns true if \c n is hidden.
483 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
485 /// This is a linear time operation and works only if
486 /// NodeIt is defined.
487 int nodeNum() const {
490 for (this->first(n); this->valid(n); this->next(n)) ++i;
494 /// This is a linear time operation and works only if
495 /// EdgeIt is defined.
496 int edgeNum() const {
499 for (this->first(e); this->valid(e); this->next(e)) ++i;
507 /// \brief A wrapper for forgetting the orientation of a graph.
509 /// A wrapper for getting an undirected graph by forgetting
510 /// the orientation of a directed one.
512 /// \author Marton Makai
513 template<typename Graph>
514 class UndirGraphWrapper : public GraphWrapper<Graph> {
516 typedef GraphWrapper<Graph> Parent;
518 UndirGraphWrapper() : GraphWrapper<Graph>() { }
521 typedef typename GraphWrapper<Graph>::Node Node;
522 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
523 typedef typename GraphWrapper<Graph>::Edge Edge;
524 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
526 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
529 friend class UndirGraphWrapper<Graph>;
530 bool out_or_in; //true iff out
531 typename Graph::OutEdgeIt out;
532 typename Graph::InEdgeIt in;
535 OutEdgeIt(const Invalid& i) : Edge(i) { }
536 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
537 out_or_in=true; _G.graph->first(out, _n);
538 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
540 operator Edge() const {
541 if (out_or_in) return Edge(out); else return Edge(in);
546 typedef OutEdgeIt InEdgeIt;
548 using GraphWrapper<Graph>::first;
549 // NodeIt& first(NodeIt& i) const {
550 // i=NodeIt(*this); return i;
552 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
553 i=OutEdgeIt(*this, p); return i;
556 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
557 // i=InEdgeIt(*this, p); return i;
559 // EdgeIt& first(EdgeIt& i) const {
560 // i=EdgeIt(*this); return i;
563 using GraphWrapper<Graph>::next;
564 // NodeIt& next(NodeIt& n) const {
565 // GraphWrapper<Graph>::next(n);
568 OutEdgeIt& next(OutEdgeIt& e) const {
570 typename Graph::Node n=this->graph->tail(e.out);
571 this->graph->next(e.out);
572 if (!this->graph->valid(e.out)) {
573 e.out_or_in=false; this->graph->first(e.in, n); }
575 this->graph->next(e.in);
580 // EdgeIt& next(EdgeIt& e) const {
581 // GraphWrapper<Graph>::next(n);
582 // // graph->next(e.e);
586 Node aNode(const OutEdgeIt& e) const {
587 if (e.out_or_in) return this->graph->tail(e); else
588 return this->graph->head(e); }
589 Node bNode(const OutEdgeIt& e) const {
590 if (e.out_or_in) return this->graph->head(e); else
591 return this->graph->tail(e); }
594 /// \brief An undirected graph template.
596 /// An undirected graph template.
597 /// This class works as an undirected graph and a directed graph of
598 /// class \c Graph is used for the physical storage.
600 template<typename Graph>
601 class UndirGraph : public UndirGraphWrapper<Graph> {
602 typedef UndirGraphWrapper<Graph> Parent;
606 UndirGraph() : UndirGraphWrapper<Graph>() {
607 Parent::setGraph(gr);
613 ///\brief A wrapper for composing a subgraph of a
614 /// bidirected graph composed from a directed one.
615 /// experimental, for fezso's sake.
617 /// A wrapper for composing a subgraps of a
618 /// bidirected graph composed from a directed one.
619 /// experimental, for fezso's sake.
620 /// A bidirected graph is composed over the directed one without physical
621 /// storage. As the oppositely directed edges are logically different ones
622 /// the maps are able to attach different values for them.
623 template<typename Graph,
624 typename ForwardFilterMap, typename BackwardFilterMap>
625 class SubBidirGraphWrapper : public GraphWrapper<Graph> {
627 typedef GraphWrapper<Graph> Parent;
629 //const CapacityMap* capacity;
632 ForwardFilterMap* forward_filter;
633 BackwardFilterMap* backward_filter;
635 SubBidirGraphWrapper() : GraphWrapper<Graph>()/*,
636 capacity(0), flow(0)*/ { }
637 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
638 forward_filter=&_forward_filter;
640 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
641 backward_filter=&_backward_filter;
646 SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
647 BackwardFilterMap& _backward_filter) :
648 GraphWrapper<Graph>(_graph),
649 forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
654 friend class OutEdgeIt;
656 //template<typename T> class NodeMap;
657 template<typename T> class EdgeMap;
659 typedef typename GraphWrapper<Graph>::Node Node;
660 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
662 class Edge : public Graph::Edge {
663 friend class SubBidirGraphWrapper<Graph,
664 ForwardFilterMap, BackwardFilterMap>;
665 ///\bug ez nem is kell
666 //template<typename T> friend class NodeMap;
667 template<typename T> friend class EdgeMap;
669 bool backward; //true, iff backward
670 // typename Graph::Edge e;
673 ///\bug =false kell-e? zsoltnak kell az addEdge miatt
674 Edge(const typename Graph::Edge& _e, bool _backward=false) :
675 Graph::Edge(_e), backward(_backward) { }
676 Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
677 //the unique invalid iterator
678 friend bool operator==(const Edge& u, const Edge& v) {
679 return (v.backward==u.backward &&
680 static_cast<typename Graph::Edge>(u)==
681 static_cast<typename Graph::Edge>(v));
683 friend bool operator!=(const Edge& u, const Edge& v) {
684 return (v.backward!=u.backward ||
685 static_cast<typename Graph::Edge>(u)!=
686 static_cast<typename Graph::Edge>(v));
691 friend class SubBidirGraphWrapper<Graph,
692 ForwardFilterMap, BackwardFilterMap>;
694 typename Graph::OutEdgeIt out;
695 typename Graph::InEdgeIt in;
700 // OutEdgeIt(const Edge& e) : Edge(e) { }
701 OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
702 //the unique invalid iterator
703 OutEdgeIt(const SubBidirGraphWrapper<Graph,
704 ForwardFilterMap, BackwardFilterMap>& _G, Node v) {
706 _G.graph->first(out, v);
707 while(_G.graph->valid(out) && !(*_G.forward_filter)[*this]) { _G.graph->next(out); }
708 if (!_G.graph->valid(out)) {
710 _G.graph->first(in, v);
711 while(_G.graph->valid(in) && !(*_G.backward_filter)[*this]) { _G.graph->next(in); }
714 operator Edge() const {
716 // e.forward=this->forward;
717 // if (this->forward) e=out; else e=in;
720 return Edge(in, this->backward);
722 return Edge(out, this->backward);
727 friend class SubBidirGraphWrapper<Graph,
728 ForwardFilterMap, BackwardFilterMap>;
730 typename Graph::OutEdgeIt out;
731 typename Graph::InEdgeIt in;
736 // OutEdgeIt(const Edge& e) : Edge(e) { }
737 InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
738 //the unique invalid iterator
739 InEdgeIt(const SubBidirGraphWrapper<Graph,
740 ForwardFilterMap, BackwardFilterMap>& _G, Node v) {
742 _G.graph->first(in, v);
743 while(_G.graph->valid(in) && !(*_G.forward_filter)[*this]) { _G.graph->next(in); }
744 if (!_G.graph->valid(in)) {
746 _G.graph->first(out, v);
747 while(_G.graph->valid(out) && !(*_G.backward_filter)[*this]) { _G.graph->next(out); }
750 operator Edge() const {
752 // e.forward=this->forward;
753 // if (this->forward) e=out; else e=in;
756 return Edge(out, this->backward);
758 return Edge(in, this->backward);
763 friend class SubBidirGraphWrapper<Graph,
764 ForwardFilterMap, BackwardFilterMap>;
766 typename Graph::EdgeIt e;
770 EdgeIt(const Invalid& i) : e(i), backward(true) { }
771 EdgeIt(const SubBidirGraphWrapper<Graph,
772 ForwardFilterMap, BackwardFilterMap>& _G) {
775 while (_G.graph->valid(e) && !(*_G.forward_filter)[*this]) _G.graph->next(e);
776 if (!_G.graph->valid(e)) {
779 while (_G.graph->valid(e) && !(*_G.backward_filter)[*this]) _G.graph->next(e);
782 operator Edge() const {
783 return Edge(e, this->backward);
787 using GraphWrapper<Graph>::first;
788 // NodeIt& first(NodeIt& i) const {
789 // i=NodeIt(*this); return i;
791 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
792 i=OutEdgeIt(*this, p); return i;
795 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
796 i=InEdgeIt(*this, p); return i;
798 EdgeIt& first(EdgeIt& i) const {
799 i=EdgeIt(*this); return i;
802 using GraphWrapper<Graph>::next;
803 // NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
804 OutEdgeIt& next(OutEdgeIt& e) const {
806 Node v=this->graph->aNode(e.out);
807 this->graph->next(e.out);
808 while(this->graph->valid(e.out) && !(*forward_filter)[e]) {
809 this->graph->next(e.out); }
810 if (!this->graph->valid(e.out)) {
812 this->graph->first(e.in, v);
813 while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
814 this->graph->next(e.in); }
817 this->graph->next(e.in);
818 while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
819 this->graph->next(e.in); }
824 InEdgeIt& next(InEdgeIt& e) const {
826 Node v=this->graph->aNode(e.in);
827 this->graph->next(e.in);
828 while(this->graph->valid(e.in) && !(*forward_filter)[e]) {
829 this->graph->next(e.in); }
830 if (!this->graph->valid(e.in)) {
832 this->graph->first(e.out, v);
833 while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
834 this->graph->next(e.out); }
837 this->graph->next(e.out);
838 while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
839 this->graph->next(e.out); }
843 EdgeIt& next(EdgeIt& e) const {
845 this->graph->next(e.e);
846 while(this->graph->valid(e.e) && !(*forward_filter)[e]) {
847 this->graph->next(e.e); }
848 if (!this->graph->valid(e.e)) {
850 this->graph->first(e.e);
851 while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
852 this->graph->next(e.e); }
855 this->graph->next(e.e);
856 while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
857 this->graph->next(e.e); }
862 Node tail(Edge e) const {
863 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
864 Node head(Edge e) const {
865 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
867 Node aNode(OutEdgeIt e) const {
868 return ((!e.backward) ? this->graph->aNode(e.out) :
869 this->graph->aNode(e.in)); }
870 Node bNode(OutEdgeIt e) const {
871 return ((!e.backward) ? this->graph->bNode(e.out) :
872 this->graph->bNode(e.in)); }
874 Node aNode(InEdgeIt e) const {
875 return ((!e.backward) ? this->graph->aNode(e.in) :
876 this->graph->aNode(e.out)); }
877 Node bNode(InEdgeIt e) const {
878 return ((!e.backward) ? this->graph->bNode(e.in) :
879 this->graph->bNode(e.out)); }
881 /// Gives back the opposite edge.
882 Edge opposite(const Edge& e) const {
884 f.backward=!f.backward;
888 // int nodeNum() const { return graph->nodeNum(); }
890 void edgeNum() const { }
891 //int edgeNum() const { return graph->edgeNum(); }
894 // int id(Node v) const { return graph->id(v); }
896 bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
897 bool valid(Edge e) const {
898 return this->graph->valid(e);
899 //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
902 bool forward(const Edge& e) const { return !e.backward; }
903 bool backward(const Edge& e) const { return e.backward; }
905 // void augment(const Edge& e, Number a) const {
907 // // flow->set(e.out, flow->get(e.out)+a);
908 // flow->set(e, (*flow)[e]+a);
910 // // flow->set(e.in, flow->get(e.in)-a);
911 // flow->set(e, (*flow)[e]-a);
914 // bool enabled(const Edge& e) const {
916 // // return (capacity->get(e.out)-flow->get(e.out));
917 // //return ((*capacity)[e]-(*flow)[e]);
920 // // return (flow->get(e.in));
921 // //return ((*flow)[e]);
925 // Number enabled(typename Graph::OutEdgeIt out) const {
926 // // return (capacity->get(out)-flow->get(out));
927 // return ((*capacity)[out]-(*flow)[out]);
930 // Number enabled(typename Graph::InEdgeIt in) const {
931 // // return (flow->get(in));
932 // return ((*flow)[in]);
935 template <typename T>
937 typename Graph::template EdgeMap<T> forward_map, backward_map;
940 typedef Edge KeyType;
941 EdgeMap(const SubBidirGraphWrapper<Graph,
942 ForwardFilterMap, BackwardFilterMap>& _G) :
943 forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
944 EdgeMap(const SubBidirGraphWrapper<Graph,
945 ForwardFilterMap, BackwardFilterMap>& _G, T a) :
946 forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
947 void set(Edge e, T a) {
949 forward_map.set(e/*.out*/, a);
951 backward_map.set(e/*.in*/, a);
953 T operator[](Edge e) const {
955 return forward_map[e/*.out*/];
957 return backward_map[e/*.in*/];
960 forward_map.update();
961 backward_map.update();
963 // T get(Edge e) const {
965 // return forward_map.get(e.out);
967 // return backward_map.get(e.in);
974 ///\brief A wrapper for composing bidirected graph from a directed one.
975 /// experimental, for fezso's sake.
977 /// A wrapper for composing bidirected graph from a directed one.
978 /// experimental, for fezso's sake.
979 /// A bidirected graph is composed over the directed one without physical
980 /// storage. As the oppositely directed edges are logically different ones
981 /// the maps are able to attach different values for them.
982 template<typename Graph>
983 class BidirGraphWrapper :
984 public SubBidirGraphWrapper<
986 ConstMap<typename Graph::Edge, bool>,
987 ConstMap<typename Graph::Edge, bool> > {
989 typedef SubBidirGraphWrapper<
991 ConstMap<typename Graph::Edge, bool>,
992 ConstMap<typename Graph::Edge, bool> > Parent;
994 ConstMap<typename Graph::Edge, bool> cm;
995 //const CapacityMap* capacity;
998 BidirGraphWrapper() : Parent(), cm(true) {
999 Parent::setForwardFilterMap(cm);
1000 Parent::setBackwardFilterMap(cm);
1002 // void setCapacityMap(const CapacityMap& _capacity) {
1003 // capacity=&_capacity;
1005 // void setFlowMap(FlowMap& _flow) {
1011 BidirGraphWrapper(Graph& _graph) : Parent() {
1012 Parent::setGraph(_graph);
1013 Parent::setForwardFilterMap(cm);
1014 Parent::setBackwardFilterMap(cm);
1017 int edgeNum() const {
1018 return 2*this->graph->edgeNum();
1025 template<typename Graph>
1026 class OldBidirGraphWrapper : public GraphWrapper<Graph> {
1028 typedef GraphWrapper<Graph> Parent;
1030 //const CapacityMap* capacity;
1033 OldBidirGraphWrapper() : GraphWrapper<Graph>()/*,
1034 capacity(0), flow(0)*/ { }
1035 // void setCapacityMap(const CapacityMap& _capacity) {
1036 // capacity=&_capacity;
1038 // void setFlowMap(FlowMap& _flow) {
1044 OldBidirGraphWrapper(Graph& _graph/*, const CapacityMap& _capacity,
1046 GraphWrapper<Graph>(_graph)/*, capacity(&_capacity), flow(&_flow)*/ { }
1051 friend class OutEdgeIt;
1053 //template<typename T> class NodeMap;
1054 template<typename T> class EdgeMap;
1056 typedef typename GraphWrapper<Graph>::Node Node;
1057 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
1059 class Edge : public Graph::Edge {
1060 friend class OldBidirGraphWrapper<Graph>;
1061 ///\bug ez nem is kell
1062 //template<typename T> friend class NodeMap;
1063 template<typename T> friend class EdgeMap;
1065 bool backward; //true, iff backward
1066 // typename Graph::Edge e;
1069 ///\bug =false kell-e? zsoltnak kell az addEdge miatt
1070 Edge(const typename Graph::Edge& _e, bool _backward=false) :
1071 Graph::Edge(_e), backward(_backward) { }
1072 Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
1073 //the unique invalid iterator
1074 friend bool operator==(const Edge& u, const Edge& v) {
1075 return (v.backward==u.backward &&
1076 static_cast<typename Graph::Edge>(u)==
1077 static_cast<typename Graph::Edge>(v));
1079 friend bool operator!=(const Edge& u, const Edge& v) {
1080 return (v.backward!=u.backward ||
1081 static_cast<typename Graph::Edge>(u)!=
1082 static_cast<typename Graph::Edge>(v));
1087 friend class OldBidirGraphWrapper<Graph>;
1089 typename Graph::OutEdgeIt out;
1090 typename Graph::InEdgeIt in;
1095 // OutEdgeIt(const Edge& e) : Edge(e) { }
1096 OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1097 //the unique invalid iterator
1098 OutEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) {
1100 _G.graph->first(out, v);
1101 while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
1102 if (!_G.graph->valid(out)) {
1104 _G.graph->first(in, v);
1105 while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
1108 operator Edge() const {
1110 // e.forward=this->forward;
1111 // if (this->forward) e=out; else e=in;
1114 return Edge(in, this->backward);
1116 return Edge(out, this->backward);
1121 friend class OldBidirGraphWrapper<Graph>;
1123 typename Graph::OutEdgeIt out;
1124 typename Graph::InEdgeIt in;
1129 // OutEdgeIt(const Edge& e) : Edge(e) { }
1130 InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1131 //the unique invalid iterator
1132 InEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) {
1134 _G.graph->first(in, v);
1135 while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
1136 if (!_G.graph->valid(in)) {
1138 _G.graph->first(out, v);
1139 while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
1142 operator Edge() const {
1144 // e.forward=this->forward;
1145 // if (this->forward) e=out; else e=in;
1148 return Edge(out, this->backward);
1150 return Edge(in, this->backward);
1155 friend class OldBidirGraphWrapper<Graph>;
1157 typename Graph::EdgeIt e;
1161 EdgeIt(const Invalid& i) : e(i), backward(true) { }
1162 EdgeIt(const OldBidirGraphWrapper<Graph>& _G) {
1165 while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
1166 if (!_G.graph->valid(e)) {
1169 while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
1172 operator Edge() const {
1173 return Edge(e, this->backward);
1177 using GraphWrapper<Graph>::first;
1178 // NodeIt& first(NodeIt& i) const {
1179 // i=NodeIt(*this); return i;
1181 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1182 i=OutEdgeIt(*this, p); return i;
1185 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1186 i=InEdgeIt(*this, p); return i;
1188 EdgeIt& first(EdgeIt& i) const {
1189 i=EdgeIt(*this); return i;
1192 using GraphWrapper<Graph>::next;
1193 // NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
1194 OutEdgeIt& next(OutEdgeIt& e) const {
1196 Node v=this->graph->aNode(e.out);
1197 this->graph->next(e.out);
1198 while(this->graph->valid(e.out) && !enabled(e)) {
1199 this->graph->next(e.out); }
1200 if (!this->graph->valid(e.out)) {
1202 this->graph->first(e.in, v);
1203 while(this->graph->valid(e.in) && !enabled(e)) {
1204 this->graph->next(e.in); }
1207 this->graph->next(e.in);
1208 while(this->graph->valid(e.in) && !enabled(e)) {
1209 this->graph->next(e.in); }
1214 InEdgeIt& next(InEdgeIt& e) const {
1216 Node v=this->graph->aNode(e.in);
1217 this->graph->next(e.in);
1218 while(this->graph->valid(e.in) && !enabled(e)) {
1219 this->graph->next(e.in); }
1220 if (!this->graph->valid(e.in)) {
1222 this->graph->first(e.out, v);
1223 while(this->graph->valid(e.out) && !enabled(e)) {
1224 this->graph->next(e.out); }
1227 this->graph->next(e.out);
1228 while(this->graph->valid(e.out) && !enabled(e)) {
1229 this->graph->next(e.out); }
1233 EdgeIt& next(EdgeIt& e) const {
1235 this->graph->next(e.e);
1236 while(this->graph->valid(e.e) && !enabled(e)) {
1237 this->graph->next(e.e); }
1238 if (!this->graph->valid(e.e)) {
1240 this->graph->first(e.e);
1241 while(this->graph->valid(e.e) && !enabled(e)) {
1242 this->graph->next(e.e); }
1245 this->graph->next(e.e);
1246 while(this->graph->valid(e.e) && !enabled(e)) {
1247 this->graph->next(e.e); }
1252 Node tail(Edge e) const {
1253 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
1254 Node head(Edge e) const {
1255 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
1257 Node aNode(OutEdgeIt e) const {
1258 return ((!e.backward) ? this->graph->aNode(e.out) :
1259 this->graph->aNode(e.in)); }
1260 Node bNode(OutEdgeIt e) const {
1261 return ((!e.backward) ? this->graph->bNode(e.out) :
1262 this->graph->bNode(e.in)); }
1264 Node aNode(InEdgeIt e) const {
1265 return ((!e.backward) ? this->graph->aNode(e.in) :
1266 this->graph->aNode(e.out)); }
1267 Node bNode(InEdgeIt e) const {
1268 return ((!e.backward) ? this->graph->bNode(e.in) :
1269 this->graph->bNode(e.out)); }
1271 /// Gives back the opposite edge.
1272 Edge opposite(const Edge& e) const {
1274 f.backward=!f.backward;
1278 // int nodeNum() const { return graph->nodeNum(); }
1280 void edgeNum() const { }
1281 //int edgeNum() const { return graph->edgeNum(); }
1284 // int id(Node v) const { return graph->id(v); }
1286 bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
1287 bool valid(Edge e) const {
1288 return this->graph->valid(e);
1289 //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
1292 bool forward(const Edge& e) const { return !e.backward; }
1293 bool backward(const Edge& e) const { return e.backward; }
1295 // void augment(const Edge& e, Number a) const {
1297 // // flow->set(e.out, flow->get(e.out)+a);
1298 // flow->set(e, (*flow)[e]+a);
1300 // // flow->set(e.in, flow->get(e.in)-a);
1301 // flow->set(e, (*flow)[e]-a);
1304 bool enabled(const Edge& e) const {
1306 // return (capacity->get(e.out)-flow->get(e.out));
1307 //return ((*capacity)[e]-(*flow)[e]);
1310 // return (flow->get(e.in));
1311 //return ((*flow)[e]);
1315 // Number enabled(typename Graph::OutEdgeIt out) const {
1316 // // return (capacity->get(out)-flow->get(out));
1317 // return ((*capacity)[out]-(*flow)[out]);
1320 // Number enabled(typename Graph::InEdgeIt in) const {
1321 // // return (flow->get(in));
1322 // return ((*flow)[in]);
1325 template <typename T>
1327 typename Graph::template EdgeMap<T> forward_map, backward_map;
1329 typedef T ValueType;
1330 typedef Edge KeyType;
1331 EdgeMap(const OldBidirGraphWrapper<Graph>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
1332 EdgeMap(const OldBidirGraphWrapper<Graph>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
1333 void set(Edge e, T a) {
1335 forward_map.set(e/*.out*/, a);
1337 backward_map.set(e/*.in*/, a);
1339 T operator[](Edge e) const {
1341 return forward_map[e/*.out*/];
1343 return backward_map[e/*.in*/];
1346 forward_map.update();
1347 backward_map.update();
1349 // T get(Edge e) const {
1351 // return forward_map.get(e.out);
1353 // return backward_map.get(e.in);
1360 /// \brief A bidirected graph template.
1362 /// A bidirected graph template.
1363 /// Such a bidirected graph stores each pair of oppositely directed edges
1364 /// ones in the memory, i.e. a directed graph of type
1365 /// \c Graph is used for that.
1366 /// As the oppositely directed edges are logically different ones
1367 /// the maps are able to attach different values for them.
1369 template<typename Graph>
1370 class BidirGraph : public BidirGraphWrapper<Graph> {
1372 typedef UndirGraphWrapper<Graph> Parent;
1376 BidirGraph() : BidirGraphWrapper<Graph>() {
1377 Parent::setGraph(gr);
1383 template<typename Graph, typename Number,
1384 typename CapacityMap, typename FlowMap>
1385 class ResForwardFilter {
1386 // const Graph* graph;
1387 const CapacityMap* capacity;
1388 const FlowMap* flow;
1390 ResForwardFilter(/*const Graph& _graph, */
1391 const CapacityMap& _capacity, const FlowMap& _flow) :
1392 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1393 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1394 //void setGraph(const Graph& _graph) { graph=&_graph; }
1395 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1396 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1397 bool operator[](const typename Graph::Edge& e) const {
1398 return (Number((*flow)[e]) < Number((*capacity)[e]));
1402 template<typename Graph, typename Number,
1403 typename CapacityMap, typename FlowMap>
1404 class ResBackwardFilter {
1405 //const Graph* graph;
1406 const CapacityMap* capacity;
1407 const FlowMap* flow;
1409 ResBackwardFilter(/*const Graph& _graph,*/
1410 const CapacityMap& _capacity, const FlowMap& _flow) :
1411 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1412 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1413 //void setGraph(const Graph& _graph) { graph=&_graph; }
1414 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1415 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1416 bool operator[](const typename Graph::Edge& e) const {
1417 return (Number(0) < Number((*flow)[e]));
1422 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1424 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1425 template<typename Graph, typename Number,
1426 typename CapacityMap, typename FlowMap>
1427 class ResGraphWrapper :
1428 public SubBidirGraphWrapper<
1430 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1431 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
1433 typedef SubBidirGraphWrapper<
1435 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1436 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
1438 const CapacityMap* capacity;
1440 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
1441 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
1442 ResGraphWrapper() : Parent(),
1443 capacity(0), flow(0) { }
1444 void setCapacityMap(const CapacityMap& _capacity) {
1445 capacity=&_capacity;
1446 forward_filter.setCapacity(_capacity);
1447 backward_filter.setCapacity(_capacity);
1449 void setFlowMap(FlowMap& _flow) {
1451 forward_filter.setFlow(_flow);
1452 backward_filter.setFlow(_flow);
1454 // /// \bug does graph reference needed in filtermaps??
1455 // void setGraph(const Graph& _graph) {
1456 // Parent::setGraph(_graph);
1457 // forward_filter.setGraph(_graph);
1458 // backward_filter.setGraph(_graph);
1461 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1463 Parent(), capacity(&_capacity), flow(&_flow),
1464 forward_filter(/*_graph,*/ _capacity, _flow),
1465 backward_filter(/*_graph,*/ _capacity, _flow) {
1466 Parent::setGraph(_graph);
1467 Parent::setForwardFilterMap(forward_filter);
1468 Parent::setBackwardFilterMap(backward_filter);
1471 typedef typename Parent::Edge Edge;
1473 // bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
1474 //bool backward(const Edge& e) const { return e.backward; }
1476 void augment(const Edge& e, Number a) const {
1477 if (Parent::forward(e))
1478 // flow->set(e.out, flow->get(e.out)+a);
1479 flow->set(e, (*flow)[e]+a);
1481 //flow->set(e.in, flow->get(e.in)-a);
1482 flow->set(e, (*flow)[e]-a);
1487 Number resCap(const Edge& e) const {
1488 if (Parent::forward(e))
1489 // return (capacity->get(e.out)-flow->get(e.out));
1490 return ((*capacity)[e]-(*flow)[e]);
1492 // return (flow->get(e.in));
1493 return ((*flow)[e]);
1496 /// \brief Residual capacity map.
1498 /// In generic residual graphs the residual capacity can be obtained as a map.
1501 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
1503 typedef Number ValueType;
1504 typedef Edge KeyType;
1505 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) :
1506 res_graph(&_res_graph) { }
1507 Number operator[](const Edge& e) const {
1508 if (res_graph->forward(e))
1509 // return (capacity->get(e.out)-flow->get(e.out));
1510 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1512 // return (flow->get(e.in));
1513 return (*(res_graph->flow))[e];
1515 /// \bug not needed with dynamic maps, or does it?
1522 template<typename Graph, typename Number,
1523 typename CapacityMap, typename FlowMap>
1524 class OldResGraphWrapper : public GraphWrapper<Graph> {
1526 typedef GraphWrapper<Graph> Parent;
1528 const CapacityMap* capacity;
1531 OldResGraphWrapper() : GraphWrapper<Graph>(0),
1532 capacity(0), flow(0) { }
1533 void setCapacityMap(const CapacityMap& _capacity) {
1534 capacity=&_capacity;
1536 void setFlowMap(FlowMap& _flow) {
1542 OldResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1544 GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
1549 friend class OutEdgeIt;
1551 typedef typename GraphWrapper<Graph>::Node Node;
1552 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
1553 class Edge : public Graph::Edge {
1554 friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1556 bool backward; //true, iff backward
1557 // typename Graph::Edge e;
1560 Edge(const typename Graph::Edge& _e, bool _backward) :
1561 Graph::Edge(_e), backward(_backward) { }
1562 Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
1563 //the unique invalid iterator
1564 friend bool operator==(const Edge& u, const Edge& v) {
1565 return (v.backward==u.backward &&
1566 static_cast<typename Graph::Edge>(u)==
1567 static_cast<typename Graph::Edge>(v));
1569 friend bool operator!=(const Edge& u, const Edge& v) {
1570 return (v.backward!=u.backward ||
1571 static_cast<typename Graph::Edge>(u)!=
1572 static_cast<typename Graph::Edge>(v));
1577 friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1579 typename Graph::OutEdgeIt out;
1580 typename Graph::InEdgeIt in;
1585 // OutEdgeIt(const Edge& e) : Edge(e) { }
1586 OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1587 //the unique invalid iterator
1588 OutEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) {
1590 _G.graph->first(out, v);
1591 while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
1592 if (!_G.graph->valid(out)) {
1594 _G.graph->first(in, v);
1595 while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
1598 operator Edge() const {
1600 // e.forward=this->forward;
1601 // if (this->forward) e=out; else e=in;
1604 return Edge(in, this->backward);
1606 return Edge(out, this->backward);
1611 friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1613 typename Graph::OutEdgeIt out;
1614 typename Graph::InEdgeIt in;
1619 // OutEdgeIt(const Edge& e) : Edge(e) { }
1620 InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1621 //the unique invalid iterator
1622 InEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) {
1624 _G.graph->first(in, v);
1625 while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
1626 if (!_G.graph->valid(in)) {
1628 _G.graph->first(out, v);
1629 while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
1632 operator Edge() const {
1634 // e.forward=this->forward;
1635 // if (this->forward) e=out; else e=in;
1638 return Edge(out, this->backward);
1640 return Edge(in, this->backward);
1645 friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1647 typename Graph::EdgeIt e;
1651 EdgeIt(const Invalid& i) : e(i), backward(true) { }
1652 EdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) {
1655 while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
1656 if (!_G.graph->valid(e)) {
1659 while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
1662 operator Edge() const {
1663 return Edge(e, this->backward);
1667 using GraphWrapper<Graph>::first;
1668 // NodeIt& first(NodeIt& i) const {
1669 // i=NodeIt(*this); return i;
1671 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1672 i=OutEdgeIt(*this, p); return i;
1675 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1676 i=InEdgeIt(*this, p); return i;
1678 EdgeIt& first(EdgeIt& i) const {
1679 i=EdgeIt(*this); return i;
1682 using GraphWrapper<Graph>::next;
1683 // NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
1684 OutEdgeIt& next(OutEdgeIt& e) const {
1686 Node v=this->graph->aNode(e.out);
1687 this->graph->next(e.out);
1688 while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1689 this->graph->next(e.out); }
1690 if (!this->graph->valid(e.out)) {
1692 this->graph->first(e.in, v);
1693 while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1694 this->graph->next(e.in); }
1697 this->graph->next(e.in);
1698 while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1699 this->graph->next(e.in); }
1704 InEdgeIt& next(InEdgeIt& e) const {
1706 Node v=this->graph->aNode(e.in);
1707 this->graph->next(e.in);
1708 while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1709 this->graph->next(e.in); }
1710 if (!this->graph->valid(e.in)) {
1712 this->graph->first(e.out, v);
1713 while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1714 this->graph->next(e.out); }
1717 this->graph->next(e.out);
1718 while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1719 this->graph->next(e.out); }
1723 EdgeIt& next(EdgeIt& e) const {
1725 this->graph->next(e.e);
1726 while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1727 this->graph->next(e.e); }
1728 if (!this->graph->valid(e.e)) {
1730 this->graph->first(e.e);
1731 while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1732 this->graph->next(e.e); }
1735 this->graph->next(e.e);
1736 while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1737 this->graph->next(e.e); }
1742 Node tail(Edge e) const {
1743 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
1744 Node head(Edge e) const {
1745 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
1747 Node aNode(OutEdgeIt e) const {
1748 return ((!e.backward) ? this->graph->aNode(e.out) :
1749 this->graph->aNode(e.in)); }
1750 Node bNode(OutEdgeIt e) const {
1751 return ((!e.backward) ? this->graph->bNode(e.out) :
1752 this->graph->bNode(e.in)); }
1754 Node aNode(InEdgeIt e) const {
1755 return ((!e.backward) ? this->graph->aNode(e.in) :
1756 this->graph->aNode(e.out)); }
1757 Node bNode(InEdgeIt e) const {
1758 return ((!e.backward) ? this->graph->bNode(e.in) :
1759 this->graph->bNode(e.out)); }
1761 // int nodeNum() const { return graph->nodeNum(); }
1763 void edgeNum() const { }
1764 //int edgeNum() const { return graph->edgeNum(); }
1767 // int id(Node v) const { return graph->id(v); }
1769 bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
1770 bool valid(Edge e) const {
1771 return this->graph->valid(e);
1772 //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
1775 bool forward(const Edge& e) const { return !e.backward; }
1776 bool backward(const Edge& e) const { return e.backward; }
1778 void augment(const Edge& e, Number a) const {
1780 // flow->set(e.out, flow->get(e.out)+a);
1781 flow->set(e, (*flow)[e]+a);
1783 // flow->set(e.in, flow->get(e.in)-a);
1784 flow->set(e, (*flow)[e]-a);
1787 Number resCap(const Edge& e) const {
1789 // return (capacity->get(e.out)-flow->get(e.out));
1790 return ((*capacity)[e]-(*flow)[e]);
1792 // return (flow->get(e.in));
1793 return ((*flow)[e]);
1796 // Number resCap(typename Graph::OutEdgeIt out) const {
1797 // // return (capacity->get(out)-flow->get(out));
1798 // return ((*capacity)[out]-(*flow)[out]);
1801 // Number resCap(typename Graph::InEdgeIt in) const {
1802 // // return (flow->get(in));
1803 // return ((*flow)[in]);
1806 template <typename T>
1808 typename Graph::template EdgeMap<T> forward_map, backward_map;
1810 typedef T ValueType;
1811 typedef Edge KeyType;
1812 EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
1813 EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
1814 void set(Edge e, T a) {
1816 forward_map.set(e/*.out*/, a);
1818 backward_map.set(e/*.in*/, a);
1820 T operator[](Edge e) const {
1822 return forward_map[e/*.out*/];
1824 return backward_map[e/*.in*/];
1827 forward_map.update();
1828 backward_map.update();
1830 // T get(Edge e) const {
1832 // return forward_map.get(e.out);
1834 // return backward_map.get(e.in);
1841 /// For blocking flows.
1843 /// This graph wrapper is used for Dinits blocking flow computations.
1844 /// For each node, an out-edge is stored which is used when the
1846 /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1850 ///\author Marton Makai
1851 template<typename Graph, typename FirstOutEdgesMap>
1852 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1854 typedef GraphWrapper<Graph> Parent;
1856 FirstOutEdgesMap* first_out_edges;
1858 ErasingFirstGraphWrapper(Graph& _graph,
1859 FirstOutEdgesMap& _first_out_edges) :
1860 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1862 typedef typename GraphWrapper<Graph>::Node Node;
1864 // friend class GraphWrapper<Graph>;
1865 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1866 // typename Graph::NodeIt n;
1869 // NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
1870 // NodeIt(const Invalid& i) : n(i) { }
1871 // NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1872 // n(*(_G.graph)) { }
1873 // operator Node() const { return Node(typename Graph::Node(n)); }
1875 typedef typename GraphWrapper<Graph>::Edge Edge;
1877 friend class GraphWrapper<Graph>;
1878 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1879 // typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
1880 typename Graph::OutEdgeIt e;
1883 OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
1884 OutEdgeIt(const Invalid& i) : e(i) { }
1885 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1887 e((*_G.first_out_edges)[_n]) { }
1888 operator Edge() const { return Edge(typename Graph::Edge(e)); }
1891 friend class GraphWrapper<Graph>;
1892 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1893 // typedef typename Graph::InEdgeIt GraphInEdgeIt;
1894 typename Graph::InEdgeIt e;
1897 InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
1898 InEdgeIt(const Invalid& i) : e(i) { }
1899 InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1901 e(*(_G.graph), typename Graph::Node(_n)) { }
1902 operator Edge() const { return Edge(typename Graph::Edge(e)); }
1904 //typedef typename Graph::SymEdgeIt SymEdgeIt;
1906 friend class GraphWrapper<Graph>;
1907 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1908 // typedef typename Graph::EdgeIt GraphEdgeIt;
1909 typename Graph::EdgeIt e;
1912 EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
1913 EdgeIt(const Invalid& i) : e(i) { }
1914 EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1916 operator Edge() const { return Edge(typename Graph::Edge(e)); }
1919 using GraphWrapper<Graph>::first;
1920 // NodeIt& first(NodeIt& i) const {
1921 // i=NodeIt(*this); return i;
1923 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1924 i=OutEdgeIt(*this, p); return i;
1926 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1927 i=InEdgeIt(*this, p); return i;
1929 EdgeIt& first(EdgeIt& i) const {
1930 i=EdgeIt(*this); return i;
1933 using GraphWrapper<Graph>::next;
1934 // NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
1935 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
1936 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
1937 EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }
1939 Node aNode(const OutEdgeIt& e) const {
1940 return Node(this->graph->aNode(e.e)); }
1941 Node aNode(const InEdgeIt& e) const {
1942 return Node(this->graph->aNode(e.e)); }
1943 Node bNode(const OutEdgeIt& e) const {
1944 return Node(this->graph->bNode(e.e)); }
1945 Node bNode(const InEdgeIt& e) const {
1946 return Node(this->graph->bNode(e.e)); }
1948 void erase(const OutEdgeIt& e) const {
1951 first_out_edges->set(this->tail(e), f.e);
1959 #endif //HUGO_GRAPH_WRAPPER_H