1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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_ADAPTORS_H
20 #define LEMON_ADAPTORS_H
22 /// \ingroup graph_adaptors
24 /// \brief Several graph adaptors
26 /// This file contains several useful adaptors for digraphs and graphs.
28 #include <lemon/core.h>
29 #include <lemon/maps.h>
30 #include <lemon/bits/variant.h>
32 #include <lemon/bits/graph_adaptor_extender.h>
33 #include <lemon/tolerance.h>
39 template<typename _Digraph>
40 class DigraphAdaptorBase {
42 typedef _Digraph Digraph;
43 typedef DigraphAdaptorBase Adaptor;
44 typedef Digraph ParentDigraph;
48 DigraphAdaptorBase() : _digraph(0) { }
49 void setDigraph(Digraph& digraph) { _digraph = &digraph; }
52 DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
54 typedef typename Digraph::Node Node;
55 typedef typename Digraph::Arc Arc;
57 void first(Node& i) const { _digraph->first(i); }
58 void first(Arc& i) const { _digraph->first(i); }
59 void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
60 void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
62 void next(Node& i) const { _digraph->next(i); }
63 void next(Arc& i) const { _digraph->next(i); }
64 void nextIn(Arc& i) const { _digraph->nextIn(i); }
65 void nextOut(Arc& i) const { _digraph->nextOut(i); }
67 Node source(const Arc& a) const { return _digraph->source(a); }
68 Node target(const Arc& a) const { return _digraph->target(a); }
70 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
71 int nodeNum() const { return _digraph->nodeNum(); }
73 typedef ArcNumTagIndicator<Digraph> ArcNumTag;
74 int arcNum() const { return _digraph->arcNum(); }
76 typedef FindArcTagIndicator<Digraph> FindArcTag;
77 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
78 return _digraph->findArc(u, v, prev);
81 Node addNode() { return _digraph->addNode(); }
82 Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
84 void erase(const Node& n) { _digraph->erase(n); }
85 void erase(const Arc& a) { _digraph->erase(a); }
87 void clear() { _digraph->clear(); }
89 int id(const Node& n) const { return _digraph->id(n); }
90 int id(const Arc& a) const { return _digraph->id(a); }
92 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
93 Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
95 int maxNodeId() const { return _digraph->maxNodeId(); }
96 int maxArcId() const { return _digraph->maxArcId(); }
98 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
99 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
101 typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier;
102 ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
104 template <typename _Value>
105 class NodeMap : public Digraph::template NodeMap<_Value> {
108 typedef typename Digraph::template NodeMap<_Value> Parent;
110 explicit NodeMap(const Adaptor& adaptor)
111 : Parent(*adaptor._digraph) {}
113 NodeMap(const Adaptor& adaptor, const _Value& value)
114 : Parent(*adaptor._digraph, value) { }
117 NodeMap& operator=(const NodeMap& cmap) {
118 return operator=<NodeMap>(cmap);
121 template <typename CMap>
122 NodeMap& operator=(const CMap& cmap) {
123 Parent::operator=(cmap);
129 template <typename _Value>
130 class ArcMap : public Digraph::template ArcMap<_Value> {
133 typedef typename Digraph::template ArcMap<_Value> Parent;
135 explicit ArcMap(const Adaptor& adaptor)
136 : Parent(*adaptor._digraph) {}
138 ArcMap(const Adaptor& adaptor, const _Value& value)
139 : Parent(*adaptor._digraph, value) {}
142 ArcMap& operator=(const ArcMap& cmap) {
143 return operator=<ArcMap>(cmap);
146 template <typename CMap>
147 ArcMap& operator=(const CMap& cmap) {
148 Parent::operator=(cmap);
156 template<typename _Graph>
157 class GraphAdaptorBase {
159 typedef _Graph Graph;
160 typedef Graph ParentGraph;
165 GraphAdaptorBase() : _graph(0) {}
167 void setGraph(Graph& graph) { _graph = &graph; }
170 GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
172 typedef typename Graph::Node Node;
173 typedef typename Graph::Arc Arc;
174 typedef typename Graph::Edge Edge;
176 void first(Node& i) const { _graph->first(i); }
177 void first(Arc& i) const { _graph->first(i); }
178 void first(Edge& i) const { _graph->first(i); }
179 void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
180 void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
181 void firstInc(Edge &i, bool &d, const Node &n) const {
182 _graph->firstInc(i, d, n);
185 void next(Node& i) const { _graph->next(i); }
186 void next(Arc& i) const { _graph->next(i); }
187 void next(Edge& i) const { _graph->next(i); }
188 void nextIn(Arc& i) const { _graph->nextIn(i); }
189 void nextOut(Arc& i) const { _graph->nextOut(i); }
190 void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
192 Node u(const Edge& e) const { return _graph->u(e); }
193 Node v(const Edge& e) const { return _graph->v(e); }
195 Node source(const Arc& a) const { return _graph->source(a); }
196 Node target(const Arc& a) const { return _graph->target(a); }
198 typedef NodeNumTagIndicator<Graph> NodeNumTag;
199 int nodeNum() const { return _graph->nodeNum(); }
201 typedef ArcNumTagIndicator<Graph> ArcNumTag;
202 int arcNum() const { return _graph->arcNum(); }
204 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
205 int edgeNum() const { return _graph->edgeNum(); }
207 typedef FindArcTagIndicator<Graph> FindArcTag;
208 Arc findArc(const Node& u, const Node& v,
209 const Arc& prev = INVALID) const {
210 return _graph->findArc(u, v, prev);
213 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
214 Edge findEdge(const Node& u, const Node& v,
215 const Edge& prev = INVALID) const {
216 return _graph->findEdge(u, v, prev);
219 Node addNode() { return _graph->addNode(); }
220 Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
222 void erase(const Node& i) { _graph->erase(i); }
223 void erase(const Edge& i) { _graph->erase(i); }
225 void clear() { _graph->clear(); }
227 bool direction(const Arc& a) const { return _graph->direction(a); }
228 Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
230 int id(const Node& v) const { return _graph->id(v); }
231 int id(const Arc& a) const { return _graph->id(a); }
232 int id(const Edge& e) const { return _graph->id(e); }
234 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
235 Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
236 Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
238 int maxNodeId() const { return _graph->maxNodeId(); }
239 int maxArcId() const { return _graph->maxArcId(); }
240 int maxEdgeId() const { return _graph->maxEdgeId(); }
242 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
243 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
245 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
246 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
248 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
249 EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
251 template <typename _Value>
252 class NodeMap : public Graph::template NodeMap<_Value> {
254 typedef typename Graph::template NodeMap<_Value> Parent;
255 explicit NodeMap(const GraphAdaptorBase<Graph>& adapter)
256 : Parent(*adapter._graph) {}
257 NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
258 : Parent(*adapter._graph, value) {}
261 NodeMap& operator=(const NodeMap& cmap) {
262 return operator=<NodeMap>(cmap);
265 template <typename CMap>
266 NodeMap& operator=(const CMap& cmap) {
267 Parent::operator=(cmap);
273 template <typename _Value>
274 class ArcMap : public Graph::template ArcMap<_Value> {
276 typedef typename Graph::template ArcMap<_Value> Parent;
277 explicit ArcMap(const GraphAdaptorBase<Graph>& adapter)
278 : Parent(*adapter._graph) {}
279 ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
280 : Parent(*adapter._graph, value) {}
283 ArcMap& operator=(const ArcMap& cmap) {
284 return operator=<ArcMap>(cmap);
287 template <typename CMap>
288 ArcMap& operator=(const CMap& cmap) {
289 Parent::operator=(cmap);
294 template <typename _Value>
295 class EdgeMap : public Graph::template EdgeMap<_Value> {
297 typedef typename Graph::template EdgeMap<_Value> Parent;
298 explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter)
299 : Parent(*adapter._graph) {}
300 EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
301 : Parent(*adapter._graph, value) {}
304 EdgeMap& operator=(const EdgeMap& cmap) {
305 return operator=<EdgeMap>(cmap);
308 template <typename CMap>
309 EdgeMap& operator=(const CMap& cmap) {
310 Parent::operator=(cmap);
317 template <typename _Digraph>
318 class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> {
320 typedef _Digraph Digraph;
321 typedef DigraphAdaptorBase<_Digraph> Parent;
323 ReverseDigraphBase() : Parent() { }
325 typedef typename Parent::Node Node;
326 typedef typename Parent::Arc Arc;
328 void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
329 void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
331 void nextIn(Arc& a) const { Parent::nextOut(a); }
332 void nextOut(Arc& a) const { Parent::nextIn(a); }
334 Node source(const Arc& a) const { return Parent::target(a); }
335 Node target(const Arc& a) const { return Parent::source(a); }
337 Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
339 typedef FindArcTagIndicator<Digraph> FindArcTag;
340 Arc findArc(const Node& u, const Node& v,
341 const Arc& prev = INVALID) const {
342 return Parent::findArc(v, u, prev);
347 /// \ingroup graph_adaptors
349 /// \brief A digraph adaptor which reverses the orientation of the arcs.
351 /// ReverseDigraph reverses the arcs in the adapted digraph. The
352 /// SubDigraph is conform to the \ref concepts::Digraph
353 /// "Digraph concept".
355 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
356 /// "Digraph concept". The type can be specified to be const.
357 template<typename _Digraph>
358 class ReverseDigraph :
359 public DigraphAdaptorExtender<ReverseDigraphBase<_Digraph> > {
361 typedef _Digraph Digraph;
362 typedef DigraphAdaptorExtender<
363 ReverseDigraphBase<_Digraph> > Parent;
368 /// \brief Constructor
370 /// Creates a reverse digraph adaptor for the given digraph
371 explicit ReverseDigraph(Digraph& digraph) {
372 Parent::setDigraph(digraph);
376 /// \brief Just gives back a reverse digraph adaptor
378 /// Just gives back a reverse digraph adaptor
379 template<typename Digraph>
380 ReverseDigraph<const Digraph> reverseDigraph(const Digraph& digraph) {
381 return ReverseDigraph<const Digraph>(digraph);
384 template <typename _Digraph, typename _NodeFilterMap,
385 typename _ArcFilterMap, bool _checked = true>
386 class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
388 typedef _Digraph Digraph;
389 typedef _NodeFilterMap NodeFilterMap;
390 typedef _ArcFilterMap ArcFilterMap;
392 typedef SubDigraphBase Adaptor;
393 typedef DigraphAdaptorBase<_Digraph> Parent;
395 NodeFilterMap* _node_filter;
396 ArcFilterMap* _arc_filter;
398 : Parent(), _node_filter(0), _arc_filter(0) { }
400 void setNodeFilterMap(NodeFilterMap& node_filter) {
401 _node_filter = &node_filter;
403 void setArcFilterMap(ArcFilterMap& arc_filter) {
404 _arc_filter = &arc_filter;
409 typedef typename Parent::Node Node;
410 typedef typename Parent::Arc Arc;
412 void first(Node& i) const {
414 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
417 void first(Arc& i) const {
419 while (i != INVALID && (!(*_arc_filter)[i]
420 || !(*_node_filter)[Parent::source(i)]
421 || !(*_node_filter)[Parent::target(i)]))
425 void firstIn(Arc& i, const Node& n) const {
426 Parent::firstIn(i, n);
427 while (i != INVALID && (!(*_arc_filter)[i]
428 || !(*_node_filter)[Parent::source(i)]))
432 void firstOut(Arc& i, const Node& n) const {
433 Parent::firstOut(i, n);
434 while (i != INVALID && (!(*_arc_filter)[i]
435 || !(*_node_filter)[Parent::target(i)]))
439 void next(Node& i) const {
441 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
444 void next(Arc& i) const {
446 while (i != INVALID && (!(*_arc_filter)[i]
447 || !(*_node_filter)[Parent::source(i)]
448 || !(*_node_filter)[Parent::target(i)]))
452 void nextIn(Arc& i) const {
454 while (i != INVALID && (!(*_arc_filter)[i]
455 || !(*_node_filter)[Parent::source(i)]))
459 void nextOut(Arc& i) const {
461 while (i != INVALID && (!(*_arc_filter)[i]
462 || !(*_node_filter)[Parent::target(i)]))
466 void hide(const Node& n) const { _node_filter->set(n, false); }
467 void hide(const Arc& a) const { _arc_filter->set(a, false); }
469 void unHide(const Node& n) const { _node_filter->set(n, true); }
470 void unHide(const Arc& a) const { _arc_filter->set(a, true); }
472 bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
473 bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; }
475 typedef False NodeNumTag;
476 typedef False ArcNumTag;
478 typedef FindArcTagIndicator<Digraph> FindArcTag;
479 Arc findArc(const Node& source, const Node& target,
480 const Arc& prev = INVALID) const {
481 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
484 Arc arc = Parent::findArc(source, target, prev);
485 while (arc != INVALID && !(*_arc_filter)[arc]) {
486 arc = Parent::findArc(source, target, arc);
491 template <typename _Value>
492 class NodeMap : public SubMapExtender<Adaptor,
493 typename Parent::template NodeMap<_Value> > {
495 typedef _Value Value;
496 typedef SubMapExtender<Adaptor, typename Parent::
497 template NodeMap<Value> > MapParent;
499 NodeMap(const Adaptor& adaptor)
500 : MapParent(adaptor) {}
501 NodeMap(const Adaptor& adaptor, const Value& value)
502 : MapParent(adaptor, value) {}
505 NodeMap& operator=(const NodeMap& cmap) {
506 return operator=<NodeMap>(cmap);
509 template <typename CMap>
510 NodeMap& operator=(const CMap& cmap) {
511 MapParent::operator=(cmap);
516 template <typename _Value>
517 class ArcMap : public SubMapExtender<Adaptor,
518 typename Parent::template ArcMap<_Value> > {
520 typedef _Value Value;
521 typedef SubMapExtender<Adaptor, typename Parent::
522 template ArcMap<Value> > MapParent;
524 ArcMap(const Adaptor& adaptor)
525 : MapParent(adaptor) {}
526 ArcMap(const Adaptor& adaptor, const Value& value)
527 : MapParent(adaptor, value) {}
530 ArcMap& operator=(const ArcMap& cmap) {
531 return operator=<ArcMap>(cmap);
534 template <typename CMap>
535 ArcMap& operator=(const CMap& cmap) {
536 MapParent::operator=(cmap);
543 template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap>
544 class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false>
545 : public DigraphAdaptorBase<_Digraph> {
547 typedef _Digraph Digraph;
548 typedef _NodeFilterMap NodeFilterMap;
549 typedef _ArcFilterMap ArcFilterMap;
551 typedef SubDigraphBase Adaptor;
552 typedef DigraphAdaptorBase<Digraph> Parent;
554 NodeFilterMap* _node_filter;
555 ArcFilterMap* _arc_filter;
557 : Parent(), _node_filter(0), _arc_filter(0) { }
559 void setNodeFilterMap(NodeFilterMap& node_filter) {
560 _node_filter = &node_filter;
562 void setArcFilterMap(ArcFilterMap& arc_filter) {
563 _arc_filter = &arc_filter;
568 typedef typename Parent::Node Node;
569 typedef typename Parent::Arc Arc;
571 void first(Node& i) const {
573 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
576 void first(Arc& i) const {
578 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
581 void firstIn(Arc& i, const Node& n) const {
582 Parent::firstIn(i, n);
583 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
586 void firstOut(Arc& i, const Node& n) const {
587 Parent::firstOut(i, n);
588 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
591 void next(Node& i) const {
593 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
595 void next(Arc& i) const {
597 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
599 void nextIn(Arc& i) const {
601 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
604 void nextOut(Arc& i) const {
606 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
609 void hide(const Node& n) const { _node_filter->set(n, false); }
610 void hide(const Arc& e) const { _arc_filter->set(e, false); }
612 void unHide(const Node& n) const { _node_filter->set(n, true); }
613 void unHide(const Arc& e) const { _arc_filter->set(e, true); }
615 bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
616 bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; }
618 typedef False NodeNumTag;
619 typedef False ArcNumTag;
621 typedef FindArcTagIndicator<Digraph> FindArcTag;
622 Arc findArc(const Node& source, const Node& target,
623 const Arc& prev = INVALID) const {
624 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
627 Arc arc = Parent::findArc(source, target, prev);
628 while (arc != INVALID && !(*_arc_filter)[arc]) {
629 arc = Parent::findArc(source, target, arc);
634 template <typename _Value>
635 class NodeMap : public SubMapExtender<Adaptor,
636 typename Parent::template NodeMap<_Value> > {
638 typedef _Value Value;
639 typedef SubMapExtender<Adaptor, typename Parent::
640 template NodeMap<Value> > MapParent;
642 NodeMap(const Adaptor& adaptor)
643 : MapParent(adaptor) {}
644 NodeMap(const Adaptor& adaptor, const Value& value)
645 : MapParent(adaptor, value) {}
648 NodeMap& operator=(const NodeMap& cmap) {
649 return operator=<NodeMap>(cmap);
652 template <typename CMap>
653 NodeMap& operator=(const CMap& cmap) {
654 MapParent::operator=(cmap);
659 template <typename _Value>
660 class ArcMap : public SubMapExtender<Adaptor,
661 typename Parent::template ArcMap<_Value> > {
663 typedef _Value Value;
664 typedef SubMapExtender<Adaptor, typename Parent::
665 template ArcMap<Value> > MapParent;
667 ArcMap(const Adaptor& adaptor)
668 : MapParent(adaptor) {}
669 ArcMap(const Adaptor& adaptor, const Value& value)
670 : MapParent(adaptor, value) {}
673 ArcMap& operator=(const ArcMap& cmap) {
674 return operator=<ArcMap>(cmap);
677 template <typename CMap>
678 ArcMap& operator=(const CMap& cmap) {
679 MapParent::operator=(cmap);
686 /// \ingroup graph_adaptors
688 /// \brief An adaptor for hiding nodes and arcs in a digraph
690 /// SubDigraph hides nodes and arcs in a digraph. A bool node map
691 /// and a bool arc map must be specified, which define the filters
692 /// for nodes and arcs. Just the nodes and arcs with true value are
693 /// shown in the subdigraph. The SubDigraph is conform to the \ref
694 /// concepts::Digraph "Digraph concept". If the \c _checked parameter
695 /// is true, then the arcs incident to filtered nodes are also
698 /// \tparam _Digraph It must be conform to the \ref
699 /// concepts::Digraph "Digraph concept". The type can be specified
701 /// \tparam _NodeFilterMap A bool valued node map of the the adapted digraph.
702 /// \tparam _ArcFilterMap A bool valued arc map of the the adapted digraph.
703 /// \tparam _checked If the parameter is false then the arc filtering
704 /// is not checked with respect to node filter. Otherwise, each arc
705 /// is automatically filtered, which is incident to a filtered node.
709 template<typename _Digraph,
710 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
711 typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>,
712 bool _checked = true>
714 : public DigraphAdaptorExtender<
715 SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, _checked> > {
717 typedef _Digraph Digraph;
718 typedef _NodeFilterMap NodeFilterMap;
719 typedef _ArcFilterMap ArcFilterMap;
721 typedef DigraphAdaptorExtender<
722 SubDigraphBase<Digraph, NodeFilterMap, ArcFilterMap, _checked> >
725 typedef typename Parent::Node Node;
726 typedef typename Parent::Arc Arc;
732 /// \brief Constructor
734 /// Creates a subdigraph for the given digraph with
735 /// given node and arc map filters.
736 SubDigraph(Digraph& digraph, NodeFilterMap& node_filter,
737 ArcFilterMap& arc_filter) {
739 setNodeFilterMap(node_filter);
740 setArcFilterMap(arc_filter);
743 /// \brief Hides the node of the graph
745 /// This function hides \c n in the digraph, i.e. the iteration
746 /// jumps over it. This is done by simply setting the value of \c n
747 /// to be false in the corresponding node-map.
748 void hide(const Node& n) const { Parent::hide(n); }
750 /// \brief Hides the arc of the graph
752 /// This function hides \c a in the digraph, i.e. the iteration
753 /// jumps over it. This is done by simply setting the value of \c a
754 /// to be false in the corresponding arc-map.
755 void hide(const Arc& a) const { Parent::hide(a); }
757 /// \brief Unhides the node of the graph
759 /// The value of \c n is set to be true in the node-map which stores
760 /// hide information. If \c n was hidden previuosly, then it is shown
762 void unHide(const Node& n) const { Parent::unHide(n); }
764 /// \brief Unhides the arc of the graph
766 /// The value of \c a is set to be true in the arc-map which stores
767 /// hide information. If \c a was hidden previuosly, then it is shown
769 void unHide(const Arc& a) const { Parent::unHide(a); }
771 /// \brief Returns true if \c n is hidden.
773 /// Returns true if \c n is hidden.
775 bool hidden(const Node& n) const { return Parent::hidden(n); }
777 /// \brief Returns true if \c a is hidden.
779 /// Returns true if \c a is hidden.
781 bool hidden(const Arc& a) const { return Parent::hidden(a); }
785 /// \brief Just gives back a subdigraph
787 /// Just gives back a subdigraph
788 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
789 SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap>
790 subDigraph(const Digraph& digraph, NodeFilterMap& nfm, ArcFilterMap& afm) {
791 return SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap>
795 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
796 SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap>
797 subDigraph(const Digraph& digraph,
798 const NodeFilterMap& nfm, ArcFilterMap& afm) {
799 return SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap>
803 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
804 SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap>
805 subDigraph(const Digraph& digraph,
806 NodeFilterMap& nfm, const ArcFilterMap& afm) {
807 return SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap>
811 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
812 SubDigraph<const Digraph, const NodeFilterMap, const ArcFilterMap>
813 subDigraph(const Digraph& digraph,
814 const NodeFilterMap& nfm, const ArcFilterMap& afm) {
815 return SubDigraph<const Digraph, const NodeFilterMap,
816 const ArcFilterMap>(digraph, nfm, afm);
820 template <typename _Graph, typename _NodeFilterMap,
821 typename _EdgeFilterMap, bool _checked = true>
822 class SubGraphBase : public GraphAdaptorBase<_Graph> {
824 typedef _Graph Graph;
825 typedef _NodeFilterMap NodeFilterMap;
826 typedef _EdgeFilterMap EdgeFilterMap;
828 typedef SubGraphBase Adaptor;
829 typedef GraphAdaptorBase<_Graph> Parent;
832 NodeFilterMap* _node_filter_map;
833 EdgeFilterMap* _edge_filter_map;
836 : Parent(), _node_filter_map(0), _edge_filter_map(0) { }
838 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
839 _node_filter_map=&node_filter_map;
841 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
842 _edge_filter_map=&edge_filter_map;
847 typedef typename Parent::Node Node;
848 typedef typename Parent::Arc Arc;
849 typedef typename Parent::Edge Edge;
851 void first(Node& i) const {
853 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
856 void first(Arc& i) const {
858 while (i!=INVALID && (!(*_edge_filter_map)[i]
859 || !(*_node_filter_map)[Parent::source(i)]
860 || !(*_node_filter_map)[Parent::target(i)]))
864 void first(Edge& i) const {
866 while (i!=INVALID && (!(*_edge_filter_map)[i]
867 || !(*_node_filter_map)[Parent::u(i)]
868 || !(*_node_filter_map)[Parent::v(i)]))
872 void firstIn(Arc& i, const Node& n) const {
873 Parent::firstIn(i, n);
874 while (i!=INVALID && (!(*_edge_filter_map)[i]
875 || !(*_node_filter_map)[Parent::source(i)]))
879 void firstOut(Arc& i, const Node& n) const {
880 Parent::firstOut(i, n);
881 while (i!=INVALID && (!(*_edge_filter_map)[i]
882 || !(*_node_filter_map)[Parent::target(i)]))
886 void firstInc(Edge& i, bool& d, const Node& n) const {
887 Parent::firstInc(i, d, n);
888 while (i!=INVALID && (!(*_edge_filter_map)[i]
889 || !(*_node_filter_map)[Parent::u(i)]
890 || !(*_node_filter_map)[Parent::v(i)]))
891 Parent::nextInc(i, d);
894 void next(Node& i) const {
896 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
899 void next(Arc& i) const {
901 while (i!=INVALID && (!(*_edge_filter_map)[i]
902 || !(*_node_filter_map)[Parent::source(i)]
903 || !(*_node_filter_map)[Parent::target(i)]))
907 void next(Edge& i) const {
909 while (i!=INVALID && (!(*_edge_filter_map)[i]
910 || !(*_node_filter_map)[Parent::u(i)]
911 || !(*_node_filter_map)[Parent::v(i)]))
915 void nextIn(Arc& i) const {
917 while (i!=INVALID && (!(*_edge_filter_map)[i]
918 || !(*_node_filter_map)[Parent::source(i)]))
922 void nextOut(Arc& i) const {
924 while (i!=INVALID && (!(*_edge_filter_map)[i]
925 || !(*_node_filter_map)[Parent::target(i)]))
929 void nextInc(Edge& i, bool& d) const {
930 Parent::nextInc(i, d);
931 while (i!=INVALID && (!(*_edge_filter_map)[i]
932 || !(*_node_filter_map)[Parent::u(i)]
933 || !(*_node_filter_map)[Parent::v(i)]))
934 Parent::nextInc(i, d);
937 void hide(const Node& n) const { _node_filter_map->set(n, false); }
938 void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
940 void unHide(const Node& n) const { _node_filter_map->set(n, true); }
941 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
943 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
944 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
946 typedef False NodeNumTag;
947 typedef False ArcNumTag;
948 typedef False EdgeNumTag;
950 typedef FindArcTagIndicator<Graph> FindArcTag;
951 Arc findArc(const Node& u, const Node& v,
952 const Arc& prev = INVALID) const {
953 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
956 Arc arc = Parent::findArc(u, v, prev);
957 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
958 arc = Parent::findArc(u, v, arc);
963 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
964 Edge findEdge(const Node& u, const Node& v,
965 const Edge& prev = INVALID) const {
966 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
969 Edge edge = Parent::findEdge(u, v, prev);
970 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
971 edge = Parent::findEdge(u, v, edge);
976 template <typename _Value>
977 class NodeMap : public SubMapExtender<Adaptor,
978 typename Parent::template NodeMap<_Value> > {
980 typedef _Value Value;
981 typedef SubMapExtender<Adaptor, typename Parent::
982 template NodeMap<Value> > MapParent;
984 NodeMap(const Adaptor& adaptor)
985 : MapParent(adaptor) {}
986 NodeMap(const Adaptor& adaptor, const Value& value)
987 : MapParent(adaptor, value) {}
990 NodeMap& operator=(const NodeMap& cmap) {
991 return operator=<NodeMap>(cmap);
994 template <typename CMap>
995 NodeMap& operator=(const CMap& cmap) {
996 MapParent::operator=(cmap);
1001 template <typename _Value>
1002 class ArcMap : public SubMapExtender<Adaptor,
1003 typename Parent::template ArcMap<_Value> > {
1005 typedef _Value Value;
1006 typedef SubMapExtender<Adaptor, typename Parent::
1007 template ArcMap<Value> > MapParent;
1009 ArcMap(const Adaptor& adaptor)
1010 : MapParent(adaptor) {}
1011 ArcMap(const Adaptor& adaptor, const Value& value)
1012 : MapParent(adaptor, value) {}
1015 ArcMap& operator=(const ArcMap& cmap) {
1016 return operator=<ArcMap>(cmap);
1019 template <typename CMap>
1020 ArcMap& operator=(const CMap& cmap) {
1021 MapParent::operator=(cmap);
1026 template <typename _Value>
1027 class EdgeMap : public SubMapExtender<Adaptor,
1028 typename Parent::template EdgeMap<_Value> > {
1030 typedef _Value Value;
1031 typedef SubMapExtender<Adaptor, typename Parent::
1032 template EdgeMap<Value> > MapParent;
1034 EdgeMap(const Adaptor& adaptor)
1035 : MapParent(adaptor) {}
1037 EdgeMap(const Adaptor& adaptor, const Value& value)
1038 : MapParent(adaptor, value) {}
1041 EdgeMap& operator=(const EdgeMap& cmap) {
1042 return operator=<EdgeMap>(cmap);
1045 template <typename CMap>
1046 EdgeMap& operator=(const CMap& cmap) {
1047 MapParent::operator=(cmap);
1054 template <typename _Graph, typename _NodeFilterMap, typename _EdgeFilterMap>
1055 class SubGraphBase<_Graph, _NodeFilterMap, _EdgeFilterMap, false>
1056 : public GraphAdaptorBase<_Graph> {
1058 typedef _Graph Graph;
1059 typedef _NodeFilterMap NodeFilterMap;
1060 typedef _EdgeFilterMap EdgeFilterMap;
1062 typedef SubGraphBase Adaptor;
1063 typedef GraphAdaptorBase<_Graph> Parent;
1065 NodeFilterMap* _node_filter_map;
1066 EdgeFilterMap* _edge_filter_map;
1067 SubGraphBase() : Parent(),
1068 _node_filter_map(0), _edge_filter_map(0) { }
1070 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
1071 _node_filter_map=&node_filter_map;
1073 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
1074 _edge_filter_map=&edge_filter_map;
1079 typedef typename Parent::Node Node;
1080 typedef typename Parent::Arc Arc;
1081 typedef typename Parent::Edge Edge;
1083 void first(Node& i) const {
1085 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1088 void first(Arc& i) const {
1090 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1093 void first(Edge& i) const {
1095 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1098 void firstIn(Arc& i, const Node& n) const {
1099 Parent::firstIn(i, n);
1100 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1103 void firstOut(Arc& i, const Node& n) const {
1104 Parent::firstOut(i, n);
1105 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1108 void firstInc(Edge& i, bool& d, const Node& n) const {
1109 Parent::firstInc(i, d, n);
1110 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1113 void next(Node& i) const {
1115 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1117 void next(Arc& i) const {
1119 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1121 void next(Edge& i) const {
1123 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1125 void nextIn(Arc& i) const {
1127 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1130 void nextOut(Arc& i) const {
1132 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1134 void nextInc(Edge& i, bool& d) const {
1135 Parent::nextInc(i, d);
1136 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1139 void hide(const Node& n) const { _node_filter_map->set(n, false); }
1140 void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
1142 void unHide(const Node& n) const { _node_filter_map->set(n, true); }
1143 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
1145 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
1146 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
1148 typedef False NodeNumTag;
1149 typedef False ArcNumTag;
1150 typedef False EdgeNumTag;
1152 typedef FindArcTagIndicator<Graph> FindArcTag;
1153 Arc findArc(const Node& u, const Node& v,
1154 const Arc& prev = INVALID) const {
1155 Arc arc = Parent::findArc(u, v, prev);
1156 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
1157 arc = Parent::findArc(u, v, arc);
1162 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1163 Edge findEdge(const Node& u, const Node& v,
1164 const Edge& prev = INVALID) const {
1165 Edge edge = Parent::findEdge(u, v, prev);
1166 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1167 edge = Parent::findEdge(u, v, edge);
1172 template <typename _Value>
1173 class NodeMap : public SubMapExtender<Adaptor,
1174 typename Parent::template NodeMap<_Value> > {
1176 typedef _Value Value;
1177 typedef SubMapExtender<Adaptor, typename Parent::
1178 template NodeMap<Value> > MapParent;
1180 NodeMap(const Adaptor& adaptor)
1181 : MapParent(adaptor) {}
1182 NodeMap(const Adaptor& adaptor, const Value& value)
1183 : MapParent(adaptor, value) {}
1186 NodeMap& operator=(const NodeMap& cmap) {
1187 return operator=<NodeMap>(cmap);
1190 template <typename CMap>
1191 NodeMap& operator=(const CMap& cmap) {
1192 MapParent::operator=(cmap);
1197 template <typename _Value>
1198 class ArcMap : public SubMapExtender<Adaptor,
1199 typename Parent::template ArcMap<_Value> > {
1201 typedef _Value Value;
1202 typedef SubMapExtender<Adaptor, typename Parent::
1203 template ArcMap<Value> > MapParent;
1205 ArcMap(const Adaptor& adaptor)
1206 : MapParent(adaptor) {}
1207 ArcMap(const Adaptor& adaptor, const Value& value)
1208 : MapParent(adaptor, value) {}
1211 ArcMap& operator=(const ArcMap& cmap) {
1212 return operator=<ArcMap>(cmap);
1215 template <typename CMap>
1216 ArcMap& operator=(const CMap& cmap) {
1217 MapParent::operator=(cmap);
1222 template <typename _Value>
1223 class EdgeMap : public SubMapExtender<Adaptor,
1224 typename Parent::template EdgeMap<_Value> > {
1226 typedef _Value Value;
1227 typedef SubMapExtender<Adaptor, typename Parent::
1228 template EdgeMap<Value> > MapParent;
1230 EdgeMap(const Adaptor& adaptor)
1231 : MapParent(adaptor) {}
1233 EdgeMap(const Adaptor& adaptor, const _Value& value)
1234 : MapParent(adaptor, value) {}
1237 EdgeMap& operator=(const EdgeMap& cmap) {
1238 return operator=<EdgeMap>(cmap);
1241 template <typename CMap>
1242 EdgeMap& operator=(const CMap& cmap) {
1243 MapParent::operator=(cmap);
1250 /// \ingroup graph_adaptors
1252 /// \brief A graph adaptor for hiding nodes and edges in an
1253 /// undirected graph.
1255 /// SubGraph hides nodes and edges in a graph. A bool node map and a
1256 /// bool edge map must be specified, which define the filters for
1257 /// nodes and edges. Just the nodes and edges with true value are
1258 /// shown in the subgraph. The SubGraph is conform to the \ref
1259 /// concepts::Graph "Graph concept". If the \c _checked parameter is
1260 /// true, then the edges incident to filtered nodes are also
1263 /// \tparam _Graph It must be conform to the \ref
1264 /// concepts::Graph "Graph concept". The type can be specified
1266 /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph.
1267 /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted graph.
1268 /// \tparam _checked If the parameter is false then the edge filtering
1269 /// is not checked with respect to node filter. Otherwise, each edge
1270 /// is automatically filtered, which is incident to a filtered node.
1272 /// \see FilterNodes
1273 /// \see FilterEdges
1274 template<typename _Graph, typename NodeFilterMap,
1275 typename EdgeFilterMap, bool _checked = true>
1277 : public GraphAdaptorExtender<
1278 SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, _checked> > {
1280 typedef _Graph Graph;
1281 typedef GraphAdaptorExtender<
1282 SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
1284 typedef typename Parent::Node Node;
1285 typedef typename Parent::Edge Edge;
1291 /// \brief Constructor
1293 /// Creates a subgraph for the given graph with given node and
1294 /// edge map filters.
1295 SubGraph(Graph& _graph, NodeFilterMap& node_filter_map,
1296 EdgeFilterMap& edge_filter_map) {
1298 setNodeFilterMap(node_filter_map);
1299 setEdgeFilterMap(edge_filter_map);
1302 /// \brief Hides the node of the graph
1304 /// This function hides \c n in the graph, i.e. the iteration
1305 /// jumps over it. This is done by simply setting the value of \c n
1306 /// to be false in the corresponding node-map.
1307 void hide(const Node& n) const { Parent::hide(n); }
1309 /// \brief Hides the edge of the graph
1311 /// This function hides \c e in the graph, i.e. the iteration
1312 /// jumps over it. This is done by simply setting the value of \c e
1313 /// to be false in the corresponding edge-map.
1314 void hide(const Edge& e) const { Parent::hide(e); }
1316 /// \brief Unhides the node of the graph
1318 /// The value of \c n is set to be true in the node-map which stores
1319 /// hide information. If \c n was hidden previuosly, then it is shown
1321 void unHide(const Node& n) const { Parent::unHide(n); }
1323 /// \brief Unhides the edge of the graph
1325 /// The value of \c e is set to be true in the edge-map which stores
1326 /// hide information. If \c e was hidden previuosly, then it is shown
1328 void unHide(const Edge& e) const { Parent::unHide(e); }
1330 /// \brief Returns true if \c n is hidden.
1332 /// Returns true if \c n is hidden.
1334 bool hidden(const Node& n) const { return Parent::hidden(n); }
1336 /// \brief Returns true if \c e is hidden.
1338 /// Returns true if \c e is hidden.
1340 bool hidden(const Edge& e) const { return Parent::hidden(e); }
1343 /// \brief Just gives back a subgraph
1345 /// Just gives back a subgraph
1346 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1347 SubGraph<const Graph, NodeFilterMap, ArcFilterMap>
1348 subGraph(const Graph& graph, NodeFilterMap& nfm, ArcFilterMap& efm) {
1349 return SubGraph<const Graph, NodeFilterMap, ArcFilterMap>(graph, nfm, efm);
1352 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1353 SubGraph<const Graph, const NodeFilterMap, ArcFilterMap>
1354 subGraph(const Graph& graph,
1355 const NodeFilterMap& nfm, ArcFilterMap& efm) {
1356 return SubGraph<const Graph, const NodeFilterMap, ArcFilterMap>
1360 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1361 SubGraph<const Graph, NodeFilterMap, const ArcFilterMap>
1362 subGraph(const Graph& graph,
1363 NodeFilterMap& nfm, const ArcFilterMap& efm) {
1364 return SubGraph<const Graph, NodeFilterMap, const ArcFilterMap>
1368 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1369 SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap>
1370 subGraph(const Graph& graph,
1371 const NodeFilterMap& nfm, const ArcFilterMap& efm) {
1372 return SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap>
1376 /// \ingroup graph_adaptors
1378 /// \brief An adaptor for hiding nodes from a digraph or a graph.
1380 /// FilterNodes adaptor hides nodes in a graph or a digraph. A bool
1381 /// node map must be specified, which defines the filters for
1382 /// nodes. Just the unfiltered nodes and the arcs or edges incident
1383 /// to unfiltered nodes are shown in the subdigraph or subgraph. The
1384 /// FilterNodes is conform to the \ref concepts::Digraph
1385 /// "Digraph concept" or \ref concepts::Graph "Graph concept" depending
1386 /// on the \c _Digraph template parameter. If the \c _checked
1387 /// parameter is true, then the arc or edges incident to filtered nodes
1388 /// are also filtered out.
1390 /// \tparam _Digraph It must be conform to the \ref
1391 /// concepts::Digraph "Digraph concept" or \ref concepts::Graph
1392 /// "Graph concept". The type can be specified to be const.
1393 /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph.
1394 /// \tparam _checked If the parameter is false then the arc or edge
1395 /// filtering is not checked with respect to node filter. In this
1396 /// case just isolated nodes can be filtered out from the
1399 template<typename _Digraph,
1400 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
1401 bool _checked = true>
1403 template<typename _Digraph,
1404 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
1405 bool _checked = true,
1406 typename Enable = void>
1409 : public SubDigraph<_Digraph, _NodeFilterMap,
1410 ConstMap<typename _Digraph::Arc, bool>, _checked> {
1413 typedef _Digraph Digraph;
1414 typedef _NodeFilterMap NodeFilterMap;
1416 typedef SubDigraph<Digraph, NodeFilterMap,
1417 ConstMap<typename Digraph::Arc, bool>, _checked>
1420 typedef typename Parent::Node Node;
1423 ConstMap<typename Digraph::Arc, bool> const_true_map;
1425 FilterNodes() : const_true_map(true) {
1426 Parent::setArcFilterMap(const_true_map);
1431 /// \brief Constructor
1433 /// Creates an adaptor for the given digraph or graph with
1434 /// given node filter map.
1435 FilterNodes(Digraph& _digraph, NodeFilterMap& node_filter) :
1436 Parent(), const_true_map(true) {
1437 Parent::setDigraph(_digraph);
1438 Parent::setNodeFilterMap(node_filter);
1439 Parent::setArcFilterMap(const_true_map);
1442 /// \brief Hides the node of the graph
1444 /// This function hides \c n in the digraph or graph, i.e. the iteration
1445 /// jumps over it. This is done by simply setting the value of \c n
1446 /// to be false in the corresponding node map.
1447 void hide(const Node& n) const { Parent::hide(n); }
1449 /// \brief Unhides the node of the graph
1451 /// The value of \c n is set to be true in the node-map which stores
1452 /// hide information. If \c n was hidden previuosly, then it is shown
1454 void unHide(const Node& n) const { Parent::unHide(n); }
1456 /// \brief Returns true if \c n is hidden.
1458 /// Returns true if \c n is hidden.
1460 bool hidden(const Node& n) const { return Parent::hidden(n); }
1464 template<typename _Graph, typename _NodeFilterMap, bool _checked>
1465 class FilterNodes<_Graph, _NodeFilterMap, _checked,
1466 typename enable_if<UndirectedTagIndicator<_Graph> >::type>
1467 : public SubGraph<_Graph, _NodeFilterMap,
1468 ConstMap<typename _Graph::Edge, bool>, _checked> {
1470 typedef _Graph Graph;
1471 typedef _NodeFilterMap NodeFilterMap;
1472 typedef SubGraph<Graph, NodeFilterMap,
1473 ConstMap<typename Graph::Edge, bool> > Parent;
1475 typedef typename Parent::Node Node;
1477 ConstMap<typename Graph::Edge, bool> const_true_map;
1479 FilterNodes() : const_true_map(true) {
1480 Parent::setEdgeFilterMap(const_true_map);
1485 FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) :
1486 Parent(), const_true_map(true) {
1487 Parent::setGraph(_graph);
1488 Parent::setNodeFilterMap(node_filter_map);
1489 Parent::setEdgeFilterMap(const_true_map);
1492 void hide(const Node& n) const { Parent::hide(n); }
1493 void unHide(const Node& n) const { Parent::unHide(n); }
1494 bool hidden(const Node& n) const { return Parent::hidden(n); }
1499 /// \brief Just gives back a FilterNodes adaptor
1501 /// Just gives back a FilterNodes adaptor
1502 template<typename Digraph, typename NodeFilterMap>
1503 FilterNodes<const Digraph, NodeFilterMap>
1504 filterNodes(const Digraph& digraph, NodeFilterMap& nfm) {
1505 return FilterNodes<const Digraph, NodeFilterMap>(digraph, nfm);
1508 template<typename Digraph, typename NodeFilterMap>
1509 FilterNodes<const Digraph, const NodeFilterMap>
1510 filterNodes(const Digraph& digraph, const NodeFilterMap& nfm) {
1511 return FilterNodes<const Digraph, const NodeFilterMap>(digraph, nfm);
1514 /// \ingroup graph_adaptors
1516 /// \brief An adaptor for hiding arcs from a digraph.
1518 /// FilterArcs adaptor hides arcs in a digraph. A bool arc map must
1519 /// be specified, which defines the filters for arcs. Just the
1520 /// unfiltered arcs are shown in the subdigraph. The FilterArcs is
1521 /// conform to the \ref concepts::Digraph "Digraph concept".
1523 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
1524 /// "Digraph concept". The type can be specified to be const.
1525 /// \tparam _ArcFilterMap A bool valued arc map of the the adapted
1527 template<typename _Digraph, typename _ArcFilterMap>
1529 public SubDigraph<_Digraph, ConstMap<typename _Digraph::Node, bool>,
1530 _ArcFilterMap, false> {
1532 typedef _Digraph Digraph;
1533 typedef _ArcFilterMap ArcFilterMap;
1535 typedef SubDigraph<Digraph, ConstMap<typename Digraph::Node, bool>,
1536 ArcFilterMap, false> Parent;
1538 typedef typename Parent::Arc Arc;
1541 ConstMap<typename Digraph::Node, bool> const_true_map;
1543 FilterArcs() : const_true_map(true) {
1544 Parent::setNodeFilterMap(const_true_map);
1549 /// \brief Constructor
1551 /// Creates a FilterArcs adaptor for the given graph with
1552 /// given arc map filter.
1553 FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter)
1554 : Parent(), const_true_map(true) {
1555 Parent::setDigraph(digraph);
1556 Parent::setNodeFilterMap(const_true_map);
1557 Parent::setArcFilterMap(arc_filter);
1560 /// \brief Hides the arc of the graph
1562 /// This function hides \c a in the graph, i.e. the iteration
1563 /// jumps over it. This is done by simply setting the value of \c a
1564 /// to be false in the corresponding arc map.
1565 void hide(const Arc& a) const { Parent::hide(a); }
1567 /// \brief Unhides the arc of the graph
1569 /// The value of \c a is set to be true in the arc-map which stores
1570 /// hide information. If \c a was hidden previuosly, then it is shown
1572 void unHide(const Arc& a) const { Parent::unHide(a); }
1574 /// \brief Returns true if \c a is hidden.
1576 /// Returns true if \c a is hidden.
1578 bool hidden(const Arc& a) const { return Parent::hidden(a); }
1582 /// \brief Just gives back an FilterArcs adaptor
1584 /// Just gives back an FilterArcs adaptor
1585 template<typename Digraph, typename ArcFilterMap>
1586 FilterArcs<const Digraph, ArcFilterMap>
1587 filterArcs(const Digraph& digraph, ArcFilterMap& afm) {
1588 return FilterArcs<const Digraph, ArcFilterMap>(digraph, afm);
1591 template<typename Digraph, typename ArcFilterMap>
1592 FilterArcs<const Digraph, const ArcFilterMap>
1593 filterArcs(const Digraph& digraph, const ArcFilterMap& afm) {
1594 return FilterArcs<const Digraph, const ArcFilterMap>(digraph, afm);
1597 /// \ingroup graph_adaptors
1599 /// \brief An adaptor for hiding edges from a graph.
1601 /// FilterEdges adaptor hides edges in a digraph. A bool edge map must
1602 /// be specified, which defines the filters for edges. Just the
1603 /// unfiltered edges are shown in the subdigraph. The FilterEdges is
1604 /// conform to the \ref concepts::Graph "Graph concept".
1606 /// \tparam _Graph It must be conform to the \ref concepts::Graph
1607 /// "Graph concept". The type can be specified to be const.
1608 /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted
1610 template<typename _Graph, typename _EdgeFilterMap>
1612 public SubGraph<_Graph, ConstMap<typename _Graph::Node,bool>,
1613 _EdgeFilterMap, false> {
1615 typedef _Graph Graph;
1616 typedef _EdgeFilterMap EdgeFilterMap;
1617 typedef SubGraph<Graph, ConstMap<typename Graph::Node,bool>,
1618 EdgeFilterMap, false> Parent;
1619 typedef typename Parent::Edge Edge;
1621 ConstMap<typename Graph::Node, bool> const_true_map;
1623 FilterEdges() : const_true_map(true) {
1624 Parent::setNodeFilterMap(const_true_map);
1629 /// \brief Constructor
1631 /// Creates a FilterEdges adaptor for the given graph with
1632 /// given edge map filters.
1633 FilterEdges(Graph& _graph, EdgeFilterMap& edge_filter_map) :
1634 Parent(), const_true_map(true) {
1635 Parent::setGraph(_graph);
1636 Parent::setNodeFilterMap(const_true_map);
1637 Parent::setEdgeFilterMap(edge_filter_map);
1640 /// \brief Hides the edge of the graph
1642 /// This function hides \c e in the graph, i.e. the iteration
1643 /// jumps over it. This is done by simply setting the value of \c e
1644 /// to be false in the corresponding edge-map.
1645 void hide(const Edge& e) const { Parent::hide(e); }
1647 /// \brief Unhides the edge of the graph
1649 /// The value of \c e is set to be true in the edge-map which stores
1650 /// hide information. If \c e was hidden previuosly, then it is shown
1652 void unHide(const Edge& e) const { Parent::unHide(e); }
1654 /// \brief Returns true if \c e is hidden.
1656 /// Returns true if \c e is hidden.
1658 bool hidden(const Edge& e) const { return Parent::hidden(e); }
1662 /// \brief Just gives back a FilterEdges adaptor
1664 /// Just gives back a FilterEdges adaptor
1665 template<typename Graph, typename EdgeFilterMap>
1666 FilterEdges<const Graph, EdgeFilterMap>
1667 filterEdges(const Graph& graph, EdgeFilterMap& efm) {
1668 return FilterEdges<const Graph, EdgeFilterMap>(graph, efm);
1671 template<typename Graph, typename EdgeFilterMap>
1672 FilterEdges<const Graph, const EdgeFilterMap>
1673 filterEdges(const Graph& graph, const EdgeFilterMap& efm) {
1674 return FilterEdges<const Graph, const EdgeFilterMap>(graph, efm);
1677 template <typename _Digraph>
1678 class UndirectorBase {
1680 typedef _Digraph Digraph;
1681 typedef UndirectorBase Adaptor;
1683 typedef True UndirectedTag;
1685 typedef typename Digraph::Arc Edge;
1686 typedef typename Digraph::Node Node;
1688 class Arc : public Edge {
1689 friend class UndirectorBase;
1693 Arc(const Edge& edge, bool forward) :
1694 Edge(edge), _forward(forward) {}
1699 Arc(Invalid) : Edge(INVALID), _forward(true) {}
1701 bool operator==(const Arc &other) const {
1702 return _forward == other._forward &&
1703 static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1705 bool operator!=(const Arc &other) const {
1706 return _forward != other._forward ||
1707 static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1709 bool operator<(const Arc &other) const {
1710 return _forward < other._forward ||
1711 (_forward == other._forward &&
1712 static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1718 void first(Node& n) const {
1722 void next(Node& n) const {
1726 void first(Arc& a) const {
1731 void next(Arc& a) const {
1740 void first(Edge& e) const {
1744 void next(Edge& e) const {
1748 void firstOut(Arc& a, const Node& n) const {
1749 _digraph->firstIn(a, n);
1750 if( static_cast<const Edge&>(a) != INVALID ) {
1753 _digraph->firstOut(a, n);
1757 void nextOut(Arc &a) const {
1759 Node n = _digraph->target(a);
1760 _digraph->nextIn(a);
1761 if (static_cast<const Edge&>(a) == INVALID ) {
1762 _digraph->firstOut(a, n);
1767 _digraph->nextOut(a);
1771 void firstIn(Arc &a, const Node &n) const {
1772 _digraph->firstOut(a, n);
1773 if (static_cast<const Edge&>(a) != INVALID ) {
1776 _digraph->firstIn(a, n);
1780 void nextIn(Arc &a) const {
1782 Node n = _digraph->source(a);
1783 _digraph->nextOut(a);
1784 if( static_cast<const Edge&>(a) == INVALID ) {
1785 _digraph->firstIn(a, n);
1790 _digraph->nextIn(a);
1794 void firstInc(Edge &e, bool &d, const Node &n) const {
1796 _digraph->firstOut(e, n);
1797 if (e != INVALID) return;
1799 _digraph->firstIn(e, n);
1802 void nextInc(Edge &e, bool &d) const {
1804 Node s = _digraph->source(e);
1805 _digraph->nextOut(e);
1806 if (e != INVALID) return;
1808 _digraph->firstIn(e, s);
1810 _digraph->nextIn(e);
1814 Node u(const Edge& e) const {
1815 return _digraph->source(e);
1818 Node v(const Edge& e) const {
1819 return _digraph->target(e);
1822 Node source(const Arc &a) const {
1823 return a._forward ? _digraph->source(a) : _digraph->target(a);
1826 Node target(const Arc &a) const {
1827 return a._forward ? _digraph->target(a) : _digraph->source(a);
1830 static Arc direct(const Edge &e, bool d) {
1833 Arc direct(const Edge &e, const Node& n) const {
1834 return Arc(e, _digraph->source(e) == n);
1837 static bool direction(const Arc &a) { return a._forward; }
1839 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1840 Arc arcFromId(int ix) const {
1841 return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1843 Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1845 int id(const Node &n) const { return _digraph->id(n); }
1846 int id(const Arc &a) const {
1847 return (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1849 int id(const Edge &e) const { return _digraph->id(e); }
1851 int maxNodeId() const { return _digraph->maxNodeId(); }
1852 int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
1853 int maxEdgeId() const { return _digraph->maxArcId(); }
1855 Node addNode() { return _digraph->addNode(); }
1856 Edge addEdge(const Node& u, const Node& v) {
1857 return _digraph->addArc(u, v);
1860 void erase(const Node& i) { _digraph->erase(i); }
1861 void erase(const Edge& i) { _digraph->erase(i); }
1863 void clear() { _digraph->clear(); }
1865 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
1866 int nodeNum() const { return _digraph->nodeNum(); }
1868 typedef ArcNumTagIndicator<Digraph> ArcNumTag;
1869 int arcNum() const { return 2 * _digraph->arcNum(); }
1871 typedef ArcNumTag EdgeNumTag;
1872 int edgeNum() const { return _digraph->arcNum(); }
1874 typedef FindArcTagIndicator<Digraph> FindArcTag;
1875 Arc findArc(Node s, Node t, Arc p = INVALID) const {
1877 Edge arc = _digraph->findArc(s, t);
1878 if (arc != INVALID) return direct(arc, true);
1879 arc = _digraph->findArc(t, s);
1880 if (arc != INVALID) return direct(arc, false);
1881 } else if (direction(p)) {
1882 Edge arc = _digraph->findArc(s, t, p);
1883 if (arc != INVALID) return direct(arc, true);
1884 arc = _digraph->findArc(t, s);
1885 if (arc != INVALID) return direct(arc, false);
1887 Edge arc = _digraph->findArc(t, s, p);
1888 if (arc != INVALID) return direct(arc, false);
1893 typedef FindArcTag FindEdgeTag;
1894 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
1897 Edge arc = _digraph->findArc(s, t);
1898 if (arc != INVALID) return arc;
1899 arc = _digraph->findArc(t, s);
1900 if (arc != INVALID) return arc;
1901 } else if (_digraph->source(p) == s) {
1902 Edge arc = _digraph->findArc(s, t, p);
1903 if (arc != INVALID) return arc;
1904 arc = _digraph->findArc(t, s);
1905 if (arc != INVALID) return arc;
1907 Edge arc = _digraph->findArc(t, s, p);
1908 if (arc != INVALID) return arc;
1911 return _digraph->findArc(s, t, p);
1918 template <typename _Value>
1922 typedef typename Digraph::template ArcMap<_Value> MapImpl;
1926 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1928 typedef _Value Value;
1930 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
1931 typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
1932 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
1933 typedef typename MapTraits<MapImpl>::ReturnValue Reference;
1935 ArcMapBase(const Adaptor& adaptor) :
1936 _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
1938 ArcMapBase(const Adaptor& adaptor, const Value& v)
1939 : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {}
1941 void set(const Arc& a, const Value& v) {
1945 _backward.set(a, v);
1949 ConstReturnValue operator[](const Arc& a) const {
1953 return _backward[a];
1957 ReturnValue operator[](const Arc& a) {
1961 return _backward[a];
1967 MapImpl _forward, _backward;
1973 template <typename _Value>
1974 class NodeMap : public Digraph::template NodeMap<_Value> {
1977 typedef _Value Value;
1978 typedef typename Digraph::template NodeMap<Value> Parent;
1980 explicit NodeMap(const Adaptor& adaptor)
1981 : Parent(*adaptor._digraph) {}
1983 NodeMap(const Adaptor& adaptor, const _Value& value)
1984 : Parent(*adaptor._digraph, value) { }
1987 NodeMap& operator=(const NodeMap& cmap) {
1988 return operator=<NodeMap>(cmap);
1991 template <typename CMap>
1992 NodeMap& operator=(const CMap& cmap) {
1993 Parent::operator=(cmap);
1999 template <typename _Value>
2001 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
2004 typedef _Value Value;
2005 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
2007 explicit ArcMap(const Adaptor& adaptor)
2008 : Parent(adaptor) {}
2010 ArcMap(const Adaptor& adaptor, const Value& value)
2011 : Parent(adaptor, value) {}
2014 ArcMap& operator=(const ArcMap& cmap) {
2015 return operator=<ArcMap>(cmap);
2018 template <typename CMap>
2019 ArcMap& operator=(const CMap& cmap) {
2020 Parent::operator=(cmap);
2025 template <typename _Value>
2026 class EdgeMap : public Digraph::template ArcMap<_Value> {
2029 typedef _Value Value;
2030 typedef typename Digraph::template ArcMap<Value> Parent;
2032 explicit EdgeMap(const Adaptor& adaptor)
2033 : Parent(*adaptor._digraph) {}
2035 EdgeMap(const Adaptor& adaptor, const Value& value)
2036 : Parent(*adaptor._digraph, value) {}
2039 EdgeMap& operator=(const EdgeMap& cmap) {
2040 return operator=<EdgeMap>(cmap);
2043 template <typename CMap>
2044 EdgeMap& operator=(const CMap& cmap) {
2045 Parent::operator=(cmap);
2051 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
2052 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2054 typedef typename ItemSetTraits<Digraph, Edge>::ItemNotifier EdgeNotifier;
2055 EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2059 UndirectorBase() : _digraph(0) {}
2063 void setDigraph(Digraph& digraph) {
2064 _digraph = &digraph;
2069 /// \ingroup graph_adaptors
2071 /// \brief Undirect the graph
2073 /// This adaptor makes an undirected graph from a directed
2074 /// graph. All arcs of the underlying digraph will be showed in the
2075 /// adaptor as an edge. The Orienter adaptor is conform to the \ref
2076 /// concepts::Graph "Graph concept".
2078 /// \tparam _Digraph It must be conform to the \ref
2079 /// concepts::Digraph "Digraph concept". The type can be specified
2081 template<typename _Digraph>
2083 : public GraphAdaptorExtender<UndirectorBase<_Digraph> > {
2085 typedef _Digraph Digraph;
2086 typedef GraphAdaptorExtender<UndirectorBase<Digraph> > Parent;
2091 /// \brief Constructor
2093 /// Creates a undirected graph from the given digraph
2094 Undirector(_Digraph& digraph) {
2095 setDigraph(digraph);
2098 /// \brief ArcMap combined from two original ArcMap
2100 /// This class adapts two original digraph ArcMap to
2101 /// get an arc map on the undirected graph.
2102 template <typename _ForwardMap, typename _BackwardMap>
2103 class CombinedArcMap {
2106 typedef _ForwardMap ForwardMap;
2107 typedef _BackwardMap BackwardMap;
2109 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2111 typedef typename ForwardMap::Value Value;
2112 typedef typename Parent::Arc Key;
2114 typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
2115 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
2116 typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
2117 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
2119 /// \brief Constructor
2122 CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2123 : _forward(&forward), _backward(&backward) {}
2126 /// \brief Sets the value associated with a key.
2128 /// Sets the value associated with a key.
2129 void set(const Key& e, const Value& a) {
2130 if (Parent::direction(e)) {
2131 _forward->set(e, a);
2133 _backward->set(e, a);
2137 /// \brief Returns the value associated with a key.
2139 /// Returns the value associated with a key.
2140 ConstReturnValue operator[](const Key& e) const {
2141 if (Parent::direction(e)) {
2142 return (*_forward)[e];
2144 return (*_backward)[e];
2148 /// \brief Returns the value associated with a key.
2150 /// Returns the value associated with a key.
2151 ReturnValue operator[](const Key& e) {
2152 if (Parent::direction(e)) {
2153 return (*_forward)[e];
2155 return (*_backward)[e];
2161 ForwardMap* _forward;
2162 BackwardMap* _backward;
2166 /// \brief Just gives back a combined arc map
2168 /// Just gives back a combined arc map
2169 template <typename ForwardMap, typename BackwardMap>
2170 static CombinedArcMap<ForwardMap, BackwardMap>
2171 combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2172 return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2175 template <typename ForwardMap, typename BackwardMap>
2176 static CombinedArcMap<const ForwardMap, BackwardMap>
2177 combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2178 return CombinedArcMap<const ForwardMap,
2179 BackwardMap>(forward, backward);
2182 template <typename ForwardMap, typename BackwardMap>
2183 static CombinedArcMap<ForwardMap, const BackwardMap>
2184 combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2185 return CombinedArcMap<ForwardMap,
2186 const BackwardMap>(forward, backward);
2189 template <typename ForwardMap, typename BackwardMap>
2190 static CombinedArcMap<const ForwardMap, const BackwardMap>
2191 combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2192 return CombinedArcMap<const ForwardMap,
2193 const BackwardMap>(forward, backward);
2198 /// \brief Just gives back an undirected view of the given digraph
2200 /// Just gives back an undirected view of the given digraph
2201 template<typename Digraph>
2202 Undirector<const Digraph>
2203 undirector(const Digraph& digraph) {
2204 return Undirector<const Digraph>(digraph);
2207 template <typename _Graph, typename _DirectionMap>
2208 class OrienterBase {
2211 typedef _Graph Graph;
2212 typedef _DirectionMap DirectionMap;
2214 typedef typename Graph::Node Node;
2215 typedef typename Graph::Edge Arc;
2217 void reverseArc(const Arc& arc) {
2218 _direction->set(arc, !(*_direction)[arc]);
2221 void first(Node& i) const { _graph->first(i); }
2222 void first(Arc& i) const { _graph->first(i); }
2223 void firstIn(Arc& i, const Node& n) const {
2225 _graph->firstInc(i, d, n);
2226 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2228 void firstOut(Arc& i, const Node& n ) const {
2230 _graph->firstInc(i, d, n);
2231 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2234 void next(Node& i) const { _graph->next(i); }
2235 void next(Arc& i) const { _graph->next(i); }
2236 void nextIn(Arc& i) const {
2237 bool d = !(*_direction)[i];
2238 _graph->nextInc(i, d);
2239 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2241 void nextOut(Arc& i) const {
2242 bool d = (*_direction)[i];
2243 _graph->nextInc(i, d);
2244 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2247 Node source(const Arc& e) const {
2248 return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2250 Node target(const Arc& e) const {
2251 return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2254 typedef NodeNumTagIndicator<Graph> NodeNumTag;
2255 int nodeNum() const { return _graph->nodeNum(); }
2257 typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2258 int arcNum() const { return _graph->edgeNum(); }
2260 typedef FindEdgeTagIndicator<Graph> FindArcTag;
2261 Arc findArc(const Node& u, const Node& v,
2262 const Arc& prev = INVALID) const {
2263 Arc arc = _graph->findEdge(u, v, prev);
2264 while (arc != INVALID && source(arc) != u) {
2265 arc = _graph->findEdge(u, v, arc);
2271 return Node(_graph->addNode());
2274 Arc addArc(const Node& u, const Node& v) {
2275 Arc arc = _graph->addEdge(u, v);
2276 _direction->set(arc, _graph->u(arc) == u);
2280 void erase(const Node& i) { _graph->erase(i); }
2281 void erase(const Arc& i) { _graph->erase(i); }
2283 void clear() { _graph->clear(); }
2285 int id(const Node& v) const { return _graph->id(v); }
2286 int id(const Arc& e) const { return _graph->id(e); }
2288 Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2289 Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2291 int maxNodeId() const { return _graph->maxNodeId(); }
2292 int maxArcId() const { return _graph->maxEdgeId(); }
2294 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
2295 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2297 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
2298 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2300 template <typename _Value>
2301 class NodeMap : public _Graph::template NodeMap<_Value> {
2304 typedef typename _Graph::template NodeMap<_Value> Parent;
2306 explicit NodeMap(const OrienterBase& adapter)
2307 : Parent(*adapter._graph) {}
2309 NodeMap(const OrienterBase& adapter, const _Value& value)
2310 : Parent(*adapter._graph, value) {}
2313 NodeMap& operator=(const NodeMap& cmap) {
2314 return operator=<NodeMap>(cmap);
2317 template <typename CMap>
2318 NodeMap& operator=(const CMap& cmap) {
2319 Parent::operator=(cmap);
2325 template <typename _Value>
2326 class ArcMap : public _Graph::template EdgeMap<_Value> {
2329 typedef typename Graph::template EdgeMap<_Value> Parent;
2331 explicit ArcMap(const OrienterBase& adapter)
2332 : Parent(*adapter._graph) { }
2334 ArcMap(const OrienterBase& adapter, const _Value& value)
2335 : Parent(*adapter._graph, value) { }
2338 ArcMap& operator=(const ArcMap& cmap) {
2339 return operator=<ArcMap>(cmap);
2342 template <typename CMap>
2343 ArcMap& operator=(const CMap& cmap) {
2344 Parent::operator=(cmap);
2353 DirectionMap* _direction;
2355 void setDirectionMap(DirectionMap& direction) {
2356 _direction = &direction;
2359 void setGraph(Graph& graph) {
2365 /// \ingroup graph_adaptors
2367 /// \brief Orients the edges of the graph to get a digraph
2369 /// This adaptor orients each edge in the undirected graph. The
2370 /// direction of the arcs stored in an edge node map. The arcs can
2371 /// be easily reverted by the \c reverseArc() member function in the
2372 /// adaptor. The Orienter adaptor is conform to the \ref
2373 /// concepts::Digraph "Digraph concept".
2375 /// \tparam _Graph It must be conform to the \ref concepts::Graph
2376 /// "Graph concept". The type can be specified to be const.
2377 /// \tparam _DirectionMap A bool valued edge map of the the adapted
2381 template<typename _Graph,
2382 typename DirectionMap = typename _Graph::template EdgeMap<bool> >
2384 public DigraphAdaptorExtender<OrienterBase<_Graph, DirectionMap> > {
2386 typedef _Graph Graph;
2387 typedef DigraphAdaptorExtender<
2388 OrienterBase<_Graph, DirectionMap> > Parent;
2389 typedef typename Parent::Arc Arc;
2394 /// \brief Constructor of the adaptor
2396 /// Constructor of the adaptor
2397 Orienter(Graph& graph, DirectionMap& direction) {
2399 setDirectionMap(direction);
2402 /// \brief Reverse arc
2404 /// It reverse the given arc. It simply negate the direction in the map.
2405 void reverseArc(const Arc& a) {
2406 Parent::reverseArc(a);
2410 /// \brief Just gives back a Orienter
2412 /// Just gives back a Orienter
2413 template<typename Graph, typename DirectionMap>
2414 Orienter<const Graph, DirectionMap>
2415 orienter(const Graph& graph, DirectionMap& dm) {
2416 return Orienter<const Graph, DirectionMap>(graph, dm);
2419 template<typename Graph, typename DirectionMap>
2420 Orienter<const Graph, const DirectionMap>
2421 orienter(const Graph& graph, const DirectionMap& dm) {
2422 return Orienter<const Graph, const DirectionMap>(graph, dm);
2425 namespace _adaptor_bits {
2427 template<typename _Digraph,
2428 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2429 typename _FlowMap = _CapacityMap,
2430 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2431 class ResForwardFilter {
2434 typedef _Digraph Digraph;
2435 typedef _CapacityMap CapacityMap;
2436 typedef _FlowMap FlowMap;
2437 typedef _Tolerance Tolerance;
2439 typedef typename Digraph::Arc Key;
2444 const CapacityMap* _capacity;
2445 const FlowMap* _flow;
2446 Tolerance _tolerance;
2449 ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2450 const Tolerance& tolerance = Tolerance())
2451 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2453 bool operator[](const typename Digraph::Arc& a) const {
2454 return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2458 template<typename _Digraph,
2459 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2460 typename _FlowMap = _CapacityMap,
2461 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2462 class ResBackwardFilter {
2465 typedef _Digraph Digraph;
2466 typedef _CapacityMap CapacityMap;
2467 typedef _FlowMap FlowMap;
2468 typedef _Tolerance Tolerance;
2470 typedef typename Digraph::Arc Key;
2475 const CapacityMap* _capacity;
2476 const FlowMap* _flow;
2477 Tolerance _tolerance;
2481 ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2482 const Tolerance& tolerance = Tolerance())
2483 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2485 bool operator[](const typename Digraph::Arc& a) const {
2486 return _tolerance.positive((*_flow)[a]);
2492 /// \ingroup graph_adaptors
2494 /// \brief An adaptor for composing the residual graph for directed
2495 /// flow and circulation problems.
2497 /// An adaptor for composing the residual graph for directed flow and
2498 /// circulation problems. Let \f$ G=(V, A) \f$ be a directed graph
2499 /// and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$,
2500 /// be functions on the arc-set.
2502 /// Then Residual implements the digraph structure with
2503 /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} \f$,
2504 /// where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
2505 /// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so
2506 /// called residual graph. When we take the union
2507 /// \f$ A_{forward}\cup A_{backward} \f$, multiplicities are counted,
2508 /// i.e. if an arc is in both \f$ A_{forward} \f$ and
2509 /// \f$ A_{backward} \f$, then in the adaptor it appears in both
2512 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
2513 /// "Digraph concept". The type is implicitly const.
2514 /// \tparam _CapacityMap An arc map of some numeric type, it defines
2515 /// the capacities in the flow problem. The map is implicitly const.
2516 /// \tparam _FlowMap An arc map of some numeric type, it defines
2517 /// the capacities in the flow problem.
2518 /// \tparam _Tolerance Handler for inexact computation.
2519 template<typename _Digraph,
2520 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2521 typename _FlowMap = _CapacityMap,
2522 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2525 Undirector<const _Digraph>,
2526 typename Undirector<const _Digraph>::template CombinedArcMap<
2527 _adaptor_bits::ResForwardFilter<const _Digraph, _CapacityMap,
2528 _FlowMap, _Tolerance>,
2529 _adaptor_bits::ResBackwardFilter<const _Digraph, _CapacityMap,
2530 _FlowMap, _Tolerance> > >
2534 typedef _Digraph Digraph;
2535 typedef _CapacityMap CapacityMap;
2536 typedef _FlowMap FlowMap;
2537 typedef _Tolerance Tolerance;
2539 typedef typename CapacityMap::Value Value;
2540 typedef Residual Adaptor;
2544 typedef Undirector<const Digraph> Undirected;
2546 typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap,
2547 FlowMap, Tolerance> ForwardFilter;
2549 typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap,
2550 FlowMap, Tolerance> BackwardFilter;
2552 typedef typename Undirected::
2553 template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2555 typedef FilterArcs<Undirected, ArcFilter> Parent;
2557 const CapacityMap* _capacity;
2561 ForwardFilter _forward_filter;
2562 BackwardFilter _backward_filter;
2563 ArcFilter _arc_filter;
2567 /// \brief Constructor of the residual digraph.
2569 /// Constructor of the residual graph. The parameters are the digraph,
2570 /// the flow map, the capacity map and a tolerance object.
2571 Residual(const Digraph& digraph, const CapacityMap& capacity,
2572 FlowMap& flow, const Tolerance& tolerance = Tolerance())
2573 : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph),
2574 _forward_filter(capacity, flow, tolerance),
2575 _backward_filter(capacity, flow, tolerance),
2576 _arc_filter(_forward_filter, _backward_filter)
2578 Parent::setDigraph(_graph);
2579 Parent::setArcFilterMap(_arc_filter);
2582 typedef typename Parent::Arc Arc;
2584 /// \brief Gives back the residual capacity of the arc.
2586 /// Gives back the residual capacity of the arc.
2587 Value residualCapacity(const Arc& a) const {
2588 if (Undirected::direction(a)) {
2589 return (*_capacity)[a] - (*_flow)[a];
2595 /// \brief Augment on the given arc in the residual graph.
2597 /// Augment on the given arc in the residual graph. It increase
2598 /// or decrease the flow on the original arc depend on the direction
2599 /// of the residual arc.
2600 void augment(const Arc& a, const Value& v) const {
2601 if (Undirected::direction(a)) {
2602 _flow->set(a, (*_flow)[a] + v);
2604 _flow->set(a, (*_flow)[a] - v);
2608 /// \brief Returns the direction of the arc.
2610 /// Returns true when the arc is same oriented as the original arc.
2611 static bool forward(const Arc& a) {
2612 return Undirected::direction(a);
2615 /// \brief Returns the direction of the arc.
2617 /// Returns true when the arc is opposite oriented as the original arc.
2618 static bool backward(const Arc& a) {
2619 return !Undirected::direction(a);
2622 /// \brief Gives back the forward oriented residual arc.
2624 /// Gives back the forward oriented residual arc.
2625 static Arc forward(const typename Digraph::Arc& a) {
2626 return Undirected::direct(a, true);
2629 /// \brief Gives back the backward oriented residual arc.
2631 /// Gives back the backward oriented residual arc.
2632 static Arc backward(const typename Digraph::Arc& a) {
2633 return Undirected::direct(a, false);
2636 /// \brief Residual capacity map.
2638 /// In generic residual graph the residual capacity can be obtained
2640 class ResidualCapacity {
2642 const Adaptor* _adaptor;
2647 typedef typename _CapacityMap::Value Value;
2650 ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {}
2653 Value operator[](const Arc& a) const {
2654 return _adaptor->residualCapacity(a);
2661 template <typename _Digraph>
2662 class SplitNodesBase {
2665 typedef _Digraph Digraph;
2666 typedef DigraphAdaptorBase<const _Digraph> Parent;
2667 typedef SplitNodesBase Adaptor;
2669 typedef typename Digraph::Node DigraphNode;
2670 typedef typename Digraph::Arc DigraphArc;
2677 template <typename T> class NodeMapBase;
2678 template <typename T> class ArcMapBase;
2682 class Node : public DigraphNode {
2683 friend class SplitNodesBase;
2684 template <typename T> friend class NodeMapBase;
2688 Node(DigraphNode node, bool in)
2689 : DigraphNode(node), _in(in) {}
2694 Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2696 bool operator==(const Node& node) const {
2697 return DigraphNode::operator==(node) && _in == node._in;
2700 bool operator!=(const Node& node) const {
2701 return !(*this == node);
2704 bool operator<(const Node& node) const {
2705 return DigraphNode::operator<(node) ||
2706 (DigraphNode::operator==(node) && _in < node._in);
2711 friend class SplitNodesBase;
2712 template <typename T> friend class ArcMapBase;
2714 typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2716 explicit Arc(const DigraphArc& arc) : _item(arc) {}
2717 explicit Arc(const DigraphNode& node) : _item(node) {}
2723 Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2725 bool operator==(const Arc& arc) const {
2726 if (_item.firstState()) {
2727 if (arc._item.firstState()) {
2728 return _item.first() == arc._item.first();
2731 if (arc._item.secondState()) {
2732 return _item.second() == arc._item.second();
2738 bool operator!=(const Arc& arc) const {
2739 return !(*this == arc);
2742 bool operator<(const Arc& arc) const {
2743 if (_item.firstState()) {
2744 if (arc._item.firstState()) {
2745 return _item.first() < arc._item.first();
2749 if (arc._item.secondState()) {
2750 return _item.second() < arc._item.second();
2756 operator DigraphArc() const { return _item.first(); }
2757 operator DigraphNode() const { return _item.second(); }
2761 void first(Node& n) const {
2766 void next(Node& n) const {
2775 void first(Arc& e) const {
2776 e._item.setSecond();
2777 _digraph->first(e._item.second());
2778 if (e._item.second() == INVALID) {
2780 _digraph->first(e._item.first());
2784 void next(Arc& e) const {
2785 if (e._item.secondState()) {
2786 _digraph->next(e._item.second());
2787 if (e._item.second() == INVALID) {
2789 _digraph->first(e._item.first());
2792 _digraph->next(e._item.first());
2796 void firstOut(Arc& e, const Node& n) const {
2798 e._item.setSecond(n);
2801 _digraph->firstOut(e._item.first(), n);
2805 void nextOut(Arc& e) const {
2806 if (!e._item.firstState()) {
2807 e._item.setFirst(INVALID);
2809 _digraph->nextOut(e._item.first());
2813 void firstIn(Arc& e, const Node& n) const {
2815 e._item.setSecond(n);
2818 _digraph->firstIn(e._item.first(), n);
2822 void nextIn(Arc& e) const {
2823 if (!e._item.firstState()) {
2824 e._item.setFirst(INVALID);
2826 _digraph->nextIn(e._item.first());
2830 Node source(const Arc& e) const {
2831 if (e._item.firstState()) {
2832 return Node(_digraph->source(e._item.first()), false);
2834 return Node(e._item.second(), true);
2838 Node target(const Arc& e) const {
2839 if (e._item.firstState()) {
2840 return Node(_digraph->target(e._item.first()), true);
2842 return Node(e._item.second(), false);
2846 int id(const Node& n) const {
2847 return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
2849 Node nodeFromId(int ix) const {
2850 return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
2852 int maxNodeId() const {
2853 return 2 * _digraph->maxNodeId() + 1;
2856 int id(const Arc& e) const {
2857 if (e._item.firstState()) {
2858 return _digraph->id(e._item.first()) << 1;
2860 return (_digraph->id(e._item.second()) << 1) | 1;
2863 Arc arcFromId(int ix) const {
2864 if ((ix & 1) == 0) {
2865 return Arc(_digraph->arcFromId(ix >> 1));
2867 return Arc(_digraph->nodeFromId(ix >> 1));
2870 int maxArcId() const {
2871 return std::max(_digraph->maxNodeId() << 1,
2872 (_digraph->maxArcId() << 1) | 1);
2875 static bool inNode(const Node& n) {
2879 static bool outNode(const Node& n) {
2883 static bool origArc(const Arc& e) {
2884 return e._item.firstState();
2887 static bool bindArc(const Arc& e) {
2888 return e._item.secondState();
2891 static Node inNode(const DigraphNode& n) {
2892 return Node(n, true);
2895 static Node outNode(const DigraphNode& n) {
2896 return Node(n, false);
2899 static Arc arc(const DigraphNode& n) {
2903 static Arc arc(const DigraphArc& e) {
2907 typedef True NodeNumTag;
2908 int nodeNum() const {
2909 return 2 * countNodes(*_digraph);
2912 typedef True ArcNumTag;
2913 int arcNum() const {
2914 return countArcs(*_digraph) + countNodes(*_digraph);
2917 typedef True FindArcTag;
2918 Arc findArc(const Node& u, const Node& v,
2919 const Arc& prev = INVALID) const {
2920 if (inNode(u) && outNode(v)) {
2921 if (static_cast<const DigraphNode&>(u) ==
2922 static_cast<const DigraphNode&>(v) && prev == INVALID) {
2926 else if (outNode(u) && inNode(v)) {
2927 return Arc(::lemon::findArc(*_digraph, u, v, prev));
2934 template <typename _Value>
2936 : public MapTraits<typename Parent::template NodeMap<_Value> > {
2937 typedef typename Parent::template NodeMap<_Value> NodeImpl;
2940 typedef _Value Value;
2941 typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
2942 typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
2943 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
2944 typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
2945 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
2947 NodeMapBase(const Adaptor& adaptor)
2948 : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
2949 NodeMapBase(const Adaptor& adaptor, const Value& value)
2950 : _in_map(*adaptor._digraph, value),
2951 _out_map(*adaptor._digraph, value) {}
2953 void set(const Node& key, const Value& val) {
2954 if (Adaptor::inNode(key)) { _in_map.set(key, val); }
2955 else {_out_map.set(key, val); }
2958 ReturnValue operator[](const Node& key) {
2959 if (Adaptor::inNode(key)) { return _in_map[key]; }
2960 else { return _out_map[key]; }
2963 ConstReturnValue operator[](const Node& key) const {
2964 if (Adaptor::inNode(key)) { return _in_map[key]; }
2965 else { return _out_map[key]; }
2969 NodeImpl _in_map, _out_map;
2972 template <typename _Value>
2974 : public MapTraits<typename Parent::template ArcMap<_Value> > {
2975 typedef typename Parent::template ArcMap<_Value> ArcImpl;
2976 typedef typename Parent::template NodeMap<_Value> NodeImpl;
2979 typedef _Value Value;
2980 typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
2981 typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
2982 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
2983 typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
2984 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
2986 ArcMapBase(const Adaptor& adaptor)
2987 : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
2988 ArcMapBase(const Adaptor& adaptor, const Value& value)
2989 : _arc_map(*adaptor._digraph, value),
2990 _node_map(*adaptor._digraph, value) {}
2992 void set(const Arc& key, const Value& val) {
2993 if (Adaptor::origArc(key)) {
2994 _arc_map.set(key._item.first(), val);
2996 _node_map.set(key._item.second(), val);
3000 ReturnValue operator[](const Arc& key) {
3001 if (Adaptor::origArc(key)) {
3002 return _arc_map[key._item.first()];
3004 return _node_map[key._item.second()];
3008 ConstReturnValue operator[](const Arc& key) const {
3009 if (Adaptor::origArc(key)) {
3010 return _arc_map[key._item.first()];
3012 return _node_map[key._item.second()];
3023 template <typename _Value>
3025 : public SubMapExtender<Adaptor, NodeMapBase<_Value> >
3028 typedef _Value Value;
3029 typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent;
3031 NodeMap(const Adaptor& adaptor)
3032 : Parent(adaptor) {}
3034 NodeMap(const Adaptor& adaptor, const Value& value)
3035 : Parent(adaptor, value) {}
3038 NodeMap& operator=(const NodeMap& cmap) {
3039 return operator=<NodeMap>(cmap);
3042 template <typename CMap>
3043 NodeMap& operator=(const CMap& cmap) {
3044 Parent::operator=(cmap);
3049 template <typename _Value>
3051 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
3054 typedef _Value Value;
3055 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
3057 ArcMap(const Adaptor& adaptor)
3058 : Parent(adaptor) {}
3060 ArcMap(const Adaptor& adaptor, const Value& value)
3061 : Parent(adaptor, value) {}
3064 ArcMap& operator=(const ArcMap& cmap) {
3065 return operator=<ArcMap>(cmap);
3068 template <typename CMap>
3069 ArcMap& operator=(const CMap& cmap) {
3070 Parent::operator=(cmap);
3077 SplitNodesBase() : _digraph(0) {}
3081 void setDigraph(Digraph& digraph) {
3082 _digraph = &digraph;
3087 /// \ingroup graph_adaptors
3089 /// \brief Split the nodes of a directed graph
3091 /// The SplitNodes adaptor splits each node into an in-node and an
3092 /// out-node. Formaly, the adaptor replaces each \f$ u \f$ node in
3093 /// the digraph with two nodes(namely node \f$ u_{in} \f$ and node
3094 /// \f$ u_{out} \f$). If there is a \f$ (v, u) \f$ arc in the
3095 /// original digraph the new target of the arc will be \f$ u_{in} \f$
3096 /// and similarly the source of the original \f$ (u, v) \f$ arc
3097 /// will be \f$ u_{out} \f$. The adaptor will add for each node in
3098 /// the original digraph an additional arc which connects
3099 /// \f$ (u_{in}, u_{out}) \f$.
3101 /// The aim of this class is to run algorithm with node costs if the
3102 /// algorithm can use directly just arc costs. In this case we should use
3103 /// a \c SplitNodes and set the node cost of the graph to the
3104 /// bind arc in the adapted graph.
3106 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
3107 /// "Digraph concept". The type can be specified to be const.
3108 template <typename _Digraph>
3110 : public DigraphAdaptorExtender<SplitNodesBase<const _Digraph> > {
3112 typedef _Digraph Digraph;
3113 typedef DigraphAdaptorExtender<SplitNodesBase<const Digraph> > Parent;
3115 typedef typename Digraph::Node DigraphNode;
3116 typedef typename Digraph::Arc DigraphArc;
3118 typedef typename Parent::Node Node;
3119 typedef typename Parent::Arc Arc;
3121 /// \brief Constructor of the adaptor.
3123 /// Constructor of the adaptor.
3124 SplitNodes(const Digraph& g) {
3125 Parent::setDigraph(g);
3128 /// \brief Returns true when the node is in-node.
3130 /// Returns true when the node is in-node.
3131 static bool inNode(const Node& n) {
3132 return Parent::inNode(n);
3135 /// \brief Returns true when the node is out-node.
3137 /// Returns true when the node is out-node.
3138 static bool outNode(const Node& n) {
3139 return Parent::outNode(n);
3142 /// \brief Returns true when the arc is arc in the original digraph.
3144 /// Returns true when the arc is arc in the original digraph.
3145 static bool origArc(const Arc& a) {
3146 return Parent::origArc(a);
3149 /// \brief Returns true when the arc binds an in-node and an out-node.
3151 /// Returns true when the arc binds an in-node and an out-node.
3152 static bool bindArc(const Arc& a) {
3153 return Parent::bindArc(a);
3156 /// \brief Gives back the in-node created from the \c node.
3158 /// Gives back the in-node created from the \c node.
3159 static Node inNode(const DigraphNode& n) {
3160 return Parent::inNode(n);
3163 /// \brief Gives back the out-node created from the \c node.
3165 /// Gives back the out-node created from the \c node.
3166 static Node outNode(const DigraphNode& n) {
3167 return Parent::outNode(n);
3170 /// \brief Gives back the arc binds the two part of the node.
3172 /// Gives back the arc binds the two part of the node.
3173 static Arc arc(const DigraphNode& n) {
3174 return Parent::arc(n);
3177 /// \brief Gives back the arc of the original arc.
3179 /// Gives back the arc of the original arc.
3180 static Arc arc(const DigraphArc& a) {
3181 return Parent::arc(a);
3184 /// \brief NodeMap combined from two original NodeMap
3186 /// This class adapt two of the original digraph NodeMap to
3187 /// get a node map on the adapted digraph.
3188 template <typename InNodeMap, typename OutNodeMap>
3189 class CombinedNodeMap {
3193 typedef typename InNodeMap::Value Value;
3195 typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
3196 typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
3197 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
3198 typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
3199 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
3201 /// \brief Constructor
3204 CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3205 : _in_map(in_map), _out_map(out_map) {}
3207 /// \brief The subscript operator.
3209 /// The subscript operator.
3210 Value& operator[](const Key& key) {
3211 if (Parent::inNode(key)) {
3212 return _in_map[key];
3214 return _out_map[key];
3218 /// \brief The const subscript operator.
3220 /// The const subscript operator.
3221 Value operator[](const Key& key) const {
3222 if (Parent::inNode(key)) {
3223 return _in_map[key];
3225 return _out_map[key];
3229 /// \brief The setter function of the map.
3231 /// The setter function of the map.
3232 void set(const Key& key, const Value& value) {
3233 if (Parent::inNode(key)) {
3234 _in_map.set(key, value);
3236 _out_map.set(key, value);
3243 OutNodeMap& _out_map;
3248 /// \brief Just gives back a combined node map
3250 /// Just gives back a combined node map
3251 template <typename InNodeMap, typename OutNodeMap>
3252 static CombinedNodeMap<InNodeMap, OutNodeMap>
3253 combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3254 return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3257 template <typename InNodeMap, typename OutNodeMap>
3258 static CombinedNodeMap<const InNodeMap, OutNodeMap>
3259 combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3260 return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3263 template <typename InNodeMap, typename OutNodeMap>
3264 static CombinedNodeMap<InNodeMap, const OutNodeMap>
3265 combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3266 return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3269 template <typename InNodeMap, typename OutNodeMap>
3270 static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3271 combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3272 return CombinedNodeMap<const InNodeMap,
3273 const OutNodeMap>(in_map, out_map);
3276 /// \brief ArcMap combined from an original ArcMap and a NodeMap
3278 /// This class adapt an original ArcMap and a NodeMap to get an
3279 /// arc map on the adapted digraph
3280 template <typename DigraphArcMap, typename DigraphNodeMap>
3281 class CombinedArcMap {
3285 typedef typename DigraphArcMap::Value Value;
3287 typedef typename MapTraits<DigraphArcMap>::ReferenceMapTag
3289 typedef typename MapTraits<DigraphArcMap>::ReturnValue
3291 typedef typename MapTraits<DigraphArcMap>::ConstReturnValue
3293 typedef typename MapTraits<DigraphArcMap>::ReturnValue
3295 typedef typename MapTraits<DigraphArcMap>::ConstReturnValue
3298 /// \brief Constructor
3301 CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map)
3302 : _arc_map(arc_map), _node_map(node_map) {}
3304 /// \brief The subscript operator.
3306 /// The subscript operator.
3307 void set(const Arc& arc, const Value& val) {
3308 if (Parent::origArc(arc)) {
3309 _arc_map.set(arc, val);
3311 _node_map.set(arc, val);
3315 /// \brief The const subscript operator.
3317 /// The const subscript operator.
3318 Value operator[](const Key& arc) const {
3319 if (Parent::origArc(arc)) {
3320 return _arc_map[arc];
3322 return _node_map[arc];
3326 /// \brief The const subscript operator.
3328 /// The const subscript operator.
3329 Value& operator[](const Key& arc) {
3330 if (Parent::origArc(arc)) {
3331 return _arc_map[arc];
3333 return _node_map[arc];
3338 DigraphArcMap& _arc_map;
3339 DigraphNodeMap& _node_map;
3342 /// \brief Just gives back a combined arc map
3344 /// Just gives back a combined arc map
3345 template <typename DigraphArcMap, typename DigraphNodeMap>
3346 static CombinedArcMap<DigraphArcMap, DigraphNodeMap>
3347 combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) {
3348 return CombinedArcMap<DigraphArcMap, DigraphNodeMap>(arc_map, node_map);
3351 template <typename DigraphArcMap, typename DigraphNodeMap>
3352 static CombinedArcMap<const DigraphArcMap, DigraphNodeMap>
3353 combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) {
3354 return CombinedArcMap<const DigraphArcMap,
3355 DigraphNodeMap>(arc_map, node_map);
3358 template <typename DigraphArcMap, typename DigraphNodeMap>
3359 static CombinedArcMap<DigraphArcMap, const DigraphNodeMap>
3360 combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) {
3361 return CombinedArcMap<DigraphArcMap,
3362 const DigraphNodeMap>(arc_map, node_map);
3365 template <typename DigraphArcMap, typename DigraphNodeMap>
3366 static CombinedArcMap<const DigraphArcMap, const DigraphNodeMap>
3367 combinedArcMap(const DigraphArcMap& arc_map,
3368 const DigraphNodeMap& node_map) {
3369 return CombinedArcMap<const DigraphArcMap,
3370 const DigraphNodeMap>(arc_map, node_map);
3375 /// \brief Just gives back a node splitter
3377 /// Just gives back a node splitter
3378 template<typename Digraph>
3380 splitNodes(const Digraph& digraph) {
3381 return SplitNodes<Digraph>(digraph);
3387 #endif //LEMON_ADAPTORS_H