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.
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
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);
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.
113 template<typename _Graph>
114 class GraphWrapperBase {
116 typedef _Graph Graph;
117 /// \todo Is it needed?
118 typedef Graph BaseGraph;
119 typedef Graph ParentGraph;
123 GraphWrapperBase() : graph(0) { }
124 void setGraph(Graph& _graph) { graph=&_graph; }
127 GraphWrapperBase(Graph& _graph) : graph(&_graph) { }
128 // GraphWrapperBase(const GraphWrapperBase<_Graph>& gw) : graph(gw.graph) { }
130 typedef typename Graph::Node Node;
131 typedef typename Graph::Edge Edge;
133 void first(Node& i) const { graph->first(i); }
134 void first(Edge& i) const { graph->first(i); }
135 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
136 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
137 // NodeIt& first(NodeIt& i) const {
138 // i=NodeIt(*this); return i;
140 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
141 // i=OutEdgeIt(*this, p); return i;
143 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
144 // i=InEdgeIt(*this, p); return i;
146 // EdgeIt& first(EdgeIt& i) const {
147 // i=EdgeIt(*this); return i;
150 void next(Node& i) const { graph->next(i); }
151 void next(Edge& i) const { graph->next(i); }
152 void nextIn(Edge& i) const { graph->nextIn(i); }
153 void nextOut(Edge& i) const { graph->nextOut(i); }
155 Node source(const Edge& e) const { return graph->source(e); }
156 Node target(const Edge& e) const { return graph->target(e); }
157 // Node source(const Edge& e) const {
158 // return Node(graph->source(static_cast<typename Graph::Edge>(e))); }
159 // Node target(const Edge& e) const {
160 // return Node(graph->target(static_cast<typename Graph::Edge>(e))); }
162 int nodeNum() const { return graph->nodeNum(); }
163 int edgeNum() const { return graph->edgeNum(); }
165 Node addNode() const { return Node(graph->addNode()); }
166 Edge addEdge(const Node& source, const Node& target) const {
167 return Edge(graph->addEdge(source, target)); }
169 void erase(const Node& i) const { graph->erase(i); }
170 void erase(const Edge& i) const { graph->erase(i); }
172 void clear() const { graph->clear(); }
174 bool forward(const Edge& e) const { return graph->forward(e); }
175 bool backward(const Edge& e) const { return graph->backward(e); }
177 int id(const Node& v) const { return graph->id(v); }
178 int id(const Edge& e) const { return graph->id(e); }
180 Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
182 template <typename _Value>
183 class NodeMap : public _Graph::template NodeMap<_Value> {
185 typedef typename _Graph::template NodeMap<_Value> Parent;
186 NodeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
187 NodeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
188 : Parent(*gw.graph, value) { }
191 template <typename _Value>
192 class EdgeMap : public _Graph::template EdgeMap<_Value> {
194 typedef typename _Graph::template EdgeMap<_Value> Parent;
195 EdgeMap(const GraphWrapperBase<_Graph>& gw) : Parent(*gw.graph) { }
196 EdgeMap(const GraphWrapperBase<_Graph>& gw, const _Value& value)
197 : Parent(*gw.graph, value) { }
202 template <typename _Graph>
204 public IterableGraphExtender<GraphWrapperBase<_Graph> > {
206 typedef _Graph Graph;
207 typedef IterableGraphExtender<GraphWrapperBase<_Graph> > Parent;
209 GraphWrapper() : Parent() { }
212 GraphWrapper(Graph& _graph) { setGraph(_graph); }
215 template <typename _Graph>
216 class RevGraphWrapperBase : public GraphWrapperBase<_Graph> {
218 typedef _Graph Graph;
219 typedef GraphWrapperBase<_Graph> Parent;
221 RevGraphWrapperBase() : Parent() { }
223 typedef typename Parent::Node Node;
224 typedef typename Parent::Edge Edge;
227 void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
228 void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
231 void nextIn(Edge& i) const { Parent::nextOut(i); }
232 void nextOut(Edge& i) const { Parent::nextIn(i); }
234 Node source(const Edge& e) const { return Parent::target(e); }
235 Node target(const Edge& e) const { return Parent::source(e); }
239 /// A graph wrapper which reverses the orientation of the edges.
241 ///\warning Graph wrappers are in even more experimental state than the other
242 ///parts of the lib. Use them at you own risk.
244 /// Let \f$G=(V, A)\f$ be a directed graph and
245 /// suppose that a graph instange \c g of type
246 /// \c ListGraph implements \f$G\f$.
250 /// For each directed edge
251 /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
252 /// reversing its orientation.
253 /// Then RevGraphWrapper implements the graph structure with node-set
254 /// \f$V\f$ and edge-set
255 /// \f$\{\bar e : e\in A \}\f$, i.e. the graph obtained from \f$G\f$ be
256 /// reversing the orientation of its edges. The following code shows how
257 /// such an instance can be constructed.
259 /// RevGraphWrapper<ListGraph> gw(g);
261 ///\author Marton Makai
262 template<typename _Graph>
263 class RevGraphWrapper :
264 public IterableGraphExtender<RevGraphWrapperBase<_Graph> > {
266 typedef _Graph Graph;
267 typedef IterableGraphExtender<
268 RevGraphWrapperBase<_Graph> > Parent;
270 RevGraphWrapper() { }
272 RevGraphWrapper(_Graph& _graph) { setGraph(_graph); }
274 // template<typename Graph>
275 // class RevGraphWrapper : public GraphWrapper<Graph> {
277 // typedef GraphWrapper<Graph> Parent;
279 // RevGraphWrapper() : GraphWrapper<Graph>() { }
281 // RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
282 // RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
284 // typedef typename GraphWrapper<Graph>::Node Node;
285 // typedef typename GraphWrapper<Graph>::Edge Edge;
286 // //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
287 // //because this does not work is some of them are not defined in the
288 // //original graph. The problem with this is that typedef-ed stuff
289 // //are instantiated in c++.
290 // class OutEdgeIt : public Edge {
291 // const RevGraphWrapper<Graph>* gw;
292 // friend class GraphWrapper<Graph>;
295 // OutEdgeIt(Invalid i) : Edge(i) { }
296 // OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
297 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
298 // OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
299 // Edge(e), gw(&_gw) { }
300 // OutEdgeIt& operator++() {
301 // *(static_cast<Edge*>(this))=
302 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
306 // class InEdgeIt : public Edge {
307 // const RevGraphWrapper<Graph>* gw;
308 // friend class GraphWrapper<Graph>;
311 // InEdgeIt(Invalid i) : Edge(i) { }
312 // InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
313 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
314 // InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
315 // Edge(e), gw(&_gw) { }
316 // InEdgeIt& operator++() {
317 // *(static_cast<Edge*>(this))=
318 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
323 // using GraphWrapper<Graph>::first;
324 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
325 // i=OutEdgeIt(*this, p); return i;
327 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
328 // i=InEdgeIt(*this, p); return i;
331 // Node source(const Edge& e) const {
332 // return GraphWrapper<Graph>::target(e); }
333 // Node target(const Edge& e) const {
334 // return GraphWrapper<Graph>::source(e); }
336 // // KEEP_MAPS(Parent, RevGraphWrapper);
341 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
342 class SubGraphWrapperBase : public GraphWrapperBase<_Graph> {
344 typedef _Graph Graph;
345 typedef GraphWrapperBase<_Graph> Parent;
347 NodeFilterMap* node_filter_map;
348 EdgeFilterMap* edge_filter_map;
349 SubGraphWrapperBase() : Parent(),
350 node_filter_map(0), edge_filter_map(0) { }
352 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
353 node_filter_map=&_node_filter_map;
355 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
356 edge_filter_map=&_edge_filter_map;
360 // SubGraphWrapperBase(Graph& _graph,
361 // NodeFilterMap& _node_filter_map,
362 // EdgeFilterMap& _edge_filter_map) :
364 // node_filter_map(&node_filter_map),
365 // edge_filter_map(&edge_filter_map) { }
367 typedef typename Parent::Node Node;
368 typedef typename Parent::Edge Edge;
370 void first(Node& i) const {
372 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
374 void first(Edge& i) const {
376 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
378 void firstIn(Edge& i, const Node& n) const {
379 Parent::firstIn(i, n);
380 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
382 void firstOut(Edge& i, const Node& n) const {
383 Parent::firstOut(i, n);
384 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
387 void next(Node& i) const {
389 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
391 void next(Edge& i) const {
393 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
395 void nextIn(Edge& i) const {
397 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
399 void nextOut(Edge& i) const {
401 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
404 /// This function hides \c n in the graph, i.e. the iteration
405 /// jumps over it. This is done by simply setting the value of \c n
406 /// to be false in the corresponding node-map.
407 void hide(const Node& n) const { node_filter_map->set(n, false); }
409 /// This function hides \c e in the graph, i.e. the iteration
410 /// jumps over it. This is done by simply setting the value of \c e
411 /// to be false in the corresponding edge-map.
412 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
414 /// The value of \c n is set to be true in the node-map which stores
415 /// hide information. If \c n was hidden previuosly, then it is shown
417 void unHide(const Node& n) const { node_filter_map->set(n, true); }
419 /// The value of \c e is set to be true in the edge-map which stores
420 /// hide information. If \c e was hidden previuosly, then it is shown
422 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
424 /// Returns true if \c n is hidden.
425 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
427 /// Returns true if \c n is hidden.
428 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
430 /// \warning This is a linear time operation and works only if s
431 /// \c Graph::NodeIt is defined.
432 /// \todo assign tags.
433 int nodeNum() const {
436 for (first(n); n!=INVALID; next(n)) ++i;
440 /// \warning This is a linear time operation and works only if
441 /// \c Graph::EdgeIt is defined.
442 /// \todo assign tags.
443 int edgeNum() const {
446 for (first(e); e!=INVALID; next(e)) ++i;
453 /*! \brief A graph wrapper for hiding nodes and edges from a graph.
455 \warning Graph wrappers are in even more experimental state than the other
456 parts of the lib. Use them at you own risk.
458 This wrapper shows a graph with filtered node-set and
460 Given a bool-valued map on the node-set and one on
461 the edge-set of the graph, the iterators show only the objects
462 having true value. We have to note that this does not mean that an
463 induced subgraph is obtained, the node-iterator cares only the filter
464 on the node-set, and the edge-iterators care only the filter on the
467 typedef SmartGraph Graph;
469 typedef Graph::Node Node;
470 typedef Graph::Edge Edge;
471 Node u=g.addNode(); //node of id 0
472 Node v=g.addNode(); //node of id 1
473 Node e=g.addEdge(u, v); //edge of id 0
474 Node f=g.addEdge(v, u); //edge of id 1
475 Graph::NodeMap<bool> nm(g, true);
477 Graph::EdgeMap<bool> em(g, true);
479 typedef SubGraphWrapper<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
481 for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
482 std::cout << ":-)" << std::endl;
483 for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
485 The output of the above code is the following.
491 Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
492 \c Graph::Node that is why \c g.id(n) can be applied.
494 For other examples see also the documentation of NodeSubGraphWrapper and
499 template<typename _Graph, typename NodeFilterMap,
500 typename EdgeFilterMap>
501 class SubGraphWrapper :
502 public IterableGraphExtender<
503 SubGraphWrapperBase<_Graph, NodeFilterMap, EdgeFilterMap> > {
505 typedef _Graph Graph;
506 typedef IterableGraphExtender<
507 SubGraphWrapperBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
509 SubGraphWrapper() { }
511 SubGraphWrapper(_Graph& _graph, NodeFilterMap& _node_filter_map,
512 EdgeFilterMap& _edge_filter_map) {
514 setNodeFilterMap(_node_filter_map);
515 setEdgeFilterMap(_edge_filter_map);
519 // template<typename Graph, typename NodeFilterMap,
520 // typename EdgeFilterMap>
521 // class SubGraphWrapper : public GraphWrapper<Graph> {
523 // typedef GraphWrapper<Graph> Parent;
525 // NodeFilterMap* node_filter_map;
526 // EdgeFilterMap* edge_filter_map;
528 // SubGraphWrapper() : GraphWrapper<Graph>(),
529 // node_filter_map(0), edge_filter_map(0) { }
530 // void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
531 // node_filter_map=&_node_filter_map;
533 // void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
534 // edge_filter_map=&_edge_filter_map;
538 // SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
539 // EdgeFilterMap& _edge_filter_map) :
540 // GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
541 // edge_filter_map(&_edge_filter_map) { }
543 // typedef typename GraphWrapper<Graph>::Node Node;
544 // class NodeIt : public Node {
545 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
546 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
549 // NodeIt(Invalid i) : Node(i) { }
550 // NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
551 // Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) {
552 // while (*static_cast<Node*>(this)!=INVALID &&
553 // !(*(gw->node_filter_map))[*this])
554 // *(static_cast<Node*>(this))=
555 // ++(typename Graph::NodeIt(*(gw->graph), *this));
557 // NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
559 // Node(n), gw(&_gw) { }
560 // NodeIt& operator++() {
561 // *(static_cast<Node*>(this))=
562 // ++(typename Graph::NodeIt(*(gw->graph), *this));
563 // while (*static_cast<Node*>(this)!=INVALID &&
564 // !(*(gw->node_filter_map))[*this])
565 // *(static_cast<Node*>(this))=
566 // ++(typename Graph::NodeIt(*(gw->graph), *this));
570 // typedef typename GraphWrapper<Graph>::Edge Edge;
571 // class OutEdgeIt : public Edge {
572 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
573 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
576 // OutEdgeIt(Invalid i) : Edge(i) { }
577 // OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
578 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) {
579 // while (*static_cast<Edge*>(this)!=INVALID &&
580 // !(*(gw->edge_filter_map))[*this])
581 // *(static_cast<Edge*>(this))=
582 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
584 // OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
586 // Edge(e), gw(&_gw) { }
587 // OutEdgeIt& operator++() {
588 // *(static_cast<Edge*>(this))=
589 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
590 // while (*static_cast<Edge*>(this)!=INVALID &&
591 // !(*(gw->edge_filter_map))[*this])
592 // *(static_cast<Edge*>(this))=
593 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
597 // class InEdgeIt : public Edge {
598 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
599 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
602 // // InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
603 // InEdgeIt(Invalid i) : Edge(i) { }
604 // InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
605 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) {
606 // while (*static_cast<Edge*>(this)!=INVALID &&
607 // !(*(gw->edge_filter_map))[*this])
608 // *(static_cast<Edge*>(this))=
609 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
611 // InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
613 // Edge(e), gw(&_gw) { }
614 // InEdgeIt& operator++() {
615 // *(static_cast<Edge*>(this))=
616 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
617 // while (*static_cast<Edge*>(this)!=INVALID &&
618 // !(*(gw->edge_filter_map))[*this])
619 // *(static_cast<Edge*>(this))=
620 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
624 // class EdgeIt : public Edge {
625 // const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
626 // friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
629 // EdgeIt(Invalid i) : Edge(i) { }
630 // EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
631 // Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) {
632 // while (*static_cast<Edge*>(this)!=INVALID &&
633 // !(*(gw->edge_filter_map))[*this])
634 // *(static_cast<Edge*>(this))=
635 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
637 // EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
639 // Edge(e), gw(&_gw) { }
640 // EdgeIt& operator++() {
641 // *(static_cast<Edge*>(this))=
642 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
643 // while (*static_cast<Edge*>(this)!=INVALID &&
644 // !(*(gw->edge_filter_map))[*this])
645 // *(static_cast<Edge*>(this))=
646 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
651 // NodeIt& first(NodeIt& i) const {
652 // i=NodeIt(*this); return i;
654 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
655 // i=OutEdgeIt(*this, p); return i;
657 // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
658 // i=InEdgeIt(*this, p); return i;
660 // EdgeIt& first(EdgeIt& i) const {
661 // i=EdgeIt(*this); return i;
664 // /// This function hides \c n in the graph, i.e. the iteration
665 // /// jumps over it. This is done by simply setting the value of \c n
666 // /// to be false in the corresponding node-map.
667 // void hide(const Node& n) const { node_filter_map->set(n, false); }
669 // /// This function hides \c e in the graph, i.e. the iteration
670 // /// jumps over it. This is done by simply setting the value of \c e
671 // /// to be false in the corresponding edge-map.
672 // void hide(const Edge& e) const { edge_filter_map->set(e, false); }
674 // /// The value of \c n is set to be true in the node-map which stores
675 // /// hide information. If \c n was hidden previuosly, then it is shown
677 // void unHide(const Node& n) const { node_filter_map->set(n, true); }
679 // /// The value of \c e is set to be true in the edge-map which stores
680 // /// hide information. If \c e was hidden previuosly, then it is shown
682 // void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
684 // /// Returns true if \c n is hidden.
685 // bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
687 // /// Returns true if \c n is hidden.
688 // bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
690 // /// \warning This is a linear time operation and works only if
691 // /// \c Graph::NodeIt is defined.
692 // int nodeNum() const {
694 // for (NodeIt n(*this); n!=INVALID; ++n) ++i;
698 // /// \warning This is a linear time operation and works only if
699 // /// \c Graph::EdgeIt is defined.
700 // int edgeNum() const {
702 // for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
706 // // KEEP_MAPS(Parent, SubGraphWrapper);
710 /*! \brief A wrapper for hiding nodes from a graph.
712 \warning Graph wrappers are in even more experimental state than the other
713 parts of the lib. Use them at you own risk.
715 A wrapper for hiding nodes from a graph.
716 This wrapper specializes SubGraphWrapper in the way that only the node-set
717 can be filtered. Note that this does not mean of considering induced
718 subgraph, the edge-iterators consider the original edge-set.
721 template<typename Graph, typename NodeFilterMap>
722 class NodeSubGraphWrapper :
723 public SubGraphWrapper<Graph, NodeFilterMap,
724 ConstMap<typename Graph::Edge,bool> > {
726 typedef SubGraphWrapper<Graph, NodeFilterMap,
727 ConstMap<typename Graph::Edge,bool> > Parent;
729 ConstMap<typename Graph::Edge, bool> const_true_map;
731 NodeSubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map) :
732 Parent(), const_true_map(true) {
733 Parent::setGraph(_graph);
734 Parent::setNodeFilterMap(_node_filter_map);
735 Parent::setEdgeFilterMap(const_true_map);
740 /*! \brief A wrapper for hiding edges from a graph.
742 \warning Graph wrappers are in even more experimental state than the other
743 parts of the lib. Use them at you own risk.
745 A wrapper for hiding edges from a graph.
746 This wrapper specializes SubGraphWrapper in the way that only the edge-set
747 can be filtered. The usefulness of this wrapper is demonstrated in the
748 problem of searching a maximum number of edge-disjoint shortest paths
750 two nodes \c s and \c t. Shortest here means being shortest w.r.t.
751 non-negative edge-lengths. Note that
752 the comprehension of the presented solution
753 need's some knowledge from elementary combinatorial optimization.
755 If a single shortest path is to be
756 searched between two nodes \c s and \c t, then this can be done easily by
757 applying the Dijkstra algorithm class. What happens, if a maximum number of
758 edge-disjoint shortest paths is to be computed. It can be proved that an
759 edge can be in a shortest path if and only if it is tight with respect to
760 the potential function computed by Dijkstra. Moreover, any path containing
761 only such edges is a shortest one. Thus we have to compute a maximum number
762 of edge-disjoint paths between \c s and \c t in the graph which has edge-set
763 all the tight edges. The computation will be demonstrated on the following
764 graph, which is read from a dimacs file.
767 digraph lemon_dot_example {
768 node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
769 n0 [ label="0 (s)" ];
775 n6 [ label="6 (t)" ];
776 edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
777 n5 -> n6 [ label="9, length:4" ];
778 n4 -> n6 [ label="8, length:2" ];
779 n3 -> n5 [ label="7, length:1" ];
780 n2 -> n5 [ label="6, length:3" ];
781 n2 -> n6 [ label="5, length:5" ];
782 n2 -> n4 [ label="4, length:2" ];
783 n1 -> n4 [ label="3, length:3" ];
784 n0 -> n3 [ label="2, length:1" ];
785 n0 -> n2 [ label="1, length:2" ];
786 n0 -> n1 [ label="0, length:3" ];
795 readDimacs(std::cin, g, length, s, t);
797 cout << "edges with lengths (of form id, source--length->target): " << endl;
798 for(EdgeIt e(g); e!=INVALID; ++e)
799 cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
800 << length[e] << "->" << g.id(g.target(e)) << endl;
802 cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
804 Next, the potential function is computed with Dijkstra.
806 typedef Dijkstra<Graph, LengthMap> Dijkstra;
807 Dijkstra dijkstra(g, length);
810 Next, we consrtruct a map which filters the edge-set to the tight edges.
812 typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
814 TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
816 typedef EdgeSubGraphWrapper<Graph, TightEdgeFilter> SubGW;
817 SubGW gw(g, tight_edge_filter);
819 Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
820 with a max flow algorithm Preflow.
822 ConstMap<Edge, int> const_1_map(1);
823 Graph::EdgeMap<int> flow(g, 0);
825 Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
826 preflow(gw, s, t, const_1_map, flow);
831 cout << "maximum number of edge-disjoint shortest path: "
832 << preflow.flowValue() << endl;
833 cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
835 for(EdgeIt e(g); e!=INVALID; ++e)
837 cout << " " << g.id(g.source(e)) << "--"
838 << length[e] << "->" << g.id(g.target(e)) << endl;
840 The program has the following (expected :-)) output:
842 edges with lengths (of form id, source--length->target):
854 maximum number of edge-disjoint shortest path: 2
855 edges of the maximum number of edge-disjoint shortest s-t paths:
866 template<typename Graph, typename EdgeFilterMap>
867 class EdgeSubGraphWrapper :
868 public SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>,
871 typedef SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>,
872 EdgeFilterMap> Parent;
874 ConstMap<typename Graph::Node, bool> const_true_map;
876 EdgeSubGraphWrapper(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
877 Parent(), const_true_map(true) {
878 Parent::setGraph(_graph);
879 Parent::setNodeFilterMap(const_true_map);
880 Parent::setEdgeFilterMap(_edge_filter_map);
885 template<typename Graph>
886 class UndirGraphWrapper : public GraphWrapper<Graph> {
888 typedef GraphWrapper<Graph> Parent;
890 UndirGraphWrapper() : GraphWrapper<Graph>() { }
893 typedef typename GraphWrapper<Graph>::Node Node;
894 typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
895 typedef typename GraphWrapper<Graph>::Edge Edge;
896 typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
898 UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
901 friend class UndirGraphWrapper<Graph>;
902 bool out_or_in; //true iff out
903 typename Graph::OutEdgeIt out;
904 typename Graph::InEdgeIt in;
907 OutEdgeIt(const Invalid& i) : Edge(i) { }
908 OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
909 out_or_in=true; _G.graph->first(out, _n);
910 if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
912 operator Edge() const {
913 if (out_or_in) return Edge(out); else return Edge(in);
917 typedef OutEdgeIt InEdgeIt;
919 using GraphWrapper<Graph>::first;
920 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
921 i=OutEdgeIt(*this, p); return i;
924 using GraphWrapper<Graph>::next;
926 OutEdgeIt& next(OutEdgeIt& e) const {
928 typename Graph::Node n=this->graph->source(e.out);
929 this->graph->next(e.out);
930 if (!this->graph->valid(e.out)) {
931 e.out_or_in=false; this->graph->first(e.in, n); }
933 this->graph->next(e.in);
938 Node aNode(const OutEdgeIt& e) const {
939 if (e.out_or_in) return this->graph->source(e); else
940 return this->graph->target(e); }
941 Node bNode(const OutEdgeIt& e) const {
942 if (e.out_or_in) return this->graph->target(e); else
943 return this->graph->source(e); }
945 // KEEP_MAPS(Parent, UndirGraphWrapper);
949 // /// \brief An undirected graph template.
951 // ///\warning Graph wrappers are in even more experimental state than the other
952 // ///parts of the lib. Use them at your own risk.
954 // /// An undirected graph template.
955 // /// This class works as an undirected graph and a directed graph of
956 // /// class \c Graph is used for the physical storage.
957 // /// \ingroup graphs
958 template<typename Graph>
959 class UndirGraph : public UndirGraphWrapper<Graph> {
960 typedef UndirGraphWrapper<Graph> Parent;
964 UndirGraph() : UndirGraphWrapper<Graph>() {
965 Parent::setGraph(gr);
968 // KEEP_MAPS(Parent, UndirGraph);
972 template <typename _Graph,
973 typename ForwardFilterMap, typename BackwardFilterMap>
974 class SubBidirGraphWrapperBase : public GraphWrapperBase<_Graph> {
976 typedef _Graph Graph;
977 typedef GraphWrapperBase<_Graph> Parent;
979 ForwardFilterMap* forward_filter;
980 BackwardFilterMap* backward_filter;
981 SubBidirGraphWrapperBase() : Parent(),
982 forward_filter(0), backward_filter(0) { }
984 void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
985 forward_filter=&_forward_filter;
987 void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
988 backward_filter=&_backward_filter;
992 // SubGraphWrapperBase(Graph& _graph,
993 // NodeFilterMap& _node_filter_map,
994 // EdgeFilterMap& _edge_filter_map) :
996 // node_filter_map(&node_filter_map),
997 // edge_filter_map(&edge_filter_map) { }
999 typedef typename Parent::Node Node;
1000 typedef typename _Graph::Edge GraphEdge;
1001 template <typename T> class EdgeMap;
1002 /// SubBidirGraphWrapperBase<..., ..., ...>::Edge is inherited from
1003 /// _Graph::Edge. It contains an extra bool flag which is true
1004 /// if and only if the
1005 /// edge is the backward version of the original edge.
1006 class Edge : public _Graph::Edge {
1007 friend class SubBidirGraphWrapperBase<
1008 Graph, ForwardFilterMap, BackwardFilterMap>;
1009 template<typename T> friend class EdgeMap;
1011 bool backward; //true, iff backward
1014 /// \todo =false is needed, or causes problems?
1015 /// If \c _backward is false, then we get an edge corresponding to the
1016 /// original one, otherwise its oppositely directed pair is obtained.
1017 Edge(const typename _Graph::Edge& e, bool _backward/*=false*/) :
1018 _Graph::Edge(e), backward(_backward) { }
1019 Edge(Invalid i) : _Graph::Edge(i), backward(true) { }
1020 bool operator==(const Edge& v) const {
1021 return (this->backward==v.backward &&
1022 static_cast<typename _Graph::Edge>(*this)==
1023 static_cast<typename _Graph::Edge>(v));
1025 bool operator!=(const Edge& v) const {
1026 return (this->backward!=v.backward ||
1027 static_cast<typename _Graph::Edge>(*this)!=
1028 static_cast<typename _Graph::Edge>(v));
1032 void first(Node& i) const {
1036 void first(Edge& i) const {
1039 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1040 !(*forward_filter)[i]) Parent::next(i);
1041 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1044 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1045 !(*backward_filter)[i]) Parent::next(i);
1049 void firstIn(Edge& i, const Node& n) const {
1050 Parent::firstIn(i, n);
1052 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1053 !(*forward_filter)[i]) Parent::nextOut(i);
1054 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1055 Parent::firstOut(i, n);
1057 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1058 !(*backward_filter)[i]) Parent::nextOut(i);
1062 void firstOut(Edge& i, const Node& n) const {
1063 Parent::firstOut(i, n);
1065 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1066 !(*forward_filter)[i]) Parent::nextOut(i);
1067 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1068 Parent::firstIn(i, n);
1070 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1071 !(*backward_filter)[i]) Parent::nextIn(i);
1075 void next(Node& i) const {
1079 void next(Edge& i) const {
1080 if (!(i.backward)) {
1082 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1083 !(*forward_filter)[i]) Parent::next(i);
1084 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1087 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1088 !(*backward_filter)[i]) Parent::next(i);
1092 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1093 !(*backward_filter)[i]) Parent::next(i);
1097 void nextIn(Edge& i) const {
1098 if (!(i.backward)) {
1099 Node n=Parent::target(i);
1101 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1102 !(*forward_filter)[i]) Parent::nextIn(i);
1103 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1104 Parent::firstOut(i, n);
1106 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1107 !(*backward_filter)[i]) Parent::nextOut(i);
1111 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1112 !(*backward_filter)[i]) Parent::nextOut(i);
1116 void nextOut(Edge& i) const {
1117 if (!(i.backward)) {
1118 Node n=Parent::source(i);
1120 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1121 !(*forward_filter)[i]) Parent::nextOut(i);
1122 if (*static_cast<GraphEdge*>(&i)==INVALID) {
1123 Parent::firstIn(i, n);
1125 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1126 !(*backward_filter)[i]) Parent::nextIn(i);
1130 while (*static_cast<GraphEdge*>(&i)!=INVALID &&
1131 !(*backward_filter)[i]) Parent::nextIn(i);
1135 Node source(Edge e) const {
1136 return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
1137 Node target(Edge e) const {
1138 return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
1140 /// Gives back the opposite edge.
1141 Edge opposite(const Edge& e) const {
1143 f.backward=!f.backward;
1147 /// \warning This is a linear time operation and works only if
1148 /// \c Graph::EdgeIt is defined.
1150 int edgeNum() const {
1153 for (first(e); e!=INVALID; next(e)) ++i;
1157 bool forward(const Edge& e) const { return !e.backward; }
1158 bool backward(const Edge& e) const { return e.backward; }
1160 template <typename T>
1161 /// \c SubBidirGraphWrapperBase<..., ..., ...>::EdgeMap contains two
1162 /// _Graph::EdgeMap one for the forward edges and
1163 /// one for the backward edges.
1165 template <typename TT> friend class EdgeMap;
1166 typename _Graph::template EdgeMap<T> forward_map, backward_map;
1171 EdgeMap(const SubBidirGraphWrapperBase<_Graph,
1172 ForwardFilterMap, BackwardFilterMap>& g) :
1173 forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1175 EdgeMap(const SubBidirGraphWrapperBase<_Graph,
1176 ForwardFilterMap, BackwardFilterMap>& g, T a) :
1177 forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1179 // template <typename TT>
1180 // EdgeMap(const EdgeMap<TT>& copy)
1181 // : forward_map(copy.forward_map), backward_map(copy.backward_map) {}
1183 // template <typename TT>
1184 // EdgeMap& operator=(const EdgeMap<TT>& copy) {
1185 // forward_map = copy.forward_map;
1186 // backward_map = copy.backward_map;
1190 void set(Edge e, T a) {
1192 forward_map.set(e, a);
1194 backward_map.set(e, a);
1197 // typename _Graph::template EdgeMap<T>::ConstReference
1198 // operator[](Edge e) const {
1200 // return forward_map[e];
1202 // return backward_map[e];
1205 // typename _Graph::template EdgeMap<T>::Reference
1206 T operator[](Edge e) {
1208 return forward_map[e];
1210 return backward_map[e];
1214 forward_map.update();
1215 backward_map.update();
1222 ///\brief A wrapper for composing a subgraph of a
1223 /// bidirected graph made from a directed one.
1225 /// A wrapper for composing a subgraph of a
1226 /// bidirected graph made from a directed one.
1228 ///\warning Graph wrappers are in even more experimental state than the other
1229 ///parts of the lib. Use them at you own risk.
1231 /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge
1232 /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
1233 /// reversing its orientation. We are given moreover two bool valued
1234 /// maps on the edge-set,
1235 /// \f$forward\_filter\f$, and \f$backward\_filter\f$.
1236 /// SubBidirGraphWrapper implements the graph structure with node-set
1237 /// \f$V\f$ and edge-set
1238 /// \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$.
1239 /// The purpose of writing + instead of union is because parallel
1240 /// edges can arise. (Similarly, antiparallel edges also can arise).
1241 /// In other words, a subgraph of the bidirected graph obtained, which
1242 /// is given by orienting the edges of the original graph in both directions.
1243 /// As the oppositely directed edges are logically different,
1244 /// the maps are able to attach different values for them.
1246 /// An example for such a construction is \c RevGraphWrapper where the
1247 /// forward_filter is everywhere false and the backward_filter is
1248 /// everywhere true. We note that for sake of efficiency,
1249 /// \c RevGraphWrapper is implemented in a different way.
1250 /// But BidirGraphWrapper is obtained from
1251 /// SubBidirGraphWrapper by considering everywhere true
1252 /// valued maps both for forward_filter and backward_filter.
1253 /// Finally, one of the most important applications of SubBidirGraphWrapper
1254 /// is ResGraphWrapper, which stands for the residual graph in directed
1255 /// flow and circulation problems.
1256 /// As wrappers usually, the SubBidirGraphWrapper implements the
1257 /// above mentioned graph structure without its physical storage,
1258 /// that is the whole stuff is stored in constant memory.
1259 template<typename _Graph,
1260 typename ForwardFilterMap, typename BackwardFilterMap>
1261 class SubBidirGraphWrapper :
1262 public IterableGraphExtender<
1263 SubBidirGraphWrapperBase<_Graph, ForwardFilterMap, BackwardFilterMap> > {
1265 typedef _Graph Graph;
1266 typedef IterableGraphExtender<
1267 SubBidirGraphWrapperBase<
1268 _Graph, ForwardFilterMap, BackwardFilterMap> > Parent;
1270 SubBidirGraphWrapper() { }
1272 SubBidirGraphWrapper(_Graph& _graph, ForwardFilterMap& _forward_filter,
1273 BackwardFilterMap& _backward_filter) {
1275 setForwardFilterMap(_forward_filter);
1276 setBackwardFilterMap(_backward_filter);
1280 // template<typename Graph,
1281 // typename ForwardFilterMap, typename BackwardFilterMap>
1282 // class SubBidirGraphWrapper : public GraphWrapper<Graph> {
1284 // typedef GraphWrapper<Graph> Parent;
1286 // ForwardFilterMap* forward_filter;
1287 // BackwardFilterMap* backward_filter;
1289 // SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
1290 // void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
1291 // forward_filter=&_forward_filter;
1293 // void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
1294 // backward_filter=&_backward_filter;
1299 // SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
1300 // BackwardFilterMap& _backward_filter) :
1301 // GraphWrapper<Graph>(_graph),
1302 // forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
1303 // SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph,
1304 // ForwardFilterMap, BackwardFilterMap>& gw) :
1306 // forward_filter(gw.forward_filter),
1307 // backward_filter(gw.backward_filter) { }
1311 // friend class Edge;
1312 // friend class OutEdgeIt;
1314 // template<typename T> class EdgeMap;
1316 // typedef typename GraphWrapper<Graph>::Node Node;
1318 // typedef typename Graph::Edge GraphEdge;
1319 // /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from
1320 // /// Graph::Edge. It contains an extra bool flag which is true
1321 // /// if and only if the
1322 // /// edge is the backward version of the original edge.
1323 // class Edge : public Graph::Edge {
1324 // friend class SubBidirGraphWrapper<Graph,
1325 // ForwardFilterMap, BackwardFilterMap>;
1326 // template<typename T> friend class EdgeMap;
1328 // bool backward; //true, iff backward
1331 // /// \todo =false is needed, or causes problems?
1332 // /// If \c _backward is false, then we get an edge corresponding to the
1333 // /// original one, otherwise its oppositely directed pair is obtained.
1334 // Edge(const typename Graph::Edge& e, bool _backward/*=false*/) :
1335 // Graph::Edge(e), backward(_backward) { }
1336 // Edge(Invalid i) : Graph::Edge(i), backward(true) { }
1337 // bool operator==(const Edge& v) const {
1338 // return (this->backward==v.backward &&
1339 // static_cast<typename Graph::Edge>(*this)==
1340 // static_cast<typename Graph::Edge>(v));
1342 // bool operator!=(const Edge& v) const {
1343 // return (this->backward!=v.backward ||
1344 // static_cast<typename Graph::Edge>(*this)!=
1345 // static_cast<typename Graph::Edge>(v));
1349 // class OutEdgeIt : public Edge {
1350 // friend class SubBidirGraphWrapper<Graph,
1351 // ForwardFilterMap, BackwardFilterMap>;
1353 // const SubBidirGraphWrapper<Graph,
1354 // ForwardFilterMap, BackwardFilterMap>* gw;
1357 // OutEdgeIt(Invalid i) : Edge(i) { }
1358 // OutEdgeIt(const SubBidirGraphWrapper<Graph,
1359 // ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
1360 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
1361 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1362 // !(*(gw->forward_filter))[*this])
1363 // *(static_cast<GraphEdge*>(this))=
1364 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1365 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1366 // *static_cast<Edge*>(this)=
1367 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
1368 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1369 // !(*(gw->backward_filter))[*this])
1370 // *(static_cast<GraphEdge*>(this))=
1371 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1374 // OutEdgeIt(const SubBidirGraphWrapper<Graph,
1375 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1376 // Edge(e), gw(&_gw) { }
1377 // OutEdgeIt& operator++() {
1378 // if (!this->backward) {
1379 // Node n=gw->source(*this);
1380 // *(static_cast<GraphEdge*>(this))=
1381 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1382 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1383 // !(*(gw->forward_filter))[*this])
1384 // *(static_cast<GraphEdge*>(this))=
1385 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1386 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1387 // *static_cast<Edge*>(this)=
1388 // Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
1389 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1390 // !(*(gw->backward_filter))[*this])
1391 // *(static_cast<GraphEdge*>(this))=
1392 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1395 // *(static_cast<GraphEdge*>(this))=
1396 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1397 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1398 // !(*(gw->backward_filter))[*this])
1399 // *(static_cast<GraphEdge*>(this))=
1400 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1406 // class InEdgeIt : public Edge {
1407 // friend class SubBidirGraphWrapper<Graph,
1408 // ForwardFilterMap, BackwardFilterMap>;
1410 // const SubBidirGraphWrapper<Graph,
1411 // ForwardFilterMap, BackwardFilterMap>* gw;
1414 // InEdgeIt(Invalid i) : Edge(i) { }
1415 // InEdgeIt(const SubBidirGraphWrapper<Graph,
1416 // ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
1417 // Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
1418 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1419 // !(*(gw->forward_filter))[*this])
1420 // *(static_cast<GraphEdge*>(this))=
1421 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1422 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1423 // *static_cast<Edge*>(this)=
1424 // Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
1425 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1426 // !(*(gw->backward_filter))[*this])
1427 // *(static_cast<GraphEdge*>(this))=
1428 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1431 // InEdgeIt(const SubBidirGraphWrapper<Graph,
1432 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1433 // Edge(e), gw(&_gw) { }
1434 // InEdgeIt& operator++() {
1435 // if (!this->backward) {
1436 // Node n=gw->source(*this);
1437 // *(static_cast<GraphEdge*>(this))=
1438 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1439 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1440 // !(*(gw->forward_filter))[*this])
1441 // *(static_cast<GraphEdge*>(this))=
1442 // ++(typename Graph::InEdgeIt(*(gw->graph), *this));
1443 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1444 // *static_cast<Edge*>(this)=
1445 // Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
1446 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1447 // !(*(gw->backward_filter))[*this])
1448 // *(static_cast<GraphEdge*>(this))=
1449 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1452 // *(static_cast<GraphEdge*>(this))=
1453 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1454 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1455 // !(*(gw->backward_filter))[*this])
1456 // *(static_cast<GraphEdge*>(this))=
1457 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1463 // class EdgeIt : public Edge {
1464 // friend class SubBidirGraphWrapper<Graph,
1465 // ForwardFilterMap, BackwardFilterMap>;
1467 // const SubBidirGraphWrapper<Graph,
1468 // ForwardFilterMap, BackwardFilterMap>* gw;
1471 // EdgeIt(Invalid i) : Edge(i) { }
1472 // EdgeIt(const SubBidirGraphWrapper<Graph,
1473 // ForwardFilterMap, BackwardFilterMap>& _gw) :
1474 // Edge(typename Graph::EdgeIt(*(_gw.graph)), false), gw(&_gw) {
1475 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1476 // !(*(gw->forward_filter))[*this])
1477 // *(static_cast<GraphEdge*>(this))=
1478 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1479 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1480 // *static_cast<Edge*>(this)=
1481 // Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
1482 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1483 // !(*(gw->backward_filter))[*this])
1484 // *(static_cast<GraphEdge*>(this))=
1485 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1488 // EdgeIt(const SubBidirGraphWrapper<Graph,
1489 // ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
1490 // Edge(e), gw(&_gw) { }
1491 // EdgeIt& operator++() {
1492 // if (!this->backward) {
1493 // *(static_cast<GraphEdge*>(this))=
1494 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1495 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1496 // !(*(gw->forward_filter))[*this])
1497 // *(static_cast<GraphEdge*>(this))=
1498 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1499 // if (*static_cast<GraphEdge*>(this)==INVALID) {
1500 // *static_cast<Edge*>(this)=
1501 // Edge(typename Graph::EdgeIt(*(gw->graph)), true);
1502 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1503 // !(*(gw->backward_filter))[*this])
1504 // *(static_cast<GraphEdge*>(this))=
1505 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1508 // *(static_cast<GraphEdge*>(this))=
1509 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1510 // while (*static_cast<GraphEdge*>(this)!=INVALID &&
1511 // !(*(gw->backward_filter))[*this])
1512 // *(static_cast<GraphEdge*>(this))=
1513 // ++(typename Graph::EdgeIt(*(gw->graph), *this));
1519 // // using GraphWrapper<Graph>::first;
1520 // // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1521 // // i=OutEdgeIt(*this, p); return i;
1523 // // InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1524 // // i=InEdgeIt(*this, p); return i;
1526 // // EdgeIt& first(EdgeIt& i) const {
1527 // // i=EdgeIt(*this); return i;
1531 // Node source(Edge e) const {
1532 // return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
1533 // Node target(Edge e) const {
1534 // return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
1536 // /// Gives back the opposite edge.
1537 // Edge opposite(const Edge& e) const {
1539 // f.backward=!f.backward;
1543 // /// \warning This is a linear time operation and works only if
1544 // /// \c Graph::EdgeIt is defined.
1545 // int edgeNum() const {
1547 // for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
1551 // bool forward(const Edge& e) const { return !e.backward; }
1552 // bool backward(const Edge& e) const { return e.backward; }
1555 // template <typename T>
1556 // /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two
1557 // /// Graph::EdgeMap one for the forward edges and
1558 // /// one for the backward edges.
1560 // template <typename TT> friend class EdgeMap;
1561 // typename Graph::template EdgeMap<T> forward_map, backward_map;
1564 // typedef Edge Key;
1566 // EdgeMap(const SubBidirGraphWrapper<Graph,
1567 // ForwardFilterMap, BackwardFilterMap>& g) :
1568 // forward_map(*(g.graph)), backward_map(*(g.graph)) { }
1570 // EdgeMap(const SubBidirGraphWrapper<Graph,
1571 // ForwardFilterMap, BackwardFilterMap>& g, T a) :
1572 // forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
1574 // template <typename TT>
1575 // EdgeMap(const EdgeMap<TT>& copy)
1576 // : forward_map(copy.forward_map), backward_map(copy.backward_map) {}
1578 // template <typename TT>
1579 // EdgeMap& operator=(const EdgeMap<TT>& copy) {
1580 // forward_map = copy.forward_map;
1581 // backward_map = copy.backward_map;
1585 // void set(Edge e, T a) {
1587 // forward_map.set(e, a);
1589 // backward_map.set(e, a);
1592 // typename Graph::template EdgeMap<T>::ConstReference
1593 // operator[](Edge e) const {
1595 // return forward_map[e];
1597 // return backward_map[e];
1600 // typename Graph::template EdgeMap<T>::Reference
1601 // operator[](Edge e) {
1603 // return forward_map[e];
1605 // return backward_map[e];
1609 // forward_map.update();
1610 // backward_map.update();
1615 // // KEEP_NODE_MAP(Parent, SubBidirGraphWrapper);
1620 ///\brief A wrapper for composing bidirected graph from a directed one.
1622 ///\warning Graph wrappers are in even more experimental state than the other
1623 ///parts of the lib. Use them at you own risk.
1625 /// A wrapper for composing bidirected graph from a directed one.
1626 /// A bidirected graph is composed over the directed one without physical
1627 /// storage. As the oppositely directed edges are logically different ones
1628 /// the maps are able to attach different values for them.
1629 template<typename Graph>
1630 class BidirGraphWrapper :
1631 public SubBidirGraphWrapper<
1633 ConstMap<typename Graph::Edge, bool>,
1634 ConstMap<typename Graph::Edge, bool> > {
1636 typedef SubBidirGraphWrapper<
1638 ConstMap<typename Graph::Edge, bool>,
1639 ConstMap<typename Graph::Edge, bool> > Parent;
1641 ConstMap<typename Graph::Edge, bool> cm;
1643 BidirGraphWrapper() : Parent(), cm(true) {
1644 Parent::setForwardFilterMap(cm);
1645 Parent::setBackwardFilterMap(cm);
1648 BidirGraphWrapper(Graph& _graph) : Parent() {
1649 Parent::setGraph(_graph);
1650 Parent::setForwardFilterMap(cm);
1651 Parent::setBackwardFilterMap(cm);
1654 int edgeNum() const {
1655 return 2*this->graph->edgeNum();
1657 // KEEP_MAPS(Parent, BidirGraphWrapper);
1661 /// \brief A bidirected graph template.
1663 ///\warning Graph wrappers are in even more experimental state than the other
1664 ///parts of the lib. Use them at you own risk.
1666 /// A bidirected graph template.
1667 /// Such a bidirected graph stores each pair of oppositely directed edges
1668 /// ones in the memory, i.e. a directed graph of type
1669 /// \c Graph is used for that.
1670 /// As the oppositely directed edges are logically different ones
1671 /// the maps are able to attach different values for them.
1673 template<typename Graph>
1674 class BidirGraph : public BidirGraphWrapper<Graph> {
1676 typedef UndirGraphWrapper<Graph> Parent;
1680 BidirGraph() : BidirGraphWrapper<Graph>() {
1681 Parent::setGraph(gr);
1683 // KEEP_MAPS(Parent, BidirGraph);
1688 template<typename Graph, typename Number,
1689 typename CapacityMap, typename FlowMap>
1690 class ResForwardFilter {
1691 // const Graph* graph;
1692 const CapacityMap* capacity;
1693 const FlowMap* flow;
1695 ResForwardFilter(/*const Graph& _graph, */
1696 const CapacityMap& _capacity, const FlowMap& _flow) :
1697 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1698 ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1699 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1700 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1701 bool operator[](const typename Graph::Edge& e) const {
1702 return (Number((*flow)[e]) < Number((*capacity)[e]));
1706 template<typename Graph, typename Number,
1707 typename CapacityMap, typename FlowMap>
1708 class ResBackwardFilter {
1709 const CapacityMap* capacity;
1710 const FlowMap* flow;
1712 ResBackwardFilter(/*const Graph& _graph,*/
1713 const CapacityMap& _capacity, const FlowMap& _flow) :
1714 /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
1715 ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
1716 void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
1717 void setFlow(const FlowMap& _flow) { flow=&_flow; }
1718 bool operator[](const typename Graph::Edge& e) const {
1719 return (Number(0) < Number((*flow)[e]));
1724 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1726 ///\warning Graph wrappers are in even more experimental state than the other
1727 ///parts of the lib. Use them at you own risk.
1729 /// A wrapper for composing the residual graph for directed flow and circulation problems.
1730 template<typename Graph, typename Number,
1731 typename CapacityMap, typename FlowMap>
1732 class ResGraphWrapper :
1733 public SubBidirGraphWrapper<
1735 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1736 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
1738 typedef SubBidirGraphWrapper<
1740 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1741 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
1743 const CapacityMap* capacity;
1745 ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
1746 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
1747 ResGraphWrapper() : Parent(),
1748 capacity(0), flow(0) { }
1749 void setCapacityMap(const CapacityMap& _capacity) {
1750 capacity=&_capacity;
1751 forward_filter.setCapacity(_capacity);
1752 backward_filter.setCapacity(_capacity);
1754 void setFlowMap(FlowMap& _flow) {
1756 forward_filter.setFlow(_flow);
1757 backward_filter.setFlow(_flow);
1760 ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
1762 Parent(), capacity(&_capacity), flow(&_flow),
1763 forward_filter(/*_graph,*/ _capacity, _flow),
1764 backward_filter(/*_graph,*/ _capacity, _flow) {
1765 Parent::setGraph(_graph);
1766 Parent::setForwardFilterMap(forward_filter);
1767 Parent::setBackwardFilterMap(backward_filter);
1770 typedef typename Parent::Edge Edge;
1772 void augment(const Edge& e, Number a) const {
1773 if (Parent::forward(e))
1774 flow->set(e, (*flow)[e]+a);
1776 flow->set(e, (*flow)[e]-a);
1779 /// \brief Residual capacity map.
1781 /// In generic residual graphs the residual capacity can be obtained
1785 const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
1787 typedef Number Value;
1789 ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>&
1790 _res_graph) : res_graph(&_res_graph) { }
1791 Number operator[](const Edge& e) const {
1792 if (res_graph->forward(e))
1793 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1795 return (*(res_graph->flow))[e];
1799 // KEEP_MAPS(Parent, ResGraphWrapper);
1804 template <typename _Graph, typename FirstOutEdgesMap>
1805 class ErasingFirstGraphWrapperBase : public GraphWrapperBase<_Graph> {
1807 typedef _Graph Graph;
1808 typedef GraphWrapperBase<_Graph> Parent;
1810 FirstOutEdgesMap* first_out_edges;
1811 ErasingFirstGraphWrapperBase() : Parent(),
1812 first_out_edges(0) { }
1814 void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
1815 first_out_edges=&_first_out_edges;
1820 typedef typename Parent::Node Node;
1821 typedef typename Parent::Edge Edge;
1823 // using Parent::first;
1824 // void first(Node& i) const {
1825 // Parent::first(i);
1826 // while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
1828 // void first(Edge& i) const {
1829 // Parent::first(i);
1830 // while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
1832 // void firstIn(Edge& i, const Node& n) const {
1833 // Parent::firstIn(i, n);
1834 // while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
1836 void firstOut(Edge& i, const Node& n) const {
1837 i=(*first_out_edges)[n];
1840 void erase(const Edge& e) const {
1844 first_out_edges->set(n, f);
1846 // void next(Node& i) const {
1848 // while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
1850 // void next(Edge& i) const {
1852 // while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
1854 // void nextIn(Edge& i) const {
1855 // Parent::nextIn(i);
1856 // while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
1858 // void nextOut(Edge& i) const {
1859 // Parent::nextOut(i);
1860 // while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
1865 /// For blocking flows.
1867 ///\warning Graph wrappers are in even more experimental state than the other
1868 ///parts of the lib. Use them at you own risk.
1870 /// This graph wrapper is used for on-the-fly
1871 /// Dinits blocking flow computations.
1872 /// For each node, an out-edge is stored which is used when the
1874 /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1878 /// \author Marton Makai
1879 template <typename _Graph, typename FirstOutEdgesMap>
1880 class ErasingFirstGraphWrapper :
1881 public IterableGraphExtender<
1882 ErasingFirstGraphWrapperBase<_Graph, FirstOutEdgesMap> > {
1884 typedef _Graph Graph;
1885 typedef IterableGraphExtender<
1886 ErasingFirstGraphWrapperBase<_Graph, FirstOutEdgesMap> > Parent;
1887 ErasingFirstGraphWrapper(Graph& _graph,
1888 FirstOutEdgesMap& _first_out_edges) {
1890 setFirstOutEdgesMap(_first_out_edges);
1892 // using GraphWrapper<Graph>::first;
1893 // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1894 // i=OutEdgeIt(*this, p); return i;
1897 // template<typename Graph, typename FirstOutEdgesMap>
1898 // class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1900 // typedef GraphWrapper<Graph> Parent;
1902 // FirstOutEdgesMap* first_out_edges;
1904 // ErasingFirstGraphWrapper(Graph& _graph,
1905 // FirstOutEdgesMap& _first_out_edges) :
1906 // GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
1908 // typedef typename GraphWrapper<Graph>::Node Node;
1909 // typedef typename GraphWrapper<Graph>::Edge Edge;
1910 // class OutEdgeIt : public Edge {
1911 // friend class GraphWrapper<Graph>;
1912 // friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1913 // const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
1916 // OutEdgeIt(Invalid i) : Edge(i) { }
1917 // OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1919 // Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
1920 // OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
1922 // Edge(e), gw(&_gw) { }
1923 // OutEdgeIt& operator++() {
1924 // *(static_cast<Edge*>(this))=
1925 // ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
1930 // // using GraphWrapper<Graph>::first;
1931 // // OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1932 // // i=OutEdgeIt(*this, p); return i;
1934 // void erase(const Edge& e) const {
1935 // Node n=source(e);
1936 // typename Graph::OutEdgeIt f(*Parent::graph, n);
1938 // first_out_edges->set(n, f);
1941 // // KEEP_MAPS(Parent, ErasingFirstGraphWrapper);
1948 #endif //LEMON_GRAPH_WRAPPER_H