Set 'svn:ignore' property.
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>
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 GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
100 // Graph& getGraph() const { return *graph; }
102 typedef typename Graph::Node Node;
103 class NodeIt : public Node {
104 const GraphWrapper<Graph>* gw;
105 friend class GraphWrapper<Graph>;
108 // NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
109 NodeIt(Invalid i) : Node(i) { }
110 NodeIt(const GraphWrapper<Graph>& _gw) :
111 Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
112 NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
113 Node(n), gw(&_gw) { }
114 NodeIt& operator++() {
115 *(static_cast<Node*>(this))=
116 ++(typename Graph::NodeIt(*(gw->graph), *this));
120 typedef typename Graph::Edge Edge;
121 class OutEdgeIt : public Edge {
122 const GraphWrapper<Graph>* gw;
123 friend class GraphWrapper<Graph>;
126 //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
127 OutEdgeIt(Invalid i) : Edge(i) { }
128 OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
129 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
130 OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
131 Edge(e), gw(&_gw) { }
132 OutEdgeIt& operator++() {
133 *(static_cast<Edge*>(this))=
134 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
138 class InEdgeIt : public Edge {
139 const GraphWrapper<Graph>* gw;
140 friend class GraphWrapper<Graph>;
143 //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
144 InEdgeIt(Invalid i) : Edge(i) { }
145 InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
146 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
147 InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
148 Edge(e), gw(&_gw) { }
149 InEdgeIt& operator++() {
150 *(static_cast<Edge*>(this))=
151 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
155 //typedef typename Graph::SymEdgeIt SymEdgeIt;
156 class EdgeIt : public Edge {
157 const GraphWrapper<Graph>* gw;
158 friend class GraphWrapper<Graph>;
161 //EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
162 EdgeIt(Invalid i) : Edge(i) { }
163 EdgeIt(const GraphWrapper<Graph>& _gw) :
164 Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
165 EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
166 Edge(e), gw(&_gw) { }
167 EdgeIt& operator++() {
168 *(static_cast<Edge*>(this))=
169 ++(typename Graph::EdgeIt(*(gw->graph), *this));
174 NodeIt& first(NodeIt& i) const {
175 i=NodeIt(*this); return i;
177 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
178 i=OutEdgeIt(*this, p); return i;
180 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
181 i=InEdgeIt(*this, p); return i;
183 EdgeIt& first(EdgeIt& i) const {
184 i=EdgeIt(*this); return i;
187 // NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
188 // OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
189 // InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
190 // EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }
192 Node tail(const Edge& e) const {
193 return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
194 Node head(const Edge& e) const {
195 return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
197 // bool valid(const Node& n) const {
198 // return graph->valid(static_cast<typename Graph::Node>(n)); }
199 // bool valid(const Edge& e) const {
200 // return graph->valid(static_cast<typename Graph::Edge>(e)); }
202 int nodeNum() const { return graph->nodeNum(); }
203 int edgeNum() const { return graph->edgeNum(); }
205 // Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
206 // Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
207 // Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
208 // Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
210 Node addNode() const { return Node(graph->addNode()); }
211 Edge addEdge(const Node& tail, const Node& head) const {
212 return Edge(graph->addEdge(tail, head)); }
214 void erase(const Node& i) const { graph->erase(i); }
215 void erase(const Edge& i) const { graph->erase(i); }
217 void clear() const { graph->clear(); }
219 bool forward(const Edge& e) const { return graph->forward(e); }
220 bool backward(const Edge& e) const { return graph->backward(e); }
222 int id(const Node& v) const { return graph->id(v); }
223 int id(const Edge& e) const { return graph->id(e); }
225 Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
227 template<typename T> class NodeMap : public Graph::template NodeMap<T> {
228 typedef typename Graph::template NodeMap<T> Parent;
230 NodeMap(const GraphWrapper<Graph>& gw) : Parent(*(gw.graph)) { }
231 NodeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
234 template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
235 typedef typename Graph::template EdgeMap<T> Parent;
237 EdgeMap(const GraphWrapper<Graph>& gw) : Parent(*(gw.graph)) { }
238 EdgeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
244 /// A graph wrapper which reverses the orientation of the edges.
246 /// A graph wrapper which reverses the orientation of the edges.
247 /// Thus \c Graph have to be a directed graph type.
249 ///\author Marton Makai
250 template<typename Graph>
251 class RevGraphWrapper : public GraphWrapper<Graph> {
253 typedef GraphWrapper<Graph> Parent;
255 RevGraphWrapper() : GraphWrapper<Graph>() { }
257 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
258 RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
260 typedef typename GraphWrapper<Graph>::Node Node;
261 typedef typename GraphWrapper<Graph>::Edge Edge;
262 //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
263 //because this does not work is some of them are not defined in the
264 //original graph. The problem with this is that typedef-ed stuff
265 //are instantiated in c++.
266 class OutEdgeIt : public Edge {
267 const RevGraphWrapper<Graph>* gw;
268 friend class GraphWrapper<Graph>;
271 //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
272 OutEdgeIt(Invalid i) : Edge(i) { }
273 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
274 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
275 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
276 Edge(e), gw(&_gw) { }
277 OutEdgeIt& operator++() {
278 *(static_cast<Edge*>(this))=
279 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
283 class InEdgeIt : public Edge {
284 const RevGraphWrapper<Graph>* gw;
285 friend class GraphWrapper<Graph>;
288 //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
289 InEdgeIt(Invalid i) : Edge(i) { }
290 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
291 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
292 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
293 Edge(e), gw(&_gw) { }
294 InEdgeIt& operator++() {
295 *(static_cast<Edge*>(this))=
296 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
301 using GraphWrapper<Graph>::first;
302 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
303 i=OutEdgeIt(*this, p); return i;
305 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
306 i=InEdgeIt(*this, p); return i;
309 // using GraphWrapper<Graph>::next;
310 // OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
311 // InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
313 // Node aNode(const OutEdgeIt& e) const {
314 // return Node(this->graph->aNode(e.e)); }
315 // Node aNode(const InEdgeIt& e) const {
316 // return Node(this->graph->aNode(e.e)); }
317 // Node bNode(const OutEdgeIt& e) const {
318 // return Node(this->graph->bNode(e.e)); }
319 // Node bNode(const InEdgeIt& e) const {
320 // return Node(this->graph->bNode(e.e)); }
322 Node tail(const Edge& e) const {
323 return GraphWrapper<Graph>::head(e); }
324 Node head(const Edge& e) const {
325 return GraphWrapper<Graph>::tail(e); }
331 /// A graph wrapper for hiding nodes and edges from a graph.
333 /// This wrapper shows a graph with filtered node-set and
334 /// edge-set. Given a bool-valued map on the node-set and one on
335 /// the edge-set of the graphs, the iterators shows only the objects
336 /// having true value.
337 /// The quick brown fox iterators jump over
338 /// the lazy dog nodes or edges if their values for are false in the
339 /// corresponding bool maps.
341 ///\author Marton Makai
342 template<typename Graph, typename NodeFilterMap,
343 typename EdgeFilterMap>
344 class SubGraphWrapper : public GraphWrapper<Graph> {
346 typedef GraphWrapper<Graph> Parent;
348 NodeFilterMap* node_filter_map;
349 EdgeFilterMap* edge_filter_map;
351 SubGraphWrapper() : GraphWrapper<Graph>(),
352 node_filter_map(0), edge_filter_map(0) { }
353 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
354 node_filter_map=&_node_filter_map;
356 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
357 edge_filter_map=&_edge_filter_map;
361 SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
362 EdgeFilterMap& _edge_filter_map) :
363 GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
364 edge_filter_map(&_edge_filter_map) { }
366 typedef typename GraphWrapper<Graph>::Node Node;
367 class NodeIt : public Node {
368 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
369 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
372 // NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
373 NodeIt(Invalid i) : Node(i) { }
374 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
375 Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
376 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
378 Node(n), gw(&_gw) { }
379 NodeIt& operator++() {
380 *(static_cast<Node*>(this))=
381 ++(typename Graph::NodeIt(*(gw->graph), *this));
382 while (*static_cast<Node*>(this)!=INVALID &&
383 !(*(gw->node_filter_map))[*this])
384 *(static_cast<Node*>(this))=
385 ++(typename Graph::NodeIt(*(gw->graph), *this));
389 typedef typename GraphWrapper<Graph>::Edge Edge;
390 class OutEdgeIt : public Edge {
391 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
392 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
395 // OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
396 OutEdgeIt(Invalid i) : Edge(i) { }
397 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
398 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
399 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
401 Edge(e), gw(&_gw) { }
402 OutEdgeIt& operator++() {
403 *(static_cast<Edge*>(this))=
404 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
405 while (*static_cast<Edge*>(this)!=INVALID &&
406 !(*(gw->edge_filter_map))[*this])
407 *(static_cast<Edge*>(this))=
408 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
412 class InEdgeIt : public Edge {
413 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
414 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
417 // InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
418 InEdgeIt(Invalid i) : Edge(i) { }
419 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
420 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
421 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
423 Edge(e), gw(&_gw) { }
424 InEdgeIt& operator++() {
425 *(static_cast<Edge*>(this))=
426 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
427 while (*static_cast<Edge*>(this)!=INVALID &&
428 !(*(gw->edge_filter_map))[*this])
429 *(static_cast<Edge*>(this))=
430 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
434 class EdgeIt : public Edge {
435 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
436 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
439 // EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
440 EdgeIt(Invalid i) : Edge(i) { }
441 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
442 Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
443 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
445 Edge(e), gw(&_gw) { }
446 EdgeIt& operator++() {
447 *(static_cast<Edge*>(this))=
448 ++(typename Graph::EdgeIt(*(gw->graph), *this));
449 while (*static_cast<Edge*>(this)!=INVALID &&
450 !(*(gw->edge_filter_map))[*this])
451 *(static_cast<Edge*>(this))=
452 ++(typename Graph::EdgeIt(*(gw->graph), *this));
457 NodeIt& first(NodeIt& i) const {
458 i=NodeIt(*this); return i;
460 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
461 i=OutEdgeIt(*this, p); return i;
463 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
464 i=InEdgeIt(*this, p); return i;
466 EdgeIt& first(EdgeIt& i) const {
467 i=EdgeIt(*this); return i;
470 // NodeIt& next(NodeIt& i) const {
471 // this->graph->next(i.n);
472 // while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
473 // this->graph->next(i.n); }
476 // OutEdgeIt& next(OutEdgeIt& i) const {
477 // this->graph->next(i.e);
478 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
479 // this->graph->next(i.e); }
482 // InEdgeIt& next(InEdgeIt& i) const {
483 // this->graph->next(i.e);
484 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
485 // this->graph->next(i.e); }
488 // EdgeIt& next(EdgeIt& i) const {
489 // this->graph->next(i.e);
490 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
491 // this->graph->next(i.e); }
495 // Node aNode(const OutEdgeIt& e) const {
496 // return Node(this->graph->aNode(e.e)); }
497 // Node aNode(const InEdgeIt& e) const {
498 // return Node(this->graph->aNode(e.e)); }
499 // Node bNode(const OutEdgeIt& e) const {
500 // return Node(this->graph->bNode(e.e)); }
501 // Node bNode(const InEdgeIt& e) const {
502 // return Node(this->graph->bNode(e.e)); }
504 /// This function hides \c n in the graph, i.e. the iteration
505 /// jumps over it. This is done by simply setting the value of \c n
506 /// to be false in the corresponding node-map.
507 void hide(const Node& n) const { node_filter_map->set(n, false); }
509 /// This function hides \c e in the graph, i.e. the iteration
510 /// jumps over it. This is done by simply setting the value of \c e
511 /// to be false in the corresponding edge-map.
512 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
514 /// The value of \c n is set to be true in the node-map which stores
515 /// hide information. If \c n was hidden previuosly, then it is shown
517 void unHide(const Node& n) const { node_filter_map->set(n, true); }
519 /// The value of \c e is set to be true in the edge-map which stores
520 /// hide information. If \c e was hidden previuosly, then it is shown
522 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
524 /// Returns true if \c n is hidden.
525 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
527 /// Returns true if \c n is hidden.
528 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
530 /// \warning This is a linear time operation and works only if
531 /// \c Graph::NodeIt is defined.
532 int nodeNum() const {
534 for (NodeIt n(*this); n!=INVALID; ++n) ++i;
538 /// \warning This is a linear time operation and works only if
539 /// \c Graph::EdgeIt is defined.
540 int edgeNum() const {
542 for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
550 // /// \brief A wrapper for forgetting the orientation of a graph.
552 // /// A wrapper for getting an undirected graph by forgetting
553 // /// the orientation of a directed one.
555 // /// \author Marton Makai
556 // /// does not work in the new concept.
557 template<typename Graph>
558 class UndirGraphWrapper : public GraphWrapper<Graph> {
560 typedef GraphWrapper<Graph> Parent;
562 UndirGraphWrapper() : GraphWrapper<Graph>() { }
565 typedef typename GraphWrapper<Graph>::Node Node;
566 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
567 typedef typename GraphWrapper<Graph>::Edge Edge;
568 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
570 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
573 friend class UndirGraphWrapper<Graph>;
574 bool out_or_in; //true iff out
575 typename Graph::OutEdgeIt out;
576 typename Graph::InEdgeIt in;
579 OutEdgeIt(const Invalid& i) : Edge(i) { }
580 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
581 out_or_in=true; _G.graph->first(out, _n);
582 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
584 operator Edge() const {
585 if (out_or_in) return Edge(out); else return Edge(in);
590 typedef OutEdgeIt InEdgeIt;
592 using GraphWrapper<Graph>::first;
593 // NodeIt& first(NodeIt& i) const {
594 // i=NodeIt(*this); return i;
596 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
597 i=OutEdgeIt(*this, p); return i;
600 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
601 // i=InEdgeIt(*this, p); return i;
603 // EdgeIt& first(EdgeIt& i) const {
604 // i=EdgeIt(*this); return i;
607 using GraphWrapper<Graph>::next;
608 // NodeIt& next(NodeIt& n) const {
609 // GraphWrapper<Graph>::next(n);
612 OutEdgeIt& next(OutEdgeIt& e) const {
614 typename Graph::Node n=this->graph->tail(e.out);
615 this->graph->next(e.out);
616 if (!this->graph->valid(e.out)) {
617 e.out_or_in=false; this->graph->first(e.in, n); }
619 this->graph->next(e.in);
624 // EdgeIt& next(EdgeIt& e) const {
625 // GraphWrapper<Graph>::next(n);
626 // // graph->next(e.e);
630 Node aNode(const OutEdgeIt& e) const {
631 if (e.out_or_in) return this->graph->tail(e); else
632 return this->graph->head(e); }
633 Node bNode(const OutEdgeIt& e) const {
634 if (e.out_or_in) return this->graph->head(e); else
635 return this->graph->tail(e); }
638 /// \brief An undirected graph template.
640 /// An undirected graph template.
641 /// This class works as an undirected graph and a directed graph of
642 /// class \c Graph is used for the physical storage.
644 template<typename Graph>
645 class UndirGraph : public UndirGraphWrapper<Graph> {
646 typedef UndirGraphWrapper<Graph> Parent;
650 UndirGraph() : UndirGraphWrapper<Graph>() {
651 Parent::setGraph(gr);
657 ///\brief A wrapper for composing a subgraph of a
658 /// bidirected graph made from a directed one.
660 /// Suppose that for a directed graph $G=(V, A)$,
661 /// two predicates on the edge-set, $forward_filter$, and $backward_filter$
662 /// is given, and we are dealing with the directed graph
663 /// a $G'=(V, \{uv : uv\in A \mbox{ and } forward_filter(uv) \mbox{ is true}\}+\{vu : uv\in A \mbox{ and } backward_filter(uv) \mbox{ is true}\})$.
664 /// The purpose of writing + instead of union is because parallel
666 /// In other words, a subgraph of the bidirected graph obtained, which
667 /// is given by orienting the edges of the original graph in both directions.
668 /// An example for such a construction is the \c RevGraphWrapper where the
669 /// forward_filter is everywhere false and the backward_filter is
670 /// everywhere true. We note that for sake of efficiency,
671 /// \c RevGraphWrapper is implemented in a different way.
672 /// But BidirGraphWrapper is obtained from
673 /// SubBidirGraphWrapper by considering everywhere true
674 /// predicates both forward_filter and backward_filter.
675 /// Finally, one of the most important applications of SubBidirGraphWrapper
676 /// is ResGraphWrapper, which stands for the residual graph in directed
677 /// flow and circulation problems.
678 /// As wrappers usually, the SubBidirGraphWrapper implements the
679 /// above mentioned graph structure without its physical storage,
680 /// that is the whole stuff eats constant memory.
681 /// As the oppositely directed edges are logical different,
682 /// the maps are able to attach different values for them.
683 template<typename Graph,
684 typename ForwardFilterMap, typename BackwardFilterMap>
685 class SubBidirGraphWrapper : public GraphWrapper<Graph> {
687 typedef GraphWrapper<Graph> Parent;
689 ForwardFilterMap* forward_filter;
690 BackwardFilterMap* backward_filter;
692 SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
693 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
694 forward_filter=&_forward_filter;
696 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
697 backward_filter=&_backward_filter;
702 SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
703 BackwardFilterMap& _backward_filter) :
704 GraphWrapper<Graph>(_graph),
705 forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
706 SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph,
707 ForwardFilterMap, BackwardFilterMap>& gw) :
709 forward_filter(gw.forward_filter),
710 backward_filter(gw.backward_filter) { }
715 friend class OutEdgeIt;
717 template<typename T> class EdgeMap;
719 typedef typename GraphWrapper<Graph>::Node Node;
721 typedef typename Graph::Edge GraphEdge;
722 /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from
723 /// Graph::Edge. It contains an extra bool flag which shows if the
724 /// edge is the backward version of the original edge.
725 class Edge : public Graph::Edge {
726 friend class SubBidirGraphWrapper<Graph,
727 ForwardFilterMap, BackwardFilterMap>;
728 template<typename T> friend class EdgeMap;
730 bool backward; //true, iff backward
733 /// \todo =false is needed, or causes problems?
734 /// If \c _backward is false, then we get an edge corresponding to the
735 /// original one, otherwise its oppositely directed pair is obtained.
736 Edge(const typename Graph::Edge& e, bool _backward/*=false*/) :
737 Graph::Edge(e), backward(_backward) { }
738 Edge(Invalid i) : Graph::Edge(i), backward(true) { }
739 //the unique invalid iterator
740 // friend bool operator==(const Edge& u, const Edge& v) {
741 // return (u.backward==v.backward &&
742 // static_cast<typename Graph::Edge>(u)==
743 // static_cast<typename Graph::Edge>(v));
745 // friend bool operator!=(const Edge& u, const Edge& v) {
746 // return (u.backward!=v.backward ||
747 // static_cast<typename Graph::Edge>(u)!=
748 // static_cast<typename Graph::Edge>(v));
750 bool operator==(const Edge& v) const {
751 return (this->backward==v.backward &&
752 static_cast<typename Graph::Edge>(*this)==
753 static_cast<typename Graph::Edge>(v));
755 bool operator!=(const Edge& v) const {
756 return (this->backward!=v.backward ||
757 static_cast<typename Graph::Edge>(*this)!=
758 static_cast<typename Graph::Edge>(v));
762 class OutEdgeIt : public Edge {
763 friend class SubBidirGraphWrapper<Graph,
764 ForwardFilterMap, BackwardFilterMap>;
766 const SubBidirGraphWrapper<Graph,
767 ForwardFilterMap, BackwardFilterMap>* gw;
770 OutEdgeIt(Invalid i) : Edge(i) { }
771 //the unique invalid iterator
772 OutEdgeIt(const SubBidirGraphWrapper<Graph,
773 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
774 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
775 while (*static_cast<GraphEdge*>(this)!=INVALID &&
776 !(*(gw->forward_filter))[*this])
777 *(static_cast<GraphEdge*>(this))=
778 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
779 if (*static_cast<GraphEdge*>(this)==INVALID) {
780 *static_cast<Edge*>(this)=
781 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
782 while (*static_cast<GraphEdge*>(this)!=INVALID &&
783 !(*(gw->backward_filter))[*this])
784 *(static_cast<GraphEdge*>(this))=
785 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
788 OutEdgeIt(const SubBidirGraphWrapper<Graph,
789 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
790 Edge(e), gw(&_gw) { }
791 OutEdgeIt& operator++() {
792 if (!this->backward) {
793 Node n=gw->tail(*this);
794 *(static_cast<GraphEdge*>(this))=
795 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
796 while (*static_cast<GraphEdge*>(this)!=INVALID &&
797 !(*(gw->forward_filter))[*this])
798 *(static_cast<GraphEdge*>(this))=
799 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
800 if (*static_cast<GraphEdge*>(this)==INVALID) {
801 *static_cast<Edge*>(this)=
802 Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
803 while (*static_cast<GraphEdge*>(this)!=INVALID &&
804 !(*(gw->backward_filter))[*this])
805 *(static_cast<GraphEdge*>(this))=
806 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
809 *(static_cast<GraphEdge*>(this))=
810 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
811 while (*static_cast<GraphEdge*>(this)!=INVALID &&
812 !(*(gw->backward_filter))[*this])
813 *(static_cast<GraphEdge*>(this))=
814 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
820 class InEdgeIt : public Edge {
821 friend class SubBidirGraphWrapper<Graph,
822 ForwardFilterMap, BackwardFilterMap>;
824 const SubBidirGraphWrapper<Graph,
825 ForwardFilterMap, BackwardFilterMap>* gw;
828 InEdgeIt(Invalid i) : Edge(i) { }
829 //the unique invalid iterator
830 InEdgeIt(const SubBidirGraphWrapper<Graph,
831 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
832 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
833 while (*static_cast<GraphEdge*>(this)!=INVALID &&
834 !(*(gw->forward_filter))[*this])
835 *(static_cast<GraphEdge*>(this))=
836 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
837 if (*static_cast<GraphEdge*>(this)==INVALID) {
838 *static_cast<Edge*>(this)=
839 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
840 while (*static_cast<GraphEdge*>(this)!=INVALID &&
841 !(*(gw->backward_filter))[*this])
842 *(static_cast<GraphEdge*>(this))=
843 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
846 InEdgeIt(const SubBidirGraphWrapper<Graph,
847 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
848 Edge(e), gw(&_gw) { }
849 InEdgeIt& operator++() {
850 if (!this->backward) {
851 Node n=gw->tail(*this);
852 *(static_cast<GraphEdge*>(this))=
853 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
854 while (*static_cast<GraphEdge*>(this)!=INVALID &&
855 !(*(gw->forward_filter))[*this])
856 *(static_cast<GraphEdge*>(this))=
857 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
858 if (*static_cast<GraphEdge*>(this)==INVALID) {
859 *static_cast<Edge*>(this)=
860 Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
861 while (*static_cast<GraphEdge*>(this)!=INVALID &&
862 !(*(gw->backward_filter))[*this])
863 *(static_cast<GraphEdge*>(this))=
864 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
867 *(static_cast<GraphEdge*>(this))=
868 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
869 while (*static_cast<GraphEdge*>(this)!=INVALID &&
870 !(*(gw->backward_filter))[*this])
871 *(static_cast<GraphEdge*>(this))=
872 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
878 class EdgeIt : public Edge {
879 friend class SubBidirGraphWrapper<Graph,
880 ForwardFilterMap, BackwardFilterMap>;
882 const SubBidirGraphWrapper<Graph,
883 ForwardFilterMap, BackwardFilterMap>* gw;
886 EdgeIt(Invalid i) : Edge(i) { }
887 //the unique invalid iterator
888 EdgeIt(const SubBidirGraphWrapper<Graph,
889 ForwardFilterMap, BackwardFilterMap>& _gw) :
890 Edge(typename Graph::OutEdgeIt(*(_gw.graph)), false), gw(&_gw) {
891 while (*static_cast<GraphEdge*>(this)!=INVALID &&
892 !(*(gw->forward_filter))[*this])
893 *(static_cast<GraphEdge*>(this))=
894 ++(typename Graph::EdgeIt(*(gw->graph), *this));
895 if (*static_cast<GraphEdge*>(this)==INVALID) {
896 *static_cast<Edge*>(this)=
897 Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
898 while (*static_cast<GraphEdge*>(this)!=INVALID &&
899 !(*(gw->backward_filter))[*this])
900 *(static_cast<GraphEdge*>(this))=
901 ++(typename Graph::EdgeIt(*(gw->graph), *this));
904 EdgeIt(const SubBidirGraphWrapper<Graph,
905 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
906 Edge(e), gw(&_gw) { }
907 EdgeIt& operator++() {
908 if (!this->backward) {
909 *(static_cast<GraphEdge*>(this))=
910 ++(typename Graph::EdgeIt(*(gw->graph), *this));
911 while (*static_cast<GraphEdge*>(this)!=INVALID &&
912 !(*(gw->forward_filter))[*this])
913 *(static_cast<GraphEdge*>(this))=
914 ++(typename Graph::EdgeIt(*(gw->graph), *this));
915 if (*static_cast<GraphEdge*>(this)==INVALID) {
916 *static_cast<Edge*>(this)=
917 Edge(typename Graph::EdgeIt(*(gw->graph)), true);
918 while (*static_cast<GraphEdge*>(this)!=INVALID &&
919 !(*(gw->backward_filter))[*this])
920 *(static_cast<GraphEdge*>(this))=
921 ++(typename Graph::EdgeIt(*(gw->graph), *this));
924 *(static_cast<GraphEdge*>(this))=
925 ++(typename Graph::EdgeIt(*(gw->graph), *this));
926 while (*static_cast<GraphEdge*>(this)!=INVALID &&
927 !(*(gw->backward_filter))[*this])
928 *(static_cast<GraphEdge*>(this))=
929 ++(typename Graph::EdgeIt(*(gw->graph), *this));
935 using GraphWrapper<Graph>::first;
936 // NodeIt& first(NodeIt& i) const {
937 // i=NodeIt(*this); return i;
939 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
940 i=OutEdgeIt(*this, p); return i;
942 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
943 i=InEdgeIt(*this, p); return i;
945 EdgeIt& first(EdgeIt& i) const {
946 i=EdgeIt(*this); return i;
949 // using GraphWrapper<Graph>::next;
950 // // NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
951 // OutEdgeIt& next(OutEdgeIt& e) const {
952 // if (!e.backward) {
953 // Node v=this->graph->aNode(e.out);
954 // this->graph->next(e.out);
955 // while(this->graph->valid(e.out) && !(*forward_filter)[e]) {
956 // this->graph->next(e.out); }
957 // if (!this->graph->valid(e.out)) {
959 // this->graph->first(e.in, v);
960 // while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
961 // this->graph->next(e.in); }
964 // this->graph->next(e.in);
965 // while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
966 // this->graph->next(e.in); }
970 // // FIXME Not tested
971 // InEdgeIt& next(InEdgeIt& e) const {
972 // if (!e.backward) {
973 // Node v=this->graph->aNode(e.in);
974 // this->graph->next(e.in);
975 // while(this->graph->valid(e.in) && !(*forward_filter)[e]) {
976 // this->graph->next(e.in); }
977 // if (!this->graph->valid(e.in)) {
979 // this->graph->first(e.out, v);
980 // while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
981 // this->graph->next(e.out); }
984 // this->graph->next(e.out);
985 // while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
986 // this->graph->next(e.out); }
990 // EdgeIt& next(EdgeIt& e) const {
991 // if (!e.backward) {
992 // this->graph->next(e.e);
993 // while(this->graph->valid(e.e) && !(*forward_filter)[e]) {
994 // this->graph->next(e.e); }
995 // if (!this->graph->valid(e.e)) {
997 // this->graph->first(e.e);
998 // while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
999 // this->graph->next(e.e); }
1002 // this->graph->next(e.e);
1003 // while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
1004 // this->graph->next(e.e); }
1009 Node tail(Edge e) const {
1010 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
1011 Node head(Edge e) const {
1012 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
1014 // Node aNode(OutEdgeIt e) const {
1015 // return ((!e.backward) ? this->graph->aNode(e.out) :
1016 // this->graph->aNode(e.in)); }
1017 // Node bNode(OutEdgeIt e) const {
1018 // return ((!e.backward) ? this->graph->bNode(e.out) :
1019 // this->graph->bNode(e.in)); }
1021 // Node aNode(InEdgeIt e) const {
1022 // return ((!e.backward) ? this->graph->aNode(e.in) :
1023 // this->graph->aNode(e.out)); }
1024 // Node bNode(InEdgeIt e) const {
1025 // return ((!e.backward) ? this->graph->bNode(e.in) :
1026 // this->graph->bNode(e.out)); }
1028 /// Gives back the opposite edge.
1029 Edge opposite(const Edge& e) const {
1031 f.backward=!f.backward;
1035 /// \warning This is a linear time operation and works only if
1036 /// \c Graph::EdgeIt is defined.
1037 int edgeNum() const {
1039 for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
1043 bool forward(const Edge& e) const { return !e.backward; }
1044 bool backward(const Edge& e) const { return e.backward; }
1047 template <typename T>
1048 /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two
1049 /// Graph::EdgeMap one for the forward edges and
1050 /// one for the backward edges.
1052 typename Graph::template EdgeMap<T> forward_map, backward_map;
1054 typedef T ValueType;
1055 typedef Edge KeyType;
1056 EdgeMap(const SubBidirGraphWrapper<Graph,
1057 ForwardFilterMap, BackwardFilterMap>& g) :
1058 forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1059 EdgeMap(const SubBidirGraphWrapper<Graph,
1060 ForwardFilterMap, BackwardFilterMap>& g, T a) :
1061 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1062 void set(Edge e, T a) {
1064 forward_map.set(e, a);
1066 backward_map.set(e, a);
1068 T operator[](Edge e) const {
1070 return forward_map[e];
1072 return backward_map[e];
1075 forward_map.update();
1076 backward_map.update();
1078 // T get(Edge e) const {
1080 // return forward_map.get(e.out);
1082 // return backward_map.get(e.in);
1088 ///\brief A wrapper for composing bidirected graph from a directed one.
1090 /// A wrapper for composing bidirected graph from a directed one.
1091 /// A bidirected graph is composed over the directed one without physical
1092 /// storage. As the oppositely directed edges are logically different ones
1093 /// the maps are able to attach different values for them.
1094 template<typename Graph>
1095 class BidirGraphWrapper :
1096 public SubBidirGraphWrapper<
1098 ConstMap<typename Graph::Edge, bool>,
1099 ConstMap<typename Graph::Edge, bool> > {
1101 typedef SubBidirGraphWrapper<
1103 ConstMap<typename Graph::Edge, bool>,
1104 ConstMap<typename Graph::Edge, bool> > Parent;
1106 ConstMap<typename Graph::Edge, bool> cm;
1108 BidirGraphWrapper() : Parent(), cm(true) {
1109 Parent::setForwardFilterMap(cm);
1110 Parent::setBackwardFilterMap(cm);
1113 BidirGraphWrapper(Graph& _graph) : Parent() {
1114 Parent::setGraph(_graph);
1115 Parent::setForwardFilterMap(cm);
1116 Parent::setBackwardFilterMap(cm);
1119 int edgeNum() const {
1120 return 2*this->graph->edgeNum();
1125 /// \brief A bidirected graph template.
1127 /// A bidirected graph template.
1128 /// Such a bidirected graph stores each pair of oppositely directed edges
1129 /// ones in the memory, i.e. a directed graph of type
1130 /// \c Graph is used for that.
1131 /// As the oppositely directed edges are logically different ones
1132 /// the maps are able to attach different values for them.
1134 template<typename Graph>
1135 class BidirGraph : public BidirGraphWrapper<Graph> {
1137 typedef UndirGraphWrapper<Graph> Parent;
1141 BidirGraph() : BidirGraphWrapper<Graph>() {
1142 Parent::setGraph(gr);
1148 template<typename Graph, typename Number,
1149 typename CapacityMap, typename FlowMap>
1150 class ResForwardFilter {
1151 // const Graph* graph;
1152 const CapacityMap* capacity;
1153 const FlowMap* flow;
1155 ResForwardFilter(/*const Graph& _graph, */
1156 const CapacityMap& _capacity, const FlowMap& _flow) :
1157 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1158 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1159 //void setGraph(const Graph& _graph) { graph=&_graph; }
1160 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1161 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1162 bool operator[](const typename Graph::Edge& e) const {
1163 return (Number((*flow)[e]) < Number((*capacity)[e]));
1167 template<typename Graph, typename Number,
1168 typename CapacityMap, typename FlowMap>
1169 class ResBackwardFilter {
1170 //const Graph* graph;
1171 const CapacityMap* capacity;
1172 const FlowMap* flow;
1174 ResBackwardFilter(/*const Graph& _graph,*/
1175 const CapacityMap& _capacity, const FlowMap& _flow) :
1176 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1177 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1178 //void setGraph(const Graph& _graph) { graph=&_graph; }
1179 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1180 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1181 bool operator[](const typename Graph::Edge& e) const {
1182 return (Number(0) < Number((*flow)[e]));
1187 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1189 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1190 template<typename Graph, typename Number,
1191 typename CapacityMap, typename FlowMap>
1192 class ResGraphWrapper :
1193 public SubBidirGraphWrapper<
1195 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1196 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
1198 typedef SubBidirGraphWrapper<
1200 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1201 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
1203 const CapacityMap* capacity;
1205 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
1206 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
1207 ResGraphWrapper() : Parent(),
1208 capacity(0), flow(0) { }
1209 void setCapacityMap(const CapacityMap& _capacity) {
1210 capacity=&_capacity;
1211 forward_filter.setCapacity(_capacity);
1212 backward_filter.setCapacity(_capacity);
1214 void setFlowMap(FlowMap& _flow) {
1216 forward_filter.setFlow(_flow);
1217 backward_filter.setFlow(_flow);
1219 // /// \bug does graph reference needed in filtermaps??
1220 // void setGraph(const Graph& _graph) {
1221 // Parent::setGraph(_graph);
1222 // forward_filter.setGraph(_graph);
1223 // backward_filter.setGraph(_graph);
1226 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1228 Parent(), capacity(&_capacity), flow(&_flow),
1229 forward_filter(/*_graph,*/ _capacity, _flow),
1230 backward_filter(/*_graph,*/ _capacity, _flow) {
1231 Parent::setGraph(_graph);
1232 Parent::setForwardFilterMap(forward_filter);
1233 Parent::setBackwardFilterMap(backward_filter);
1236 typedef typename Parent::Edge Edge;
1238 // bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
1239 //bool backward(const Edge& e) const { return e.backward; }
1241 void augment(const Edge& e, Number a) const {
1242 if (Parent::forward(e))
1243 // flow->set(e.out, flow->get(e.out)+a);
1244 flow->set(e, (*flow)[e]+a);
1246 //flow->set(e.in, flow->get(e.in)-a);
1247 flow->set(e, (*flow)[e]-a);
1252 Number resCap(const Edge& e) const {
1253 if (Parent::forward(e))
1254 // return (capacity->get(e.out)-flow->get(e.out));
1255 return ((*capacity)[e]-(*flow)[e]);
1257 // return (flow->get(e.in));
1258 return ((*flow)[e]);
1261 /// \brief Residual capacity map.
1263 /// In generic residual graphs the residual capacity can be obtained as a map. Not tested.
1266 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
1268 typedef Number ValueType;
1269 typedef Edge KeyType;
1270 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) :
1271 res_graph(&_res_graph) { }
1272 Number operator[](const Edge& e) const {
1273 if (res_graph->forward(e))
1274 // return (capacity->get(e.out)-flow->get(e.out));
1275 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1277 // return (flow->get(e.in));
1278 return (*(res_graph->flow))[e];
1280 /// \bug not needed with dynamic maps, or does it?
1287 /// For blocking flows.
1289 /// This graph wrapper is used for on-the-fly
1290 /// Dinits blocking flow computations.
1291 /// For each node, an out-edge is stored which is used when the
1293 /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1297 /// \author Marton Makai
1298 template<typename Graph, typename FirstOutEdgesMap>
1299 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1301 typedef GraphWrapper<Graph> Parent;
1303 FirstOutEdgesMap* first_out_edges;
1305 ErasingFirstGraphWrapper(Graph& _graph,
1306 FirstOutEdgesMap& _first_out_edges) :
1307 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1309 typedef typename GraphWrapper<Graph>::Node Node;
1310 typedef typename GraphWrapper<Graph>::Edge Edge;
1311 class OutEdgeIt : public Edge {
1312 friend class GraphWrapper<Graph>;
1313 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1314 const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
1317 //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
1318 OutEdgeIt(Invalid i) : Edge(i) { }
1319 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1321 Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
1322 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1324 Edge(e), gw(&_gw) { }
1325 OutEdgeIt& operator++() {
1326 *(static_cast<Edge*>(this))=
1327 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1332 // friend class GraphWrapper<Graph>;
1333 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1334 // // typedef typename Graph::InEdgeIt GraphInEdgeIt;
1335 // typename Graph::InEdgeIt e;
1338 // InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
1339 // InEdgeIt(const Invalid& i) : e(i) { }
1340 // InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1341 // const Node& _n) :
1342 // e(*(_G.graph), typename Graph::Node(_n)) { }
1343 // operator Edge() const { return Edge(typename Graph::Edge(e)); }
1345 //typedef typename Graph::SymEdgeIt SymEdgeIt;
1347 // friend class GraphWrapper<Graph>;
1348 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1349 // // typedef typename Graph::EdgeIt GraphEdgeIt;
1350 // typename Graph::EdgeIt e;
1353 // EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
1354 // EdgeIt(const Invalid& i) : e(i) { }
1355 // EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1356 // e(*(_G.graph)) { }
1357 // operator Edge() const { return Edge(typename Graph::Edge(e)); }
1360 using GraphWrapper<Graph>::first;
1361 // NodeIt& first(NodeIt& i) const {
1362 // i=NodeIt(*this); return i;
1364 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1365 i=OutEdgeIt(*this, p); return i;
1367 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1368 // i=InEdgeIt(*this, p); return i;
1370 // EdgeIt& first(EdgeIt& i) const {
1371 // i=EdgeIt(*this); return i;
1374 // using GraphWrapper<Graph>::next;
1375 // // NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
1376 // OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
1377 // InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
1378 // EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }
1380 // Node aNode(const OutEdgeIt& e) const {
1381 // return Node(this->graph->aNode(e.e)); }
1382 // Node aNode(const InEdgeIt& e) const {
1383 // return Node(this->graph->aNode(e.e)); }
1384 // Node bNode(const OutEdgeIt& e) const {
1385 // return Node(this->graph->bNode(e.e)); }
1386 // Node bNode(const InEdgeIt& e) const {
1387 // return Node(this->graph->bNode(e.e)); }
1389 void erase(const Edge& e) const {
1391 typename Graph::OutEdgeIt f(*Parent::graph, n);
1393 first_out_edges->set(n, f);
1401 #endif //HUGO_GRAPH_WRAPPER_H