3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_GRAPH_ADAPTOR_H
20 #define LEMON_GRAPH_ADAPTOR_H
22 ///\ingroup graph_adaptors
24 ///\brief Several graph adaptors.
26 ///This file contains several useful graph adaptor functions.
28 ///\author Marton Makai and Balazs Dezso
30 #include <lemon/bits/invalid.h>
31 #include <lemon/maps.h>
33 #include <lemon/bits/base_extender.h>
34 #include <lemon/bits/graph_adaptor_extender.h>
35 #include <lemon/bits/graph_extender.h>
37 #include <lemon/tolerance.h>
43 ///\brief Base type for the Graph Adaptors
44 ///\ingroup graph_adaptors
46 ///Base type for the Graph Adaptors
48 ///\warning Graph adaptors are in even
49 ///more experimental state than the other
50 ///parts of the lib. Use them at you own risk.
52 ///This is the base type for most of LEMON graph adaptors.
53 ///This class implements a trivial graph adaptor i.e. it only wraps the
54 ///functions and types of the graph. The purpose of this class is to
55 ///make easier implementing graph adaptors. E.g. if an adaptor is
56 ///considered which differs from the wrapped graph only in some of its
57 ///functions or types, then it can be derived from GraphAdaptor,
59 ///differences should be implemented.
61 ///author Marton Makai
62 template<typename _Graph>
63 class GraphAdaptorBase {
66 typedef GraphAdaptorBase Adaptor;
67 typedef Graph ParentGraph;
71 GraphAdaptorBase() : graph(0) { }
72 void setGraph(Graph& _graph) { graph=&_graph; }
75 GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
77 typedef typename Graph::Node Node;
78 typedef typename Graph::Edge Edge;
80 void first(Node& i) const { graph->first(i); }
81 void first(Edge& i) const { graph->first(i); }
82 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
83 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
85 void next(Node& i) const { graph->next(i); }
86 void next(Edge& i) const { graph->next(i); }
87 void nextIn(Edge& i) const { graph->nextIn(i); }
88 void nextOut(Edge& i) const { graph->nextOut(i); }
90 Node source(const Edge& e) const { return graph->source(e); }
91 Node target(const Edge& e) const { return graph->target(e); }
93 typedef NodeNumTagIndicator<Graph> NodeNumTag;
94 int nodeNum() const { return graph->nodeNum(); }
96 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
97 int edgeNum() const { return graph->edgeNum(); }
99 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
100 Edge findEdge(const Node& source, const Node& target,
101 const Edge& prev = INVALID) {
102 return graph->findEdge(source, target, prev);
105 Node addNode() const {
106 return Node(graph->addNode());
109 Edge addEdge(const Node& source, const Node& target) const {
110 return Edge(graph->addEdge(source, target));
113 void erase(const Node& i) const { graph->erase(i); }
114 void erase(const Edge& i) const { graph->erase(i); }
116 void clear() const { graph->clear(); }
118 int id(const Node& v) const { return graph->id(v); }
119 int id(const Edge& e) const { return graph->id(e); }
121 Node fromNodeId(int id) const {
122 return graph->fromNodeId(id);
125 Edge fromEdgeId(int id) const {
126 return graph->fromEdgeId(id);
129 int maxNodeId() const {
130 return graph->maxNodeId();
133 int maxEdgeId() const {
134 return graph->maxEdgeId();
137 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
139 NodeNotifier& getNotifier(Node) const {
140 return graph->getNotifier(Node());
143 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
145 EdgeNotifier& getNotifier(Edge) const {
146 return graph->getNotifier(Edge());
149 template <typename _Value>
150 class NodeMap : public Graph::template NodeMap<_Value> {
153 typedef typename Graph::template NodeMap<_Value> Parent;
155 explicit NodeMap(const Adaptor& ga)
156 : Parent(*ga.graph) {}
158 NodeMap(const Adaptor& ga, const _Value& value)
159 : Parent(*ga.graph, value) { }
161 NodeMap& operator=(const NodeMap& cmap) {
162 return operator=<NodeMap>(cmap);
165 template <typename CMap>
166 NodeMap& operator=(const CMap& cmap) {
167 Parent::operator=(cmap);
173 template <typename _Value>
174 class EdgeMap : public Graph::template EdgeMap<_Value> {
177 typedef typename Graph::template EdgeMap<_Value> Parent;
179 explicit EdgeMap(const Adaptor& ga)
180 : Parent(*ga.graph) {}
182 EdgeMap(const Adaptor& ga, const _Value& value)
183 : Parent(*ga.graph, value) {}
185 EdgeMap& operator=(const EdgeMap& cmap) {
186 return operator=<EdgeMap>(cmap);
189 template <typename CMap>
190 EdgeMap& operator=(const CMap& cmap) {
191 Parent::operator=(cmap);
199 template <typename _Graph>
201 public GraphAdaptorExtender<GraphAdaptorBase<_Graph> > {
203 typedef _Graph Graph;
204 typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent;
206 GraphAdaptor() : Parent() { }
209 explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
212 /// \brief Just gives back a graph adaptor
214 /// Just gives back a graph adaptor which
215 /// should be provide original graph
216 template<typename Graph>
217 GraphAdaptor<const Graph>
218 graphAdaptor(const Graph& graph) {
219 return GraphAdaptor<const Graph>(graph);
223 template <typename _Graph>
224 class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
226 typedef _Graph Graph;
227 typedef GraphAdaptorBase<_Graph> Parent;
229 RevGraphAdaptorBase() : Parent() { }
231 typedef typename Parent::Node Node;
232 typedef typename Parent::Edge Edge;
234 void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
235 void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
237 void nextIn(Edge& i) const { Parent::nextOut(i); }
238 void nextOut(Edge& i) const { Parent::nextIn(i); }
240 Node source(const Edge& e) const { return Parent::target(e); }
241 Node target(const Edge& e) const { return Parent::source(e); }
243 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
244 Edge findEdge(const Node& source, const Node& target,
245 const Edge& prev = INVALID) {
246 return Parent::findEdge(target, source, prev);
252 ///\brief A graph adaptor which reverses the orientation of the edges.
253 ///\ingroup graph_adaptors
255 ///\warning Graph adaptors are in even more experimental
256 ///state than the other
257 ///parts of the lib. Use them at you own risk.
259 /// If \c g is defined as
265 /// RevGraphAdaptor<ListGraph> ga(g);
267 ///implements the graph obtained from \c g by
268 /// reversing the orientation of its edges.
270 template<typename _Graph>
271 class RevGraphAdaptor :
272 public GraphAdaptorExtender<RevGraphAdaptorBase<_Graph> > {
274 typedef _Graph Graph;
275 typedef GraphAdaptorExtender<
276 RevGraphAdaptorBase<_Graph> > Parent;
278 RevGraphAdaptor() { }
280 explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
283 /// \brief Just gives back a reverse graph adaptor
285 /// Just gives back a reverse graph adaptor
286 template<typename Graph>
287 RevGraphAdaptor<const Graph>
288 revGraphAdaptor(const Graph& graph) {
289 return RevGraphAdaptor<const Graph>(graph);
292 template <typename _Graph, typename NodeFilterMap,
293 typename EdgeFilterMap, bool checked = true>
294 class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
296 typedef _Graph Graph;
297 typedef SubGraphAdaptorBase Adaptor;
298 typedef GraphAdaptorBase<_Graph> Parent;
300 NodeFilterMap* node_filter_map;
301 EdgeFilterMap* edge_filter_map;
302 SubGraphAdaptorBase() : Parent(),
303 node_filter_map(0), edge_filter_map(0) { }
305 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
306 node_filter_map=&_node_filter_map;
308 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
309 edge_filter_map=&_edge_filter_map;
314 typedef typename Parent::Node Node;
315 typedef typename Parent::Edge Edge;
317 void first(Node& i) const {
319 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
322 void first(Edge& i) const {
324 while (i!=INVALID && (!(*edge_filter_map)[i]
325 || !(*node_filter_map)[Parent::source(i)]
326 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
329 void firstIn(Edge& i, const Node& n) const {
330 Parent::firstIn(i, n);
331 while (i!=INVALID && (!(*edge_filter_map)[i]
332 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
335 void firstOut(Edge& i, const Node& n) const {
336 Parent::firstOut(i, n);
337 while (i!=INVALID && (!(*edge_filter_map)[i]
338 || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
341 void next(Node& i) const {
343 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
346 void next(Edge& i) const {
348 while (i!=INVALID && (!(*edge_filter_map)[i]
349 || !(*node_filter_map)[Parent::source(i)]
350 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
353 void nextIn(Edge& i) const {
355 while (i!=INVALID && (!(*edge_filter_map)[i]
356 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
359 void nextOut(Edge& i) const {
361 while (i!=INVALID && (!(*edge_filter_map)[i]
362 || !(*node_filter_map)[Parent::target(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); }
374 /// This function hides \c e in the graph, i.e. the iteration
375 /// jumps over it. This is done by simply setting the value of \c e
376 /// to be false in the corresponding edge-map.
377 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
381 /// The value of \c n is set to be true in the node-map which stores
382 /// hide information. If \c n was hidden previuosly, then it is shown
384 void unHide(const Node& n) const { node_filter_map->set(n, true); }
388 /// The value of \c e is set to be true in the edge-map which stores
389 /// hide information. If \c e was hidden previuosly, then it is shown
391 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
393 /// Returns true if \c n is hidden.
397 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
399 /// Returns true if \c n is hidden.
403 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
405 typedef False NodeNumTag;
406 typedef False EdgeNumTag;
408 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
409 Edge findEdge(const Node& source, const Node& target,
410 const Edge& prev = INVALID) {
411 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
414 Edge edge = Parent::findEdge(source, target, prev);
415 while (edge != INVALID && !(*edge_filter_map)[edge]) {
416 edge = Parent::findEdge(source, target, edge);
421 template <typename _Value>
423 : public SubMapExtender<Adaptor,
424 typename Parent::template NodeMap<_Value> >
427 typedef Adaptor Graph;
428 typedef SubMapExtender<Adaptor, typename Parent::
429 template NodeMap<_Value> > Parent;
431 NodeMap(const Graph& graph)
433 NodeMap(const Graph& graph, const _Value& value)
434 : Parent(graph, value) {}
436 NodeMap& operator=(const NodeMap& cmap) {
437 return operator=<NodeMap>(cmap);
440 template <typename CMap>
441 NodeMap& operator=(const CMap& cmap) {
442 Parent::operator=(cmap);
447 template <typename _Value>
449 : public SubMapExtender<Adaptor,
450 typename Parent::template EdgeMap<_Value> >
453 typedef Adaptor Graph;
454 typedef SubMapExtender<Adaptor, typename Parent::
455 template EdgeMap<_Value> > Parent;
457 EdgeMap(const Graph& graph)
459 EdgeMap(const Graph& graph, const _Value& value)
460 : Parent(graph, value) {}
462 EdgeMap& operator=(const EdgeMap& cmap) {
463 return operator=<EdgeMap>(cmap);
466 template <typename CMap>
467 EdgeMap& operator=(const CMap& cmap) {
468 Parent::operator=(cmap);
475 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
476 class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
477 : public GraphAdaptorBase<_Graph> {
479 typedef _Graph Graph;
480 typedef SubGraphAdaptorBase Adaptor;
481 typedef GraphAdaptorBase<_Graph> Parent;
483 NodeFilterMap* node_filter_map;
484 EdgeFilterMap* edge_filter_map;
485 SubGraphAdaptorBase() : Parent(),
486 node_filter_map(0), edge_filter_map(0) { }
488 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
489 node_filter_map=&_node_filter_map;
491 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
492 edge_filter_map=&_edge_filter_map;
497 typedef typename Parent::Node Node;
498 typedef typename Parent::Edge Edge;
500 void first(Node& i) const {
502 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
505 void first(Edge& i) const {
507 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
510 void firstIn(Edge& i, const Node& n) const {
511 Parent::firstIn(i, n);
512 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
515 void firstOut(Edge& i, const Node& n) const {
516 Parent::firstOut(i, n);
517 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
520 void next(Node& i) const {
522 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
524 void next(Edge& i) const {
526 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
528 void nextIn(Edge& i) const {
530 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
533 void nextOut(Edge& i) const {
535 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
540 /// This function hides \c n in the graph, i.e. the iteration
541 /// jumps over it. This is done by simply setting the value of \c n
542 /// to be false in the corresponding node-map.
543 void hide(const Node& n) const { node_filter_map->set(n, false); }
547 /// This function hides \c e in the graph, i.e. the iteration
548 /// jumps over it. This is done by simply setting the value of \c e
549 /// to be false in the corresponding edge-map.
550 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
554 /// The value of \c n is set to be true in the node-map which stores
555 /// hide information. If \c n was hidden previuosly, then it is shown
557 void unHide(const Node& n) const { node_filter_map->set(n, true); }
561 /// The value of \c e is set to be true in the edge-map which stores
562 /// hide information. If \c e was hidden previuosly, then it is shown
564 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
566 /// Returns true if \c n is hidden.
570 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
572 /// Returns true if \c n is hidden.
576 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
578 typedef False NodeNumTag;
579 typedef False EdgeNumTag;
581 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
582 Edge findEdge(const Node& source, const Node& target,
583 const Edge& prev = INVALID) {
584 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
587 Edge edge = Parent::findEdge(source, target, prev);
588 while (edge != INVALID && !(*edge_filter_map)[edge]) {
589 edge = Parent::findEdge(source, target, edge);
594 template <typename _Value>
596 : public SubMapExtender<Adaptor,
597 typename Parent::template NodeMap<_Value> >
600 typedef Adaptor Graph;
601 typedef SubMapExtender<Adaptor, typename Parent::
602 template NodeMap<_Value> > Parent;
604 NodeMap(const Graph& graph)
606 NodeMap(const Graph& graph, const _Value& value)
607 : Parent(graph, value) {}
609 NodeMap& operator=(const NodeMap& cmap) {
610 return operator=<NodeMap>(cmap);
613 template <typename CMap>
614 NodeMap& operator=(const CMap& cmap) {
615 Parent::operator=(cmap);
620 template <typename _Value>
622 : public SubMapExtender<Adaptor,
623 typename Parent::template EdgeMap<_Value> >
626 typedef Adaptor Graph;
627 typedef SubMapExtender<Adaptor, typename Parent::
628 template EdgeMap<_Value> > Parent;
630 EdgeMap(const Graph& graph)
632 EdgeMap(const Graph& graph, const _Value& value)
633 : Parent(graph, value) {}
635 EdgeMap& operator=(const EdgeMap& cmap) {
636 return operator=<EdgeMap>(cmap);
639 template <typename CMap>
640 EdgeMap& operator=(const CMap& cmap) {
641 Parent::operator=(cmap);
648 /// \brief A graph adaptor for hiding nodes and edges from a graph.
649 /// \ingroup graph_adaptors
651 /// \warning Graph adaptors are in even more experimental state than the
652 /// other parts of the lib. Use them at you own risk.
654 /// SubGraphAdaptor shows the graph with filtered node-set and
655 /// edge-set. If the \c checked parameter is true then it filters the edgeset
656 /// to do not get invalid edges without source or target.
657 /// Let \f$ G=(V, A) \f$ be a directed graph
658 /// and suppose that the graph instance \c g of type ListGraph
659 /// implements \f$ G \f$.
660 /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
661 /// on the node-set and edge-set.
662 /// SubGraphAdaptor<...>::NodeIt iterates
663 /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and
664 /// SubGraphAdaptor<...>::EdgeIt iterates
665 /// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly,
666 /// SubGraphAdaptor<...>::OutEdgeIt and
667 /// SubGraphAdaptor<...>::InEdgeIt iterates
668 /// only on edges leaving and entering a specific node which have true value.
670 /// If the \c checked template parameter is false then we have to note that
671 /// the node-iterator cares only the filter on the node-set, and the
672 /// edge-iterator cares only the filter on the edge-set.
673 /// This way the edge-map
674 /// should filter all edges which's source or target is filtered by the
677 /// typedef ListGraph Graph;
679 /// typedef Graph::Node Node;
680 /// typedef Graph::Edge Edge;
681 /// Node u=g.addNode(); //node of id 0
682 /// Node v=g.addNode(); //node of id 1
683 /// Node e=g.addEdge(u, v); //edge of id 0
684 /// Node f=g.addEdge(v, u); //edge of id 1
685 /// Graph::NodeMap<bool> nm(g, true);
686 /// nm.set(u, false);
687 /// Graph::EdgeMap<bool> em(g, true);
688 /// em.set(e, false);
689 /// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGA;
690 /// SubGA ga(g, nm, em);
691 /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
692 /// std::cout << ":-)" << std::endl;
693 /// for (SubGA::EdgeIt e(ga); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
695 /// The output of the above code is the following.
701 /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to
702 /// \c Graph::Node that is why \c g.id(n) can be applied.
704 /// For other examples see also the documentation of NodeSubGraphAdaptor and
705 /// EdgeSubGraphAdaptor.
707 /// \author Marton Makai
709 template<typename _Graph, typename NodeFilterMap,
710 typename EdgeFilterMap, bool checked = true>
711 class SubGraphAdaptor :
712 public GraphAdaptorExtender<
713 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
715 typedef _Graph Graph;
716 typedef GraphAdaptorExtender< SubGraphAdaptorBase<_Graph, NodeFilterMap,
717 EdgeFilterMap, checked> >
721 SubGraphAdaptor() { }
724 SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
725 EdgeFilterMap& _edge_filter_map) {
727 setNodeFilterMap(_node_filter_map);
728 setEdgeFilterMap(_edge_filter_map);
733 /// \brief Just gives back a sub graph adaptor
735 /// Just gives back a sub graph adaptor
736 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
737 SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
738 subGraphAdaptor(const Graph& graph,
739 NodeFilterMap& nfm, EdgeFilterMap& efm) {
740 return SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
744 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
745 SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
746 subGraphAdaptor(const Graph& graph,
747 NodeFilterMap& nfm, EdgeFilterMap& efm) {
748 return SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
752 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
753 SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
754 subGraphAdaptor(const Graph& graph,
755 NodeFilterMap& nfm, EdgeFilterMap& efm) {
756 return SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
760 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
761 SubGraphAdaptor<const Graph, const NodeFilterMap, const EdgeFilterMap>
762 subGraphAdaptor(const Graph& graph,
763 NodeFilterMap& nfm, EdgeFilterMap& efm) {
764 return SubGraphAdaptor<const Graph, const NodeFilterMap,
765 const EdgeFilterMap>(graph, nfm, efm);
770 ///\brief An adaptor for hiding nodes from a graph.
771 ///\ingroup graph_adaptors
773 ///\warning Graph adaptors are in even more experimental state
775 ///parts of the lib. Use them at you own risk.
777 ///An adaptor for hiding nodes from a graph.
778 ///This adaptor specializes SubGraphAdaptor in the way that only
780 ///can be filtered. In usual case the checked parameter is true, we get the
781 ///induced subgraph. But if the checked parameter is false then we can only
782 ///filter only isolated nodes.
783 ///\author Marton Makai
784 template<typename Graph, typename NodeFilterMap, bool checked = true>
785 class NodeSubGraphAdaptor :
786 public SubGraphAdaptor<Graph, NodeFilterMap,
787 ConstMap<typename Graph::Edge,bool>, checked> {
790 typedef SubGraphAdaptor<Graph, NodeFilterMap,
791 ConstMap<typename Graph::Edge,bool>, checked >
795 ConstMap<typename Graph::Edge, bool> const_true_map;
797 NodeSubGraphAdaptor() : const_true_map(true) {
798 Parent::setEdgeFilterMap(const_true_map);
803 NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
804 Parent(), const_true_map(true) {
805 Parent::setGraph(_graph);
806 Parent::setNodeFilterMap(_node_filter_map);
807 Parent::setEdgeFilterMap(const_true_map);
813 /// \brief Just gives back a node sub graph adaptor
815 /// Just gives back a node sub graph adaptor
816 template<typename Graph, typename NodeFilterMap>
817 NodeSubGraphAdaptor<const Graph, NodeFilterMap>
818 nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) {
819 return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm);
822 template<typename Graph, typename NodeFilterMap>
823 NodeSubGraphAdaptor<const Graph, const NodeFilterMap>
824 nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) {
825 return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm);
828 ///\brief An adaptor for hiding edges from a graph.
830 ///\warning Graph adaptors are in even more experimental state
831 ///than the other parts of the lib. Use them at you own risk.
833 ///An adaptor for hiding edges from a graph.
834 ///This adaptor specializes SubGraphAdaptor in the way that
836 ///can be filtered. The usefulness of this adaptor is demonstrated in the
837 ///problem of searching a maximum number of edge-disjoint shortest paths
839 ///two nodes \c s and \c t. Shortest here means being shortest w.r.t.
840 ///non-negative edge-lengths. Note that
841 ///the comprehension of the presented solution
842 ///need's some elementary knowledge from combinatorial optimization.
844 ///If a single shortest path is to be
845 ///searched between \c s and \c t, then this can be done easily by
846 ///applying the Dijkstra algorithm. What happens, if a maximum number of
847 ///edge-disjoint shortest paths is to be computed. It can be proved that an
848 ///edge can be in a shortest path if and only
849 ///if it is tight with respect to
850 ///the potential function computed by Dijkstra.
851 ///Moreover, any path containing
852 ///only such edges is a shortest one.
853 ///Thus we have to compute a maximum number
854 ///of edge-disjoint paths between \c s and \c t in
855 ///the graph which has edge-set
856 ///all the tight edges. The computation will be demonstrated
858 ///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim.
859 ///The full source code is available in \ref sub_graph_adaptor_demo.cc.
860 ///If you are interested in more demo programs, you can use
861 ///\ref dim_to_dot.cc to generate .dot files from dimacs files.
862 ///The .dot file of the following figure was generated by
863 ///the demo program \ref dim_to_dot.cc.
866 ///digraph lemon_dot_example {
867 ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
868 ///n0 [ label="0 (s)" ];
874 ///n6 [ label="6 (t)" ];
875 ///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
876 ///n5 -> n6 [ label="9, length:4" ];
877 ///n4 -> n6 [ label="8, length:2" ];
878 ///n3 -> n5 [ label="7, length:1" ];
879 ///n2 -> n5 [ label="6, length:3" ];
880 ///n2 -> n6 [ label="5, length:5" ];
881 ///n2 -> n4 [ label="4, length:2" ];
882 ///n1 -> n4 [ label="3, length:3" ];
883 ///n0 -> n3 [ label="2, length:1" ];
884 ///n0 -> n2 [ label="1, length:2" ];
885 ///n0 -> n1 [ label="0, length:3" ];
892 ///LengthMap length(g);
894 ///readDimacs(std::cin, g, length, s, t);
896 ///cout << "edges with lengths (of form id, source--length->target): " << endl;
897 ///for(EdgeIt e(g); e!=INVALID; ++e)
898 /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
899 /// << length[e] << "->" << g.id(g.target(e)) << endl;
901 ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
903 ///Next, the potential function is computed with Dijkstra.
905 ///typedef Dijkstra<Graph, LengthMap> Dijkstra;
906 ///Dijkstra dijkstra(g, length);
909 ///Next, we consrtruct a map which filters the edge-set to the tight edges.
911 ///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
913 ///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
915 ///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGA;
916 ///SubGA ga(g, tight_edge_filter);
918 ///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
919 ///with a max flow algorithm Preflow.
921 ///ConstMap<Edge, int> const_1_map(1);
922 ///Graph::EdgeMap<int> flow(g, 0);
924 ///Preflow<SubGA, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
925 /// preflow(ga, s, t, const_1_map, flow);
928 ///Last, the output is:
930 ///cout << "maximum number of edge-disjoint shortest path: "
931 /// << preflow.flowValue() << endl;
932 ///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
934 ///for(EdgeIt e(g); e!=INVALID; ++e)
936 /// cout << " " << g.id(g.source(e)) << "--"
937 /// << length[e] << "->" << g.id(g.target(e)) << endl;
939 ///The program has the following (expected :-)) output:
941 ///edges with lengths (of form id, source--length->target):
953 ///maximum number of edge-disjoint shortest path: 2
954 ///edges of the maximum number of edge-disjoint shortest s-t paths:
963 ///\author Marton Makai
964 template<typename Graph, typename EdgeFilterMap>
965 class EdgeSubGraphAdaptor :
966 public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
967 EdgeFilterMap, false> {
969 typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
970 EdgeFilterMap, false> Parent;
972 ConstMap<typename Graph::Node, bool> const_true_map;
974 EdgeSubGraphAdaptor() : const_true_map(true) {
975 Parent::setNodeFilterMap(const_true_map);
980 EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
981 Parent(), const_true_map(true) {
982 Parent::setGraph(_graph);
983 Parent::setNodeFilterMap(const_true_map);
984 Parent::setEdgeFilterMap(_edge_filter_map);
989 /// \brief Just gives back an edge sub graph adaptor
991 /// Just gives back an edge sub graph adaptor
992 template<typename Graph, typename EdgeFilterMap>
993 EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>
994 edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) {
995 return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm);
998 template<typename Graph, typename EdgeFilterMap>
999 EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>
1000 edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) {
1001 return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm);
1004 template <typename _Graph, typename Enable = void>
1005 class UndirGraphAdaptorBase :
1006 public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
1008 typedef _Graph Graph;
1009 typedef UndirGraphAdaptorBase Adaptor;
1010 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
1014 UndirGraphAdaptorBase() : Parent() {}
1018 typedef typename Parent::UEdge UEdge;
1019 typedef typename Parent::Edge Edge;
1023 template <typename _Value>
1027 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1031 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1033 typedef _Value Value;
1036 EdgeMapBase(const Adaptor& adaptor) :
1037 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1039 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1040 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1042 void set(const Edge& e, const Value& a) {
1043 if (Parent::direction(e)) {
1044 forward_map.set(e, a);
1046 backward_map.set(e, a);
1050 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1051 if (Parent::direction(e)) {
1052 return forward_map[e];
1054 return backward_map[e];
1058 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1059 if (Parent::direction(e)) {
1060 return forward_map[e];
1062 return backward_map[e];
1068 MapImpl forward_map, backward_map;
1074 template <typename _Value>
1076 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1079 typedef Adaptor Graph;
1080 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1082 EdgeMap(const Graph& graph)
1084 EdgeMap(const Graph& graph, const _Value& value)
1085 : Parent(graph, value) {}
1087 EdgeMap& operator=(const EdgeMap& cmap) {
1088 return operator=<EdgeMap>(cmap);
1091 template <typename CMap>
1092 EdgeMap& operator=(const CMap& cmap) {
1093 Parent::operator=(cmap);
1098 template <typename _Value>
1099 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1102 typedef typename Graph::template EdgeMap<_Value> Parent;
1104 explicit UEdgeMap(const Adaptor& ga)
1105 : Parent(*ga.graph) {}
1107 UEdgeMap(const Adaptor& ga, const _Value& value)
1108 : Parent(*ga.graph, value) {}
1110 UEdgeMap& operator=(const UEdgeMap& cmap) {
1111 return operator=<UEdgeMap>(cmap);
1114 template <typename CMap>
1115 UEdgeMap& operator=(const CMap& cmap) {
1116 Parent::operator=(cmap);
1124 template <typename _Graph>
1125 class UndirGraphAdaptorBase<
1126 _Graph, typename enable_if<
1127 typename _Graph::EdgeNotifier::Notifier>::type >
1128 : public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
1131 typedef _Graph Graph;
1132 typedef UndirGraphAdaptorBase Adaptor;
1133 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
1137 UndirGraphAdaptorBase()
1138 : edge_notifier(*this), edge_notifier_proxy(edge_notifier) {}
1140 void setGraph(_Graph& graph) {
1141 Parent::setGraph(graph);
1142 edge_notifier_proxy.setUEdgeNotifier(graph.getNotifier(UEdge()));
1147 ~UndirGraphAdaptorBase() {
1148 edge_notifier.clear();
1151 int maxId(typename Parent::Edge) const {
1152 return Parent::maxEdgeId();
1156 typedef typename Parent::UEdge UEdge;
1157 typedef typename Parent::Edge Edge;
1159 typedef typename Parent::EdgeNotifier UEdgeNotifier;
1161 using Parent::getNotifier;
1163 typedef AlterationNotifier<UndirGraphAdaptorBase, Edge> EdgeNotifier;
1164 EdgeNotifier& getNotifier(Edge) const { return edge_notifier; }
1168 class NotifierProxy : public UEdgeNotifier::ObserverBase {
1171 typedef typename UEdgeNotifier::ObserverBase Parent;
1172 typedef UndirGraphAdaptorBase AdaptorBase;
1174 NotifierProxy(EdgeNotifier& _edge_notifier)
1175 : Parent(), edge_notifier(_edge_notifier) {
1178 virtual ~NotifierProxy() {
1179 if (Parent::attached()) {
1184 void setUEdgeNotifier(UEdgeNotifier& _uedge_notifier) {
1185 Parent::attach(_uedge_notifier);
1191 virtual void add(const UEdge& uedge) {
1192 std::vector<Edge> edges;
1193 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1194 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1195 edge_notifier.add(edges);
1197 virtual void add(const std::vector<UEdge>& uedges) {
1198 std::vector<Edge> edges;
1199 for (int i = 0; i < (int)uedges.size(); ++i) {
1200 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1201 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1203 edge_notifier.add(edges);
1205 virtual void erase(const UEdge& uedge) {
1206 std::vector<Edge> edges;
1207 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1208 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1209 edge_notifier.erase(edges);
1211 virtual void erase(const std::vector<UEdge>& uedges) {
1212 std::vector<Edge> edges;
1213 for (int i = 0; i < (int)uedges.size(); ++i) {
1214 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1215 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1217 edge_notifier.erase(edges);
1219 virtual void build() {
1220 edge_notifier.build();
1222 virtual void clear() {
1223 edge_notifier.clear();
1226 EdgeNotifier& edge_notifier;
1230 mutable EdgeNotifier edge_notifier;
1231 NotifierProxy edge_notifier_proxy;
1235 template <typename _Value>
1239 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1243 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1245 typedef _Value Value;
1248 EdgeMapBase(const Adaptor& adaptor) :
1249 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1251 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1252 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1254 void set(const Edge& e, const Value& a) {
1255 if (Parent::direction(e)) {
1256 forward_map.set(e, a);
1258 backward_map.set(e, a);
1262 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1263 if (Parent::direction(e)) {
1264 return forward_map[e];
1266 return backward_map[e];
1270 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1271 if (Parent::direction(e)) {
1272 return forward_map[e];
1274 return backward_map[e];
1280 MapImpl forward_map, backward_map;
1286 template <typename _Value>
1288 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1291 typedef Adaptor Graph;
1292 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1294 EdgeMap(const Graph& graph)
1296 EdgeMap(const Graph& graph, const _Value& value)
1297 : Parent(graph, value) {}
1299 EdgeMap& operator=(const EdgeMap& cmap) {
1300 return operator=<EdgeMap>(cmap);
1303 template <typename CMap>
1304 EdgeMap& operator=(const CMap& cmap) {
1305 Parent::operator=(cmap);
1310 template <typename _Value>
1311 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1314 typedef typename Graph::template EdgeMap<_Value> Parent;
1316 explicit UEdgeMap(const Adaptor& ga)
1317 : Parent(*ga.graph) {}
1319 UEdgeMap(const Adaptor& ga, const _Value& value)
1320 : Parent(*ga.graph, value) {}
1322 UEdgeMap& operator=(const UEdgeMap& cmap) {
1323 return operator=<UEdgeMap>(cmap);
1326 template <typename CMap>
1327 UEdgeMap& operator=(const CMap& cmap) {
1328 Parent::operator=(cmap);
1336 ///\brief An undirected graph is made from a directed graph by an adaptor
1337 ///\ingroup graph_adaptors
1339 /// Undocumented, untested!!!
1340 /// If somebody knows nice demo application, let's polulate it.
1342 /// \author Marton Makai
1343 template<typename _Graph>
1344 class UndirGraphAdaptor :
1345 public UGraphAdaptorExtender<
1346 UndirGraphAdaptorBase<_Graph> > {
1348 typedef _Graph Graph;
1349 typedef UGraphAdaptorExtender<
1350 UndirGraphAdaptorBase<_Graph> > Parent;
1352 UndirGraphAdaptor() { }
1354 UndirGraphAdaptor(_Graph& _graph) {
1358 template <typename _ForwardMap, typename _BackwardMap>
1359 class CombinedEdgeMap {
1362 typedef _ForwardMap ForwardMap;
1363 typedef _BackwardMap BackwardMap;
1365 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
1367 typedef typename ForwardMap::Value Value;
1368 typedef typename Parent::Edge Key;
1370 CombinedEdgeMap() : forward_map(0), backward_map(0) {}
1372 CombinedEdgeMap(ForwardMap& _forward_map, BackwardMap& _backward_map)
1373 : forward_map(&_forward_map), backward_map(&_backward_map) {}
1375 void set(const Key& e, const Value& a) {
1376 if (Parent::direction(e)) {
1377 forward_map->set(e, a);
1379 backward_map->set(e, a);
1383 typename MapTraits<ForwardMap>::ConstReturnValue
1384 operator[](const Key& e) const {
1385 if (Parent::direction(e)) {
1386 return (*forward_map)[e];
1388 return (*backward_map)[e];
1392 typename MapTraits<ForwardMap>::ReturnValue
1393 operator[](const Key& e) {
1394 if (Parent::direction(e)) {
1395 return (*forward_map)[e];
1397 return (*backward_map)[e];
1401 void setForwardMap(ForwardMap& _forward_map) {
1402 forward_map = &_forward_map;
1405 void setBackwardMap(BackwardMap& _backward_map) {
1406 backward_map = &_backward_map;
1411 ForwardMap* forward_map;
1412 BackwardMap* backward_map;
1418 /// \brief Just gives back an undir graph adaptor
1420 /// Just gives back an undir graph adaptor
1421 template<typename Graph>
1422 UndirGraphAdaptor<const Graph>
1423 undirGraphAdaptor(const Graph& graph) {
1424 return UndirGraphAdaptor<const Graph>(graph);
1427 template<typename Graph, typename Number,
1428 typename CapacityMap, typename FlowMap,
1429 typename Tolerance = Tolerance<Number> >
1430 class ResForwardFilter {
1431 const CapacityMap* capacity;
1432 const FlowMap* flow;
1433 Tolerance tolerance;
1435 typedef typename Graph::Edge Key;
1438 ResForwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
1439 const Tolerance& _tolerance = Tolerance())
1440 : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
1442 ResForwardFilter(const Tolerance& _tolerance)
1443 : capacity(0), flow(0), tolerance(_tolerance) { }
1445 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1446 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1448 bool operator[](const typename Graph::Edge& e) const {
1449 return tolerance.less((*flow)[e], (*capacity)[e]);
1453 template<typename Graph, typename Number,
1454 typename CapacityMap, typename FlowMap,
1455 typename Tolerance = Tolerance<Number> >
1456 class ResBackwardFilter {
1457 const CapacityMap* capacity;
1458 const FlowMap* flow;
1459 Tolerance tolerance;
1461 typedef typename Graph::Edge Key;
1464 ResBackwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
1465 const Tolerance& _tolerance = Tolerance())
1466 : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
1467 ResBackwardFilter(const Tolerance& _tolerance = Tolerance())
1468 : capacity(0), flow(0), tolerance(_tolerance) { }
1469 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1470 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1471 bool operator[](const typename Graph::Edge& e) const {
1472 return tolerance.less(0, Number((*flow)[e]));
1477 ///\brief An adaptor for composing the residual
1478 ///graph for directed flow and circulation problems.
1480 ///\ingroup graph_adaptors
1482 ///An adaptor for composing the residual graph for
1483 ///directed flow and circulation problems.
1484 // ///Let \f$ G=(V, A) \f$ be a directed graph and let \f$ F \f$ be a
1485 // ///number type. Let moreover
1486 // ///\f$ f,c:A\to F \f$, be functions on the edge-set.
1487 // ///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands for a
1488 // ///flow and \f$ c \f$ for a capacity function.
1489 // ///Suppose that a graph instange \c g of type
1490 // ///\c ListGraph implements \f$ G \f$.
1494 // ///Then RevGraphAdaptor implements the graph structure with node-set
1495 // ///\f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$, where
1496 // ///\f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
1497 // ///\f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$,
1498 // ///i.e. the so called residual graph.
1499 // ///When we take the union \f$ A_{forward}\cup A_{backward} \f$,
1500 // ///multilicities are counted, i.e. if an edge is in both
1501 // ///\f$ A_{forward} \f$ and \f$ A_{backward} \f$, then in the adaptor it
1502 // ///appears twice. The following code shows how such an instance can be
1505 // /// typedef ListGraph Graph;
1506 // /// Graph::EdgeMap<int> f(g);
1507 // /// Graph::EdgeMap<int> c(g);
1508 // /// ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > ga(g);
1510 // ///\author Marton Makai
1512 template<typename Graph, typename Number,
1513 typename CapacityMap, typename FlowMap,
1514 typename Tolerance = Tolerance<Number> >
1515 class ResGraphAdaptor :
1516 public EdgeSubGraphAdaptor<
1517 UndirGraphAdaptor<const Graph>,
1518 typename UndirGraphAdaptor<const Graph>::template CombinedEdgeMap<
1519 ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>,
1520 ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap> > > {
1523 typedef UndirGraphAdaptor<const Graph> UGraph;
1525 typedef ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>
1528 typedef ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap>
1531 typedef typename UGraph::
1532 template CombinedEdgeMap<ForwardFilter, BackwardFilter>
1535 typedef EdgeSubGraphAdaptor<UGraph, EdgeFilter> Parent;
1539 const CapacityMap* capacity;
1543 ForwardFilter forward_filter;
1544 BackwardFilter backward_filter;
1545 EdgeFilter edge_filter;
1547 void setCapacityMap(const CapacityMap& _capacity) {
1548 capacity=&_capacity;
1549 forward_filter.setCapacity(_capacity);
1550 backward_filter.setCapacity(_capacity);
1553 void setFlowMap(FlowMap& _flow) {
1555 forward_filter.setFlow(_flow);
1556 backward_filter.setFlow(_flow);
1561 /// \brief Constructor of the residual graph.
1563 /// Constructor of the residual graph. The parameters are the graph type,
1564 /// the flow map, the capacity map and a tolerance object.
1565 ResGraphAdaptor(const Graph& _graph, const CapacityMap& _capacity,
1566 FlowMap& _flow, const Tolerance& _tolerance = Tolerance())
1567 : Parent(), capacity(&_capacity), flow(&_flow), ugraph(_graph),
1568 forward_filter(_capacity, _flow, _tolerance),
1569 backward_filter(_capacity, _flow, _tolerance),
1570 edge_filter(forward_filter, backward_filter)
1572 Parent::setGraph(ugraph);
1573 Parent::setEdgeFilterMap(edge_filter);
1576 typedef typename Parent::Edge Edge;
1578 /// \brief Gives back the residual capacity of the edge.
1580 /// Gives back the residual capacity of the edge.
1581 Number rescap(const Edge& edge) const {
1582 if (UGraph::direction(edge)) {
1583 return (*capacity)[edge]-(*flow)[edge];
1585 return (*flow)[edge];
1589 /// \brief Augment on the given edge in the residual graph.
1591 /// Augment on the given edge in the residual graph. It increase
1592 /// or decrease the flow on the original edge depend on the direction
1593 /// of the residual edge.
1594 void augment(const Edge& e, Number a) const {
1595 if (UGraph::direction(e)) {
1596 flow->set(e, (*flow)[e] + a);
1598 flow->set(e, (*flow)[e] - a);
1602 /// \brief Returns the direction of the edge.
1604 /// Returns true when the edge is same oriented as the original edge.
1605 static bool forward(const Edge& e) {
1606 return UGraph::direction(e);
1609 /// \brief Returns the direction of the edge.
1611 /// Returns true when the edge is opposite oriented as the original edge.
1612 static bool backward(const Edge& e) {
1613 return !UGraph::direction(e);
1616 /// \brief Gives back the forward oriented residual edge.
1618 /// Gives back the forward oriented residual edge.
1619 static Edge forward(const typename Graph::Edge& e) {
1620 return UGraph::direct(e, true);
1623 /// \brief Gives back the backward oriented residual edge.
1625 /// Gives back the backward oriented residual edge.
1626 static Edge backward(const typename Graph::Edge& e) {
1627 return UGraph::direct(e, false);
1630 /// \brief Residual capacity map.
1632 /// In generic residual graphs the residual capacity can be obtained
1636 const ResGraphAdaptor* res_graph;
1638 typedef Number Value;
1640 ResCap(const ResGraphAdaptor& _res_graph)
1641 : res_graph(&_res_graph) {}
1643 Number operator[](const Edge& e) const {
1644 return res_graph->rescap(e);
1653 template <typename _Graph, typename FirstOutEdgesMap>
1654 class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
1656 typedef _Graph Graph;
1657 typedef GraphAdaptorBase<_Graph> Parent;
1659 FirstOutEdgesMap* first_out_edges;
1660 ErasingFirstGraphAdaptorBase() : Parent(),
1661 first_out_edges(0) { }
1663 void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
1664 first_out_edges=&_first_out_edges;
1669 typedef typename Parent::Node Node;
1670 typedef typename Parent::Edge Edge;
1672 void firstOut(Edge& i, const Node& n) const {
1673 i=(*first_out_edges)[n];
1676 void erase(const Edge& e) const {
1680 first_out_edges->set(n, f);
1685 ///\brief For blocking flows.
1686 ///\ingroup graph_adaptors
1688 ///\warning Graph adaptors are in even more
1689 ///experimental state than the other
1690 ///parts of the lib. Use them at you own risk.
1692 ///This graph adaptor is used for on-the-fly
1693 ///Dinits blocking flow computations.
1694 ///For each node, an out-edge is stored which is used when the
1696 ///OutEdgeIt& first(OutEdgeIt&, const Node&)
1700 ///\author Marton Makai
1702 template <typename _Graph, typename FirstOutEdgesMap>
1703 class ErasingFirstGraphAdaptor :
1704 public GraphAdaptorExtender<
1705 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
1707 typedef _Graph Graph;
1708 typedef GraphAdaptorExtender<
1709 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
1710 ErasingFirstGraphAdaptor(Graph& _graph,
1711 FirstOutEdgesMap& _first_out_edges) {
1713 setFirstOutEdgesMap(_first_out_edges);
1718 // template <typename _Graph>
1719 // class SplitGraphAdaptorBase
1720 // : public GraphAdaptorBase<_Graph> {
1722 // typedef GraphAdaptorBase<_Graph> Parent;
1726 // template <typename T> class NodeMap;
1727 // template <typename T> class EdgeMap;
1730 // class Node : public Parent::Node {
1731 // friend class SplitGraphAdaptorBase;
1732 // template <typename T> friend class NodeMap;
1733 // typedef typename Parent::Node NodeParent;
1737 // Node(typename Parent::Node _node, bool _entry)
1738 // : Parent::Node(_node), entry(_entry) {}
1742 // Node(Invalid) : NodeParent(INVALID), entry(true) {}
1744 // bool operator==(const Node& node) const {
1745 // return NodeParent::operator==(node) && entry == node.entry;
1748 // bool operator!=(const Node& node) const {
1749 // return !(*this == node);
1752 // bool operator<(const Node& node) const {
1753 // return NodeParent::operator<(node) ||
1754 // (NodeParent::operator==(node) && entry < node.entry);
1758 // /// \todo May we want VARIANT/union type
1759 // class Edge : public Parent::Edge {
1760 // friend class SplitGraphAdaptorBase;
1761 // template <typename T> friend class EdgeMap;
1763 // typedef typename Parent::Edge EdgeParent;
1764 // typedef typename Parent::Node NodeParent;
1767 // Edge(const EdgeParent& edge, const NodeParent& node)
1768 // : EdgeParent(edge), bind(node) {}
1771 // Edge(Invalid) : EdgeParent(INVALID), bind(INVALID) {}
1773 // bool operator==(const Edge& edge) const {
1774 // return EdgeParent::operator==(edge) && bind == edge.bind;
1777 // bool operator!=(const Edge& edge) const {
1778 // return !(*this == edge);
1781 // bool operator<(const Edge& edge) const {
1782 // return EdgeParent::operator<(edge) ||
1783 // (EdgeParent::operator==(edge) && bind < edge.bind);
1787 // void first(Node& node) const {
1788 // Parent::first(node);
1789 // node.entry = true;
1792 // void next(Node& node) const {
1793 // if (node.entry) {
1794 // node.entry = false;
1796 // node.entry = true;
1797 // Parent::next(node);
1801 // void first(Edge& edge) const {
1802 // Parent::first(edge);
1803 // if ((typename Parent::Edge&)edge == INVALID) {
1804 // Parent::first(edge.bind);
1806 // edge.bind = INVALID;
1810 // void next(Edge& edge) const {
1811 // if ((typename Parent::Edge&)edge != INVALID) {
1812 // Parent::next(edge);
1813 // if ((typename Parent::Edge&)edge == INVALID) {
1814 // Parent::first(edge.bind);
1817 // Parent::next(edge.bind);
1821 // void firstIn(Edge& edge, const Node& node) const {
1822 // if (node.entry) {
1823 // Parent::firstIn(edge, node);
1824 // edge.bind = INVALID;
1826 // (typename Parent::Edge&)edge = INVALID;
1827 // edge.bind = node;
1831 // void nextIn(Edge& edge) const {
1832 // if ((typename Parent::Edge&)edge != INVALID) {
1833 // Parent::nextIn(edge);
1835 // edge.bind = INVALID;
1839 // void firstOut(Edge& edge, const Node& node) const {
1840 // if (!node.entry) {
1841 // Parent::firstOut(edge, node);
1842 // edge.bind = INVALID;
1844 // (typename Parent::Edge&)edge = INVALID;
1845 // edge.bind = node;
1849 // void nextOut(Edge& edge) const {
1850 // if ((typename Parent::Edge&)edge != INVALID) {
1851 // Parent::nextOut(edge);
1853 // edge.bind = INVALID;
1857 // Node source(const Edge& edge) const {
1858 // if ((typename Parent::Edge&)edge != INVALID) {
1859 // return Node(Parent::source(edge), false);
1861 // return Node(edge.bind, true);
1865 // Node target(const Edge& edge) const {
1866 // if ((typename Parent::Edge&)edge != INVALID) {
1867 // return Node(Parent::target(edge), true);
1869 // return Node(edge.bind, false);
1873 // static bool entryNode(const Node& node) {
1874 // return node.entry;
1877 // static bool exitNode(const Node& node) {
1878 // return !node.entry;
1881 // static Node getEntry(const typename Parent::Node& node) {
1882 // return Node(node, true);
1885 // static Node getExit(const typename Parent::Node& node) {
1886 // return Node(node, false);
1889 // static bool originalEdge(const Edge& edge) {
1890 // return (typename Parent::Edge&)edge != INVALID;
1893 // static bool bindingEdge(const Edge& edge) {
1894 // return edge.bind != INVALID;
1897 // static Node getBindedNode(const Edge& edge) {
1898 // return edge.bind;
1901 // int nodeNum() const {
1902 // return Parent::nodeNum() * 2;
1905 // typedef True EdgeNumTag;
1907 // int edgeNum() const {
1908 // return countEdges() + Parent::nodeNum();
1911 // Edge findEdge(const Node& source, const Node& target,
1912 // const Edge& prev = INVALID) const {
1913 // if (exitNode(source) && entryNode(target)) {
1914 // return Parent::findEdge(source, target, prev);
1916 // if (prev == INVALID && entryNode(source) && exitNode(target) &&
1917 // (typename Parent::Node&)source == (typename Parent::Node&)target) {
1918 // return Edge(INVALID, source);
1925 // template <typename T>
1926 // class NodeMap : public MapBase<Node, T> {
1927 // typedef typename Parent::template NodeMap<T> NodeImpl;
1929 // NodeMap(const SplitGraphAdaptorBase& _graph)
1930 // : entry(_graph), exit(_graph) {}
1931 // NodeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1932 // : entry(_graph, t), exit(_graph, t) {}
1934 // void set(const Node& key, const T& val) {
1935 // if (key.entry) { entry.set(key, val); }
1936 // else {exit.set(key, val); }
1939 // typename MapTraits<NodeImpl>::ReturnValue
1940 // operator[](const Node& key) {
1941 // if (key.entry) { return entry[key]; }
1942 // else { return exit[key]; }
1945 // typename MapTraits<NodeImpl>::ConstReturnValue
1946 // operator[](const Node& key) const {
1947 // if (key.entry) { return entry[key]; }
1948 // else { return exit[key]; }
1952 // NodeImpl entry, exit;
1955 // template <typename T>
1956 // class EdgeMap : public MapBase<Edge, T> {
1957 // typedef typename Parent::template NodeMap<T> NodeImpl;
1958 // typedef typename Parent::template EdgeMap<T> EdgeImpl;
1960 // EdgeMap(const SplitGraphAdaptorBase& _graph)
1961 // : bind(_graph), orig(_graph) {}
1962 // EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1963 // : bind(_graph, t), orig(_graph, t) {}
1965 // void set(const Edge& key, const T& val) {
1966 // if ((typename Parent::Edge&)key != INVALID) { orig.set(key, val); }
1967 // else {bind.set(key.bind, val); }
1970 // typename MapTraits<EdgeImpl>::ReturnValue
1971 // operator[](const Edge& key) {
1972 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1973 // else {return bind[key.bind]; }
1976 // typename MapTraits<EdgeImpl>::ConstReturnValue
1977 // operator[](const Edge& key) const {
1978 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1979 // else {return bind[key.bind]; }
1983 // typename Parent::template NodeMap<T> bind;
1984 // typename Parent::template EdgeMap<T> orig;
1987 // template <typename EntryMap, typename ExitMap>
1988 // class CombinedNodeMap : public MapBase<Node, typename EntryMap::Value> {
1990 // typedef MapBase<Node, typename EntryMap::Value> Parent;
1992 // typedef typename Parent::Key Key;
1993 // typedef typename Parent::Value Value;
1995 // CombinedNodeMap(EntryMap& _entryMap, ExitMap& _exitMap)
1996 // : entryMap(_entryMap), exitMap(_exitMap) {}
1998 // Value& operator[](const Key& key) {
2000 // return entryMap[key];
2002 // return exitMap[key];
2006 // Value operator[](const Key& key) const {
2008 // return entryMap[key];
2010 // return exitMap[key];
2014 // void set(const Key& key, const Value& value) {
2016 // entryMap.set(key, value);
2018 // exitMap.set(key, value);
2024 // EntryMap& entryMap;
2025 // ExitMap& exitMap;
2029 // template <typename EdgeMap, typename NodeMap>
2030 // class CombinedEdgeMap : public MapBase<Edge, typename EdgeMap::Value> {
2032 // typedef MapBase<Edge, typename EdgeMap::Value> Parent;
2034 // typedef typename Parent::Key Key;
2035 // typedef typename Parent::Value Value;
2037 // CombinedEdgeMap(EdgeMap& _edgeMap, NodeMap& _nodeMap)
2038 // : edgeMap(_edgeMap), nodeMap(_nodeMap) {}
2040 // void set(const Edge& edge, const Value& val) {
2041 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2042 // edgeMap.set(edge, val);
2044 // nodeMap.set(SplitGraphAdaptorBase::bindedNode(edge), val);
2048 // Value operator[](const Key& edge) const {
2049 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2050 // return edgeMap[edge];
2052 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2056 // Value& operator[](const Key& edge) {
2057 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2058 // return edgeMap[edge];
2060 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2065 // EdgeMap& edgeMap;
2066 // NodeMap& nodeMap;
2071 // template <typename _Graph>
2072 // class SplitGraphAdaptor
2073 // : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
2075 // typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
2077 // SplitGraphAdaptor(_Graph& graph) {
2078 // Parent::setGraph(graph);
2086 #endif //LEMON_GRAPH_ADAPTOR_H