diff --git a/lemon/digraph_adaptor.h b/lemon/adaptors.h copy from lemon/digraph_adaptor.h copy to lemon/adaptors.h --- a/lemon/digraph_adaptor.h +++ b/lemon/adaptors.h @@ -1,6 +1,6 @@ -/* -*- C++ -*- +/* -*- mode: C++; indent-tabs-mode: nil; -*- * - * This file is a part of LEMON, a generic C++ optimization library + * This file is a part of LEMON, a generic C++ optimization library. * * Copyright (C) 2003-2008 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport @@ -16,22 +16,20 @@ * */ -#ifndef LEMON_DIGRAPH_ADAPTOR_H -#define LEMON_DIGRAPH_ADAPTOR_H - -///\ingroup graph_adaptors -///\file -///\brief Several digraph adaptors. +#ifndef LEMON_ADAPTORS_H +#define LEMON_ADAPTORS_H + +/// \ingroup graph_adaptors +/// \file +/// \brief Several graph adaptors /// -///This file contains several useful digraph adaptor classes. +/// This file contains several useful adaptors for digraphs and graphs. #include #include #include -#include #include -#include #include #include @@ -55,7 +53,7 @@ typedef typename Digraph::Node Node; typedef typename Digraph::Arc Arc; - + void first(Node& i) const { _digraph->first(i); } void first(Arc& i) const { _digraph->first(i); } void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } @@ -71,7 +69,7 @@ typedef NodeNumTagIndicator NodeNumTag; int nodeNum() const { return _digraph->nodeNum(); } - + typedef EdgeNumTagIndicator EdgeNumTag; int arcNum() const { return _digraph->arcNum(); } @@ -79,15 +77,15 @@ Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { return _digraph->findArc(u, v, prev); } - + Node addNode() { return _digraph->addNode(); } Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } void erase(const Node& n) const { _digraph->erase(n); } void erase(const Arc& a) const { _digraph->erase(a); } - + void clear() const { _digraph->clear(); } - + int id(const Node& n) const { return _digraph->id(n); } int id(const Arc& a) const { return _digraph->id(a); } @@ -98,22 +96,22 @@ int maxArcId() const { return _digraph->maxArcId(); } typedef typename ItemSetTraits::ItemNotifier NodeNotifier; - NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } + NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } typedef typename ItemSetTraits::ItemNotifier ArcNotifier; - ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } - + ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } + template class NodeMap : public Digraph::template NodeMap<_Value> { public: typedef typename Digraph::template NodeMap<_Value> Parent; - explicit NodeMap(const Adaptor& adaptor) - : Parent(*adaptor._digraph) {} + explicit NodeMap(const Adaptor& adaptor) + : Parent(*adaptor._digraph) {} NodeMap(const Adaptor& adaptor, const _Value& value) - : Parent(*adaptor._digraph, value) { } + : Parent(*adaptor._digraph, value) { } private: NodeMap& operator=(const NodeMap& cmap) { @@ -125,20 +123,20 @@ Parent::operator=(cmap); return *this; } - + }; template class ArcMap : public Digraph::template ArcMap<_Value> { public: - + typedef typename Digraph::template ArcMap<_Value> Parent; - - explicit ArcMap(const Adaptor& adaptor) - : Parent(*adaptor._digraph) {} + + explicit ArcMap(const Adaptor& adaptor) + : Parent(*adaptor._digraph) {} ArcMap(const Adaptor& adaptor, const _Value& value) - : Parent(*adaptor._digraph, value) {} + : Parent(*adaptor._digraph, value) {} private: ArcMap& operator=(const ArcMap& cmap) { @@ -155,14 +153,168 @@ }; + template + class GraphAdaptorBase { + public: + typedef _Graph Graph; + typedef Graph ParentGraph; + + protected: + Graph* _graph; + + GraphAdaptorBase() : _graph(0) {} + + void setGraph(Graph& graph) { _graph = &graph; } + + public: + GraphAdaptorBase(Graph& graph) : _graph(&graph) {} + + typedef typename Graph::Node Node; + typedef typename Graph::Arc Arc; + typedef typename Graph::Edge Edge; + + void first(Node& i) const { _graph->first(i); } + void first(Arc& i) const { _graph->first(i); } + void first(Edge& i) const { _graph->first(i); } + void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } + void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } + void firstInc(Edge &i, bool &d, const Node &n) const { + _graph->firstInc(i, d, n); + } + + void next(Node& i) const { _graph->next(i); } + void next(Arc& i) const { _graph->next(i); } + void next(Edge& i) const { _graph->next(i); } + void nextIn(Arc& i) const { _graph->nextIn(i); } + void nextOut(Arc& i) const { _graph->nextOut(i); } + void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } + + Node u(const Edge& e) const { return _graph->u(e); } + Node v(const Edge& e) const { return _graph->v(e); } + + Node source(const Arc& a) const { return _graph->source(a); } + Node target(const Arc& a) const { return _graph->target(a); } + + typedef NodeNumTagIndicator NodeNumTag; + int nodeNum() const { return _graph->nodeNum(); } + + typedef EdgeNumTagIndicator EdgeNumTag; + int arcNum() const { return _graph->arcNum(); } + int edgeNum() const { return _graph->edgeNum(); } + + typedef FindEdgeTagIndicator FindEdgeTag; + Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { + return _graph->findArc(u, v, prev); + } + Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) { + return _graph->findEdge(u, v, prev); + } + + Node addNode() { return _graph->addNode(); } + Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } + + void erase(const Node& i) { _graph->erase(i); } + void erase(const Edge& i) { _graph->erase(i); } + + void clear() { _graph->clear(); } + + bool direction(const Arc& a) const { return _graph->direction(a); } + Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } + + int id(const Node& v) const { return _graph->id(v); } + int id(const Arc& a) const { return _graph->id(a); } + int id(const Edge& e) const { return _graph->id(e); } + + Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } + Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } + Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } + + int maxNodeId() const { return _graph->maxNodeId(); } + int maxArcId() const { return _graph->maxArcId(); } + int maxEdgeId() const { return _graph->maxEdgeId(); } + + typedef typename ItemSetTraits::ItemNotifier NodeNotifier; + NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } + + typedef typename ItemSetTraits::ItemNotifier ArcNotifier; + ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } + + typedef typename ItemSetTraits::ItemNotifier EdgeNotifier; + EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } + + template + class NodeMap : public Graph::template NodeMap<_Value> { + public: + typedef typename Graph::template NodeMap<_Value> Parent; + explicit NodeMap(const GraphAdaptorBase& adapter) + : Parent(*adapter._graph) {} + NodeMap(const GraphAdaptorBase& adapter, const _Value& value) + : Parent(*adapter._graph, value) {} + + private: + NodeMap& operator=(const NodeMap& cmap) { + return operator=(cmap); + } + + template + NodeMap& operator=(const CMap& cmap) { + Parent::operator=(cmap); + return *this; + } + + }; + + template + class ArcMap : public Graph::template ArcMap<_Value> { + public: + typedef typename Graph::template ArcMap<_Value> Parent; + explicit ArcMap(const GraphAdaptorBase& adapter) + : Parent(*adapter._graph) {} + ArcMap(const GraphAdaptorBase& adapter, const _Value& value) + : Parent(*adapter._graph, value) {} + + private: + ArcMap& operator=(const ArcMap& cmap) { + return operator=(cmap); + } + + template + ArcMap& operator=(const CMap& cmap) { + Parent::operator=(cmap); + return *this; + } + }; + + template + class EdgeMap : public Graph::template EdgeMap<_Value> { + public: + typedef typename Graph::template EdgeMap<_Value> Parent; + explicit EdgeMap(const GraphAdaptorBase& adapter) + : Parent(*adapter._graph) {} + EdgeMap(const GraphAdaptorBase& adapter, const _Value& value) + : Parent(*adapter._graph, value) {} + + private: + EdgeMap& operator=(const EdgeMap& cmap) { + return operator=(cmap); + } + + template + EdgeMap& operator=(const CMap& cmap) { + Parent::operator=(cmap); + return *this; + } + }; + + }; template - class RevDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { + class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> { public: typedef _Digraph Digraph; typedef DigraphAdaptorBase<_Digraph> Parent; protected: - RevDigraphAdaptorBase() : Parent() { } + ReverseDigraphBase() : Parent() { } public: typedef typename Parent::Node Node; typedef typename Parent::Arc Arc; @@ -176,78 +328,42 @@ Node source(const Arc& a) const { return Parent::target(a); } Node target(const Arc& a) const { return Parent::source(a); } + Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } + typedef FindEdgeTagIndicator FindEdgeTag; - Arc findArc(const Node& u, const Node& v, - const Arc& prev = INVALID) { + Arc findArc(const Node& u, const Node& v, + const Arc& prev = INVALID) { return Parent::findArc(v, u, prev); } }; - - - ///\ingroup graph_adaptors + + /// \ingroup graph_adaptors /// - ///\brief A digraph adaptor which reverses the orientation of the arcs. + /// \brief A digraph adaptor which reverses the orientation of the arcs. /// - /// If \c g is defined as - ///\code - /// ListDigraph dg; - ///\endcode - /// then - ///\code - /// RevDigraphAdaptor dga(dg); - ///\endcode - /// implements the digraph obtained from \c dg by - /// reversing the orientation of its arcs. + /// ReverseDigraph reverses the arcs in the adapted digraph. The + /// SubDigraph is conform to the \ref concepts::Digraph + /// "Digraph concept". /// - /// A good example of using RevDigraphAdaptor is to decide whether - /// the directed graph is strongly connected or not. The digraph is - /// strongly connected iff each node is reachable from one node and - /// this node is reachable from the others. Instead of this - /// condition we use a slightly different, from one node each node - /// is reachable both in the digraph and the reversed digraph. Now - /// this condition can be checked with the Dfs algorithm and the - /// RevDigraphAdaptor class. - /// - /// The implementation: - ///\code - /// bool stronglyConnected(const Digraph& digraph) { - /// if (NodeIt(digraph) == INVALID) return true; - /// Dfs dfs(digraph); - /// dfs.run(NodeIt(digraph)); - /// for (NodeIt it(digraph); it != INVALID; ++it) { - /// if (!dfs.reached(it)) { - /// return false; - /// } - /// } - /// typedef RevDigraphAdaptor RDigraph; - /// RDigraph rdigraph(digraph); - /// DfsVisit rdfs(rdigraph); - /// rdfs.run(NodeIt(digraph)); - /// for (NodeIt it(digraph); it != INVALID; ++it) { - /// if (!rdfs.reached(it)) { - /// return false; - /// } - /// } - /// return true; - /// } - ///\endcode + /// \tparam _Digraph It must be conform to the \ref concepts::Digraph + /// "Digraph concept". The type can be specified to be const. template - class RevDigraphAdaptor : - public DigraphAdaptorExtender > { + class ReverseDigraph : + public DigraphAdaptorExtender > { public: typedef _Digraph Digraph; typedef DigraphAdaptorExtender< - RevDigraphAdaptorBase<_Digraph> > Parent; + ReverseDigraphBase<_Digraph> > Parent; protected: - RevDigraphAdaptor() { } + ReverseDigraph() { } public: /// \brief Constructor /// - /// Creates a reverse graph adaptor for the given digraph - explicit RevDigraphAdaptor(Digraph& digraph) { - Parent::setDigraph(digraph); + /// Creates a reverse digraph adaptor for the given digraph + explicit ReverseDigraph(Digraph& digraph) { + Parent::setDigraph(digraph); } }; @@ -255,25 +371,24 @@ /// /// Just gives back a reverse digraph adaptor template - RevDigraphAdaptor - revDigraphAdaptor(const Digraph& digraph) { - return RevDigraphAdaptor(digraph); + ReverseDigraph reverseDigraph(const Digraph& digraph) { + return ReverseDigraph(digraph); } - template - class SubDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { + template + class SubDigraphBase : public DigraphAdaptorBase<_Digraph> { public: typedef _Digraph Digraph; typedef _NodeFilterMap NodeFilterMap; typedef _ArcFilterMap ArcFilterMap; - typedef SubDigraphAdaptorBase Adaptor; + typedef SubDigraphBase Adaptor; typedef DigraphAdaptorBase<_Digraph> Parent; protected: NodeFilterMap* _node_filter; ArcFilterMap* _arc_filter; - SubDigraphAdaptorBase() + SubDigraphBase() : Parent(), _node_filter(0), _arc_filter(0) { } void setNodeFilterMap(NodeFilterMap& node_filter) { @@ -288,52 +403,58 @@ typedef typename Parent::Node Node; typedef typename Parent::Arc Arc; - void first(Node& i) const { - Parent::first(i); - while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); + void first(Node& i) const { + Parent::first(i); + while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); } - void first(Arc& i) const { - Parent::first(i); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::source(i)] - || !(*_node_filter)[Parent::target(i)])) Parent::next(i); + void first(Arc& i) const { + Parent::first(i); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::source(i)] + || !(*_node_filter)[Parent::target(i)])) + Parent::next(i); } - void firstIn(Arc& i, const Node& n) const { - Parent::firstIn(i, n); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); + void firstIn(Arc& i, const Node& n) const { + Parent::firstIn(i, n); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::source(i)])) + Parent::nextIn(i); } - void firstOut(Arc& i, const Node& n) const { - Parent::firstOut(i, n); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); + void firstOut(Arc& i, const Node& n) const { + Parent::firstOut(i, n); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::target(i)])) + Parent::nextOut(i); } - void next(Node& i) const { - Parent::next(i); - while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); + void next(Node& i) const { + Parent::next(i); + while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); } - void next(Arc& i) const { - Parent::next(i); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::source(i)] - || !(*_node_filter)[Parent::target(i)])) Parent::next(i); + void next(Arc& i) const { + Parent::next(i); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::source(i)] + || !(*_node_filter)[Parent::target(i)])) + Parent::next(i); } - void nextIn(Arc& i) const { - Parent::nextIn(i); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); + void nextIn(Arc& i) const { + Parent::nextIn(i); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::source(i)])) + Parent::nextIn(i); } - void nextOut(Arc& i) const { - Parent::nextOut(i); - while (i != INVALID && (!(*_arc_filter)[i] - || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); + void nextOut(Arc& i) const { + Parent::nextOut(i); + while (i != INVALID && (!(*_arc_filter)[i] + || !(*_node_filter)[Parent::target(i)])) + Parent::nextOut(i); } void hide(const Node& n) const { _node_filter->set(n, false); } @@ -349,8 +470,8 @@ typedef False EdgeNumTag; typedef FindEdgeTagIndicator FindEdgeTag; - Arc findArc(const Node& source, const Node& target, - const Arc& prev = INVALID) { + Arc findArc(const Node& source, const Node& target, + const Arc& prev = INVALID) { if (!(*_node_filter)[source] || !(*_node_filter)[target]) { return INVALID; } @@ -362,71 +483,71 @@ } template - class NodeMap : public SubMapExtender > { + class NodeMap : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > MapParent; - - NodeMap(const Adaptor& adaptor) - : MapParent(adaptor) {} - NodeMap(const Adaptor& adaptor, const Value& value) - : MapParent(adaptor, value) {} - + + NodeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + NodeMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + private: NodeMap& operator=(const NodeMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template NodeMap& operator=(const CMap& cmap) { MapParent::operator=(cmap); - return *this; + return *this; } }; template - class ArcMap : public SubMapExtender > { + class ArcMap : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > MapParent; - - ArcMap(const Adaptor& adaptor) - : MapParent(adaptor) {} - ArcMap(const Adaptor& adaptor, const Value& value) - : MapParent(adaptor, value) {} - + + ArcMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + ArcMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + private: ArcMap& operator=(const ArcMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template ArcMap& operator=(const CMap& cmap) { MapParent::operator=(cmap); - return *this; + return *this; } }; }; template - class SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> + class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> : public DigraphAdaptorBase<_Digraph> { public: typedef _Digraph Digraph; typedef _NodeFilterMap NodeFilterMap; typedef _ArcFilterMap ArcFilterMap; - typedef SubDigraphAdaptorBase Adaptor; + typedef SubDigraphBase Adaptor; typedef DigraphAdaptorBase Parent; protected: NodeFilterMap* _node_filter; ArcFilterMap* _arc_filter; - SubDigraphAdaptorBase() + SubDigraphBase() : Parent(), _node_filter(0), _arc_filter(0) { } void setNodeFilterMap(NodeFilterMap& node_filter) { @@ -441,42 +562,42 @@ typedef typename Parent::Node Node; typedef typename Parent::Arc Arc; - void first(Node& i) const { - Parent::first(i); - while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); + void first(Node& i) const { + Parent::first(i); + while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); } - void first(Arc& i) const { - Parent::first(i); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); + void first(Arc& i) const { + Parent::first(i); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); } - void firstIn(Arc& i, const Node& n) const { - Parent::firstIn(i, n); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); + void firstIn(Arc& i, const Node& n) const { + Parent::firstIn(i, n); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); } - void firstOut(Arc& i, const Node& n) const { - Parent::firstOut(i, n); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); + void firstOut(Arc& i, const Node& n) const { + Parent::firstOut(i, n); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); } - void next(Node& i) const { - Parent::next(i); - while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); + void next(Node& i) const { + Parent::next(i); + while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); } - void next(Arc& i) const { - Parent::next(i); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); + void next(Arc& i) const { + Parent::next(i); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); } - void nextIn(Arc& i) const { - Parent::nextIn(i); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); + void nextIn(Arc& i) const { + Parent::nextIn(i); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); } - void nextOut(Arc& i) const { - Parent::nextOut(i); - while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); + void nextOut(Arc& i) const { + Parent::nextOut(i); + while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); } void hide(const Node& n) const { _node_filter->set(n, false); } @@ -492,8 +613,8 @@ typedef False EdgeNumTag; typedef FindEdgeTagIndicator FindEdgeTag; - Arc findArc(const Node& source, const Node& target, - const Arc& prev = INVALID) { + Arc findArc(const Node& source, const Node& target, + const Arc& prev = INVALID) { if (!(*_node_filter)[source] || !(*_node_filter)[target]) { return INVALID; } @@ -505,52 +626,52 @@ } template - class NodeMap : public SubMapExtender > { + class NodeMap : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > MapParent; - - NodeMap(const Adaptor& adaptor) - : MapParent(adaptor) {} - NodeMap(const Adaptor& adaptor, const Value& value) - : MapParent(adaptor, value) {} - + + NodeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + NodeMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + private: NodeMap& operator=(const NodeMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template NodeMap& operator=(const CMap& cmap) { MapParent::operator=(cmap); - return *this; + return *this; } }; template - class ArcMap : public SubMapExtender > { + class ArcMap : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > MapParent; - - ArcMap(const Adaptor& adaptor) - : MapParent(adaptor) {} - ArcMap(const Adaptor& adaptor, const Value& value) - : MapParent(adaptor, value) {} - + + ArcMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + ArcMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + private: ArcMap& operator=(const ArcMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template ArcMap& operator=(const CMap& cmap) { MapParent::operator=(cmap); - return *this; + return *this; } }; @@ -558,75 +679,56 @@ /// \ingroup graph_adaptors /// - /// \brief A digraph adaptor for hiding nodes and arcs from a digraph. - /// - /// SubDigraphAdaptor shows the digraph with filtered node-set and - /// arc-set. If the \c checked parameter is true then it filters the arc-set - /// respect to the source and target. - /// - /// If the \c checked template parameter is false then the - /// node-iterator cares only the filter on the node-set, and the - /// arc-iterator cares only the filter on the arc-set. Therefore - /// the arc-map have to filter all arcs which's source or target is - /// filtered by the node-filter. - ///\code - /// typedef ListDigraph Digraph; - /// DIGRAPH_TYPEDEFS(Digraph); - /// Digraph g; - /// Node u=g.addNode(); //node of id 0 - /// Node v=g.addNode(); //node of id 1 - /// Arc a=g.addArc(u, v); //arc of id 0 - /// Arc f=g.addArc(v, u); //arc of id 1 - /// BoolNodeMap nm(g, true); - /// nm.set(u, false); - /// BoolArcMap am(g, true); - /// am.set(a, false); - /// typedef SubDigraphAdaptor SubDGA; - /// SubDGA ga(g, nm, am); - /// for (SubDGA::NodeIt n(ga); n!=INVALID; ++n) - /// std::cout << g.id(n) << std::endl; - /// for (SubDGA::ArcIt a(ga); a!=INVALID; ++a) - /// std::cout << g.id(a) << std::endl; - ///\endcode - /// The output of the above code is the following. - ///\code - /// 1 - /// 1 - ///\endcode - /// Note that \c n is of type \c SubDGA::NodeIt, but it can be converted to - /// \c Digraph::Node that is why \c g.id(n) can be applied. - /// - /// For other examples see also the documentation of - /// NodeSubDigraphAdaptor and ArcSubDigraphAdaptor. - template, - typename _ArcFilterMap = typename _Digraph::template ArcMap, - bool checked = true> - class SubDigraphAdaptor : - public DigraphAdaptorExtender< - SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, checked> > { + /// \brief An adaptor for hiding nodes and arcs in a digraph + /// + /// SubDigraph hides nodes and arcs in a digraph. A bool node map + /// and a bool arc map must be specified, which define the filters + /// for nodes and arcs. Just the nodes and arcs with true value are + /// shown in the subdigraph. The SubDigraph is conform to the \ref + /// concepts::Digraph "Digraph concept". If the \c _checked parameter + /// is true, then the arcs incident to filtered nodes are also + /// filtered out. + /// + /// \tparam _Digraph It must be conform to the \ref + /// concepts::Digraph "Digraph concept". The type can be specified + /// to const. + /// \tparam _NodeFilterMap A bool valued node map of the the adapted digraph. + /// \tparam _ArcFilterMap A bool valued arc map of the the adapted digraph. + /// \tparam _checked If the parameter is false then the arc filtering + /// is not checked with respect to node filter. Otherwise, each arc + /// is automatically filtered, which is incident to a filtered node. + /// + /// \see FilterNodes + /// \see FilterArcs + template, + typename _ArcFilterMap = typename _Digraph::template ArcMap, + bool _checked = true> + class SubDigraph + : public DigraphAdaptorExtender< + SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, _checked> > { public: typedef _Digraph Digraph; typedef _NodeFilterMap NodeFilterMap; typedef _ArcFilterMap ArcFilterMap; typedef DigraphAdaptorExtender< - SubDigraphAdaptorBase > + SubDigraphBase > Parent; typedef typename Parent::Node Node; typedef typename Parent::Arc Arc; protected: - SubDigraphAdaptor() { } + SubDigraph() { } public: /// \brief Constructor /// - /// Creates a sub-digraph-adaptor for the given digraph with + /// Creates a subdigraph for the given digraph with /// given node and arc map filters. - SubDigraphAdaptor(Digraph& digraph, NodeFilterMap& node_filter, - ArcFilterMap& arc_filter) { + SubDigraph(Digraph& digraph, NodeFilterMap& node_filter, + ArcFilterMap& arc_filter) { setDigraph(digraph); setNodeFilterMap(node_filter); setArcFilterMap(arc_filter); @@ -634,29 +736,29 @@ /// \brief Hides the node of the graph /// - /// This function hides \c n in the digraph, i.e. the iteration - /// jumps over it. This is done by simply setting the value of \c n + /// This function hides \c n in the digraph, i.e. the iteration + /// jumps over it. This is done by simply setting the value of \c n /// to be false in the corresponding node-map. void hide(const Node& n) const { Parent::hide(n); } /// \brief Hides the arc of the graph /// - /// This function hides \c a in the digraph, i.e. the iteration + /// This function hides \c a in the digraph, i.e. the iteration /// jumps over it. This is done by simply setting the value of \c a /// to be false in the corresponding arc-map. void hide(const Arc& a) const { Parent::hide(a); } /// \brief Unhides the node of the graph /// - /// The value of \c n is set to be true in the node-map which stores - /// hide information. If \c n was hidden previuosly, then it is shown + /// The value of \c n is set to be true in the node-map which stores + /// hide information. If \c n was hidden previuosly, then it is shown /// again void unHide(const Node& n) const { Parent::unHide(n); } /// \brief Unhides the arc of the graph /// - /// The value of \c a is set to be true in the arc-map which stores - /// hide information. If \c a was hidden previuosly, then it is shown + /// The value of \c a is set to be true in the arc-map which stores + /// hide information. If \c a was hidden previuosly, then it is shown /// again void unHide(const Arc& a) const { Parent::unHide(a); } @@ -674,274 +776,753 @@ }; - /// \brief Just gives back a sub-digraph-adaptor + /// \brief Just gives back a subdigraph /// - /// Just gives back a sub-digraph-adaptor + /// Just gives back a subdigraph template - SubDigraphAdaptor - subDigraphAdaptor(const Digraph& digraph, - NodeFilterMap& nfm, ArcFilterMap& afm) { - return SubDigraphAdaptor + SubDigraph + subDigraph(const Digraph& digraph, NodeFilterMap& nfm, ArcFilterMap& afm) { + return SubDigraph (digraph, nfm, afm); } template - SubDigraphAdaptor - subDigraphAdaptor(const Digraph& digraph, - NodeFilterMap& nfm, ArcFilterMap& afm) { - return SubDigraphAdaptor + SubDigraph + subDigraph(const Digraph& digraph, + const NodeFilterMap& nfm, ArcFilterMap& afm) { + return SubDigraph (digraph, nfm, afm); } template - SubDigraphAdaptor - subDigraphAdaptor(const Digraph& digraph, - NodeFilterMap& nfm, ArcFilterMap& afm) { - return SubDigraphAdaptor + SubDigraph + subDigraph(const Digraph& digraph, + NodeFilterMap& nfm, const ArcFilterMap& afm) { + return SubDigraph (digraph, nfm, afm); } template - SubDigraphAdaptor - subDigraphAdaptor(const Digraph& digraph, - NodeFilterMap& nfm, ArcFilterMap& afm) { - return SubDigraphAdaptor + subDigraph(const Digraph& digraph, + const NodeFilterMap& nfm, const ArcFilterMap& afm) { + return SubDigraph(digraph, nfm, afm); - } - - ///\ingroup graph_adaptors + template + class SubGraphBase : public GraphAdaptorBase<_Graph> { + public: + typedef _Graph Graph; + typedef SubGraphBase Adaptor; + typedef GraphAdaptorBase<_Graph> Parent; + protected: + + NodeFilterMap* _node_filter_map; + EdgeFilterMap* _edge_filter_map; + + SubGraphBase() + : Parent(), _node_filter_map(0), _edge_filter_map(0) { } + + void setNodeFilterMap(NodeFilterMap& node_filter_map) { + _node_filter_map=&node_filter_map; + } + void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { + _edge_filter_map=&edge_filter_map; + } + + public: + + typedef typename Parent::Node Node; + typedef typename Parent::Arc Arc; + typedef typename Parent::Edge Edge; + + void first(Node& i) const { + Parent::first(i); + while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); + } + + void first(Arc& i) const { + Parent::first(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::source(i)] + || !(*_node_filter_map)[Parent::target(i)])) + Parent::next(i); + } + + void first(Edge& i) const { + Parent::first(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::u(i)] + || !(*_node_filter_map)[Parent::v(i)])) + Parent::next(i); + } + + void firstIn(Arc& i, const Node& n) const { + Parent::firstIn(i, n); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::source(i)])) + Parent::nextIn(i); + } + + void firstOut(Arc& i, const Node& n) const { + Parent::firstOut(i, n); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::target(i)])) + Parent::nextOut(i); + } + + void firstInc(Edge& i, bool& d, const Node& n) const { + Parent::firstInc(i, d, n); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::u(i)] + || !(*_node_filter_map)[Parent::v(i)])) + Parent::nextInc(i, d); + } + + void next(Node& i) const { + Parent::next(i); + while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); + } + + void next(Arc& i) const { + Parent::next(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::source(i)] + || !(*_node_filter_map)[Parent::target(i)])) + Parent::next(i); + } + + void next(Edge& i) const { + Parent::next(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::u(i)] + || !(*_node_filter_map)[Parent::v(i)])) + Parent::next(i); + } + + void nextIn(Arc& i) const { + Parent::nextIn(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::source(i)])) + Parent::nextIn(i); + } + + void nextOut(Arc& i) const { + Parent::nextOut(i); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::target(i)])) + Parent::nextOut(i); + } + + void nextInc(Edge& i, bool& d) const { + Parent::nextInc(i, d); + while (i!=INVALID && (!(*_edge_filter_map)[i] + || !(*_node_filter_map)[Parent::u(i)] + || !(*_node_filter_map)[Parent::v(i)])) + Parent::nextInc(i, d); + } + + void hide(const Node& n) const { _node_filter_map->set(n, false); } + void hide(const Edge& e) const { _edge_filter_map->set(e, false); } + + void unHide(const Node& n) const { _node_filter_map->set(n, true); } + void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } + + bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } + bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } + + typedef False NodeNumTag; + typedef False EdgeNumTag; + + typedef FindEdgeTagIndicator FindEdgeTag; + Arc findArc(const Node& u, const Node& v, + const Arc& prev = INVALID) { + if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { + return INVALID; + } + Arc arc = Parent::findArc(u, v, prev); + while (arc != INVALID && !(*_edge_filter_map)[arc]) { + arc = Parent::findArc(u, v, arc); + } + return arc; + } + Edge findEdge(const Node& u, const Node& v, + const Edge& prev = INVALID) { + if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { + return INVALID; + } + Edge edge = Parent::findEdge(u, v, prev); + while (edge != INVALID && !(*_edge_filter_map)[edge]) { + edge = Parent::findEdge(u, v, edge); + } + return edge; + } + + template + class NodeMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + NodeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + NodeMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + + private: + NodeMap& operator=(const NodeMap& cmap) { + return operator=(cmap); + } + + template + NodeMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + template + class ArcMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + ArcMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + ArcMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + + private: + ArcMap& operator=(const ArcMap& cmap) { + return operator=(cmap); + } + + template + ArcMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + template + class EdgeMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + EdgeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + + EdgeMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + + private: + EdgeMap& operator=(const EdgeMap& cmap) { + return operator=(cmap); + } + + template + EdgeMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + }; + + template + class SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, false> + : public GraphAdaptorBase<_Graph> { + public: + typedef _Graph Graph; + typedef SubGraphBase Adaptor; + typedef GraphAdaptorBase<_Graph> Parent; + protected: + NodeFilterMap* _node_filter_map; + EdgeFilterMap* _edge_filter_map; + SubGraphBase() : Parent(), + _node_filter_map(0), _edge_filter_map(0) { } + + void setNodeFilterMap(NodeFilterMap& node_filter_map) { + _node_filter_map=&node_filter_map; + } + void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { + _edge_filter_map=&edge_filter_map; + } + + public: + + typedef typename Parent::Node Node; + typedef typename Parent::Arc Arc; + typedef typename Parent::Edge Edge; + + void first(Node& i) const { + Parent::first(i); + while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); + } + + void first(Arc& i) const { + Parent::first(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); + } + + void first(Edge& i) const { + Parent::first(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); + } + + void firstIn(Arc& i, const Node& n) const { + Parent::firstIn(i, n); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); + } + + void firstOut(Arc& i, const Node& n) const { + Parent::firstOut(i, n); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); + } + + void firstInc(Edge& i, bool& d, const Node& n) const { + Parent::firstInc(i, d, n); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); + } + + void next(Node& i) const { + Parent::next(i); + while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); + } + void next(Arc& i) const { + Parent::next(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); + } + void next(Edge& i) const { + Parent::next(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); + } + void nextIn(Arc& i) const { + Parent::nextIn(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); + } + + void nextOut(Arc& i) const { + Parent::nextOut(i); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); + } + void nextInc(Edge& i, bool& d) const { + Parent::nextInc(i, d); + while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); + } + + void hide(const Node& n) const { _node_filter_map->set(n, false); } + void hide(const Edge& e) const { _edge_filter_map->set(e, false); } + + void unHide(const Node& n) const { _node_filter_map->set(n, true); } + void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } + + bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } + bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } + + typedef False NodeNumTag; + typedef False EdgeNumTag; + + typedef FindEdgeTagIndicator FindEdgeTag; + Arc findArc(const Node& u, const Node& v, + const Arc& prev = INVALID) { + Arc arc = Parent::findArc(u, v, prev); + while (arc != INVALID && !(*_edge_filter_map)[arc]) { + arc = Parent::findArc(u, v, arc); + } + return arc; + } + Edge findEdge(const Node& u, const Node& v, + const Edge& prev = INVALID) { + Edge edge = Parent::findEdge(u, v, prev); + while (edge != INVALID && !(*_edge_filter_map)[edge]) { + edge = Parent::findEdge(u, v, edge); + } + return edge; + } + + template + class NodeMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + NodeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + NodeMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + + private: + NodeMap& operator=(const NodeMap& cmap) { + return operator=(cmap); + } + + template + NodeMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + template + class ArcMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + ArcMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + ArcMap(const Adaptor& adaptor, const Value& value) + : MapParent(adaptor, value) {} + + private: + ArcMap& operator=(const ArcMap& cmap) { + return operator=(cmap); + } + + template + ArcMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + template + class EdgeMap : public SubMapExtender > { + public: + typedef _Value Value; + typedef SubMapExtender > MapParent; + + EdgeMap(const Adaptor& adaptor) + : MapParent(adaptor) {} + + EdgeMap(const Adaptor& adaptor, const _Value& value) + : MapParent(adaptor, value) {} + + private: + EdgeMap& operator=(const EdgeMap& cmap) { + return operator=(cmap); + } + + template + EdgeMap& operator=(const CMap& cmap) { + MapParent::operator=(cmap); + return *this; + } + }; + + }; + + /// \ingroup graph_adaptors /// - ///\brief An adaptor for hiding nodes from a digraph. + /// \brief A graph adaptor for hiding nodes and edges in an + /// undirected graph. /// - ///An adaptor for hiding nodes from a digraph. This adaptor - ///specializes SubDigraphAdaptor in the way that only the node-set - ///can be filtered. In usual case the checked parameter is true, we - ///get the induced subgraph. But if the checked parameter is false - ///then we can filter only isolated nodes. - template, - bool checked = true> - class NodeSubDigraphAdaptor : - public SubDigraphAdaptor<_Digraph, _NodeFilterMap, - ConstMap, checked> { + /// SubGraph hides nodes and edges in a graph. A bool node map and a + /// bool edge map must be specified, which define the filters for + /// nodes and edges. Just the nodes and edges with true value are + /// shown in the subgraph. The SubGraph is conform to the \ref + /// concepts::Graph "Graph concept". If the \c _checked parameter is + /// true, then the edges incident to filtered nodes are also + /// filtered out. + /// + /// \tparam _Graph It must be conform to the \ref + /// concepts::Graph "Graph concept". The type can be specified + /// to const. + /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. + /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted graph. + /// \tparam _checked If the parameter is false then the edge filtering + /// is not checked with respect to node filter. Otherwise, each edge + /// is automatically filtered, which is incident to a filtered node. + /// + /// \see FilterNodes + /// \see FilterEdges + template + class SubGraph + : public GraphAdaptorExtender< + SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, _checked> > { public: - - typedef _Digraph Digraph; - typedef _NodeFilterMap NodeFilterMap; - - typedef SubDigraphAdaptor, checked> - Parent; + typedef _Graph Graph; + typedef GraphAdaptorExtender< + SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent; typedef typename Parent::Node Node; + typedef typename Parent::Edge Edge; protected: - ConstMap const_true_map; - - NodeSubDigraphAdaptor() : const_true_map(true) { - Parent::setArcFilterMap(const_true_map); - } - + SubGraph() { } public: /// \brief Constructor /// - /// Creates a node-sub-digraph-adaptor for the given digraph with - /// given node map filter. - NodeSubDigraphAdaptor(Digraph& _digraph, NodeFilterMap& node_filter) : - Parent(), const_true_map(true) { - Parent::setDigraph(_digraph); - Parent::setNodeFilterMap(node_filter); - Parent::setArcFilterMap(const_true_map); + /// Creates a subgraph for the given graph with given node and + /// edge map filters. + SubGraph(Graph& _graph, NodeFilterMap& node_filter_map, + EdgeFilterMap& edge_filter_map) { + setGraph(_graph); + setNodeFilterMap(node_filter_map); + setEdgeFilterMap(edge_filter_map); } /// \brief Hides the node of the graph /// - /// This function hides \c n in the digraph, i.e. the iteration - /// jumps over it. This is done by simply setting the value of \c n + /// This function hides \c n in the graph, i.e. the iteration + /// jumps over it. This is done by simply setting the value of \c n /// to be false in the corresponding node-map. void hide(const Node& n) const { Parent::hide(n); } + /// \brief Hides the edge of the graph + /// + /// This function hides \c e in the graph, i.e. the iteration + /// jumps over it. This is done by simply setting the value of \c e + /// to be false in the corresponding edge-map. + void hide(const Edge& e) const { Parent::hide(e); } + /// \brief Unhides the node of the graph /// - /// The value of \c n is set to be true in the node-map which stores - /// hide information. If \c n was hidden previuosly, then it is shown + /// The value of \c n is set to be true in the node-map which stores + /// hide information. If \c n was hidden previuosly, then it is shown /// again void unHide(const Node& n) const { Parent::unHide(n); } + /// \brief Unhides the edge of the graph + /// + /// The value of \c e is set to be true in the edge-map which stores + /// hide information. If \c e was hidden previuosly, then it is shown + /// again + void unHide(const Edge& e) const { Parent::unHide(e); } + /// \brief Returns true if \c n is hidden. /// /// Returns true if \c n is hidden. /// bool hidden(const Node& n) const { return Parent::hidden(n); } + /// \brief Returns true if \c e is hidden. + /// + /// Returns true if \c e is hidden. + /// + bool hidden(const Edge& e) const { return Parent::hidden(e); } }; - - /// \brief Just gives back a node-sub-digraph adaptor + /// \brief Just gives back a subgraph /// - /// Just gives back a node-sub-digraph adaptor + /// Just gives back a subgraph + template + SubGraph + subGraph(const Graph& graph, NodeFilterMap& nfm, ArcFilterMap& efm) { + return SubGraph(graph, nfm, efm); + } + + template + SubGraph + subGraph(const Graph& graph, + const NodeFilterMap& nfm, ArcFilterMap& efm) { + return SubGraph + (graph, nfm, efm); + } + + template + SubGraph + subGraph(const Graph& graph, + NodeFilterMap& nfm, const ArcFilterMap& efm) { + return SubGraph + (graph, nfm, efm); + } + + template + SubGraph + subGraph(const Graph& graph, + const NodeFilterMap& nfm, const ArcFilterMap& efm) { + return SubGraph + (graph, nfm, efm); + } + + /// \ingroup graph_adaptors + /// + /// \brief An adaptor for hiding nodes from a digraph or a graph. + /// + /// FilterNodes adaptor hides nodes in a graph or a digraph. A bool + /// node map must be specified, which defines the filters for + /// nodes. Just the unfiltered nodes and the arcs or edges incident + /// to unfiltered nodes are shown in the subdigraph or subgraph. The + /// FilterNodes is conform to the \ref concepts::Digraph + /// "Digraph concept" or \ref concepts::Graph "Graph concept" depending + /// on the \c _Digraph template parameter. If the \c _checked + /// parameter is true, then the arc or edges incident to filtered nodes + /// are also filtered out. + /// + /// \tparam _Digraph It must be conform to the \ref + /// concepts::Digraph "Digraph concept" or \ref concepts::Graph + /// "Graph concept". The type can be specified to be const. + /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. + /// \tparam _checked If the parameter is false then the arc or edge + /// filtering is not checked with respect to node filter. In this + /// case just isolated nodes can be filtered out from the + /// graph. +#ifdef DOXYGEN + template, + bool _checked = true> +#else + template, + bool _checked = true, + typename Enable = void> +#endif + class FilterNodes + : public SubDigraph<_Digraph, _NodeFilterMap, + ConstMap, _checked> { + public: + + typedef _Digraph Digraph; + typedef _NodeFilterMap NodeFilterMap; + + typedef SubDigraph, _checked> + Parent; + + typedef typename Parent::Node Node; + + protected: + ConstMap const_true_map; + + FilterNodes() : const_true_map(true) { + Parent::setArcFilterMap(const_true_map); + } + + public: + + /// \brief Constructor + /// + /// Creates an adaptor for the given digraph or graph with + /// given node filter map. + FilterNodes(Digraph& _digraph, NodeFilterMap& node_filter) : + Parent(), const_true_map(true) { + Parent::setDigraph(_digraph); + Parent::setNodeFilterMap(node_filter); + Parent::setArcFilterMap(const_true_map); + } + + /// \brief Hides the node of the graph + /// + /// This function hides \c n in the digraph or graph, i.e. the iteration + /// jumps over it. This is done by simply setting the value of \c n + /// to be false in the corresponding node map. + void hide(const Node& n) const { Parent::hide(n); } + + /// \brief Unhides the node of the graph + /// + /// The value of \c n is set to be true in the node-map which stores + /// hide information. If \c n was hidden previuosly, then it is shown + /// again + void unHide(const Node& n) const { Parent::unHide(n); } + + /// \brief Returns true if \c n is hidden. + /// + /// Returns true if \c n is hidden. + /// + bool hidden(const Node& n) const { return Parent::hidden(n); } + + }; + + template + class FilterNodes<_Graph, _NodeFilterMap, _checked, + typename enable_if >::type> + : public SubGraph<_Graph, _NodeFilterMap, + ConstMap, _checked> { + public: + typedef _Graph Graph; + typedef _NodeFilterMap NodeFilterMap; + typedef SubGraph > Parent; + + typedef typename Parent::Node Node; + protected: + ConstMap const_true_map; + + FilterNodes() : const_true_map(true) { + Parent::setEdgeFilterMap(const_true_map); + } + + public: + + FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) : + Parent(), const_true_map(true) { + Parent::setGraph(_graph); + Parent::setNodeFilterMap(node_filter_map); + Parent::setEdgeFilterMap(const_true_map); + } + + void hide(const Node& n) const { Parent::hide(n); } + void unHide(const Node& n) const { Parent::unHide(n); } + bool hidden(const Node& n) const { return Parent::hidden(n); } + + }; + + + /// \brief Just gives back a FilterNodes adaptor + /// + /// Just gives back a FilterNodes adaptor template - NodeSubDigraphAdaptor - nodeSubDigraphAdaptor(const Digraph& digraph, NodeFilterMap& nfm) { - return NodeSubDigraphAdaptor(digraph, nfm); + FilterNodes + filterNodes(const Digraph& digraph, NodeFilterMap& nfm) { + return FilterNodes(digraph, nfm); } template - NodeSubDigraphAdaptor - nodeSubDigraphAdaptor(const Digraph& digraph, const NodeFilterMap& nfm) { - return NodeSubDigraphAdaptor - (digraph, nfm); + FilterNodes + filterNodes(const Digraph& digraph, const NodeFilterMap& nfm) { + return FilterNodes(digraph, nfm); } - ///\ingroup graph_adaptors + /// \ingroup graph_adaptors /// - ///\brief An adaptor for hiding arcs from a digraph. + /// \brief An adaptor for hiding arcs from a digraph. /// - ///An adaptor for hiding arcs from a digraph. This adaptor - ///specializes SubDigraphAdaptor in the way that only the arc-set - ///can be filtered. The usefulness of this adaptor is demonstrated - ///in the problem of searching a maximum number of arc-disjoint - ///shortest paths between two nodes \c s and \c t. Shortest here - ///means being shortest with respect to non-negative - ///arc-lengths. Note that the comprehension of the presented - ///solution need's some elementary knowledge from combinatorial - ///optimization. + /// FilterArcs adaptor hides arcs in a digraph. A bool arc map must + /// be specified, which defines the filters for arcs. Just the + /// unfiltered arcs are shown in the subdigraph. The FilterArcs is + /// conform to the \ref concepts::Digraph "Digraph concept". /// - ///If a single shortest path is to be searched between \c s and \c - ///t, then this can be done easily by applying the Dijkstra - ///algorithm. What happens, if a maximum number of arc-disjoint - ///shortest paths is to be computed. It can be proved that an arc - ///can be in a shortest path if and only if it is tight with respect - ///to the potential function computed by Dijkstra. Moreover, any - ///path containing only such arcs is a shortest one. Thus we have - ///to compute a maximum number of arc-disjoint paths between \c s - ///and \c t in the digraph which has arc-set all the tight arcs. The - ///computation will be demonstrated on the following digraph, which - ///is read from the dimacs file \c sub_digraph_adaptor_demo.dim. - ///The full source code is available in \ref - ///sub_digraph_adaptor_demo.cc. If you are interested in more demo - ///programs, you can use \ref dim_to_dot.cc to generate .dot files - ///from dimacs files. The .dot file of the following figure was - ///generated by the demo program \ref dim_to_dot.cc. - /// - ///\dot - ///digraph lemon_dot_example { - ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; - ///n0 [ label="0 (s)" ]; - ///n1 [ label="1" ]; - ///n2 [ label="2" ]; - ///n3 [ label="3" ]; - ///n4 [ label="4" ]; - ///n5 [ label="5" ]; - ///n6 [ label="6 (t)" ]; - ///arc [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; - ///n5 -> n6 [ label="9, length:4" ]; - ///n4 -> n6 [ label="8, length:2" ]; - ///n3 -> n5 [ label="7, length:1" ]; - ///n2 -> n5 [ label="6, length:3" ]; - ///n2 -> n6 [ label="5, length:5" ]; - ///n2 -> n4 [ label="4, length:2" ]; - ///n1 -> n4 [ label="3, length:3" ]; - ///n0 -> n3 [ label="2, length:1" ]; - ///n0 -> n2 [ label="1, length:2" ]; - ///n0 -> n1 [ label="0, length:3" ]; - ///} - ///\enddot - /// - ///\code - ///Digraph g; - ///Node s, t; - ///LengthMap length(g); - /// - ///readDimacs(std::cin, g, length, s, t); - /// - ///cout << "arcs with lengths (of form id, source--length->target): " << endl; - ///for(ArcIt e(g); e!=INVALID; ++e) - /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--" - /// << length[e] << "->" << g.id(g.target(e)) << endl; - /// - ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl; - ///\endcode - ///Next, the potential function is computed with Dijkstra. - ///\code - ///typedef Dijkstra Dijkstra; - ///Dijkstra dijkstra(g, length); - ///dijkstra.run(s); - ///\endcode - ///Next, we consrtruct a map which filters the arc-set to the tight arcs. - ///\code - ///typedef TightArcFilterMap - /// TightArcFilter; - ///TightArcFilter tight_arc_filter(g, dijkstra.distMap(), length); - /// - ///typedef ArcSubDigraphAdaptor SubGA; - ///SubGA ga(g, tight_arc_filter); - ///\endcode - ///Then, the maximum nimber of arc-disjoint \c s-\c t paths are computed - ///with a max flow algorithm Preflow. - ///\code - ///ConstMap const_1_map(1); - ///Digraph::ArcMap flow(g, 0); - /// - ///Preflow, Digraph::ArcMap > - /// preflow(ga, const_1_map, s, t); - ///preflow.run(); - ///\endcode - ///Last, the output is: - ///\code - ///cout << "maximum number of arc-disjoint shortest path: " - /// << preflow.flowValue() << endl; - ///cout << "arcs of the maximum number of arc-disjoint shortest s-t paths: " - /// << endl; - ///for(ArcIt e(g); e!=INVALID; ++e) - /// if (preflow.flow(e)) - /// cout << " " << g.id(g.source(e)) << "--" - /// << length[e] << "->" << g.id(g.target(e)) << endl; - ///\endcode - ///The program has the following (expected :-)) output: - ///\code - ///arcs with lengths (of form id, source--length->target): - /// 9, 5--4->6 - /// 8, 4--2->6 - /// 7, 3--1->5 - /// 6, 2--3->5 - /// 5, 2--5->6 - /// 4, 2--2->4 - /// 3, 1--3->4 - /// 2, 0--1->3 - /// 1, 0--2->2 - /// 0, 0--3->1 - ///s: 0 t: 6 - ///maximum number of arc-disjoint shortest path: 2 - ///arcs of the maximum number of arc-disjoint shortest s-t paths: - /// 9, 5--4->6 - /// 8, 4--2->6 - /// 7, 3--1->5 - /// 4, 2--2->4 - /// 2, 0--1->3 - /// 1, 0--2->2 - ///\endcode + /// \tparam _Digraph It must be conform to the \ref concepts::Digraph + /// "Digraph concept". The type can be specified to be const. + /// \tparam _ArcFilterMap A bool valued arc map of the the adapted + /// graph. template - class ArcSubDigraphAdaptor : - public SubDigraphAdaptor<_Digraph, ConstMap, - _ArcFilterMap, false> { + class FilterArcs : + public SubDigraph<_Digraph, ConstMap, + _ArcFilterMap, false> { public: typedef _Digraph Digraph; typedef _ArcFilterMap ArcFilterMap; - typedef SubDigraphAdaptor, - ArcFilterMap, false> Parent; + typedef SubDigraph, + ArcFilterMap, false> Parent; typedef typename Parent::Arc Arc; protected: ConstMap const_true_map; - ArcSubDigraphAdaptor() : const_true_map(true) { + FilterArcs() : const_true_map(true) { Parent::setNodeFilterMap(const_true_map); } @@ -949,10 +1530,10 @@ /// \brief Constructor /// - /// Creates a arc-sub-digraph-adaptor for the given digraph with + /// Creates a FilterArcs adaptor for the given graph with /// given arc map filter. - ArcSubDigraphAdaptor(Digraph& digraph, ArcFilterMap& arc_filter) - : Parent(), const_true_map(true) { + FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter) + : Parent(), const_true_map(true) { Parent::setDigraph(digraph); Parent::setNodeFilterMap(const_true_map); Parent::setArcFilterMap(arc_filter); @@ -960,15 +1541,15 @@ /// \brief Hides the arc of the graph /// - /// This function hides \c a in the digraph, i.e. the iteration + /// This function hides \c a in the graph, i.e. the iteration /// jumps over it. This is done by simply setting the value of \c a - /// to be false in the corresponding arc-map. + /// to be false in the corresponding arc map. void hide(const Arc& a) const { Parent::hide(a); } /// \brief Unhides the arc of the graph /// - /// The value of \c a is set to be true in the arc-map which stores - /// hide information. If \c a was hidden previuosly, then it is shown + /// The value of \c a is set to be true in the arc-map which stores + /// hide information. If \c a was hidden previuosly, then it is shown /// again void unHide(const Arc& a) const { Parent::unHide(a); } @@ -980,27 +1561,106 @@ }; - /// \brief Just gives back an arc-sub-digraph adaptor + /// \brief Just gives back an FilterArcs adaptor /// - /// Just gives back an arc-sub-digraph adaptor + /// Just gives back an FilterArcs adaptor template - ArcSubDigraphAdaptor - arcSubDigraphAdaptor(const Digraph& digraph, ArcFilterMap& afm) { - return ArcSubDigraphAdaptor(digraph, afm); + FilterArcs + filterArcs(const Digraph& digraph, ArcFilterMap& afm) { + return FilterArcs(digraph, afm); } template - ArcSubDigraphAdaptor - arcSubDigraphAdaptor(const Digraph& digraph, const ArcFilterMap& afm) { - return ArcSubDigraphAdaptor - (digraph, afm); + FilterArcs + filterArcs(const Digraph& digraph, const ArcFilterMap& afm) { + return FilterArcs(digraph, afm); } + /// \ingroup graph_adaptors + /// + /// \brief An adaptor for hiding edges from a graph. + /// + /// FilterEdges adaptor hides edges in a digraph. A bool edge map must + /// be specified, which defines the filters for edges. Just the + /// unfiltered edges are shown in the subdigraph. The FilterEdges is + /// conform to the \ref concepts::Graph "Graph concept". + /// + /// \tparam _Graph It must be conform to the \ref concepts::Graph + /// "Graph concept". The type can be specified to be const. + /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted + /// graph. + template + class FilterEdges : + public SubGraph<_Graph, ConstMap, + _EdgeFilterMap, false> { + public: + typedef _Graph Graph; + typedef _EdgeFilterMap EdgeFilterMap; + typedef SubGraph, + EdgeFilterMap, false> Parent; + typedef typename Parent::Edge Edge; + protected: + ConstMap const_true_map; + + FilterEdges() : const_true_map(true) { + Parent::setNodeFilterMap(const_true_map); + } + + public: + + /// \brief Constructor + /// + /// Creates a FilterEdges adaptor for the given graph with + /// given edge map filters. + FilterEdges(Graph& _graph, EdgeFilterMap& edge_filter_map) : + Parent(), const_true_map(true) { + Parent::setGraph(_graph); + Parent::setNodeFilterMap(const_true_map); + Parent::setEdgeFilterMap(edge_filter_map); + } + + /// \brief Hides the edge of the graph + /// + /// This function hides \c e in the graph, i.e. the iteration + /// jumps over it. This is done by simply setting the value of \c e + /// to be false in the corresponding edge-map. + void hide(const Edge& e) const { Parent::hide(e); } + + /// \brief Unhides the edge of the graph + /// + /// The value of \c e is set to be true in the edge-map which stores + /// hide information. If \c e was hidden previuosly, then it is shown + /// again + void unHide(const Edge& e) const { Parent::unHide(e); } + + /// \brief Returns true if \c e is hidden. + /// + /// Returns true if \c e is hidden. + /// + bool hidden(const Edge& e) const { return Parent::hidden(e); } + + }; + + /// \brief Just gives back a FilterEdges adaptor + /// + /// Just gives back a FilterEdges adaptor + template + FilterEdges + filterEdges(const Graph& graph, EdgeFilterMap& efm) { + return FilterEdges(graph, efm); + } + + template + FilterEdges + filterEdges(const Graph& graph, const EdgeFilterMap& efm) { + return FilterEdges(graph, efm); + } + template - class UndirDigraphAdaptorBase { + class UndirectorBase { public: typedef _Digraph Digraph; - typedef UndirDigraphAdaptorBase Adaptor; + typedef UndirectorBase Adaptor; typedef True UndirectedTag; @@ -1008,7 +1668,7 @@ typedef typename Digraph::Node Node; class Arc : public Edge { - friend class UndirDigraphAdaptorBase; + friend class UndirectorBase; protected: bool _forward; @@ -1021,17 +1681,17 @@ Arc(Invalid) : Edge(INVALID), _forward(true) {} bool operator==(const Arc &other) const { - return _forward == other._forward && - static_cast(*this) == static_cast(other); + return _forward == other._forward && + static_cast(*this) == static_cast(other); } bool operator!=(const Arc &other) const { - return _forward != other._forward || - static_cast(*this) != static_cast(other); + return _forward != other._forward || + static_cast(*this) != static_cast(other); } bool operator<(const Arc &other) const { - return _forward < other._forward || - (_forward == other._forward && - static_cast(*this) < static_cast(other)); + return _forward < other._forward || + (_forward == other._forward && + static_cast(*this) < static_cast(other)); } }; @@ -1052,10 +1712,10 @@ void next(Arc& a) const { if (a._forward) { - a._forward = false; + a._forward = false; } else { - _digraph->next(a); - a._forward = true; + _digraph->next(a); + a._forward = true; } } @@ -1070,46 +1730,46 @@ void firstOut(Arc& a, const Node& n) const { _digraph->firstIn(a, n); if( static_cast(a) != INVALID ) { - a._forward = false; + a._forward = false; } else { - _digraph->firstOut(a, n); - a._forward = true; + _digraph->firstOut(a, n); + a._forward = true; } } void nextOut(Arc &a) const { if (!a._forward) { - Node n = _digraph->target(a); - _digraph->nextIn(a); - if (static_cast(a) == INVALID ) { - _digraph->firstOut(a, n); - a._forward = true; - } + Node n = _digraph->target(a); + _digraph->nextIn(a); + if (static_cast(a) == INVALID ) { + _digraph->firstOut(a, n); + a._forward = true; + } } else { - _digraph->nextOut(a); + _digraph->nextOut(a); } } void firstIn(Arc &a, const Node &n) const { _digraph->firstOut(a, n); if (static_cast(a) != INVALID ) { - a._forward = false; + a._forward = false; } else { - _digraph->firstIn(a, n); - a._forward = true; + _digraph->firstIn(a, n); + a._forward = true; } } void nextIn(Arc &a) const { if (!a._forward) { - Node n = _digraph->source(a); - _digraph->nextOut(a); - if( static_cast(a) == INVALID ) { - _digraph->firstIn(a, n); - a._forward = true; - } + Node n = _digraph->source(a); + _digraph->nextOut(a); + if( static_cast(a) == INVALID ) { + _digraph->firstIn(a, n); + a._forward = true; + } } else { - _digraph->nextIn(a); + _digraph->nextIn(a); } } @@ -1123,13 +1783,13 @@ void nextInc(Edge &e, bool &d) const { if (d) { - Node s = _digraph->source(e); - _digraph->nextOut(e); - if (e != INVALID) return; - d = false; - _digraph->firstIn(e, s); + Node s = _digraph->source(e); + _digraph->nextOut(e); + if (e != INVALID) return; + d = false; + _digraph->firstIn(e, s); } else { - _digraph->nextIn(e); + _digraph->nextIn(e); } } @@ -1175,13 +1835,13 @@ int maxEdgeId() const { return _digraph->maxArcId(); } Node addNode() { return _digraph->addNode(); } - Edge addEdge(const Node& u, const Node& v) { - return _digraph->addArc(u, v); + Edge addEdge(const Node& u, const Node& v) { + return _digraph->addArc(u, v); } void erase(const Node& i) { _digraph->erase(i); } void erase(const Edge& i) { _digraph->erase(i); } - + void clear() { _digraph->clear(); } typedef NodeNumTagIndicator NodeNumTag; @@ -1193,18 +1853,18 @@ typedef FindEdgeTagIndicator FindEdgeTag; Arc findArc(Node s, Node t, Arc p = INVALID) const { if (p == INVALID) { - Edge arc = _digraph->findArc(s, t); - if (arc != INVALID) return direct(arc, true); - arc = _digraph->findArc(t, s); - if (arc != INVALID) return direct(arc, false); + Edge arc = _digraph->findArc(s, t); + if (arc != INVALID) return direct(arc, true); + arc = _digraph->findArc(t, s); + if (arc != INVALID) return direct(arc, false); } else if (direction(p)) { - Edge arc = _digraph->findArc(s, t, p); - if (arc != INVALID) return direct(arc, true); - arc = _digraph->findArc(t, s); - if (arc != INVALID) return direct(arc, false); + Edge arc = _digraph->findArc(s, t, p); + if (arc != INVALID) return direct(arc, true); + arc = _digraph->findArc(t, s); + if (arc != INVALID) return direct(arc, false); } else { - Edge arc = _digraph->findArc(t, s, p); - if (arc != INVALID) return direct(arc, false); + Edge arc = _digraph->findArc(t, s, p); + if (arc != INVALID) return direct(arc, false); } return INVALID; } @@ -1220,10 +1880,10 @@ Edge arc = _digraph->findArc(s, t, p); if (arc != INVALID) return arc; arc = _digraph->findArc(t, s); - if (arc != INVALID) return arc; + if (arc != INVALID) return arc; } else { Edge arc = _digraph->findArc(t, s, p); - if (arc != INVALID) return arc; + if (arc != INVALID) return arc; } } else { return _digraph->findArc(s, t, p); @@ -1232,55 +1892,55 @@ } private: - + template class ArcMapBase { private: - + typedef typename Digraph::template ArcMap<_Value> MapImpl; - + public: typedef typename MapTraits::ReferenceMapTag ReferenceMapTag; typedef _Value Value; typedef Arc Key; - + ArcMapBase(const Adaptor& adaptor) : - _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} - - ArcMapBase(const Adaptor& adaptor, const Value& v) + _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} + + ArcMapBase(const Adaptor& adaptor, const Value& v) : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {} - - void set(const Arc& a, const Value& v) { - if (direction(a)) { - _forward.set(a, v); - } else { - _backward.set(a, v); - } - } - - typename MapTraits::ConstReturnValue - operator[](const Arc& a) const { - if (direction(a)) { - return _forward[a]; - } else { - return _backward[a]; + + void set(const Arc& a, const Value& v) { + if (direction(a)) { + _forward.set(a, v); + } else { + _backward.set(a, v); } } - typename MapTraits::ReturnValue - operator[](const Arc& a) { - if (direction(a)) { - return _forward[a]; - } else { - return _backward[a]; + typename MapTraits::ConstReturnValue + operator[](const Arc& a) const { + if (direction(a)) { + return _forward[a]; + } else { + return _backward[a]; } } + typename MapTraits::ReturnValue + operator[](const Arc& a) { + if (direction(a)) { + return _forward[a]; + } else { + return _backward[a]; + } + } + protected: - MapImpl _forward, _backward; + MapImpl _forward, _backward; }; @@ -1293,11 +1953,11 @@ typedef _Value Value; typedef typename Digraph::template NodeMap Parent; - explicit NodeMap(const Adaptor& adaptor) - : Parent(*adaptor._digraph) {} + explicit NodeMap(const Adaptor& adaptor) + : Parent(*adaptor._digraph) {} NodeMap(const Adaptor& adaptor, const _Value& value) - : Parent(*adaptor._digraph, value) { } + : Parent(*adaptor._digraph, value) { } private: NodeMap& operator=(const NodeMap& cmap) { @@ -1309,47 +1969,47 @@ Parent::operator=(cmap); return *this; } - + }; template - class ArcMap - : public SubMapExtender > + class ArcMap + : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > Parent; - - ArcMap(const Adaptor& adaptor) - : Parent(adaptor) {} - - ArcMap(const Adaptor& adaptor, const Value& value) - : Parent(adaptor, value) {} - + + ArcMap(const Adaptor& adaptor) + : Parent(adaptor) {} + + ArcMap(const Adaptor& adaptor, const Value& value) + : Parent(adaptor, value) {} + private: ArcMap& operator=(const ArcMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template ArcMap& operator=(const CMap& cmap) { Parent::operator=(cmap); - return *this; + return *this; } }; - + template class EdgeMap : public Digraph::template ArcMap<_Value> { public: - + typedef _Value Value; typedef typename Digraph::template ArcMap Parent; - - explicit EdgeMap(const Adaptor& adaptor) - : Parent(*adaptor._digraph) {} + + explicit EdgeMap(const Adaptor& adaptor) + : Parent(*adaptor._digraph) {} EdgeMap(const Adaptor& adaptor, const Value& value) - : Parent(*adaptor._digraph, value) {} + : Parent(*adaptor._digraph, value) {} private: EdgeMap& operator=(const EdgeMap& cmap) { @@ -1365,89 +2025,57 @@ }; typedef typename ItemSetTraits::ItemNotifier NodeNotifier; - NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } + NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } protected: - UndirDigraphAdaptorBase() : _digraph(0) {} + UndirectorBase() : _digraph(0) {} Digraph* _digraph; void setDigraph(Digraph& digraph) { _digraph = &digraph; } - + }; - ///\ingroup graph_adaptors + /// \ingroup graph_adaptors /// - /// \brief A graph is made from a directed digraph by an adaptor + /// \brief Undirect the graph /// /// This adaptor makes an undirected graph from a directed - /// graph. All arc of the underlying digraph will be showed in the - /// adaptor as an edge. Let's see an informal example about using - /// this adaptor. + /// graph. All arcs of the underlying digraph will be showed in the + /// adaptor as an edge. The Orienter adaptor is conform to the \ref + /// concepts::Graph "Graph concept". /// - /// There is a network of the streets of a town. Of course there are - /// some one-way street in the town hence the network is a directed - /// one. There is a crazy driver who go oppositely in the one-way - /// street without moral sense. Of course he can pass this streets - /// slower than the regular way, in fact his speed is half of the - /// normal speed. How long should he drive to get from a source - /// point to the target? Let see the example code which calculate it: - /// - /// \todo BadCode, SimpleMap does no exists - ///\code - /// typedef UndirDigraphAdaptor Graph; - /// Graph graph(digraph); - /// - /// typedef SimpleMap FLengthMap; - /// FLengthMap flength(length); - /// - /// typedef ScaleMap RLengthMap; - /// RLengthMap rlength(length, 2.0); - /// - /// typedef Graph::CombinedArcMap ULengthMap; - /// ULengthMap ulength(flength, rlength); - /// - /// Dijkstra dijkstra(graph, ulength); - /// std::cout << "Driving time : " << dijkstra.run(src, trg) << std::endl; - ///\endcode - /// - /// The combined arc map makes the length map for the undirected - /// graph. It is created from a forward and reverse map. The forward - /// map is created from the original length map with a SimpleMap - /// adaptor which just makes a read-write map from the reference map - /// i.e. it forgets that it can be return reference to values. The - /// reverse map is just the scaled original map with the ScaleMap - /// adaptor. The combination solves that passing the reverse way - /// takes double time than the original. To get the driving time we - /// run the dijkstra algorithm on the graph. + /// \tparam _Digraph It must be conform to the \ref + /// concepts::Digraph "Digraph concept". The type can be specified + /// to const. template - class UndirDigraphAdaptor - : public GraphAdaptorExtender > { + class Undirector + : public GraphAdaptorExtender > { public: typedef _Digraph Digraph; - typedef GraphAdaptorExtender > Parent; + typedef GraphAdaptorExtender > Parent; protected: - UndirDigraphAdaptor() { } + Undirector() { } public: /// \brief Constructor /// - /// Constructor - UndirDigraphAdaptor(_Digraph& _digraph) { - setDigraph(_digraph); + /// Creates a undirected graph from the given digraph + Undirector(_Digraph& digraph) { + setDigraph(digraph); } /// \brief ArcMap combined from two original ArcMap /// /// This class adapts two original digraph ArcMap to - /// get an arc map on the adaptor. + /// get an arc map on the undirected graph. template class CombinedArcMap { public: - + typedef _ForwardMap ForwardMap; typedef _BackwardMap BackwardMap; @@ -1456,90 +2084,430 @@ typedef typename ForwardMap::Value Value; typedef typename Parent::Arc Key; - /// \brief Constructor + /// \brief Constructor /// - /// Constructor - CombinedArcMap() : _forward(0), _backward(0) {} - - /// \brief Constructor - /// - /// Constructor - CombinedArcMap(ForwardMap& forward, BackwardMap& backward) + /// Constructor + CombinedArcMap(ForwardMap& forward, BackwardMap& backward) : _forward(&forward), _backward(&backward) {} - + /// \brief Sets the value associated with a key. /// /// Sets the value associated with a key. - void set(const Key& e, const Value& a) { - if (Parent::direction(e)) { - _forward->set(e, a); - } else { - _backward->set(e, a); - } + void set(const Key& e, const Value& a) { + if (Parent::direction(e)) { + _forward->set(e, a); + } else { + _backward->set(e, a); + } } /// \brief Returns the value associated with a key. /// /// Returns the value associated with a key. - typename MapTraits::ConstReturnValue - operator[](const Key& e) const { - if (Parent::direction(e)) { - return (*_forward)[e]; - } else { - return (*_backward)[e]; + typename MapTraits::ConstReturnValue + operator[](const Key& e) const { + if (Parent::direction(e)) { + return (*_forward)[e]; + } else { + return (*_backward)[e]; } } /// \brief Returns the value associated with a key. /// /// Returns the value associated with a key. - typename MapTraits::ReturnValue - operator[](const Key& e) { - if (Parent::direction(e)) { - return (*_forward)[e]; - } else { - return (*_backward)[e]; + typename MapTraits::ReturnValue + operator[](const Key& e) { + if (Parent::direction(e)) { + return (*_forward)[e]; + } else { + return (*_backward)[e]; } } - /// \brief Sets the forward map - /// - /// Sets the forward map - void setForwardMap(ForwardMap& forward) { - _forward = &forward; + protected: + + ForwardMap* _forward; + BackwardMap* _backward; + + }; + + /// \brief Just gives back a combined arc map + /// + /// Just gives back a combined arc map + template + static CombinedArcMap + combinedArcMap(ForwardMap& forward, BackwardMap& backward) { + return CombinedArcMap(forward, backward); + } + + template + static CombinedArcMap + combinedArcMap(const ForwardMap& forward, BackwardMap& backward) { + return CombinedArcMap(forward, backward); + } + + template + static CombinedArcMap + combinedArcMap(ForwardMap& forward, const BackwardMap& backward) { + return CombinedArcMap(forward, backward); + } + + template + static CombinedArcMap + combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) { + return CombinedArcMap(forward, backward); + } + + }; + + /// \brief Just gives back an undirected view of the given digraph + /// + /// Just gives back an undirected view of the given digraph + template + Undirector + undirector(const Digraph& digraph) { + return Undirector(digraph); + } + + template + class OrienterBase { + public: + + typedef _Graph Graph; + typedef _DirectionMap DirectionMap; + + typedef typename Graph::Node Node; + typedef typename Graph::Edge Arc; + + void reverseArc(const Arc& arc) { + _direction->set(arc, !(*_direction)[arc]); + } + + void first(Node& i) const { _graph->first(i); } + void first(Arc& i) const { _graph->first(i); } + void firstIn(Arc& i, const Node& n) const { + bool d; + _graph->firstInc(i, d, n); + while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); + } + void firstOut(Arc& i, const Node& n ) const { + bool d; + _graph->firstInc(i, d, n); + while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); + } + + void next(Node& i) const { _graph->next(i); } + void next(Arc& i) const { _graph->next(i); } + void nextIn(Arc& i) const { + bool d = !(*_direction)[i]; + _graph->nextInc(i, d); + while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); + } + void nextOut(Arc& i) const { + bool d = (*_direction)[i]; + _graph->nextInc(i, d); + while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); + } + + Node source(const Arc& e) const { + return (*_direction)[e] ? _graph->u(e) : _graph->v(e); + } + Node target(const Arc& e) const { + return (*_direction)[e] ? _graph->v(e) : _graph->u(e); + } + + typedef NodeNumTagIndicator NodeNumTag; + int nodeNum() const { return _graph->nodeNum(); } + + typedef EdgeNumTagIndicator EdgeNumTag; + int arcNum() const { return _graph->edgeNum(); } + + typedef FindEdgeTagIndicator FindEdgeTag; + Arc findArc(const Node& u, const Node& v, + const Arc& prev = INVALID) { + Arc arc = prev; + bool d = arc == INVALID ? true : (*_direction)[arc]; + if (d) { + arc = _graph->findEdge(u, v, arc); + while (arc != INVALID && !(*_direction)[arc]) { + _graph->findEdge(u, v, arc); + } + if (arc != INVALID) return arc; } - - /// \brief Sets the backward map - /// - /// Sets the backward map - void setBackwardMap(BackwardMap& backward) { - _backward = &backward; + _graph->findEdge(v, u, arc); + while (arc != INVALID && (*_direction)[arc]) { + _graph->findEdge(u, v, arc); } - - protected: - - ForwardMap* _forward; - BackwardMap* _backward; + return arc; + } + + Node addNode() { + return Node(_graph->addNode()); + } + + Arc addArc(const Node& u, const Node& v) { + Arc arc = _graph->addArc(u, v); + _direction->set(arc, _graph->source(arc) == u); + return arc; + } + + void erase(const Node& i) { _graph->erase(i); } + void erase(const Arc& i) { _graph->erase(i); } + + void clear() { _graph->clear(); } + + int id(const Node& v) const { return _graph->id(v); } + int id(const Arc& e) const { return _graph->id(e); } + + Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } + Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } + + int maxNodeId() const { return _graph->maxNodeId(); } + int maxArcId() const { return _graph->maxEdgeId(); } + + typedef typename ItemSetTraits::ItemNotifier NodeNotifier; + NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } + + typedef typename ItemSetTraits::ItemNotifier ArcNotifier; + ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } + + template + class NodeMap : public _Graph::template NodeMap<_Value> { + public: + + typedef typename _Graph::template NodeMap<_Value> Parent; + + explicit NodeMap(const OrienterBase& adapter) + : Parent(*adapter._graph) {} + + NodeMap(const OrienterBase& adapter, const _Value& value) + : Parent(*adapter._graph, value) {} + + private: + NodeMap& operator=(const NodeMap& cmap) { + return operator=(cmap); + } + + template + NodeMap& operator=(const CMap& cmap) { + Parent::operator=(cmap); + return *this; + } }; + template + class ArcMap : public _Graph::template EdgeMap<_Value> { + public: + + typedef typename Graph::template EdgeMap<_Value> Parent; + + explicit ArcMap(const OrienterBase& adapter) + : Parent(*adapter._graph) { } + + ArcMap(const OrienterBase& adapter, const _Value& value) + : Parent(*adapter._graph, value) { } + + private: + ArcMap& operator=(const ArcMap& cmap) { + return operator=(cmap); + } + + template + ArcMap& operator=(const CMap& cmap) { + Parent::operator=(cmap); + return *this; + } + }; + + + + protected: + Graph* _graph; + DirectionMap* _direction; + + void setDirectionMap(DirectionMap& direction) { + _direction = &direction; + } + + void setGraph(Graph& graph) { + _graph = &graph; + } + }; - /// \brief Just gives back an undir digraph adaptor + /// \ingroup graph_adaptors /// - /// Just gives back an undir digraph adaptor - template - UndirDigraphAdaptor - undirDigraphAdaptor(const Digraph& digraph) { - return UndirDigraphAdaptor(digraph); + /// \brief Orients the edges of the graph to get a digraph + /// + /// This adaptor orients each edge in the undirected graph. The + /// direction of the arcs stored in an edge node map. The arcs can + /// be easily reverted by the \c reverseArc() member function in the + /// adaptor. The Orienter adaptor is conform to the \ref + /// concepts::Digraph "Digraph concept". + /// + /// \tparam _Graph It must be conform to the \ref concepts::Graph + /// "Graph concept". The type can be specified to be const. + /// \tparam _DirectionMap A bool valued edge map of the the adapted + /// graph. + /// + /// \sa orienter + template > + class Orienter : + public DigraphAdaptorExtender > { + public: + typedef _Graph Graph; + typedef DigraphAdaptorExtender< + OrienterBase<_Graph, DirectionMap> > Parent; + typedef typename Parent::Arc Arc; + protected: + Orienter() { } + public: + + /// \brief Constructor of the adaptor + /// + /// Constructor of the adaptor + Orienter(Graph& graph, DirectionMap& direction) { + setGraph(graph); + setDirectionMap(direction); + } + + /// \brief Reverse arc + /// + /// It reverse the given arc. It simply negate the direction in the map. + void reverseArc(const Arc& a) { + Parent::reverseArc(a); + } + }; + + /// \brief Just gives back a Orienter + /// + /// Just gives back a Orienter + template + Orienter + orienter(const Graph& graph, DirectionMap& dm) { + return Orienter(graph, dm); } - template, - typename _FlowMap = _CapacityMap, + template + Orienter + orienter(const Graph& graph, const DirectionMap& dm) { + return Orienter(graph, dm); + } + + namespace _adaptor_bits { + + template, + typename _FlowMap = _CapacityMap, + typename _Tolerance = Tolerance > + class ResForwardFilter { + public: + + typedef _Digraph Digraph; + typedef _CapacityMap CapacityMap; + typedef _FlowMap FlowMap; + typedef _Tolerance Tolerance; + + typedef typename Digraph::Arc Key; + typedef bool Value; + + private: + + const CapacityMap* _capacity; + const FlowMap* _flow; + Tolerance _tolerance; + public: + + ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, + const Tolerance& tolerance = Tolerance()) + : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } + + bool operator[](const typename Digraph::Arc& a) const { + return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); + } + }; + + template, + typename _FlowMap = _CapacityMap, + typename _Tolerance = Tolerance > + class ResBackwardFilter { + public: + + typedef _Digraph Digraph; + typedef _CapacityMap CapacityMap; + typedef _FlowMap FlowMap; + typedef _Tolerance Tolerance; + + typedef typename Digraph::Arc Key; + typedef bool Value; + + private: + + const CapacityMap* _capacity; + const FlowMap* _flow; + Tolerance _tolerance; + + public: + + ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, + const Tolerance& tolerance = Tolerance()) + : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } + + bool operator[](const typename Digraph::Arc& a) const { + return _tolerance.positive((*_flow)[a]); + } + }; + + } + + /// \ingroup graph_adaptors + /// + /// \brief An adaptor for composing the residual graph for directed + /// flow and circulation problems. + /// + /// An adaptor for composing the residual graph for directed flow and + /// circulation problems. Let \f$ G=(V, A) \f$ be a directed graph + /// and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$, + /// be functions on the arc-set. + /// + /// Then Residual implements the digraph structure with + /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} \f$, + /// where \f$ A_{forward}=\{uv : uv\in A, f(uv)0\} \f$, i.e. the so + /// called residual graph. When we take the union + /// \f$ A_{forward}\cup A_{backward} \f$, multiplicities are counted, + /// i.e. if an arc is in both \f$ A_{forward} \f$ and + /// \f$ A_{backward} \f$, then in the adaptor it appears in both + /// orientation. + /// + /// \tparam _Digraph It must be conform to the \ref concepts::Digraph + /// "Digraph concept". The type is implicitly const. + /// \tparam _CapacityMap An arc map of some numeric type, it defines + /// the capacities in the flow problem. The map is implicitly const. + /// \tparam _FlowMap An arc map of some numeric type, it defines + /// the capacities in the flow problem. + /// \tparam _Tolerance Handler for inexact computation. + template, + typename _FlowMap = _CapacityMap, typename _Tolerance = Tolerance > - class ResForwardFilter { + class Residual : + public FilterArcs< + Undirector, + typename Undirector::template CombinedArcMap< + _adaptor_bits::ResForwardFilter, + _adaptor_bits::ResBackwardFilter > > + { public: typedef _Digraph Digraph; @@ -1547,167 +2515,42 @@ typedef _FlowMap FlowMap; typedef _Tolerance Tolerance; - typedef typename Digraph::Arc Key; - typedef bool Value; - - private: - - const CapacityMap* _capacity; - const FlowMap* _flow; - Tolerance _tolerance; - public: - - ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, - const Tolerance& tolerance = Tolerance()) - : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } - - ResForwardFilter(const Tolerance& tolerance = Tolerance()) - : _capacity(0), _flow(0), _tolerance(tolerance) { } - - void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } - void setFlow(const FlowMap& flow) { _flow = &flow; } - - bool operator[](const typename Digraph::Arc& a) const { - return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); - } - }; - - template, - typename _FlowMap = _CapacityMap, - typename _Tolerance = Tolerance > - class ResBackwardFilter { - public: - - typedef _Digraph Digraph; - typedef _CapacityMap CapacityMap; - typedef _FlowMap FlowMap; - typedef _Tolerance Tolerance; - - typedef typename Digraph::Arc Key; - typedef bool Value; - - private: - - const CapacityMap* _capacity; - const FlowMap* _flow; - Tolerance _tolerance; - - public: - - ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, - const Tolerance& tolerance = Tolerance()) - : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } - ResBackwardFilter(const Tolerance& tolerance = Tolerance()) - : _capacity(0), _flow(0), _tolerance(tolerance) { } - - void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } - void setFlow(const FlowMap& flow) { _flow = &flow; } - - bool operator[](const typename Digraph::Arc& a) const { - return _tolerance.positive((*_flow)[a]); - } - }; - - - ///\ingroup graph_adaptors - /// - ///\brief An adaptor for composing the residual graph for directed - ///flow and circulation problems. - /// - ///An adaptor for composing the residual graph for directed flow and - ///circulation problems. Let \f$ G=(V, A) \f$ be a directed digraph - ///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F - ///\f$, be functions on the arc-set. - /// - ///In the appications of ResDigraphAdaptor, \f$ f \f$ usually stands - ///for a flow and \f$ c \f$ for a capacity function. Suppose that a - ///graph instance \c g of type \c ListDigraph implements \f$ G \f$. - /// - ///\code - /// ListDigraph g; - ///\endcode - /// - ///Then ResDigraphAdaptor implements the digraph structure with - /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} - /// \f$, where \f$ A_{forward}=\{uv : uv\in A, f(uv)0\} \f$, i.e. the so - /// called residual graph. When we take the union \f$ - /// A_{forward}\cup A_{backward} \f$, multilicities are counted, - /// i.e. if an arc is in both \f$ A_{forward} \f$ and \f$ - /// A_{backward} \f$, then in the adaptor it appears twice. The - /// following code shows how such an instance can be constructed. - /// - ///\code - /// typedef ListDigraph Digraph; - /// IntArcMap f(g), c(g); - /// ResDigraphAdaptor ga(g); - ///\endcode - template, - typename _FlowMap = _CapacityMap, - typename _Tolerance = Tolerance > - class ResDigraphAdaptor : - public ArcSubDigraphAdaptor< - UndirDigraphAdaptor, - typename UndirDigraphAdaptor::template CombinedArcMap< - ResForwardFilter, - ResBackwardFilter > > { - public: - - typedef _Digraph Digraph; - typedef _CapacityMap CapacityMap; - typedef _FlowMap FlowMap; - typedef _Tolerance Tolerance; - typedef typename CapacityMap::Value Value; - typedef ResDigraphAdaptor Adaptor; + typedef Residual Adaptor; protected: - typedef UndirDigraphAdaptor UndirDigraph; - - typedef ResForwardFilter - ForwardFilter; - - typedef ResBackwardFilter - BackwardFilter; - - typedef typename UndirDigraph:: + typedef Undirector Undirected; + + typedef _adaptor_bits::ResForwardFilter ForwardFilter; + + typedef _adaptor_bits::ResBackwardFilter BackwardFilter; + + typedef typename Undirected:: template CombinedArcMap ArcFilter; - typedef ArcSubDigraphAdaptor Parent; + typedef FilterArcs Parent; const CapacityMap* _capacity; FlowMap* _flow; - UndirDigraph _graph; + Undirected _graph; ForwardFilter _forward_filter; BackwardFilter _backward_filter; ArcFilter _arc_filter; - void setCapacityMap(const CapacityMap& capacity) { - _capacity = &capacity; - _forward_filter.setCapacity(capacity); - _backward_filter.setCapacity(capacity); - } - - void setFlowMap(FlowMap& flow) { - _flow = &flow; - _forward_filter.setFlow(flow); - _backward_filter.setFlow(flow); - } - public: /// \brief Constructor of the residual digraph. /// - /// Constructor of the residual graph. The parameters are the digraph type, + /// Constructor of the residual graph. The parameters are the digraph, /// the flow map, the capacity map and a tolerance object. - ResDigraphAdaptor(const Digraph& digraph, const CapacityMap& capacity, - FlowMap& flow, const Tolerance& tolerance = Tolerance()) + Residual(const Digraph& digraph, const CapacityMap& capacity, + FlowMap& flow, const Tolerance& tolerance = Tolerance()) : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph), - _forward_filter(capacity, flow, tolerance), + _forward_filter(capacity, flow, tolerance), _backward_filter(capacity, flow, tolerance), _arc_filter(_forward_filter, _backward_filter) { @@ -1720,83 +2563,87 @@ /// \brief Gives back the residual capacity of the arc. /// /// Gives back the residual capacity of the arc. - Value rescap(const Arc& arc) const { - if (UndirDigraph::direction(arc)) { - return (*_capacity)[arc] - (*_flow)[arc]; + Value residualCapacity(const Arc& a) const { + if (Undirected::direction(a)) { + return (*_capacity)[a] - (*_flow)[a]; } else { - return (*_flow)[arc]; + return (*_flow)[a]; } - } - - /// \brief Augment on the given arc in the residual digraph. + } + + /// \brief Augment on the given arc in the residual graph. /// - /// Augment on the given arc in the residual digraph. It increase + /// Augment on the given arc in the residual graph. It increase /// or decrease the flow on the original arc depend on the direction /// of the residual arc. - void augment(const Arc& e, const Value& a) const { - if (UndirDigraph::direction(e)) { - _flow->set(e, (*_flow)[e] + a); - } else { - _flow->set(e, (*_flow)[e] - a); + void augment(const Arc& a, const Value& v) const { + if (Undirected::direction(a)) { + _flow->set(a, (*_flow)[a] + v); + } else { + _flow->set(a, (*_flow)[a] - v); } } /// \brief Returns the direction of the arc. /// /// Returns true when the arc is same oriented as the original arc. - static bool forward(const Arc& e) { - return UndirDigraph::direction(e); + static bool forward(const Arc& a) { + return Undirected::direction(a); } /// \brief Returns the direction of the arc. /// /// Returns true when the arc is opposite oriented as the original arc. - static bool backward(const Arc& e) { - return !UndirDigraph::direction(e); + static bool backward(const Arc& a) { + return !Undirected::direction(a); } /// \brief Gives back the forward oriented residual arc. /// /// Gives back the forward oriented residual arc. - static Arc forward(const typename Digraph::Arc& e) { - return UndirDigraph::direct(e, true); + static Arc forward(const typename Digraph::Arc& a) { + return Undirected::direct(a, true); } /// \brief Gives back the backward oriented residual arc. /// /// Gives back the backward oriented residual arc. - static Arc backward(const typename Digraph::Arc& e) { - return UndirDigraph::direct(e, false); + static Arc backward(const typename Digraph::Arc& a) { + return Undirected::direct(a, false); } /// \brief Residual capacity map. /// - /// In generic residual digraphs the residual capacity can be obtained - /// as a map. - class ResCap { + /// In generic residual graph the residual capacity can be obtained + /// as a map. + class ResidualCapacity { protected: const Adaptor* _adaptor; public: + /// The Key type typedef Arc Key; + /// The Value type typedef typename _CapacityMap::Value Value; - ResCap(const Adaptor& adaptor) : _adaptor(&adaptor) {} - - Value operator[](const Arc& e) const { - return _adaptor->rescap(e); + /// Constructor + ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {} + + /// \e + Value operator[](const Arc& a) const { + return _adaptor->residualCapacity(a); } - + }; }; template - class SplitDigraphAdaptorBase { + class SplitNodesBase { public: typedef _Digraph Digraph; typedef DigraphAdaptorBase Parent; - typedef SplitDigraphAdaptorBase Adaptor; + typedef SplitNodesBase Adaptor; typedef typename Digraph::Node DigraphNode; typedef typename Digraph::Arc DigraphArc; @@ -1810,44 +2657,44 @@ template class ArcMapBase; public: - + class Node : public DigraphNode { - friend class SplitDigraphAdaptorBase; + friend class SplitNodesBase; template friend class NodeMapBase; private: bool _in; Node(DigraphNode node, bool in) - : DigraphNode(node), _in(in) {} - + : DigraphNode(node), _in(in) {} + public: Node() {} Node(Invalid) : DigraphNode(INVALID), _in(true) {} bool operator==(const Node& node) const { - return DigraphNode::operator==(node) && _in == node._in; + return DigraphNode::operator==(node) && _in == node._in; } - + bool operator!=(const Node& node) const { - return !(*this == node); + return !(*this == node); } - + bool operator<(const Node& node) const { - return DigraphNode::operator<(node) || - (DigraphNode::operator==(node) && _in < node._in); + return DigraphNode::operator<(node) || + (DigraphNode::operator==(node) && _in < node._in); } }; class Arc { - friend class SplitDigraphAdaptorBase; + friend class SplitNodesBase; template friend class ArcMapBase; private: typedef BiVariant ArcImpl; explicit Arc(const DigraphArc& arc) : _item(arc) {} explicit Arc(const DigraphNode& node) : _item(node) {} - + ArcImpl _item; public: @@ -1866,11 +2713,11 @@ } return false; } - + bool operator!=(const Arc& arc) const { - return !(*this == arc); + return !(*this == arc); } - + bool operator<(const Arc& arc) const { if (_item.firstState()) { if (arc._item.firstState()) { @@ -1897,10 +2744,10 @@ void next(Node& n) const { if (n._in) { - n._in = false; + n._in = false; } else { - n._in = true; - _digraph->next(n); + n._in = true; + _digraph->next(n); } } @@ -1909,20 +2756,20 @@ _digraph->first(e._item.second()); if (e._item.second() == INVALID) { e._item.setFirst(); - _digraph->first(e._item.first()); + _digraph->first(e._item.first()); } } void next(Arc& e) const { if (e._item.secondState()) { - _digraph->next(e._item.second()); + _digraph->next(e._item.second()); if (e._item.second() == INVALID) { e._item.setFirst(); _digraph->first(e._item.first()); } } else { - _digraph->next(e._item.first()); - } + _digraph->next(e._item.first()); + } } void firstOut(Arc& e, const Node& n) const { @@ -1930,48 +2777,48 @@ e._item.setSecond(n); } else { e._item.setFirst(); - _digraph->firstOut(e._item.first(), n); + _digraph->firstOut(e._item.first(), n); } } void nextOut(Arc& e) const { if (!e._item.firstState()) { - e._item.setFirst(INVALID); + e._item.setFirst(INVALID); } else { - _digraph->nextOut(e._item.first()); - } + _digraph->nextOut(e._item.first()); + } } void firstIn(Arc& e, const Node& n) const { if (!n._in) { - e._item.setSecond(n); + e._item.setSecond(n); } else { e._item.setFirst(); - _digraph->firstIn(e._item.first(), n); + _digraph->firstIn(e._item.first(), n); } } void nextIn(Arc& e) const { if (!e._item.firstState()) { - e._item.setFirst(INVALID); + e._item.setFirst(INVALID); } else { - _digraph->nextIn(e._item.first()); + _digraph->nextIn(e._item.first()); } } Node source(const Arc& e) const { if (e._item.firstState()) { - return Node(_digraph->source(e._item.first()), false); + return Node(_digraph->source(e._item.first()), false); } else { - return Node(e._item.second(), true); + return Node(e._item.second(), true); } } Node target(const Arc& e) const { if (e._item.firstState()) { - return Node(_digraph->target(e._item.first()), true); + return Node(_digraph->target(e._item.first()), true); } else { - return Node(e._item.second(), false); + return Node(e._item.second(), false); } } @@ -2000,7 +2847,7 @@ } } int maxArcId() const { - return std::max(_digraph->maxNodeId() << 1, + return std::max(_digraph->maxNodeId() << 1, (_digraph->maxArcId() << 1) | 1); } @@ -2048,11 +2895,11 @@ } typedef True FindEdgeTag; - Arc findArc(const Node& u, const Node& v, - const Arc& prev = INVALID) const { + Arc findArc(const Node& u, const Node& v, + const Arc& prev = INVALID) const { if (inNode(u)) { if (outNode(v)) { - if (static_cast(u) == + if (static_cast(u) == static_cast(v) && prev == INVALID) { return Arc(u); } @@ -2066,36 +2913,36 @@ } private: - + template - class NodeMapBase + class NodeMapBase : public MapTraits > { typedef typename Parent::template NodeMap<_Value> NodeImpl; public: typedef Node Key; typedef _Value Value; - - NodeMapBase(const Adaptor& adaptor) - : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} - NodeMapBase(const Adaptor& adaptor, const Value& value) - : _in_map(*adaptor._digraph, value), - _out_map(*adaptor._digraph, value) {} + + NodeMapBase(const Adaptor& adaptor) + : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} + NodeMapBase(const Adaptor& adaptor, const Value& value) + : _in_map(*adaptor._digraph, value), + _out_map(*adaptor._digraph, value) {} void set(const Node& key, const Value& val) { - if (Adaptor::inNode(key)) { _in_map.set(key, val); } - else {_out_map.set(key, val); } + if (Adaptor::inNode(key)) { _in_map.set(key, val); } + else {_out_map.set(key, val); } } - - typename MapTraits::ReturnValue + + typename MapTraits::ReturnValue operator[](const Node& key) { - if (Adaptor::inNode(key)) { return _in_map[key]; } - else { return _out_map[key]; } + if (Adaptor::inNode(key)) { return _in_map[key]; } + else { return _out_map[key]; } } typename MapTraits::ConstReturnValue operator[](const Node& key) const { - if (Adaptor::inNode(key)) { return _in_map[key]; } - else { return _out_map[key]; } + if (Adaptor::inNode(key)) { return _in_map[key]; } + else { return _out_map[key]; } } private: @@ -2103,7 +2950,7 @@ }; template - class ArcMapBase + class ArcMapBase : public MapTraits > { typedef typename Parent::template ArcMap<_Value> ArcImpl; typedef typename Parent::template NodeMap<_Value> NodeImpl; @@ -2111,23 +2958,23 @@ typedef Arc Key; typedef _Value Value; - ArcMapBase(const Adaptor& adaptor) - : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} - ArcMapBase(const Adaptor& adaptor, const Value& value) - : _arc_map(*adaptor._digraph, value), - _node_map(*adaptor._digraph, value) {} + ArcMapBase(const Adaptor& adaptor) + : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} + ArcMapBase(const Adaptor& adaptor, const Value& value) + : _arc_map(*adaptor._digraph, value), + _node_map(*adaptor._digraph, value) {} void set(const Arc& key, const Value& val) { - if (Adaptor::origArc(key)) { - _arc_map.set(key._item.first(), val); + if (Adaptor::origArc(key)) { + _arc_map.set(key._item.first(), val); } else { - _node_map.set(key._item.second(), val); + _node_map.set(key._item.second(), val); } } - + typename MapTraits::ReturnValue operator[](const Arc& key) { - if (Adaptor::origArc(key)) { + if (Adaptor::origArc(key)) { return _arc_map[key._item.first()]; } else { return _node_map[key._item.second()]; @@ -2136,7 +2983,7 @@ typename MapTraits::ConstReturnValue operator[](const Arc& key) const { - if (Adaptor::origArc(key)) { + if (Adaptor::origArc(key)) { return _arc_map[key._item.first()]; } else { return _node_map[key._item.second()]; @@ -2151,142 +2998,96 @@ public: template - class NodeMap - : public SubMapExtender > + class NodeMap + : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > Parent; - - NodeMap(const Adaptor& adaptor) - : Parent(adaptor) {} - - NodeMap(const Adaptor& adaptor, const Value& value) - : Parent(adaptor, value) {} - + + NodeMap(const Adaptor& adaptor) + : Parent(adaptor) {} + + NodeMap(const Adaptor& adaptor, const Value& value) + : Parent(adaptor, value) {} + private: NodeMap& operator=(const NodeMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template NodeMap& operator=(const CMap& cmap) { Parent::operator=(cmap); - return *this; + return *this; } }; template - class ArcMap - : public SubMapExtender > + class ArcMap + : public SubMapExtender > { public: typedef _Value Value; typedef SubMapExtender > Parent; - - ArcMap(const Adaptor& adaptor) - : Parent(adaptor) {} - - ArcMap(const Adaptor& adaptor, const Value& value) - : Parent(adaptor, value) {} - + + ArcMap(const Adaptor& adaptor) + : Parent(adaptor) {} + + ArcMap(const Adaptor& adaptor, const Value& value) + : Parent(adaptor, value) {} + private: ArcMap& operator=(const ArcMap& cmap) { - return operator=(cmap); + return operator=(cmap); } - + template ArcMap& operator=(const CMap& cmap) { Parent::operator=(cmap); - return *this; + return *this; } }; protected: - SplitDigraphAdaptorBase() : _digraph(0) {} + SplitNodesBase() : _digraph(0) {} Digraph* _digraph; void setDigraph(Digraph& digraph) { _digraph = &digraph; } - + }; /// \ingroup graph_adaptors /// - /// \brief Split digraph adaptor class - /// - /// This is an digraph adaptor which splits all node into an in-node - /// and an out-node. Formaly, the adaptor replaces each \f$ u \f$ - /// node in the digraph with two node, \f$ u_{in} \f$ node and - /// \f$ u_{out} \f$ node. If there is an \f$ (v, u) \f$ arc in the - /// original digraph the new target of the arc will be \f$ u_{in} \f$ and - /// similarly the source of the original \f$ (u, v) \f$ arc will be - /// \f$ u_{out} \f$. The adaptor will add for each node in the - /// original digraph an additional arc which will connect + /// \brief Split the nodes of a directed graph + /// + /// The SplitNodes adaptor splits each node into an in-node and an + /// out-node. Formaly, the adaptor replaces each \f$ u \f$ node in + /// the digraph with two nodes(namely node \f$ u_{in} \f$ and node + /// \f$ u_{out} \f$). If there is a \f$ (v, u) \f$ arc in the + /// original digraph the new target of the arc will be \f$ u_{in} \f$ + /// and similarly the source of the original \f$ (u, v) \f$ arc + /// will be \f$ u_{out} \f$. The adaptor will add for each node in + /// the original digraph an additional arc which connects /// \f$ (u_{in}, u_{out}) \f$. /// - /// The aim of this class is to run algorithm with node costs if the + /// The aim of this class is to run algorithm with node costs if the /// algorithm can use directly just arc costs. In this case we should use - /// a \c SplitDigraphAdaptor and set the node cost of the digraph to the - /// bind arc in the adapted digraph. - /// - /// For example a maximum flow algorithm can compute how many arc - /// disjoint paths are in the digraph. But we would like to know how - /// many node disjoint paths are in the digraph. First we have to - /// adapt the digraph with the \c SplitDigraphAdaptor. Then run the flow - /// algorithm on the adapted digraph. The bottleneck of the flow will - /// be the bind arcs which bounds the flow with the count of the - /// node disjoint paths. + /// a \c SplitNodes and set the node cost of the graph to the + /// bind arc in the adapted graph. /// - ///\code - /// - /// typedef SplitDigraphAdaptor SDigraph; - /// - /// SDigraph sdigraph(digraph); - /// - /// typedef ConstMap SCapacity; - /// SCapacity scapacity(1); - /// - /// SDigraph::ArcMap sflow(sdigraph); - /// - /// Preflow - /// spreflow(sdigraph, scapacity, - /// SDigraph::outNode(source), SDigraph::inNode(target)); - /// - /// spreflow.run(); - /// - ///\endcode - /// - /// The result of the mamixum flow on the original digraph - /// shows the next figure: - /// - /// \image html arc_disjoint.png - /// \image latex arc_disjoint.eps "Arc disjoint paths" width=\textwidth - /// - /// And the maximum flow on the adapted digraph: - /// - /// \image html node_disjoint.png - /// \image latex node_disjoint.eps "Node disjoint paths" width=\textwidth - /// - /// The second solution contains just 3 disjoint paths while the first 4. - /// The full code can be found in the \ref disjoint_paths_demo.cc demo file. - /// - /// This digraph adaptor is fully conform to the - /// \ref concepts::Digraph "Digraph" concept and - /// contains some additional member functions and types. The - /// documentation of some member functions may be found just in the - /// SplitDigraphAdaptorBase class. - /// - /// \sa SplitDigraphAdaptorBase + /// \tparam _Digraph It must be conform to the \ref concepts::Digraph + /// "Digraph concept". The type can be specified to be const. template - class SplitDigraphAdaptor - : public DigraphAdaptorExtender > { + class SplitNodes + : public DigraphAdaptorExtender > { public: typedef _Digraph Digraph; - typedef DigraphAdaptorExtender > Parent; + typedef DigraphAdaptorExtender > Parent; typedef typename Digraph::Node DigraphNode; typedef typename Digraph::Arc DigraphArc; @@ -2297,7 +3098,7 @@ /// \brief Constructor of the adaptor. /// /// Constructor of the adaptor. - SplitDigraphAdaptor(Digraph& g) { + SplitNodes(Digraph& g) { Parent::setDigraph(g); } @@ -2344,14 +3145,14 @@ } /// \brief Gives back the arc binds the two part of the node. - /// + /// /// Gives back the arc binds the two part of the node. static Arc arc(const DigraphNode& n) { return Parent::arc(n); } /// \brief Gives back the arc of the original arc. - /// + /// /// Gives back the arc of the original arc. static Arc arc(const DigraphArc& a) { return Parent::arc(a); @@ -2371,177 +3172,176 @@ /// \brief Constructor /// /// Constructor. - CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) - : _in_map(in_map), _out_map(out_map) {} + CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) + : _in_map(in_map), _out_map(out_map) {} /// \brief The subscript operator. /// /// The subscript operator. Value& operator[](const Key& key) { - if (Parent::inNode(key)) { - return _in_map[key]; - } else { - return _out_map[key]; - } + if (Parent::inNode(key)) { + return _in_map[key]; + } else { + return _out_map[key]; + } } /// \brief The const subscript operator. /// /// The const subscript operator. Value operator[](const Key& key) const { - if (Parent::inNode(key)) { - return _in_map[key]; - } else { - return _out_map[key]; - } + if (Parent::inNode(key)) { + return _in_map[key]; + } else { + return _out_map[key]; + } } /// \brief The setter function of the map. - /// + /// /// The setter function of the map. void set(const Key& key, const Value& value) { - if (Parent::inNode(key)) { - _in_map.set(key, value); - } else { - _out_map.set(key, value); - } + if (Parent::inNode(key)) { + _in_map.set(key, value); + } else { + _out_map.set(key, value); + } } - + private: - + InNodeMap& _in_map; OutNodeMap& _out_map; - + }; - /// \brief Just gives back a combined node map. - /// - /// Just gives back a combined node map. + /// \brief Just gives back a combined node map + /// + /// Just gives back a combined node map template - static CombinedNodeMap + static CombinedNodeMap combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { return CombinedNodeMap(in_map, out_map); } template - static CombinedNodeMap + static CombinedNodeMap combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { return CombinedNodeMap(in_map, out_map); } template - static CombinedNodeMap + static CombinedNodeMap combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { return CombinedNodeMap(in_map, out_map); } template - static CombinedNodeMap + static CombinedNodeMap combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { - return CombinedNodeMap(in_map, out_map); } - /// \brief ArcMap combined from an original ArcMap and NodeMap + /// \brief ArcMap combined from an original ArcMap and a NodeMap /// - /// This class adapt an original digraph ArcMap and NodeMap to - /// get an arc map on the adapted digraph. + /// This class adapt an original ArcMap and a NodeMap to get an + /// arc map on the adapted digraph template class CombinedArcMap { public: - + typedef Arc Key; typedef typename DigraphArcMap::Value Value; - + /// \brief Constructor /// /// Constructor. - CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) - : _arc_map(arc_map), _node_map(node_map) {} + CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) + : _arc_map(arc_map), _node_map(node_map) {} /// \brief The subscript operator. /// /// The subscript operator. void set(const Arc& arc, const Value& val) { - if (Parent::origArc(arc)) { - _arc_map.set(arc, val); - } else { - _node_map.set(arc, val); - } + if (Parent::origArc(arc)) { + _arc_map.set(arc, val); + } else { + _node_map.set(arc, val); + } } /// \brief The const subscript operator. /// /// The const subscript operator. Value operator[](const Key& arc) const { - if (Parent::origArc(arc)) { - return _arc_map[arc]; - } else { - return _node_map[arc]; - } - } + if (Parent::origArc(arc)) { + return _arc_map[arc]; + } else { + return _node_map[arc]; + } + } /// \brief The const subscript operator. /// /// The const subscript operator. Value& operator[](const Key& arc) { - if (Parent::origArc(arc)) { - return _arc_map[arc]; - } else { - return _node_map[arc]; - } - } - + if (Parent::origArc(arc)) { + return _arc_map[arc]; + } else { + return _node_map[arc]; + } + } + private: DigraphArcMap& _arc_map; DigraphNodeMap& _node_map; }; - - /// \brief Just gives back a combined arc map. - /// - /// Just gives back a combined arc map. + + /// \brief Just gives back a combined arc map + /// + /// Just gives back a combined arc map template - static CombinedArcMap + static CombinedArcMap combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) { return CombinedArcMap(arc_map, node_map); } template - static CombinedArcMap + static CombinedArcMap combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) { - return CombinedArcMap(arc_map, node_map); } template - static CombinedArcMap + static CombinedArcMap combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) { - return CombinedArcMap(arc_map, node_map); } template - static CombinedArcMap - combinedArcMap(const DigraphArcMap& arc_map, - const DigraphNodeMap& node_map) { - return CombinedArcMap + combinedArcMap(const DigraphArcMap& arc_map, + const DigraphNodeMap& node_map) { + return CombinedArcMap(arc_map, node_map); } }; - /// \brief Just gives back a split digraph adaptor + /// \brief Just gives back a node splitter /// - /// Just gives back a split digraph adaptor + /// Just gives back a node splitter template - SplitDigraphAdaptor - splitDigraphAdaptor(const Digraph& digraph) { - return SplitDigraphAdaptor(digraph); + SplitNodes + splitNodes(const Digraph& digraph) { + return SplitNodes(digraph); } } //namespace lemon -#endif //LEMON_DIGRAPH_ADAPTOR_H - +#endif //LEMON_ADAPTORS_H