1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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 Adaptor classes for digraphs and graphs
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 Adaptor class for reversing the orientation of the arcs in
352 /// ReverseDigraph can be used for reversing the arcs in a digraph.
353 /// It conforms to the \ref concepts::Digraph "Digraph" concept.
355 /// The adapted digraph can also be modified through this adaptor
356 /// by adding or removing nodes or arcs, unless the \c GR template
357 /// parameter is set to be \c const.
359 /// \tparam GR The type of the adapted digraph.
360 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
361 /// It can also be specified to be \c const.
363 /// \note The \c Node and \c Arc types of this adaptor and the adapted
364 /// digraph are convertible to each other.
365 template<typename GR>
367 class ReverseDigraph {
369 class ReverseDigraph :
370 public DigraphAdaptorExtender<ReverseDigraphBase<GR> > {
373 /// The type of the adapted digraph.
375 typedef DigraphAdaptorExtender<ReverseDigraphBase<GR> > Parent;
380 /// \brief Constructor
382 /// Creates a reverse digraph adaptor for the given digraph.
383 explicit ReverseDigraph(Digraph& digraph) {
384 Parent::setDigraph(digraph);
388 /// \brief Returns a read-only ReverseDigraph adaptor
390 /// This function just returns a read-only \ref ReverseDigraph adaptor.
391 /// \ingroup graph_adaptors
392 /// \relates ReverseDigraph
393 template<typename GR>
394 ReverseDigraph<const GR> reverseDigraph(const GR& digraph) {
395 return ReverseDigraph<const GR>(digraph);
399 template <typename _Digraph, typename _NodeFilterMap,
400 typename _ArcFilterMap, bool _checked = true>
401 class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
403 typedef _Digraph Digraph;
404 typedef _NodeFilterMap NodeFilterMap;
405 typedef _ArcFilterMap ArcFilterMap;
407 typedef SubDigraphBase Adaptor;
408 typedef DigraphAdaptorBase<_Digraph> Parent;
410 NodeFilterMap* _node_filter;
411 ArcFilterMap* _arc_filter;
413 : Parent(), _node_filter(0), _arc_filter(0) { }
415 void setNodeFilterMap(NodeFilterMap& node_filter) {
416 _node_filter = &node_filter;
418 void setArcFilterMap(ArcFilterMap& arc_filter) {
419 _arc_filter = &arc_filter;
424 typedef typename Parent::Node Node;
425 typedef typename Parent::Arc Arc;
427 void first(Node& i) const {
429 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
432 void first(Arc& i) const {
434 while (i != INVALID && (!(*_arc_filter)[i]
435 || !(*_node_filter)[Parent::source(i)]
436 || !(*_node_filter)[Parent::target(i)]))
440 void firstIn(Arc& i, const Node& n) const {
441 Parent::firstIn(i, n);
442 while (i != INVALID && (!(*_arc_filter)[i]
443 || !(*_node_filter)[Parent::source(i)]))
447 void firstOut(Arc& i, const Node& n) const {
448 Parent::firstOut(i, n);
449 while (i != INVALID && (!(*_arc_filter)[i]
450 || !(*_node_filter)[Parent::target(i)]))
454 void next(Node& i) const {
456 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
459 void next(Arc& i) const {
461 while (i != INVALID && (!(*_arc_filter)[i]
462 || !(*_node_filter)[Parent::source(i)]
463 || !(*_node_filter)[Parent::target(i)]))
467 void nextIn(Arc& i) const {
469 while (i != INVALID && (!(*_arc_filter)[i]
470 || !(*_node_filter)[Parent::source(i)]))
474 void nextOut(Arc& i) const {
476 while (i != INVALID && (!(*_arc_filter)[i]
477 || !(*_node_filter)[Parent::target(i)]))
481 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
482 void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
484 bool status(const Node& n) const { return (*_node_filter)[n]; }
485 bool status(const Arc& a) const { return (*_arc_filter)[a]; }
487 typedef False NodeNumTag;
488 typedef False ArcNumTag;
490 typedef FindArcTagIndicator<Digraph> FindArcTag;
491 Arc findArc(const Node& source, const Node& target,
492 const Arc& prev = INVALID) const {
493 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
496 Arc arc = Parent::findArc(source, target, prev);
497 while (arc != INVALID && !(*_arc_filter)[arc]) {
498 arc = Parent::findArc(source, target, arc);
503 template <typename _Value>
504 class NodeMap : public SubMapExtender<Adaptor,
505 typename Parent::template NodeMap<_Value> > {
507 typedef _Value Value;
508 typedef SubMapExtender<Adaptor, typename Parent::
509 template NodeMap<Value> > MapParent;
511 NodeMap(const Adaptor& adaptor)
512 : MapParent(adaptor) {}
513 NodeMap(const Adaptor& adaptor, const Value& value)
514 : MapParent(adaptor, value) {}
517 NodeMap& operator=(const NodeMap& cmap) {
518 return operator=<NodeMap>(cmap);
521 template <typename CMap>
522 NodeMap& operator=(const CMap& cmap) {
523 MapParent::operator=(cmap);
528 template <typename _Value>
529 class ArcMap : public SubMapExtender<Adaptor,
530 typename Parent::template ArcMap<_Value> > {
532 typedef _Value Value;
533 typedef SubMapExtender<Adaptor, typename Parent::
534 template ArcMap<Value> > MapParent;
536 ArcMap(const Adaptor& adaptor)
537 : MapParent(adaptor) {}
538 ArcMap(const Adaptor& adaptor, const Value& value)
539 : MapParent(adaptor, value) {}
542 ArcMap& operator=(const ArcMap& cmap) {
543 return operator=<ArcMap>(cmap);
546 template <typename CMap>
547 ArcMap& operator=(const CMap& cmap) {
548 MapParent::operator=(cmap);
555 template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap>
556 class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false>
557 : public DigraphAdaptorBase<_Digraph> {
559 typedef _Digraph Digraph;
560 typedef _NodeFilterMap NodeFilterMap;
561 typedef _ArcFilterMap ArcFilterMap;
563 typedef SubDigraphBase Adaptor;
564 typedef DigraphAdaptorBase<Digraph> Parent;
566 NodeFilterMap* _node_filter;
567 ArcFilterMap* _arc_filter;
569 : Parent(), _node_filter(0), _arc_filter(0) { }
571 void setNodeFilterMap(NodeFilterMap& node_filter) {
572 _node_filter = &node_filter;
574 void setArcFilterMap(ArcFilterMap& arc_filter) {
575 _arc_filter = &arc_filter;
580 typedef typename Parent::Node Node;
581 typedef typename Parent::Arc Arc;
583 void first(Node& i) const {
585 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
588 void first(Arc& i) const {
590 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
593 void firstIn(Arc& i, const Node& n) const {
594 Parent::firstIn(i, n);
595 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
598 void firstOut(Arc& i, const Node& n) const {
599 Parent::firstOut(i, n);
600 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
603 void next(Node& i) const {
605 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
607 void next(Arc& i) const {
609 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
611 void nextIn(Arc& i) const {
613 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
616 void nextOut(Arc& i) const {
618 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
621 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
622 void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
624 bool status(const Node& n) const { return (*_node_filter)[n]; }
625 bool status(const Arc& a) const { return (*_arc_filter)[a]; }
627 typedef False NodeNumTag;
628 typedef False ArcNumTag;
630 typedef FindArcTagIndicator<Digraph> FindArcTag;
631 Arc findArc(const Node& source, const Node& target,
632 const Arc& prev = INVALID) const {
633 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
636 Arc arc = Parent::findArc(source, target, prev);
637 while (arc != INVALID && !(*_arc_filter)[arc]) {
638 arc = Parent::findArc(source, target, arc);
643 template <typename _Value>
644 class NodeMap : public SubMapExtender<Adaptor,
645 typename Parent::template NodeMap<_Value> > {
647 typedef _Value Value;
648 typedef SubMapExtender<Adaptor, typename Parent::
649 template NodeMap<Value> > MapParent;
651 NodeMap(const Adaptor& adaptor)
652 : MapParent(adaptor) {}
653 NodeMap(const Adaptor& adaptor, const Value& value)
654 : MapParent(adaptor, value) {}
657 NodeMap& operator=(const NodeMap& cmap) {
658 return operator=<NodeMap>(cmap);
661 template <typename CMap>
662 NodeMap& operator=(const CMap& cmap) {
663 MapParent::operator=(cmap);
668 template <typename _Value>
669 class ArcMap : public SubMapExtender<Adaptor,
670 typename Parent::template ArcMap<_Value> > {
672 typedef _Value Value;
673 typedef SubMapExtender<Adaptor, typename Parent::
674 template ArcMap<Value> > MapParent;
676 ArcMap(const Adaptor& adaptor)
677 : MapParent(adaptor) {}
678 ArcMap(const Adaptor& adaptor, const Value& value)
679 : MapParent(adaptor, value) {}
682 ArcMap& operator=(const ArcMap& cmap) {
683 return operator=<ArcMap>(cmap);
686 template <typename CMap>
687 ArcMap& operator=(const CMap& cmap) {
688 MapParent::operator=(cmap);
695 /// \ingroup graph_adaptors
697 /// \brief Adaptor class for hiding nodes and arcs in a digraph
699 /// SubDigraph can be used for hiding nodes and arcs in a digraph.
700 /// A \c bool node map and a \c bool arc map must be specified, which
701 /// define the filters for nodes and arcs.
702 /// Only the nodes and arcs with \c true filter value are
703 /// shown in the subdigraph. The arcs that are incident to hidden
704 /// nodes are also filtered out.
705 /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept.
707 /// The adapted digraph can also be modified through this adaptor
708 /// by adding or removing nodes or arcs, unless the \c GR template
709 /// parameter is set to be \c const.
711 /// \tparam GR The type of the adapted digraph.
712 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
713 /// It can also be specified to be \c const.
714 /// \tparam NF The type of the node filter map.
715 /// It must be a \c bool (or convertible) node map of the
716 /// adapted digraph. The default type is
717 /// \ref concepts::Digraph::NodeMap "GR::NodeMap<bool>".
718 /// \tparam AF The type of the arc filter map.
719 /// It must be \c bool (or convertible) arc map of the
720 /// adapted digraph. The default type is
721 /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
723 /// \note The \c Node and \c Arc types of this adaptor and the adapted
724 /// digraph are convertible to each other.
729 template<typename GR, typename NF, typename AF>
732 template<typename GR,
733 typename NF = typename GR::template NodeMap<bool>,
734 typename AF = typename GR::template ArcMap<bool> >
736 public DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> > {
739 /// The type of the adapted digraph.
741 /// The type of the node filter map.
742 typedef NF NodeFilterMap;
743 /// The type of the arc filter map.
744 typedef AF ArcFilterMap;
746 typedef DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> >
749 typedef typename Parent::Node Node;
750 typedef typename Parent::Arc Arc;
756 /// \brief Constructor
758 /// Creates a subdigraph for the given digraph with the
759 /// given node and arc filter maps.
760 SubDigraph(Digraph& digraph, NodeFilterMap& node_filter,
761 ArcFilterMap& arc_filter) {
763 setNodeFilterMap(node_filter);
764 setArcFilterMap(arc_filter);
767 /// \brief Sets the status of the given node
769 /// This function sets the status of the given node.
770 /// It is done by simply setting the assigned value of \c n
771 /// to \c v in the node filter map.
772 void status(const Node& n, bool v) const { Parent::status(n, v); }
774 /// \brief Sets the status of the given arc
776 /// This function sets the status of the given arc.
777 /// It is done by simply setting the assigned value of \c a
778 /// to \c v in the arc filter map.
779 void status(const Arc& a, bool v) const { Parent::status(a, v); }
781 /// \brief Returns the status of the given node
783 /// This function returns the status of the given node.
784 /// It is \c true if the given node is enabled (i.e. not hidden).
785 bool status(const Node& n) const { return Parent::status(n); }
787 /// \brief Returns the status of the given arc
789 /// This function returns the status of the given arc.
790 /// It is \c true if the given arc is enabled (i.e. not hidden).
791 bool status(const Arc& a) const { return Parent::status(a); }
793 /// \brief Disables the given node
795 /// This function disables the given node in the subdigraph,
796 /// so the iteration jumps over it.
797 /// It is the same as \ref status() "status(n, false)".
798 void disable(const Node& n) const { Parent::status(n, false); }
800 /// \brief Disables the given arc
802 /// This function disables the given arc in the subdigraph,
803 /// so the iteration jumps over it.
804 /// It is the same as \ref status() "status(a, false)".
805 void disable(const Arc& a) const { Parent::status(a, false); }
807 /// \brief Enables the given node
809 /// This function enables the given node in the subdigraph.
810 /// It is the same as \ref status() "status(n, true)".
811 void enable(const Node& n) const { Parent::status(n, true); }
813 /// \brief Enables the given arc
815 /// This function enables the given arc in the subdigraph.
816 /// It is the same as \ref status() "status(a, true)".
817 void enable(const Arc& a) const { Parent::status(a, true); }
821 /// \brief Returns a read-only SubDigraph adaptor
823 /// This function just returns a read-only \ref SubDigraph adaptor.
824 /// \ingroup graph_adaptors
825 /// \relates SubDigraph
826 template<typename GR, typename NF, typename AF>
827 SubDigraph<const GR, NF, AF>
828 subDigraph(const GR& digraph,
829 NF& node_filter_map, AF& arc_filter_map) {
830 return SubDigraph<const GR, NF, AF>
831 (digraph, node_filter_map, arc_filter_map);
834 template<typename GR, typename NF, typename AF>
835 SubDigraph<const GR, const NF, AF>
836 subDigraph(const GR& digraph,
837 const NF& node_filter_map, AF& arc_filter_map) {
838 return SubDigraph<const GR, const NF, AF>
839 (digraph, node_filter_map, arc_filter_map);
842 template<typename GR, typename NF, typename AF>
843 SubDigraph<const GR, NF, const AF>
844 subDigraph(const GR& digraph,
845 NF& node_filter_map, const AF& arc_filter_map) {
846 return SubDigraph<const GR, NF, const AF>
847 (digraph, node_filter_map, arc_filter_map);
850 template<typename GR, typename NF, typename AF>
851 SubDigraph<const GR, const NF, const AF>
852 subDigraph(const GR& digraph,
853 const NF& node_filter_map, const AF& arc_filter_map) {
854 return SubDigraph<const GR, const NF, const AF>
855 (digraph, node_filter_map, arc_filter_map);
859 template <typename _Graph, typename _NodeFilterMap,
860 typename _EdgeFilterMap, bool _checked = true>
861 class SubGraphBase : public GraphAdaptorBase<_Graph> {
863 typedef _Graph Graph;
864 typedef _NodeFilterMap NodeFilterMap;
865 typedef _EdgeFilterMap EdgeFilterMap;
867 typedef SubGraphBase Adaptor;
868 typedef GraphAdaptorBase<_Graph> Parent;
871 NodeFilterMap* _node_filter_map;
872 EdgeFilterMap* _edge_filter_map;
875 : Parent(), _node_filter_map(0), _edge_filter_map(0) { }
877 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
878 _node_filter_map=&node_filter_map;
880 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
881 _edge_filter_map=&edge_filter_map;
886 typedef typename Parent::Node Node;
887 typedef typename Parent::Arc Arc;
888 typedef typename Parent::Edge Edge;
890 void first(Node& i) const {
892 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
895 void first(Arc& i) const {
897 while (i!=INVALID && (!(*_edge_filter_map)[i]
898 || !(*_node_filter_map)[Parent::source(i)]
899 || !(*_node_filter_map)[Parent::target(i)]))
903 void first(Edge& i) const {
905 while (i!=INVALID && (!(*_edge_filter_map)[i]
906 || !(*_node_filter_map)[Parent::u(i)]
907 || !(*_node_filter_map)[Parent::v(i)]))
911 void firstIn(Arc& i, const Node& n) const {
912 Parent::firstIn(i, n);
913 while (i!=INVALID && (!(*_edge_filter_map)[i]
914 || !(*_node_filter_map)[Parent::source(i)]))
918 void firstOut(Arc& i, const Node& n) const {
919 Parent::firstOut(i, n);
920 while (i!=INVALID && (!(*_edge_filter_map)[i]
921 || !(*_node_filter_map)[Parent::target(i)]))
925 void firstInc(Edge& i, bool& d, const Node& n) const {
926 Parent::firstInc(i, d, n);
927 while (i!=INVALID && (!(*_edge_filter_map)[i]
928 || !(*_node_filter_map)[Parent::u(i)]
929 || !(*_node_filter_map)[Parent::v(i)]))
930 Parent::nextInc(i, d);
933 void next(Node& i) const {
935 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
938 void next(Arc& i) const {
940 while (i!=INVALID && (!(*_edge_filter_map)[i]
941 || !(*_node_filter_map)[Parent::source(i)]
942 || !(*_node_filter_map)[Parent::target(i)]))
946 void next(Edge& i) const {
948 while (i!=INVALID && (!(*_edge_filter_map)[i]
949 || !(*_node_filter_map)[Parent::u(i)]
950 || !(*_node_filter_map)[Parent::v(i)]))
954 void nextIn(Arc& i) const {
956 while (i!=INVALID && (!(*_edge_filter_map)[i]
957 || !(*_node_filter_map)[Parent::source(i)]))
961 void nextOut(Arc& i) const {
963 while (i!=INVALID && (!(*_edge_filter_map)[i]
964 || !(*_node_filter_map)[Parent::target(i)]))
968 void nextInc(Edge& i, bool& d) const {
969 Parent::nextInc(i, d);
970 while (i!=INVALID && (!(*_edge_filter_map)[i]
971 || !(*_node_filter_map)[Parent::u(i)]
972 || !(*_node_filter_map)[Parent::v(i)]))
973 Parent::nextInc(i, d);
976 void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
977 void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
979 bool status(const Node& n) const { return (*_node_filter_map)[n]; }
980 bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
982 typedef False NodeNumTag;
983 typedef False ArcNumTag;
984 typedef False EdgeNumTag;
986 typedef FindArcTagIndicator<Graph> FindArcTag;
987 Arc findArc(const Node& u, const Node& v,
988 const Arc& prev = INVALID) const {
989 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
992 Arc arc = Parent::findArc(u, v, prev);
993 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
994 arc = Parent::findArc(u, v, arc);
999 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1000 Edge findEdge(const Node& u, const Node& v,
1001 const Edge& prev = INVALID) const {
1002 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
1005 Edge edge = Parent::findEdge(u, v, prev);
1006 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1007 edge = Parent::findEdge(u, v, edge);
1012 template <typename _Value>
1013 class NodeMap : public SubMapExtender<Adaptor,
1014 typename Parent::template NodeMap<_Value> > {
1016 typedef _Value Value;
1017 typedef SubMapExtender<Adaptor, typename Parent::
1018 template NodeMap<Value> > MapParent;
1020 NodeMap(const Adaptor& adaptor)
1021 : MapParent(adaptor) {}
1022 NodeMap(const Adaptor& adaptor, const Value& value)
1023 : MapParent(adaptor, value) {}
1026 NodeMap& operator=(const NodeMap& cmap) {
1027 return operator=<NodeMap>(cmap);
1030 template <typename CMap>
1031 NodeMap& operator=(const CMap& cmap) {
1032 MapParent::operator=(cmap);
1037 template <typename _Value>
1038 class ArcMap : public SubMapExtender<Adaptor,
1039 typename Parent::template ArcMap<_Value> > {
1041 typedef _Value Value;
1042 typedef SubMapExtender<Adaptor, typename Parent::
1043 template ArcMap<Value> > MapParent;
1045 ArcMap(const Adaptor& adaptor)
1046 : MapParent(adaptor) {}
1047 ArcMap(const Adaptor& adaptor, const Value& value)
1048 : MapParent(adaptor, value) {}
1051 ArcMap& operator=(const ArcMap& cmap) {
1052 return operator=<ArcMap>(cmap);
1055 template <typename CMap>
1056 ArcMap& operator=(const CMap& cmap) {
1057 MapParent::operator=(cmap);
1062 template <typename _Value>
1063 class EdgeMap : public SubMapExtender<Adaptor,
1064 typename Parent::template EdgeMap<_Value> > {
1066 typedef _Value Value;
1067 typedef SubMapExtender<Adaptor, typename Parent::
1068 template EdgeMap<Value> > MapParent;
1070 EdgeMap(const Adaptor& adaptor)
1071 : MapParent(adaptor) {}
1073 EdgeMap(const Adaptor& adaptor, const Value& value)
1074 : MapParent(adaptor, value) {}
1077 EdgeMap& operator=(const EdgeMap& cmap) {
1078 return operator=<EdgeMap>(cmap);
1081 template <typename CMap>
1082 EdgeMap& operator=(const CMap& cmap) {
1083 MapParent::operator=(cmap);
1090 template <typename _Graph, typename _NodeFilterMap, typename _EdgeFilterMap>
1091 class SubGraphBase<_Graph, _NodeFilterMap, _EdgeFilterMap, false>
1092 : public GraphAdaptorBase<_Graph> {
1094 typedef _Graph Graph;
1095 typedef _NodeFilterMap NodeFilterMap;
1096 typedef _EdgeFilterMap EdgeFilterMap;
1098 typedef SubGraphBase Adaptor;
1099 typedef GraphAdaptorBase<_Graph> Parent;
1101 NodeFilterMap* _node_filter_map;
1102 EdgeFilterMap* _edge_filter_map;
1103 SubGraphBase() : Parent(),
1104 _node_filter_map(0), _edge_filter_map(0) { }
1106 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
1107 _node_filter_map=&node_filter_map;
1109 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
1110 _edge_filter_map=&edge_filter_map;
1115 typedef typename Parent::Node Node;
1116 typedef typename Parent::Arc Arc;
1117 typedef typename Parent::Edge Edge;
1119 void first(Node& i) const {
1121 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1124 void first(Arc& i) const {
1126 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1129 void first(Edge& i) const {
1131 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1134 void firstIn(Arc& i, const Node& n) const {
1135 Parent::firstIn(i, n);
1136 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1139 void firstOut(Arc& i, const Node& n) const {
1140 Parent::firstOut(i, n);
1141 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1144 void firstInc(Edge& i, bool& d, const Node& n) const {
1145 Parent::firstInc(i, d, n);
1146 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1149 void next(Node& i) const {
1151 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1153 void next(Arc& i) const {
1155 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1157 void next(Edge& i) const {
1159 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1161 void nextIn(Arc& i) const {
1163 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1166 void nextOut(Arc& i) const {
1168 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1170 void nextInc(Edge& i, bool& d) const {
1171 Parent::nextInc(i, d);
1172 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1175 void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
1176 void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
1178 bool status(const Node& n) const { return (*_node_filter_map)[n]; }
1179 bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
1181 typedef False NodeNumTag;
1182 typedef False ArcNumTag;
1183 typedef False EdgeNumTag;
1185 typedef FindArcTagIndicator<Graph> FindArcTag;
1186 Arc findArc(const Node& u, const Node& v,
1187 const Arc& prev = INVALID) const {
1188 Arc arc = Parent::findArc(u, v, prev);
1189 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
1190 arc = Parent::findArc(u, v, arc);
1195 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1196 Edge findEdge(const Node& u, const Node& v,
1197 const Edge& prev = INVALID) const {
1198 Edge edge = Parent::findEdge(u, v, prev);
1199 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1200 edge = Parent::findEdge(u, v, edge);
1205 template <typename _Value>
1206 class NodeMap : public SubMapExtender<Adaptor,
1207 typename Parent::template NodeMap<_Value> > {
1209 typedef _Value Value;
1210 typedef SubMapExtender<Adaptor, typename Parent::
1211 template NodeMap<Value> > MapParent;
1213 NodeMap(const Adaptor& adaptor)
1214 : MapParent(adaptor) {}
1215 NodeMap(const Adaptor& adaptor, const Value& value)
1216 : MapParent(adaptor, value) {}
1219 NodeMap& operator=(const NodeMap& cmap) {
1220 return operator=<NodeMap>(cmap);
1223 template <typename CMap>
1224 NodeMap& operator=(const CMap& cmap) {
1225 MapParent::operator=(cmap);
1230 template <typename _Value>
1231 class ArcMap : public SubMapExtender<Adaptor,
1232 typename Parent::template ArcMap<_Value> > {
1234 typedef _Value Value;
1235 typedef SubMapExtender<Adaptor, typename Parent::
1236 template ArcMap<Value> > MapParent;
1238 ArcMap(const Adaptor& adaptor)
1239 : MapParent(adaptor) {}
1240 ArcMap(const Adaptor& adaptor, const Value& value)
1241 : MapParent(adaptor, value) {}
1244 ArcMap& operator=(const ArcMap& cmap) {
1245 return operator=<ArcMap>(cmap);
1248 template <typename CMap>
1249 ArcMap& operator=(const CMap& cmap) {
1250 MapParent::operator=(cmap);
1255 template <typename _Value>
1256 class EdgeMap : public SubMapExtender<Adaptor,
1257 typename Parent::template EdgeMap<_Value> > {
1259 typedef _Value Value;
1260 typedef SubMapExtender<Adaptor, typename Parent::
1261 template EdgeMap<Value> > MapParent;
1263 EdgeMap(const Adaptor& adaptor)
1264 : MapParent(adaptor) {}
1266 EdgeMap(const Adaptor& adaptor, const _Value& value)
1267 : MapParent(adaptor, value) {}
1270 EdgeMap& operator=(const EdgeMap& cmap) {
1271 return operator=<EdgeMap>(cmap);
1274 template <typename CMap>
1275 EdgeMap& operator=(const CMap& cmap) {
1276 MapParent::operator=(cmap);
1283 /// \ingroup graph_adaptors
1285 /// \brief Adaptor class for hiding nodes and edges in an undirected
1288 /// SubGraph can be used for hiding nodes and edges in a graph.
1289 /// A \c bool node map and a \c bool edge map must be specified, which
1290 /// define the filters for nodes and edges.
1291 /// Only the nodes and edges with \c true filter value are
1292 /// shown in the subgraph. The edges that are incident to hidden
1293 /// nodes are also filtered out.
1294 /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
1296 /// The adapted graph can also be modified through this adaptor
1297 /// by adding or removing nodes or edges, unless the \c GR template
1298 /// parameter is set to be \c const.
1300 /// \tparam GR The type of the adapted graph.
1301 /// It must conform to the \ref concepts::Graph "Graph" concept.
1302 /// It can also be specified to be \c const.
1303 /// \tparam NF The type of the node filter map.
1304 /// It must be a \c bool (or convertible) node map of the
1305 /// adapted graph. The default type is
1306 /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1307 /// \tparam EF The type of the edge filter map.
1308 /// It must be a \c bool (or convertible) edge map of the
1309 /// adapted graph. The default type is
1310 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1312 /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1313 /// adapted graph are convertible to each other.
1315 /// \see FilterNodes
1316 /// \see FilterEdges
1318 template<typename GR, typename NF, typename EF>
1321 template<typename GR,
1322 typename NF = typename GR::template NodeMap<bool>,
1323 typename EF = typename GR::template EdgeMap<bool> >
1325 public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
1328 /// The type of the adapted graph.
1330 /// The type of the node filter map.
1331 typedef NF NodeFilterMap;
1332 /// The type of the edge filter map.
1333 typedef EF EdgeFilterMap;
1335 typedef GraphAdaptorExtender< SubGraphBase<GR, NF, EF, true> >
1338 typedef typename Parent::Node Node;
1339 typedef typename Parent::Edge Edge;
1345 /// \brief Constructor
1347 /// Creates a subgraph for the given graph with the given node
1348 /// and edge filter maps.
1349 SubGraph(Graph& graph, NodeFilterMap& node_filter_map,
1350 EdgeFilterMap& edge_filter_map) {
1352 setNodeFilterMap(node_filter_map);
1353 setEdgeFilterMap(edge_filter_map);
1356 /// \brief Sets the status of the given node
1358 /// This function sets the status of the given node.
1359 /// It is done by simply setting the assigned value of \c n
1360 /// to \c v in the node filter map.
1361 void status(const Node& n, bool v) const { Parent::status(n, v); }
1363 /// \brief Sets the status of the given edge
1365 /// This function sets the status of the given edge.
1366 /// It is done by simply setting the assigned value of \c e
1367 /// to \c v in the edge filter map.
1368 void status(const Edge& e, bool v) const { Parent::status(e, v); }
1370 /// \brief Returns the status of the given node
1372 /// This function returns the status of the given node.
1373 /// It is \c true if the given node is enabled (i.e. not hidden).
1374 bool status(const Node& n) const { return Parent::status(n); }
1376 /// \brief Returns the status of the given edge
1378 /// This function returns the status of the given edge.
1379 /// It is \c true if the given edge is enabled (i.e. not hidden).
1380 bool status(const Edge& e) const { return Parent::status(e); }
1382 /// \brief Disables the given node
1384 /// This function disables the given node in the subdigraph,
1385 /// so the iteration jumps over it.
1386 /// It is the same as \ref status() "status(n, false)".
1387 void disable(const Node& n) const { Parent::status(n, false); }
1389 /// \brief Disables the given edge
1391 /// This function disables the given edge in the subgraph,
1392 /// so the iteration jumps over it.
1393 /// It is the same as \ref status() "status(e, false)".
1394 void disable(const Edge& e) const { Parent::status(e, false); }
1396 /// \brief Enables the given node
1398 /// This function enables the given node in the subdigraph.
1399 /// It is the same as \ref status() "status(n, true)".
1400 void enable(const Node& n) const { Parent::status(n, true); }
1402 /// \brief Enables the given edge
1404 /// This function enables the given edge in the subgraph.
1405 /// It is the same as \ref status() "status(e, true)".
1406 void enable(const Edge& e) const { Parent::status(e, true); }
1410 /// \brief Returns a read-only SubGraph adaptor
1412 /// This function just returns a read-only \ref SubGraph adaptor.
1413 /// \ingroup graph_adaptors
1414 /// \relates SubGraph
1415 template<typename GR, typename NF, typename EF>
1416 SubGraph<const GR, NF, EF>
1417 subGraph(const GR& graph,
1418 NF& node_filter_map, EF& edge_filter_map) {
1419 return SubGraph<const GR, NF, EF>
1420 (graph, node_filter_map, edge_filter_map);
1423 template<typename GR, typename NF, typename EF>
1424 SubGraph<const GR, const NF, EF>
1425 subGraph(const GR& graph,
1426 const NF& node_filter_map, EF& edge_filter_map) {
1427 return SubGraph<const GR, const NF, EF>
1428 (graph, node_filter_map, edge_filter_map);
1431 template<typename GR, typename NF, typename EF>
1432 SubGraph<const GR, NF, const EF>
1433 subGraph(const GR& graph,
1434 NF& node_filter_map, const EF& edge_filter_map) {
1435 return SubGraph<const GR, NF, const EF>
1436 (graph, node_filter_map, edge_filter_map);
1439 template<typename GR, typename NF, typename EF>
1440 SubGraph<const GR, const NF, const EF>
1441 subGraph(const GR& graph,
1442 const NF& node_filter_map, const EF& edge_filter_map) {
1443 return SubGraph<const GR, const NF, const EF>
1444 (graph, node_filter_map, edge_filter_map);
1448 /// \ingroup graph_adaptors
1450 /// \brief Adaptor class for hiding nodes in a digraph or a graph.
1452 /// FilterNodes adaptor can be used for hiding nodes in a digraph or a
1453 /// graph. A \c bool node map must be specified, which defines the filter
1454 /// for the nodes. Only the nodes with \c true filter value and the
1455 /// arcs/edges incident to nodes both with \c true filter value are shown
1456 /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph
1457 /// "Digraph" concept or the \ref concepts::Graph "Graph" concept
1458 /// depending on the \c GR template parameter.
1460 /// The adapted (di)graph can also be modified through this adaptor
1461 /// by adding or removing nodes or arcs/edges, unless the \c GR template
1462 /// parameter is set to be \c const.
1464 /// \tparam GR The type of the adapted digraph or graph.
1465 /// It must conform to the \ref concepts::Digraph "Digraph" concept
1466 /// or the \ref concepts::Graph "Graph" concept.
1467 /// It can also be specified to be \c const.
1468 /// \tparam NF The type of the node filter map.
1469 /// It must be a \c bool (or convertible) node map of the
1470 /// adapted (di)graph. The default type is
1471 /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1473 /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the
1474 /// adapted (di)graph are convertible to each other.
1476 template<typename GR, typename NF>
1479 template<typename GR,
1480 typename NF = typename GR::template NodeMap<bool>,
1481 typename Enable = void>
1483 public DigraphAdaptorExtender<
1484 SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> > {
1489 typedef NF NodeFilterMap;
1491 typedef DigraphAdaptorExtender<
1492 SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> >
1495 typedef typename Parent::Node Node;
1498 ConstMap<typename Digraph::Arc, bool> const_true_map;
1500 FilterNodes() : const_true_map(true) {
1501 Parent::setArcFilterMap(const_true_map);
1506 /// \brief Constructor
1508 /// Creates a subgraph for the given digraph or graph with the
1509 /// given node filter map.
1510 FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1511 Parent(), const_true_map(true)
1513 Parent::setDigraph(graph);
1514 Parent::setNodeFilterMap(node_filter);
1515 Parent::setArcFilterMap(const_true_map);
1518 /// \brief Sets the status of the given node
1520 /// This function sets the status of the given node.
1521 /// It is done by simply setting the assigned value of \c n
1522 /// to \c v in the node filter map.
1523 void status(const Node& n, bool v) const { Parent::status(n, v); }
1525 /// \brief Returns the status of the given node
1527 /// This function returns the status of the given node.
1528 /// It is \c true if the given node is enabled (i.e. not hidden).
1529 bool status(const Node& n) const { return Parent::status(n); }
1531 /// \brief Disables the given node
1533 /// This function disables the given node, so the iteration
1535 /// It is the same as \ref status() "status(n, false)".
1536 void disable(const Node& n) const { Parent::status(n, false); }
1538 /// \brief Enables the given node
1540 /// This function enables the given node.
1541 /// It is the same as \ref status() "status(n, true)".
1542 void enable(const Node& n) const { Parent::status(n, true); }
1546 template<typename GR, typename NF>
1547 class FilterNodes<GR, NF,
1548 typename enable_if<UndirectedTagIndicator<GR> >::type> :
1549 public GraphAdaptorExtender<
1550 SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> > {
1554 typedef NF NodeFilterMap;
1555 typedef GraphAdaptorExtender<
1556 SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> >
1559 typedef typename Parent::Node Node;
1561 ConstMap<typename Graph::Edge, bool> const_true_map;
1563 FilterNodes() : const_true_map(true) {
1564 Parent::setEdgeFilterMap(const_true_map);
1569 FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) :
1570 Parent(), const_true_map(true) {
1571 Parent::setGraph(_graph);
1572 Parent::setNodeFilterMap(node_filter_map);
1573 Parent::setEdgeFilterMap(const_true_map);
1576 void status(const Node& n, bool v) const { Parent::status(n, v); }
1577 bool status(const Node& n) const { return Parent::status(n); }
1578 void disable(const Node& n) const { Parent::status(n, false); }
1579 void enable(const Node& n) const { Parent::status(n, true); }
1584 /// \brief Returns a read-only FilterNodes adaptor
1586 /// This function just returns a read-only \ref FilterNodes adaptor.
1587 /// \ingroup graph_adaptors
1588 /// \relates FilterNodes
1589 template<typename GR, typename NF>
1590 FilterNodes<const GR, NF>
1591 filterNodes(const GR& graph, NF& node_filter_map) {
1592 return FilterNodes<const GR, NF>(graph, node_filter_map);
1595 template<typename GR, typename NF>
1596 FilterNodes<const GR, const NF>
1597 filterNodes(const GR& graph, const NF& node_filter_map) {
1598 return FilterNodes<const GR, const NF>(graph, node_filter_map);
1601 /// \ingroup graph_adaptors
1603 /// \brief Adaptor class for hiding arcs in a digraph.
1605 /// FilterArcs adaptor can be used for hiding arcs in a digraph.
1606 /// A \c bool arc map must be specified, which defines the filter for
1607 /// the arcs. Only the arcs with \c true filter value are shown in the
1608 /// subdigraph. This adaptor conforms to the \ref concepts::Digraph
1609 /// "Digraph" concept.
1611 /// The adapted digraph can also be modified through this adaptor
1612 /// by adding or removing nodes or arcs, unless the \c GR template
1613 /// parameter is set to be \c const.
1615 /// \tparam GR The type of the adapted digraph.
1616 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
1617 /// It can also be specified to be \c const.
1618 /// \tparam AF The type of the arc filter map.
1619 /// It must be a \c bool (or convertible) arc map of the
1620 /// adapted digraph. The default type is
1621 /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
1623 /// \note The \c Node and \c Arc types of this adaptor and the adapted
1624 /// digraph are convertible to each other.
1626 template<typename GR,
1630 template<typename GR,
1631 typename AF = typename GR::template ArcMap<bool> >
1633 public DigraphAdaptorExtender<
1634 SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> > {
1637 /// The type of the adapted digraph.
1639 /// The type of the arc filter map.
1640 typedef AF ArcFilterMap;
1642 typedef DigraphAdaptorExtender<
1643 SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> >
1646 typedef typename Parent::Arc Arc;
1649 ConstMap<typename Digraph::Node, bool> const_true_map;
1651 FilterArcs() : const_true_map(true) {
1652 Parent::setNodeFilterMap(const_true_map);
1657 /// \brief Constructor
1659 /// Creates a subdigraph for the given digraph with the given arc
1661 FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter)
1662 : Parent(), const_true_map(true) {
1663 Parent::setDigraph(digraph);
1664 Parent::setNodeFilterMap(const_true_map);
1665 Parent::setArcFilterMap(arc_filter);
1668 /// \brief Sets the status of the given arc
1670 /// This function sets the status of the given arc.
1671 /// It is done by simply setting the assigned value of \c a
1672 /// to \c v in the arc filter map.
1673 void status(const Arc& a, bool v) const { Parent::status(a, v); }
1675 /// \brief Returns the status of the given arc
1677 /// This function returns the status of the given arc.
1678 /// It is \c true if the given arc is enabled (i.e. not hidden).
1679 bool status(const Arc& a) const { return Parent::status(a); }
1681 /// \brief Disables the given arc
1683 /// This function disables the given arc in the subdigraph,
1684 /// so the iteration jumps over it.
1685 /// It is the same as \ref status() "status(a, false)".
1686 void disable(const Arc& a) const { Parent::status(a, false); }
1688 /// \brief Enables the given arc
1690 /// This function enables the given arc in the subdigraph.
1691 /// It is the same as \ref status() "status(a, true)".
1692 void enable(const Arc& a) const { Parent::status(a, true); }
1696 /// \brief Returns a read-only FilterArcs adaptor
1698 /// This function just returns a read-only \ref FilterArcs adaptor.
1699 /// \ingroup graph_adaptors
1700 /// \relates FilterArcs
1701 template<typename GR, typename AF>
1702 FilterArcs<const GR, AF>
1703 filterArcs(const GR& digraph, AF& arc_filter_map) {
1704 return FilterArcs<const GR, AF>(digraph, arc_filter_map);
1707 template<typename GR, typename AF>
1708 FilterArcs<const GR, const AF>
1709 filterArcs(const GR& digraph, const AF& arc_filter_map) {
1710 return FilterArcs<const GR, const AF>(digraph, arc_filter_map);
1713 /// \ingroup graph_adaptors
1715 /// \brief Adaptor class for hiding edges in a graph.
1717 /// FilterEdges adaptor can be used for hiding edges in a graph.
1718 /// A \c bool edge map must be specified, which defines the filter for
1719 /// the edges. Only the edges with \c true filter value are shown in the
1720 /// subgraph. This adaptor conforms to the \ref concepts::Graph
1721 /// "Graph" concept.
1723 /// The adapted graph can also be modified through this adaptor
1724 /// by adding or removing nodes or edges, unless the \c GR template
1725 /// parameter is set to be \c const.
1727 /// \tparam GR The type of the adapted graph.
1728 /// It must conform to the \ref concepts::Graph "Graph" concept.
1729 /// It can also be specified to be \c const.
1730 /// \tparam EF The type of the edge filter map.
1731 /// It must be a \c bool (or convertible) edge map of the
1732 /// adapted graph. The default type is
1733 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1735 /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1736 /// adapted graph are convertible to each other.
1738 template<typename GR,
1742 template<typename GR,
1743 typename EF = typename GR::template EdgeMap<bool> >
1745 public GraphAdaptorExtender<
1746 SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> > {
1749 /// The type of the adapted graph.
1751 /// The type of the edge filter map.
1752 typedef EF EdgeFilterMap;
1754 typedef GraphAdaptorExtender<
1755 SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> >
1758 typedef typename Parent::Edge Edge;
1761 ConstMap<typename Graph::Node, bool> const_true_map;
1763 FilterEdges() : const_true_map(true) {
1764 Parent::setNodeFilterMap(const_true_map);
1769 /// \brief Constructor
1771 /// Creates a subgraph for the given graph with the given edge
1773 FilterEdges(Graph& graph, EdgeFilterMap& edge_filter_map) :
1774 Parent(), const_true_map(true) {
1775 Parent::setGraph(graph);
1776 Parent::setNodeFilterMap(const_true_map);
1777 Parent::setEdgeFilterMap(edge_filter_map);
1780 /// \brief Sets the status of the given edge
1782 /// This function sets the status of the given edge.
1783 /// It is done by simply setting the assigned value of \c e
1784 /// to \c v in the edge filter map.
1785 void status(const Edge& e, bool v) const { Parent::status(e, v); }
1787 /// \brief Returns the status of the given edge
1789 /// This function returns the status of the given edge.
1790 /// It is \c true if the given edge is enabled (i.e. not hidden).
1791 bool status(const Edge& e) const { return Parent::status(e); }
1793 /// \brief Disables the given edge
1795 /// This function disables the given edge in the subgraph,
1796 /// so the iteration jumps over it.
1797 /// It is the same as \ref status() "status(e, false)".
1798 void disable(const Edge& e) const { Parent::status(e, false); }
1800 /// \brief Enables the given edge
1802 /// This function enables the given edge in the subgraph.
1803 /// It is the same as \ref status() "status(e, true)".
1804 void enable(const Edge& e) const { Parent::status(e, true); }
1808 /// \brief Returns a read-only FilterEdges adaptor
1810 /// This function just returns a read-only \ref FilterEdges adaptor.
1811 /// \ingroup graph_adaptors
1812 /// \relates FilterEdges
1813 template<typename GR, typename EF>
1814 FilterEdges<const GR, EF>
1815 filterEdges(const GR& graph, EF& edge_filter_map) {
1816 return FilterEdges<const GR, EF>(graph, edge_filter_map);
1819 template<typename GR, typename EF>
1820 FilterEdges<const GR, const EF>
1821 filterEdges(const GR& graph, const EF& edge_filter_map) {
1822 return FilterEdges<const GR, const EF>(graph, edge_filter_map);
1826 template <typename _Digraph>
1827 class UndirectorBase {
1829 typedef _Digraph Digraph;
1830 typedef UndirectorBase Adaptor;
1832 typedef True UndirectedTag;
1834 typedef typename Digraph::Arc Edge;
1835 typedef typename Digraph::Node Node;
1837 class Arc : public Edge {
1838 friend class UndirectorBase;
1842 Arc(const Edge& edge, bool forward) :
1843 Edge(edge), _forward(forward) {}
1848 Arc(Invalid) : Edge(INVALID), _forward(true) {}
1850 bool operator==(const Arc &other) const {
1851 return _forward == other._forward &&
1852 static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1854 bool operator!=(const Arc &other) const {
1855 return _forward != other._forward ||
1856 static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1858 bool operator<(const Arc &other) const {
1859 return _forward < other._forward ||
1860 (_forward == other._forward &&
1861 static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1865 void first(Node& n) const {
1869 void next(Node& n) const {
1873 void first(Arc& a) const {
1878 void next(Arc& a) const {
1887 void first(Edge& e) const {
1891 void next(Edge& e) const {
1895 void firstOut(Arc& a, const Node& n) const {
1896 _digraph->firstIn(a, n);
1897 if( static_cast<const Edge&>(a) != INVALID ) {
1900 _digraph->firstOut(a, n);
1904 void nextOut(Arc &a) const {
1906 Node n = _digraph->target(a);
1907 _digraph->nextIn(a);
1908 if (static_cast<const Edge&>(a) == INVALID ) {
1909 _digraph->firstOut(a, n);
1914 _digraph->nextOut(a);
1918 void firstIn(Arc &a, const Node &n) const {
1919 _digraph->firstOut(a, n);
1920 if (static_cast<const Edge&>(a) != INVALID ) {
1923 _digraph->firstIn(a, n);
1927 void nextIn(Arc &a) const {
1929 Node n = _digraph->source(a);
1930 _digraph->nextOut(a);
1931 if( static_cast<const Edge&>(a) == INVALID ) {
1932 _digraph->firstIn(a, n);
1937 _digraph->nextIn(a);
1941 void firstInc(Edge &e, bool &d, const Node &n) const {
1943 _digraph->firstOut(e, n);
1944 if (e != INVALID) return;
1946 _digraph->firstIn(e, n);
1949 void nextInc(Edge &e, bool &d) const {
1951 Node s = _digraph->source(e);
1952 _digraph->nextOut(e);
1953 if (e != INVALID) return;
1955 _digraph->firstIn(e, s);
1957 _digraph->nextIn(e);
1961 Node u(const Edge& e) const {
1962 return _digraph->source(e);
1965 Node v(const Edge& e) const {
1966 return _digraph->target(e);
1969 Node source(const Arc &a) const {
1970 return a._forward ? _digraph->source(a) : _digraph->target(a);
1973 Node target(const Arc &a) const {
1974 return a._forward ? _digraph->target(a) : _digraph->source(a);
1977 static Arc direct(const Edge &e, bool d) {
1980 Arc direct(const Edge &e, const Node& n) const {
1981 return Arc(e, _digraph->source(e) == n);
1984 static bool direction(const Arc &a) { return a._forward; }
1986 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1987 Arc arcFromId(int ix) const {
1988 return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1990 Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1992 int id(const Node &n) const { return _digraph->id(n); }
1993 int id(const Arc &a) const {
1994 return (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1996 int id(const Edge &e) const { return _digraph->id(e); }
1998 int maxNodeId() const { return _digraph->maxNodeId(); }
1999 int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
2000 int maxEdgeId() const { return _digraph->maxArcId(); }
2002 Node addNode() { return _digraph->addNode(); }
2003 Edge addEdge(const Node& u, const Node& v) {
2004 return _digraph->addArc(u, v);
2007 void erase(const Node& i) { _digraph->erase(i); }
2008 void erase(const Edge& i) { _digraph->erase(i); }
2010 void clear() { _digraph->clear(); }
2012 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
2013 int nodeNum() const { return _digraph->nodeNum(); }
2015 typedef ArcNumTagIndicator<Digraph> ArcNumTag;
2016 int arcNum() const { return 2 * _digraph->arcNum(); }
2018 typedef ArcNumTag EdgeNumTag;
2019 int edgeNum() const { return _digraph->arcNum(); }
2021 typedef FindArcTagIndicator<Digraph> FindArcTag;
2022 Arc findArc(Node s, Node t, Arc p = INVALID) const {
2024 Edge arc = _digraph->findArc(s, t);
2025 if (arc != INVALID) return direct(arc, true);
2026 arc = _digraph->findArc(t, s);
2027 if (arc != INVALID) return direct(arc, false);
2028 } else if (direction(p)) {
2029 Edge arc = _digraph->findArc(s, t, p);
2030 if (arc != INVALID) return direct(arc, true);
2031 arc = _digraph->findArc(t, s);
2032 if (arc != INVALID) return direct(arc, false);
2034 Edge arc = _digraph->findArc(t, s, p);
2035 if (arc != INVALID) return direct(arc, false);
2040 typedef FindArcTag FindEdgeTag;
2041 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
2044 Edge arc = _digraph->findArc(s, t);
2045 if (arc != INVALID) return arc;
2046 arc = _digraph->findArc(t, s);
2047 if (arc != INVALID) return arc;
2048 } else if (_digraph->source(p) == s) {
2049 Edge arc = _digraph->findArc(s, t, p);
2050 if (arc != INVALID) return arc;
2051 arc = _digraph->findArc(t, s);
2052 if (arc != INVALID) return arc;
2054 Edge arc = _digraph->findArc(t, s, p);
2055 if (arc != INVALID) return arc;
2058 return _digraph->findArc(s, t, p);
2065 template <typename _Value>
2069 typedef typename Digraph::template ArcMap<_Value> MapImpl;
2073 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2075 typedef _Value Value;
2077 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
2078 typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
2079 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
2080 typedef typename MapTraits<MapImpl>::ReturnValue Reference;
2082 ArcMapBase(const Adaptor& adaptor) :
2083 _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
2085 ArcMapBase(const Adaptor& adaptor, const Value& v)
2086 : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {}
2088 void set(const Arc& a, const Value& v) {
2092 _backward.set(a, v);
2096 ConstReturnValue operator[](const Arc& a) const {
2100 return _backward[a];
2104 ReturnValue operator[](const Arc& a) {
2108 return _backward[a];
2114 MapImpl _forward, _backward;
2120 template <typename _Value>
2121 class NodeMap : public Digraph::template NodeMap<_Value> {
2124 typedef _Value Value;
2125 typedef typename Digraph::template NodeMap<Value> Parent;
2127 explicit NodeMap(const Adaptor& adaptor)
2128 : Parent(*adaptor._digraph) {}
2130 NodeMap(const Adaptor& adaptor, const _Value& value)
2131 : Parent(*adaptor._digraph, value) { }
2134 NodeMap& operator=(const NodeMap& cmap) {
2135 return operator=<NodeMap>(cmap);
2138 template <typename CMap>
2139 NodeMap& operator=(const CMap& cmap) {
2140 Parent::operator=(cmap);
2146 template <typename _Value>
2148 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
2151 typedef _Value Value;
2152 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
2154 explicit ArcMap(const Adaptor& adaptor)
2155 : Parent(adaptor) {}
2157 ArcMap(const Adaptor& adaptor, const Value& value)
2158 : Parent(adaptor, value) {}
2161 ArcMap& operator=(const ArcMap& cmap) {
2162 return operator=<ArcMap>(cmap);
2165 template <typename CMap>
2166 ArcMap& operator=(const CMap& cmap) {
2167 Parent::operator=(cmap);
2172 template <typename _Value>
2173 class EdgeMap : public Digraph::template ArcMap<_Value> {
2176 typedef _Value Value;
2177 typedef typename Digraph::template ArcMap<Value> Parent;
2179 explicit EdgeMap(const Adaptor& adaptor)
2180 : Parent(*adaptor._digraph) {}
2182 EdgeMap(const Adaptor& adaptor, const Value& value)
2183 : Parent(*adaptor._digraph, value) {}
2186 EdgeMap& operator=(const EdgeMap& cmap) {
2187 return operator=<EdgeMap>(cmap);
2190 template <typename CMap>
2191 EdgeMap& operator=(const CMap& cmap) {
2192 Parent::operator=(cmap);
2198 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
2199 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2201 typedef typename ItemSetTraits<Digraph, Edge>::ItemNotifier EdgeNotifier;
2202 EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2206 UndirectorBase() : _digraph(0) {}
2210 void setDigraph(Digraph& digraph) {
2211 _digraph = &digraph;
2216 /// \ingroup graph_adaptors
2218 /// \brief Adaptor class for viewing a digraph as an undirected graph.
2220 /// Undirector adaptor can be used for viewing a digraph as an undirected
2221 /// graph. All arcs of the underlying digraph are showed in the
2222 /// adaptor as an edge (and also as a pair of arcs, of course).
2223 /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
2225 /// The adapted digraph can also be modified through this adaptor
2226 /// by adding or removing nodes or edges, unless the \c GR template
2227 /// parameter is set to be \c const.
2229 /// \tparam GR The type of the adapted digraph.
2230 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2231 /// It can also be specified to be \c const.
2233 /// \note The \c Node type of this adaptor and the adapted digraph are
2234 /// convertible to each other, moreover the \c Edge type of the adaptor
2235 /// and the \c Arc type of the adapted digraph are also convertible to
2237 /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type
2238 /// of the adapted digraph.)
2239 template<typename GR>
2244 public GraphAdaptorExtender<UndirectorBase<GR> > {
2247 /// The type of the adapted digraph.
2249 typedef GraphAdaptorExtender<UndirectorBase<GR> > Parent;
2254 /// \brief Constructor
2256 /// Creates an undirected graph from the given digraph.
2257 Undirector(Digraph& digraph) {
2258 setDigraph(digraph);
2261 /// \brief Arc map combined from two original arc maps
2263 /// This map adaptor class adapts two arc maps of the underlying
2264 /// digraph to get an arc map of the undirected graph.
2265 /// Its value type is inherited from the first arc map type
2266 /// (\c %ForwardMap).
2267 template <typename ForwardMap, typename BackwardMap>
2268 class CombinedArcMap {
2271 /// The key type of the map
2272 typedef typename Parent::Arc Key;
2273 /// The value type of the map
2274 typedef typename ForwardMap::Value Value;
2276 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2278 typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
2279 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
2280 typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
2281 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
2284 CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2285 : _forward(&forward), _backward(&backward) {}
2287 /// Sets the value associated with the given key.
2288 void set(const Key& e, const Value& a) {
2289 if (Parent::direction(e)) {
2290 _forward->set(e, a);
2292 _backward->set(e, a);
2296 /// Returns the value associated with the given key.
2297 ConstReturnValue operator[](const Key& e) const {
2298 if (Parent::direction(e)) {
2299 return (*_forward)[e];
2301 return (*_backward)[e];
2305 /// Returns a reference to the value associated with the given key.
2306 ReturnValue operator[](const Key& e) {
2307 if (Parent::direction(e)) {
2308 return (*_forward)[e];
2310 return (*_backward)[e];
2316 ForwardMap* _forward;
2317 BackwardMap* _backward;
2321 /// \brief Returns a combined arc map
2323 /// This function just returns a combined arc map.
2324 template <typename ForwardMap, typename BackwardMap>
2325 static CombinedArcMap<ForwardMap, BackwardMap>
2326 combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2327 return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2330 template <typename ForwardMap, typename BackwardMap>
2331 static CombinedArcMap<const ForwardMap, BackwardMap>
2332 combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2333 return CombinedArcMap<const ForwardMap,
2334 BackwardMap>(forward, backward);
2337 template <typename ForwardMap, typename BackwardMap>
2338 static CombinedArcMap<ForwardMap, const BackwardMap>
2339 combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2340 return CombinedArcMap<ForwardMap,
2341 const BackwardMap>(forward, backward);
2344 template <typename ForwardMap, typename BackwardMap>
2345 static CombinedArcMap<const ForwardMap, const BackwardMap>
2346 combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2347 return CombinedArcMap<const ForwardMap,
2348 const BackwardMap>(forward, backward);
2353 /// \brief Returns a read-only Undirector adaptor
2355 /// This function just returns a read-only \ref Undirector adaptor.
2356 /// \ingroup graph_adaptors
2357 /// \relates Undirector
2358 template<typename GR>
2359 Undirector<const GR> undirector(const GR& digraph) {
2360 return Undirector<const GR>(digraph);
2364 template <typename _Graph, typename _DirectionMap>
2365 class OrienterBase {
2368 typedef _Graph Graph;
2369 typedef _DirectionMap DirectionMap;
2371 typedef typename Graph::Node Node;
2372 typedef typename Graph::Edge Arc;
2374 void reverseArc(const Arc& arc) {
2375 _direction->set(arc, !(*_direction)[arc]);
2378 void first(Node& i) const { _graph->first(i); }
2379 void first(Arc& i) const { _graph->first(i); }
2380 void firstIn(Arc& i, const Node& n) const {
2382 _graph->firstInc(i, d, n);
2383 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2385 void firstOut(Arc& i, const Node& n ) const {
2387 _graph->firstInc(i, d, n);
2388 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2391 void next(Node& i) const { _graph->next(i); }
2392 void next(Arc& i) const { _graph->next(i); }
2393 void nextIn(Arc& i) const {
2394 bool d = !(*_direction)[i];
2395 _graph->nextInc(i, d);
2396 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2398 void nextOut(Arc& i) const {
2399 bool d = (*_direction)[i];
2400 _graph->nextInc(i, d);
2401 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2404 Node source(const Arc& e) const {
2405 return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2407 Node target(const Arc& e) const {
2408 return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2411 typedef NodeNumTagIndicator<Graph> NodeNumTag;
2412 int nodeNum() const { return _graph->nodeNum(); }
2414 typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2415 int arcNum() const { return _graph->edgeNum(); }
2417 typedef FindEdgeTagIndicator<Graph> FindArcTag;
2418 Arc findArc(const Node& u, const Node& v,
2419 const Arc& prev = INVALID) const {
2420 Arc arc = _graph->findEdge(u, v, prev);
2421 while (arc != INVALID && source(arc) != u) {
2422 arc = _graph->findEdge(u, v, arc);
2428 return Node(_graph->addNode());
2431 Arc addArc(const Node& u, const Node& v) {
2432 Arc arc = _graph->addEdge(u, v);
2433 _direction->set(arc, _graph->u(arc) == u);
2437 void erase(const Node& i) { _graph->erase(i); }
2438 void erase(const Arc& i) { _graph->erase(i); }
2440 void clear() { _graph->clear(); }
2442 int id(const Node& v) const { return _graph->id(v); }
2443 int id(const Arc& e) const { return _graph->id(e); }
2445 Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2446 Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2448 int maxNodeId() const { return _graph->maxNodeId(); }
2449 int maxArcId() const { return _graph->maxEdgeId(); }
2451 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
2452 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2454 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
2455 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2457 template <typename _Value>
2458 class NodeMap : public _Graph::template NodeMap<_Value> {
2461 typedef typename _Graph::template NodeMap<_Value> Parent;
2463 explicit NodeMap(const OrienterBase& adapter)
2464 : Parent(*adapter._graph) {}
2466 NodeMap(const OrienterBase& adapter, const _Value& value)
2467 : Parent(*adapter._graph, value) {}
2470 NodeMap& operator=(const NodeMap& cmap) {
2471 return operator=<NodeMap>(cmap);
2474 template <typename CMap>
2475 NodeMap& operator=(const CMap& cmap) {
2476 Parent::operator=(cmap);
2482 template <typename _Value>
2483 class ArcMap : public _Graph::template EdgeMap<_Value> {
2486 typedef typename Graph::template EdgeMap<_Value> Parent;
2488 explicit ArcMap(const OrienterBase& adapter)
2489 : Parent(*adapter._graph) { }
2491 ArcMap(const OrienterBase& adapter, const _Value& value)
2492 : Parent(*adapter._graph, value) { }
2495 ArcMap& operator=(const ArcMap& cmap) {
2496 return operator=<ArcMap>(cmap);
2499 template <typename CMap>
2500 ArcMap& operator=(const CMap& cmap) {
2501 Parent::operator=(cmap);
2510 DirectionMap* _direction;
2512 void setDirectionMap(DirectionMap& direction) {
2513 _direction = &direction;
2516 void setGraph(Graph& graph) {
2522 /// \ingroup graph_adaptors
2524 /// \brief Adaptor class for orienting the edges of a graph to get a digraph
2526 /// Orienter adaptor can be used for orienting the edges of a graph to
2527 /// get a digraph. A \c bool edge map of the underlying graph must be
2528 /// specified, which define the direction of the arcs in the adaptor.
2529 /// The arcs can be easily reversed by the \c reverseArc() member function
2531 /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2533 /// The adapted graph can also be modified through this adaptor
2534 /// by adding or removing nodes or arcs, unless the \c GR template
2535 /// parameter is set to be \c const.
2537 /// \tparam GR The type of the adapted graph.
2538 /// It must conform to the \ref concepts::Graph "Graph" concept.
2539 /// It can also be specified to be \c const.
2540 /// \tparam DM The type of the direction map.
2541 /// It must be a \c bool (or convertible) edge map of the
2542 /// adapted graph. The default type is
2543 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
2545 /// \note The \c Node type of this adaptor and the adapted graph are
2546 /// convertible to each other, moreover the \c Arc type of the adaptor
2547 /// and the \c Edge type of the adapted graph are also convertible to
2550 template<typename GR,
2554 template<typename GR,
2555 typename DM = typename GR::template EdgeMap<bool> >
2557 public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
2561 /// The type of the adapted graph.
2563 /// The type of the direction edge map.
2564 typedef DM DirectionMap;
2566 typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2567 typedef typename Parent::Arc Arc;
2572 /// \brief Constructor
2574 /// Constructor of the adaptor.
2575 Orienter(Graph& graph, DirectionMap& direction) {
2577 setDirectionMap(direction);
2580 /// \brief Reverses the given arc
2582 /// This function reverses the given arc.
2583 /// It is done by simply negate the assigned value of \c a
2584 /// in the direction map.
2585 void reverseArc(const Arc& a) {
2586 Parent::reverseArc(a);
2590 /// \brief Returns a read-only Orienter adaptor
2592 /// This function just returns a read-only \ref Orienter adaptor.
2593 /// \ingroup graph_adaptors
2594 /// \relates Orienter
2595 template<typename GR, typename DM>
2596 Orienter<const GR, DM>
2597 orienter(const GR& graph, DM& direction_map) {
2598 return Orienter<const GR, DM>(graph, direction_map);
2601 template<typename GR, typename DM>
2602 Orienter<const GR, const DM>
2603 orienter(const GR& graph, const DM& direction_map) {
2604 return Orienter<const GR, const DM>(graph, direction_map);
2607 namespace _adaptor_bits {
2609 template<typename Digraph,
2610 typename CapacityMap,
2613 class ResForwardFilter {
2616 typedef typename Digraph::Arc Key;
2621 const CapacityMap* _capacity;
2622 const FlowMap* _flow;
2623 Tolerance _tolerance;
2626 ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2627 const Tolerance& tolerance = Tolerance())
2628 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2630 bool operator[](const typename Digraph::Arc& a) const {
2631 return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2635 template<typename Digraph,
2636 typename CapacityMap,
2639 class ResBackwardFilter {
2642 typedef typename Digraph::Arc Key;
2647 const CapacityMap* _capacity;
2648 const FlowMap* _flow;
2649 Tolerance _tolerance;
2653 ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2654 const Tolerance& tolerance = Tolerance())
2655 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2657 bool operator[](const typename Digraph::Arc& a) const {
2658 return _tolerance.positive((*_flow)[a]);
2664 /// \ingroup graph_adaptors
2666 /// \brief Adaptor class for composing the residual digraph for directed
2667 /// flow and circulation problems.
2669 /// ResidualDigraph can be used for composing the \e residual digraph
2670 /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$
2671 /// be a directed graph and let \f$ F \f$ be a number type.
2672 /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs.
2673 /// This adaptor implements a digraph structure with node set \f$ V \f$
2674 /// and arc set \f$ A_{forward}\cup A_{backward} \f$,
2675 /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and
2676 /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so
2677 /// called residual digraph.
2678 /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken,
2679 /// multiplicities are counted, i.e. the adaptor has exactly
2680 /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel
2682 /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2684 /// \tparam GR The type of the adapted digraph.
2685 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2686 /// It is implicitly \c const.
2687 /// \tparam CM The type of the capacity map.
2688 /// It must be an arc map of some numerical type, which defines
2689 /// the capacities in the flow problem. It is implicitly \c const.
2690 /// The default type is
2691 /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
2692 /// \tparam FM The type of the flow map.
2693 /// It must be an arc map of some numerical type, which defines
2694 /// the flow values in the flow problem. The default type is \c CM.
2695 /// \tparam TL The tolerance type for handling inexact computation.
2696 /// The default tolerance type depends on the value type of the
2699 /// \note This adaptor is implemented using Undirector and FilterArcs
2702 /// \note The \c Node type of this adaptor and the adapted digraph are
2703 /// convertible to each other, moreover the \c Arc type of the adaptor
2704 /// is convertible to the \c Arc type of the adapted digraph.
2706 template<typename GR, typename CM, typename FM, typename TL>
2707 class ResidualDigraph
2709 template<typename GR,
2710 typename CM = typename GR::template ArcMap<int>,
2712 typename TL = Tolerance<typename CM::Value> >
2713 class ResidualDigraph :
2715 Undirector<const GR>,
2716 typename Undirector<const GR>::template CombinedArcMap<
2717 _adaptor_bits::ResForwardFilter<const GR, CM, FM, TL>,
2718 _adaptor_bits::ResBackwardFilter<const GR, CM, FM, TL> > >
2723 /// The type of the underlying digraph.
2725 /// The type of the capacity map.
2726 typedef CM CapacityMap;
2727 /// The type of the flow map.
2729 /// The tolerance type.
2730 typedef TL Tolerance;
2732 typedef typename CapacityMap::Value Value;
2733 typedef ResidualDigraph Adaptor;
2737 typedef Undirector<const Digraph> Undirected;
2739 typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap,
2740 FlowMap, Tolerance> ForwardFilter;
2742 typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap,
2743 FlowMap, Tolerance> BackwardFilter;
2745 typedef typename Undirected::
2746 template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2748 typedef FilterArcs<Undirected, ArcFilter> Parent;
2750 const CapacityMap* _capacity;
2754 ForwardFilter _forward_filter;
2755 BackwardFilter _backward_filter;
2756 ArcFilter _arc_filter;
2760 /// \brief Constructor
2762 /// Constructor of the residual digraph adaptor. The parameters are the
2763 /// digraph, the capacity map, the flow map, and a tolerance object.
2764 ResidualDigraph(const Digraph& digraph, const CapacityMap& capacity,
2765 FlowMap& flow, const Tolerance& tolerance = Tolerance())
2766 : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph),
2767 _forward_filter(capacity, flow, tolerance),
2768 _backward_filter(capacity, flow, tolerance),
2769 _arc_filter(_forward_filter, _backward_filter)
2771 Parent::setDigraph(_graph);
2772 Parent::setArcFilterMap(_arc_filter);
2775 typedef typename Parent::Arc Arc;
2777 /// \brief Returns the residual capacity of the given arc.
2779 /// Returns the residual capacity of the given arc.
2780 Value residualCapacity(const Arc& a) const {
2781 if (Undirected::direction(a)) {
2782 return (*_capacity)[a] - (*_flow)[a];
2788 /// \brief Augments on the given arc in the residual digraph.
2790 /// Augments on the given arc in the residual digraph. It increases
2791 /// or decreases the flow value on the original arc according to the
2792 /// direction of the residual arc.
2793 void augment(const Arc& a, const Value& v) const {
2794 if (Undirected::direction(a)) {
2795 _flow->set(a, (*_flow)[a] + v);
2797 _flow->set(a, (*_flow)[a] - v);
2801 /// \brief Returns \c true if the given residual arc is a forward arc.
2803 /// Returns \c true if the given residual arc has the same orientation
2804 /// as the original arc, i.e. it is a so called forward arc.
2805 static bool forward(const Arc& a) {
2806 return Undirected::direction(a);
2809 /// \brief Returns \c true if the given residual arc is a backward arc.
2811 /// Returns \c true if the given residual arc has the opposite orientation
2812 /// than the original arc, i.e. it is a so called backward arc.
2813 static bool backward(const Arc& a) {
2814 return !Undirected::direction(a);
2817 /// \brief Returns the forward oriented residual arc.
2819 /// Returns the forward oriented residual arc related to the given
2820 /// arc of the underlying digraph.
2821 static Arc forward(const typename Digraph::Arc& a) {
2822 return Undirected::direct(a, true);
2825 /// \brief Returns the backward oriented residual arc.
2827 /// Returns the backward oriented residual arc related to the given
2828 /// arc of the underlying digraph.
2829 static Arc backward(const typename Digraph::Arc& a) {
2830 return Undirected::direct(a, false);
2833 /// \brief Residual capacity map.
2835 /// This map adaptor class can be used for obtaining the residual
2836 /// capacities as an arc map of the residual digraph.
2837 /// Its value type is inherited from the capacity map.
2838 class ResidualCapacity {
2840 const Adaptor* _adaptor;
2842 /// The key type of the map
2844 /// The value type of the map
2845 typedef typename CapacityMap::Value Value;
2848 ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {}
2850 /// Returns the value associated with the given residual arc
2851 Value operator[](const Arc& a) const {
2852 return _adaptor->residualCapacity(a);
2857 /// \brief Returns a residual capacity map
2859 /// This function just returns a residual capacity map.
2860 ResidualCapacity residualCapacity() const {
2861 return ResidualCapacity(*this);
2866 /// \brief Returns a (read-only) Residual adaptor
2868 /// This function just returns a (read-only) \ref Residual adaptor.
2869 /// \ingroup graph_adaptors
2870 /// \relates Residual
2871 template<typename GR, typename CM, typename FM>
2872 ResidualDigraph<GR, CM, FM>
2873 residualDigraph(const GR& digraph, const CM& capacity_map, FM& flow_map) {
2874 return ResidualDigraph<GR, CM, FM> (digraph, capacity_map, flow_map);
2878 template <typename _Digraph>
2879 class SplitNodesBase {
2882 typedef _Digraph Digraph;
2883 typedef DigraphAdaptorBase<const _Digraph> Parent;
2884 typedef SplitNodesBase Adaptor;
2886 typedef typename Digraph::Node DigraphNode;
2887 typedef typename Digraph::Arc DigraphArc;
2894 template <typename T> class NodeMapBase;
2895 template <typename T> class ArcMapBase;
2899 class Node : public DigraphNode {
2900 friend class SplitNodesBase;
2901 template <typename T> friend class NodeMapBase;
2905 Node(DigraphNode node, bool in)
2906 : DigraphNode(node), _in(in) {}
2911 Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2913 bool operator==(const Node& node) const {
2914 return DigraphNode::operator==(node) && _in == node._in;
2917 bool operator!=(const Node& node) const {
2918 return !(*this == node);
2921 bool operator<(const Node& node) const {
2922 return DigraphNode::operator<(node) ||
2923 (DigraphNode::operator==(node) && _in < node._in);
2928 friend class SplitNodesBase;
2929 template <typename T> friend class ArcMapBase;
2931 typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2933 explicit Arc(const DigraphArc& arc) : _item(arc) {}
2934 explicit Arc(const DigraphNode& node) : _item(node) {}
2940 Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2942 bool operator==(const Arc& arc) const {
2943 if (_item.firstState()) {
2944 if (arc._item.firstState()) {
2945 return _item.first() == arc._item.first();
2948 if (arc._item.secondState()) {
2949 return _item.second() == arc._item.second();
2955 bool operator!=(const Arc& arc) const {
2956 return !(*this == arc);
2959 bool operator<(const Arc& arc) const {
2960 if (_item.firstState()) {
2961 if (arc._item.firstState()) {
2962 return _item.first() < arc._item.first();
2966 if (arc._item.secondState()) {
2967 return _item.second() < arc._item.second();
2973 operator DigraphArc() const { return _item.first(); }
2974 operator DigraphNode() const { return _item.second(); }
2978 void first(Node& n) const {
2983 void next(Node& n) const {
2992 void first(Arc& e) const {
2993 e._item.setSecond();
2994 _digraph->first(e._item.second());
2995 if (e._item.second() == INVALID) {
2997 _digraph->first(e._item.first());
3001 void next(Arc& e) const {
3002 if (e._item.secondState()) {
3003 _digraph->next(e._item.second());
3004 if (e._item.second() == INVALID) {
3006 _digraph->first(e._item.first());
3009 _digraph->next(e._item.first());
3013 void firstOut(Arc& e, const Node& n) const {
3015 e._item.setSecond(n);
3018 _digraph->firstOut(e._item.first(), n);
3022 void nextOut(Arc& e) const {
3023 if (!e._item.firstState()) {
3024 e._item.setFirst(INVALID);
3026 _digraph->nextOut(e._item.first());
3030 void firstIn(Arc& e, const Node& n) const {
3032 e._item.setSecond(n);
3035 _digraph->firstIn(e._item.first(), n);
3039 void nextIn(Arc& e) const {
3040 if (!e._item.firstState()) {
3041 e._item.setFirst(INVALID);
3043 _digraph->nextIn(e._item.first());
3047 Node source(const Arc& e) const {
3048 if (e._item.firstState()) {
3049 return Node(_digraph->source(e._item.first()), false);
3051 return Node(e._item.second(), true);
3055 Node target(const Arc& e) const {
3056 if (e._item.firstState()) {
3057 return Node(_digraph->target(e._item.first()), true);
3059 return Node(e._item.second(), false);
3063 int id(const Node& n) const {
3064 return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
3066 Node nodeFromId(int ix) const {
3067 return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
3069 int maxNodeId() const {
3070 return 2 * _digraph->maxNodeId() + 1;
3073 int id(const Arc& e) const {
3074 if (e._item.firstState()) {
3075 return _digraph->id(e._item.first()) << 1;
3077 return (_digraph->id(e._item.second()) << 1) | 1;
3080 Arc arcFromId(int ix) const {
3081 if ((ix & 1) == 0) {
3082 return Arc(_digraph->arcFromId(ix >> 1));
3084 return Arc(_digraph->nodeFromId(ix >> 1));
3087 int maxArcId() const {
3088 return std::max(_digraph->maxNodeId() << 1,
3089 (_digraph->maxArcId() << 1) | 1);
3092 static bool inNode(const Node& n) {
3096 static bool outNode(const Node& n) {
3100 static bool origArc(const Arc& e) {
3101 return e._item.firstState();
3104 static bool bindArc(const Arc& e) {
3105 return e._item.secondState();
3108 static Node inNode(const DigraphNode& n) {
3109 return Node(n, true);
3112 static Node outNode(const DigraphNode& n) {
3113 return Node(n, false);
3116 static Arc arc(const DigraphNode& n) {
3120 static Arc arc(const DigraphArc& e) {
3124 typedef True NodeNumTag;
3125 int nodeNum() const {
3126 return 2 * countNodes(*_digraph);
3129 typedef True ArcNumTag;
3130 int arcNum() const {
3131 return countArcs(*_digraph) + countNodes(*_digraph);
3134 typedef True FindArcTag;
3135 Arc findArc(const Node& u, const Node& v,
3136 const Arc& prev = INVALID) const {
3137 if (inNode(u) && outNode(v)) {
3138 if (static_cast<const DigraphNode&>(u) ==
3139 static_cast<const DigraphNode&>(v) && prev == INVALID) {
3143 else if (outNode(u) && inNode(v)) {
3144 return Arc(::lemon::findArc(*_digraph, u, v, prev));
3151 template <typename _Value>
3153 : public MapTraits<typename Parent::template NodeMap<_Value> > {
3154 typedef typename Parent::template NodeMap<_Value> NodeImpl;
3157 typedef _Value Value;
3158 typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
3159 typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
3160 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
3161 typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
3162 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
3164 NodeMapBase(const Adaptor& adaptor)
3165 : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
3166 NodeMapBase(const Adaptor& adaptor, const Value& value)
3167 : _in_map(*adaptor._digraph, value),
3168 _out_map(*adaptor._digraph, value) {}
3170 void set(const Node& key, const Value& val) {
3171 if (Adaptor::inNode(key)) { _in_map.set(key, val); }
3172 else {_out_map.set(key, val); }
3175 ReturnValue operator[](const Node& key) {
3176 if (Adaptor::inNode(key)) { return _in_map[key]; }
3177 else { return _out_map[key]; }
3180 ConstReturnValue operator[](const Node& key) const {
3181 if (Adaptor::inNode(key)) { return _in_map[key]; }
3182 else { return _out_map[key]; }
3186 NodeImpl _in_map, _out_map;
3189 template <typename _Value>
3191 : public MapTraits<typename Parent::template ArcMap<_Value> > {
3192 typedef typename Parent::template ArcMap<_Value> ArcImpl;
3193 typedef typename Parent::template NodeMap<_Value> NodeImpl;
3196 typedef _Value Value;
3197 typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
3198 typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
3199 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
3200 typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
3201 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
3203 ArcMapBase(const Adaptor& adaptor)
3204 : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
3205 ArcMapBase(const Adaptor& adaptor, const Value& value)
3206 : _arc_map(*adaptor._digraph, value),
3207 _node_map(*adaptor._digraph, value) {}
3209 void set(const Arc& key, const Value& val) {
3210 if (Adaptor::origArc(key)) {
3211 _arc_map.set(key._item.first(), val);
3213 _node_map.set(key._item.second(), val);
3217 ReturnValue operator[](const Arc& key) {
3218 if (Adaptor::origArc(key)) {
3219 return _arc_map[key._item.first()];
3221 return _node_map[key._item.second()];
3225 ConstReturnValue operator[](const Arc& key) const {
3226 if (Adaptor::origArc(key)) {
3227 return _arc_map[key._item.first()];
3229 return _node_map[key._item.second()];
3240 template <typename _Value>
3242 : public SubMapExtender<Adaptor, NodeMapBase<_Value> >
3245 typedef _Value Value;
3246 typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent;
3248 NodeMap(const Adaptor& adaptor)
3249 : Parent(adaptor) {}
3251 NodeMap(const Adaptor& adaptor, const Value& value)
3252 : Parent(adaptor, value) {}
3255 NodeMap& operator=(const NodeMap& cmap) {
3256 return operator=<NodeMap>(cmap);
3259 template <typename CMap>
3260 NodeMap& operator=(const CMap& cmap) {
3261 Parent::operator=(cmap);
3266 template <typename _Value>
3268 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
3271 typedef _Value Value;
3272 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
3274 ArcMap(const Adaptor& adaptor)
3275 : Parent(adaptor) {}
3277 ArcMap(const Adaptor& adaptor, const Value& value)
3278 : Parent(adaptor, value) {}
3281 ArcMap& operator=(const ArcMap& cmap) {
3282 return operator=<ArcMap>(cmap);
3285 template <typename CMap>
3286 ArcMap& operator=(const CMap& cmap) {
3287 Parent::operator=(cmap);
3294 SplitNodesBase() : _digraph(0) {}
3298 void setDigraph(Digraph& digraph) {
3299 _digraph = &digraph;
3304 /// \ingroup graph_adaptors
3306 /// \brief Adaptor class for splitting the nodes of a digraph.
3308 /// SplitNodes adaptor can be used for splitting each node into an
3309 /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor
3310 /// replaces each node \f$ u \f$ in the digraph with two nodes,
3311 /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$.
3312 /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the
3313 /// new target of the arc will be \f$ u_{in} \f$ and similarly the
3314 /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$.
3315 /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$
3316 /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph.
3318 /// The aim of this class is running an algorithm with respect to node
3319 /// costs or capacities if the algorithm considers only arc costs or
3320 /// capacities directly.
3321 /// In this case you can use \c SplitNodes adaptor, and set the node
3322 /// costs/capacities of the original digraph to the \e bind \e arcs
3325 /// \tparam GR The type of the adapted digraph.
3326 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
3327 /// It is implicitly \c const.
3329 /// \note The \c Node type of this adaptor is converible to the \c Node
3330 /// type of the adapted digraph.
3331 template <typename GR>
3336 : public DigraphAdaptorExtender<SplitNodesBase<const GR> > {
3340 typedef DigraphAdaptorExtender<SplitNodesBase<const GR> > Parent;
3342 typedef typename Digraph::Node DigraphNode;
3343 typedef typename Digraph::Arc DigraphArc;
3345 typedef typename Parent::Node Node;
3346 typedef typename Parent::Arc Arc;
3348 /// \brief Constructor
3350 /// Constructor of the adaptor.
3351 SplitNodes(const Digraph& g) {
3352 Parent::setDigraph(g);
3355 /// \brief Returns \c true if the given node is an in-node.
3357 /// Returns \c true if the given node is an in-node.
3358 static bool inNode(const Node& n) {
3359 return Parent::inNode(n);
3362 /// \brief Returns \c true if the given node is an out-node.
3364 /// Returns \c true if the given node is an out-node.
3365 static bool outNode(const Node& n) {
3366 return Parent::outNode(n);
3369 /// \brief Returns \c true if the given arc is an original arc.
3371 /// Returns \c true if the given arc is one of the arcs in the
3372 /// original digraph.
3373 static bool origArc(const Arc& a) {
3374 return Parent::origArc(a);
3377 /// \brief Returns \c true if the given arc is a bind arc.
3379 /// Returns \c true if the given arc is a bind arc, i.e. it connects
3380 /// an in-node and an out-node.
3381 static bool bindArc(const Arc& a) {
3382 return Parent::bindArc(a);
3385 /// \brief Returns the in-node created from the given original node.
3387 /// Returns the in-node created from the given original node.
3388 static Node inNode(const DigraphNode& n) {
3389 return Parent::inNode(n);
3392 /// \brief Returns the out-node created from the given original node.
3394 /// Returns the out-node created from the given original node.
3395 static Node outNode(const DigraphNode& n) {
3396 return Parent::outNode(n);
3399 /// \brief Returns the bind arc that corresponds to the given
3402 /// Returns the bind arc in the adaptor that corresponds to the given
3403 /// original node, i.e. the arc connecting the in-node and out-node
3405 static Arc arc(const DigraphNode& n) {
3406 return Parent::arc(n);
3409 /// \brief Returns the arc that corresponds to the given original arc.
3411 /// Returns the arc in the adaptor that corresponds to the given
3413 static Arc arc(const DigraphArc& a) {
3414 return Parent::arc(a);
3417 /// \brief Node map combined from two original node maps
3419 /// This map adaptor class adapts two node maps of the original digraph
3420 /// to get a node map of the split digraph.
3421 /// Its value type is inherited from the first node map type
3423 template <typename InNodeMap, typename OutNodeMap>
3424 class CombinedNodeMap {
3427 /// The key type of the map
3429 /// The value type of the map
3430 typedef typename InNodeMap::Value Value;
3432 typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
3433 typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
3434 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
3435 typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
3436 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
3439 CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3440 : _in_map(in_map), _out_map(out_map) {}
3442 /// Returns the value associated with the given key.
3443 Value operator[](const Key& key) const {
3444 if (Parent::inNode(key)) {
3445 return _in_map[key];
3447 return _out_map[key];
3451 /// Returns a reference to the value associated with the given key.
3452 Value& operator[](const Key& key) {
3453 if (Parent::inNode(key)) {
3454 return _in_map[key];
3456 return _out_map[key];
3460 /// Sets the value associated with the given key.
3461 void set(const Key& key, const Value& value) {
3462 if (Parent::inNode(key)) {
3463 _in_map.set(key, value);
3465 _out_map.set(key, value);
3472 OutNodeMap& _out_map;
3477 /// \brief Returns a combined node map
3479 /// This function just returns a combined node map.
3480 template <typename InNodeMap, typename OutNodeMap>
3481 static CombinedNodeMap<InNodeMap, OutNodeMap>
3482 combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3483 return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3486 template <typename InNodeMap, typename OutNodeMap>
3487 static CombinedNodeMap<const InNodeMap, OutNodeMap>
3488 combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3489 return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3492 template <typename InNodeMap, typename OutNodeMap>
3493 static CombinedNodeMap<InNodeMap, const OutNodeMap>
3494 combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3495 return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3498 template <typename InNodeMap, typename OutNodeMap>
3499 static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3500 combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3501 return CombinedNodeMap<const InNodeMap,
3502 const OutNodeMap>(in_map, out_map);
3505 /// \brief Arc map combined from an arc map and a node map of the
3506 /// original digraph.
3508 /// This map adaptor class adapts an arc map and a node map of the
3509 /// original digraph to get an arc map of the split digraph.
3510 /// Its value type is inherited from the original arc map type
3512 template <typename ArcMap, typename NodeMap>
3513 class CombinedArcMap {
3516 /// The key type of the map
3518 /// The value type of the map
3519 typedef typename ArcMap::Value Value;
3521 typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag;
3522 typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue;
3523 typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue;
3524 typedef typename MapTraits<ArcMap>::ReturnValue Reference;
3525 typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference;
3528 CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
3529 : _arc_map(arc_map), _node_map(node_map) {}
3531 /// Returns the value associated with the given key.
3532 Value operator[](const Key& arc) const {
3533 if (Parent::origArc(arc)) {
3534 return _arc_map[arc];
3536 return _node_map[arc];
3540 /// Returns a reference to the value associated with the given key.
3541 Value& operator[](const Key& arc) {
3542 if (Parent::origArc(arc)) {
3543 return _arc_map[arc];
3545 return _node_map[arc];
3549 /// Sets the value associated with the given key.
3550 void set(const Arc& arc, const Value& val) {
3551 if (Parent::origArc(arc)) {
3552 _arc_map.set(arc, val);
3554 _node_map.set(arc, val);
3563 /// \brief Returns a combined arc map
3565 /// This function just returns a combined arc map.
3566 template <typename ArcMap, typename NodeMap>
3567 static CombinedArcMap<ArcMap, NodeMap>
3568 combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
3569 return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
3572 template <typename ArcMap, typename NodeMap>
3573 static CombinedArcMap<const ArcMap, NodeMap>
3574 combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
3575 return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
3578 template <typename ArcMap, typename NodeMap>
3579 static CombinedArcMap<ArcMap, const NodeMap>
3580 combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
3581 return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
3584 template <typename ArcMap, typename NodeMap>
3585 static CombinedArcMap<const ArcMap, const NodeMap>
3586 combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
3587 return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
3592 /// \brief Returns a (read-only) SplitNodes adaptor
3594 /// This function just returns a (read-only) \ref SplitNodes adaptor.
3595 /// \ingroup graph_adaptors
3596 /// \relates SplitNodes
3597 template<typename GR>
3599 splitNodes(const GR& digraph) {
3600 return SplitNodes<GR>(digraph);
3605 #endif //LEMON_ADAPTORS_H