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) { }
232 // NodeMap(const NodeMap<T>& map) : Parent(map) { }
233 // template<typename Map>
234 // NodeMap(const Map& map) : Parent(map) { }
237 template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
238 typedef typename Graph::template EdgeMap<T> Parent;
240 EdgeMap(const GraphWrapper<Graph>& gw) : Parent(*(gw.graph)) { }
241 EdgeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
242 // EdgeMap(const EdgeMap<T>& map) : Parent(map) { }
243 // template<typename Map>
244 // EdgeMap(const Map& map) : Parent(map) { }
250 /// A graph wrapper which reverses the orientation of the edges.
252 /// A graph wrapper which reverses the orientation of the edges.
253 /// Thus \c Graph have to be a directed graph type.
255 ///\author Marton Makai
256 template<typename Graph>
257 class RevGraphWrapper : public GraphWrapper<Graph> {
259 typedef GraphWrapper<Graph> Parent;
261 RevGraphWrapper() : GraphWrapper<Graph>() { }
263 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
264 RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
266 typedef typename GraphWrapper<Graph>::Node Node;
267 typedef typename GraphWrapper<Graph>::Edge Edge;
268 //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
269 //because this does not work is some of them are not defined in the
270 //original graph. The problem with this is that typedef-ed stuff
271 //are instantiated in c++.
272 class OutEdgeIt : public Edge {
273 const RevGraphWrapper<Graph>* gw;
274 friend class GraphWrapper<Graph>;
277 //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
278 OutEdgeIt(Invalid i) : Edge(i) { }
279 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
280 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
281 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
282 Edge(e), gw(&_gw) { }
283 OutEdgeIt& operator++() {
284 *(static_cast<Edge*>(this))=
285 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
289 class InEdgeIt : public Edge {
290 const RevGraphWrapper<Graph>* gw;
291 friend class GraphWrapper<Graph>;
294 //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
295 InEdgeIt(Invalid i) : Edge(i) { }
296 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
297 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
298 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
299 Edge(e), gw(&_gw) { }
300 InEdgeIt& operator++() {
301 *(static_cast<Edge*>(this))=
302 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
307 using GraphWrapper<Graph>::first;
308 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
309 i=OutEdgeIt(*this, p); return i;
311 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
312 i=InEdgeIt(*this, p); return i;
315 // using GraphWrapper<Graph>::next;
316 // OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
317 // InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
319 // Node aNode(const OutEdgeIt& e) const {
320 // return Node(this->graph->aNode(e.e)); }
321 // Node aNode(const InEdgeIt& e) const {
322 // return Node(this->graph->aNode(e.e)); }
323 // Node bNode(const OutEdgeIt& e) const {
324 // return Node(this->graph->bNode(e.e)); }
325 // Node bNode(const InEdgeIt& e) const {
326 // return Node(this->graph->bNode(e.e)); }
328 Node tail(const Edge& e) const {
329 return GraphWrapper<Graph>::head(e); }
330 Node head(const Edge& e) const {
331 return GraphWrapper<Graph>::tail(e); }
337 /// A graph wrapper for hiding nodes and edges from a graph.
339 /// This wrapper shows a graph with filtered node-set and
340 /// edge-set. Given a bool-valued map on the node-set and one on
341 /// the edge-set of the graphs, the iterators shows only the objects
342 /// having true value.
343 /// The quick brown fox iterators jump over
344 /// the lazy dog nodes or edges if their values for are false in the
345 /// corresponding bool maps.
347 ///\author Marton Makai
348 template<typename Graph, typename NodeFilterMap,
349 typename EdgeFilterMap>
350 class SubGraphWrapper : public GraphWrapper<Graph> {
352 typedef GraphWrapper<Graph> Parent;
354 NodeFilterMap* node_filter_map;
355 EdgeFilterMap* edge_filter_map;
357 SubGraphWrapper() : GraphWrapper<Graph>(),
358 node_filter_map(0), edge_filter_map(0) { }
359 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
360 node_filter_map=&_node_filter_map;
362 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
363 edge_filter_map=&_edge_filter_map;
367 SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
368 EdgeFilterMap& _edge_filter_map) :
369 GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
370 edge_filter_map(&_edge_filter_map) { }
372 typedef typename GraphWrapper<Graph>::Node Node;
373 class NodeIt : public Node {
374 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
375 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
378 // NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
379 NodeIt(Invalid i) : Node(i) { }
380 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
381 Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) {
382 while (*static_cast<Node*>(this)!=INVALID &&
383 !(*(gw->edge_filter_map))[*this])
384 *(static_cast<Node*>(this))=
385 ++(typename Graph::NodeIt(*(gw->graph), *this));
387 NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
389 Node(n), gw(&_gw) { }
390 NodeIt& operator++() {
391 *(static_cast<Node*>(this))=
392 ++(typename Graph::NodeIt(*(gw->graph), *this));
393 while (*static_cast<Node*>(this)!=INVALID &&
394 !(*(gw->node_filter_map))[*this])
395 *(static_cast<Node*>(this))=
396 ++(typename Graph::NodeIt(*(gw->graph), *this));
400 typedef typename GraphWrapper<Graph>::Edge Edge;
401 class OutEdgeIt : public Edge {
402 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
403 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
406 // OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
407 OutEdgeIt(Invalid i) : Edge(i) { }
408 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
409 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) {
410 while (*static_cast<Edge*>(this)!=INVALID &&
411 !(*(gw->edge_filter_map))[*this])
412 *(static_cast<Edge*>(this))=
413 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
415 OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
417 Edge(e), gw(&_gw) { }
418 OutEdgeIt& operator++() {
419 *(static_cast<Edge*>(this))=
420 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
421 while (*static_cast<Edge*>(this)!=INVALID &&
422 !(*(gw->edge_filter_map))[*this])
423 *(static_cast<Edge*>(this))=
424 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
428 class InEdgeIt : public Edge {
429 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
430 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
433 // InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
434 InEdgeIt(Invalid i) : Edge(i) { }
435 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
436 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) {
437 while (*static_cast<Edge*>(this)!=INVALID &&
438 !(*(gw->edge_filter_map))[*this])
439 *(static_cast<Edge*>(this))=
440 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
442 InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
444 Edge(e), gw(&_gw) { }
445 InEdgeIt& operator++() {
446 *(static_cast<Edge*>(this))=
447 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
448 while (*static_cast<Edge*>(this)!=INVALID &&
449 !(*(gw->edge_filter_map))[*this])
450 *(static_cast<Edge*>(this))=
451 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
455 class EdgeIt : public Edge {
456 const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
457 friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
460 // EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
461 EdgeIt(Invalid i) : Edge(i) { }
462 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
463 Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) {
464 while (*static_cast<Edge*>(this)!=INVALID &&
465 !(*(gw->edge_filter_map))[*this])
466 *(static_cast<Edge*>(this))=
467 ++(typename Graph::EdgeIt(*(gw->graph), *this));
469 EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
471 Edge(e), gw(&_gw) { }
472 EdgeIt& operator++() {
473 *(static_cast<Edge*>(this))=
474 ++(typename Graph::EdgeIt(*(gw->graph), *this));
475 while (*static_cast<Edge*>(this)!=INVALID &&
476 !(*(gw->edge_filter_map))[*this])
477 *(static_cast<Edge*>(this))=
478 ++(typename Graph::EdgeIt(*(gw->graph), *this));
483 NodeIt& first(NodeIt& i) const {
484 i=NodeIt(*this); return i;
486 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
487 i=OutEdgeIt(*this, p); return i;
489 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
490 i=InEdgeIt(*this, p); return i;
492 EdgeIt& first(EdgeIt& i) const {
493 i=EdgeIt(*this); return i;
496 // NodeIt& next(NodeIt& i) const {
497 // this->graph->next(i.n);
498 // while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
499 // this->graph->next(i.n); }
502 // OutEdgeIt& next(OutEdgeIt& i) const {
503 // this->graph->next(i.e);
504 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
505 // this->graph->next(i.e); }
508 // InEdgeIt& next(InEdgeIt& i) const {
509 // this->graph->next(i.e);
510 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
511 // this->graph->next(i.e); }
514 // EdgeIt& next(EdgeIt& i) const {
515 // this->graph->next(i.e);
516 // while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
517 // this->graph->next(i.e); }
521 // Node aNode(const OutEdgeIt& e) const {
522 // return Node(this->graph->aNode(e.e)); }
523 // Node aNode(const InEdgeIt& e) const {
524 // return Node(this->graph->aNode(e.e)); }
525 // Node bNode(const OutEdgeIt& e) const {
526 // return Node(this->graph->bNode(e.e)); }
527 // Node bNode(const InEdgeIt& e) const {
528 // return Node(this->graph->bNode(e.e)); }
530 /// This function hides \c n in the graph, i.e. the iteration
531 /// jumps over it. This is done by simply setting the value of \c n
532 /// to be false in the corresponding node-map.
533 void hide(const Node& n) const { node_filter_map->set(n, false); }
535 /// This function hides \c e in the graph, i.e. the iteration
536 /// jumps over it. This is done by simply setting the value of \c e
537 /// to be false in the corresponding edge-map.
538 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
540 /// The value of \c n is set to be true in the node-map which stores
541 /// hide information. If \c n was hidden previuosly, then it is shown
543 void unHide(const Node& n) const { node_filter_map->set(n, true); }
545 /// The value of \c e is set to be true in the edge-map which stores
546 /// hide information. If \c e was hidden previuosly, then it is shown
548 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
550 /// Returns true if \c n is hidden.
551 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
553 /// Returns true if \c n is hidden.
554 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
556 /// \warning This is a linear time operation and works only if
557 /// \c Graph::NodeIt is defined.
558 int nodeNum() const {
560 for (NodeIt n(*this); n!=INVALID; ++n) ++i;
564 /// \warning This is a linear time operation and works only if
565 /// \c Graph::EdgeIt is defined.
566 int edgeNum() const {
568 for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
576 // /// \brief A wrapper for forgetting the orientation of a graph.
578 // /// A wrapper for getting an undirected graph by forgetting
579 // /// the orientation of a directed one.
581 // /// \author Marton Makai
582 // /// does not work in the new concept.
583 template<typename Graph>
584 class UndirGraphWrapper : public GraphWrapper<Graph> {
586 typedef GraphWrapper<Graph> Parent;
588 UndirGraphWrapper() : GraphWrapper<Graph>() { }
591 typedef typename GraphWrapper<Graph>::Node Node;
592 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
593 typedef typename GraphWrapper<Graph>::Edge Edge;
594 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
596 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
599 friend class UndirGraphWrapper<Graph>;
600 bool out_or_in; //true iff out
601 typename Graph::OutEdgeIt out;
602 typename Graph::InEdgeIt in;
605 OutEdgeIt(const Invalid& i) : Edge(i) { }
606 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
607 out_or_in=true; _G.graph->first(out, _n);
608 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
610 operator Edge() const {
611 if (out_or_in) return Edge(out); else return Edge(in);
616 typedef OutEdgeIt InEdgeIt;
618 using GraphWrapper<Graph>::first;
619 // NodeIt& first(NodeIt& i) const {
620 // i=NodeIt(*this); return i;
622 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
623 i=OutEdgeIt(*this, p); return i;
626 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
627 // i=InEdgeIt(*this, p); return i;
629 // EdgeIt& first(EdgeIt& i) const {
630 // i=EdgeIt(*this); return i;
633 using GraphWrapper<Graph>::next;
634 // NodeIt& next(NodeIt& n) const {
635 // GraphWrapper<Graph>::next(n);
638 OutEdgeIt& next(OutEdgeIt& e) const {
640 typename Graph::Node n=this->graph->tail(e.out);
641 this->graph->next(e.out);
642 if (!this->graph->valid(e.out)) {
643 e.out_or_in=false; this->graph->first(e.in, n); }
645 this->graph->next(e.in);
650 // EdgeIt& next(EdgeIt& e) const {
651 // GraphWrapper<Graph>::next(n);
652 // // graph->next(e.e);
656 Node aNode(const OutEdgeIt& e) const {
657 if (e.out_or_in) return this->graph->tail(e); else
658 return this->graph->head(e); }
659 Node bNode(const OutEdgeIt& e) const {
660 if (e.out_or_in) return this->graph->head(e); else
661 return this->graph->tail(e); }
664 /// \brief An undirected graph template.
666 /// An undirected graph template.
667 /// This class works as an undirected graph and a directed graph of
668 /// class \c Graph is used for the physical storage.
670 template<typename Graph>
671 class UndirGraph : public UndirGraphWrapper<Graph> {
672 typedef UndirGraphWrapper<Graph> Parent;
676 UndirGraph() : UndirGraphWrapper<Graph>() {
677 Parent::setGraph(gr);
683 ///\brief A wrapper for composing a subgraph of a
684 /// bidirected graph made from a directed one.
686 /// Suppose that for a directed graph $G=(V, A)$,
687 /// two predicates on the edge-set, $forward_filter$, and $backward_filter$
688 /// is given, and we are dealing with the directed graph
689 /// 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}\})$.
690 /// The purpose of writing + instead of union is because parallel
692 /// In other words, a subgraph of the bidirected graph obtained, which
693 /// is given by orienting the edges of the original graph in both directions.
694 /// An example for such a construction is the \c RevGraphWrapper where the
695 /// forward_filter is everywhere false and the backward_filter is
696 /// everywhere true. We note that for sake of efficiency,
697 /// \c RevGraphWrapper is implemented in a different way.
698 /// But BidirGraphWrapper is obtained from
699 /// SubBidirGraphWrapper by considering everywhere true
700 /// predicates both forward_filter and backward_filter.
701 /// Finally, one of the most important applications of SubBidirGraphWrapper
702 /// is ResGraphWrapper, which stands for the residual graph in directed
703 /// flow and circulation problems.
704 /// As wrappers usually, the SubBidirGraphWrapper implements the
705 /// above mentioned graph structure without its physical storage,
706 /// that is the whole stuff eats constant memory.
707 /// As the oppositely directed edges are logical different,
708 /// the maps are able to attach different values for them.
709 template<typename Graph,
710 typename ForwardFilterMap, typename BackwardFilterMap>
711 class SubBidirGraphWrapper : public GraphWrapper<Graph> {
713 typedef GraphWrapper<Graph> Parent;
715 ForwardFilterMap* forward_filter;
716 BackwardFilterMap* backward_filter;
718 SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
719 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
720 forward_filter=&_forward_filter;
722 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
723 backward_filter=&_backward_filter;
728 SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
729 BackwardFilterMap& _backward_filter) :
730 GraphWrapper<Graph>(_graph),
731 forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
732 SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph,
733 ForwardFilterMap, BackwardFilterMap>& gw) :
735 forward_filter(gw.forward_filter),
736 backward_filter(gw.backward_filter) { }
741 friend class OutEdgeIt;
743 template<typename T> class EdgeMap;
745 typedef typename GraphWrapper<Graph>::Node Node;
747 typedef typename Graph::Edge GraphEdge;
748 /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from
749 /// Graph::Edge. It contains an extra bool flag which shows if the
750 /// edge is the backward version of the original edge.
751 class Edge : public Graph::Edge {
752 friend class SubBidirGraphWrapper<Graph,
753 ForwardFilterMap, BackwardFilterMap>;
754 template<typename T> friend class EdgeMap;
756 bool backward; //true, iff backward
759 /// \todo =false is needed, or causes problems?
760 /// If \c _backward is false, then we get an edge corresponding to the
761 /// original one, otherwise its oppositely directed pair is obtained.
762 Edge(const typename Graph::Edge& e, bool _backward/*=false*/) :
763 Graph::Edge(e), backward(_backward) { }
764 Edge(Invalid i) : Graph::Edge(i), backward(true) { }
765 //the unique invalid iterator
766 // friend bool operator==(const Edge& u, const Edge& v) {
767 // return (u.backward==v.backward &&
768 // static_cast<typename Graph::Edge>(u)==
769 // static_cast<typename Graph::Edge>(v));
771 // friend bool operator!=(const Edge& u, const Edge& v) {
772 // return (u.backward!=v.backward ||
773 // static_cast<typename Graph::Edge>(u)!=
774 // static_cast<typename Graph::Edge>(v));
776 bool operator==(const Edge& v) const {
777 return (this->backward==v.backward &&
778 static_cast<typename Graph::Edge>(*this)==
779 static_cast<typename Graph::Edge>(v));
781 bool operator!=(const Edge& v) const {
782 return (this->backward!=v.backward ||
783 static_cast<typename Graph::Edge>(*this)!=
784 static_cast<typename Graph::Edge>(v));
788 class OutEdgeIt : public Edge {
789 friend class SubBidirGraphWrapper<Graph,
790 ForwardFilterMap, BackwardFilterMap>;
792 const SubBidirGraphWrapper<Graph,
793 ForwardFilterMap, BackwardFilterMap>* gw;
796 OutEdgeIt(Invalid i) : Edge(i) { }
797 //the unique invalid iterator
798 OutEdgeIt(const SubBidirGraphWrapper<Graph,
799 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
800 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
801 while (*static_cast<GraphEdge*>(this)!=INVALID &&
802 !(*(gw->forward_filter))[*this])
803 *(static_cast<GraphEdge*>(this))=
804 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
805 if (*static_cast<GraphEdge*>(this)==INVALID) {
806 *static_cast<Edge*>(this)=
807 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
808 while (*static_cast<GraphEdge*>(this)!=INVALID &&
809 !(*(gw->backward_filter))[*this])
810 *(static_cast<GraphEdge*>(this))=
811 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
814 OutEdgeIt(const SubBidirGraphWrapper<Graph,
815 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
816 Edge(e), gw(&_gw) { }
817 OutEdgeIt& operator++() {
818 if (!this->backward) {
819 Node n=gw->tail(*this);
820 *(static_cast<GraphEdge*>(this))=
821 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
822 while (*static_cast<GraphEdge*>(this)!=INVALID &&
823 !(*(gw->forward_filter))[*this])
824 *(static_cast<GraphEdge*>(this))=
825 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
826 if (*static_cast<GraphEdge*>(this)==INVALID) {
827 *static_cast<Edge*>(this)=
828 Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
829 while (*static_cast<GraphEdge*>(this)!=INVALID &&
830 !(*(gw->backward_filter))[*this])
831 *(static_cast<GraphEdge*>(this))=
832 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
835 *(static_cast<GraphEdge*>(this))=
836 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
837 while (*static_cast<GraphEdge*>(this)!=INVALID &&
838 !(*(gw->backward_filter))[*this])
839 *(static_cast<GraphEdge*>(this))=
840 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
846 class InEdgeIt : public Edge {
847 friend class SubBidirGraphWrapper<Graph,
848 ForwardFilterMap, BackwardFilterMap>;
850 const SubBidirGraphWrapper<Graph,
851 ForwardFilterMap, BackwardFilterMap>* gw;
854 InEdgeIt(Invalid i) : Edge(i) { }
855 //the unique invalid iterator
856 InEdgeIt(const SubBidirGraphWrapper<Graph,
857 ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
858 Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
859 while (*static_cast<GraphEdge*>(this)!=INVALID &&
860 !(*(gw->forward_filter))[*this])
861 *(static_cast<GraphEdge*>(this))=
862 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
863 if (*static_cast<GraphEdge*>(this)==INVALID) {
864 *static_cast<Edge*>(this)=
865 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
866 while (*static_cast<GraphEdge*>(this)!=INVALID &&
867 !(*(gw->backward_filter))[*this])
868 *(static_cast<GraphEdge*>(this))=
869 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
872 InEdgeIt(const SubBidirGraphWrapper<Graph,
873 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
874 Edge(e), gw(&_gw) { }
875 InEdgeIt& operator++() {
876 if (!this->backward) {
877 Node n=gw->tail(*this);
878 *(static_cast<GraphEdge*>(this))=
879 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
880 while (*static_cast<GraphEdge*>(this)!=INVALID &&
881 !(*(gw->forward_filter))[*this])
882 *(static_cast<GraphEdge*>(this))=
883 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
884 if (*static_cast<GraphEdge*>(this)==INVALID) {
885 *static_cast<Edge*>(this)=
886 Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
887 while (*static_cast<GraphEdge*>(this)!=INVALID &&
888 !(*(gw->backward_filter))[*this])
889 *(static_cast<GraphEdge*>(this))=
890 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
893 *(static_cast<GraphEdge*>(this))=
894 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
895 while (*static_cast<GraphEdge*>(this)!=INVALID &&
896 !(*(gw->backward_filter))[*this])
897 *(static_cast<GraphEdge*>(this))=
898 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
904 class EdgeIt : public Edge {
905 friend class SubBidirGraphWrapper<Graph,
906 ForwardFilterMap, BackwardFilterMap>;
908 const SubBidirGraphWrapper<Graph,
909 ForwardFilterMap, BackwardFilterMap>* gw;
912 EdgeIt(Invalid i) : Edge(i) { }
913 //the unique invalid iterator
914 EdgeIt(const SubBidirGraphWrapper<Graph,
915 ForwardFilterMap, BackwardFilterMap>& _gw) :
916 Edge(typename Graph::OutEdgeIt(*(_gw.graph)), false), gw(&_gw) {
917 while (*static_cast<GraphEdge*>(this)!=INVALID &&
918 !(*(gw->forward_filter))[*this])
919 *(static_cast<GraphEdge*>(this))=
920 ++(typename Graph::EdgeIt(*(gw->graph), *this));
921 if (*static_cast<GraphEdge*>(this)==INVALID) {
922 *static_cast<Edge*>(this)=
923 Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
924 while (*static_cast<GraphEdge*>(this)!=INVALID &&
925 !(*(gw->backward_filter))[*this])
926 *(static_cast<GraphEdge*>(this))=
927 ++(typename Graph::EdgeIt(*(gw->graph), *this));
930 EdgeIt(const SubBidirGraphWrapper<Graph,
931 ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
932 Edge(e), gw(&_gw) { }
933 EdgeIt& operator++() {
934 if (!this->backward) {
935 *(static_cast<GraphEdge*>(this))=
936 ++(typename Graph::EdgeIt(*(gw->graph), *this));
937 while (*static_cast<GraphEdge*>(this)!=INVALID &&
938 !(*(gw->forward_filter))[*this])
939 *(static_cast<GraphEdge*>(this))=
940 ++(typename Graph::EdgeIt(*(gw->graph), *this));
941 if (*static_cast<GraphEdge*>(this)==INVALID) {
942 *static_cast<Edge*>(this)=
943 Edge(typename Graph::EdgeIt(*(gw->graph)), true);
944 while (*static_cast<GraphEdge*>(this)!=INVALID &&
945 !(*(gw->backward_filter))[*this])
946 *(static_cast<GraphEdge*>(this))=
947 ++(typename Graph::EdgeIt(*(gw->graph), *this));
950 *(static_cast<GraphEdge*>(this))=
951 ++(typename Graph::EdgeIt(*(gw->graph), *this));
952 while (*static_cast<GraphEdge*>(this)!=INVALID &&
953 !(*(gw->backward_filter))[*this])
954 *(static_cast<GraphEdge*>(this))=
955 ++(typename Graph::EdgeIt(*(gw->graph), *this));
961 using GraphWrapper<Graph>::first;
962 // NodeIt& first(NodeIt& i) const {
963 // i=NodeIt(*this); return i;
965 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
966 i=OutEdgeIt(*this, p); return i;
968 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
969 i=InEdgeIt(*this, p); return i;
971 EdgeIt& first(EdgeIt& i) const {
972 i=EdgeIt(*this); return i;
975 // using GraphWrapper<Graph>::next;
976 // // NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
977 // OutEdgeIt& next(OutEdgeIt& e) const {
978 // if (!e.backward) {
979 // Node v=this->graph->aNode(e.out);
980 // this->graph->next(e.out);
981 // while(this->graph->valid(e.out) && !(*forward_filter)[e]) {
982 // this->graph->next(e.out); }
983 // if (!this->graph->valid(e.out)) {
985 // this->graph->first(e.in, v);
986 // while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
987 // this->graph->next(e.in); }
990 // this->graph->next(e.in);
991 // while(this->graph->valid(e.in) && !(*backward_filter)[e]) {
992 // this->graph->next(e.in); }
996 // // FIXME Not tested
997 // InEdgeIt& next(InEdgeIt& e) const {
998 // if (!e.backward) {
999 // Node v=this->graph->aNode(e.in);
1000 // this->graph->next(e.in);
1001 // while(this->graph->valid(e.in) && !(*forward_filter)[e]) {
1002 // this->graph->next(e.in); }
1003 // if (!this->graph->valid(e.in)) {
1005 // this->graph->first(e.out, v);
1006 // while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
1007 // this->graph->next(e.out); }
1010 // this->graph->next(e.out);
1011 // while(this->graph->valid(e.out) && !(*backward_filter)[e]) {
1012 // this->graph->next(e.out); }
1016 // EdgeIt& next(EdgeIt& e) const {
1017 // if (!e.backward) {
1018 // this->graph->next(e.e);
1019 // while(this->graph->valid(e.e) && !(*forward_filter)[e]) {
1020 // this->graph->next(e.e); }
1021 // if (!this->graph->valid(e.e)) {
1023 // this->graph->first(e.e);
1024 // while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
1025 // this->graph->next(e.e); }
1028 // this->graph->next(e.e);
1029 // while(this->graph->valid(e.e) && !(*backward_filter)[e]) {
1030 // this->graph->next(e.e); }
1035 Node tail(Edge e) const {
1036 return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
1037 Node head(Edge e) const {
1038 return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
1040 // Node aNode(OutEdgeIt e) const {
1041 // return ((!e.backward) ? this->graph->aNode(e.out) :
1042 // this->graph->aNode(e.in)); }
1043 // Node bNode(OutEdgeIt e) const {
1044 // return ((!e.backward) ? this->graph->bNode(e.out) :
1045 // this->graph->bNode(e.in)); }
1047 // Node aNode(InEdgeIt e) const {
1048 // return ((!e.backward) ? this->graph->aNode(e.in) :
1049 // this->graph->aNode(e.out)); }
1050 // Node bNode(InEdgeIt e) const {
1051 // return ((!e.backward) ? this->graph->bNode(e.in) :
1052 // this->graph->bNode(e.out)); }
1054 /// Gives back the opposite edge.
1055 Edge opposite(const Edge& e) const {
1057 f.backward=!f.backward;
1061 /// \warning This is a linear time operation and works only if
1062 /// \c Graph::EdgeIt is defined.
1063 int edgeNum() const {
1065 for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
1069 bool forward(const Edge& e) const { return !e.backward; }
1070 bool backward(const Edge& e) const { return e.backward; }
1073 template <typename T>
1074 /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two
1075 /// Graph::EdgeMap one for the forward edges and
1076 /// one for the backward edges.
1078 typename Graph::template EdgeMap<T> forward_map, backward_map;
1080 typedef T ValueType;
1081 typedef Edge KeyType;
1082 EdgeMap(const SubBidirGraphWrapper<Graph,
1083 ForwardFilterMap, BackwardFilterMap>& g) :
1084 forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1085 EdgeMap(const SubBidirGraphWrapper<Graph,
1086 ForwardFilterMap, BackwardFilterMap>& g, T a) :
1087 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1088 void set(Edge e, T a) {
1090 forward_map.set(e, a);
1092 backward_map.set(e, a);
1094 T operator[](Edge e) const {
1096 return forward_map[e];
1098 return backward_map[e];
1101 forward_map.update();
1102 backward_map.update();
1104 // T get(Edge e) const {
1106 // return forward_map.get(e.out);
1108 // return backward_map.get(e.in);
1114 ///\brief A wrapper for composing bidirected graph from a directed one.
1116 /// A wrapper for composing bidirected graph from a directed one.
1117 /// A bidirected graph is composed over the directed one without physical
1118 /// storage. As the oppositely directed edges are logically different ones
1119 /// the maps are able to attach different values for them.
1120 template<typename Graph>
1121 class BidirGraphWrapper :
1122 public SubBidirGraphWrapper<
1124 ConstMap<typename Graph::Edge, bool>,
1125 ConstMap<typename Graph::Edge, bool> > {
1127 typedef SubBidirGraphWrapper<
1129 ConstMap<typename Graph::Edge, bool>,
1130 ConstMap<typename Graph::Edge, bool> > Parent;
1132 ConstMap<typename Graph::Edge, bool> cm;
1134 BidirGraphWrapper() : Parent(), cm(true) {
1135 Parent::setForwardFilterMap(cm);
1136 Parent::setBackwardFilterMap(cm);
1139 BidirGraphWrapper(Graph& _graph) : Parent() {
1140 Parent::setGraph(_graph);
1141 Parent::setForwardFilterMap(cm);
1142 Parent::setBackwardFilterMap(cm);
1145 int edgeNum() const {
1146 return 2*this->graph->edgeNum();
1151 /// \brief A bidirected graph template.
1153 /// A bidirected graph template.
1154 /// Such a bidirected graph stores each pair of oppositely directed edges
1155 /// ones in the memory, i.e. a directed graph of type
1156 /// \c Graph is used for that.
1157 /// As the oppositely directed edges are logically different ones
1158 /// the maps are able to attach different values for them.
1160 template<typename Graph>
1161 class BidirGraph : public BidirGraphWrapper<Graph> {
1163 typedef UndirGraphWrapper<Graph> Parent;
1167 BidirGraph() : BidirGraphWrapper<Graph>() {
1168 Parent::setGraph(gr);
1174 template<typename Graph, typename Number,
1175 typename CapacityMap, typename FlowMap>
1176 class ResForwardFilter {
1177 // const Graph* graph;
1178 const CapacityMap* capacity;
1179 const FlowMap* flow;
1181 ResForwardFilter(/*const Graph& _graph, */
1182 const CapacityMap& _capacity, const FlowMap& _flow) :
1183 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1184 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1185 //void setGraph(const Graph& _graph) { graph=&_graph; }
1186 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1187 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1188 bool operator[](const typename Graph::Edge& e) const {
1189 return (Number((*flow)[e]) < Number((*capacity)[e]));
1193 template<typename Graph, typename Number,
1194 typename CapacityMap, typename FlowMap>
1195 class ResBackwardFilter {
1196 //const Graph* graph;
1197 const CapacityMap* capacity;
1198 const FlowMap* flow;
1200 ResBackwardFilter(/*const Graph& _graph,*/
1201 const CapacityMap& _capacity, const FlowMap& _flow) :
1202 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1203 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1204 //void setGraph(const Graph& _graph) { graph=&_graph; }
1205 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1206 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1207 bool operator[](const typename Graph::Edge& e) const {
1208 return (Number(0) < Number((*flow)[e]));
1213 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1215 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1216 template<typename Graph, typename Number,
1217 typename CapacityMap, typename FlowMap>
1218 class ResGraphWrapper :
1219 public SubBidirGraphWrapper<
1221 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1222 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
1224 typedef SubBidirGraphWrapper<
1226 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1227 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
1229 const CapacityMap* capacity;
1231 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
1232 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
1233 ResGraphWrapper() : Parent(),
1234 capacity(0), flow(0) { }
1235 void setCapacityMap(const CapacityMap& _capacity) {
1236 capacity=&_capacity;
1237 forward_filter.setCapacity(_capacity);
1238 backward_filter.setCapacity(_capacity);
1240 void setFlowMap(FlowMap& _flow) {
1242 forward_filter.setFlow(_flow);
1243 backward_filter.setFlow(_flow);
1245 // /// \bug does graph reference needed in filtermaps??
1246 // void setGraph(const Graph& _graph) {
1247 // Parent::setGraph(_graph);
1248 // forward_filter.setGraph(_graph);
1249 // backward_filter.setGraph(_graph);
1252 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1254 Parent(), capacity(&_capacity), flow(&_flow),
1255 forward_filter(/*_graph,*/ _capacity, _flow),
1256 backward_filter(/*_graph,*/ _capacity, _flow) {
1257 Parent::setGraph(_graph);
1258 Parent::setForwardFilterMap(forward_filter);
1259 Parent::setBackwardFilterMap(backward_filter);
1262 typedef typename Parent::Edge Edge;
1264 // bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
1265 //bool backward(const Edge& e) const { return e.backward; }
1267 void augment(const Edge& e, Number a) const {
1268 if (Parent::forward(e))
1269 // flow->set(e.out, flow->get(e.out)+a);
1270 flow->set(e, (*flow)[e]+a);
1272 //flow->set(e.in, flow->get(e.in)-a);
1273 flow->set(e, (*flow)[e]-a);
1278 Number resCap(const Edge& e) const {
1279 if (Parent::forward(e))
1280 // return (capacity->get(e.out)-flow->get(e.out));
1281 return ((*capacity)[e]-(*flow)[e]);
1283 // return (flow->get(e.in));
1284 return ((*flow)[e]);
1287 /// \brief Residual capacity map.
1289 /// In generic residual graphs the residual capacity can be obtained as a map. Not tested.
1292 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
1294 typedef Number ValueType;
1295 typedef Edge KeyType;
1296 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) :
1297 res_graph(&_res_graph) { }
1298 Number operator[](const Edge& e) const {
1299 if (res_graph->forward(e))
1300 // return (capacity->get(e.out)-flow->get(e.out));
1301 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1303 // return (flow->get(e.in));
1304 return (*(res_graph->flow))[e];
1306 /// \bug not needed with dynamic maps, or does it?
1313 /// For blocking flows.
1315 /// This graph wrapper is used for on-the-fly
1316 /// Dinits blocking flow computations.
1317 /// For each node, an out-edge is stored which is used when the
1319 /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1323 /// \author Marton Makai
1324 template<typename Graph, typename FirstOutEdgesMap>
1325 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1327 typedef GraphWrapper<Graph> Parent;
1329 FirstOutEdgesMap* first_out_edges;
1331 ErasingFirstGraphWrapper(Graph& _graph,
1332 FirstOutEdgesMap& _first_out_edges) :
1333 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1335 typedef typename GraphWrapper<Graph>::Node Node;
1336 typedef typename GraphWrapper<Graph>::Edge Edge;
1337 class OutEdgeIt : public Edge {
1338 friend class GraphWrapper<Graph>;
1339 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1340 const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
1343 //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
1344 OutEdgeIt(Invalid i) : Edge(i) { }
1345 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1347 Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
1348 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1350 Edge(e), gw(&_gw) { }
1351 OutEdgeIt& operator++() {
1352 *(static_cast<Edge*>(this))=
1353 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1358 // friend class GraphWrapper<Graph>;
1359 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1360 // // typedef typename Graph::InEdgeIt GraphInEdgeIt;
1361 // typename Graph::InEdgeIt e;
1364 // InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
1365 // InEdgeIt(const Invalid& i) : e(i) { }
1366 // InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1367 // const Node& _n) :
1368 // e(*(_G.graph), typename Graph::Node(_n)) { }
1369 // operator Edge() const { return Edge(typename Graph::Edge(e)); }
1371 //typedef typename Graph::SymEdgeIt SymEdgeIt;
1373 // friend class GraphWrapper<Graph>;
1374 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1375 // // typedef typename Graph::EdgeIt GraphEdgeIt;
1376 // typename Graph::EdgeIt e;
1379 // EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
1380 // EdgeIt(const Invalid& i) : e(i) { }
1381 // EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1382 // e(*(_G.graph)) { }
1383 // operator Edge() const { return Edge(typename Graph::Edge(e)); }
1386 using GraphWrapper<Graph>::first;
1387 // NodeIt& first(NodeIt& i) const {
1388 // i=NodeIt(*this); return i;
1390 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1391 i=OutEdgeIt(*this, p); return i;
1393 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1394 // i=InEdgeIt(*this, p); return i;
1396 // EdgeIt& first(EdgeIt& i) const {
1397 // i=EdgeIt(*this); return i;
1400 // using GraphWrapper<Graph>::next;
1401 // // NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
1402 // OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
1403 // InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
1404 // EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }
1406 // Node aNode(const OutEdgeIt& e) const {
1407 // return Node(this->graph->aNode(e.e)); }
1408 // Node aNode(const InEdgeIt& e) const {
1409 // return Node(this->graph->aNode(e.e)); }
1410 // Node bNode(const OutEdgeIt& e) const {
1411 // return Node(this->graph->bNode(e.e)); }
1412 // Node bNode(const InEdgeIt& e) const {
1413 // return Node(this->graph->bNode(e.e)); }
1415 void erase(const Edge& e) const {
1417 typename Graph::OutEdgeIt f(*Parent::graph, n);
1419 first_out_edges->set(n, f);
1427 #endif //HUGO_GRAPH_WRAPPER_H