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>
40 #define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED
42 #define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED
45 template<typename DGR>
46 class DigraphAdaptorBase {
49 typedef DigraphAdaptorBase Adaptor;
53 DigraphAdaptorBase() : _digraph(0) { }
54 void initialize(DGR& digraph) { _digraph = &digraph; }
57 DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { }
59 typedef typename DGR::Node Node;
60 typedef typename DGR::Arc Arc;
62 void first(Node& i) const { _digraph->first(i); }
63 void first(Arc& i) const { _digraph->first(i); }
64 void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
65 void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
67 void next(Node& i) const { _digraph->next(i); }
68 void next(Arc& i) const { _digraph->next(i); }
69 void nextIn(Arc& i) const { _digraph->nextIn(i); }
70 void nextOut(Arc& i) const { _digraph->nextOut(i); }
72 Node source(const Arc& a) const { return _digraph->source(a); }
73 Node target(const Arc& a) const { return _digraph->target(a); }
75 typedef NodeNumTagIndicator<DGR> NodeNumTag;
76 int nodeNum() const { return _digraph->nodeNum(); }
78 typedef ArcNumTagIndicator<DGR> ArcNumTag;
79 int arcNum() const { return _digraph->arcNum(); }
81 typedef FindArcTagIndicator<DGR> FindArcTag;
82 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
83 return _digraph->findArc(u, v, prev);
86 Node addNode() { return _digraph->addNode(); }
87 Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
89 void erase(const Node& n) { _digraph->erase(n); }
90 void erase(const Arc& a) { _digraph->erase(a); }
92 void clear() { _digraph->clear(); }
94 int id(const Node& n) const { return _digraph->id(n); }
95 int id(const Arc& a) const { return _digraph->id(a); }
97 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
98 Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
100 int maxNodeId() const { return _digraph->maxNodeId(); }
101 int maxArcId() const { return _digraph->maxArcId(); }
103 typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
104 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
106 typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier;
107 ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
109 template <typename V>
110 class NodeMap : public DGR::template NodeMap<V> {
113 typedef typename DGR::template NodeMap<V> Parent;
115 explicit NodeMap(const Adaptor& adaptor)
116 : Parent(*adaptor._digraph) {}
118 NodeMap(const Adaptor& adaptor, const V& value)
119 : Parent(*adaptor._digraph, value) { }
122 NodeMap& operator=(const NodeMap& cmap) {
123 return operator=<NodeMap>(cmap);
126 template <typename CMap>
127 NodeMap& operator=(const CMap& cmap) {
128 Parent::operator=(cmap);
134 template <typename V>
135 class ArcMap : public DGR::template ArcMap<V> {
138 typedef typename DGR::template ArcMap<V> Parent;
140 explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor)
141 : Parent(*adaptor._digraph) {}
143 ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value)
144 : Parent(*adaptor._digraph, value) {}
147 ArcMap& operator=(const ArcMap& cmap) {
148 return operator=<ArcMap>(cmap);
151 template <typename CMap>
152 ArcMap& operator=(const CMap& cmap) {
153 Parent::operator=(cmap);
161 template<typename GR>
162 class GraphAdaptorBase {
169 GraphAdaptorBase() : _graph(0) {}
171 void initialize(GR& graph) { _graph = &graph; }
174 GraphAdaptorBase(GR& graph) : _graph(&graph) {}
176 typedef typename GR::Node Node;
177 typedef typename GR::Arc Arc;
178 typedef typename GR::Edge Edge;
180 void first(Node& i) const { _graph->first(i); }
181 void first(Arc& i) const { _graph->first(i); }
182 void first(Edge& i) const { _graph->first(i); }
183 void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
184 void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
185 void firstInc(Edge &i, bool &d, const Node &n) const {
186 _graph->firstInc(i, d, n);
189 void next(Node& i) const { _graph->next(i); }
190 void next(Arc& i) const { _graph->next(i); }
191 void next(Edge& i) const { _graph->next(i); }
192 void nextIn(Arc& i) const { _graph->nextIn(i); }
193 void nextOut(Arc& i) const { _graph->nextOut(i); }
194 void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
196 Node u(const Edge& e) const { return _graph->u(e); }
197 Node v(const Edge& e) const { return _graph->v(e); }
199 Node source(const Arc& a) const { return _graph->source(a); }
200 Node target(const Arc& a) const { return _graph->target(a); }
202 typedef NodeNumTagIndicator<Graph> NodeNumTag;
203 int nodeNum() const { return _graph->nodeNum(); }
205 typedef ArcNumTagIndicator<Graph> ArcNumTag;
206 int arcNum() const { return _graph->arcNum(); }
208 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
209 int edgeNum() const { return _graph->edgeNum(); }
211 typedef FindArcTagIndicator<Graph> FindArcTag;
212 Arc findArc(const Node& u, const Node& v,
213 const Arc& prev = INVALID) const {
214 return _graph->findArc(u, v, prev);
217 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
218 Edge findEdge(const Node& u, const Node& v,
219 const Edge& prev = INVALID) const {
220 return _graph->findEdge(u, v, prev);
223 Node addNode() { return _graph->addNode(); }
224 Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
226 void erase(const Node& i) { _graph->erase(i); }
227 void erase(const Edge& i) { _graph->erase(i); }
229 void clear() { _graph->clear(); }
231 bool direction(const Arc& a) const { return _graph->direction(a); }
232 Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
234 int id(const Node& v) const { return _graph->id(v); }
235 int id(const Arc& a) const { return _graph->id(a); }
236 int id(const Edge& e) const { return _graph->id(e); }
238 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
239 Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
240 Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
242 int maxNodeId() const { return _graph->maxNodeId(); }
243 int maxArcId() const { return _graph->maxArcId(); }
244 int maxEdgeId() const { return _graph->maxEdgeId(); }
246 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
247 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
249 typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
250 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
252 typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier;
253 EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
255 template <typename V>
256 class NodeMap : public GR::template NodeMap<V> {
258 typedef typename GR::template NodeMap<V> Parent;
259 explicit NodeMap(const GraphAdaptorBase<GR>& adapter)
260 : Parent(*adapter._graph) {}
261 NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
262 : Parent(*adapter._graph, value) {}
265 NodeMap& operator=(const NodeMap& cmap) {
266 return operator=<NodeMap>(cmap);
269 template <typename CMap>
270 NodeMap& operator=(const CMap& cmap) {
271 Parent::operator=(cmap);
277 template <typename V>
278 class ArcMap : public GR::template ArcMap<V> {
280 typedef typename GR::template ArcMap<V> Parent;
281 explicit ArcMap(const GraphAdaptorBase<GR>& adapter)
282 : Parent(*adapter._graph) {}
283 ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value)
284 : Parent(*adapter._graph, value) {}
287 ArcMap& operator=(const ArcMap& cmap) {
288 return operator=<ArcMap>(cmap);
291 template <typename CMap>
292 ArcMap& operator=(const CMap& cmap) {
293 Parent::operator=(cmap);
298 template <typename V>
299 class EdgeMap : public GR::template EdgeMap<V> {
301 typedef typename GR::template EdgeMap<V> Parent;
302 explicit EdgeMap(const GraphAdaptorBase<GR>& adapter)
303 : Parent(*adapter._graph) {}
304 EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
305 : Parent(*adapter._graph, value) {}
308 EdgeMap& operator=(const EdgeMap& cmap) {
309 return operator=<EdgeMap>(cmap);
312 template <typename CMap>
313 EdgeMap& operator=(const CMap& cmap) {
314 Parent::operator=(cmap);
321 template <typename DGR>
322 class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
325 typedef DigraphAdaptorBase<DGR> Parent;
327 ReverseDigraphBase() : Parent() { }
329 typedef typename Parent::Node Node;
330 typedef typename Parent::Arc Arc;
332 void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
333 void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
335 void nextIn(Arc& a) const { Parent::nextOut(a); }
336 void nextOut(Arc& a) const { Parent::nextIn(a); }
338 Node source(const Arc& a) const { return Parent::target(a); }
339 Node target(const Arc& a) const { return Parent::source(a); }
341 Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
343 typedef FindArcTagIndicator<DGR> FindArcTag;
344 Arc findArc(const Node& u, const Node& v,
345 const Arc& prev = INVALID) const {
346 return Parent::findArc(v, u, prev);
351 /// \ingroup graph_adaptors
353 /// \brief Adaptor class for reversing the orientation of the arcs in
356 /// ReverseDigraph can be used for reversing the arcs in a digraph.
357 /// It conforms to the \ref concepts::Digraph "Digraph" concept.
359 /// The adapted digraph can also be modified through this adaptor
360 /// by adding or removing nodes or arcs, unless the \c GR template
361 /// parameter is set to be \c const.
363 /// \tparam DGR The type of the adapted digraph.
364 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
365 /// It can also be specified to be \c const.
367 /// \note The \c Node and \c Arc types of this adaptor and the adapted
368 /// digraph are convertible to each other.
369 template<typename DGR>
371 class ReverseDigraph {
373 class ReverseDigraph :
374 public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
377 /// The type of the adapted digraph.
379 typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent;
384 /// \brief Constructor
386 /// Creates a reverse digraph adaptor for the given digraph.
387 explicit ReverseDigraph(DGR& digraph) {
388 Parent::initialize(digraph);
392 /// \brief Returns a read-only ReverseDigraph adaptor
394 /// This function just returns a read-only \ref ReverseDigraph adaptor.
395 /// \ingroup graph_adaptors
396 /// \relates ReverseDigraph
397 template<typename DGR>
398 ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
399 return ReverseDigraph<const DGR>(digraph);
403 template <typename DGR, typename NF, typename AF, bool ch = true>
404 class SubDigraphBase : public DigraphAdaptorBase<DGR> {
407 typedef NF NodeFilterMap;
408 typedef AF ArcFilterMap;
410 typedef SubDigraphBase Adaptor;
411 typedef DigraphAdaptorBase<DGR> Parent;
416 : Parent(), _node_filter(0), _arc_filter(0) { }
418 void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
419 Parent::initialize(digraph);
420 _node_filter = &node_filter;
421 _arc_filter = &arc_filter;
426 typedef typename Parent::Node Node;
427 typedef typename Parent::Arc Arc;
429 void first(Node& i) const {
431 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
434 void first(Arc& i) const {
436 while (i != INVALID && (!(*_arc_filter)[i]
437 || !(*_node_filter)[Parent::source(i)]
438 || !(*_node_filter)[Parent::target(i)]))
442 void firstIn(Arc& i, const Node& n) const {
443 Parent::firstIn(i, n);
444 while (i != INVALID && (!(*_arc_filter)[i]
445 || !(*_node_filter)[Parent::source(i)]))
449 void firstOut(Arc& i, const Node& n) const {
450 Parent::firstOut(i, n);
451 while (i != INVALID && (!(*_arc_filter)[i]
452 || !(*_node_filter)[Parent::target(i)]))
456 void next(Node& i) const {
458 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
461 void next(Arc& i) const {
463 while (i != INVALID && (!(*_arc_filter)[i]
464 || !(*_node_filter)[Parent::source(i)]
465 || !(*_node_filter)[Parent::target(i)]))
469 void nextIn(Arc& i) const {
471 while (i != INVALID && (!(*_arc_filter)[i]
472 || !(*_node_filter)[Parent::source(i)]))
476 void nextOut(Arc& i) const {
478 while (i != INVALID && (!(*_arc_filter)[i]
479 || !(*_node_filter)[Parent::target(i)]))
483 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
484 void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
486 bool status(const Node& n) const { return (*_node_filter)[n]; }
487 bool status(const Arc& a) const { return (*_arc_filter)[a]; }
489 typedef False NodeNumTag;
490 typedef False ArcNumTag;
492 typedef FindArcTagIndicator<DGR> FindArcTag;
493 Arc findArc(const Node& source, const Node& target,
494 const Arc& prev = INVALID) const {
495 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
498 Arc arc = Parent::findArc(source, target, prev);
499 while (arc != INVALID && !(*_arc_filter)[arc]) {
500 arc = Parent::findArc(source, target, arc);
507 template <typename V>
509 : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
510 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
513 typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
514 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
516 NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
518 NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
519 : Parent(adaptor, value) {}
522 NodeMap& operator=(const NodeMap& cmap) {
523 return operator=<NodeMap>(cmap);
526 template <typename CMap>
527 NodeMap& operator=(const CMap& cmap) {
528 Parent::operator=(cmap);
533 template <typename V>
535 : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
536 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
539 typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
540 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
542 ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
544 ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
545 : Parent(adaptor, value) {}
548 ArcMap& operator=(const ArcMap& cmap) {
549 return operator=<ArcMap>(cmap);
552 template <typename CMap>
553 ArcMap& operator=(const CMap& cmap) {
554 Parent::operator=(cmap);
561 template <typename DGR, typename NF, typename AF>
562 class SubDigraphBase<DGR, NF, AF, false>
563 : public DigraphAdaptorBase<DGR> {
566 typedef NF NodeFilterMap;
567 typedef AF ArcFilterMap;
569 typedef SubDigraphBase Adaptor;
570 typedef DigraphAdaptorBase<Digraph> Parent;
575 : Parent(), _node_filter(0), _arc_filter(0) { }
577 void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
578 Parent::initialize(digraph);
579 _node_filter = &node_filter;
580 _arc_filter = &arc_filter;
585 typedef typename Parent::Node Node;
586 typedef typename Parent::Arc Arc;
588 void first(Node& i) const {
590 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
593 void first(Arc& i) const {
595 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
598 void firstIn(Arc& i, const Node& n) const {
599 Parent::firstIn(i, n);
600 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
603 void firstOut(Arc& i, const Node& n) const {
604 Parent::firstOut(i, n);
605 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
608 void next(Node& i) const {
610 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
612 void next(Arc& i) const {
614 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
616 void nextIn(Arc& i) const {
618 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
621 void nextOut(Arc& i) const {
623 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
626 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
627 void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
629 bool status(const Node& n) const { return (*_node_filter)[n]; }
630 bool status(const Arc& a) const { return (*_arc_filter)[a]; }
632 typedef False NodeNumTag;
633 typedef False ArcNumTag;
635 typedef FindArcTagIndicator<DGR> FindArcTag;
636 Arc findArc(const Node& source, const Node& target,
637 const Arc& prev = INVALID) const {
638 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
641 Arc arc = Parent::findArc(source, target, prev);
642 while (arc != INVALID && !(*_arc_filter)[arc]) {
643 arc = Parent::findArc(source, target, arc);
648 template <typename V>
650 : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
651 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
654 typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
655 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
657 NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
659 NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
660 : Parent(adaptor, value) {}
663 NodeMap& operator=(const NodeMap& cmap) {
664 return operator=<NodeMap>(cmap);
667 template <typename CMap>
668 NodeMap& operator=(const CMap& cmap) {
669 Parent::operator=(cmap);
674 template <typename V>
676 : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
677 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
680 typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
681 LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
683 ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
685 ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
686 : Parent(adaptor, value) {}
689 ArcMap& operator=(const ArcMap& cmap) {
690 return operator=<ArcMap>(cmap);
693 template <typename CMap>
694 ArcMap& operator=(const CMap& cmap) {
695 Parent::operator=(cmap);
702 /// \ingroup graph_adaptors
704 /// \brief Adaptor class for hiding nodes and arcs in a digraph
706 /// SubDigraph can be used for hiding nodes and arcs in a digraph.
707 /// A \c bool node map and a \c bool arc map must be specified, which
708 /// define the filters for nodes and arcs.
709 /// Only the nodes and arcs with \c true filter value are
710 /// shown in the subdigraph. The arcs that are incident to hidden
711 /// nodes are also filtered out.
712 /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept.
714 /// The adapted digraph can also be modified through this adaptor
715 /// by adding or removing nodes or arcs, unless the \c GR template
716 /// parameter is set to be \c const.
718 /// \tparam DGR The type of the adapted digraph.
719 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
720 /// It can also be specified to be \c const.
721 /// \tparam NF The type of the node filter map.
722 /// It must be a \c bool (or convertible) node map of the
723 /// adapted digraph. The default type is
724 /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>".
725 /// \tparam AF The type of the arc filter map.
726 /// It must be \c bool (or convertible) arc map of the
727 /// adapted digraph. The default type is
728 /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
730 /// \note The \c Node and \c Arc types of this adaptor and the adapted
731 /// digraph are convertible to each other.
736 template<typename DGR, typename NF, typename AF>
739 template<typename DGR,
740 typename NF = typename DGR::template NodeMap<bool>,
741 typename AF = typename DGR::template ArcMap<bool> >
743 public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
746 /// The type of the adapted digraph.
748 /// The type of the node filter map.
749 typedef NF NodeFilterMap;
750 /// The type of the arc filter map.
751 typedef AF ArcFilterMap;
753 typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> >
756 typedef typename Parent::Node Node;
757 typedef typename Parent::Arc Arc;
763 /// \brief Constructor
765 /// Creates a subdigraph for the given digraph with the
766 /// given node and arc filter maps.
767 SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) {
768 Parent::initialize(digraph, node_filter, arc_filter);
771 /// \brief Sets the status of the given node
773 /// This function sets the status of the given node.
774 /// It is done by simply setting the assigned value of \c n
775 /// to \c v in the node filter map.
776 void status(const Node& n, bool v) const { Parent::status(n, v); }
778 /// \brief Sets the status of the given arc
780 /// This function sets the status of the given arc.
781 /// It is done by simply setting the assigned value of \c a
782 /// to \c v in the arc filter map.
783 void status(const Arc& a, bool v) const { Parent::status(a, v); }
785 /// \brief Returns the status of the given node
787 /// This function returns the status of the given node.
788 /// It is \c true if the given node is enabled (i.e. not hidden).
789 bool status(const Node& n) const { return Parent::status(n); }
791 /// \brief Returns the status of the given arc
793 /// This function returns the status of the given arc.
794 /// It is \c true if the given arc is enabled (i.e. not hidden).
795 bool status(const Arc& a) const { return Parent::status(a); }
797 /// \brief Disables the given node
799 /// This function disables the given node in the subdigraph,
800 /// so the iteration jumps over it.
801 /// It is the same as \ref status() "status(n, false)".
802 void disable(const Node& n) const { Parent::status(n, false); }
804 /// \brief Disables the given arc
806 /// This function disables the given arc in the subdigraph,
807 /// so the iteration jumps over it.
808 /// It is the same as \ref status() "status(a, false)".
809 void disable(const Arc& a) const { Parent::status(a, false); }
811 /// \brief Enables the given node
813 /// This function enables the given node in the subdigraph.
814 /// It is the same as \ref status() "status(n, true)".
815 void enable(const Node& n) const { Parent::status(n, true); }
817 /// \brief Enables the given arc
819 /// This function enables the given arc in the subdigraph.
820 /// It is the same as \ref status() "status(a, true)".
821 void enable(const Arc& a) const { Parent::status(a, true); }
825 /// \brief Returns a read-only SubDigraph adaptor
827 /// This function just returns a read-only \ref SubDigraph adaptor.
828 /// \ingroup graph_adaptors
829 /// \relates SubDigraph
830 template<typename DGR, typename NF, typename AF>
831 SubDigraph<const DGR, NF, AF>
832 subDigraph(const DGR& digraph,
833 NF& node_filter, AF& arc_filter) {
834 return SubDigraph<const DGR, NF, AF>
835 (digraph, node_filter, arc_filter);
838 template<typename DGR, typename NF, typename AF>
839 SubDigraph<const DGR, const NF, AF>
840 subDigraph(const DGR& digraph,
841 const NF& node_filter, AF& arc_filter) {
842 return SubDigraph<const DGR, const NF, AF>
843 (digraph, node_filter, arc_filter);
846 template<typename DGR, typename NF, typename AF>
847 SubDigraph<const DGR, NF, const AF>
848 subDigraph(const DGR& digraph,
849 NF& node_filter, const AF& arc_filter) {
850 return SubDigraph<const DGR, NF, const AF>
851 (digraph, node_filter, arc_filter);
854 template<typename DGR, typename NF, typename AF>
855 SubDigraph<const DGR, const NF, const AF>
856 subDigraph(const DGR& digraph,
857 const NF& node_filter, const AF& arc_filter) {
858 return SubDigraph<const DGR, const NF, const AF>
859 (digraph, node_filter, arc_filter);
863 template <typename GR, typename NF, typename EF, bool ch = true>
864 class SubGraphBase : public GraphAdaptorBase<GR> {
867 typedef NF NodeFilterMap;
868 typedef EF EdgeFilterMap;
870 typedef SubGraphBase Adaptor;
871 typedef GraphAdaptorBase<GR> Parent;
878 : Parent(), _node_filter(0), _edge_filter(0) { }
880 void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
881 Parent::initialize(graph);
882 _node_filter = &node_filter;
883 _edge_filter = &edge_filter;
888 typedef typename Parent::Node Node;
889 typedef typename Parent::Arc Arc;
890 typedef typename Parent::Edge Edge;
892 void first(Node& i) const {
894 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
897 void first(Arc& i) const {
899 while (i!=INVALID && (!(*_edge_filter)[i]
900 || !(*_node_filter)[Parent::source(i)]
901 || !(*_node_filter)[Parent::target(i)]))
905 void first(Edge& i) const {
907 while (i!=INVALID && (!(*_edge_filter)[i]
908 || !(*_node_filter)[Parent::u(i)]
909 || !(*_node_filter)[Parent::v(i)]))
913 void firstIn(Arc& i, const Node& n) const {
914 Parent::firstIn(i, n);
915 while (i!=INVALID && (!(*_edge_filter)[i]
916 || !(*_node_filter)[Parent::source(i)]))
920 void firstOut(Arc& i, const Node& n) const {
921 Parent::firstOut(i, n);
922 while (i!=INVALID && (!(*_edge_filter)[i]
923 || !(*_node_filter)[Parent::target(i)]))
927 void firstInc(Edge& i, bool& d, const Node& n) const {
928 Parent::firstInc(i, d, n);
929 while (i!=INVALID && (!(*_edge_filter)[i]
930 || !(*_node_filter)[Parent::u(i)]
931 || !(*_node_filter)[Parent::v(i)]))
932 Parent::nextInc(i, d);
935 void next(Node& i) const {
937 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
940 void next(Arc& i) const {
942 while (i!=INVALID && (!(*_edge_filter)[i]
943 || !(*_node_filter)[Parent::source(i)]
944 || !(*_node_filter)[Parent::target(i)]))
948 void next(Edge& i) const {
950 while (i!=INVALID && (!(*_edge_filter)[i]
951 || !(*_node_filter)[Parent::u(i)]
952 || !(*_node_filter)[Parent::v(i)]))
956 void nextIn(Arc& i) const {
958 while (i!=INVALID && (!(*_edge_filter)[i]
959 || !(*_node_filter)[Parent::source(i)]))
963 void nextOut(Arc& i) const {
965 while (i!=INVALID && (!(*_edge_filter)[i]
966 || !(*_node_filter)[Parent::target(i)]))
970 void nextInc(Edge& i, bool& d) const {
971 Parent::nextInc(i, d);
972 while (i!=INVALID && (!(*_edge_filter)[i]
973 || !(*_node_filter)[Parent::u(i)]
974 || !(*_node_filter)[Parent::v(i)]))
975 Parent::nextInc(i, d);
978 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
979 void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
981 bool status(const Node& n) const { return (*_node_filter)[n]; }
982 bool status(const Edge& e) const { return (*_edge_filter)[e]; }
984 typedef False NodeNumTag;
985 typedef False ArcNumTag;
986 typedef False EdgeNumTag;
988 typedef FindArcTagIndicator<Graph> FindArcTag;
989 Arc findArc(const Node& u, const Node& v,
990 const Arc& prev = INVALID) const {
991 if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
994 Arc arc = Parent::findArc(u, v, prev);
995 while (arc != INVALID && !(*_edge_filter)[arc]) {
996 arc = Parent::findArc(u, v, arc);
1001 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1002 Edge findEdge(const Node& u, const Node& v,
1003 const Edge& prev = INVALID) const {
1004 if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
1007 Edge edge = Parent::findEdge(u, v, prev);
1008 while (edge != INVALID && !(*_edge_filter)[edge]) {
1009 edge = Parent::findEdge(u, v, edge);
1014 template <typename V>
1016 : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1017 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1020 typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1021 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1023 NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1024 : Parent(adaptor) {}
1025 NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1026 : Parent(adaptor, value) {}
1029 NodeMap& operator=(const NodeMap& cmap) {
1030 return operator=<NodeMap>(cmap);
1033 template <typename CMap>
1034 NodeMap& operator=(const CMap& cmap) {
1035 Parent::operator=(cmap);
1040 template <typename V>
1042 : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1043 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1046 typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1047 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1049 ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1050 : Parent(adaptor) {}
1051 ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1052 : Parent(adaptor, value) {}
1055 ArcMap& operator=(const ArcMap& cmap) {
1056 return operator=<ArcMap>(cmap);
1059 template <typename CMap>
1060 ArcMap& operator=(const CMap& cmap) {
1061 Parent::operator=(cmap);
1066 template <typename V>
1068 : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1069 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1072 typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1073 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1075 EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1076 : Parent(adaptor) {}
1078 EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1079 : Parent(adaptor, value) {}
1082 EdgeMap& operator=(const EdgeMap& cmap) {
1083 return operator=<EdgeMap>(cmap);
1086 template <typename CMap>
1087 EdgeMap& operator=(const CMap& cmap) {
1088 Parent::operator=(cmap);
1095 template <typename GR, typename NF, typename EF>
1096 class SubGraphBase<GR, NF, EF, false>
1097 : public GraphAdaptorBase<GR> {
1100 typedef NF NodeFilterMap;
1101 typedef EF EdgeFilterMap;
1103 typedef SubGraphBase Adaptor;
1104 typedef GraphAdaptorBase<GR> Parent;
1109 : Parent(), _node_filter(0), _edge_filter(0) { }
1111 void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
1112 Parent::initialize(graph);
1113 _node_filter = &node_filter;
1114 _edge_filter = &edge_filter;
1119 typedef typename Parent::Node Node;
1120 typedef typename Parent::Arc Arc;
1121 typedef typename Parent::Edge Edge;
1123 void first(Node& i) const {
1125 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1128 void first(Arc& i) const {
1130 while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1133 void first(Edge& i) const {
1135 while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1138 void firstIn(Arc& i, const Node& n) const {
1139 Parent::firstIn(i, n);
1140 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1143 void firstOut(Arc& i, const Node& n) const {
1144 Parent::firstOut(i, n);
1145 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1148 void firstInc(Edge& i, bool& d, const Node& n) const {
1149 Parent::firstInc(i, d, n);
1150 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1153 void next(Node& i) const {
1155 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1157 void next(Arc& i) const {
1159 while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1161 void next(Edge& i) const {
1163 while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1165 void nextIn(Arc& i) const {
1167 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1170 void nextOut(Arc& i) const {
1172 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1174 void nextInc(Edge& i, bool& d) const {
1175 Parent::nextInc(i, d);
1176 while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1179 void status(const Node& n, bool v) const { _node_filter->set(n, v); }
1180 void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
1182 bool status(const Node& n) const { return (*_node_filter)[n]; }
1183 bool status(const Edge& e) const { return (*_edge_filter)[e]; }
1185 typedef False NodeNumTag;
1186 typedef False ArcNumTag;
1187 typedef False EdgeNumTag;
1189 typedef FindArcTagIndicator<Graph> FindArcTag;
1190 Arc findArc(const Node& u, const Node& v,
1191 const Arc& prev = INVALID) const {
1192 Arc arc = Parent::findArc(u, v, prev);
1193 while (arc != INVALID && !(*_edge_filter)[arc]) {
1194 arc = Parent::findArc(u, v, arc);
1199 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1200 Edge findEdge(const Node& u, const Node& v,
1201 const Edge& prev = INVALID) const {
1202 Edge edge = Parent::findEdge(u, v, prev);
1203 while (edge != INVALID && !(*_edge_filter)[edge]) {
1204 edge = Parent::findEdge(u, v, edge);
1209 template <typename V>
1211 : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1212 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1215 typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1216 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1218 NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1219 : Parent(adaptor) {}
1220 NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1221 : Parent(adaptor, value) {}
1224 NodeMap& operator=(const NodeMap& cmap) {
1225 return operator=<NodeMap>(cmap);
1228 template <typename CMap>
1229 NodeMap& operator=(const CMap& cmap) {
1230 Parent::operator=(cmap);
1235 template <typename V>
1237 : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1238 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1241 typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1242 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1244 ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1245 : Parent(adaptor) {}
1246 ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1247 : Parent(adaptor, value) {}
1250 ArcMap& operator=(const ArcMap& cmap) {
1251 return operator=<ArcMap>(cmap);
1254 template <typename CMap>
1255 ArcMap& operator=(const CMap& cmap) {
1256 Parent::operator=(cmap);
1261 template <typename V>
1263 : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1264 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1267 typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1268 LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1270 EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1271 : Parent(adaptor) {}
1273 EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1274 : Parent(adaptor, value) {}
1277 EdgeMap& operator=(const EdgeMap& cmap) {
1278 return operator=<EdgeMap>(cmap);
1281 template <typename CMap>
1282 EdgeMap& operator=(const CMap& cmap) {
1283 Parent::operator=(cmap);
1290 /// \ingroup graph_adaptors
1292 /// \brief Adaptor class for hiding nodes and edges in an undirected
1295 /// SubGraph can be used for hiding nodes and edges in a graph.
1296 /// A \c bool node map and a \c bool edge map must be specified, which
1297 /// define the filters for nodes and edges.
1298 /// Only the nodes and edges with \c true filter value are
1299 /// shown in the subgraph. The edges that are incident to hidden
1300 /// nodes are also filtered out.
1301 /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
1303 /// The adapted graph can also be modified through this adaptor
1304 /// by adding or removing nodes or edges, unless the \c GR template
1305 /// parameter is set to be \c const.
1307 /// \tparam GR The type of the adapted graph.
1308 /// It must conform to the \ref concepts::Graph "Graph" concept.
1309 /// It can also be specified to be \c const.
1310 /// \tparam NF The type of the node filter map.
1311 /// It must be a \c bool (or convertible) node map of the
1312 /// adapted graph. The default type is
1313 /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1314 /// \tparam EF The type of the edge filter map.
1315 /// It must be a \c bool (or convertible) edge map of the
1316 /// adapted graph. The default type is
1317 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1319 /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1320 /// adapted graph are convertible to each other.
1322 /// \see FilterNodes
1323 /// \see FilterEdges
1325 template<typename GR, typename NF, typename EF>
1328 template<typename GR,
1329 typename NF = typename GR::template NodeMap<bool>,
1330 typename EF = typename GR::template EdgeMap<bool> >
1332 public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
1335 /// The type of the adapted graph.
1337 /// The type of the node filter map.
1338 typedef NF NodeFilterMap;
1339 /// The type of the edge filter map.
1340 typedef EF EdgeFilterMap;
1342 typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> >
1345 typedef typename Parent::Node Node;
1346 typedef typename Parent::Edge Edge;
1352 /// \brief Constructor
1354 /// Creates a subgraph for the given graph with the given node
1355 /// and edge filter maps.
1356 SubGraph(GR& graph, NF& node_filter, EF& edge_filter) {
1357 initialize(graph, node_filter, edge_filter);
1360 /// \brief Sets the status of the given node
1362 /// This function sets the status of the given node.
1363 /// It is done by simply setting the assigned value of \c n
1364 /// to \c v in the node filter map.
1365 void status(const Node& n, bool v) const { Parent::status(n, v); }
1367 /// \brief Sets the status of the given edge
1369 /// This function sets the status of the given edge.
1370 /// It is done by simply setting the assigned value of \c e
1371 /// to \c v in the edge filter map.
1372 void status(const Edge& e, bool v) const { Parent::status(e, v); }
1374 /// \brief Returns the status of the given node
1376 /// This function returns the status of the given node.
1377 /// It is \c true if the given node is enabled (i.e. not hidden).
1378 bool status(const Node& n) const { return Parent::status(n); }
1380 /// \brief Returns the status of the given edge
1382 /// This function returns the status of the given edge.
1383 /// It is \c true if the given edge is enabled (i.e. not hidden).
1384 bool status(const Edge& e) const { return Parent::status(e); }
1386 /// \brief Disables the given node
1388 /// This function disables the given node in the subdigraph,
1389 /// so the iteration jumps over it.
1390 /// It is the same as \ref status() "status(n, false)".
1391 void disable(const Node& n) const { Parent::status(n, false); }
1393 /// \brief Disables the given edge
1395 /// This function disables the given edge in the subgraph,
1396 /// so the iteration jumps over it.
1397 /// It is the same as \ref status() "status(e, false)".
1398 void disable(const Edge& e) const { Parent::status(e, false); }
1400 /// \brief Enables the given node
1402 /// This function enables the given node in the subdigraph.
1403 /// It is the same as \ref status() "status(n, true)".
1404 void enable(const Node& n) const { Parent::status(n, true); }
1406 /// \brief Enables the given edge
1408 /// This function enables the given edge in the subgraph.
1409 /// It is the same as \ref status() "status(e, true)".
1410 void enable(const Edge& e) const { Parent::status(e, true); }
1414 /// \brief Returns a read-only SubGraph adaptor
1416 /// This function just returns a read-only \ref SubGraph adaptor.
1417 /// \ingroup graph_adaptors
1418 /// \relates SubGraph
1419 template<typename GR, typename NF, typename EF>
1420 SubGraph<const GR, NF, EF>
1421 subGraph(const GR& graph, NF& node_filter, EF& edge_filter) {
1422 return SubGraph<const GR, NF, EF>
1423 (graph, node_filter, edge_filter);
1426 template<typename GR, typename NF, typename EF>
1427 SubGraph<const GR, const NF, EF>
1428 subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
1429 return SubGraph<const GR, const NF, EF>
1430 (graph, node_filter, edge_filter);
1433 template<typename GR, typename NF, typename EF>
1434 SubGraph<const GR, NF, const EF>
1435 subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
1436 return SubGraph<const GR, NF, const EF>
1437 (graph, node_filter, edge_filter);
1440 template<typename GR, typename NF, typename EF>
1441 SubGraph<const GR, const NF, const EF>
1442 subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
1443 return SubGraph<const GR, const NF, const EF>
1444 (graph, node_filter, edge_filter);
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, Const<bool, true> >,
1490 typedef NF NodeFilterMap;
1492 typedef DigraphAdaptorExtender<
1493 SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >,
1496 typedef typename Parent::Node Node;
1499 ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map;
1501 FilterNodes() : const_true_map() {}
1505 /// \brief Constructor
1507 /// Creates a subgraph for the given digraph or graph with the
1508 /// given node filter map.
1509 FilterNodes(GR& graph, NF& node_filter)
1510 : Parent(), const_true_map()
1512 Parent::initialize(graph, node_filter, const_true_map);
1515 /// \brief Sets the status of the given node
1517 /// This function sets the status of the given node.
1518 /// It is done by simply setting the assigned value of \c n
1519 /// to \c v in the node filter map.
1520 void status(const Node& n, bool v) const { Parent::status(n, v); }
1522 /// \brief Returns the status of the given node
1524 /// This function returns the status of the given node.
1525 /// It is \c true if the given node is enabled (i.e. not hidden).
1526 bool status(const Node& n) const { return Parent::status(n); }
1528 /// \brief Disables the given node
1530 /// This function disables the given node, so the iteration
1532 /// It is the same as \ref status() "status(n, false)".
1533 void disable(const Node& n) const { Parent::status(n, false); }
1535 /// \brief Enables the given node
1537 /// This function enables the given node.
1538 /// It is the same as \ref status() "status(n, true)".
1539 void enable(const Node& n) const { Parent::status(n, true); }
1543 template<typename GR, typename NF>
1544 class FilterNodes<GR, NF,
1545 typename enable_if<UndirectedTagIndicator<GR> >::type> :
1546 public GraphAdaptorExtender<
1547 SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >,
1552 typedef NF NodeFilterMap;
1553 typedef GraphAdaptorExtender<
1554 SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >,
1557 typedef typename Parent::Node Node;
1559 ConstMap<typename GR::Edge, Const<bool, true> > const_true_map;
1561 FilterNodes() : const_true_map() {}
1565 FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1566 Parent(), const_true_map() {
1567 Parent::initialize(graph, node_filter, const_true_map);
1570 void status(const Node& n, bool v) const { Parent::status(n, v); }
1571 bool status(const Node& n) const { return Parent::status(n); }
1572 void disable(const Node& n) const { Parent::status(n, false); }
1573 void enable(const Node& n) const { Parent::status(n, true); }
1578 /// \brief Returns a read-only FilterNodes adaptor
1580 /// This function just returns a read-only \ref FilterNodes adaptor.
1581 /// \ingroup graph_adaptors
1582 /// \relates FilterNodes
1583 template<typename GR, typename NF>
1584 FilterNodes<const GR, NF>
1585 filterNodes(const GR& graph, NF& node_filter) {
1586 return FilterNodes<const GR, NF>(graph, node_filter);
1589 template<typename GR, typename NF>
1590 FilterNodes<const GR, const NF>
1591 filterNodes(const GR& graph, const NF& node_filter) {
1592 return FilterNodes<const GR, const NF>(graph, node_filter);
1595 /// \ingroup graph_adaptors
1597 /// \brief Adaptor class for hiding arcs in a digraph.
1599 /// FilterArcs adaptor can be used for hiding arcs in a digraph.
1600 /// A \c bool arc map must be specified, which defines the filter for
1601 /// the arcs. Only the arcs with \c true filter value are shown in the
1602 /// subdigraph. This adaptor conforms to the \ref concepts::Digraph
1603 /// "Digraph" concept.
1605 /// The adapted digraph can also be modified through this adaptor
1606 /// by adding or removing nodes or arcs, unless the \c GR template
1607 /// parameter is set to be \c const.
1609 /// \tparam DGR The type of the adapted digraph.
1610 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
1611 /// It can also be specified to be \c const.
1612 /// \tparam AF The type of the arc filter map.
1613 /// It must be a \c bool (or convertible) arc map of the
1614 /// adapted digraph. The default type is
1615 /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
1617 /// \note The \c Node and \c Arc types of this adaptor and the adapted
1618 /// digraph are convertible to each other.
1620 template<typename DGR,
1624 template<typename DGR,
1625 typename AF = typename DGR::template ArcMap<bool> >
1627 public DigraphAdaptorExtender<
1628 SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1632 /// The type of the adapted digraph.
1633 typedef DGR Digraph;
1634 /// The type of the arc filter map.
1635 typedef AF ArcFilterMap;
1637 typedef DigraphAdaptorExtender<
1638 SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1639 AF, false> > Parent;
1641 typedef typename Parent::Arc Arc;
1644 ConstMap<typename DGR::Node, Const<bool, true> > const_true_map;
1646 FilterArcs() : const_true_map() {}
1650 /// \brief Constructor
1652 /// Creates a subdigraph for the given digraph with the given arc
1654 FilterArcs(DGR& digraph, ArcFilterMap& arc_filter)
1655 : Parent(), const_true_map() {
1656 Parent::initialize(digraph, const_true_map, arc_filter);
1659 /// \brief Sets the status of the given arc
1661 /// This function sets the status of the given arc.
1662 /// It is done by simply setting the assigned value of \c a
1663 /// to \c v in the arc filter map.
1664 void status(const Arc& a, bool v) const { Parent::status(a, v); }
1666 /// \brief Returns the status of the given arc
1668 /// This function returns the status of the given arc.
1669 /// It is \c true if the given arc is enabled (i.e. not hidden).
1670 bool status(const Arc& a) const { return Parent::status(a); }
1672 /// \brief Disables the given arc
1674 /// This function disables the given arc in the subdigraph,
1675 /// so the iteration jumps over it.
1676 /// It is the same as \ref status() "status(a, false)".
1677 void disable(const Arc& a) const { Parent::status(a, false); }
1679 /// \brief Enables the given arc
1681 /// This function enables the given arc in the subdigraph.
1682 /// It is the same as \ref status() "status(a, true)".
1683 void enable(const Arc& a) const { Parent::status(a, true); }
1687 /// \brief Returns a read-only FilterArcs adaptor
1689 /// This function just returns a read-only \ref FilterArcs adaptor.
1690 /// \ingroup graph_adaptors
1691 /// \relates FilterArcs
1692 template<typename DGR, typename AF>
1693 FilterArcs<const DGR, AF>
1694 filterArcs(const DGR& digraph, AF& arc_filter) {
1695 return FilterArcs<const DGR, AF>(digraph, arc_filter);
1698 template<typename DGR, typename AF>
1699 FilterArcs<const DGR, const AF>
1700 filterArcs(const DGR& digraph, const AF& arc_filter) {
1701 return FilterArcs<const DGR, const AF>(digraph, arc_filter);
1704 /// \ingroup graph_adaptors
1706 /// \brief Adaptor class for hiding edges in a graph.
1708 /// FilterEdges adaptor can be used for hiding edges in a graph.
1709 /// A \c bool edge map must be specified, which defines the filter for
1710 /// the edges. Only the edges with \c true filter value are shown in the
1711 /// subgraph. This adaptor conforms to the \ref concepts::Graph
1712 /// "Graph" concept.
1714 /// The adapted graph can also be modified through this adaptor
1715 /// by adding or removing nodes or edges, unless the \c GR template
1716 /// parameter is set to be \c const.
1718 /// \tparam GR The type of the adapted graph.
1719 /// It must conform to the \ref concepts::Graph "Graph" concept.
1720 /// It can also be specified to be \c const.
1721 /// \tparam EF The type of the edge filter map.
1722 /// It must be a \c bool (or convertible) edge map of the
1723 /// adapted graph. The default type is
1724 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1726 /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1727 /// adapted graph are convertible to each other.
1729 template<typename GR,
1733 template<typename GR,
1734 typename EF = typename GR::template EdgeMap<bool> >
1736 public GraphAdaptorExtender<
1737 SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >,
1741 /// The type of the adapted graph.
1743 /// The type of the edge filter map.
1744 typedef EF EdgeFilterMap;
1746 typedef GraphAdaptorExtender<
1747 SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >,
1748 EF, false> > Parent;
1750 typedef typename Parent::Edge Edge;
1753 ConstMap<typename GR::Node, Const<bool, true> > const_true_map;
1755 FilterEdges() : const_true_map(true) {
1756 Parent::setNodeFilterMap(const_true_map);
1761 /// \brief Constructor
1763 /// Creates a subgraph for the given graph with the given edge
1765 FilterEdges(GR& graph, EF& edge_filter)
1766 : Parent(), const_true_map() {
1767 Parent::initialize(graph, const_true_map, edge_filter);
1770 /// \brief Sets the status of the given edge
1772 /// This function sets the status of the given edge.
1773 /// It is done by simply setting the assigned value of \c e
1774 /// to \c v in the edge filter map.
1775 void status(const Edge& e, bool v) const { Parent::status(e, v); }
1777 /// \brief Returns the status of the given edge
1779 /// This function returns the status of the given edge.
1780 /// It is \c true if the given edge is enabled (i.e. not hidden).
1781 bool status(const Edge& e) const { return Parent::status(e); }
1783 /// \brief Disables the given edge
1785 /// This function disables the given edge in the subgraph,
1786 /// so the iteration jumps over it.
1787 /// It is the same as \ref status() "status(e, false)".
1788 void disable(const Edge& e) const { Parent::status(e, false); }
1790 /// \brief Enables the given edge
1792 /// This function enables the given edge in the subgraph.
1793 /// It is the same as \ref status() "status(e, true)".
1794 void enable(const Edge& e) const { Parent::status(e, true); }
1798 /// \brief Returns a read-only FilterEdges adaptor
1800 /// This function just returns a read-only \ref FilterEdges adaptor.
1801 /// \ingroup graph_adaptors
1802 /// \relates FilterEdges
1803 template<typename GR, typename EF>
1804 FilterEdges<const GR, EF>
1805 filterEdges(const GR& graph, EF& edge_filter) {
1806 return FilterEdges<const GR, EF>(graph, edge_filter);
1809 template<typename GR, typename EF>
1810 FilterEdges<const GR, const EF>
1811 filterEdges(const GR& graph, const EF& edge_filter) {
1812 return FilterEdges<const GR, const EF>(graph, edge_filter);
1816 template <typename DGR>
1817 class UndirectorBase {
1819 typedef DGR Digraph;
1820 typedef UndirectorBase Adaptor;
1822 typedef True UndirectedTag;
1824 typedef typename Digraph::Arc Edge;
1825 typedef typename Digraph::Node Node;
1827 class Arc : public Edge {
1828 friend class UndirectorBase;
1832 Arc(const Edge& edge, bool forward) :
1833 Edge(edge), _forward(forward) {}
1838 Arc(Invalid) : Edge(INVALID), _forward(true) {}
1840 bool operator==(const Arc &other) const {
1841 return _forward == other._forward &&
1842 static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1844 bool operator!=(const Arc &other) const {
1845 return _forward != other._forward ||
1846 static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1848 bool operator<(const Arc &other) const {
1849 return _forward < other._forward ||
1850 (_forward == other._forward &&
1851 static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1855 void first(Node& n) const {
1859 void next(Node& n) const {
1863 void first(Arc& a) const {
1868 void next(Arc& a) const {
1877 void first(Edge& e) const {
1881 void next(Edge& e) const {
1885 void firstOut(Arc& a, const Node& n) const {
1886 _digraph->firstIn(a, n);
1887 if( static_cast<const Edge&>(a) != INVALID ) {
1890 _digraph->firstOut(a, n);
1894 void nextOut(Arc &a) const {
1896 Node n = _digraph->target(a);
1897 _digraph->nextIn(a);
1898 if (static_cast<const Edge&>(a) == INVALID ) {
1899 _digraph->firstOut(a, n);
1904 _digraph->nextOut(a);
1908 void firstIn(Arc &a, const Node &n) const {
1909 _digraph->firstOut(a, n);
1910 if (static_cast<const Edge&>(a) != INVALID ) {
1913 _digraph->firstIn(a, n);
1917 void nextIn(Arc &a) const {
1919 Node n = _digraph->source(a);
1920 _digraph->nextOut(a);
1921 if( static_cast<const Edge&>(a) == INVALID ) {
1922 _digraph->firstIn(a, n);
1927 _digraph->nextIn(a);
1931 void firstInc(Edge &e, bool &d, const Node &n) const {
1933 _digraph->firstOut(e, n);
1934 if (e != INVALID) return;
1936 _digraph->firstIn(e, n);
1939 void nextInc(Edge &e, bool &d) const {
1941 Node s = _digraph->source(e);
1942 _digraph->nextOut(e);
1943 if (e != INVALID) return;
1945 _digraph->firstIn(e, s);
1947 _digraph->nextIn(e);
1951 Node u(const Edge& e) const {
1952 return _digraph->source(e);
1955 Node v(const Edge& e) const {
1956 return _digraph->target(e);
1959 Node source(const Arc &a) const {
1960 return a._forward ? _digraph->source(a) : _digraph->target(a);
1963 Node target(const Arc &a) const {
1964 return a._forward ? _digraph->target(a) : _digraph->source(a);
1967 static Arc direct(const Edge &e, bool d) {
1970 Arc direct(const Edge &e, const Node& n) const {
1971 return Arc(e, _digraph->source(e) == n);
1974 static bool direction(const Arc &a) { return a._forward; }
1976 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1977 Arc arcFromId(int ix) const {
1978 return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1980 Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1982 int id(const Node &n) const { return _digraph->id(n); }
1983 int id(const Arc &a) const {
1984 return (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1986 int id(const Edge &e) const { return _digraph->id(e); }
1988 int maxNodeId() const { return _digraph->maxNodeId(); }
1989 int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
1990 int maxEdgeId() const { return _digraph->maxArcId(); }
1992 Node addNode() { return _digraph->addNode(); }
1993 Edge addEdge(const Node& u, const Node& v) {
1994 return _digraph->addArc(u, v);
1997 void erase(const Node& i) { _digraph->erase(i); }
1998 void erase(const Edge& i) { _digraph->erase(i); }
2000 void clear() { _digraph->clear(); }
2002 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
2003 int nodeNum() const { return _digraph->nodeNum(); }
2005 typedef ArcNumTagIndicator<Digraph> ArcNumTag;
2006 int arcNum() const { return 2 * _digraph->arcNum(); }
2008 typedef ArcNumTag EdgeNumTag;
2009 int edgeNum() const { return _digraph->arcNum(); }
2011 typedef FindArcTagIndicator<Digraph> FindArcTag;
2012 Arc findArc(Node s, Node t, Arc p = INVALID) const {
2014 Edge arc = _digraph->findArc(s, t);
2015 if (arc != INVALID) return direct(arc, true);
2016 arc = _digraph->findArc(t, s);
2017 if (arc != INVALID) return direct(arc, false);
2018 } else if (direction(p)) {
2019 Edge arc = _digraph->findArc(s, t, p);
2020 if (arc != INVALID) return direct(arc, true);
2021 arc = _digraph->findArc(t, s);
2022 if (arc != INVALID) return direct(arc, false);
2024 Edge arc = _digraph->findArc(t, s, p);
2025 if (arc != INVALID) return direct(arc, false);
2030 typedef FindArcTag FindEdgeTag;
2031 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
2034 Edge arc = _digraph->findArc(s, t);
2035 if (arc != INVALID) return arc;
2036 arc = _digraph->findArc(t, s);
2037 if (arc != INVALID) return arc;
2038 } else if (_digraph->source(p) == s) {
2039 Edge arc = _digraph->findArc(s, t, p);
2040 if (arc != INVALID) return arc;
2041 arc = _digraph->findArc(t, s);
2042 if (arc != INVALID) return arc;
2044 Edge arc = _digraph->findArc(t, s, p);
2045 if (arc != INVALID) return arc;
2048 return _digraph->findArc(s, t, p);
2055 template <typename V>
2059 typedef typename DGR::template ArcMap<V> MapImpl;
2063 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2067 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
2068 typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
2069 typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
2070 typedef typename MapTraits<MapImpl>::ReturnValue Reference;
2072 ArcMapBase(const UndirectorBase<DGR>& adaptor) :
2073 _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
2075 ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value)
2076 : _forward(*adaptor._digraph, value),
2077 _backward(*adaptor._digraph, value) {}
2079 void set(const Arc& a, const V& value) {
2081 _forward.set(a, value);
2083 _backward.set(a, value);
2087 ConstReturnValue operator[](const Arc& a) const {
2091 return _backward[a];
2095 ReturnValue operator[](const Arc& a) {
2099 return _backward[a];
2105 MapImpl _forward, _backward;
2111 template <typename V>
2112 class NodeMap : public DGR::template NodeMap<V> {
2116 typedef typename DGR::template NodeMap<Value> Parent;
2118 explicit NodeMap(const UndirectorBase<DGR>& adaptor)
2119 : Parent(*adaptor._digraph) {}
2121 NodeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2122 : Parent(*adaptor._digraph, value) { }
2125 NodeMap& operator=(const NodeMap& cmap) {
2126 return operator=<NodeMap>(cmap);
2129 template <typename CMap>
2130 NodeMap& operator=(const CMap& cmap) {
2131 Parent::operator=(cmap);
2137 template <typename V>
2139 : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> >
2143 typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent;
2145 explicit ArcMap(const UndirectorBase<DGR>& adaptor)
2146 : Parent(adaptor) {}
2148 ArcMap(const UndirectorBase<DGR>& adaptor, const V& value)
2149 : Parent(adaptor, value) {}
2152 ArcMap& operator=(const ArcMap& cmap) {
2153 return operator=<ArcMap>(cmap);
2156 template <typename CMap>
2157 ArcMap& operator=(const CMap& cmap) {
2158 Parent::operator=(cmap);
2163 template <typename V>
2164 class EdgeMap : public Digraph::template ArcMap<V> {
2168 typedef typename Digraph::template ArcMap<V> Parent;
2170 explicit EdgeMap(const UndirectorBase<DGR>& adaptor)
2171 : Parent(*adaptor._digraph) {}
2173 EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2174 : Parent(*adaptor._digraph, value) {}
2177 EdgeMap& operator=(const EdgeMap& cmap) {
2178 return operator=<EdgeMap>(cmap);
2181 template <typename CMap>
2182 EdgeMap& operator=(const CMap& cmap) {
2183 Parent::operator=(cmap);
2189 typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
2190 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2192 typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier;
2193 EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2197 UndirectorBase() : _digraph(0) {}
2201 void initialize(DGR& digraph) {
2202 _digraph = &digraph;
2207 /// \ingroup graph_adaptors
2209 /// \brief Adaptor class for viewing a digraph as an undirected graph.
2211 /// Undirector adaptor can be used for viewing a digraph as an undirected
2212 /// graph. All arcs of the underlying digraph are showed in the
2213 /// adaptor as an edge (and also as a pair of arcs, of course).
2214 /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
2216 /// The adapted digraph can also be modified through this adaptor
2217 /// by adding or removing nodes or edges, unless the \c GR template
2218 /// parameter is set to be \c const.
2220 /// \tparam DGR The type of the adapted digraph.
2221 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2222 /// It can also be specified to be \c const.
2224 /// \note The \c Node type of this adaptor and the adapted digraph are
2225 /// convertible to each other, moreover the \c Edge type of the adaptor
2226 /// and the \c Arc type of the adapted digraph are also convertible to
2228 /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type
2229 /// of the adapted digraph.)
2230 template<typename DGR>
2235 public GraphAdaptorExtender<UndirectorBase<DGR> > {
2238 /// The type of the adapted digraph.
2239 typedef DGR Digraph;
2240 typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent;
2245 /// \brief Constructor
2247 /// Creates an undirected graph from the given digraph.
2248 Undirector(DGR& digraph) {
2249 initialize(digraph);
2252 /// \brief Arc map combined from two original arc maps
2254 /// This map adaptor class adapts two arc maps of the underlying
2255 /// digraph to get an arc map of the undirected graph.
2256 /// Its value type is inherited from the first arc map type
2257 /// (\c %ForwardMap).
2258 template <typename ForwardMap, typename BackwardMap>
2259 class CombinedArcMap {
2262 /// The key type of the map
2263 typedef typename Parent::Arc Key;
2264 /// The value type of the map
2265 typedef typename ForwardMap::Value Value;
2267 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2269 typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
2270 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
2271 typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
2272 typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
2275 CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2276 : _forward(&forward), _backward(&backward) {}
2278 /// Sets the value associated with the given key.
2279 void set(const Key& e, const Value& a) {
2280 if (Parent::direction(e)) {
2281 _forward->set(e, a);
2283 _backward->set(e, a);
2287 /// Returns the value associated with the given key.
2288 ConstReturnValue operator[](const Key& e) const {
2289 if (Parent::direction(e)) {
2290 return (*_forward)[e];
2292 return (*_backward)[e];
2296 /// Returns a reference to the value associated with the given key.
2297 ReturnValue operator[](const Key& e) {
2298 if (Parent::direction(e)) {
2299 return (*_forward)[e];
2301 return (*_backward)[e];
2307 ForwardMap* _forward;
2308 BackwardMap* _backward;
2312 /// \brief Returns a combined arc map
2314 /// This function just returns a combined arc map.
2315 template <typename ForwardMap, typename BackwardMap>
2316 static CombinedArcMap<ForwardMap, BackwardMap>
2317 combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2318 return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2321 template <typename ForwardMap, typename BackwardMap>
2322 static CombinedArcMap<const ForwardMap, BackwardMap>
2323 combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2324 return CombinedArcMap<const ForwardMap,
2325 BackwardMap>(forward, backward);
2328 template <typename ForwardMap, typename BackwardMap>
2329 static CombinedArcMap<ForwardMap, const BackwardMap>
2330 combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2331 return CombinedArcMap<ForwardMap,
2332 const BackwardMap>(forward, backward);
2335 template <typename ForwardMap, typename BackwardMap>
2336 static CombinedArcMap<const ForwardMap, const BackwardMap>
2337 combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2338 return CombinedArcMap<const ForwardMap,
2339 const BackwardMap>(forward, backward);
2344 /// \brief Returns a read-only Undirector adaptor
2346 /// This function just returns a read-only \ref Undirector adaptor.
2347 /// \ingroup graph_adaptors
2348 /// \relates Undirector
2349 template<typename DGR>
2350 Undirector<const DGR> undirector(const DGR& digraph) {
2351 return Undirector<const DGR>(digraph);
2355 template <typename GR, typename DM>
2356 class OrienterBase {
2360 typedef DM DirectionMap;
2362 typedef typename GR::Node Node;
2363 typedef typename GR::Edge Arc;
2365 void reverseArc(const Arc& arc) {
2366 _direction->set(arc, !(*_direction)[arc]);
2369 void first(Node& i) const { _graph->first(i); }
2370 void first(Arc& i) const { _graph->first(i); }
2371 void firstIn(Arc& i, const Node& n) const {
2373 _graph->firstInc(i, d, n);
2374 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2376 void firstOut(Arc& i, const Node& n ) const {
2378 _graph->firstInc(i, d, n);
2379 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2382 void next(Node& i) const { _graph->next(i); }
2383 void next(Arc& i) const { _graph->next(i); }
2384 void nextIn(Arc& i) const {
2385 bool d = !(*_direction)[i];
2386 _graph->nextInc(i, d);
2387 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2389 void nextOut(Arc& i) const {
2390 bool d = (*_direction)[i];
2391 _graph->nextInc(i, d);
2392 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2395 Node source(const Arc& e) const {
2396 return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2398 Node target(const Arc& e) const {
2399 return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2402 typedef NodeNumTagIndicator<Graph> NodeNumTag;
2403 int nodeNum() const { return _graph->nodeNum(); }
2405 typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2406 int arcNum() const { return _graph->edgeNum(); }
2408 typedef FindEdgeTagIndicator<Graph> FindArcTag;
2409 Arc findArc(const Node& u, const Node& v,
2410 const Arc& prev = INVALID) const {
2411 Arc arc = _graph->findEdge(u, v, prev);
2412 while (arc != INVALID && source(arc) != u) {
2413 arc = _graph->findEdge(u, v, arc);
2419 return Node(_graph->addNode());
2422 Arc addArc(const Node& u, const Node& v) {
2423 Arc arc = _graph->addEdge(u, v);
2424 _direction->set(arc, _graph->u(arc) == u);
2428 void erase(const Node& i) { _graph->erase(i); }
2429 void erase(const Arc& i) { _graph->erase(i); }
2431 void clear() { _graph->clear(); }
2433 int id(const Node& v) const { return _graph->id(v); }
2434 int id(const Arc& e) const { return _graph->id(e); }
2436 Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2437 Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2439 int maxNodeId() const { return _graph->maxNodeId(); }
2440 int maxArcId() const { return _graph->maxEdgeId(); }
2442 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
2443 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2445 typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
2446 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2448 template <typename V>
2449 class NodeMap : public GR::template NodeMap<V> {
2452 typedef typename GR::template NodeMap<V> Parent;
2454 explicit NodeMap(const OrienterBase<GR, DM>& adapter)
2455 : Parent(*adapter._graph) {}
2457 NodeMap(const OrienterBase<GR, DM>& adapter, const V& value)
2458 : Parent(*adapter._graph, value) {}
2461 NodeMap& operator=(const NodeMap& cmap) {
2462 return operator=<NodeMap>(cmap);
2465 template <typename CMap>
2466 NodeMap& operator=(const CMap& cmap) {
2467 Parent::operator=(cmap);
2473 template <typename V>
2474 class ArcMap : public GR::template EdgeMap<V> {
2477 typedef typename Graph::template EdgeMap<V> Parent;
2479 explicit ArcMap(const OrienterBase<GR, DM>& adapter)
2480 : Parent(*adapter._graph) { }
2482 ArcMap(const OrienterBase<GR, DM>& adapter, const V& value)
2483 : Parent(*adapter._graph, value) { }
2486 ArcMap& operator=(const ArcMap& cmap) {
2487 return operator=<ArcMap>(cmap);
2490 template <typename CMap>
2491 ArcMap& operator=(const CMap& cmap) {
2492 Parent::operator=(cmap);
2503 void initialize(GR& graph, DM& direction) {
2505 _direction = &direction;
2510 /// \ingroup graph_adaptors
2512 /// \brief Adaptor class for orienting the edges of a graph to get a digraph
2514 /// Orienter adaptor can be used for orienting the edges of a graph to
2515 /// get a digraph. A \c bool edge map of the underlying graph must be
2516 /// specified, which define the direction of the arcs in the adaptor.
2517 /// The arcs can be easily reversed by the \c reverseArc() member function
2519 /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2521 /// The adapted graph can also be modified through this adaptor
2522 /// by adding or removing nodes or arcs, unless the \c GR template
2523 /// parameter is set to be \c const.
2525 /// \tparam GR The type of the adapted graph.
2526 /// It must conform to the \ref concepts::Graph "Graph" concept.
2527 /// It can also be specified to be \c const.
2528 /// \tparam DM The type of the direction map.
2529 /// It must be a \c bool (or convertible) edge map of the
2530 /// adapted graph. The default type is
2531 /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
2533 /// \note The \c Node type of this adaptor and the adapted graph are
2534 /// convertible to each other, moreover the \c Arc type of the adaptor
2535 /// and the \c Edge type of the adapted graph are also convertible to
2538 template<typename GR,
2542 template<typename GR,
2543 typename DM = typename GR::template EdgeMap<bool> >
2545 public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
2549 /// The type of the adapted graph.
2551 /// The type of the direction edge map.
2552 typedef DM DirectionMap;
2554 typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2555 typedef typename Parent::Arc Arc;
2560 /// \brief Constructor
2562 /// Constructor of the adaptor.
2563 Orienter(GR& graph, DM& direction) {
2564 Parent::initialize(graph, direction);
2567 /// \brief Reverses the given arc
2569 /// This function reverses the given arc.
2570 /// It is done by simply negate the assigned value of \c a
2571 /// in the direction map.
2572 void reverseArc(const Arc& a) {
2573 Parent::reverseArc(a);
2577 /// \brief Returns a read-only Orienter adaptor
2579 /// This function just returns a read-only \ref Orienter adaptor.
2580 /// \ingroup graph_adaptors
2581 /// \relates Orienter
2582 template<typename GR, typename DM>
2583 Orienter<const GR, DM>
2584 orienter(const GR& graph, DM& direction) {
2585 return Orienter<const GR, DM>(graph, direction);
2588 template<typename GR, typename DM>
2589 Orienter<const GR, const DM>
2590 orienter(const GR& graph, const DM& direction) {
2591 return Orienter<const GR, const DM>(graph, direction);
2594 namespace _adaptor_bits {
2596 template <typename DGR, typename CM, typename FM, typename TL>
2597 class ResForwardFilter {
2600 typedef typename DGR::Arc Key;
2605 const CM* _capacity;
2611 ResForwardFilter(const CM& capacity, const FM& flow,
2612 const TL& tolerance = TL())
2613 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2615 bool operator[](const typename DGR::Arc& a) const {
2616 return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2620 template<typename DGR,typename CM, typename FM, typename TL>
2621 class ResBackwardFilter {
2624 typedef typename DGR::Arc Key;
2629 const CM* _capacity;
2635 ResBackwardFilter(const CM& capacity, const FM& flow,
2636 const TL& tolerance = TL())
2637 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2639 bool operator[](const typename DGR::Arc& a) const {
2640 return _tolerance.positive((*_flow)[a]);
2646 /// \ingroup graph_adaptors
2648 /// \brief Adaptor class for composing the residual digraph for directed
2649 /// flow and circulation problems.
2651 /// ResidualDigraph can be used for composing the \e residual digraph
2652 /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$
2653 /// be a directed graph and let \f$ F \f$ be a number type.
2654 /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs.
2655 /// This adaptor implements a digraph structure with node set \f$ V \f$
2656 /// and arc set \f$ A_{forward}\cup A_{backward} \f$,
2657 /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and
2658 /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so
2659 /// called residual digraph.
2660 /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken,
2661 /// multiplicities are counted, i.e. the adaptor has exactly
2662 /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel
2664 /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2666 /// \tparam DGR The type of the adapted digraph.
2667 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2668 /// It is implicitly \c const.
2669 /// \tparam CM The type of the capacity map.
2670 /// It must be an arc map of some numerical type, which defines
2671 /// the capacities in the flow problem. It is implicitly \c const.
2672 /// The default type is
2673 /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
2674 /// \tparam FM The type of the flow map.
2675 /// It must be an arc map of some numerical type, which defines
2676 /// the flow values in the flow problem. The default type is \c CM.
2677 /// \tparam TL The tolerance type for handling inexact computation.
2678 /// The default tolerance type depends on the value type of the
2681 /// \note This adaptor is implemented using Undirector and FilterArcs
2684 /// \note The \c Node type of this adaptor and the adapted digraph are
2685 /// convertible to each other, moreover the \c Arc type of the adaptor
2686 /// is convertible to the \c Arc type of the adapted digraph.
2688 template<typename DGR, typename CM, typename FM, typename TL>
2689 class ResidualDigraph
2691 template<typename DGR,
2692 typename CM = typename DGR::template ArcMap<int>,
2694 typename TL = Tolerance<typename CM::Value> >
2695 class ResidualDigraph
2696 : public SubDigraph<
2697 Undirector<const DGR>,
2698 ConstMap<typename DGR::Node, Const<bool, true> >,
2699 typename Undirector<const DGR>::template CombinedArcMap<
2700 _adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>,
2701 _adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > >
2706 /// The type of the underlying digraph.
2707 typedef DGR Digraph;
2708 /// The type of the capacity map.
2709 typedef CM CapacityMap;
2710 /// The type of the flow map.
2712 /// The tolerance type.
2713 typedef TL Tolerance;
2715 typedef typename CapacityMap::Value Value;
2716 typedef ResidualDigraph Adaptor;
2720 typedef Undirector<const Digraph> Undirected;
2722 typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter;
2724 typedef _adaptor_bits::ResForwardFilter<const DGR, CM,
2725 FM, TL> ForwardFilter;
2727 typedef _adaptor_bits::ResBackwardFilter<const DGR, CM,
2728 FM, TL> BackwardFilter;
2730 typedef typename Undirected::
2731 template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2733 typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent;
2735 const CapacityMap* _capacity;
2739 NodeFilter _node_filter;
2740 ForwardFilter _forward_filter;
2741 BackwardFilter _backward_filter;
2742 ArcFilter _arc_filter;
2746 /// \brief Constructor
2748 /// Constructor of the residual digraph adaptor. The parameters are the
2749 /// digraph, the capacity map, the flow map, and a tolerance object.
2750 ResidualDigraph(const DGR& digraph, const CM& capacity,
2751 FM& flow, const TL& tolerance = Tolerance())
2752 : Parent(), _capacity(&capacity), _flow(&flow),
2753 _graph(digraph), _node_filter(),
2754 _forward_filter(capacity, flow, tolerance),
2755 _backward_filter(capacity, flow, tolerance),
2756 _arc_filter(_forward_filter, _backward_filter)
2758 Parent::initialize(_graph, _node_filter, _arc_filter);
2761 typedef typename Parent::Arc Arc;
2763 /// \brief Returns the residual capacity of the given arc.
2765 /// Returns the residual capacity of the given arc.
2766 Value residualCapacity(const Arc& a) const {
2767 if (Undirected::direction(a)) {
2768 return (*_capacity)[a] - (*_flow)[a];
2774 /// \brief Augments on the given arc in the residual digraph.
2776 /// Augments on the given arc in the residual digraph. It increases
2777 /// or decreases the flow value on the original arc according to the
2778 /// direction of the residual arc.
2779 void augment(const Arc& a, const Value& v) const {
2780 if (Undirected::direction(a)) {
2781 _flow->set(a, (*_flow)[a] + v);
2783 _flow->set(a, (*_flow)[a] - v);
2787 /// \brief Returns \c true if the given residual arc is a forward arc.
2789 /// Returns \c true if the given residual arc has the same orientation
2790 /// as the original arc, i.e. it is a so called forward arc.
2791 static bool forward(const Arc& a) {
2792 return Undirected::direction(a);
2795 /// \brief Returns \c true if the given residual arc is a backward arc.
2797 /// Returns \c true if the given residual arc has the opposite orientation
2798 /// than the original arc, i.e. it is a so called backward arc.
2799 static bool backward(const Arc& a) {
2800 return !Undirected::direction(a);
2803 /// \brief Returns the forward oriented residual arc.
2805 /// Returns the forward oriented residual arc related to the given
2806 /// arc of the underlying digraph.
2807 static Arc forward(const typename Digraph::Arc& a) {
2808 return Undirected::direct(a, true);
2811 /// \brief Returns the backward oriented residual arc.
2813 /// Returns the backward oriented residual arc related to the given
2814 /// arc of the underlying digraph.
2815 static Arc backward(const typename Digraph::Arc& a) {
2816 return Undirected::direct(a, false);
2819 /// \brief Residual capacity map.
2821 /// This map adaptor class can be used for obtaining the residual
2822 /// capacities as an arc map of the residual digraph.
2823 /// Its value type is inherited from the capacity map.
2824 class ResidualCapacity {
2826 const Adaptor* _adaptor;
2828 /// The key type of the map
2830 /// The value type of the map
2831 typedef typename CapacityMap::Value Value;
2834 ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor)
2835 : _adaptor(&adaptor) {}
2837 /// Returns the value associated with the given residual arc
2838 Value operator[](const Arc& a) const {
2839 return _adaptor->residualCapacity(a);
2844 /// \brief Returns a residual capacity map
2846 /// This function just returns a residual capacity map.
2847 ResidualCapacity residualCapacity() const {
2848 return ResidualCapacity(*this);
2853 /// \brief Returns a (read-only) Residual adaptor
2855 /// This function just returns a (read-only) \ref ResidualDigraph adaptor.
2856 /// \ingroup graph_adaptors
2857 /// \relates ResidualDigraph
2858 template<typename DGR, typename CM, typename FM>
2859 ResidualDigraph<DGR, CM, FM>
2860 residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) {
2861 return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map);
2865 template <typename DGR>
2866 class SplitNodesBase {
2869 typedef DGR Digraph;
2870 typedef DigraphAdaptorBase<const DGR> Parent;
2871 typedef SplitNodesBase Adaptor;
2873 typedef typename DGR::Node DigraphNode;
2874 typedef typename DGR::Arc DigraphArc;
2881 template <typename T> class NodeMapBase;
2882 template <typename T> class ArcMapBase;
2886 class Node : public DigraphNode {
2887 friend class SplitNodesBase;
2888 template <typename T> friend class NodeMapBase;
2892 Node(DigraphNode node, bool in)
2893 : DigraphNode(node), _in(in) {}
2898 Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2900 bool operator==(const Node& node) const {
2901 return DigraphNode::operator==(node) && _in == node._in;
2904 bool operator!=(const Node& node) const {
2905 return !(*this == node);
2908 bool operator<(const Node& node) const {
2909 return DigraphNode::operator<(node) ||
2910 (DigraphNode::operator==(node) && _in < node._in);
2915 friend class SplitNodesBase;
2916 template <typename T> friend class ArcMapBase;
2918 typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2920 explicit Arc(const DigraphArc& arc) : _item(arc) {}
2921 explicit Arc(const DigraphNode& node) : _item(node) {}
2927 Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2929 bool operator==(const Arc& arc) const {
2930 if (_item.firstState()) {
2931 if (arc._item.firstState()) {
2932 return _item.first() == arc._item.first();
2935 if (arc._item.secondState()) {
2936 return _item.second() == arc._item.second();
2942 bool operator!=(const Arc& arc) const {
2943 return !(*this == arc);
2946 bool operator<(const Arc& arc) const {
2947 if (_item.firstState()) {
2948 if (arc._item.firstState()) {
2949 return _item.first() < arc._item.first();
2953 if (arc._item.secondState()) {
2954 return _item.second() < arc._item.second();
2960 operator DigraphArc() const { return _item.first(); }
2961 operator DigraphNode() const { return _item.second(); }
2965 void first(Node& n) const {
2970 void next(Node& n) const {
2979 void first(Arc& e) const {
2980 e._item.setSecond();
2981 _digraph->first(e._item.second());
2982 if (e._item.second() == INVALID) {
2984 _digraph->first(e._item.first());
2988 void next(Arc& e) const {
2989 if (e._item.secondState()) {
2990 _digraph->next(e._item.second());
2991 if (e._item.second() == INVALID) {
2993 _digraph->first(e._item.first());
2996 _digraph->next(e._item.first());
3000 void firstOut(Arc& e, const Node& n) const {
3002 e._item.setSecond(n);
3005 _digraph->firstOut(e._item.first(), n);
3009 void nextOut(Arc& e) const {
3010 if (!e._item.firstState()) {
3011 e._item.setFirst(INVALID);
3013 _digraph->nextOut(e._item.first());
3017 void firstIn(Arc& e, const Node& n) const {
3019 e._item.setSecond(n);
3022 _digraph->firstIn(e._item.first(), n);
3026 void nextIn(Arc& e) const {
3027 if (!e._item.firstState()) {
3028 e._item.setFirst(INVALID);
3030 _digraph->nextIn(e._item.first());
3034 Node source(const Arc& e) const {
3035 if (e._item.firstState()) {
3036 return Node(_digraph->source(e._item.first()), false);
3038 return Node(e._item.second(), true);
3042 Node target(const Arc& e) const {
3043 if (e._item.firstState()) {
3044 return Node(_digraph->target(e._item.first()), true);
3046 return Node(e._item.second(), false);
3050 int id(const Node& n) const {
3051 return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
3053 Node nodeFromId(int ix) const {
3054 return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
3056 int maxNodeId() const {
3057 return 2 * _digraph->maxNodeId() + 1;
3060 int id(const Arc& e) const {
3061 if (e._item.firstState()) {
3062 return _digraph->id(e._item.first()) << 1;
3064 return (_digraph->id(e._item.second()) << 1) | 1;
3067 Arc arcFromId(int ix) const {
3068 if ((ix & 1) == 0) {
3069 return Arc(_digraph->arcFromId(ix >> 1));
3071 return Arc(_digraph->nodeFromId(ix >> 1));
3074 int maxArcId() const {
3075 return std::max(_digraph->maxNodeId() << 1,
3076 (_digraph->maxArcId() << 1) | 1);
3079 static bool inNode(const Node& n) {
3083 static bool outNode(const Node& n) {
3087 static bool origArc(const Arc& e) {
3088 return e._item.firstState();
3091 static bool bindArc(const Arc& e) {
3092 return e._item.secondState();
3095 static Node inNode(const DigraphNode& n) {
3096 return Node(n, true);
3099 static Node outNode(const DigraphNode& n) {
3100 return Node(n, false);
3103 static Arc arc(const DigraphNode& n) {
3107 static Arc arc(const DigraphArc& e) {
3111 typedef True NodeNumTag;
3112 int nodeNum() const {
3113 return 2 * countNodes(*_digraph);
3116 typedef True ArcNumTag;
3117 int arcNum() const {
3118 return countArcs(*_digraph) + countNodes(*_digraph);
3121 typedef True FindArcTag;
3122 Arc findArc(const Node& u, const Node& v,
3123 const Arc& prev = INVALID) const {
3124 if (inNode(u) && outNode(v)) {
3125 if (static_cast<const DigraphNode&>(u) ==
3126 static_cast<const DigraphNode&>(v) && prev == INVALID) {
3130 else if (outNode(u) && inNode(v)) {
3131 return Arc(::lemon::findArc(*_digraph, u, v, prev));
3138 template <typename V>
3140 : public MapTraits<typename Parent::template NodeMap<V> > {
3141 typedef typename Parent::template NodeMap<V> NodeImpl;
3145 typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
3146 typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
3147 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
3148 typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
3149 typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
3151 NodeMapBase(const SplitNodesBase<DGR>& adaptor)
3152 : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
3153 NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3154 : _in_map(*adaptor._digraph, value),
3155 _out_map(*adaptor._digraph, value) {}
3157 void set(const Node& key, const V& val) {
3158 if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); }
3159 else {_out_map.set(key, val); }
3162 ReturnValue operator[](const Node& key) {
3163 if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; }
3164 else { return _out_map[key]; }
3167 ConstReturnValue operator[](const Node& key) const {
3168 if (Adaptor::inNode(key)) { return _in_map[key]; }
3169 else { return _out_map[key]; }
3173 NodeImpl _in_map, _out_map;
3176 template <typename V>
3178 : public MapTraits<typename Parent::template ArcMap<V> > {
3179 typedef typename Parent::template ArcMap<V> ArcImpl;
3180 typedef typename Parent::template NodeMap<V> NodeImpl;
3184 typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
3185 typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
3186 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
3187 typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
3188 typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
3190 ArcMapBase(const SplitNodesBase<DGR>& adaptor)
3191 : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
3192 ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3193 : _arc_map(*adaptor._digraph, value),
3194 _node_map(*adaptor._digraph, value) {}
3196 void set(const Arc& key, const V& val) {
3197 if (SplitNodesBase<DGR>::origArc(key)) {
3198 _arc_map.set(key._item.first(), val);
3200 _node_map.set(key._item.second(), val);
3204 ReturnValue operator[](const Arc& key) {
3205 if (SplitNodesBase<DGR>::origArc(key)) {
3206 return _arc_map[key._item.first()];
3208 return _node_map[key._item.second()];
3212 ConstReturnValue operator[](const Arc& key) const {
3213 if (SplitNodesBase<DGR>::origArc(key)) {
3214 return _arc_map[key._item.first()];
3216 return _node_map[key._item.second()];
3227 template <typename V>
3229 : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> >
3233 typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent;
3235 NodeMap(const SplitNodesBase<DGR>& adaptor)
3236 : Parent(adaptor) {}
3238 NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3239 : Parent(adaptor, value) {}
3242 NodeMap& operator=(const NodeMap& cmap) {
3243 return operator=<NodeMap>(cmap);
3246 template <typename CMap>
3247 NodeMap& operator=(const CMap& cmap) {
3248 Parent::operator=(cmap);
3253 template <typename V>
3255 : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> >
3259 typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent;
3261 ArcMap(const SplitNodesBase<DGR>& adaptor)
3262 : Parent(adaptor) {}
3264 ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3265 : Parent(adaptor, value) {}
3268 ArcMap& operator=(const ArcMap& cmap) {
3269 return operator=<ArcMap>(cmap);
3272 template <typename CMap>
3273 ArcMap& operator=(const CMap& cmap) {
3274 Parent::operator=(cmap);
3281 SplitNodesBase() : _digraph(0) {}
3285 void initialize(Digraph& digraph) {
3286 _digraph = &digraph;
3291 /// \ingroup graph_adaptors
3293 /// \brief Adaptor class for splitting the nodes of a digraph.
3295 /// SplitNodes adaptor can be used for splitting each node into an
3296 /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor
3297 /// replaces each node \f$ u \f$ in the digraph with two nodes,
3298 /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$.
3299 /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the
3300 /// new target of the arc will be \f$ u_{in} \f$ and similarly the
3301 /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$.
3302 /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$
3303 /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph.
3305 /// The aim of this class is running an algorithm with respect to node
3306 /// costs or capacities if the algorithm considers only arc costs or
3307 /// capacities directly.
3308 /// In this case you can use \c SplitNodes adaptor, and set the node
3309 /// costs/capacities of the original digraph to the \e bind \e arcs
3312 /// \tparam DGR The type of the adapted digraph.
3313 /// It must conform to the \ref concepts::Digraph "Digraph" concept.
3314 /// It is implicitly \c const.
3316 /// \note The \c Node type of this adaptor is converible to the \c Node
3317 /// type of the adapted digraph.
3318 template <typename DGR>
3323 : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > {
3326 typedef DGR Digraph;
3327 typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent;
3329 typedef typename DGR::Node DigraphNode;
3330 typedef typename DGR::Arc DigraphArc;
3332 typedef typename Parent::Node Node;
3333 typedef typename Parent::Arc Arc;
3335 /// \brief Constructor
3337 /// Constructor of the adaptor.
3338 SplitNodes(const DGR& g) {
3339 Parent::initialize(g);
3342 /// \brief Returns \c true if the given node is an in-node.
3344 /// Returns \c true if the given node is an in-node.
3345 static bool inNode(const Node& n) {
3346 return Parent::inNode(n);
3349 /// \brief Returns \c true if the given node is an out-node.
3351 /// Returns \c true if the given node is an out-node.
3352 static bool outNode(const Node& n) {
3353 return Parent::outNode(n);
3356 /// \brief Returns \c true if the given arc is an original arc.
3358 /// Returns \c true if the given arc is one of the arcs in the
3359 /// original digraph.
3360 static bool origArc(const Arc& a) {
3361 return Parent::origArc(a);
3364 /// \brief Returns \c true if the given arc is a bind arc.
3366 /// Returns \c true if the given arc is a bind arc, i.e. it connects
3367 /// an in-node and an out-node.
3368 static bool bindArc(const Arc& a) {
3369 return Parent::bindArc(a);
3372 /// \brief Returns the in-node created from the given original node.
3374 /// Returns the in-node created from the given original node.
3375 static Node inNode(const DigraphNode& n) {
3376 return Parent::inNode(n);
3379 /// \brief Returns the out-node created from the given original node.
3381 /// Returns the out-node created from the given original node.
3382 static Node outNode(const DigraphNode& n) {
3383 return Parent::outNode(n);
3386 /// \brief Returns the bind arc that corresponds to the given
3389 /// Returns the bind arc in the adaptor that corresponds to the given
3390 /// original node, i.e. the arc connecting the in-node and out-node
3392 static Arc arc(const DigraphNode& n) {
3393 return Parent::arc(n);
3396 /// \brief Returns the arc that corresponds to the given original arc.
3398 /// Returns the arc in the adaptor that corresponds to the given
3400 static Arc arc(const DigraphArc& a) {
3401 return Parent::arc(a);
3404 /// \brief Node map combined from two original node maps
3406 /// This map adaptor class adapts two node maps of the original digraph
3407 /// to get a node map of the split digraph.
3408 /// Its value type is inherited from the first node map type
3410 template <typename InNodeMap, typename OutNodeMap>
3411 class CombinedNodeMap {
3414 /// The key type of the map
3416 /// The value type of the map
3417 typedef typename InNodeMap::Value Value;
3419 typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
3420 typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
3421 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
3422 typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
3423 typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
3426 CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3427 : _in_map(in_map), _out_map(out_map) {}
3429 /// Returns the value associated with the given key.
3430 Value operator[](const Key& key) const {
3431 if (SplitNodesBase<const DGR>::inNode(key)) {
3432 return _in_map[key];
3434 return _out_map[key];
3438 /// Returns a reference to the value associated with the given key.
3439 Value& operator[](const Key& key) {
3440 if (SplitNodesBase<const DGR>::inNode(key)) {
3441 return _in_map[key];
3443 return _out_map[key];
3447 /// Sets the value associated with the given key.
3448 void set(const Key& key, const Value& value) {
3449 if (SplitNodesBase<const DGR>::inNode(key)) {
3450 _in_map.set(key, value);
3452 _out_map.set(key, value);
3459 OutNodeMap& _out_map;
3464 /// \brief Returns a combined node map
3466 /// This function just returns a combined node map.
3467 template <typename InNodeMap, typename OutNodeMap>
3468 static CombinedNodeMap<InNodeMap, OutNodeMap>
3469 combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3470 return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3473 template <typename InNodeMap, typename OutNodeMap>
3474 static CombinedNodeMap<const InNodeMap, OutNodeMap>
3475 combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3476 return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3479 template <typename InNodeMap, typename OutNodeMap>
3480 static CombinedNodeMap<InNodeMap, const OutNodeMap>
3481 combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3482 return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3485 template <typename InNodeMap, typename OutNodeMap>
3486 static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3487 combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3488 return CombinedNodeMap<const InNodeMap,
3489 const OutNodeMap>(in_map, out_map);
3492 /// \brief Arc map combined from an arc map and a node map of the
3493 /// original digraph.
3495 /// This map adaptor class adapts an arc map and a node map of the
3496 /// original digraph to get an arc map of the split digraph.
3497 /// Its value type is inherited from the original arc map type
3499 template <typename ArcMap, typename NodeMap>
3500 class CombinedArcMap {
3503 /// The key type of the map
3505 /// The value type of the map
3506 typedef typename ArcMap::Value Value;
3508 typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag;
3509 typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue;
3510 typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue;
3511 typedef typename MapTraits<ArcMap>::ReturnValue Reference;
3512 typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference;
3515 CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
3516 : _arc_map(arc_map), _node_map(node_map) {}
3518 /// Returns the value associated with the given key.
3519 Value operator[](const Key& arc) const {
3520 if (SplitNodesBase<const DGR>::origArc(arc)) {
3521 return _arc_map[arc];
3523 return _node_map[arc];
3527 /// Returns a reference to the value associated with the given key.
3528 Value& operator[](const Key& arc) {
3529 if (SplitNodesBase<const DGR>::origArc(arc)) {
3530 return _arc_map[arc];
3532 return _node_map[arc];
3536 /// Sets the value associated with the given key.
3537 void set(const Arc& arc, const Value& val) {
3538 if (SplitNodesBase<const DGR>::origArc(arc)) {
3539 _arc_map.set(arc, val);
3541 _node_map.set(arc, val);
3550 /// \brief Returns a combined arc map
3552 /// This function just returns a combined arc map.
3553 template <typename ArcMap, typename NodeMap>
3554 static CombinedArcMap<ArcMap, NodeMap>
3555 combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
3556 return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
3559 template <typename ArcMap, typename NodeMap>
3560 static CombinedArcMap<const ArcMap, NodeMap>
3561 combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
3562 return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
3565 template <typename ArcMap, typename NodeMap>
3566 static CombinedArcMap<ArcMap, const NodeMap>
3567 combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
3568 return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
3571 template <typename ArcMap, typename NodeMap>
3572 static CombinedArcMap<const ArcMap, const NodeMap>
3573 combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
3574 return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
3579 /// \brief Returns a (read-only) SplitNodes adaptor
3581 /// This function just returns a (read-only) \ref SplitNodes adaptor.
3582 /// \ingroup graph_adaptors
3583 /// \relates SplitNodes
3584 template<typename DGR>
3586 splitNodes(const DGR& digraph) {
3587 return SplitNodes<DGR>(digraph);
3590 #undef LEMON_SCOPE_FIX
3594 #endif //LEMON_ADAPTORS_H