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 ///This is the base type for most of LEMON graph adaptors.
49 ///This class implements a trivial graph adaptor i.e. it only wraps the
50 ///functions and types of the graph. The purpose of this class is to
51 ///make easier implementing graph adaptors. E.g. if an adaptor is
52 ///considered which differs from the wrapped graph only in some of its
53 ///functions or types, then it can be derived from GraphAdaptor,
55 ///differences should be implemented.
57 ///author Marton Makai
58 template<typename _Graph>
59 class GraphAdaptorBase {
62 typedef GraphAdaptorBase Adaptor;
63 typedef Graph ParentGraph;
67 GraphAdaptorBase() : graph(0) { }
68 void setGraph(Graph& _graph) { graph=&_graph; }
71 GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
73 typedef typename Graph::Node Node;
74 typedef typename Graph::Edge Edge;
76 void first(Node& i) const { graph->first(i); }
77 void first(Edge& i) const { graph->first(i); }
78 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
79 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
81 void next(Node& i) const { graph->next(i); }
82 void next(Edge& i) const { graph->next(i); }
83 void nextIn(Edge& i) const { graph->nextIn(i); }
84 void nextOut(Edge& i) const { graph->nextOut(i); }
86 Node source(const Edge& e) const { return graph->source(e); }
87 Node target(const Edge& e) const { return graph->target(e); }
89 typedef NodeNumTagIndicator<Graph> NodeNumTag;
90 int nodeNum() const { return graph->nodeNum(); }
92 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
93 int edgeNum() const { return graph->edgeNum(); }
95 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
96 Edge findEdge(const Node& source, const Node& target,
97 const Edge& prev = INVALID) {
98 return graph->findEdge(source, target, prev);
101 Node addNode() const {
102 return Node(graph->addNode());
105 Edge addEdge(const Node& source, const Node& target) const {
106 return Edge(graph->addEdge(source, target));
109 void erase(const Node& i) const { graph->erase(i); }
110 void erase(const Edge& i) const { graph->erase(i); }
112 void clear() const { graph->clear(); }
114 int id(const Node& v) const { return graph->id(v); }
115 int id(const Edge& e) const { return graph->id(e); }
117 Node fromNodeId(int id) const {
118 return graph->fromNodeId(id);
121 Edge fromEdgeId(int id) const {
122 return graph->fromEdgeId(id);
125 int maxNodeId() const {
126 return graph->maxNodeId();
129 int maxEdgeId() const {
130 return graph->maxEdgeId();
133 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
135 NodeNotifier& getNotifier(Node) const {
136 return graph->getNotifier(Node());
139 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
141 EdgeNotifier& getNotifier(Edge) const {
142 return graph->getNotifier(Edge());
145 template <typename _Value>
146 class NodeMap : public Graph::template NodeMap<_Value> {
149 typedef typename Graph::template NodeMap<_Value> Parent;
151 explicit NodeMap(const Adaptor& ga)
152 : Parent(*ga.graph) {}
154 NodeMap(const Adaptor& ga, const _Value& value)
155 : Parent(*ga.graph, value) { }
157 NodeMap& operator=(const NodeMap& cmap) {
158 return operator=<NodeMap>(cmap);
161 template <typename CMap>
162 NodeMap& operator=(const CMap& cmap) {
163 Parent::operator=(cmap);
169 template <typename _Value>
170 class EdgeMap : public Graph::template EdgeMap<_Value> {
173 typedef typename Graph::template EdgeMap<_Value> Parent;
175 explicit EdgeMap(const Adaptor& ga)
176 : Parent(*ga.graph) {}
178 EdgeMap(const Adaptor& ga, const _Value& value)
179 : Parent(*ga.graph, value) {}
181 EdgeMap& operator=(const EdgeMap& cmap) {
182 return operator=<EdgeMap>(cmap);
185 template <typename CMap>
186 EdgeMap& operator=(const CMap& cmap) {
187 Parent::operator=(cmap);
195 template <typename _Graph>
197 public GraphAdaptorExtender<GraphAdaptorBase<_Graph> > {
199 typedef _Graph Graph;
200 typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent;
202 GraphAdaptor() : Parent() { }
205 explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
208 /// \brief Just gives back a graph adaptor
210 /// Just gives back a graph adaptor which
211 /// should be provide original graph
212 template<typename Graph>
213 GraphAdaptor<const Graph>
214 graphAdaptor(const Graph& graph) {
215 return GraphAdaptor<const Graph>(graph);
219 template <typename _Graph>
220 class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
222 typedef _Graph Graph;
223 typedef GraphAdaptorBase<_Graph> Parent;
225 RevGraphAdaptorBase() : Parent() { }
227 typedef typename Parent::Node Node;
228 typedef typename Parent::Edge Edge;
230 void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
231 void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
233 void nextIn(Edge& i) const { Parent::nextOut(i); }
234 void nextOut(Edge& i) const { Parent::nextIn(i); }
236 Node source(const Edge& e) const { return Parent::target(e); }
237 Node target(const Edge& e) const { return Parent::source(e); }
239 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
240 Edge findEdge(const Node& source, const Node& target,
241 const Edge& prev = INVALID) {
242 return Parent::findEdge(target, source, prev);
248 ///\brief A graph adaptor which reverses the orientation of the edges.
249 ///\ingroup graph_adaptors
251 /// If \c g is defined as
257 /// RevGraphAdaptor<ListGraph> ga(g);
259 ///implements the graph obtained from \c g by
260 /// reversing the orientation of its edges.
262 template<typename _Graph>
263 class RevGraphAdaptor :
264 public GraphAdaptorExtender<RevGraphAdaptorBase<_Graph> > {
266 typedef _Graph Graph;
267 typedef GraphAdaptorExtender<
268 RevGraphAdaptorBase<_Graph> > Parent;
270 RevGraphAdaptor() { }
272 explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
275 /// \brief Just gives back a reverse graph adaptor
277 /// Just gives back a reverse graph adaptor
278 template<typename Graph>
279 RevGraphAdaptor<const Graph>
280 revGraphAdaptor(const Graph& graph) {
281 return RevGraphAdaptor<const Graph>(graph);
284 template <typename _Graph, typename NodeFilterMap,
285 typename EdgeFilterMap, bool checked = true>
286 class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
288 typedef _Graph Graph;
289 typedef SubGraphAdaptorBase Adaptor;
290 typedef GraphAdaptorBase<_Graph> Parent;
292 NodeFilterMap* node_filter_map;
293 EdgeFilterMap* edge_filter_map;
294 SubGraphAdaptorBase() : Parent(),
295 node_filter_map(0), edge_filter_map(0) { }
297 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
298 node_filter_map=&_node_filter_map;
300 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
301 edge_filter_map=&_edge_filter_map;
306 typedef typename Parent::Node Node;
307 typedef typename Parent::Edge Edge;
309 void first(Node& i) const {
311 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
314 void first(Edge& i) const {
316 while (i!=INVALID && (!(*edge_filter_map)[i]
317 || !(*node_filter_map)[Parent::source(i)]
318 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
321 void firstIn(Edge& i, const Node& n) const {
322 Parent::firstIn(i, n);
323 while (i!=INVALID && (!(*edge_filter_map)[i]
324 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
327 void firstOut(Edge& i, const Node& n) const {
328 Parent::firstOut(i, n);
329 while (i!=INVALID && (!(*edge_filter_map)[i]
330 || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
333 void next(Node& i) const {
335 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
338 void next(Edge& i) const {
340 while (i!=INVALID && (!(*edge_filter_map)[i]
341 || !(*node_filter_map)[Parent::source(i)]
342 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
345 void nextIn(Edge& i) const {
347 while (i!=INVALID && (!(*edge_filter_map)[i]
348 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
351 void nextOut(Edge& i) const {
353 while (i!=INVALID && (!(*edge_filter_map)[i]
354 || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
359 /// This function hides \c n in the graph, i.e. the iteration
360 /// jumps over it. This is done by simply setting the value of \c n
361 /// to be false in the corresponding node-map.
362 void hide(const Node& n) const { node_filter_map->set(n, false); }
366 /// This function hides \c e in the graph, i.e. the iteration
367 /// jumps over it. This is done by simply setting the value of \c e
368 /// to be false in the corresponding edge-map.
369 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
373 /// The value of \c n is set to be true in the node-map which stores
374 /// hide information. If \c n was hidden previuosly, then it is shown
376 void unHide(const Node& n) const { node_filter_map->set(n, true); }
380 /// The value of \c e is set to be true in the edge-map which stores
381 /// hide information. If \c e was hidden previuosly, then it is shown
383 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
385 /// Returns true if \c n is hidden.
389 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
391 /// Returns true if \c n is hidden.
395 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
397 typedef False NodeNumTag;
398 typedef False EdgeNumTag;
400 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
401 Edge findEdge(const Node& source, const Node& target,
402 const Edge& prev = INVALID) {
403 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
406 Edge edge = Parent::findEdge(source, target, prev);
407 while (edge != INVALID && !(*edge_filter_map)[edge]) {
408 edge = Parent::findEdge(source, target, edge);
413 template <typename _Value>
415 : public SubMapExtender<Adaptor,
416 typename Parent::template NodeMap<_Value> >
419 typedef Adaptor Graph;
420 typedef SubMapExtender<Adaptor, typename Parent::
421 template NodeMap<_Value> > Parent;
423 NodeMap(const Graph& graph)
425 NodeMap(const Graph& graph, const _Value& value)
426 : Parent(graph, value) {}
428 NodeMap& operator=(const NodeMap& cmap) {
429 return operator=<NodeMap>(cmap);
432 template <typename CMap>
433 NodeMap& operator=(const CMap& cmap) {
434 Parent::operator=(cmap);
439 template <typename _Value>
441 : public SubMapExtender<Adaptor,
442 typename Parent::template EdgeMap<_Value> >
445 typedef Adaptor Graph;
446 typedef SubMapExtender<Adaptor, typename Parent::
447 template EdgeMap<_Value> > Parent;
449 EdgeMap(const Graph& graph)
451 EdgeMap(const Graph& graph, const _Value& value)
452 : Parent(graph, value) {}
454 EdgeMap& operator=(const EdgeMap& cmap) {
455 return operator=<EdgeMap>(cmap);
458 template <typename CMap>
459 EdgeMap& operator=(const CMap& cmap) {
460 Parent::operator=(cmap);
467 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
468 class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
469 : public GraphAdaptorBase<_Graph> {
471 typedef _Graph Graph;
472 typedef SubGraphAdaptorBase Adaptor;
473 typedef GraphAdaptorBase<_Graph> Parent;
475 NodeFilterMap* node_filter_map;
476 EdgeFilterMap* edge_filter_map;
477 SubGraphAdaptorBase() : Parent(),
478 node_filter_map(0), edge_filter_map(0) { }
480 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
481 node_filter_map=&_node_filter_map;
483 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
484 edge_filter_map=&_edge_filter_map;
489 typedef typename Parent::Node Node;
490 typedef typename Parent::Edge Edge;
492 void first(Node& i) const {
494 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
497 void first(Edge& i) const {
499 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
502 void firstIn(Edge& i, const Node& n) const {
503 Parent::firstIn(i, n);
504 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
507 void firstOut(Edge& i, const Node& n) const {
508 Parent::firstOut(i, n);
509 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
512 void next(Node& i) const {
514 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
516 void next(Edge& i) const {
518 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
520 void nextIn(Edge& i) const {
522 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
525 void nextOut(Edge& i) const {
527 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
532 /// This function hides \c n in the graph, i.e. the iteration
533 /// jumps over it. This is done by simply setting the value of \c n
534 /// to be false in the corresponding node-map.
535 void hide(const Node& n) const { node_filter_map->set(n, false); }
539 /// This function hides \c e in the graph, i.e. the iteration
540 /// jumps over it. This is done by simply setting the value of \c e
541 /// to be false in the corresponding edge-map.
542 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
546 /// The value of \c n is set to be true in the node-map which stores
547 /// hide information. If \c n was hidden previuosly, then it is shown
549 void unHide(const Node& n) const { node_filter_map->set(n, true); }
553 /// The value of \c e is set to be true in the edge-map which stores
554 /// hide information. If \c e was hidden previuosly, then it is shown
556 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
558 /// Returns true if \c n is hidden.
562 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
564 /// Returns true if \c n is hidden.
568 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
570 typedef False NodeNumTag;
571 typedef False EdgeNumTag;
573 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
574 Edge findEdge(const Node& source, const Node& target,
575 const Edge& prev = INVALID) {
576 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
579 Edge edge = Parent::findEdge(source, target, prev);
580 while (edge != INVALID && !(*edge_filter_map)[edge]) {
581 edge = Parent::findEdge(source, target, edge);
586 template <typename _Value>
588 : public SubMapExtender<Adaptor,
589 typename Parent::template NodeMap<_Value> >
592 typedef Adaptor Graph;
593 typedef SubMapExtender<Adaptor, typename Parent::
594 template NodeMap<_Value> > Parent;
596 NodeMap(const Graph& graph)
598 NodeMap(const Graph& graph, const _Value& value)
599 : Parent(graph, value) {}
601 NodeMap& operator=(const NodeMap& cmap) {
602 return operator=<NodeMap>(cmap);
605 template <typename CMap>
606 NodeMap& operator=(const CMap& cmap) {
607 Parent::operator=(cmap);
612 template <typename _Value>
614 : public SubMapExtender<Adaptor,
615 typename Parent::template EdgeMap<_Value> >
618 typedef Adaptor Graph;
619 typedef SubMapExtender<Adaptor, typename Parent::
620 template EdgeMap<_Value> > Parent;
622 EdgeMap(const Graph& graph)
624 EdgeMap(const Graph& graph, const _Value& value)
625 : Parent(graph, value) {}
627 EdgeMap& operator=(const EdgeMap& cmap) {
628 return operator=<EdgeMap>(cmap);
631 template <typename CMap>
632 EdgeMap& operator=(const CMap& cmap) {
633 Parent::operator=(cmap);
640 /// \brief A graph adaptor for hiding nodes and edges from a graph.
641 /// \ingroup graph_adaptors
643 /// SubGraphAdaptor shows the graph with filtered node-set and
644 /// edge-set. If the \c checked parameter is true then it filters the edgeset
645 /// to do not get invalid edges without source or target.
646 /// Let \f$ G=(V, A) \f$ be a directed graph
647 /// and suppose that the graph instance \c g of type ListGraph
648 /// implements \f$ G \f$.
649 /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
650 /// on the node-set and edge-set.
651 /// SubGraphAdaptor<...>::NodeIt iterates
652 /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and
653 /// SubGraphAdaptor<...>::EdgeIt iterates
654 /// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly,
655 /// SubGraphAdaptor<...>::OutEdgeIt and
656 /// SubGraphAdaptor<...>::InEdgeIt iterates
657 /// only on edges leaving and entering a specific node which have true value.
659 /// If the \c checked template parameter is false then we have to note that
660 /// the node-iterator cares only the filter on the node-set, and the
661 /// edge-iterator cares only the filter on the edge-set.
662 /// This way the edge-map
663 /// should filter all edges which's source or target is filtered by the
666 /// typedef ListGraph Graph;
668 /// typedef Graph::Node Node;
669 /// typedef Graph::Edge Edge;
670 /// Node u=g.addNode(); //node of id 0
671 /// Node v=g.addNode(); //node of id 1
672 /// Node e=g.addEdge(u, v); //edge of id 0
673 /// Node f=g.addEdge(v, u); //edge of id 1
674 /// Graph::NodeMap<bool> nm(g, true);
675 /// nm.set(u, false);
676 /// Graph::EdgeMap<bool> em(g, true);
677 /// em.set(e, false);
678 /// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGA;
679 /// SubGA ga(g, nm, em);
680 /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
681 /// std::cout << ":-)" << std::endl;
682 /// for (SubGA::EdgeIt e(ga); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
684 /// The output of the above code is the following.
690 /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to
691 /// \c Graph::Node that is why \c g.id(n) can be applied.
693 /// For other examples see also the documentation of NodeSubGraphAdaptor and
694 /// EdgeSubGraphAdaptor.
696 /// \author Marton Makai
698 template<typename _Graph, typename NodeFilterMap,
699 typename EdgeFilterMap, bool checked = true>
700 class SubGraphAdaptor :
701 public GraphAdaptorExtender<
702 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
704 typedef _Graph Graph;
705 typedef GraphAdaptorExtender< SubGraphAdaptorBase<_Graph, NodeFilterMap,
706 EdgeFilterMap, checked> >
710 SubGraphAdaptor() { }
713 SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
714 EdgeFilterMap& _edge_filter_map) {
716 setNodeFilterMap(_node_filter_map);
717 setEdgeFilterMap(_edge_filter_map);
722 /// \brief Just gives back a sub graph adaptor
724 /// Just gives back a sub graph adaptor
725 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
726 SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
727 subGraphAdaptor(const Graph& graph,
728 NodeFilterMap& nfm, EdgeFilterMap& efm) {
729 return SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
733 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
734 SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
735 subGraphAdaptor(const Graph& graph,
736 NodeFilterMap& nfm, EdgeFilterMap& efm) {
737 return SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
741 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
742 SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
743 subGraphAdaptor(const Graph& graph,
744 NodeFilterMap& nfm, EdgeFilterMap& efm) {
745 return SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
749 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
750 SubGraphAdaptor<const Graph, const NodeFilterMap, const EdgeFilterMap>
751 subGraphAdaptor(const Graph& graph,
752 NodeFilterMap& nfm, EdgeFilterMap& efm) {
753 return SubGraphAdaptor<const Graph, const NodeFilterMap,
754 const EdgeFilterMap>(graph, nfm, efm);
759 ///\brief An adaptor for hiding nodes from a graph.
760 ///\ingroup graph_adaptors
762 ///An adaptor for hiding nodes from a graph.
763 ///This adaptor specializes SubGraphAdaptor in the way that only
765 ///can be filtered. In usual case the checked parameter is true, we get the
766 ///induced subgraph. But if the checked parameter is false then we can only
767 ///filter only isolated nodes.
768 ///\author Marton Makai
769 template<typename Graph, typename NodeFilterMap, bool checked = true>
770 class NodeSubGraphAdaptor :
771 public SubGraphAdaptor<Graph, NodeFilterMap,
772 ConstMap<typename Graph::Edge,bool>, checked> {
775 typedef SubGraphAdaptor<Graph, NodeFilterMap,
776 ConstMap<typename Graph::Edge,bool>, checked >
780 ConstMap<typename Graph::Edge, bool> const_true_map;
782 NodeSubGraphAdaptor() : const_true_map(true) {
783 Parent::setEdgeFilterMap(const_true_map);
788 NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
789 Parent(), const_true_map(true) {
790 Parent::setGraph(_graph);
791 Parent::setNodeFilterMap(_node_filter_map);
792 Parent::setEdgeFilterMap(const_true_map);
798 /// \brief Just gives back a node sub graph adaptor
800 /// Just gives back a node sub graph adaptor
801 template<typename Graph, typename NodeFilterMap>
802 NodeSubGraphAdaptor<const Graph, NodeFilterMap>
803 nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) {
804 return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm);
807 template<typename Graph, typename NodeFilterMap>
808 NodeSubGraphAdaptor<const Graph, const NodeFilterMap>
809 nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) {
810 return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm);
813 ///\brief An adaptor for hiding edges from a graph.
815 ///An adaptor for hiding edges from a graph.
816 ///This adaptor specializes SubGraphAdaptor in the way that
818 ///can be filtered. The usefulness of this adaptor is demonstrated in the
819 ///problem of searching a maximum number of edge-disjoint shortest paths
821 ///two nodes \c s and \c t. Shortest here means being shortest w.r.t.
822 ///non-negative edge-lengths. Note that
823 ///the comprehension of the presented solution
824 ///need's some elementary knowledge from combinatorial optimization.
826 ///If a single shortest path is to be
827 ///searched between \c s and \c t, then this can be done easily by
828 ///applying the Dijkstra algorithm. What happens, if a maximum number of
829 ///edge-disjoint shortest paths is to be computed. It can be proved that an
830 ///edge can be in a shortest path if and only
831 ///if it is tight with respect to
832 ///the potential function computed by Dijkstra.
833 ///Moreover, any path containing
834 ///only such edges is a shortest one.
835 ///Thus we have to compute a maximum number
836 ///of edge-disjoint paths between \c s and \c t in
837 ///the graph which has edge-set
838 ///all the tight edges. The computation will be demonstrated
840 ///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim.
841 ///The full source code is available in \ref sub_graph_adaptor_demo.cc.
842 ///If you are interested in more demo programs, you can use
843 ///\ref dim_to_dot.cc to generate .dot files from dimacs files.
844 ///The .dot file of the following figure was generated by
845 ///the demo program \ref dim_to_dot.cc.
848 ///digraph lemon_dot_example {
849 ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
850 ///n0 [ label="0 (s)" ];
856 ///n6 [ label="6 (t)" ];
857 ///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
858 ///n5 -> n6 [ label="9, length:4" ];
859 ///n4 -> n6 [ label="8, length:2" ];
860 ///n3 -> n5 [ label="7, length:1" ];
861 ///n2 -> n5 [ label="6, length:3" ];
862 ///n2 -> n6 [ label="5, length:5" ];
863 ///n2 -> n4 [ label="4, length:2" ];
864 ///n1 -> n4 [ label="3, length:3" ];
865 ///n0 -> n3 [ label="2, length:1" ];
866 ///n0 -> n2 [ label="1, length:2" ];
867 ///n0 -> n1 [ label="0, length:3" ];
874 ///LengthMap length(g);
876 ///readDimacs(std::cin, g, length, s, t);
878 ///cout << "edges with lengths (of form id, source--length->target): " << endl;
879 ///for(EdgeIt e(g); e!=INVALID; ++e)
880 /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
881 /// << length[e] << "->" << g.id(g.target(e)) << endl;
883 ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
885 ///Next, the potential function is computed with Dijkstra.
887 ///typedef Dijkstra<Graph, LengthMap> Dijkstra;
888 ///Dijkstra dijkstra(g, length);
891 ///Next, we consrtruct a map which filters the edge-set to the tight edges.
893 ///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
895 ///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
897 ///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGA;
898 ///SubGA ga(g, tight_edge_filter);
900 ///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
901 ///with a max flow algorithm Preflow.
903 ///ConstMap<Edge, int> const_1_map(1);
904 ///Graph::EdgeMap<int> flow(g, 0);
906 ///Preflow<SubGA, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
907 /// preflow(ga, s, t, const_1_map, flow);
910 ///Last, the output is:
912 ///cout << "maximum number of edge-disjoint shortest path: "
913 /// << preflow.flowValue() << endl;
914 ///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
916 ///for(EdgeIt e(g); e!=INVALID; ++e)
918 /// cout << " " << g.id(g.source(e)) << "--"
919 /// << length[e] << "->" << g.id(g.target(e)) << endl;
921 ///The program has the following (expected :-)) output:
923 ///edges with lengths (of form id, source--length->target):
935 ///maximum number of edge-disjoint shortest path: 2
936 ///edges of the maximum number of edge-disjoint shortest s-t paths:
945 ///\author Marton Makai
946 template<typename Graph, typename EdgeFilterMap>
947 class EdgeSubGraphAdaptor :
948 public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
949 EdgeFilterMap, false> {
951 typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
952 EdgeFilterMap, false> Parent;
954 ConstMap<typename Graph::Node, bool> const_true_map;
956 EdgeSubGraphAdaptor() : const_true_map(true) {
957 Parent::setNodeFilterMap(const_true_map);
962 EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
963 Parent(), const_true_map(true) {
964 Parent::setGraph(_graph);
965 Parent::setNodeFilterMap(const_true_map);
966 Parent::setEdgeFilterMap(_edge_filter_map);
971 /// \brief Just gives back an edge sub graph adaptor
973 /// Just gives back an edge sub graph adaptor
974 template<typename Graph, typename EdgeFilterMap>
975 EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>
976 edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) {
977 return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm);
980 template<typename Graph, typename EdgeFilterMap>
981 EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>
982 edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) {
983 return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm);
986 template <typename _Graph, typename Enable = void>
987 class UndirGraphAdaptorBase :
988 public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
990 typedef _Graph Graph;
991 typedef UndirGraphAdaptorBase Adaptor;
992 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
996 UndirGraphAdaptorBase() : Parent() {}
1000 typedef typename Parent::UEdge UEdge;
1001 typedef typename Parent::Edge Edge;
1005 template <typename _Value>
1009 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1013 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1015 typedef _Value Value;
1018 EdgeMapBase(const Adaptor& adaptor) :
1019 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1021 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1022 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1024 void set(const Edge& e, const Value& a) {
1025 if (Parent::direction(e)) {
1026 forward_map.set(e, a);
1028 backward_map.set(e, a);
1032 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1033 if (Parent::direction(e)) {
1034 return forward_map[e];
1036 return backward_map[e];
1040 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1041 if (Parent::direction(e)) {
1042 return forward_map[e];
1044 return backward_map[e];
1050 MapImpl forward_map, backward_map;
1056 template <typename _Value>
1058 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1061 typedef Adaptor Graph;
1062 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1064 EdgeMap(const Graph& graph)
1066 EdgeMap(const Graph& graph, const _Value& value)
1067 : Parent(graph, value) {}
1069 EdgeMap& operator=(const EdgeMap& cmap) {
1070 return operator=<EdgeMap>(cmap);
1073 template <typename CMap>
1074 EdgeMap& operator=(const CMap& cmap) {
1075 Parent::operator=(cmap);
1080 template <typename _Value>
1081 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1084 typedef typename Graph::template EdgeMap<_Value> Parent;
1086 explicit UEdgeMap(const Adaptor& ga)
1087 : Parent(*ga.graph) {}
1089 UEdgeMap(const Adaptor& ga, const _Value& value)
1090 : Parent(*ga.graph, value) {}
1092 UEdgeMap& operator=(const UEdgeMap& cmap) {
1093 return operator=<UEdgeMap>(cmap);
1096 template <typename CMap>
1097 UEdgeMap& operator=(const CMap& cmap) {
1098 Parent::operator=(cmap);
1106 template <typename _Graph>
1107 class UndirGraphAdaptorBase<
1108 _Graph, typename enable_if<
1109 typename _Graph::EdgeNotifier::Notifier>::type >
1110 : public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
1113 typedef _Graph Graph;
1114 typedef UndirGraphAdaptorBase Adaptor;
1115 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
1119 UndirGraphAdaptorBase()
1120 : edge_notifier(*this), edge_notifier_proxy(edge_notifier) {}
1122 void setGraph(_Graph& graph) {
1123 Parent::setGraph(graph);
1124 edge_notifier_proxy.setUEdgeNotifier(graph.getNotifier(UEdge()));
1129 ~UndirGraphAdaptorBase() {
1130 edge_notifier.clear();
1133 int maxId(typename Parent::Edge) const {
1134 return Parent::maxEdgeId();
1138 typedef typename Parent::UEdge UEdge;
1139 typedef typename Parent::Edge Edge;
1141 typedef typename Parent::EdgeNotifier UEdgeNotifier;
1143 using Parent::getNotifier;
1145 typedef AlterationNotifier<UndirGraphAdaptorBase, Edge> EdgeNotifier;
1146 EdgeNotifier& getNotifier(Edge) const { return edge_notifier; }
1150 class NotifierProxy : public UEdgeNotifier::ObserverBase {
1153 typedef typename UEdgeNotifier::ObserverBase Parent;
1154 typedef UndirGraphAdaptorBase AdaptorBase;
1156 NotifierProxy(EdgeNotifier& _edge_notifier)
1157 : Parent(), edge_notifier(_edge_notifier) {
1160 virtual ~NotifierProxy() {
1161 if (Parent::attached()) {
1166 void setUEdgeNotifier(UEdgeNotifier& _uedge_notifier) {
1167 Parent::attach(_uedge_notifier);
1173 virtual void add(const UEdge& uedge) {
1174 std::vector<Edge> edges;
1175 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1176 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1177 edge_notifier.add(edges);
1179 virtual void add(const std::vector<UEdge>& uedges) {
1180 std::vector<Edge> edges;
1181 for (int i = 0; i < (int)uedges.size(); ++i) {
1182 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1183 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1185 edge_notifier.add(edges);
1187 virtual void erase(const UEdge& uedge) {
1188 std::vector<Edge> edges;
1189 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1190 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1191 edge_notifier.erase(edges);
1193 virtual void erase(const std::vector<UEdge>& uedges) {
1194 std::vector<Edge> edges;
1195 for (int i = 0; i < (int)uedges.size(); ++i) {
1196 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1197 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1199 edge_notifier.erase(edges);
1201 virtual void build() {
1202 edge_notifier.build();
1204 virtual void clear() {
1205 edge_notifier.clear();
1208 EdgeNotifier& edge_notifier;
1212 mutable EdgeNotifier edge_notifier;
1213 NotifierProxy edge_notifier_proxy;
1217 template <typename _Value>
1221 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1225 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1227 typedef _Value Value;
1230 EdgeMapBase(const Adaptor& adaptor) :
1231 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1233 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1234 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1236 void set(const Edge& e, const Value& a) {
1237 if (Parent::direction(e)) {
1238 forward_map.set(e, a);
1240 backward_map.set(e, a);
1244 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1245 if (Parent::direction(e)) {
1246 return forward_map[e];
1248 return backward_map[e];
1252 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1253 if (Parent::direction(e)) {
1254 return forward_map[e];
1256 return backward_map[e];
1262 MapImpl forward_map, backward_map;
1268 template <typename _Value>
1270 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1273 typedef Adaptor Graph;
1274 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1276 EdgeMap(const Graph& graph)
1278 EdgeMap(const Graph& graph, const _Value& value)
1279 : Parent(graph, value) {}
1281 EdgeMap& operator=(const EdgeMap& cmap) {
1282 return operator=<EdgeMap>(cmap);
1285 template <typename CMap>
1286 EdgeMap& operator=(const CMap& cmap) {
1287 Parent::operator=(cmap);
1292 template <typename _Value>
1293 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1296 typedef typename Graph::template EdgeMap<_Value> Parent;
1298 explicit UEdgeMap(const Adaptor& ga)
1299 : Parent(*ga.graph) {}
1301 UEdgeMap(const Adaptor& ga, const _Value& value)
1302 : Parent(*ga.graph, value) {}
1304 UEdgeMap& operator=(const UEdgeMap& cmap) {
1305 return operator=<UEdgeMap>(cmap);
1308 template <typename CMap>
1309 UEdgeMap& operator=(const CMap& cmap) {
1310 Parent::operator=(cmap);
1318 ///\brief An undirected graph is made from a directed graph by an adaptor
1319 ///\ingroup graph_adaptors
1321 /// Undocumented, untested!!!
1322 /// If somebody knows nice demo application, let's polulate it.
1324 /// \author Marton Makai
1325 template<typename _Graph>
1326 class UndirGraphAdaptor :
1327 public UGraphAdaptorExtender<
1328 UndirGraphAdaptorBase<_Graph> > {
1330 typedef _Graph Graph;
1331 typedef UGraphAdaptorExtender<
1332 UndirGraphAdaptorBase<_Graph> > Parent;
1334 UndirGraphAdaptor() { }
1336 UndirGraphAdaptor(_Graph& _graph) {
1340 template <typename _ForwardMap, typename _BackwardMap>
1341 class CombinedEdgeMap {
1344 typedef _ForwardMap ForwardMap;
1345 typedef _BackwardMap BackwardMap;
1347 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
1349 typedef typename ForwardMap::Value Value;
1350 typedef typename Parent::Edge Key;
1352 CombinedEdgeMap() : forward_map(0), backward_map(0) {}
1354 CombinedEdgeMap(ForwardMap& _forward_map, BackwardMap& _backward_map)
1355 : forward_map(&_forward_map), backward_map(&_backward_map) {}
1357 void set(const Key& e, const Value& a) {
1358 if (Parent::direction(e)) {
1359 forward_map->set(e, a);
1361 backward_map->set(e, a);
1365 typename MapTraits<ForwardMap>::ConstReturnValue
1366 operator[](const Key& e) const {
1367 if (Parent::direction(e)) {
1368 return (*forward_map)[e];
1370 return (*backward_map)[e];
1374 typename MapTraits<ForwardMap>::ReturnValue
1375 operator[](const Key& e) {
1376 if (Parent::direction(e)) {
1377 return (*forward_map)[e];
1379 return (*backward_map)[e];
1383 void setForwardMap(ForwardMap& _forward_map) {
1384 forward_map = &_forward_map;
1387 void setBackwardMap(BackwardMap& _backward_map) {
1388 backward_map = &_backward_map;
1393 ForwardMap* forward_map;
1394 BackwardMap* backward_map;
1400 /// \brief Just gives back an undir graph adaptor
1402 /// Just gives back an undir graph adaptor
1403 template<typename Graph>
1404 UndirGraphAdaptor<const Graph>
1405 undirGraphAdaptor(const Graph& graph) {
1406 return UndirGraphAdaptor<const Graph>(graph);
1409 template<typename Graph, typename Number,
1410 typename CapacityMap, typename FlowMap,
1411 typename Tolerance = Tolerance<Number> >
1412 class ResForwardFilter {
1413 const CapacityMap* capacity;
1414 const FlowMap* flow;
1415 Tolerance tolerance;
1417 typedef typename Graph::Edge Key;
1420 ResForwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
1421 const Tolerance& _tolerance = Tolerance())
1422 : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
1424 ResForwardFilter(const Tolerance& _tolerance)
1425 : capacity(0), flow(0), tolerance(_tolerance) { }
1427 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1428 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1430 bool operator[](const typename Graph::Edge& e) const {
1431 return tolerance.less((*flow)[e], (*capacity)[e]);
1435 template<typename Graph, typename Number,
1436 typename CapacityMap, typename FlowMap,
1437 typename Tolerance = Tolerance<Number> >
1438 class ResBackwardFilter {
1439 const CapacityMap* capacity;
1440 const FlowMap* flow;
1441 Tolerance tolerance;
1443 typedef typename Graph::Edge Key;
1446 ResBackwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
1447 const Tolerance& _tolerance = Tolerance())
1448 : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
1449 ResBackwardFilter(const Tolerance& _tolerance = Tolerance())
1450 : capacity(0), flow(0), tolerance(_tolerance) { }
1451 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1452 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1453 bool operator[](const typename Graph::Edge& e) const {
1454 return tolerance.less(0, Number((*flow)[e]));
1459 ///\brief An adaptor for composing the residual
1460 ///graph for directed flow and circulation problems.
1462 ///\ingroup graph_adaptors
1464 ///An adaptor for composing the residual graph for directed flow and
1465 ///circulation problems. Let \f$ G=(V, A) \f$ be a directed graph
1466 ///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$,
1467 ///be functions on the edge-set.
1469 ///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands
1470 ///for a flow and \f$ c \f$ for a capacity function. Suppose that a
1471 ///graph instange \c g of type \c ListGraph implements \f$ G \f$.
1477 ///Then RevGraphAdaptor implements the graph structure with node-set
1478 /// \f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$,
1479 ///where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
1480 /// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so called
1481 ///residual graph. When we take the union
1482 /// \f$ A_{forward}\cup A_{backward} \f$, multilicities are counted, i.e.
1483 ///if an edge is in both \f$ A_{forward} \f$ and \f$ A_{backward} \f$,
1484 ///then in the adaptor it appears twice. The following code shows how
1485 ///such an instance can be constructed.
1488 /// typedef ListGraph Graph;
1489 /// Graph::EdgeMap<int> f(g);
1490 /// Graph::EdgeMap<int> c(g);
1491 /// ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > ga(g);
1493 ///\author Marton Makai
1495 template<typename Graph, typename Number,
1496 typename CapacityMap, typename FlowMap,
1497 typename Tolerance = Tolerance<Number> >
1498 class ResGraphAdaptor :
1499 public EdgeSubGraphAdaptor<
1500 UndirGraphAdaptor<const Graph>,
1501 typename UndirGraphAdaptor<const Graph>::template CombinedEdgeMap<
1502 ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>,
1503 ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap> > > {
1506 typedef UndirGraphAdaptor<const Graph> UGraph;
1508 typedef ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>
1511 typedef ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap>
1514 typedef typename UGraph::
1515 template CombinedEdgeMap<ForwardFilter, BackwardFilter>
1518 typedef EdgeSubGraphAdaptor<UGraph, EdgeFilter> Parent;
1522 const CapacityMap* capacity;
1526 ForwardFilter forward_filter;
1527 BackwardFilter backward_filter;
1528 EdgeFilter edge_filter;
1530 void setCapacityMap(const CapacityMap& _capacity) {
1531 capacity=&_capacity;
1532 forward_filter.setCapacity(_capacity);
1533 backward_filter.setCapacity(_capacity);
1536 void setFlowMap(FlowMap& _flow) {
1538 forward_filter.setFlow(_flow);
1539 backward_filter.setFlow(_flow);
1544 /// \brief Constructor of the residual graph.
1546 /// Constructor of the residual graph. The parameters are the graph type,
1547 /// the flow map, the capacity map and a tolerance object.
1548 ResGraphAdaptor(const Graph& _graph, const CapacityMap& _capacity,
1549 FlowMap& _flow, const Tolerance& _tolerance = Tolerance())
1550 : Parent(), capacity(&_capacity), flow(&_flow), ugraph(_graph),
1551 forward_filter(_capacity, _flow, _tolerance),
1552 backward_filter(_capacity, _flow, _tolerance),
1553 edge_filter(forward_filter, backward_filter)
1555 Parent::setGraph(ugraph);
1556 Parent::setEdgeFilterMap(edge_filter);
1559 typedef typename Parent::Edge Edge;
1561 /// \brief Gives back the residual capacity of the edge.
1563 /// Gives back the residual capacity of the edge.
1564 Number rescap(const Edge& edge) const {
1565 if (UGraph::direction(edge)) {
1566 return (*capacity)[edge]-(*flow)[edge];
1568 return (*flow)[edge];
1572 /// \brief Augment on the given edge in the residual graph.
1574 /// Augment on the given edge in the residual graph. It increase
1575 /// or decrease the flow on the original edge depend on the direction
1576 /// of the residual edge.
1577 void augment(const Edge& e, Number a) const {
1578 if (UGraph::direction(e)) {
1579 flow->set(e, (*flow)[e] + a);
1581 flow->set(e, (*flow)[e] - a);
1585 /// \brief Returns the direction of the edge.
1587 /// Returns true when the edge is same oriented as the original edge.
1588 static bool forward(const Edge& e) {
1589 return UGraph::direction(e);
1592 /// \brief Returns the direction of the edge.
1594 /// Returns true when the edge is opposite oriented as the original edge.
1595 static bool backward(const Edge& e) {
1596 return !UGraph::direction(e);
1599 /// \brief Gives back the forward oriented residual edge.
1601 /// Gives back the forward oriented residual edge.
1602 static Edge forward(const typename Graph::Edge& e) {
1603 return UGraph::direct(e, true);
1606 /// \brief Gives back the backward oriented residual edge.
1608 /// Gives back the backward oriented residual edge.
1609 static Edge backward(const typename Graph::Edge& e) {
1610 return UGraph::direct(e, false);
1613 /// \brief Residual capacity map.
1615 /// In generic residual graphs the residual capacity can be obtained
1619 const ResGraphAdaptor* res_graph;
1621 typedef Number Value;
1623 ResCap(const ResGraphAdaptor& _res_graph)
1624 : res_graph(&_res_graph) {}
1626 Number operator[](const Edge& e) const {
1627 return res_graph->rescap(e);
1636 template <typename _Graph, typename FirstOutEdgesMap>
1637 class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
1639 typedef _Graph Graph;
1640 typedef GraphAdaptorBase<_Graph> Parent;
1642 FirstOutEdgesMap* first_out_edges;
1643 ErasingFirstGraphAdaptorBase() : Parent(),
1644 first_out_edges(0) { }
1646 void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
1647 first_out_edges=&_first_out_edges;
1652 typedef typename Parent::Node Node;
1653 typedef typename Parent::Edge Edge;
1655 void firstOut(Edge& i, const Node& n) const {
1656 i=(*first_out_edges)[n];
1659 void erase(const Edge& e) const {
1663 first_out_edges->set(n, f);
1668 ///\brief For blocking flows.
1669 ///\ingroup graph_adaptors
1671 ///This graph adaptor is used for on-the-fly
1672 ///Dinits blocking flow computations.
1673 ///For each node, an out-edge is stored which is used when the
1675 ///OutEdgeIt& first(OutEdgeIt&, const Node&)
1679 ///\author Marton Makai
1681 template <typename _Graph, typename FirstOutEdgesMap>
1682 class ErasingFirstGraphAdaptor :
1683 public GraphAdaptorExtender<
1684 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
1686 typedef _Graph Graph;
1687 typedef GraphAdaptorExtender<
1688 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
1689 ErasingFirstGraphAdaptor(Graph& _graph,
1690 FirstOutEdgesMap& _first_out_edges) {
1692 setFirstOutEdgesMap(_first_out_edges);
1697 // template <typename _Graph>
1698 // class SplitGraphAdaptorBase
1699 // : public GraphAdaptorBase<_Graph> {
1701 // typedef GraphAdaptorBase<_Graph> Parent;
1705 // template <typename T> class NodeMap;
1706 // template <typename T> class EdgeMap;
1709 // class Node : public Parent::Node {
1710 // friend class SplitGraphAdaptorBase;
1711 // template <typename T> friend class NodeMap;
1712 // typedef typename Parent::Node NodeParent;
1716 // Node(typename Parent::Node _node, bool _entry)
1717 // : Parent::Node(_node), entry(_entry) {}
1721 // Node(Invalid) : NodeParent(INVALID), entry(true) {}
1723 // bool operator==(const Node& node) const {
1724 // return NodeParent::operator==(node) && entry == node.entry;
1727 // bool operator!=(const Node& node) const {
1728 // return !(*this == node);
1731 // bool operator<(const Node& node) const {
1732 // return NodeParent::operator<(node) ||
1733 // (NodeParent::operator==(node) && entry < node.entry);
1737 // /// \todo May we want VARIANT/union type
1738 // class Edge : public Parent::Edge {
1739 // friend class SplitGraphAdaptorBase;
1740 // template <typename T> friend class EdgeMap;
1742 // typedef typename Parent::Edge EdgeParent;
1743 // typedef typename Parent::Node NodeParent;
1746 // Edge(const EdgeParent& edge, const NodeParent& node)
1747 // : EdgeParent(edge), bind(node) {}
1750 // Edge(Invalid) : EdgeParent(INVALID), bind(INVALID) {}
1752 // bool operator==(const Edge& edge) const {
1753 // return EdgeParent::operator==(edge) && bind == edge.bind;
1756 // bool operator!=(const Edge& edge) const {
1757 // return !(*this == edge);
1760 // bool operator<(const Edge& edge) const {
1761 // return EdgeParent::operator<(edge) ||
1762 // (EdgeParent::operator==(edge) && bind < edge.bind);
1766 // void first(Node& node) const {
1767 // Parent::first(node);
1768 // node.entry = true;
1771 // void next(Node& node) const {
1772 // if (node.entry) {
1773 // node.entry = false;
1775 // node.entry = true;
1776 // Parent::next(node);
1780 // void first(Edge& edge) const {
1781 // Parent::first(edge);
1782 // if ((typename Parent::Edge&)edge == INVALID) {
1783 // Parent::first(edge.bind);
1785 // edge.bind = INVALID;
1789 // void next(Edge& edge) const {
1790 // if ((typename Parent::Edge&)edge != INVALID) {
1791 // Parent::next(edge);
1792 // if ((typename Parent::Edge&)edge == INVALID) {
1793 // Parent::first(edge.bind);
1796 // Parent::next(edge.bind);
1800 // void firstIn(Edge& edge, const Node& node) const {
1801 // if (node.entry) {
1802 // Parent::firstIn(edge, node);
1803 // edge.bind = INVALID;
1805 // (typename Parent::Edge&)edge = INVALID;
1806 // edge.bind = node;
1810 // void nextIn(Edge& edge) const {
1811 // if ((typename Parent::Edge&)edge != INVALID) {
1812 // Parent::nextIn(edge);
1814 // edge.bind = INVALID;
1818 // void firstOut(Edge& edge, const Node& node) const {
1819 // if (!node.entry) {
1820 // Parent::firstOut(edge, node);
1821 // edge.bind = INVALID;
1823 // (typename Parent::Edge&)edge = INVALID;
1824 // edge.bind = node;
1828 // void nextOut(Edge& edge) const {
1829 // if ((typename Parent::Edge&)edge != INVALID) {
1830 // Parent::nextOut(edge);
1832 // edge.bind = INVALID;
1836 // Node source(const Edge& edge) const {
1837 // if ((typename Parent::Edge&)edge != INVALID) {
1838 // return Node(Parent::source(edge), false);
1840 // return Node(edge.bind, true);
1844 // Node target(const Edge& edge) const {
1845 // if ((typename Parent::Edge&)edge != INVALID) {
1846 // return Node(Parent::target(edge), true);
1848 // return Node(edge.bind, false);
1852 // static bool entryNode(const Node& node) {
1853 // return node.entry;
1856 // static bool exitNode(const Node& node) {
1857 // return !node.entry;
1860 // static Node getEntry(const typename Parent::Node& node) {
1861 // return Node(node, true);
1864 // static Node getExit(const typename Parent::Node& node) {
1865 // return Node(node, false);
1868 // static bool originalEdge(const Edge& edge) {
1869 // return (typename Parent::Edge&)edge != INVALID;
1872 // static bool bindingEdge(const Edge& edge) {
1873 // return edge.bind != INVALID;
1876 // static Node getBindedNode(const Edge& edge) {
1877 // return edge.bind;
1880 // int nodeNum() const {
1881 // return Parent::nodeNum() * 2;
1884 // typedef True EdgeNumTag;
1886 // int edgeNum() const {
1887 // return countEdges() + Parent::nodeNum();
1890 // Edge findEdge(const Node& source, const Node& target,
1891 // const Edge& prev = INVALID) const {
1892 // if (exitNode(source) && entryNode(target)) {
1893 // return Parent::findEdge(source, target, prev);
1895 // if (prev == INVALID && entryNode(source) && exitNode(target) &&
1896 // (typename Parent::Node&)source == (typename Parent::Node&)target) {
1897 // return Edge(INVALID, source);
1904 // template <typename T>
1905 // class NodeMap : public MapBase<Node, T> {
1906 // typedef typename Parent::template NodeMap<T> NodeImpl;
1908 // NodeMap(const SplitGraphAdaptorBase& _graph)
1909 // : entry(_graph), exit(_graph) {}
1910 // NodeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1911 // : entry(_graph, t), exit(_graph, t) {}
1913 // void set(const Node& key, const T& val) {
1914 // if (key.entry) { entry.set(key, val); }
1915 // else {exit.set(key, val); }
1918 // typename MapTraits<NodeImpl>::ReturnValue
1919 // operator[](const Node& key) {
1920 // if (key.entry) { return entry[key]; }
1921 // else { return exit[key]; }
1924 // typename MapTraits<NodeImpl>::ConstReturnValue
1925 // operator[](const Node& key) const {
1926 // if (key.entry) { return entry[key]; }
1927 // else { return exit[key]; }
1931 // NodeImpl entry, exit;
1934 // template <typename T>
1935 // class EdgeMap : public MapBase<Edge, T> {
1936 // typedef typename Parent::template NodeMap<T> NodeImpl;
1937 // typedef typename Parent::template EdgeMap<T> EdgeImpl;
1939 // EdgeMap(const SplitGraphAdaptorBase& _graph)
1940 // : bind(_graph), orig(_graph) {}
1941 // EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1942 // : bind(_graph, t), orig(_graph, t) {}
1944 // void set(const Edge& key, const T& val) {
1945 // if ((typename Parent::Edge&)key != INVALID) { orig.set(key, val); }
1946 // else {bind.set(key.bind, val); }
1949 // typename MapTraits<EdgeImpl>::ReturnValue
1950 // operator[](const Edge& key) {
1951 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1952 // else {return bind[key.bind]; }
1955 // typename MapTraits<EdgeImpl>::ConstReturnValue
1956 // operator[](const Edge& key) const {
1957 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1958 // else {return bind[key.bind]; }
1962 // typename Parent::template NodeMap<T> bind;
1963 // typename Parent::template EdgeMap<T> orig;
1966 // template <typename EntryMap, typename ExitMap>
1967 // class CombinedNodeMap : public MapBase<Node, typename EntryMap::Value> {
1969 // typedef MapBase<Node, typename EntryMap::Value> Parent;
1971 // typedef typename Parent::Key Key;
1972 // typedef typename Parent::Value Value;
1974 // CombinedNodeMap(EntryMap& _entryMap, ExitMap& _exitMap)
1975 // : entryMap(_entryMap), exitMap(_exitMap) {}
1977 // Value& operator[](const Key& key) {
1979 // return entryMap[key];
1981 // return exitMap[key];
1985 // Value operator[](const Key& key) const {
1987 // return entryMap[key];
1989 // return exitMap[key];
1993 // void set(const Key& key, const Value& value) {
1995 // entryMap.set(key, value);
1997 // exitMap.set(key, value);
2003 // EntryMap& entryMap;
2004 // ExitMap& exitMap;
2008 // template <typename EdgeMap, typename NodeMap>
2009 // class CombinedEdgeMap : public MapBase<Edge, typename EdgeMap::Value> {
2011 // typedef MapBase<Edge, typename EdgeMap::Value> Parent;
2013 // typedef typename Parent::Key Key;
2014 // typedef typename Parent::Value Value;
2016 // CombinedEdgeMap(EdgeMap& _edgeMap, NodeMap& _nodeMap)
2017 // : edgeMap(_edgeMap), nodeMap(_nodeMap) {}
2019 // void set(const Edge& edge, const Value& val) {
2020 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2021 // edgeMap.set(edge, val);
2023 // nodeMap.set(SplitGraphAdaptorBase::bindedNode(edge), val);
2027 // Value operator[](const Key& edge) const {
2028 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2029 // return edgeMap[edge];
2031 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2035 // Value& operator[](const Key& edge) {
2036 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2037 // return edgeMap[edge];
2039 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2044 // EdgeMap& edgeMap;
2045 // NodeMap& nodeMap;
2050 // template <typename _Graph>
2051 // class SplitGraphAdaptor
2052 // : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
2054 // typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
2056 // SplitGraphAdaptor(_Graph& graph) {
2057 // Parent::setGraph(graph);
2065 #endif //LEMON_GRAPH_ADAPTOR_H