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>
41 ///\brief Base type for the Graph Adaptors
42 ///\ingroup graph_adaptors
44 ///Base type for the Graph Adaptors
46 ///\warning Graph adaptors are in even
47 ///more experimental state than the other
48 ///parts of the lib. Use them at you own risk.
50 ///This is the base type for most of LEMON graph adaptors.
51 ///This class implements a trivial graph adaptor i.e. it only wraps the
52 ///functions and types of the graph. The purpose of this class is to
53 ///make easier implementing graph adaptors. E.g. if an adaptor is
54 ///considered which differs from the wrapped graph only in some of its
55 ///functions or types, then it can be derived from GraphAdaptor,
57 ///differences should be implemented.
59 ///author Marton Makai
60 template<typename _Graph>
61 class GraphAdaptorBase {
64 typedef GraphAdaptorBase Adaptor;
65 typedef Graph ParentGraph;
69 GraphAdaptorBase() : graph(0) { }
70 void setGraph(Graph& _graph) { graph=&_graph; }
73 GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
75 typedef typename Graph::Node Node;
76 typedef typename Graph::Edge Edge;
78 void first(Node& i) const { graph->first(i); }
79 void first(Edge& i) const { graph->first(i); }
80 void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
81 void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
83 void next(Node& i) const { graph->next(i); }
84 void next(Edge& i) const { graph->next(i); }
85 void nextIn(Edge& i) const { graph->nextIn(i); }
86 void nextOut(Edge& i) const { graph->nextOut(i); }
88 Node source(const Edge& e) const { return graph->source(e); }
89 Node target(const Edge& e) const { return graph->target(e); }
91 typedef NodeNumTagIndicator<Graph> NodeNumTag;
92 int nodeNum() const { return graph->nodeNum(); }
94 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
95 int edgeNum() const { return graph->edgeNum(); }
97 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
98 Edge findEdge(const Node& source, const Node& target,
99 const Edge& prev = INVALID) {
100 return graph->findEdge(source, target, prev);
103 Node addNode() const {
104 return Node(graph->addNode());
107 Edge addEdge(const Node& source, const Node& target) const {
108 return Edge(graph->addEdge(source, target));
111 void erase(const Node& i) const { graph->erase(i); }
112 void erase(const Edge& i) const { graph->erase(i); }
114 void clear() const { graph->clear(); }
116 int id(const Node& v) const { return graph->id(v); }
117 int id(const Edge& e) const { return graph->id(e); }
119 Node fromNodeId(int id) const {
120 return graph->fromNodeId(id);
123 Edge fromEdgeId(int id) const {
124 return graph->fromEdgeId(id);
127 int maxNodeId() const {
128 return graph->maxNodeId();
131 int maxEdgeId() const {
132 return graph->maxEdgeId();
135 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
137 NodeNotifier& getNotifier(Node) const {
138 return graph->getNotifier(Node());
141 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
143 EdgeNotifier& getNotifier(Edge) const {
144 return graph->getNotifier(Edge());
147 template <typename _Value>
148 class NodeMap : public Graph::template NodeMap<_Value> {
151 typedef typename Graph::template NodeMap<_Value> Parent;
153 explicit NodeMap(const Adaptor& ga)
154 : Parent(*ga.graph) {}
156 NodeMap(const Adaptor& ga, const _Value& value)
157 : Parent(*ga.graph, value) { }
159 NodeMap& operator=(const NodeMap& cmap) {
160 return operator=<NodeMap>(cmap);
163 template <typename CMap>
164 NodeMap& operator=(const CMap& cmap) {
165 Parent::operator=(cmap);
171 template <typename _Value>
172 class EdgeMap : public Graph::template EdgeMap<_Value> {
175 typedef typename Graph::template EdgeMap<_Value> Parent;
177 explicit EdgeMap(const Adaptor& ga)
178 : Parent(*ga.graph) {}
180 EdgeMap(const Adaptor& ga, const _Value& value)
181 : Parent(*ga.graph, value) {}
183 EdgeMap& operator=(const EdgeMap& cmap) {
184 return operator=<EdgeMap>(cmap);
187 template <typename CMap>
188 EdgeMap& operator=(const CMap& cmap) {
189 Parent::operator=(cmap);
197 template <typename _Graph>
199 public GraphAdaptorExtender<GraphAdaptorBase<_Graph> > {
201 typedef _Graph Graph;
202 typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent;
204 GraphAdaptor() : Parent() { }
207 explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
210 /// \brief Just gives back a graph adaptor
212 /// Just gives back a graph adaptor which
213 /// should be provide original graph
214 template<typename Graph>
215 GraphAdaptor<const Graph>
216 graphAdaptor(const Graph& graph) {
217 return GraphAdaptor<const Graph>(graph);
221 template <typename _Graph>
222 class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
224 typedef _Graph Graph;
225 typedef GraphAdaptorBase<_Graph> Parent;
227 RevGraphAdaptorBase() : Parent() { }
229 typedef typename Parent::Node Node;
230 typedef typename Parent::Edge Edge;
232 void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
233 void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
235 void nextIn(Edge& i) const { Parent::nextOut(i); }
236 void nextOut(Edge& i) const { Parent::nextIn(i); }
238 Node source(const Edge& e) const { return Parent::target(e); }
239 Node target(const Edge& e) const { return Parent::source(e); }
241 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
242 Edge findEdge(const Node& source, const Node& target,
243 const Edge& prev = INVALID) {
244 return Parent::findEdge(target, source, prev);
250 ///\brief A graph adaptor which reverses the orientation of the edges.
251 ///\ingroup graph_adaptors
253 ///\warning Graph adaptors are in even more experimental
254 ///state than the other
255 ///parts of the lib. Use them at you own risk.
257 /// If \c g is defined as
263 /// RevGraphAdaptor<ListGraph> ga(g);
265 ///implements the graph obtained from \c g by
266 /// reversing the orientation of its edges.
268 template<typename _Graph>
269 class RevGraphAdaptor :
270 public GraphAdaptorExtender<RevGraphAdaptorBase<_Graph> > {
272 typedef _Graph Graph;
273 typedef GraphAdaptorExtender<
274 RevGraphAdaptorBase<_Graph> > Parent;
276 RevGraphAdaptor() { }
278 explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
281 /// \brief Just gives back a reverse graph adaptor
283 /// Just gives back a reverse graph adaptor
284 template<typename Graph>
285 RevGraphAdaptor<const Graph>
286 revGraphAdaptor(const Graph& graph) {
287 return RevGraphAdaptor<const Graph>(graph);
290 template <typename _Graph, typename NodeFilterMap,
291 typename EdgeFilterMap, bool checked = true>
292 class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
294 typedef _Graph Graph;
295 typedef SubGraphAdaptorBase Adaptor;
296 typedef GraphAdaptorBase<_Graph> Parent;
298 NodeFilterMap* node_filter_map;
299 EdgeFilterMap* edge_filter_map;
300 SubGraphAdaptorBase() : Parent(),
301 node_filter_map(0), edge_filter_map(0) { }
303 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
304 node_filter_map=&_node_filter_map;
306 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
307 edge_filter_map=&_edge_filter_map;
312 typedef typename Parent::Node Node;
313 typedef typename Parent::Edge Edge;
315 void first(Node& i) const {
317 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
320 void first(Edge& i) const {
322 while (i!=INVALID && (!(*edge_filter_map)[i]
323 || !(*node_filter_map)[Parent::source(i)]
324 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
327 void firstIn(Edge& i, const Node& n) const {
328 Parent::firstIn(i, n);
329 while (i!=INVALID && (!(*edge_filter_map)[i]
330 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
333 void firstOut(Edge& i, const Node& n) const {
334 Parent::firstOut(i, n);
335 while (i!=INVALID && (!(*edge_filter_map)[i]
336 || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
339 void next(Node& i) const {
341 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
344 void next(Edge& i) const {
346 while (i!=INVALID && (!(*edge_filter_map)[i]
347 || !(*node_filter_map)[Parent::source(i)]
348 || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
351 void nextIn(Edge& i) const {
353 while (i!=INVALID && (!(*edge_filter_map)[i]
354 || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
357 void nextOut(Edge& i) const {
359 while (i!=INVALID && (!(*edge_filter_map)[i]
360 || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
365 /// This function hides \c n in the graph, i.e. the iteration
366 /// jumps over it. This is done by simply setting the value of \c n
367 /// to be false in the corresponding node-map.
368 void hide(const Node& n) const { node_filter_map->set(n, false); }
372 /// This function hides \c e in the graph, i.e. the iteration
373 /// jumps over it. This is done by simply setting the value of \c e
374 /// to be false in the corresponding edge-map.
375 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
379 /// The value of \c n is set to be true in the node-map which stores
380 /// hide information. If \c n was hidden previuosly, then it is shown
382 void unHide(const Node& n) const { node_filter_map->set(n, true); }
386 /// The value of \c e is set to be true in the edge-map which stores
387 /// hide information. If \c e was hidden previuosly, then it is shown
389 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
391 /// Returns true if \c n is hidden.
395 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
397 /// Returns true if \c n is hidden.
401 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
403 typedef False NodeNumTag;
404 typedef False EdgeNumTag;
406 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
407 Edge findEdge(const Node& source, const Node& target,
408 const Edge& prev = INVALID) {
409 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
412 Edge edge = Parent::findEdge(source, target, prev);
413 while (edge != INVALID && !(*edge_filter_map)[edge]) {
414 edge = Parent::findEdge(source, target, edge);
419 template <typename _Value>
421 : public SubMapExtender<Adaptor,
422 typename Parent::template NodeMap<_Value> >
425 typedef Adaptor Graph;
426 typedef SubMapExtender<Adaptor, typename Parent::
427 template NodeMap<_Value> > Parent;
429 NodeMap(const Graph& graph)
431 NodeMap(const Graph& graph, const _Value& value)
432 : Parent(graph, value) {}
434 NodeMap& operator=(const NodeMap& cmap) {
435 return operator=<NodeMap>(cmap);
438 template <typename CMap>
439 NodeMap& operator=(const CMap& cmap) {
440 Parent::operator=(cmap);
445 template <typename _Value>
447 : public SubMapExtender<Adaptor,
448 typename Parent::template EdgeMap<_Value> >
451 typedef Adaptor Graph;
452 typedef SubMapExtender<Adaptor, typename Parent::
453 template EdgeMap<_Value> > Parent;
455 EdgeMap(const Graph& graph)
457 EdgeMap(const Graph& graph, const _Value& value)
458 : Parent(graph, value) {}
460 EdgeMap& operator=(const EdgeMap& cmap) {
461 return operator=<EdgeMap>(cmap);
464 template <typename CMap>
465 EdgeMap& operator=(const CMap& cmap) {
466 Parent::operator=(cmap);
473 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
474 class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
475 : public GraphAdaptorBase<_Graph> {
477 typedef _Graph Graph;
478 typedef SubGraphAdaptorBase Adaptor;
479 typedef GraphAdaptorBase<_Graph> Parent;
481 NodeFilterMap* node_filter_map;
482 EdgeFilterMap* edge_filter_map;
483 SubGraphAdaptorBase() : Parent(),
484 node_filter_map(0), edge_filter_map(0) { }
486 void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
487 node_filter_map=&_node_filter_map;
489 void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
490 edge_filter_map=&_edge_filter_map;
495 typedef typename Parent::Node Node;
496 typedef typename Parent::Edge Edge;
498 void first(Node& i) const {
500 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
503 void first(Edge& i) const {
505 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
508 void firstIn(Edge& i, const Node& n) const {
509 Parent::firstIn(i, n);
510 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
513 void firstOut(Edge& i, const Node& n) const {
514 Parent::firstOut(i, n);
515 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
518 void next(Node& i) const {
520 while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
522 void next(Edge& i) const {
524 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
526 void nextIn(Edge& i) const {
528 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
531 void nextOut(Edge& i) const {
533 while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
538 /// This function hides \c n in the graph, i.e. the iteration
539 /// jumps over it. This is done by simply setting the value of \c n
540 /// to be false in the corresponding node-map.
541 void hide(const Node& n) const { node_filter_map->set(n, false); }
545 /// This function hides \c e in the graph, i.e. the iteration
546 /// jumps over it. This is done by simply setting the value of \c e
547 /// to be false in the corresponding edge-map.
548 void hide(const Edge& e) const { edge_filter_map->set(e, false); }
552 /// The value of \c n is set to be true in the node-map which stores
553 /// hide information. If \c n was hidden previuosly, then it is shown
555 void unHide(const Node& n) const { node_filter_map->set(n, true); }
559 /// The value of \c e is set to be true in the edge-map which stores
560 /// hide information. If \c e was hidden previuosly, then it is shown
562 void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
564 /// Returns true if \c n is hidden.
568 bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
570 /// Returns true if \c n is hidden.
574 bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
576 typedef False NodeNumTag;
577 typedef False EdgeNumTag;
579 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
580 Edge findEdge(const Node& source, const Node& target,
581 const Edge& prev = INVALID) {
582 if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
585 Edge edge = Parent::findEdge(source, target, prev);
586 while (edge != INVALID && !(*edge_filter_map)[edge]) {
587 edge = Parent::findEdge(source, target, edge);
592 template <typename _Value>
594 : public SubMapExtender<Adaptor,
595 typename Parent::template NodeMap<_Value> >
598 typedef Adaptor Graph;
599 typedef SubMapExtender<Adaptor, typename Parent::
600 template NodeMap<_Value> > Parent;
602 NodeMap(const Graph& graph)
604 NodeMap(const Graph& graph, const _Value& value)
605 : Parent(graph, value) {}
607 NodeMap& operator=(const NodeMap& cmap) {
608 return operator=<NodeMap>(cmap);
611 template <typename CMap>
612 NodeMap& operator=(const CMap& cmap) {
613 Parent::operator=(cmap);
618 template <typename _Value>
620 : public SubMapExtender<Adaptor,
621 typename Parent::template EdgeMap<_Value> >
624 typedef Adaptor Graph;
625 typedef SubMapExtender<Adaptor, typename Parent::
626 template EdgeMap<_Value> > Parent;
628 EdgeMap(const Graph& graph)
630 EdgeMap(const Graph& graph, const _Value& value)
631 : Parent(graph, value) {}
633 EdgeMap& operator=(const EdgeMap& cmap) {
634 return operator=<EdgeMap>(cmap);
637 template <typename CMap>
638 EdgeMap& operator=(const CMap& cmap) {
639 Parent::operator=(cmap);
646 /// \brief A graph adaptor for hiding nodes and edges from a graph.
647 /// \ingroup graph_adaptors
649 /// \warning Graph adaptors are in even more experimental state than the
650 /// other parts of the lib. Use them at you own risk.
652 /// SubGraphAdaptor shows the graph with filtered node-set and
653 /// edge-set. If the \c checked parameter is true then it filters the edgeset
654 /// to do not get invalid edges without source or target.
655 /// Let \f$ G=(V, A) \f$ be a directed graph
656 /// and suppose that the graph instance \c g of type ListGraph
657 /// implements \f$ G \f$.
658 /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
659 /// on the node-set and edge-set.
660 /// SubGraphAdaptor<...>::NodeIt iterates
661 /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and
662 /// SubGraphAdaptor<...>::EdgeIt iterates
663 /// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly,
664 /// SubGraphAdaptor<...>::OutEdgeIt and
665 /// SubGraphAdaptor<...>::InEdgeIt iterates
666 /// only on edges leaving and entering a specific node which have true value.
668 /// If the \c checked template parameter is false then we have to note that
669 /// the node-iterator cares only the filter on the node-set, and the
670 /// edge-iterator cares only the filter on the edge-set.
671 /// This way the edge-map
672 /// should filter all edges which's source or target is filtered by the
675 /// typedef ListGraph Graph;
677 /// typedef Graph::Node Node;
678 /// typedef Graph::Edge Edge;
679 /// Node u=g.addNode(); //node of id 0
680 /// Node v=g.addNode(); //node of id 1
681 /// Node e=g.addEdge(u, v); //edge of id 0
682 /// Node f=g.addEdge(v, u); //edge of id 1
683 /// Graph::NodeMap<bool> nm(g, true);
684 /// nm.set(u, false);
685 /// Graph::EdgeMap<bool> em(g, true);
686 /// em.set(e, false);
687 /// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGA;
688 /// SubGA ga(g, nm, em);
689 /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
690 /// std::cout << ":-)" << std::endl;
691 /// for (SubGA::EdgeIt e(ga); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
693 /// The output of the above code is the following.
699 /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to
700 /// \c Graph::Node that is why \c g.id(n) can be applied.
702 /// For other examples see also the documentation of NodeSubGraphAdaptor and
703 /// EdgeSubGraphAdaptor.
705 /// \author Marton Makai
707 template<typename _Graph, typename NodeFilterMap,
708 typename EdgeFilterMap, bool checked = true>
709 class SubGraphAdaptor :
710 public GraphAdaptorExtender<
711 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
713 typedef _Graph Graph;
714 typedef GraphAdaptorExtender< SubGraphAdaptorBase<_Graph, NodeFilterMap,
715 EdgeFilterMap, checked> >
719 SubGraphAdaptor() { }
722 SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
723 EdgeFilterMap& _edge_filter_map) {
725 setNodeFilterMap(_node_filter_map);
726 setEdgeFilterMap(_edge_filter_map);
731 /// \brief Just gives back a sub graph adaptor
733 /// Just gives back a sub graph adaptor
734 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
735 SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
736 subGraphAdaptor(const Graph& graph,
737 NodeFilterMap& nfm, EdgeFilterMap& efm) {
738 return SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
742 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
743 SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
744 subGraphAdaptor(const Graph& graph,
745 NodeFilterMap& nfm, EdgeFilterMap& efm) {
746 return SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
750 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
751 SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
752 subGraphAdaptor(const Graph& graph,
753 NodeFilterMap& nfm, EdgeFilterMap& efm) {
754 return SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
758 template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
759 SubGraphAdaptor<const Graph, const NodeFilterMap, const EdgeFilterMap>
760 subGraphAdaptor(const Graph& graph,
761 NodeFilterMap& nfm, EdgeFilterMap& efm) {
762 return SubGraphAdaptor<const Graph, const NodeFilterMap,
763 const EdgeFilterMap>(graph, nfm, efm);
768 ///\brief An adaptor for hiding nodes from a graph.
769 ///\ingroup graph_adaptors
771 ///\warning Graph adaptors are in even more experimental state
773 ///parts of the lib. Use them at you own risk.
775 ///An adaptor for hiding nodes from a graph.
776 ///This adaptor specializes SubGraphAdaptor in the way that only
778 ///can be filtered. In usual case the checked parameter is true, we get the
779 ///induced subgraph. But if the checked parameter is false then we can only
780 ///filter only isolated nodes.
781 ///\author Marton Makai
782 template<typename Graph, typename NodeFilterMap, bool checked = true>
783 class NodeSubGraphAdaptor :
784 public SubGraphAdaptor<Graph, NodeFilterMap,
785 ConstMap<typename Graph::Edge,bool>, checked> {
788 typedef SubGraphAdaptor<Graph, NodeFilterMap,
789 ConstMap<typename Graph::Edge,bool>, checked >
793 ConstMap<typename Graph::Edge, bool> const_true_map;
795 NodeSubGraphAdaptor() : const_true_map(true) {
796 Parent::setEdgeFilterMap(const_true_map);
801 NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
802 Parent(), const_true_map(true) {
803 Parent::setGraph(_graph);
804 Parent::setNodeFilterMap(_node_filter_map);
805 Parent::setEdgeFilterMap(const_true_map);
811 /// \brief Just gives back a node sub graph adaptor
813 /// Just gives back a node sub graph adaptor
814 template<typename Graph, typename NodeFilterMap>
815 NodeSubGraphAdaptor<const Graph, NodeFilterMap>
816 nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) {
817 return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm);
820 template<typename Graph, typename NodeFilterMap>
821 NodeSubGraphAdaptor<const Graph, const NodeFilterMap>
822 nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) {
823 return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm);
826 ///\brief An adaptor for hiding edges from a graph.
828 ///\warning Graph adaptors are in even more experimental state
829 ///than the other parts of the lib. Use them at you own risk.
831 ///An adaptor for hiding edges from a graph.
832 ///This adaptor specializes SubGraphAdaptor in the way that
834 ///can be filtered. The usefulness of this adaptor is demonstrated in the
835 ///problem of searching a maximum number of edge-disjoint shortest paths
837 ///two nodes \c s and \c t. Shortest here means being shortest w.r.t.
838 ///non-negative edge-lengths. Note that
839 ///the comprehension of the presented solution
840 ///need's some elementary knowledge from combinatorial optimization.
842 ///If a single shortest path is to be
843 ///searched between \c s and \c t, then this can be done easily by
844 ///applying the Dijkstra algorithm. What happens, if a maximum number of
845 ///edge-disjoint shortest paths is to be computed. It can be proved that an
846 ///edge can be in a shortest path if and only
847 ///if it is tight with respect to
848 ///the potential function computed by Dijkstra.
849 ///Moreover, any path containing
850 ///only such edges is a shortest one.
851 ///Thus we have to compute a maximum number
852 ///of edge-disjoint paths between \c s and \c t in
853 ///the graph which has edge-set
854 ///all the tight edges. The computation will be demonstrated
856 ///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim.
857 ///The full source code is available in \ref sub_graph_adaptor_demo.cc.
858 ///If you are interested in more demo programs, you can use
859 ///\ref dim_to_dot.cc to generate .dot files from dimacs files.
860 ///The .dot file of the following figure was generated by
861 ///the demo program \ref dim_to_dot.cc.
864 ///digraph lemon_dot_example {
865 ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
866 ///n0 [ label="0 (s)" ];
872 ///n6 [ label="6 (t)" ];
873 ///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
874 ///n5 -> n6 [ label="9, length:4" ];
875 ///n4 -> n6 [ label="8, length:2" ];
876 ///n3 -> n5 [ label="7, length:1" ];
877 ///n2 -> n5 [ label="6, length:3" ];
878 ///n2 -> n6 [ label="5, length:5" ];
879 ///n2 -> n4 [ label="4, length:2" ];
880 ///n1 -> n4 [ label="3, length:3" ];
881 ///n0 -> n3 [ label="2, length:1" ];
882 ///n0 -> n2 [ label="1, length:2" ];
883 ///n0 -> n1 [ label="0, length:3" ];
890 ///LengthMap length(g);
892 ///readDimacs(std::cin, g, length, s, t);
894 ///cout << "edges with lengths (of form id, source--length->target): " << endl;
895 ///for(EdgeIt e(g); e!=INVALID; ++e)
896 /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
897 /// << length[e] << "->" << g.id(g.target(e)) << endl;
899 ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
901 ///Next, the potential function is computed with Dijkstra.
903 ///typedef Dijkstra<Graph, LengthMap> Dijkstra;
904 ///Dijkstra dijkstra(g, length);
907 ///Next, we consrtruct a map which filters the edge-set to the tight edges.
909 ///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
911 ///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
913 ///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGA;
914 ///SubGA ga(g, tight_edge_filter);
916 ///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
917 ///with a max flow algorithm Preflow.
919 ///ConstMap<Edge, int> const_1_map(1);
920 ///Graph::EdgeMap<int> flow(g, 0);
922 ///Preflow<SubGA, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
923 /// preflow(ga, s, t, const_1_map, flow);
926 ///Last, the output is:
928 ///cout << "maximum number of edge-disjoint shortest path: "
929 /// << preflow.flowValue() << endl;
930 ///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
932 ///for(EdgeIt e(g); e!=INVALID; ++e)
934 /// cout << " " << g.id(g.source(e)) << "--"
935 /// << length[e] << "->" << g.id(g.target(e)) << endl;
937 ///The program has the following (expected :-)) output:
939 ///edges with lengths (of form id, source--length->target):
951 ///maximum number of edge-disjoint shortest path: 2
952 ///edges of the maximum number of edge-disjoint shortest s-t paths:
961 ///\author Marton Makai
962 template<typename Graph, typename EdgeFilterMap>
963 class EdgeSubGraphAdaptor :
964 public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
965 EdgeFilterMap, false> {
967 typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
968 EdgeFilterMap, false> Parent;
970 ConstMap<typename Graph::Node, bool> const_true_map;
972 EdgeSubGraphAdaptor() : const_true_map(true) {
973 Parent::setNodeFilterMap(const_true_map);
978 EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
979 Parent(), const_true_map(true) {
980 Parent::setGraph(_graph);
981 Parent::setNodeFilterMap(const_true_map);
982 Parent::setEdgeFilterMap(_edge_filter_map);
987 /// \brief Just gives back an edge sub graph adaptor
989 /// Just gives back an edge sub graph adaptor
990 template<typename Graph, typename EdgeFilterMap>
991 EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>
992 edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) {
993 return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm);
996 template<typename Graph, typename EdgeFilterMap>
997 EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>
998 edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) {
999 return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm);
1002 template <typename _Graph, typename Enable = void>
1003 class UndirGraphAdaptorBase :
1004 public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
1006 typedef _Graph Graph;
1007 typedef UndirGraphAdaptorBase Adaptor;
1008 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
1012 UndirGraphAdaptorBase() : Parent() {}
1016 typedef typename Parent::UEdge UEdge;
1017 typedef typename Parent::Edge Edge;
1021 template <typename _Value>
1025 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1029 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1031 typedef _Value Value;
1034 EdgeMapBase(const Adaptor& adaptor) :
1035 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1037 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1038 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1040 void set(const Edge& e, const Value& a) {
1041 if (Parent::direction(e)) {
1042 forward_map.set(e, a);
1044 backward_map.set(e, a);
1048 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1049 if (Parent::direction(e)) {
1050 return forward_map[e];
1052 return backward_map[e];
1056 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1057 if (Parent::direction(e)) {
1058 return forward_map[e];
1060 return backward_map[e];
1066 MapImpl forward_map, backward_map;
1072 template <typename _Value>
1074 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1077 typedef Adaptor Graph;
1078 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1080 EdgeMap(const Graph& graph)
1082 EdgeMap(const Graph& graph, const _Value& value)
1083 : Parent(graph, value) {}
1085 EdgeMap& operator=(const EdgeMap& cmap) {
1086 return operator=<EdgeMap>(cmap);
1089 template <typename CMap>
1090 EdgeMap& operator=(const CMap& cmap) {
1091 Parent::operator=(cmap);
1096 template <typename _Value>
1097 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1100 typedef typename Graph::template EdgeMap<_Value> Parent;
1102 explicit UEdgeMap(const Adaptor& ga)
1103 : Parent(*ga.graph) {}
1105 UEdgeMap(const Adaptor& ga, const _Value& value)
1106 : Parent(*ga.graph, value) {}
1108 UEdgeMap& operator=(const UEdgeMap& cmap) {
1109 return operator=<UEdgeMap>(cmap);
1112 template <typename CMap>
1113 UEdgeMap& operator=(const CMap& cmap) {
1114 Parent::operator=(cmap);
1122 template <typename _Graph>
1123 class UndirGraphAdaptorBase<
1124 _Graph, typename enable_if<
1125 typename _Graph::EdgeNotifier::Notifier>::type >
1126 : public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
1129 typedef _Graph Graph;
1130 typedef UndirGraphAdaptorBase Adaptor;
1131 typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
1135 UndirGraphAdaptorBase()
1136 : edge_notifier(*this), edge_notifier_proxy(edge_notifier) {}
1138 void setGraph(_Graph& graph) {
1139 Parent::setGraph(graph);
1140 edge_notifier_proxy.setUEdgeNotifier(graph.getNotifier(UEdge()));
1145 ~UndirGraphAdaptorBase() {
1146 edge_notifier.clear();
1149 int maxId(typename Parent::Edge) const {
1150 return Parent::maxEdgeId();
1154 typedef typename Parent::UEdge UEdge;
1155 typedef typename Parent::Edge Edge;
1157 typedef typename Parent::EdgeNotifier UEdgeNotifier;
1159 using Parent::getNotifier;
1161 typedef AlterationNotifier<UndirGraphAdaptorBase, Edge> EdgeNotifier;
1162 EdgeNotifier& getNotifier(Edge) const { return edge_notifier; }
1166 class NotifierProxy : public UEdgeNotifier::ObserverBase {
1169 typedef typename UEdgeNotifier::ObserverBase Parent;
1170 typedef UndirGraphAdaptorBase AdaptorBase;
1172 NotifierProxy(EdgeNotifier& _edge_notifier)
1173 : Parent(), edge_notifier(_edge_notifier) {
1176 virtual ~NotifierProxy() {
1177 if (Parent::attached()) {
1182 void setUEdgeNotifier(UEdgeNotifier& _uedge_notifier) {
1183 Parent::attach(_uedge_notifier);
1189 virtual void add(const UEdge& uedge) {
1190 std::vector<Edge> edges;
1191 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1192 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1193 edge_notifier.add(edges);
1195 virtual void add(const std::vector<UEdge>& uedges) {
1196 std::vector<Edge> edges;
1197 for (int i = 0; i < (int)uedges.size(); ++i) {
1198 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1199 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1201 edge_notifier.add(edges);
1203 virtual void erase(const UEdge& uedge) {
1204 std::vector<Edge> edges;
1205 edges.push_back(AdaptorBase::Parent::direct(uedge, true));
1206 edges.push_back(AdaptorBase::Parent::direct(uedge, false));
1207 edge_notifier.erase(edges);
1209 virtual void erase(const std::vector<UEdge>& uedges) {
1210 std::vector<Edge> edges;
1211 for (int i = 0; i < (int)uedges.size(); ++i) {
1212 edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
1213 edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
1215 edge_notifier.erase(edges);
1217 virtual void build() {
1218 edge_notifier.build();
1220 virtual void clear() {
1221 edge_notifier.clear();
1224 EdgeNotifier& edge_notifier;
1228 mutable EdgeNotifier edge_notifier;
1229 NotifierProxy edge_notifier_proxy;
1233 template <typename _Value>
1237 typedef typename _Graph::template EdgeMap<_Value> MapImpl;
1241 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1243 typedef _Value Value;
1246 EdgeMapBase(const Adaptor& adaptor) :
1247 forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
1249 EdgeMapBase(const Adaptor& adaptor, const Value& v)
1250 : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
1252 void set(const Edge& e, const Value& a) {
1253 if (Parent::direction(e)) {
1254 forward_map.set(e, a);
1256 backward_map.set(e, a);
1260 typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
1261 if (Parent::direction(e)) {
1262 return forward_map[e];
1264 return backward_map[e];
1268 typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
1269 if (Parent::direction(e)) {
1270 return forward_map[e];
1272 return backward_map[e];
1278 MapImpl forward_map, backward_map;
1284 template <typename _Value>
1286 : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
1289 typedef Adaptor Graph;
1290 typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
1292 EdgeMap(const Graph& graph)
1294 EdgeMap(const Graph& graph, const _Value& value)
1295 : Parent(graph, value) {}
1297 EdgeMap& operator=(const EdgeMap& cmap) {
1298 return operator=<EdgeMap>(cmap);
1301 template <typename CMap>
1302 EdgeMap& operator=(const CMap& cmap) {
1303 Parent::operator=(cmap);
1308 template <typename _Value>
1309 class UEdgeMap : public Graph::template EdgeMap<_Value> {
1312 typedef typename Graph::template EdgeMap<_Value> Parent;
1314 explicit UEdgeMap(const Adaptor& ga)
1315 : Parent(*ga.graph) {}
1317 UEdgeMap(const Adaptor& ga, const _Value& value)
1318 : Parent(*ga.graph, value) {}
1320 UEdgeMap& operator=(const UEdgeMap& cmap) {
1321 return operator=<UEdgeMap>(cmap);
1324 template <typename CMap>
1325 UEdgeMap& operator=(const CMap& cmap) {
1326 Parent::operator=(cmap);
1334 ///\brief An undirected graph is made from a directed graph by an adaptor
1335 ///\ingroup graph_adaptors
1337 /// Undocumented, untested!!!
1338 /// If somebody knows nice demo application, let's polulate it.
1340 /// \author Marton Makai
1341 template<typename _Graph>
1342 class UndirGraphAdaptor :
1343 public UGraphAdaptorExtender<
1344 UndirGraphAdaptorBase<_Graph> > {
1346 typedef _Graph Graph;
1347 typedef UGraphAdaptorExtender<
1348 UndirGraphAdaptorBase<_Graph> > Parent;
1350 UndirGraphAdaptor() { }
1352 UndirGraphAdaptor(_Graph& _graph) {
1356 template <typename _ForwardMap, typename _BackwardMap>
1357 class CombinedEdgeMap {
1360 typedef _ForwardMap ForwardMap;
1361 typedef _BackwardMap BackwardMap;
1363 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
1365 typedef typename ForwardMap::Value Value;
1366 typedef typename Parent::Edge Key;
1368 CombinedEdgeMap() : forward_map(0), backward_map(0) {}
1370 CombinedEdgeMap(ForwardMap& _forward_map, BackwardMap& _backward_map)
1371 : forward_map(&_forward_map), backward_map(&_backward_map) {}
1373 void set(const Key& e, const Value& a) {
1374 if (Parent::direction(e)) {
1375 forward_map->set(e, a);
1377 backward_map->set(e, a);
1381 typename MapTraits<ForwardMap>::ConstReturnValue
1382 operator[](const Key& e) const {
1383 if (Parent::direction(e)) {
1384 return (*forward_map)[e];
1386 return (*backward_map)[e];
1390 typename MapTraits<ForwardMap>::ReturnValue
1391 operator[](const Key& e) {
1392 if (Parent::direction(e)) {
1393 return (*forward_map)[e];
1395 return (*backward_map)[e];
1399 void setForwardMap(ForwardMap& _forward_map) {
1400 forward_map = &_forward_map;
1403 void setBackwardMap(BackwardMap& _backward_map) {
1404 backward_map = &_backward_map;
1409 ForwardMap* forward_map;
1410 BackwardMap* backward_map;
1416 /// \brief Just gives back an undir graph adaptor
1418 /// Just gives back an undir graph adaptor
1419 template<typename Graph>
1420 UndirGraphAdaptor<const Graph>
1421 undirGraphAdaptor(const Graph& graph) {
1422 return UndirGraphAdaptor<const Graph>(graph);
1425 template<typename Graph, typename Number,
1426 typename CapacityMap, typename FlowMap>
1427 class ResForwardFilter {
1428 const CapacityMap* capacity;
1429 const FlowMap* flow;
1431 typedef typename Graph::Edge Key;
1434 ResForwardFilter(const CapacityMap& _capacity, const FlowMap& _flow)
1435 : capacity(&_capacity), flow(&_flow) { }
1436 ResForwardFilter() : capacity(0), flow(0) { }
1437 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1438 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1439 bool operator[](const typename Graph::Edge& e) const {
1440 return (Number((*flow)[e]) < Number((*capacity)[e]));
1444 template<typename Graph, typename Number,
1445 typename CapacityMap, typename FlowMap>
1446 class ResBackwardFilter {
1447 const CapacityMap* capacity;
1448 const FlowMap* flow;
1450 typedef typename Graph::Edge Key;
1453 ResBackwardFilter(const CapacityMap& _capacity, const FlowMap& _flow)
1454 : capacity(&_capacity), flow(&_flow) { }
1455 ResBackwardFilter() : capacity(0), flow(0) { }
1456 void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
1457 void setFlow(const FlowMap& _flow) { flow = &_flow; }
1458 bool operator[](const typename Graph::Edge& e) const {
1459 return (Number(0) < Number((*flow)[e]));
1464 ///\brief An adaptor for composing the residual
1465 ///graph for directed flow and circulation problems.
1466 ///\ingroup graph_adaptors
1468 ///An adaptor for composing the residual graph for
1469 ///directed flow and circulation problems.
1470 ///Let \f$ G=(V, A) \f$ be a directed graph and let \f$ F \f$ be a
1471 ///number type. Let moreover
1472 ///\f$ f,c:A\to F \f$, be functions on the edge-set.
1473 ///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands for a flow
1474 ///and \f$ c \f$ for a capacity function.
1475 ///Suppose that a graph instange \c g of type
1476 ///\c ListGraph implements \f$ G \f$.
1480 ///Then RevGraphAdaptor implements the graph structure with node-set
1481 ///\f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$, where
1482 ///\f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
1483 ///\f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$,
1484 ///i.e. the so called residual graph.
1485 ///When we take the union \f$ A_{forward}\cup A_{backward} \f$,
1486 ///multilicities are counted, i.e. if an edge is in both
1487 ///\f$ A_{forward} \f$ and \f$ A_{backward} \f$, then in the adaptor it
1489 ///The following code shows how
1490 ///such an instance can be constructed.
1492 ///typedef ListGraph Graph;
1493 ///Graph::EdgeMap<int> f(g);
1494 ///Graph::EdgeMap<int> c(g);
1495 ///ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > ga(g);
1497 ///\author Marton Makai
1499 template<typename Graph, typename Number,
1500 typename CapacityMap, typename FlowMap>
1501 class ResGraphAdaptor :
1502 public EdgeSubGraphAdaptor<
1503 UndirGraphAdaptor<Graph>,
1504 typename UndirGraphAdaptor<Graph>::template CombinedEdgeMap<
1505 ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
1506 ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > > {
1509 typedef UndirGraphAdaptor<Graph> UGraph;
1511 typedef ResForwardFilter<Graph, Number, CapacityMap, FlowMap>
1514 typedef ResBackwardFilter<Graph, Number, CapacityMap, FlowMap>
1517 typedef typename UGraph::
1518 template CombinedEdgeMap<ForwardFilter, BackwardFilter>
1521 typedef EdgeSubGraphAdaptor<UGraph, EdgeFilter> Parent;
1525 const CapacityMap* capacity;
1529 ForwardFilter forward_filter;
1530 BackwardFilter backward_filter;
1531 EdgeFilter edge_filter;
1533 void setCapacityMap(const CapacityMap& _capacity) {
1534 capacity=&_capacity;
1535 forward_filter.setCapacity(_capacity);
1536 backward_filter.setCapacity(_capacity);
1539 void setFlowMap(FlowMap& _flow) {
1541 forward_filter.setFlow(_flow);
1542 backward_filter.setFlow(_flow);
1547 ResGraphAdaptor(Graph& _graph, const CapacityMap& _capacity,
1549 : Parent(), capacity(&_capacity), flow(&_flow),
1551 forward_filter(_capacity, _flow),
1552 backward_filter(_capacity, _flow),
1553 edge_filter(forward_filter, backward_filter) {
1554 Parent::setGraph(ugraph);
1555 Parent::setEdgeFilterMap(edge_filter);
1558 typedef typename Parent::Edge Edge;
1560 void augment(const Edge& e, Number a) const {
1561 if (UGraph::direction(e)) {
1562 flow->set(e, (*flow)[e]+a);
1564 flow->set(e, (*flow)[e]-a);
1568 static bool forward(const Edge& e) {
1569 return UGraph::direction(e);
1572 static bool backward(const Edge& e) {
1573 return !UGraph::direction(e);
1576 static Edge forward(const typename Graph::Edge& e) {
1577 return UGraph::direct(e, true);
1580 static Edge backward(const typename Graph::Edge& e) {
1581 return UGraph::direct(e, false);
1585 /// \brief Residual capacity map.
1587 /// In generic residual graphs the residual capacity can be obtained
1591 const ResGraphAdaptor* res_graph;
1593 typedef Number Value;
1595 ResCap(const ResGraphAdaptor& _res_graph)
1596 : res_graph(&_res_graph) {}
1598 Number operator[](const Edge& e) const {
1599 if (ResGraphAdaptor::direction(e))
1600 return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
1602 return (*(res_graph->flow))[e];
1611 template <typename _Graph, typename FirstOutEdgesMap>
1612 class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
1614 typedef _Graph Graph;
1615 typedef GraphAdaptorBase<_Graph> Parent;
1617 FirstOutEdgesMap* first_out_edges;
1618 ErasingFirstGraphAdaptorBase() : Parent(),
1619 first_out_edges(0) { }
1621 void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
1622 first_out_edges=&_first_out_edges;
1627 typedef typename Parent::Node Node;
1628 typedef typename Parent::Edge Edge;
1630 void firstOut(Edge& i, const Node& n) const {
1631 i=(*first_out_edges)[n];
1634 void erase(const Edge& e) const {
1638 first_out_edges->set(n, f);
1643 ///\brief For blocking flows.
1644 ///\ingroup graph_adaptors
1646 ///\warning Graph adaptors are in even more
1647 ///experimental state than the other
1648 ///parts of the lib. Use them at you own risk.
1650 ///This graph adaptor is used for on-the-fly
1651 ///Dinits blocking flow computations.
1652 ///For each node, an out-edge is stored which is used when the
1654 ///OutEdgeIt& first(OutEdgeIt&, const Node&)
1658 ///\author Marton Makai
1660 template <typename _Graph, typename FirstOutEdgesMap>
1661 class ErasingFirstGraphAdaptor :
1662 public GraphAdaptorExtender<
1663 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
1665 typedef _Graph Graph;
1666 typedef GraphAdaptorExtender<
1667 ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
1668 ErasingFirstGraphAdaptor(Graph& _graph,
1669 FirstOutEdgesMap& _first_out_edges) {
1671 setFirstOutEdgesMap(_first_out_edges);
1676 // template <typename _Graph>
1677 // class SplitGraphAdaptorBase
1678 // : public GraphAdaptorBase<_Graph> {
1680 // typedef GraphAdaptorBase<_Graph> Parent;
1684 // template <typename T> class NodeMap;
1685 // template <typename T> class EdgeMap;
1688 // class Node : public Parent::Node {
1689 // friend class SplitGraphAdaptorBase;
1690 // template <typename T> friend class NodeMap;
1691 // typedef typename Parent::Node NodeParent;
1695 // Node(typename Parent::Node _node, bool _entry)
1696 // : Parent::Node(_node), entry(_entry) {}
1700 // Node(Invalid) : NodeParent(INVALID), entry(true) {}
1702 // bool operator==(const Node& node) const {
1703 // return NodeParent::operator==(node) && entry == node.entry;
1706 // bool operator!=(const Node& node) const {
1707 // return !(*this == node);
1710 // bool operator<(const Node& node) const {
1711 // return NodeParent::operator<(node) ||
1712 // (NodeParent::operator==(node) && entry < node.entry);
1716 // /// \todo May we want VARIANT/union type
1717 // class Edge : public Parent::Edge {
1718 // friend class SplitGraphAdaptorBase;
1719 // template <typename T> friend class EdgeMap;
1721 // typedef typename Parent::Edge EdgeParent;
1722 // typedef typename Parent::Node NodeParent;
1725 // Edge(const EdgeParent& edge, const NodeParent& node)
1726 // : EdgeParent(edge), bind(node) {}
1729 // Edge(Invalid) : EdgeParent(INVALID), bind(INVALID) {}
1731 // bool operator==(const Edge& edge) const {
1732 // return EdgeParent::operator==(edge) && bind == edge.bind;
1735 // bool operator!=(const Edge& edge) const {
1736 // return !(*this == edge);
1739 // bool operator<(const Edge& edge) const {
1740 // return EdgeParent::operator<(edge) ||
1741 // (EdgeParent::operator==(edge) && bind < edge.bind);
1745 // void first(Node& node) const {
1746 // Parent::first(node);
1747 // node.entry = true;
1750 // void next(Node& node) const {
1751 // if (node.entry) {
1752 // node.entry = false;
1754 // node.entry = true;
1755 // Parent::next(node);
1759 // void first(Edge& edge) const {
1760 // Parent::first(edge);
1761 // if ((typename Parent::Edge&)edge == INVALID) {
1762 // Parent::first(edge.bind);
1764 // edge.bind = INVALID;
1768 // void next(Edge& edge) const {
1769 // if ((typename Parent::Edge&)edge != INVALID) {
1770 // Parent::next(edge);
1771 // if ((typename Parent::Edge&)edge == INVALID) {
1772 // Parent::first(edge.bind);
1775 // Parent::next(edge.bind);
1779 // void firstIn(Edge& edge, const Node& node) const {
1780 // if (node.entry) {
1781 // Parent::firstIn(edge, node);
1782 // edge.bind = INVALID;
1784 // (typename Parent::Edge&)edge = INVALID;
1785 // edge.bind = node;
1789 // void nextIn(Edge& edge) const {
1790 // if ((typename Parent::Edge&)edge != INVALID) {
1791 // Parent::nextIn(edge);
1793 // edge.bind = INVALID;
1797 // void firstOut(Edge& edge, const Node& node) const {
1798 // if (!node.entry) {
1799 // Parent::firstOut(edge, node);
1800 // edge.bind = INVALID;
1802 // (typename Parent::Edge&)edge = INVALID;
1803 // edge.bind = node;
1807 // void nextOut(Edge& edge) const {
1808 // if ((typename Parent::Edge&)edge != INVALID) {
1809 // Parent::nextOut(edge);
1811 // edge.bind = INVALID;
1815 // Node source(const Edge& edge) const {
1816 // if ((typename Parent::Edge&)edge != INVALID) {
1817 // return Node(Parent::source(edge), false);
1819 // return Node(edge.bind, true);
1823 // Node target(const Edge& edge) const {
1824 // if ((typename Parent::Edge&)edge != INVALID) {
1825 // return Node(Parent::target(edge), true);
1827 // return Node(edge.bind, false);
1831 // static bool entryNode(const Node& node) {
1832 // return node.entry;
1835 // static bool exitNode(const Node& node) {
1836 // return !node.entry;
1839 // static Node getEntry(const typename Parent::Node& node) {
1840 // return Node(node, true);
1843 // static Node getExit(const typename Parent::Node& node) {
1844 // return Node(node, false);
1847 // static bool originalEdge(const Edge& edge) {
1848 // return (typename Parent::Edge&)edge != INVALID;
1851 // static bool bindingEdge(const Edge& edge) {
1852 // return edge.bind != INVALID;
1855 // static Node getBindedNode(const Edge& edge) {
1856 // return edge.bind;
1859 // int nodeNum() const {
1860 // return Parent::nodeNum() * 2;
1863 // typedef True EdgeNumTag;
1865 // int edgeNum() const {
1866 // return countEdges() + Parent::nodeNum();
1869 // Edge findEdge(const Node& source, const Node& target,
1870 // const Edge& prev = INVALID) const {
1871 // if (exitNode(source) && entryNode(target)) {
1872 // return Parent::findEdge(source, target, prev);
1874 // if (prev == INVALID && entryNode(source) && exitNode(target) &&
1875 // (typename Parent::Node&)source == (typename Parent::Node&)target) {
1876 // return Edge(INVALID, source);
1883 // template <typename T>
1884 // class NodeMap : public MapBase<Node, T> {
1885 // typedef typename Parent::template NodeMap<T> NodeImpl;
1887 // NodeMap(const SplitGraphAdaptorBase& _graph)
1888 // : entry(_graph), exit(_graph) {}
1889 // NodeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1890 // : entry(_graph, t), exit(_graph, t) {}
1892 // void set(const Node& key, const T& val) {
1893 // if (key.entry) { entry.set(key, val); }
1894 // else {exit.set(key, val); }
1897 // typename MapTraits<NodeImpl>::ReturnValue
1898 // operator[](const Node& key) {
1899 // if (key.entry) { return entry[key]; }
1900 // else { return exit[key]; }
1903 // typename MapTraits<NodeImpl>::ConstReturnValue
1904 // operator[](const Node& key) const {
1905 // if (key.entry) { return entry[key]; }
1906 // else { return exit[key]; }
1910 // NodeImpl entry, exit;
1913 // template <typename T>
1914 // class EdgeMap : public MapBase<Edge, T> {
1915 // typedef typename Parent::template NodeMap<T> NodeImpl;
1916 // typedef typename Parent::template EdgeMap<T> EdgeImpl;
1918 // EdgeMap(const SplitGraphAdaptorBase& _graph)
1919 // : bind(_graph), orig(_graph) {}
1920 // EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t)
1921 // : bind(_graph, t), orig(_graph, t) {}
1923 // void set(const Edge& key, const T& val) {
1924 // if ((typename Parent::Edge&)key != INVALID) { orig.set(key, val); }
1925 // else {bind.set(key.bind, val); }
1928 // typename MapTraits<EdgeImpl>::ReturnValue
1929 // operator[](const Edge& key) {
1930 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1931 // else {return bind[key.bind]; }
1934 // typename MapTraits<EdgeImpl>::ConstReturnValue
1935 // operator[](const Edge& key) const {
1936 // if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
1937 // else {return bind[key.bind]; }
1941 // typename Parent::template NodeMap<T> bind;
1942 // typename Parent::template EdgeMap<T> orig;
1945 // template <typename EntryMap, typename ExitMap>
1946 // class CombinedNodeMap : public MapBase<Node, typename EntryMap::Value> {
1948 // typedef MapBase<Node, typename EntryMap::Value> Parent;
1950 // typedef typename Parent::Key Key;
1951 // typedef typename Parent::Value Value;
1953 // CombinedNodeMap(EntryMap& _entryMap, ExitMap& _exitMap)
1954 // : entryMap(_entryMap), exitMap(_exitMap) {}
1956 // Value& operator[](const Key& key) {
1958 // return entryMap[key];
1960 // return exitMap[key];
1964 // Value operator[](const Key& key) const {
1966 // return entryMap[key];
1968 // return exitMap[key];
1972 // void set(const Key& key, const Value& value) {
1974 // entryMap.set(key, value);
1976 // exitMap.set(key, value);
1982 // EntryMap& entryMap;
1983 // ExitMap& exitMap;
1987 // template <typename EdgeMap, typename NodeMap>
1988 // class CombinedEdgeMap : public MapBase<Edge, typename EdgeMap::Value> {
1990 // typedef MapBase<Edge, typename EdgeMap::Value> Parent;
1992 // typedef typename Parent::Key Key;
1993 // typedef typename Parent::Value Value;
1995 // CombinedEdgeMap(EdgeMap& _edgeMap, NodeMap& _nodeMap)
1996 // : edgeMap(_edgeMap), nodeMap(_nodeMap) {}
1998 // void set(const Edge& edge, const Value& val) {
1999 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2000 // edgeMap.set(edge, val);
2002 // nodeMap.set(SplitGraphAdaptorBase::bindedNode(edge), val);
2006 // Value operator[](const Key& edge) const {
2007 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2008 // return edgeMap[edge];
2010 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2014 // Value& operator[](const Key& edge) {
2015 // if (SplitGraphAdaptorBase::originalEdge(edge)) {
2016 // return edgeMap[edge];
2018 // return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
2023 // EdgeMap& edgeMap;
2024 // NodeMap& nodeMap;
2029 // template <typename _Graph>
2030 // class SplitGraphAdaptor
2031 // : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
2033 // typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
2035 // SplitGraphAdaptor(_Graph& graph) {
2036 // Parent::setGraph(graph);
2044 #endif //LEMON_GRAPH_ADAPTOR_H