Unified style hyperlinks in the doc.
2 * src/lemon/graph_wrapper.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_GRAPH_WRAPPER_H
18 #define LEMON_GRAPH_WRAPPER_H
22 ///\brief Several graph wrappers.
24 ///This file contains several useful graph wrapper functions.
26 ///\author Marton Makai
28 #include <lemon/invalid.h>
29 #include <lemon/maps.h>
30 #include <lemon/iterable_graph_extender.h>
31 #include <lemon/map_defines.h>
38 /// \addtogroup gwrappers
39 /// The main parts of LEMON are the different graph structures,
40 /// generic graph algorithms, graph concepts which couple these, and
41 /// graph wrappers. While the previous ones are more or less clear, the
42 /// latter notion needs further explanation.
43 /// Graph wrappers are graph classes which serve for considering graph
44 /// structures in different ways. A short example makes the notion much
46 /// Suppose that we have an instance \c g of a directed graph
47 /// type say \c ListGraph and an algorithm
48 /// \code template<typename Graph> int algorithm(const Graph&); \endcode
49 /// is needed to run on the reversely oriented graph.
50 /// It may be expensive (in time or in memory usage) to copy
51 /// \c g with the reverse orientation.
52 /// Thus, a wrapper class
53 /// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
54 /// The code looks as follows
57 /// RevGraphWrapper<ListGraph> rgw(g);
58 /// int result=algorithm(rgw);
60 /// After running the algorithm, the original graph \c g
61 /// remains untouched. Thus the graph wrapper used above is to consider the
62 /// original graph with reverse orientation.
63 /// This techniques gives rise to an elegant code, and
64 /// based on stable graph wrappers, complex algorithms can be
65 /// implemented easily.
66 /// In flow, circulation and bipartite matching problems, the residual
67 /// graph is of particular importance. Combining a wrapper implementing
68 /// this, shortest path algorithms and minimum mean cycle algorithms,
69 /// a range of weighted and cardinality optimization algorithms can be
70 /// obtained. For lack of space, for other examples,
71 /// the interested user is referred to the detailed documentation of graph
73 /// The behavior of graph wrappers can be very different. Some of them keep
74 /// capabilities of the original graph while in other cases this would be
75 /// meaningless. This means that the concepts that they are a model of depend
76 /// on the graph wrapper, and the wrapped graph(s).
77 /// If an edge of \c rgw is deleted, this is carried out by
78 /// deleting the corresponding edge of \c g. But for a residual
79 /// graph, this operation has no sense.
80 /// Let we stand one more example here to simplify your work.
82 /// \code template<typename Graph> class RevGraphWrapper; \endcode
84 /// <tt> RevGraphWrapper(Graph& _g)</tt>.
85 /// This means that in a situation,
86 /// when a <tt> const ListGraph& </tt> reference to a graph is given,
87 /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
89 /// int algorithm1(const ListGraph& g) {
90 /// RevGraphWrapper<const ListGraph> rgw(g);
91 /// return algorithm2(rgw);
95 /// \addtogroup gwrappers
98 ///Base type for the Graph Wrappers
100 ///\warning Graph wrappers are in even more experimental state than the other
101 ///parts of the lib. Use them at you own risk.
103 /// This is the base type for most of LEMON graph wrappers.
104 /// This class implements a trivial graph wrapper i.e. it only wraps the
105 /// functions and types of the graph. The purpose of this class is to
106 /// make easier implementing graph wrappers. E.g. if a wrapper is
107 /// considered which differs from the wrapped graph only in some of its
108 /// functions or types, then it can be derived from GraphWrapper, and only the
109 /// differences should be implemented.
111 ///\author Marton Makai
112 template<typename _Graph>
113 class GraphWrapperBase {
115 typedef _Graph Graph;
116 /// \todo Is it needed?
117 typedef Graph BaseGraph;
118 typedef Graph ParentGraph;
122 GraphWrapperBase() : graph(0) { }
123 void setGraph(Graph& _graph) { graph=&_graph; }
126 GraphWrapperBase(Graph& _graph) : graph(&_graph) { }
127 // GraphWrapperBase(const GraphWrapperBase<_Graph>& gw) : graph(gw.graph) { }
129 typedef typename Graph::Node Node;
130 typedef typename Graph::Edge Edge;
132 void first(Node& i) const { graph->first(i); }
133 void first(Edge& i) const { graph->first(i); }
134 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
135 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
136 // NodeIt& first(NodeIt& i) const {
137 // i=NodeIt(*this); return i;
139 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
140 // i=OutEdgeIt(*this, p); return i;
142 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
143 // i=InEdgeIt(*this, p); return i;
145 // EdgeIt& first(EdgeIt& i) const {
146 // i=EdgeIt(*this); return i;
149 void next(Node& i) const { graph->next(i); }
150 void next(Edge& i) const { graph->next(i); }
151 void nextIn(Edge& i) const { graph->nextIn(i); }
152 void nextOut(Edge& i) const { graph->nextOut(i); }
154 Node source(const Edge& e) const { return graph->source(e); }
155 Node target(const Edge& e) const { return graph->target(e); }
156 // Node source(const Edge& e) const {
157 // return Node(graph->source(static_cast<typename Graph::Edge>(e))); }
158 // Node target(const Edge& e) const {
159 // return Node(graph->target(static_cast<typename Graph::Edge>(e))); }
161 int nodeNum() const { return graph->nodeNum(); }
162 int edgeNum() const { return graph->edgeNum(); }
164 Node addNode() const { return Node(graph->addNode()); }
165 Edge addEdge(const Node& source, const Node& target) const {
166 return Edge(graph->addEdge(source, target)); }
168 void erase(const Node& i) const { graph->erase(i); }
169 void erase(const Edge& i) const { graph->erase(i); }
171 void clear() const { graph->clear(); }
173 bool forward(const Edge& e) const { return graph->forward(e); }
174 bool backward(const Edge& e) const { return graph->backward(e); }
176 int id(const Node& v) const { return graph->id(v); }
177 int id(const Edge& e) const { return graph->id(e); }
179 Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
181 template <typename _Value>
182 class NodeMap : public _Graph::template NodeMap<_Value> {
184 typedef typename _Graph::template NodeMap<_Value> Parent;
185 NodeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
186 NodeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
187 : Parent(*gw.graph, value) { }
190 template <typename _Value>
191 class EdgeMap : public _Graph::template EdgeMap<_Value> {
193 typedef typename _Graph::template EdgeMap<_Value> Parent;
194 EdgeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
195 EdgeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
196 : Parent(*gw.graph, value) { }
201 template <typename _Graph>
203 public IterableGraphExtender<GraphWrapperBase<_Graph> > {
205 typedef _Graph Graph;
206 typedef IterableGraphExtender<GraphWrapperBase<_Graph> > Parent;
208 GraphWrapper() : Parent() { }
211 GraphWrapper(Graph& _graph) { setGraph(_graph); }
214 /// A graph wrapper which reverses the orientation of the edges.
216 ///\warning Graph wrappers are in even more experimental state than the other
217 ///parts of the lib. Use them at you own risk.
219 /// Let \f$G=(V, A)\f$ be a directed graph and
220 /// suppose that a graph instange \c g of type
221 /// \c ListGraph implements \f$G\f$.
225 /// For each directed edge
226 /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
227 /// reversing its orientation.
228 /// Then RevGraphWrapper implements the graph structure with node-set
229 /// \f$V\f$ and edge-set
230 /// \f$\{\bar e : e\in A \}\f$, i.e. the graph obtained from \f$G\f$ be
231 /// reversing the orientation of its edges. The following code shows how
232 /// such an instance can be constructed.
234 /// RevGraphWrapper<ListGraph> gw(g);
236 ///\author Marton Makai
237 template<typename Graph>
238 class RevGraphWrapper : public GraphWrapper<Graph> {
240 typedef GraphWrapper<Graph> Parent;
242 RevGraphWrapper() : GraphWrapper<Graph>() { }
244 RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
245 RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
247 typedef typename GraphWrapper<Graph>::Node Node;
248 typedef typename GraphWrapper<Graph>::Edge Edge;
249 //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
250 //because this does not work is some of them are not defined in the
251 //original graph. The problem with this is that typedef-ed stuff
252 //are instantiated in c++.
253 class OutEdgeIt : public Edge {
254 const RevGraphWrapper<Graph>* gw;
255 friend class GraphWrapper<Graph>;
258 OutEdgeIt(Invalid i) : Edge(i) { }
259 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
260 Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
261 OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
262 Edge(e), gw(&_gw) { }
263 OutEdgeIt& operator++() {
264 *(static_cast<Edge*>(this))=
265 ++(typename Graph::InEdgeIt(*(gw->graph), *this));
269 class InEdgeIt : public Edge {
270 const RevGraphWrapper<Graph>* gw;
271 friend class GraphWrapper<Graph>;
274 InEdgeIt(Invalid i) : Edge(i) { }
275 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
276 Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
277 InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
278 Edge(e), gw(&_gw) { }
279 InEdgeIt& operator++() {
280 *(static_cast<Edge*>(this))=
281 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
286 using GraphWrapper<Graph>::first;
287 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
288 i=OutEdgeIt(*this, p); return i;
290 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
291 i=InEdgeIt(*this, p); return i;
294 Node source(const Edge& e) const {
295 return GraphWrapper<Graph>::target(e); }
296 Node target(const Edge& e) const {
297 return GraphWrapper<Graph>::source(e); }
299 // KEEP_MAPS(Parent, RevGraphWrapper);
304 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
305 class SubGraphWrapperBase : public GraphWrapperBase<_Graph> {
307 typedef _Graph Graph;
308 typedef GraphWrapperBase<_Graph> Parent;
310 NodeFilterMap* node_filter_map;
311 EdgeFilterMap* edge_filter_map;
312 SubGraphWrapperBase() : Parent(),
313 node_filter_map(0), edge_filter_map(0) { }
315 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
316 node_filter_map=&_node_filter_map;
318 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
319 edge_filter_map=&_edge_filter_map;
323 // SubGraphWrapperBase(Graph& _graph,
324 // NodeFilterMap& _node_filter_map,
325 // EdgeFilterMap& _edge_filter_map) :
327 // node_filter_map(&node_filter_map),
328 // edge_filter_map(&edge_filter_map) { }
330 typedef typename Parent::Node Node;
331 typedef typename Parent::Edge Edge;
333 void first(Node& i) const {
335 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
337 void first(Edge& i) const {
339 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
341 void firstIn(Edge& i, const Node& n) const {
342 Parent::firstIn(i, n);
343 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
345 void firstOut(Edge& i, const Node& n) const {
346 Parent::firstOut(i, n);
347 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
350 void next(Node& i) const {
352 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
354 void next(Edge& i) const {
356 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
358 void nextIn(Edge& i) const {
360 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
362 void nextOut(Edge& i) const {
364 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
367 /// This function hides \c n in the graph, i.e. the iteration
368 /// jumps over it. This is done by simply setting the value of \c n
369 /// to be false in the corresponding node-map.
370 void hide(const Node& n) const { node_filter_map->set(n, false); }
372 /// This function hides \c e in the graph, i.e. the iteration
373 /// jumps over it. This is done by simply setting the value of \c e
374 /// to be false in the corresponding edge-map.
375 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
377 /// The value of \c n is set to be true in the node-map which stores
378 /// hide information. If \c n was hidden previuosly, then it is shown
380 void unHide(const Node& n) const { node_filter_map->set(n, true); }
382 /// The value of \c e is set to be true in the edge-map which stores
383 /// hide information. If \c e was hidden previuosly, then it is shown
385 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
387 /// Returns true if \c n is hidden.
388 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
390 /// Returns true if \c n is hidden.
391 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
393 /// \warning This is a linear time operation and works only if s
394 /// \c Graph::NodeIt is defined.
395 /// \todo assign tags.
396 int nodeNum() const {
399 for (first(n); n!=INVALID; next(n)) ++i;
403 /// \warning This is a linear time operation and works only if
404 /// \c Graph::EdgeIt is defined.
405 /// \todo assign tags.
406 int edgeNum() const {
409 for (first(e); e!=INVALID; next(e)) ++i;
416 /*! \brief A graph wrapper for hiding nodes and edges from a graph.
418 \warning Graph wrappers are in even more experimental state than the other
419 parts of the lib. Use them at you own risk.
421 This wrapper shows a graph with filtered node-set and
423 Given a bool-valued map on the node-set and one on
424 the edge-set of the graph, the iterators show only the objects
425 having true value. We have to note that this does not mean that an
426 induced subgraph is obtained, the node-iterator cares only the filter
427 on the node-set, and the edge-iterators care only the filter on the
430 typedef SmartGraph Graph;
432 typedef Graph::Node Node;
433 typedef Graph::Edge Edge;
434 Node u=g.addNode(); //node of id 0
435 Node v=g.addNode(); //node of id 1
436 Node e=g.addEdge(u, v); //edge of id 0
437 Node f=g.addEdge(v, u); //edge of id 1
438 Graph::NodeMap<bool> nm(g, true);
440 Graph::EdgeMap<bool> em(g, true);
442 typedef SubGraphWrapper<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
444 for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
445 std::cout << ":-)" << std::endl;
446 for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
448 The output of the above code is the following.
454 Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
455 \c Graph::Node that is why \c g.id(n) can be applied.
457 For other examples see also the documentation of NodeSubGraphWrapper and
462 template<typename _Graph, typename NodeFilterMap,
463 typename EdgeFilterMap>
464 class SubGraphWrapper :
465 public IterableGraphExtender<
466 SubGraphWrapperBase<_Graph, NodeFilterMap, EdgeFilterMap> > {
468 typedef _Graph Graph;
469 typedef IterableGraphExtender<
470 SubGraphWrapperBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
472 SubGraphWrapper() { }
474 SubGraphWrapper(_Graph& _graph, NodeFilterMap& _node_filter_map,
475 EdgeFilterMap& _edge_filter_map) {
477 setNodeFilterMap(_node_filter_map);
478 setEdgeFilterMap(_edge_filter_map);
482 // template<typename Graph, typename NodeFilterMap,
483 // typename EdgeFilterMap>
484 // class SubGraphWrapper : public GraphWrapper<Graph> {
486 // typedef GraphWrapper<Graph> Parent;
488 // NodeFilterMap* node_filter_map;
489 // EdgeFilterMap* edge_filter_map;
491 // SubGraphWrapper() : GraphWrapper<Graph>(),
492 // node_filter_map(0), edge_filter_map(0) { }
493 // void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
494 // node_filter_map=&_node_filter_map;
496 // void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
497 // edge_filter_map=&_edge_filter_map;
501 // SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
502 // EdgeFilterMap& _edge_filter_map) :
503 // GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
504 // edge_filter_map(&_edge_filter_map) { }
506 // typedef typename GraphWrapper<Graph>::Node Node;
507 // class NodeIt : public Node {
508 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
509 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
512 // NodeIt(Invalid i) : Node(i) { }
513 // NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
514 // Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) {
515 // while (*static_cast<Node*>(this)!=INVALID &&
516 // !(*(gw->node_filter_map))[*this])
517 // *(static_cast<Node*>(this))=
518 // ++(typename Graph::NodeIt(*(gw->graph), *this));
520 // NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
522 // Node(n), gw(&_gw) { }
523 // NodeIt& operator++() {
524 // *(static_cast<Node*>(this))=
525 // ++(typename Graph::NodeIt(*(gw->graph), *this));
526 // while (*static_cast<Node*>(this)!=INVALID &&
527 // !(*(gw->node_filter_map))[*this])
528 // *(static_cast<Node*>(this))=
529 // ++(typename Graph::NodeIt(*(gw->graph), *this));
533 // typedef typename GraphWrapper<Graph>::Edge Edge;
534 // class OutEdgeIt : public Edge {
535 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
536 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
539 // OutEdgeIt(Invalid i) : Edge(i) { }
540 // OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
541 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) {
542 // while (*static_cast<Edge*>(this)!=INVALID &&
543 // !(*(gw->edge_filter_map))[*this])
544 // *(static_cast<Edge*>(this))=
545 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
547 // OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
549 // Edge(e), gw(&_gw) { }
550 // OutEdgeIt& operator++() {
551 // *(static_cast<Edge*>(this))=
552 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
553 // while (*static_cast<Edge*>(this)!=INVALID &&
554 // !(*(gw->edge_filter_map))[*this])
555 // *(static_cast<Edge*>(this))=
556 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
560 // class InEdgeIt : public Edge {
561 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
562 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
565 // // InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
566 // InEdgeIt(Invalid i) : Edge(i) { }
567 // InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
568 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) {
569 // while (*static_cast<Edge*>(this)!=INVALID &&
570 // !(*(gw->edge_filter_map))[*this])
571 // *(static_cast<Edge*>(this))=
572 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
574 // InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
576 // Edge(e), gw(&_gw) { }
577 // InEdgeIt& operator++() {
578 // *(static_cast<Edge*>(this))=
579 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
580 // while (*static_cast<Edge*>(this)!=INVALID &&
581 // !(*(gw->edge_filter_map))[*this])
582 // *(static_cast<Edge*>(this))=
583 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
587 // class EdgeIt : public Edge {
588 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
589 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
592 // EdgeIt(Invalid i) : Edge(i) { }
593 // EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
594 // Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) {
595 // while (*static_cast<Edge*>(this)!=INVALID &&
596 // !(*(gw->edge_filter_map))[*this])
597 // *(static_cast<Edge*>(this))=
598 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
600 // EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
602 // Edge(e), gw(&_gw) { }
603 // EdgeIt& operator++() {
604 // *(static_cast<Edge*>(this))=
605 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
606 // while (*static_cast<Edge*>(this)!=INVALID &&
607 // !(*(gw->edge_filter_map))[*this])
608 // *(static_cast<Edge*>(this))=
609 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
614 // NodeIt& first(NodeIt& i) const {
615 // i=NodeIt(*this); return i;
617 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
618 // i=OutEdgeIt(*this, p); return i;
620 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
621 // i=InEdgeIt(*this, p); return i;
623 // EdgeIt& first(EdgeIt& i) const {
624 // i=EdgeIt(*this); return i;
627 // /// This function hides \c n in the graph, i.e. the iteration
628 // /// jumps over it. This is done by simply setting the value of \c n
629 // /// to be false in the corresponding node-map.
630 // void hide(const Node& n) const { node_filter_map->set(n, false); }
632 // /// This function hides \c e in the graph, i.e. the iteration
633 // /// jumps over it. This is done by simply setting the value of \c e
634 // /// to be false in the corresponding edge-map.
635 // void hide(const Edge& e) const { edge_filter_map->set(e, false); }
637 // /// The value of \c n is set to be true in the node-map which stores
638 // /// hide information. If \c n was hidden previuosly, then it is shown
640 // void unHide(const Node& n) const { node_filter_map->set(n, true); }
642 // /// The value of \c e is set to be true in the edge-map which stores
643 // /// hide information. If \c e was hidden previuosly, then it is shown
645 // void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
647 // /// Returns true if \c n is hidden.
648 // bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
650 // /// Returns true if \c n is hidden.
651 // bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
653 // /// \warning This is a linear time operation and works only if
654 // /// \c Graph::NodeIt is defined.
655 // int nodeNum() const {
657 // for (NodeIt n(*this); n!=INVALID; ++n) ++i;
661 // /// \warning This is a linear time operation and works only if
662 // /// \c Graph::EdgeIt is defined.
663 // int edgeNum() const {
665 // for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
669 // // KEEP_MAPS(Parent, SubGraphWrapper);
673 /*! \brief A wrapper for hiding nodes from a graph.
675 \warning Graph wrappers are in even more experimental state than the other
676 parts of the lib. Use them at you own risk.
678 A wrapper for hiding nodes from a graph.
679 This wrapper specializes SubGraphWrapper in the way that only the node-set
680 can be filtered. Note that this does not mean of considering induced
681 subgraph, the edge-iterators consider the original edge-set.
684 template<typename Graph, typename NodeFilterMap>
685 class NodeSubGraphWrapper :
686 public SubGraphWrapper<Graph, NodeFilterMap,
687 ConstMap<typename Graph::Edge,bool> > {
689 typedef SubGraphWrapper<Graph, NodeFilterMap,
690 ConstMap<typename Graph::Edge,bool> > Parent;
692 ConstMap<typename Graph::Edge, bool> const_true_map;
694 NodeSubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map) :
695 Parent(), const_true_map(true) {
696 Parent::setGraph(_graph);
697 Parent::setNodeFilterMap(_node_filter_map);
698 Parent::setEdgeFilterMap(const_true_map);
703 /*! \brief A wrapper for hiding edges from a graph.
705 \warning Graph wrappers are in even more experimental state than the other
706 parts of the lib. Use them at you own risk.
708 A wrapper for hiding edges from a graph.
709 This wrapper specializes SubGraphWrapper in the way that only the edge-set
710 can be filtered. The usefulness of this wrapper is demonstrated in the
711 problem of searching a maximum number of edge-disjoint shortest paths
713 two nodes \c s and \c t. Shortest here means being shortest w.r.t.
714 non-negative edge-lengths. Note that
715 the comprehension of the presented solution
716 need's some knowledge from elementary combinatorial optimization.
718 If a single shortest path is to be
719 searched between two nodes \c s and \c t, then this can be done easily by
720 applying the Dijkstra algorithm class. What happens, if a maximum number of
721 edge-disjoint shortest paths is to be computed. It can be proved that an
722 edge can be in a shortest path if and only if it is tight with respect to
723 the potential function computed by Dijkstra. Moreover, any path containing
724 only such edges is a shortest one. Thus we have to compute a maximum number
725 of edge-disjoint paths between \c s and \c t in the graph which has edge-set
726 all the tight edges. The computation will be demonstrated on the following
727 graph, which is read from a dimacs file.
730 digraph lemon_dot_example {
731 node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
732 n0 [ label="0 (s)" ];
738 n6 [ label="6 (t)" ];
739 edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
740 n5 -> n6 [ label="9, length:4" ];
741 n4 -> n6 [ label="8, length:2" ];
742 n3 -> n5 [ label="7, length:1" ];
743 n2 -> n5 [ label="6, length:3" ];
744 n2 -> n6 [ label="5, length:5" ];
745 n2 -> n4 [ label="4, length:2" ];
746 n1 -> n4 [ label="3, length:3" ];
747 n0 -> n3 [ label="2, length:1" ];
748 n0 -> n2 [ label="1, length:2" ];
749 n0 -> n1 [ label="0, length:3" ];
758 readDimacs(std::cin, g, length, s, t);
760 cout << "edges with lengths (of form id, source--length->target): " << endl;
761 for(EdgeIt e(g); e!=INVALID; ++e)
762 cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
763 << length[e] << "->" << g.id(g.target(e)) << endl;
765 cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
767 Next, the potential function is computed with Dijkstra.
769 typedef Dijkstra<Graph, LengthMap> Dijkstra;
770 Dijkstra dijkstra(g, length);
773 Next, we consrtruct a map which filters the edge-set to the tight edges.
775 typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
777 TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
779 typedef EdgeSubGraphWrapper<Graph, TightEdgeFilter> SubGW;
780 SubGW gw(g, tight_edge_filter);
782 Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
783 with a max flow algorithm Preflow.
785 ConstMap<Edge, int> const_1_map(1);
786 Graph::EdgeMap<int> flow(g, 0);
788 Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
789 preflow(gw, s, t, const_1_map, flow);
794 cout << "maximum number of edge-disjoint shortest path: "
795 << preflow.flowValue() << endl;
796 cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
798 for(EdgeIt e(g); e!=INVALID; ++e)
800 cout << " " << g.id(g.source(e)) << "--"
801 << length[e] << "->" << g.id(g.target(e)) << endl;
803 The program has the following (expected :-)) output:
805 edges with lengths (of form id, source--length->target):
817 maximum number of edge-disjoint shortest path: 2
818 edges of the maximum number of edge-disjoint shortest s-t paths:
829 template<typename Graph, typename EdgeFilterMap>
830 class EdgeSubGraphWrapper :
831 public SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>,
834 typedef SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>,
835 EdgeFilterMap> Parent;
837 ConstMap<typename Graph::Node, bool> const_true_map;
839 EdgeSubGraphWrapper(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
840 Parent(), const_true_map(true) {
841 Parent::setGraph(_graph);
842 Parent::setNodeFilterMap(const_true_map);
843 Parent::setEdgeFilterMap(_edge_filter_map);
848 template<typename Graph>
849 class UndirGraphWrapper : public GraphWrapper<Graph> {
851 typedef GraphWrapper<Graph> Parent;
853 UndirGraphWrapper() : GraphWrapper<Graph>() { }
856 typedef typename GraphWrapper<Graph>::Node Node;
857 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
858 typedef typename GraphWrapper<Graph>::Edge Edge;
859 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
861 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
864 friend class UndirGraphWrapper<Graph>;
865 bool out_or_in; //true iff out
866 typename Graph::OutEdgeIt out;
867 typename Graph::InEdgeIt in;
870 OutEdgeIt(const Invalid& i) : Edge(i) { }
871 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
872 out_or_in=true; _G.graph->first(out, _n);
873 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
875 operator Edge() const {
876 if (out_or_in) return Edge(out); else return Edge(in);
880 typedef OutEdgeIt InEdgeIt;
882 using GraphWrapper<Graph>::first;
883 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
884 i=OutEdgeIt(*this, p); return i;
887 using GraphWrapper<Graph>::next;
889 OutEdgeIt& next(OutEdgeIt& e) const {
891 typename Graph::Node n=this->graph->source(e.out);
892 this->graph->next(e.out);
893 if (!this->graph->valid(e.out)) {
894 e.out_or_in=false; this->graph->first(e.in, n); }
896 this->graph->next(e.in);
901 Node aNode(const OutEdgeIt& e) const {
902 if (e.out_or_in) return this->graph->source(e); else
903 return this->graph->target(e); }
904 Node bNode(const OutEdgeIt& e) const {
905 if (e.out_or_in) return this->graph->target(e); else
906 return this->graph->source(e); }
908 // KEEP_MAPS(Parent, UndirGraphWrapper);
912 // /// \brief An undirected graph template.
914 // ///\warning Graph wrappers are in even more experimental state than the other
915 // ///parts of the lib. Use them at your own risk.
917 // /// An undirected graph template.
918 // /// This class works as an undirected graph and a directed graph of
919 // /// class \c Graph is used for the physical storage.
920 // /// \ingroup graphs
921 template<typename Graph>
922 class UndirGraph : public UndirGraphWrapper<Graph> {
923 typedef UndirGraphWrapper<Graph> Parent;
927 UndirGraph() : UndirGraphWrapper<Graph>() {
928 Parent::setGraph(gr);
931 // KEEP_MAPS(Parent, UndirGraph);
935 template <typename _Graph,
936 typename ForwardFilterMap, typename BackwardFilterMap>
937 class SubBidirGraphWrapperBase : public GraphWrapperBase<_Graph> {
939 typedef _Graph Graph;
940 typedef GraphWrapperBase<_Graph> Parent;
942 ForwardFilterMap* forward_filter;
943 BackwardFilterMap* backward_filter;
944 SubBidirGraphWrapperBase() : Parent(),
945 forward_filter(0), backward_filter(0) { }
947 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
948 forward_filter=&_forward_filter;
950 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
951 backward_filter=&_backward_filter;
955 // SubGraphWrapperBase(Graph& _graph,
956 // NodeFilterMap& _node_filter_map,
957 // EdgeFilterMap& _edge_filter_map) :
959 // node_filter_map(&node_filter_map),
960 // edge_filter_map(&edge_filter_map) { }
962 typedef typename Parent::Node Node;
963 typedef typename _Graph::Edge GraphEdge;
964 template <typename T> class EdgeMap;
965 /// SubBidirGraphWrapperBase<..., ..., ...>::Edge is inherited from
966 /// _Graph::Edge. It contains an extra bool flag which is true
967 /// if and only if the
968 /// edge is the backward version of the original edge.
969 class Edge : public _Graph::Edge {
970 friend class SubBidirGraphWrapperBase<
971 Graph, ForwardFilterMap, BackwardFilterMap>;
972 template<typename T> friend class EdgeMap;
974 bool backward; //true, iff backward
977 /// \todo =false is needed, or causes problems?
978 /// If \c _backward is false, then we get an edge corresponding to the
979 /// original one, otherwise its oppositely directed pair is obtained.
980 Edge(const typename _Graph::Edge& e, bool _backward/*=false*/) :
981 _Graph::Edge(e), backward(_backward) { }
982 Edge(Invalid i) : _Graph::Edge(i), backward(true) { }
983 bool operator==(const Edge& v) const {
984 return (this->backward==v.backward &&
985 static_cast<typename _Graph::Edge>(*this)==
986 static_cast<typename _Graph::Edge>(v));
988 bool operator!=(const Edge& v) const {
989 return (this->backward!=v.backward ||
990 static_cast<typename _Graph::Edge>(*this)!=
991 static_cast<typename _Graph::Edge>(v));
995 void first(Node& i) const {
999 void first(Edge& i) const {
1002 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1003 !(*forward_filter)[i]) Parent::next(i);
1004 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1007 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1008 !(*backward_filter)[i]) Parent::next(i);
1012 void firstIn(Edge& i, const Node& n) const {
1013 Parent::firstIn(i, n);
1015 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1016 !(*forward_filter)[i]) Parent::nextOut(i);
1017 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1018 Parent::firstOut(i, n);
1020 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1021 !(*backward_filter)[i]) Parent::nextOut(i);
1025 void firstOut(Edge& i, const Node& n) const {
1026 Parent::firstOut(i, n);
1028 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1029 !(*forward_filter)[i]) Parent::nextOut(i);
1030 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1031 Parent::firstIn(i, n);
1033 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1034 !(*backward_filter)[i]) Parent::nextIn(i);
1038 void next(Node& i) const {
1042 void next(Edge& i) const {
1043 if (!(i.backward)) {
1045 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1046 !(*forward_filter)[i]) Parent::next(i);
1047 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1050 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1051 !(*backward_filter)[i]) Parent::next(i);
1055 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1056 !(*backward_filter)[i]) Parent::next(i);
1060 void nextIn(Edge& i) const {
1061 if (!(i.backward)) {
1062 Node n=Parent::target(i);
1064 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1065 !(*forward_filter)[i]) Parent::nextIn(i);
1066 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1067 Parent::firstOut(i, n);
1069 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1070 !(*backward_filter)[i]) Parent::nextOut(i);
1074 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1075 !(*backward_filter)[i]) Parent::nextOut(i);
1079 void nextOut(Edge& i) const {
1080 if (!(i.backward)) {
1081 Node n=Parent::source(i);
1083 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1084 !(*forward_filter)[i]) Parent::nextOut(i);
1085 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1086 Parent::firstIn(i, n);
1088 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1089 !(*backward_filter)[i]) Parent::nextIn(i);
1093 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1094 !(*backward_filter)[i]) Parent::nextIn(i);
1098 Node source(Edge e) const {
1099 return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
1100 Node target(Edge e) const {
1101 return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
1103 /// Gives back the opposite edge.
1104 Edge opposite(const Edge& e) const {
1106 f.backward=!f.backward;
1110 /// \warning This is a linear time operation and works only if
1111 /// \c Graph::EdgeIt is defined.
1113 int edgeNum() const {
1116 for (first(e); e!=INVALID; next(e)) ++i;
1120 bool forward(const Edge& e) const { return !e.backward; }
1121 bool backward(const Edge& e) const { return e.backward; }
1123 template <typename T>
1124 /// \c SubBidirGraphWrapperBase<..., ..., ...>::EdgeMap contains two
1125 /// _Graph::EdgeMap one for the forward edges and
1126 /// one for the backward edges.
1128 template <typename TT> friend class EdgeMap;
1129 typename _Graph::template EdgeMap<T> forward_map, backward_map;
1134 EdgeMap(const SubBidirGraphWrapperBase<_Graph,
1135 ForwardFilterMap, BackwardFilterMap>& g) :
1136 forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1138 EdgeMap(const SubBidirGraphWrapperBase<_Graph,
1139 ForwardFilterMap, BackwardFilterMap>& g, T a) :
1140 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1142 // template <typename TT>
1143 // EdgeMap(const EdgeMap<TT>& copy)
1144 // : forward_map(copy.forward_map), backward_map(copy.backward_map) {}
1146 // template <typename TT>
1147 // EdgeMap& operator=(const EdgeMap<TT>& copy) {
1148 // forward_map = copy.forward_map;
1149 // backward_map = copy.backward_map;
1153 void set(Edge e, T a) {
1155 forward_map.set(e, a);
1157 backward_map.set(e, a);
1160 // typename _Graph::template EdgeMap<T>::ConstReference
1161 // operator[](Edge e) const {
1163 // return forward_map[e];
1165 // return backward_map[e];
1168 // typename _Graph::template EdgeMap<T>::Reference
1169 T operator[](Edge e) {
1171 return forward_map[e];
1173 return backward_map[e];
1177 forward_map.update();
1178 backward_map.update();
1185 ///\brief A wrapper for composing a subgraph of a
1186 /// bidirected graph made from a directed one.
1188 /// A wrapper for composing a subgraph of a
1189 /// bidirected graph made from a directed one.
1191 ///\warning Graph wrappers are in even more experimental state than the other
1192 ///parts of the lib. Use them at you own risk.
1194 /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge
1195 /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
1196 /// reversing its orientation. We are given moreover two bool valued
1197 /// maps on the edge-set,
1198 /// \f$forward\_filter\f$, and \f$backward\_filter\f$.
1199 /// SubBidirGraphWrapper implements the graph structure with node-set
1200 /// \f$V\f$ and edge-set
1201 /// \f$\{e : e\in A \mbox{ and } forward\_filter(e) \mbox{ is true}\}+\{\bar e : e\in A \mbox{ and } backward\_filter(e) \mbox{ is true}\}\f$.
1202 /// The purpose of writing + instead of union is because parallel
1203 /// edges can arise. (Similarly, antiparallel edges also can arise).
1204 /// In other words, a subgraph of the bidirected graph obtained, which
1205 /// is given by orienting the edges of the original graph in both directions.
1206 /// As the oppositely directed edges are logically different,
1207 /// the maps are able to attach different values for them.
1209 /// An example for such a construction is \c RevGraphWrapper where the
1210 /// forward_filter is everywhere false and the backward_filter is
1211 /// everywhere true. We note that for sake of efficiency,
1212 /// \c RevGraphWrapper is implemented in a different way.
1213 /// But BidirGraphWrapper is obtained from
1214 /// SubBidirGraphWrapper by considering everywhere true
1215 /// valued maps both for forward_filter and backward_filter.
1216 /// Finally, one of the most important applications of SubBidirGraphWrapper
1217 /// is ResGraphWrapper, which stands for the residual graph in directed
1218 /// flow and circulation problems.
1219 /// As wrappers usually, the SubBidirGraphWrapper implements the
1220 /// above mentioned graph structure without its physical storage,
1221 /// that is the whole stuff is stored in constant memory.
1222 template<typename _Graph,
1223 typename ForwardFilterMap, typename BackwardFilterMap>
1224 class SubBidirGraphWrapper :
1225 public IterableGraphExtender<
1226 SubBidirGraphWrapperBase<_Graph, ForwardFilterMap, BackwardFilterMap> > {
1228 typedef _Graph Graph;
1229 typedef IterableGraphExtender<
1230 SubBidirGraphWrapperBase<
1231 _Graph, ForwardFilterMap, BackwardFilterMap> > Parent;
1233 SubBidirGraphWrapper() { }
1235 SubBidirGraphWrapper(_Graph& _graph, ForwardFilterMap& _forward_filter,
1236 BackwardFilterMap& _backward_filter) {
1238 setForwardFilterMap(_forward_filter);
1239 setBackwardFilterMap(_backward_filter);
1243 // template<typename Graph,
1244 // typename ForwardFilterMap, typename BackwardFilterMap>
1245 // class SubBidirGraphWrapper : public GraphWrapper<Graph> {
1247 // typedef GraphWrapper<Graph> Parent;
1249 // ForwardFilterMap* forward_filter;
1250 // BackwardFilterMap* backward_filter;
1252 // SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
1253 // void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
1254 // forward_filter=&_forward_filter;
1256 // void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
1257 // backward_filter=&_backward_filter;
1262 // SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
1263 // BackwardFilterMap& _backward_filter) :
1264 // GraphWrapper<Graph>(_graph),
1265 // forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
1266 // SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph,
1267 // ForwardFilterMap, BackwardFilterMap>& gw) :
1269 // forward_filter(gw.forward_filter),
1270 // backward_filter(gw.backward_filter) { }
1274 // friend class Edge;
1275 // friend class OutEdgeIt;
1277 // template<typename T> class EdgeMap;
1279 // typedef typename GraphWrapper<Graph>::Node Node;
1281 // typedef typename Graph::Edge GraphEdge;
1282 // /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from
1283 // /// Graph::Edge. It contains an extra bool flag which is true
1284 // /// if and only if the
1285 // /// edge is the backward version of the original edge.
1286 // class Edge : public Graph::Edge {
1287 // friend class SubBidirGraphWrapper<Graph,
1288 // ForwardFilterMap, BackwardFilterMap>;
1289 // template<typename T> friend class EdgeMap;
1291 // bool backward; //true, iff backward
1294 // /// \todo =false is needed, or causes problems?
1295 // /// If \c _backward is false, then we get an edge corresponding to the
1296 // /// original one, otherwise its oppositely directed pair is obtained.
1297 // Edge(const typename Graph::Edge& e, bool _backward/*=false*/) :
1298 // Graph::Edge(e), backward(_backward) { }
1299 // Edge(Invalid i) : Graph::Edge(i), backward(true) { }
1300 // bool operator==(const Edge& v) const {
1301 // return (this->backward==v.backward &&
1302 // static_cast<typename Graph::Edge>(*this)==
1303 // static_cast<typename Graph::Edge>(v));
1305 // bool operator!=(const Edge& v) const {
1306 // return (this->backward!=v.backward ||
1307 // static_cast<typename Graph::Edge>(*this)!=
1308 // static_cast<typename Graph::Edge>(v));
1312 // class OutEdgeIt : public Edge {
1313 // friend class SubBidirGraphWrapper<Graph,
1314 // ForwardFilterMap, BackwardFilterMap>;
1316 // const SubBidirGraphWrapper<Graph,
1317 // ForwardFilterMap, BackwardFilterMap>* gw;
1320 // OutEdgeIt(Invalid i) : Edge(i) { }
1321 // OutEdgeIt(const SubBidirGraphWrapper<Graph,
1322 // ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
1323 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
1324 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1325 // !(*(gw->forward_filter))[*this])
1326 // *(static_cast<GraphEdge*>(this))=
1327 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1328 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1329 // *static_cast<Edge*>(this)=
1330 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
1331 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1332 // !(*(gw->backward_filter))[*this])
1333 // *(static_cast<GraphEdge*>(this))=
1334 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1337 // OutEdgeIt(const SubBidirGraphWrapper<Graph,
1338 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1339 // Edge(e), gw(&_gw) { }
1340 // OutEdgeIt& operator++() {
1341 // if (!this->backward) {
1342 // Node n=gw->source(*this);
1343 // *(static_cast<GraphEdge*>(this))=
1344 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1345 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1346 // !(*(gw->forward_filter))[*this])
1347 // *(static_cast<GraphEdge*>(this))=
1348 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1349 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1350 // *static_cast<Edge*>(this)=
1351 // Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
1352 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1353 // !(*(gw->backward_filter))[*this])
1354 // *(static_cast<GraphEdge*>(this))=
1355 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1358 // *(static_cast<GraphEdge*>(this))=
1359 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1360 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1361 // !(*(gw->backward_filter))[*this])
1362 // *(static_cast<GraphEdge*>(this))=
1363 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1369 // class InEdgeIt : public Edge {
1370 // friend class SubBidirGraphWrapper<Graph,
1371 // ForwardFilterMap, BackwardFilterMap>;
1373 // const SubBidirGraphWrapper<Graph,
1374 // ForwardFilterMap, BackwardFilterMap>* gw;
1377 // InEdgeIt(Invalid i) : Edge(i) { }
1378 // InEdgeIt(const SubBidirGraphWrapper<Graph,
1379 // ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
1380 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
1381 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1382 // !(*(gw->forward_filter))[*this])
1383 // *(static_cast<GraphEdge*>(this))=
1384 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1385 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1386 // *static_cast<Edge*>(this)=
1387 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
1388 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1389 // !(*(gw->backward_filter))[*this])
1390 // *(static_cast<GraphEdge*>(this))=
1391 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1394 // InEdgeIt(const SubBidirGraphWrapper<Graph,
1395 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1396 // Edge(e), gw(&_gw) { }
1397 // InEdgeIt& operator++() {
1398 // if (!this->backward) {
1399 // Node n=gw->source(*this);
1400 // *(static_cast<GraphEdge*>(this))=
1401 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1402 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1403 // !(*(gw->forward_filter))[*this])
1404 // *(static_cast<GraphEdge*>(this))=
1405 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1406 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1407 // *static_cast<Edge*>(this)=
1408 // Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
1409 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1410 // !(*(gw->backward_filter))[*this])
1411 // *(static_cast<GraphEdge*>(this))=
1412 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1415 // *(static_cast<GraphEdge*>(this))=
1416 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1417 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1418 // !(*(gw->backward_filter))[*this])
1419 // *(static_cast<GraphEdge*>(this))=
1420 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1426 // class EdgeIt : public Edge {
1427 // friend class SubBidirGraphWrapper<Graph,
1428 // ForwardFilterMap, BackwardFilterMap>;
1430 // const SubBidirGraphWrapper<Graph,
1431 // ForwardFilterMap, BackwardFilterMap>* gw;
1434 // EdgeIt(Invalid i) : Edge(i) { }
1435 // EdgeIt(const SubBidirGraphWrapper<Graph,
1436 // ForwardFilterMap, BackwardFilterMap>& _gw) :
1437 // Edge(typename Graph::EdgeIt(*(_gw.graph)), false), gw(&_gw) {
1438 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1439 // !(*(gw->forward_filter))[*this])
1440 // *(static_cast<GraphEdge*>(this))=
1441 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1442 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1443 // *static_cast<Edge*>(this)=
1444 // Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
1445 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1446 // !(*(gw->backward_filter))[*this])
1447 // *(static_cast<GraphEdge*>(this))=
1448 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1451 // EdgeIt(const SubBidirGraphWrapper<Graph,
1452 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1453 // Edge(e), gw(&_gw) { }
1454 // EdgeIt& operator++() {
1455 // if (!this->backward) {
1456 // *(static_cast<GraphEdge*>(this))=
1457 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1458 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1459 // !(*(gw->forward_filter))[*this])
1460 // *(static_cast<GraphEdge*>(this))=
1461 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1462 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1463 // *static_cast<Edge*>(this)=
1464 // Edge(typename Graph::EdgeIt(*(gw->graph)), true);
1465 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1466 // !(*(gw->backward_filter))[*this])
1467 // *(static_cast<GraphEdge*>(this))=
1468 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1471 // *(static_cast<GraphEdge*>(this))=
1472 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1473 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1474 // !(*(gw->backward_filter))[*this])
1475 // *(static_cast<GraphEdge*>(this))=
1476 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1482 // // using GraphWrapper<Graph>::first;
1483 // // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1484 // // i=OutEdgeIt(*this, p); return i;
1486 // // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1487 // // i=InEdgeIt(*this, p); return i;
1489 // // EdgeIt& first(EdgeIt& i) const {
1490 // // i=EdgeIt(*this); return i;
1494 // Node source(Edge e) const {
1495 // return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
1496 // Node target(Edge e) const {
1497 // return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
1499 // /// Gives back the opposite edge.
1500 // Edge opposite(const Edge& e) const {
1502 // f.backward=!f.backward;
1506 // /// \warning This is a linear time operation and works only if
1507 // /// \c Graph::EdgeIt is defined.
1508 // int edgeNum() const {
1510 // for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
1514 // bool forward(const Edge& e) const { return !e.backward; }
1515 // bool backward(const Edge& e) const { return e.backward; }
1518 // template <typename T>
1519 // /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two
1520 // /// Graph::EdgeMap one for the forward edges and
1521 // /// one for the backward edges.
1523 // template <typename TT> friend class EdgeMap;
1524 // typename Graph::template EdgeMap<T> forward_map, backward_map;
1527 // typedef Edge Key;
1529 // EdgeMap(const SubBidirGraphWrapper<Graph,
1530 // ForwardFilterMap, BackwardFilterMap>& g) :
1531 // forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1533 // EdgeMap(const SubBidirGraphWrapper<Graph,
1534 // ForwardFilterMap, BackwardFilterMap>& g, T a) :
1535 // forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1537 // template <typename TT>
1538 // EdgeMap(const EdgeMap<TT>& copy)
1539 // : forward_map(copy.forward_map), backward_map(copy.backward_map) {}
1541 // template <typename TT>
1542 // EdgeMap& operator=(const EdgeMap<TT>& copy) {
1543 // forward_map = copy.forward_map;
1544 // backward_map = copy.backward_map;
1548 // void set(Edge e, T a) {
1550 // forward_map.set(e, a);
1552 // backward_map.set(e, a);
1555 // typename Graph::template EdgeMap<T>::ConstReference
1556 // operator[](Edge e) const {
1558 // return forward_map[e];
1560 // return backward_map[e];
1563 // typename Graph::template EdgeMap<T>::Reference
1564 // operator[](Edge e) {
1566 // return forward_map[e];
1568 // return backward_map[e];
1572 // forward_map.update();
1573 // backward_map.update();
1578 // // KEEP_NODE_MAP(Parent, SubBidirGraphWrapper);
1583 ///\brief A wrapper for composing bidirected graph from a directed one.
1585 ///\warning Graph wrappers are in even more experimental state than the other
1586 ///parts of the lib. Use them at you own risk.
1588 /// A wrapper for composing bidirected graph from a directed one.
1589 /// A bidirected graph is composed over the directed one without physical
1590 /// storage. As the oppositely directed edges are logically different ones
1591 /// the maps are able to attach different values for them.
1592 template<typename Graph>
1593 class BidirGraphWrapper :
1594 public SubBidirGraphWrapper<
1596 ConstMap<typename Graph::Edge, bool>,
1597 ConstMap<typename Graph::Edge, bool> > {
1599 typedef SubBidirGraphWrapper<
1601 ConstMap<typename Graph::Edge, bool>,
1602 ConstMap<typename Graph::Edge, bool> > Parent;
1604 ConstMap<typename Graph::Edge, bool> cm;
1606 BidirGraphWrapper() : Parent(), cm(true) {
1607 Parent::setForwardFilterMap(cm);
1608 Parent::setBackwardFilterMap(cm);
1611 BidirGraphWrapper(Graph& _graph) : Parent() {
1612 Parent::setGraph(_graph);
1613 Parent::setForwardFilterMap(cm);
1614 Parent::setBackwardFilterMap(cm);
1617 int edgeNum() const {
1618 return 2*this->graph->edgeNum();
1620 // KEEP_MAPS(Parent, BidirGraphWrapper);
1624 /// \brief A bidirected graph template.
1626 ///\warning Graph wrappers are in even more experimental state than the other
1627 ///parts of the lib. Use them at you own risk.
1629 /// A bidirected graph template.
1630 /// Such a bidirected graph stores each pair of oppositely directed edges
1631 /// ones in the memory, i.e. a directed graph of type
1632 /// \c Graph is used for that.
1633 /// As the oppositely directed edges are logically different ones
1634 /// the maps are able to attach different values for them.
1636 template<typename Graph>
1637 class BidirGraph : public BidirGraphWrapper<Graph> {
1639 typedef UndirGraphWrapper<Graph> Parent;
1643 BidirGraph() : BidirGraphWrapper<Graph>() {
1644 Parent::setGraph(gr);
1646 // KEEP_MAPS(Parent, BidirGraph);
1651 template<typename Graph, typename Number,
1652 typename CapacityMap, typename FlowMap>
1653 class ResForwardFilter {
1654 // const Graph* graph;
1655 const CapacityMap* capacity;
1656 const FlowMap* flow;
1658 ResForwardFilter(/*const Graph& _graph, */
1659 const CapacityMap& _capacity, const FlowMap& _flow) :
1660 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1661 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1662 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1663 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1664 bool operator[](const typename Graph::Edge& e) const {
1665 return (Number((*flow)[e]) < Number((*capacity)[e]));
1669 template<typename Graph, typename Number,
1670 typename CapacityMap, typename FlowMap>
1671 class ResBackwardFilter {
1672 const CapacityMap* capacity;
1673 const FlowMap* flow;
1675 ResBackwardFilter(/*const Graph& _graph,*/
1676 const CapacityMap& _capacity, const FlowMap& _flow) :
1677 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1678 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1679 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1680 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1681 bool operator[](const typename Graph::Edge& e) const {
1682 return (Number(0) < Number((*flow)[e]));
1687 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1689 ///\warning Graph wrappers are in even more experimental state than the other
1690 ///parts of the lib. Use them at you own risk.
1692 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1693 template<typename Graph, typename Number,
1694 typename CapacityMap, typename FlowMap>
1695 class ResGraphWrapper :
1696 public SubBidirGraphWrapper<
1698 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1699 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
1701 typedef SubBidirGraphWrapper<
1703 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1704 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
1706 const CapacityMap* capacity;
1708 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
1709 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
1710 ResGraphWrapper() : Parent(),
1711 capacity(0), flow(0) { }
1712 void setCapacityMap(const CapacityMap& _capacity) {
1713 capacity=&_capacity;
1714 forward_filter.setCapacity(_capacity);
1715 backward_filter.setCapacity(_capacity);
1717 void setFlowMap(FlowMap& _flow) {
1719 forward_filter.setFlow(_flow);
1720 backward_filter.setFlow(_flow);
1723 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1725 Parent(), capacity(&_capacity), flow(&_flow),
1726 forward_filter(/*_graph,*/ _capacity, _flow),
1727 backward_filter(/*_graph,*/ _capacity, _flow) {
1728 Parent::setGraph(_graph);
1729 Parent::setForwardFilterMap(forward_filter);
1730 Parent::setBackwardFilterMap(backward_filter);
1733 typedef typename Parent::Edge Edge;
1735 void augment(const Edge& e, Number a) const {
1736 if (Parent::forward(e))
1737 flow->set(e, (*flow)[e]+a);
1739 flow->set(e, (*flow)[e]-a);
1742 /// \brief Residual capacity map.
1744 /// In generic residual graphs the residual capacity can be obtained
1748 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
1750 typedef Number Value;
1752 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>&
1753 _res_graph) : res_graph(&_res_graph) { }
1754 Number operator[](const Edge& e) const {
1755 if (res_graph->forward(e))
1756 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1758 return (*(res_graph->flow))[e];
1762 // KEEP_MAPS(Parent, ResGraphWrapper);
1766 /// For blocking flows.
1768 ///\warning Graph wrappers are in even more experimental state than the other
1769 ///parts of the lib. Use them at you own risk.
1771 /// This graph wrapper is used for on-the-fly
1772 /// Dinits blocking flow computations.
1773 /// For each node, an out-edge is stored which is used when the
1775 /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1779 /// \author Marton Makai
1780 template<typename Graph, typename FirstOutEdgesMap>
1781 class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1783 typedef GraphWrapper<Graph> Parent;
1785 FirstOutEdgesMap* first_out_edges;
1787 ErasingFirstGraphWrapper(Graph& _graph,
1788 FirstOutEdgesMap& _first_out_edges) :
1789 GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1791 typedef typename GraphWrapper<Graph>::Node Node;
1792 typedef typename GraphWrapper<Graph>::Edge Edge;
1793 class OutEdgeIt : public Edge {
1794 friend class GraphWrapper<Graph>;
1795 friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1796 const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
1799 OutEdgeIt(Invalid i) : Edge(i) { }
1800 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1802 Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
1803 OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1805 Edge(e), gw(&_gw) { }
1806 OutEdgeIt& operator++() {
1807 *(static_cast<Edge*>(this))=
1808 ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1813 // using GraphWrapper<Graph>::first;
1814 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1815 // i=OutEdgeIt(*this, p); return i;
1817 void erase(const Edge& e) const {
1819 typename Graph::OutEdgeIt f(*Parent::graph, n);
1821 first_out_edges->set(n, f);
1824 // KEEP_MAPS(Parent, ErasingFirstGraphWrapper);
1831 #endif //LEMON_GRAPH_WRAPPER_H