deba@414: /* -*- C++ -*- deba@414: * deba@414: * This file is a part of LEMON, a generic C++ optimization library deba@414: * deba@414: * Copyright (C) 2003-2008 deba@414: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@414: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@414: * deba@414: * Permission to use, modify and distribute this software is granted deba@414: * provided that this copyright notice appears in all copies. For deba@414: * precise terms see the accompanying LICENSE file. deba@414: * deba@414: * This software is provided "AS IS" with no warranty of any kind, deba@414: * express or implied, and with no claim as to its suitability for any deba@414: * purpose. deba@414: * deba@414: */ deba@414: deba@414: #ifndef LEMON_DIGRAPH_ADAPTOR_H deba@414: #define LEMON_DIGRAPH_ADAPTOR_H deba@414: deba@414: ///\ingroup graph_adaptors deba@414: ///\file deba@414: ///\brief Several digraph adaptors. deba@414: /// deba@414: ///This file contains several useful digraph adaptor functions. deba@414: deba@414: #include deba@414: #include deba@414: #include deba@414: deba@414: #include deba@414: #include deba@414: #include deba@414: #include deba@414: deba@414: #include deba@414: deba@414: namespace lemon { deba@414: deba@414: ///\brief Base type for the Digraph Adaptors deba@414: /// deba@414: ///Base type for the Digraph Adaptors deba@414: /// deba@414: ///This is the base type for most of LEMON digraph adaptors. This deba@414: ///class implements a trivial digraph adaptor i.e. it only wraps the deba@414: ///functions and types of the digraph. The purpose of this class is deba@414: ///to make easier implementing digraph adaptors. E.g. if an adaptor deba@414: ///is considered which differs from the wrapped digraph only in some deba@414: ///of its functions or types, then it can be derived from deba@414: ///DigraphAdaptor, and only the differences should be implemented. deba@414: template deba@414: class DigraphAdaptorBase { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorBase Adaptor; deba@414: typedef Digraph ParentDigraph; deba@414: deba@414: protected: deba@414: Digraph* _digraph; deba@414: DigraphAdaptorBase() : _digraph(0) { } deba@414: void setDigraph(Digraph& digraph) { _digraph = &digraph; } deba@414: deba@414: public: deba@414: DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { } deba@414: deba@414: typedef typename Digraph::Node Node; deba@414: typedef typename Digraph::Arc Arc; deba@414: deba@414: void first(Node& i) const { _digraph->first(i); } deba@414: void first(Arc& i) const { _digraph->first(i); } deba@414: void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } deba@414: void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } deba@414: deba@414: void next(Node& i) const { _digraph->next(i); } deba@414: void next(Arc& i) const { _digraph->next(i); } deba@414: void nextIn(Arc& i) const { _digraph->nextIn(i); } deba@414: void nextOut(Arc& i) const { _digraph->nextOut(i); } deba@414: deba@414: Node source(const Arc& a) const { return _digraph->source(a); } deba@414: Node target(const Arc& a) const { return _digraph->target(a); } deba@414: deba@414: typedef NodeNumTagIndicator NodeNumTag; deba@414: int nodeNum() const { return _digraph->nodeNum(); } deba@414: deba@414: typedef EdgeNumTagIndicator EdgeNumTag; deba@414: int arcNum() const { return _digraph->arcNum(); } deba@414: deba@414: typedef FindEdgeTagIndicator FindEdgeTag; deba@414: Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { deba@414: return _digraph->findArc(u, v, prev); deba@414: } deba@414: deba@414: Node addNode() { return _digraph->addNode(); } deba@414: Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } deba@414: deba@414: void erase(const Node& n) const { _digraph->erase(n); } deba@414: void erase(const Arc& a) const { _digraph->erase(a); } deba@414: deba@414: void clear() const { _digraph->clear(); } deba@414: deba@414: int id(const Node& n) const { return _digraph->id(n); } deba@414: int id(const Arc& a) const { return _digraph->id(a); } deba@414: deba@414: Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } deba@414: Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } deba@414: deba@414: int maxNodeId() const { return _digraph->maxNodeId(); } deba@414: int maxArcId() const { return _digraph->maxArcId(); } deba@414: deba@414: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@414: NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } deba@414: deba@414: typedef typename ItemSetTraits::ItemNotifier ArcNotifier; deba@414: ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } deba@414: deba@414: template deba@414: class NodeMap : public Digraph::template NodeMap<_Value> { deba@414: public: deba@414: deba@414: typedef typename Digraph::template NodeMap<_Value> Parent; deba@414: deba@414: explicit NodeMap(const Adaptor& adaptor) deba@414: : Parent(*adaptor._digraph) {} deba@414: deba@414: NodeMap(const Adaptor& adaptor, const _Value& value) deba@414: : Parent(*adaptor._digraph, value) { } deba@414: deba@414: private: deba@414: NodeMap& operator=(const NodeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: NodeMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: deba@414: }; deba@414: deba@414: template deba@414: class ArcMap : public Digraph::template ArcMap<_Value> { deba@414: public: deba@414: deba@414: typedef typename Digraph::template ArcMap<_Value> Parent; deba@414: deba@414: explicit ArcMap(const Adaptor& adaptor) deba@414: : Parent(*adaptor._digraph) {} deba@414: deba@414: ArcMap(const Adaptor& adaptor, const _Value& value) deba@414: : Parent(*adaptor._digraph, value) {} deba@414: deba@414: private: deba@414: ArcMap& operator=(const ArcMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: ArcMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: deba@414: }; deba@414: deba@414: }; deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: ///\brief Trivial Digraph Adaptor deba@414: /// deba@414: /// This class is an adaptor which does not change the adapted deba@414: /// digraph. It can be used only to test the digraph adaptors. deba@414: template deba@414: class DigraphAdaptor : deba@414: public DigraphAdaptorExtender > { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorExtender > Parent; deba@414: protected: deba@414: DigraphAdaptor() : Parent() { } deba@414: deba@414: public: deba@414: explicit DigraphAdaptor(Digraph& digraph) { setDigraph(digraph); } deba@414: }; deba@414: deba@414: /// \brief Just gives back a digraph adaptor deba@414: /// deba@414: /// Just gives back a digraph adaptor which deba@414: /// should be provide original digraph deba@414: template deba@414: DigraphAdaptor deba@414: digraphAdaptor(const Digraph& digraph) { deba@414: return DigraphAdaptor(digraph); deba@414: } deba@414: deba@414: deba@414: template deba@414: class RevDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorBase<_Digraph> Parent; deba@414: protected: deba@414: RevDigraphAdaptorBase() : Parent() { } deba@414: public: deba@414: typedef typename Parent::Node Node; deba@414: typedef typename Parent::Arc Arc; deba@414: deba@414: void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } deba@414: void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } deba@414: deba@414: void nextIn(Arc& a) const { Parent::nextOut(a); } deba@414: void nextOut(Arc& a) const { Parent::nextIn(a); } deba@414: deba@414: Node source(const Arc& a) const { return Parent::target(a); } deba@414: Node target(const Arc& a) const { return Parent::source(a); } deba@414: deba@414: typedef FindEdgeTagIndicator FindEdgeTag; deba@414: Arc findArc(const Node& u, const Node& v, deba@414: const Arc& prev = INVALID) { deba@414: return Parent::findArc(v, u, prev); deba@414: } deba@414: deba@414: }; deba@414: deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: ///\brief A digraph adaptor which reverses the orientation of the arcs. deba@414: /// deba@414: /// If \c g is defined as deba@414: ///\code deba@414: /// ListDigraph g; deba@414: ///\endcode deba@414: /// then deba@414: ///\code deba@414: /// RevDigraphAdaptor ga(g); deba@414: ///\endcode deba@414: /// implements the digraph obtained from \c g by deba@414: /// reversing the orientation of its arcs. deba@414: /// deba@414: /// A good example of using RevDigraphAdaptor is to decide that the deba@414: /// directed graph is wheter strongly connected or not. If from one deba@414: /// node each node is reachable and from each node is reachable this deba@414: /// node then and just then the digraph is strongly deba@414: /// connected. Instead of this condition we use a little bit deba@414: /// different. From one node each node ahould be reachable in the deba@414: /// digraph and in the reversed digraph. Now this condition can be deba@414: /// checked with the Dfs algorithm class and the RevDigraphAdaptor deba@414: /// algorithm class. deba@414: /// deba@414: /// And look at the code: deba@414: /// deba@414: ///\code deba@414: /// bool stronglyConnected(const Digraph& digraph) { deba@414: /// if (NodeIt(digraph) == INVALID) return true; deba@414: /// Dfs dfs(digraph); deba@414: /// dfs.run(NodeIt(digraph)); deba@414: /// for (NodeIt it(digraph); it != INVALID; ++it) { deba@414: /// if (!dfs.reached(it)) { deba@414: /// return false; deba@414: /// } deba@414: /// } deba@414: /// typedef RevDigraphAdaptor RDigraph; deba@414: /// RDigraph rdigraph(digraph); deba@414: /// DfsVisit rdfs(rdigraph); deba@414: /// rdfs.run(NodeIt(digraph)); deba@414: /// for (NodeIt it(digraph); it != INVALID; ++it) { deba@414: /// if (!rdfs.reached(it)) { deba@414: /// return false; deba@414: /// } deba@414: /// } deba@414: /// return true; deba@414: /// } deba@414: ///\endcode deba@414: template deba@414: class RevDigraphAdaptor : deba@414: public DigraphAdaptorExtender > { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorExtender< deba@414: RevDigraphAdaptorBase<_Digraph> > Parent; deba@414: protected: deba@414: RevDigraphAdaptor() { } deba@414: public: deba@414: explicit RevDigraphAdaptor(Digraph& digraph) { deba@414: Parent::setDigraph(digraph); deba@414: } deba@414: }; deba@414: deba@414: /// \brief Just gives back a reverse digraph adaptor deba@414: /// deba@414: /// Just gives back a reverse digraph adaptor deba@414: template deba@414: RevDigraphAdaptor deba@414: revDigraphAdaptor(const Digraph& digraph) { deba@414: return RevDigraphAdaptor(digraph); deba@414: } deba@414: deba@414: template deba@414: class SubDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef _NodeFilterMap NodeFilterMap; deba@414: typedef _ArcFilterMap ArcFilterMap; deba@414: deba@414: typedef SubDigraphAdaptorBase Adaptor; deba@414: typedef DigraphAdaptorBase<_Digraph> Parent; deba@414: protected: deba@414: NodeFilterMap* _node_filter; deba@414: ArcFilterMap* _arc_filter; deba@414: SubDigraphAdaptorBase() deba@414: : Parent(), _node_filter(0), _arc_filter(0) { } deba@414: deba@414: void setNodeFilterMap(NodeFilterMap& node_filter) { deba@414: _node_filter = &node_filter; deba@414: } deba@414: void setArcFilterMap(ArcFilterMap& arc_filter) { deba@414: _arc_filter = &arc_filter; deba@414: } deba@414: deba@414: public: deba@414: deba@414: typedef typename Parent::Node Node; deba@414: typedef typename Parent::Arc Arc; deba@414: deba@414: void first(Node& i) const { deba@414: Parent::first(i); deba@414: while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); deba@414: } deba@414: deba@414: void first(Arc& i) const { deba@414: Parent::first(i); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::source(i)] deba@414: || !(*_node_filter)[Parent::target(i)])) Parent::next(i); deba@414: } deba@414: deba@414: void firstIn(Arc& i, const Node& n) const { deba@414: Parent::firstIn(i, n); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); deba@414: } deba@414: deba@414: void firstOut(Arc& i, const Node& n) const { deba@414: Parent::firstOut(i, n); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); deba@414: } deba@414: deba@414: void next(Node& i) const { deba@414: Parent::next(i); deba@414: while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); deba@414: } deba@414: deba@414: void next(Arc& i) const { deba@414: Parent::next(i); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::source(i)] deba@414: || !(*_node_filter)[Parent::target(i)])) Parent::next(i); deba@414: } deba@414: deba@414: void nextIn(Arc& i) const { deba@414: Parent::nextIn(i); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); deba@414: } deba@414: deba@414: void nextOut(Arc& i) const { deba@414: Parent::nextOut(i); deba@414: while (i != INVALID && (!(*_arc_filter)[i] deba@414: || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); deba@414: } deba@414: deba@414: ///\e deba@414: deba@414: /// This function hides \c n in the digraph, i.e. the iteration deba@414: /// jumps over it. This is done by simply setting the value of \c n deba@414: /// to be false in the corresponding node-map. deba@414: void hide(const Node& n) const { _node_filter->set(n, false); } deba@414: deba@414: ///\e deba@414: deba@414: /// This function hides \c a in the digraph, i.e. the iteration deba@414: /// jumps over it. This is done by simply setting the value of \c a deba@414: /// to be false in the corresponding arc-map. deba@414: void hide(const Arc& a) const { _arc_filter->set(a, false); } deba@414: deba@414: ///\e deba@414: deba@414: /// The value of \c n is set to be true in the node-map which stores deba@414: /// hide information. If \c n was hidden previuosly, then it is shown deba@414: /// again deba@414: void unHide(const Node& n) const { _node_filter->set(n, true); } deba@414: deba@414: ///\e deba@414: deba@414: /// The value of \c a is set to be true in the arc-map which stores deba@414: /// hide information. If \c a was hidden previuosly, then it is shown deba@414: /// again deba@414: void unHide(const Arc& a) const { _arc_filter->set(a, true); } deba@414: deba@414: /// Returns true if \c n is hidden. deba@414: deba@414: ///\e deba@414: /// deba@414: bool hidden(const Node& n) const { return !(*_node_filter)[n]; } deba@414: deba@414: /// Returns true if \c a is hidden. deba@414: deba@414: ///\e deba@414: /// deba@414: bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; } deba@414: deba@414: typedef False NodeNumTag; deba@414: typedef False EdgeNumTag; deba@414: deba@414: typedef FindEdgeTagIndicator FindEdgeTag; deba@414: Arc findArc(const Node& source, const Node& target, deba@414: const Arc& prev = INVALID) { deba@414: if (!(*_node_filter)[source] || !(*_node_filter)[target]) { deba@414: return INVALID; deba@414: } deba@414: Arc arc = Parent::findArc(source, target, prev); deba@414: while (arc != INVALID && !(*_arc_filter)[arc]) { deba@414: arc = Parent::findArc(source, target, arc); deba@414: } deba@414: return arc; deba@414: } deba@414: deba@414: template deba@414: class NodeMap : public SubMapExtender > { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > MapParent; deba@414: deba@414: NodeMap(const Adaptor& adaptor) deba@414: : MapParent(adaptor) {} deba@414: NodeMap(const Adaptor& adaptor, const Value& value) deba@414: : MapParent(adaptor, value) {} deba@414: deba@414: private: deba@414: NodeMap& operator=(const NodeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: NodeMap& operator=(const CMap& cmap) { deba@414: MapParent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: template deba@414: class ArcMap : public SubMapExtender > { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > MapParent; deba@414: deba@414: ArcMap(const Adaptor& adaptor) deba@414: : MapParent(adaptor) {} deba@414: ArcMap(const Adaptor& adaptor, const Value& value) deba@414: : MapParent(adaptor, value) {} deba@414: deba@414: private: deba@414: ArcMap& operator=(const ArcMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: ArcMap& operator=(const CMap& cmap) { deba@414: MapParent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: }; deba@414: deba@414: template deba@414: class SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> deba@414: : public DigraphAdaptorBase<_Digraph> { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef _NodeFilterMap NodeFilterMap; deba@414: typedef _ArcFilterMap ArcFilterMap; deba@414: deba@414: typedef SubDigraphAdaptorBase Adaptor; deba@414: typedef DigraphAdaptorBase Parent; deba@414: protected: deba@414: NodeFilterMap* _node_filter; deba@414: ArcFilterMap* _arc_filter; deba@414: SubDigraphAdaptorBase() deba@414: : Parent(), _node_filter(0), _arc_filter(0) { } deba@414: deba@414: void setNodeFilterMap(NodeFilterMap& node_filter) { deba@414: _node_filter = &node_filter; deba@414: } deba@414: void setArcFilterMap(ArcFilterMap& arc_filter) { deba@414: _arc_filter = &arc_filter; deba@414: } deba@414: deba@414: public: deba@414: deba@414: typedef typename Parent::Node Node; deba@414: typedef typename Parent::Arc Arc; deba@414: deba@414: void first(Node& i) const { deba@414: Parent::first(i); deba@414: while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); deba@414: } deba@414: deba@414: void first(Arc& i) const { deba@414: Parent::first(i); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); deba@414: } deba@414: deba@414: void firstIn(Arc& i, const Node& n) const { deba@414: Parent::firstIn(i, n); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); deba@414: } deba@414: deba@414: void firstOut(Arc& i, const Node& n) const { deba@414: Parent::firstOut(i, n); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); deba@414: } deba@414: deba@414: void next(Node& i) const { deba@414: Parent::next(i); deba@414: while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); deba@414: } deba@414: void next(Arc& i) const { deba@414: Parent::next(i); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); deba@414: } deba@414: void nextIn(Arc& i) const { deba@414: Parent::nextIn(i); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); deba@414: } deba@414: deba@414: void nextOut(Arc& i) const { deba@414: Parent::nextOut(i); deba@414: while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); deba@414: } deba@414: deba@414: ///\e deba@414: deba@414: /// This function hides \c n in the digraph, i.e. the iteration deba@414: /// jumps over it. This is done by simply setting the value of \c n deba@414: /// to be false in the corresponding node-map. deba@414: void hide(const Node& n) const { _node_filter->set(n, false); } deba@414: deba@414: ///\e deba@414: deba@414: /// This function hides \c e in the digraph, i.e. the iteration deba@414: /// jumps over it. This is done by simply setting the value of \c e deba@414: /// to be false in the corresponding arc-map. deba@414: void hide(const Arc& e) const { _arc_filter->set(e, false); } deba@414: deba@414: ///\e deba@414: deba@414: /// The value of \c n is set to be true in the node-map which stores deba@414: /// hide information. If \c n was hidden previuosly, then it is shown deba@414: /// again deba@414: void unHide(const Node& n) const { _node_filter->set(n, true); } deba@414: deba@414: ///\e deba@414: deba@414: /// The value of \c e is set to be true in the arc-map which stores deba@414: /// hide information. If \c e was hidden previuosly, then it is shown deba@414: /// again deba@414: void unHide(const Arc& e) const { _arc_filter->set(e, true); } deba@414: deba@414: /// Returns true if \c n is hidden. deba@414: deba@414: ///\e deba@414: /// deba@414: bool hidden(const Node& n) const { return !(*_node_filter)[n]; } deba@414: deba@414: /// Returns true if \c n is hidden. deba@414: deba@414: ///\e deba@414: /// deba@414: bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; } deba@414: deba@414: typedef False NodeNumTag; deba@414: typedef False EdgeNumTag; deba@414: deba@414: typedef FindEdgeTagIndicator FindEdgeTag; deba@414: Arc findArc(const Node& source, const Node& target, deba@414: const Arc& prev = INVALID) { deba@414: if (!(*_node_filter)[source] || !(*_node_filter)[target]) { deba@414: return INVALID; deba@414: } deba@414: Arc arc = Parent::findArc(source, target, prev); deba@414: while (arc != INVALID && !(*_arc_filter)[arc]) { deba@414: arc = Parent::findArc(source, target, arc); deba@414: } deba@414: return arc; deba@414: } deba@414: deba@414: template deba@414: class NodeMap : public SubMapExtender > { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > MapParent; deba@414: deba@414: NodeMap(const Adaptor& adaptor) deba@414: : MapParent(adaptor) {} deba@414: NodeMap(const Adaptor& adaptor, const Value& value) deba@414: : MapParent(adaptor, value) {} deba@414: deba@414: private: deba@414: NodeMap& operator=(const NodeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: NodeMap& operator=(const CMap& cmap) { deba@414: MapParent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: template deba@414: class ArcMap : public SubMapExtender > { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > MapParent; deba@414: deba@414: ArcMap(const Adaptor& adaptor) deba@414: : MapParent(adaptor) {} deba@414: ArcMap(const Adaptor& adaptor, const Value& value) deba@414: : MapParent(adaptor, value) {} deba@414: deba@414: private: deba@414: ArcMap& operator=(const ArcMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: ArcMap& operator=(const CMap& cmap) { deba@414: MapParent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: }; deba@414: deba@414: /// \ingroup graph_adaptors deba@414: /// deba@414: /// \brief A digraph adaptor for hiding nodes and arcs from a digraph. deba@414: /// deba@414: /// SubDigraphAdaptor shows the digraph with filtered node-set and deba@414: /// arc-set. If the \c checked parameter is true then it filters the arcset deba@414: /// to do not get invalid arcs without source or target. deba@414: /// Let \f$ G=(V, A) \f$ be a directed digraph deba@414: /// and suppose that the digraph instance \c g of type ListDigraph deba@414: /// implements \f$ G \f$. deba@414: /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp. deba@414: /// on the node-set and arc-set. deba@414: /// SubDigraphAdaptor<...>::NodeIt iterates deba@414: /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and deba@414: /// SubDigraphAdaptor<...>::ArcIt iterates deba@414: /// on the arc-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly, deba@414: /// SubDigraphAdaptor<...>::OutArcIt and deba@414: /// SubDigraphAdaptor<...>::InArcIt iterates deba@414: /// only on arcs leaving and entering a specific node which have true value. deba@414: /// deba@414: /// If the \c checked template parameter is false then we have to deba@414: /// note that the node-iterator cares only the filter on the deba@414: /// node-set, and the arc-iterator cares only the filter on the deba@414: /// arc-set. This way the arc-map should filter all arcs which's deba@414: /// source or target is filtered by the node-filter. deba@414: ///\code deba@414: /// typedef ListDigraph Digraph; deba@414: /// DIGRAPH_TYPEDEFS(Digraph); deba@414: /// Digraph g; deba@414: /// Node u=g.addNode(); //node of id 0 deba@414: /// Node v=g.addNode(); //node of id 1 deba@414: /// Arc a=g.addArc(u, v); //arc of id 0 deba@414: /// Arc f=g.addArc(v, u); //arc of id 1 deba@414: /// BoolNodeMap nm(g, true); deba@414: /// nm.set(u, false); deba@414: /// BoolArcMap am(g, true); deba@414: /// am.set(a, false); deba@414: /// typedef SubDigraphAdaptor SubGA; deba@414: /// SubGA ga(g, nm, am); deba@414: /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) deba@414: /// std::cout << g.id(n) << std::endl; deba@414: /// std::cout << ":-)" << std::endl; deba@414: /// for (SubGA::ArcIt a(ga); a!=INVALID; ++a) deba@414: /// std::cout << g.id(a) << std::endl; deba@414: ///\endcode deba@414: /// The output of the above code is the following. deba@414: ///\code deba@414: /// 1 deba@414: /// :-) deba@414: /// 1 deba@414: ///\endcode deba@414: /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to deba@414: /// \c Digraph::Node that is why \c g.id(n) can be applied. deba@414: /// deba@414: /// For other examples see also the documentation of deba@414: /// NodeSubDigraphAdaptor and ArcSubDigraphAdaptor. deba@414: template, deba@414: typename _ArcFilterMap = typename _Digraph::template ArcMap, deba@414: bool checked = true> deba@414: class SubDigraphAdaptor : deba@414: public DigraphAdaptorExtender< deba@414: SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, checked> > { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef _NodeFilterMap NodeFilterMap; deba@414: typedef _ArcFilterMap ArcFilterMap; deba@414: deba@414: typedef DigraphAdaptorExtender< deba@414: SubDigraphAdaptorBase > deba@414: Parent; deba@414: deba@414: protected: deba@414: SubDigraphAdaptor() { } deba@414: public: deba@414: deba@414: SubDigraphAdaptor(Digraph& digraph, NodeFilterMap& node_filter, deba@414: ArcFilterMap& arc_filter) { deba@414: setDigraph(digraph); deba@414: setNodeFilterMap(node_filter); deba@414: setArcFilterMap(arc_filter); deba@414: } deba@414: deba@414: }; deba@414: deba@414: /// \brief Just gives back a sub digraph adaptor deba@414: /// deba@414: /// Just gives back a sub digraph adaptor deba@414: template deba@414: SubDigraphAdaptor deba@414: subDigraphAdaptor(const Digraph& digraph, deba@414: NodeFilterMap& nfm, ArcFilterMap& afm) { deba@414: return SubDigraphAdaptor deba@414: (digraph, nfm, afm); deba@414: } deba@414: deba@414: template deba@414: SubDigraphAdaptor deba@414: subDigraphAdaptor(const Digraph& digraph, deba@414: NodeFilterMap& nfm, ArcFilterMap& afm) { deba@414: return SubDigraphAdaptor deba@414: (digraph, nfm, afm); deba@414: } deba@414: deba@414: template deba@414: SubDigraphAdaptor deba@414: subDigraphAdaptor(const Digraph& digraph, deba@414: NodeFilterMap& nfm, ArcFilterMap& afm) { deba@414: return SubDigraphAdaptor deba@414: (digraph, nfm, afm); deba@414: } deba@414: deba@414: template deba@414: SubDigraphAdaptor deba@414: subDigraphAdaptor(const Digraph& digraph, deba@414: NodeFilterMap& nfm, ArcFilterMap& afm) { deba@414: return SubDigraphAdaptor(digraph, nfm, afm); deba@414: } deba@414: deba@414: deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: ///\brief An adaptor for hiding nodes from a digraph. deba@414: /// deba@414: ///An adaptor for hiding nodes from a digraph. This adaptor deba@414: ///specializes SubDigraphAdaptor in the way that only the node-set deba@414: ///can be filtered. In usual case the checked parameter is true, we deba@414: ///get the induced subgraph. But if the checked parameter is false deba@414: ///then we can filter only isolated nodes. deba@414: template, deba@414: bool checked = true> deba@414: class NodeSubDigraphAdaptor : deba@414: public SubDigraphAdaptor<_Digraph, _NodeFilterMap, deba@414: ConstMap, checked> { deba@414: public: deba@414: deba@414: typedef _Digraph Digraph; deba@414: typedef _NodeFilterMap NodeFilterMap; deba@414: deba@414: typedef SubDigraphAdaptor, checked> deba@414: Parent; deba@414: deba@414: protected: deba@414: ConstMap const_true_map; deba@414: deba@414: NodeSubDigraphAdaptor() : const_true_map(true) { deba@414: Parent::setArcFilterMap(const_true_map); deba@414: } deba@414: deba@414: public: deba@414: deba@414: NodeSubDigraphAdaptor(Digraph& _digraph, NodeFilterMap& node_filter) : deba@414: Parent(), const_true_map(true) { deba@414: Parent::setDigraph(_digraph); deba@414: Parent::setNodeFilterMap(node_filter); deba@414: Parent::setArcFilterMap(const_true_map); deba@414: } deba@414: deba@414: }; deba@414: deba@414: deba@414: /// \brief Just gives back a \c NodeSubDigraphAdaptor deba@414: /// deba@414: /// Just gives back a \c NodeSubDigraphAdaptor deba@414: template deba@414: NodeSubDigraphAdaptor deba@414: nodeSubDigraphAdaptor(const Digraph& digraph, NodeFilterMap& nfm) { deba@414: return NodeSubDigraphAdaptor(digraph, nfm); deba@414: } deba@414: deba@414: template deba@414: NodeSubDigraphAdaptor deba@414: nodeSubDigraphAdaptor(const Digraph& digraph, const NodeFilterMap& nfm) { deba@414: return NodeSubDigraphAdaptor deba@414: (digraph, nfm); deba@414: } deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: ///\brief An adaptor for hiding arcs from a digraph. deba@414: /// deba@414: ///An adaptor for hiding arcs from a digraph. This adaptor deba@414: ///specializes SubDigraphAdaptor in the way that only the arc-set deba@414: ///can be filtered. The usefulness of this adaptor is demonstrated deba@414: ///in the problem of searching a maximum number of arc-disjoint deba@414: ///shortest paths between two nodes \c s and \c t. Shortest here deba@414: ///means being shortest w.r.t. non-negative arc-lengths. Note that deba@414: ///the comprehension of the presented solution need's some deba@414: ///elementary knowlarc from combinatorial optimization. deba@414: /// deba@414: ///If a single shortest path is to be searched between \c s and \c deba@414: ///t, then this can be done easily by applying the Dijkstra deba@414: ///algorithm. What happens, if a maximum number of arc-disjoint deba@414: ///shortest paths is to be computed. It can be proved that an arc deba@414: ///can be in a shortest path if and only if it is tight with respect deba@414: ///to the potential function computed by Dijkstra. Moreover, any deba@414: ///path containing only such arcs is a shortest one. Thus we have deba@414: ///to compute a maximum number of arc-disjoint paths between \c s deba@414: ///and \c t in the digraph which has arc-set all the tight arcs. The deba@414: ///computation will be demonstrated on the following digraph, which deba@414: ///is read from the dimacs file \c sub_digraph_adaptor_demo.dim. deba@414: ///The full source code is available in \ref deba@414: ///sub_digraph_adaptor_demo.cc. If you are interested in more demo deba@414: ///programs, you can use \ref dim_to_dot.cc to generate .dot files deba@414: ///from dimacs files. The .dot file of the following figure was deba@414: ///generated by the demo program \ref dim_to_dot.cc. deba@414: /// deba@414: ///\dot deba@414: ///didigraph lemon_dot_example { deba@414: ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; deba@414: ///n0 [ label="0 (s)" ]; deba@414: ///n1 [ label="1" ]; deba@414: ///n2 [ label="2" ]; deba@414: ///n3 [ label="3" ]; deba@414: ///n4 [ label="4" ]; deba@414: ///n5 [ label="5" ]; deba@414: ///n6 [ label="6 (t)" ]; deba@414: ///arc [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; deba@414: ///n5 -> n6 [ label="9, length:4" ]; deba@414: ///n4 -> n6 [ label="8, length:2" ]; deba@414: ///n3 -> n5 [ label="7, length:1" ]; deba@414: ///n2 -> n5 [ label="6, length:3" ]; deba@414: ///n2 -> n6 [ label="5, length:5" ]; deba@414: ///n2 -> n4 [ label="4, length:2" ]; deba@414: ///n1 -> n4 [ label="3, length:3" ]; deba@414: ///n0 -> n3 [ label="2, length:1" ]; deba@414: ///n0 -> n2 [ label="1, length:2" ]; deba@414: ///n0 -> n1 [ label="0, length:3" ]; deba@414: ///} deba@414: ///\enddot deba@414: /// deba@414: ///\code deba@414: ///Digraph g; deba@414: ///Node s, t; deba@414: ///LengthMap length(g); deba@414: /// deba@414: ///readDimacs(std::cin, g, length, s, t); deba@414: /// deba@414: ///cout << "arcs with lengths (of form id, source--length->target): " << endl; deba@414: ///for(ArcIt e(g); e!=INVALID; ++e) deba@414: /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--" deba@414: /// << length[e] << "->" << g.id(g.target(e)) << endl; deba@414: /// deba@414: ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl; deba@414: ///\endcode deba@414: ///Next, the potential function is computed with Dijkstra. deba@414: ///\code deba@414: ///typedef Dijkstra Dijkstra; deba@414: ///Dijkstra dijkstra(g, length); deba@414: ///dijkstra.run(s); deba@414: ///\endcode deba@414: ///Next, we consrtruct a map which filters the arc-set to the tight arcs. deba@414: ///\code deba@414: ///typedef TightArcFilterMap deba@414: /// TightArcFilter; deba@414: ///TightArcFilter tight_arc_filter(g, dijkstra.distMap(), length); deba@414: /// deba@414: ///typedef ArcSubDigraphAdaptor SubGA; deba@414: ///SubGA ga(g, tight_arc_filter); deba@414: ///\endcode deba@414: ///Then, the maximum nimber of arc-disjoint \c s-\c t paths are computed deba@414: ///with a max flow algorithm Preflow. deba@414: ///\code deba@414: ///ConstMap const_1_map(1); deba@414: ///Digraph::ArcMap flow(g, 0); deba@414: /// deba@414: ///Preflow, Digraph::ArcMap > deba@414: /// preflow(ga, const_1_map, s, t); deba@414: ///preflow.run(); deba@414: ///\endcode deba@414: ///Last, the output is: deba@414: ///\code deba@414: ///cout << "maximum number of arc-disjoint shortest path: " deba@414: /// << preflow.flowValue() << endl; deba@414: ///cout << "arcs of the maximum number of arc-disjoint shortest s-t paths: " deba@414: /// << endl; deba@414: ///for(ArcIt e(g); e!=INVALID; ++e) deba@414: /// if (preflow.flow(e)) deba@414: /// cout << " " << g.id(g.source(e)) << "--" deba@414: /// << length[e] << "->" << g.id(g.target(e)) << endl; deba@414: ///\endcode deba@414: ///The program has the following (expected :-)) output: deba@414: ///\code deba@414: ///arcs with lengths (of form id, source--length->target): deba@414: /// 9, 5--4->6 deba@414: /// 8, 4--2->6 deba@414: /// 7, 3--1->5 deba@414: /// 6, 2--3->5 deba@414: /// 5, 2--5->6 deba@414: /// 4, 2--2->4 deba@414: /// 3, 1--3->4 deba@414: /// 2, 0--1->3 deba@414: /// 1, 0--2->2 deba@414: /// 0, 0--3->1 deba@414: ///s: 0 t: 6 deba@414: ///maximum number of arc-disjoint shortest path: 2 deba@414: ///arcs of the maximum number of arc-disjoint shortest s-t paths: deba@414: /// 9, 5--4->6 deba@414: /// 8, 4--2->6 deba@414: /// 7, 3--1->5 deba@414: /// 4, 2--2->4 deba@414: /// 2, 0--1->3 deba@414: /// 1, 0--2->2 deba@414: ///\endcode deba@414: template deba@414: class ArcSubDigraphAdaptor : deba@414: public SubDigraphAdaptor<_Digraph, ConstMap, deba@414: _ArcFilterMap, false> { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef _ArcFilterMap ArcFilterMap; deba@414: deba@414: typedef SubDigraphAdaptor, deba@414: ArcFilterMap, false> Parent; deba@414: protected: deba@414: ConstMap const_true_map; deba@414: deba@414: ArcSubDigraphAdaptor() : const_true_map(true) { deba@414: Parent::setNodeFilterMap(const_true_map); deba@414: } deba@414: deba@414: public: deba@414: deba@414: ArcSubDigraphAdaptor(Digraph& digraph, ArcFilterMap& arc_filter) deba@414: : Parent(), const_true_map(true) { deba@414: Parent::setDigraph(digraph); deba@414: Parent::setNodeFilterMap(const_true_map); deba@414: Parent::setArcFilterMap(arc_filter); deba@414: } deba@414: deba@414: }; deba@414: deba@414: /// \brief Just gives back an arc sub digraph adaptor deba@414: /// deba@414: /// Just gives back an arc sub digraph adaptor deba@414: template deba@414: ArcSubDigraphAdaptor deba@414: arcSubDigraphAdaptor(const Digraph& digraph, ArcFilterMap& afm) { deba@414: return ArcSubDigraphAdaptor(digraph, afm); deba@414: } deba@414: deba@414: template deba@414: ArcSubDigraphAdaptor deba@414: arcSubDigraphAdaptor(const Digraph& digraph, const ArcFilterMap& afm) { deba@414: return ArcSubDigraphAdaptor deba@414: (digraph, afm); deba@414: } deba@414: deba@414: template deba@414: class UndirDigraphAdaptorBase { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef UndirDigraphAdaptorBase Adaptor; deba@414: deba@414: typedef True UndirectedTag; deba@414: deba@414: typedef typename Digraph::Arc Edge; deba@414: typedef typename Digraph::Node Node; deba@414: deba@414: class Arc : public Edge { deba@414: friend class UndirDigraphAdaptorBase; deba@414: protected: deba@414: bool _forward; deba@414: deba@414: Arc(const Edge& edge, bool forward) : deba@414: Edge(edge), _forward(forward) {} deba@414: deba@414: public: deba@414: Arc() {} deba@414: deba@414: Arc(Invalid) : Edge(INVALID), _forward(true) {} deba@414: deba@414: bool operator==(const Arc &other) const { deba@414: return _forward == other._forward && deba@414: static_cast(*this) == static_cast(other); deba@414: } deba@414: bool operator!=(const Arc &other) const { deba@414: return _forward != other._forward || deba@414: static_cast(*this) != static_cast(other); deba@414: } deba@414: bool operator<(const Arc &other) const { deba@414: return _forward < other._forward || deba@414: (_forward == other._forward && deba@414: static_cast(*this) < static_cast(other)); deba@414: } deba@414: }; deba@414: deba@414: deba@414: deba@414: void first(Node& n) const { deba@414: _digraph->first(n); deba@414: } deba@414: deba@414: void next(Node& n) const { deba@414: _digraph->next(n); deba@414: } deba@414: deba@414: void first(Arc& a) const { deba@414: _digraph->first(a); deba@414: a._forward = true; deba@414: } deba@414: deba@414: void next(Arc& a) const { deba@414: if (a._forward) { deba@414: a._forward = false; deba@414: } else { deba@414: _digraph->next(a); deba@414: a._forward = true; deba@414: } deba@414: } deba@414: deba@414: void first(Edge& e) const { deba@414: _digraph->first(e); deba@414: } deba@414: deba@414: void next(Edge& e) const { deba@414: _digraph->next(e); deba@414: } deba@414: deba@414: void firstOut(Arc& a, const Node& n) const { deba@414: _digraph->firstIn(a, n); deba@414: if( static_cast(a) != INVALID ) { deba@414: a._forward = false; deba@414: } else { deba@414: _digraph->firstOut(a, n); deba@414: a._forward = true; deba@414: } deba@414: } deba@414: void nextOut(Arc &a) const { deba@414: if (!a._forward) { deba@414: Node n = _digraph->target(a); deba@414: _digraph->nextIn(a); deba@414: if (static_cast(a) == INVALID ) { deba@414: _digraph->firstOut(a, n); deba@414: a._forward = true; deba@414: } deba@414: } deba@414: else { deba@414: _digraph->nextOut(a); deba@414: } deba@414: } deba@414: deba@414: void firstIn(Arc &a, const Node &n) const { deba@414: _digraph->firstOut(a, n); deba@414: if (static_cast(a) != INVALID ) { deba@414: a._forward = false; deba@414: } else { deba@414: _digraph->firstIn(a, n); deba@414: a._forward = true; deba@414: } deba@414: } deba@414: void nextIn(Arc &a) const { deba@414: if (!a._forward) { deba@414: Node n = _digraph->source(a); deba@414: _digraph->nextOut(a); deba@414: if( static_cast(a) == INVALID ) { deba@414: _digraph->firstIn(a, n); deba@414: a._forward = true; deba@414: } deba@414: } deba@414: else { deba@414: _digraph->nextIn(a); deba@414: } deba@414: } deba@414: deba@414: void firstInc(Edge &e, bool &d, const Node &n) const { deba@414: d = true; deba@414: _digraph->firstOut(e, n); deba@414: if (e != INVALID) return; deba@414: d = false; deba@414: _digraph->firstIn(e, n); deba@414: } deba@414: deba@414: void nextInc(Edge &e, bool &d) const { deba@414: if (d) { deba@414: Node s = _digraph->source(e); deba@414: _digraph->nextOut(e); deba@414: if (e != INVALID) return; deba@414: d = false; deba@414: _digraph->firstIn(e, s); deba@414: } else { deba@414: _digraph->nextIn(e); deba@414: } deba@414: } deba@414: deba@414: Node u(const Edge& e) const { deba@414: return _digraph->source(e); deba@414: } deba@414: deba@414: Node v(const Edge& e) const { deba@414: return _digraph->target(e); deba@414: } deba@414: deba@414: Node source(const Arc &a) const { deba@414: return a._forward ? _digraph->source(a) : _digraph->target(a); deba@414: } deba@414: deba@414: Node target(const Arc &a) const { deba@414: return a._forward ? _digraph->target(a) : _digraph->source(a); deba@414: } deba@414: deba@414: static Arc direct(const Edge &e, bool d) { deba@414: return Arc(e, d); deba@414: } deba@414: Arc direct(const Edge &e, const Node& n) const { deba@414: return Arc(e, _digraph->source(e) == n); deba@414: } deba@414: deba@414: static bool direction(const Arc &a) { return a._forward; } deba@414: deba@414: Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } deba@414: Arc arcFromId(int ix) const { deba@414: return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); deba@414: } deba@414: Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } deba@414: deba@414: int id(const Node &n) const { return _digraph->id(n); } deba@414: int id(const Arc &a) const { deba@414: return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); deba@414: } deba@414: int id(const Edge &e) const { return _digraph->id(e); } deba@414: deba@414: int maxNodeId() const { return _digraph->maxNodeId(); } deba@414: int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } deba@414: int maxEdgeId() const { return _digraph->maxArcId(); } deba@414: deba@414: Node addNode() { return _digraph->addNode(); } deba@414: Edge addEdge(const Node& u, const Node& v) { deba@414: return _digraph->addArc(u, v); deba@414: } deba@414: deba@414: void erase(const Node& i) { _digraph->erase(i); } deba@414: void erase(const Edge& i) { _digraph->erase(i); } deba@414: deba@414: void clear() { _digraph->clear(); } deba@414: deba@414: typedef NodeNumTagIndicator NodeNumTag; deba@414: int nodeNum() const { return 2 * _digraph->arcNum(); } deba@414: typedef EdgeNumTagIndicator EdgeNumTag; deba@414: int arcNum() const { return 2 * _digraph->arcNum(); } deba@414: int edgeNum() const { return _digraph->arcNum(); } deba@414: deba@414: typedef FindEdgeTagIndicator FindEdgeTag; deba@414: Arc findArc(Node s, Node t, Arc p = INVALID) const { deba@414: if (p == INVALID) { deba@414: Edge arc = _digraph->findArc(s, t); deba@414: if (arc != INVALID) return direct(arc, true); deba@414: arc = _digraph->findArc(t, s); deba@414: if (arc != INVALID) return direct(arc, false); deba@414: } else if (direction(p)) { deba@414: Edge arc = _digraph->findArc(s, t, p); deba@414: if (arc != INVALID) return direct(arc, true); deba@414: arc = _digraph->findArc(t, s); deba@414: if (arc != INVALID) return direct(arc, false); deba@414: } else { deba@414: Edge arc = _digraph->findArc(t, s, p); deba@414: if (arc != INVALID) return direct(arc, false); deba@414: } deba@414: return INVALID; deba@414: } deba@414: deba@414: Edge findEdge(Node s, Node t, Edge p = INVALID) const { deba@414: if (s != t) { deba@414: if (p == INVALID) { deba@414: Edge arc = _digraph->findArc(s, t); deba@414: if (arc != INVALID) return arc; deba@414: arc = _digraph->findArc(t, s); deba@414: if (arc != INVALID) return arc; deba@414: } else if (_digraph->s(p) == s) { deba@414: Edge arc = _digraph->findArc(s, t, p); deba@414: if (arc != INVALID) return arc; deba@414: arc = _digraph->findArc(t, s); deba@414: if (arc != INVALID) return arc; deba@414: } else { deba@414: Edge arc = _digraph->findArc(t, s, p); deba@414: if (arc != INVALID) return arc; deba@414: } deba@414: } else { deba@414: return _digraph->findArc(s, t, p); deba@414: } deba@414: return INVALID; deba@414: } deba@414: deba@414: private: deba@414: deba@414: template deba@414: class ArcMapBase { deba@414: private: deba@414: deba@414: typedef typename Digraph::template ArcMap<_Value> MapImpl; deba@414: deba@414: public: deba@414: deba@414: typedef typename MapTraits::ReferenceMapTag ReferenceMapTag; deba@414: deba@414: typedef _Value Value; deba@414: typedef Arc Key; deba@414: deba@414: ArcMapBase(const Adaptor& adaptor) : deba@414: _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} deba@414: deba@414: ArcMapBase(const Adaptor& adaptor, const Value& v) deba@414: : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {} deba@414: deba@414: void set(const Arc& a, const Value& v) { deba@414: if (direction(a)) { deba@414: _forward.set(a, v); deba@414: } else { deba@414: _backward.set(a, v); deba@414: } deba@414: } deba@414: deba@414: typename MapTraits::ConstReturnValue deba@414: operator[](const Arc& a) const { deba@414: if (direction(a)) { deba@414: return _forward[a]; deba@414: } else { deba@414: return _backward[a]; deba@414: } deba@414: } deba@414: deba@414: typename MapTraits::ReturnValue deba@414: operator[](const Arc& a) { deba@414: if (direction(a)) { deba@414: return _forward[a]; deba@414: } else { deba@414: return _backward[a]; deba@414: } deba@414: } deba@414: deba@414: protected: deba@414: deba@414: MapImpl _forward, _backward; deba@414: deba@414: }; deba@414: deba@414: public: deba@414: deba@414: template deba@414: class NodeMap : public Digraph::template NodeMap<_Value> { deba@414: public: deba@414: deba@414: typedef _Value Value; deba@414: typedef typename Digraph::template NodeMap Parent; deba@414: deba@414: explicit NodeMap(const Adaptor& adaptor) deba@414: : Parent(*adaptor._digraph) {} deba@414: deba@414: NodeMap(const Adaptor& adaptor, const _Value& value) deba@414: : Parent(*adaptor._digraph, value) { } deba@414: deba@414: private: deba@414: NodeMap& operator=(const NodeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: NodeMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: deba@414: }; deba@414: deba@414: template deba@414: class ArcMap deba@414: : public SubMapExtender > deba@414: { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > Parent; deba@414: deba@414: ArcMap(const Adaptor& adaptor) deba@414: : Parent(adaptor) {} deba@414: deba@414: ArcMap(const Adaptor& adaptor, const Value& value) deba@414: : Parent(adaptor, value) {} deba@414: deba@414: private: deba@414: ArcMap& operator=(const ArcMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: ArcMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: template deba@414: class EdgeMap : public Digraph::template ArcMap<_Value> { deba@414: public: deba@414: deba@414: typedef _Value Value; deba@414: typedef typename Digraph::template ArcMap Parent; deba@414: deba@414: explicit EdgeMap(const Adaptor& adaptor) deba@414: : Parent(*adaptor._digraph) {} deba@414: deba@414: EdgeMap(const Adaptor& adaptor, const Value& value) deba@414: : Parent(*adaptor._digraph, value) {} deba@414: deba@414: private: deba@414: EdgeMap& operator=(const EdgeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: EdgeMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: deba@414: }; deba@414: deba@414: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@414: NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } deba@414: deba@414: protected: deba@414: deba@414: UndirDigraphAdaptorBase() : _digraph(0) {} deba@414: deba@414: Digraph* _digraph; deba@414: deba@414: void setDigraph(Digraph& digraph) { deba@414: _digraph = &digraph; deba@414: } deba@414: deba@414: }; deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: /// \brief An graph is made from a directed digraph by an adaptor deba@414: /// deba@414: /// This adaptor makes an undirected graph from a directed deba@414: /// digraph. All arc of the underlying will be showed in the adaptor deba@414: /// as an edge. Let's see an informal example about using deba@414: /// this adaptor: deba@414: /// deba@414: /// There is a network of the streets of a town. Of course there are deba@414: /// some one-way street in the town hence the network is a directed deba@414: /// one. There is a crazy driver who go oppositely in the one-way deba@414: /// street without moral sense. Of course he can pass this streets deba@414: /// slower than the regular way, in fact his speed is half of the deba@414: /// normal speed. How long should he drive to get from a source deba@414: /// point to the target? Let see the example code which calculate it: deba@414: /// deba@414: /// \todo BadCode, SimpleMap does no exists deba@414: ///\code deba@414: /// typedef UndirDigraphAdaptor Graph; deba@414: /// Graph graph(digraph); deba@414: /// deba@414: /// typedef SimpleMap FLengthMap; deba@414: /// FLengthMap flength(length); deba@414: /// deba@414: /// typedef ScaleMap RLengthMap; deba@414: /// RLengthMap rlength(length, 2.0); deba@414: /// deba@414: /// typedef Graph::CombinedArcMap ULengthMap; deba@414: /// ULengthMap ulength(flength, rlength); deba@414: /// deba@414: /// Dijkstra dijkstra(graph, ulength); deba@414: /// std::cout << "Driving time : " << dijkstra.run(src, trg) << std::endl; deba@414: ///\endcode deba@414: /// deba@414: /// The combined arc map makes the length map for the undirected deba@414: /// graph. It is created from a forward and reverse map. The forward deba@414: /// map is created from the original length map with a SimpleMap deba@414: /// adaptor which just makes a read-write map from the reference map deba@414: /// i.e. it forgets that it can be return reference to values. The deba@414: /// reverse map is just the scaled original map with the ScaleMap deba@414: /// adaptor. The combination solves that passing the reverse way deba@414: /// takes double time than the original. To get the driving time we deba@414: /// run the dijkstra algorithm on the graph. deba@414: template deba@414: class UndirDigraphAdaptor deba@414: : public GraphAdaptorExtender > { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef GraphAdaptorExtender > Parent; deba@414: protected: deba@414: UndirDigraphAdaptor() { } deba@414: public: deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// Constructor deba@414: UndirDigraphAdaptor(_Digraph& _digraph) { deba@414: setDigraph(_digraph); deba@414: } deba@414: deba@414: /// \brief ArcMap combined from two original ArcMap deba@414: /// deba@414: /// This class adapts two original digraph ArcMap to deba@414: /// get an arc map on the adaptor. deba@414: template deba@414: class CombinedArcMap { deba@414: public: deba@414: deba@414: typedef _ForwardMap ForwardMap; deba@414: typedef _BackwardMap BackwardMap; deba@414: deba@414: typedef typename MapTraits::ReferenceMapTag ReferenceMapTag; deba@414: deba@414: typedef typename ForwardMap::Value Value; deba@414: typedef typename Parent::Arc Key; deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// Constructor deba@414: CombinedArcMap() : _forward(0), _backward(0) {} deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// Constructor deba@414: CombinedArcMap(ForwardMap& forward, BackwardMap& backward) deba@414: : _forward(&forward), _backward(&backward) {} deba@414: deba@414: deba@414: /// \brief Sets the value associated with a key. deba@414: /// deba@414: /// Sets the value associated with a key. deba@414: void set(const Key& e, const Value& a) { deba@414: if (Parent::direction(e)) { deba@414: _forward->set(e, a); deba@414: } else { deba@414: _backward->set(e, a); deba@414: } deba@414: } deba@414: deba@414: /// \brief Returns the value associated with a key. deba@414: /// deba@414: /// Returns the value associated with a key. deba@414: typename MapTraits::ConstReturnValue deba@414: operator[](const Key& e) const { deba@414: if (Parent::direction(e)) { deba@414: return (*_forward)[e]; deba@414: } else { deba@414: return (*_backward)[e]; deba@414: } deba@414: } deba@414: deba@414: /// \brief Returns the value associated with a key. deba@414: /// deba@414: /// Returns the value associated with a key. deba@414: typename MapTraits::ReturnValue deba@414: operator[](const Key& e) { deba@414: if (Parent::direction(e)) { deba@414: return (*_forward)[e]; deba@414: } else { deba@414: return (*_backward)[e]; deba@414: } deba@414: } deba@414: deba@414: /// \brief Sets the forward map deba@414: /// deba@414: /// Sets the forward map deba@414: void setForwardMap(ForwardMap& forward) { deba@414: _forward = &forward; deba@414: } deba@414: deba@414: /// \brief Sets the backward map deba@414: /// deba@414: /// Sets the backward map deba@414: void setBackwardMap(BackwardMap& backward) { deba@414: _backward = &backward; deba@414: } deba@414: deba@414: protected: deba@414: deba@414: ForwardMap* _forward; deba@414: BackwardMap* _backward; deba@414: deba@414: }; deba@414: deba@414: }; deba@414: deba@414: /// \brief Just gives back an undir digraph adaptor deba@414: /// deba@414: /// Just gives back an undir digraph adaptor deba@414: template deba@414: UndirDigraphAdaptor deba@414: undirDigraphAdaptor(const Digraph& digraph) { deba@414: return UndirDigraphAdaptor(digraph); deba@414: } deba@414: deba@414: template, deba@414: typename _FlowMap = _CapacityMap, deba@414: typename _Tolerance = Tolerance > deba@414: class ResForwardFilter { deba@414: public: deba@414: deba@414: typedef _Digraph Digraph; deba@414: typedef _CapacityMap CapacityMap; deba@414: typedef _FlowMap FlowMap; deba@414: typedef _Tolerance Tolerance; deba@414: deba@414: typedef typename Digraph::Arc Key; deba@414: typedef bool Value; deba@414: deba@414: private: deba@414: deba@414: const CapacityMap* _capacity; deba@414: const FlowMap* _flow; deba@414: Tolerance _tolerance; deba@414: public: deba@414: deba@414: ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, deba@414: const Tolerance& tolerance = Tolerance()) deba@414: : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } deba@414: deba@414: ResForwardFilter(const Tolerance& tolerance = Tolerance()) deba@414: : _capacity(0), _flow(0), _tolerance(tolerance) { } deba@414: deba@414: void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } deba@414: void setFlow(const FlowMap& flow) { _flow = &flow; } deba@414: deba@414: bool operator[](const typename Digraph::Arc& a) const { deba@414: return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); deba@414: } deba@414: }; deba@414: deba@414: template, deba@414: typename _FlowMap = _CapacityMap, deba@414: typename _Tolerance = Tolerance > deba@414: class ResBackwardFilter { deba@414: public: deba@414: deba@414: typedef _Digraph Digraph; deba@414: typedef _CapacityMap CapacityMap; deba@414: typedef _FlowMap FlowMap; deba@414: typedef _Tolerance Tolerance; deba@414: deba@414: typedef typename Digraph::Arc Key; deba@414: typedef bool Value; deba@414: deba@414: private: deba@414: deba@414: const CapacityMap* _capacity; deba@414: const FlowMap* _flow; deba@414: Tolerance _tolerance; deba@414: deba@414: public: deba@414: deba@414: ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, deba@414: const Tolerance& tolerance = Tolerance()) deba@414: : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } deba@414: ResBackwardFilter(const Tolerance& tolerance = Tolerance()) deba@414: : _capacity(0), _flow(0), _tolerance(tolerance) { } deba@414: deba@414: void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } deba@414: void setFlow(const FlowMap& flow) { _flow = &flow; } deba@414: deba@414: bool operator[](const typename Digraph::Arc& a) const { deba@414: return _tolerance.positive((*_flow)[a]); deba@414: } deba@414: }; deba@414: deba@414: deba@414: ///\ingroup graph_adaptors deba@414: /// deba@414: ///\brief An adaptor for composing the residual graph for directed deba@414: ///flow and circulation problems. deba@414: /// deba@414: ///An adaptor for composing the residual graph for directed flow and deba@414: ///circulation problems. Let \f$ G=(V, A) \f$ be a directed digraph deba@414: ///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F deba@414: ///\f$, be functions on the arc-set. deba@414: /// deba@414: ///In the appications of ResDigraphAdaptor, \f$ f \f$ usually stands deba@414: ///for a flow and \f$ c \f$ for a capacity function. Suppose that a deba@414: ///graph instance \c g of type \c ListDigraph implements \f$ G \f$. deba@414: /// deba@414: ///\code deba@414: /// ListDigraph g; deba@414: ///\endcode deba@414: /// deba@414: ///Then ResDigraphAdaptor implements the digraph structure with deba@414: /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} deba@414: /// \f$, where \f$ A_{forward}=\{uv : uv\in A, f(uv)0\} \f$, i.e. the so deba@414: /// called residual graph. When we take the union \f$ deba@414: /// A_{forward}\cup A_{backward} \f$, multilicities are counted, deba@414: /// i.e. if an arc is in both \f$ A_{forward} \f$ and \f$ deba@414: /// A_{backward} \f$, then in the adaptor it appears twice. The deba@414: /// following code shows how such an instance can be constructed. deba@414: /// deba@414: ///\code deba@414: /// typedef ListDigraph Digraph; deba@414: /// IntArcMap f(g), c(g); deba@414: /// ResDigraphAdaptor ga(g); deba@414: ///\endcode deba@414: template, deba@414: typename _FlowMap = _CapacityMap, deba@414: typename _Tolerance = Tolerance > deba@414: class ResDigraphAdaptor : deba@414: public ArcSubDigraphAdaptor< deba@414: UndirDigraphAdaptor, deba@414: typename UndirDigraphAdaptor::template CombinedArcMap< deba@414: ResForwardFilter, deba@414: ResBackwardFilter > > { deba@414: public: deba@414: deba@414: typedef _Digraph Digraph; deba@414: typedef _CapacityMap CapacityMap; deba@414: typedef _FlowMap FlowMap; deba@414: typedef _Tolerance Tolerance; deba@414: deba@414: typedef typename CapacityMap::Value Value; deba@414: typedef ResDigraphAdaptor Adaptor; deba@414: deba@414: protected: deba@414: deba@414: typedef UndirDigraphAdaptor UndirDigraph; deba@414: deba@414: typedef ResForwardFilter deba@414: ForwardFilter; deba@414: deba@414: typedef ResBackwardFilter deba@414: BackwardFilter; deba@414: deba@414: typedef typename UndirDigraph:: deba@414: template CombinedArcMap ArcFilter; deba@414: deba@414: typedef ArcSubDigraphAdaptor Parent; deba@414: deba@414: const CapacityMap* _capacity; deba@414: FlowMap* _flow; deba@414: deba@414: UndirDigraph _graph; deba@414: ForwardFilter _forward_filter; deba@414: BackwardFilter _backward_filter; deba@414: ArcFilter _arc_filter; deba@414: deba@414: void setCapacityMap(const CapacityMap& capacity) { deba@414: _capacity = &capacity; deba@414: _forward_filter.setCapacity(capacity); deba@414: _backward_filter.setCapacity(capacity); deba@414: } deba@414: deba@414: void setFlowMap(FlowMap& flow) { deba@414: _flow = &flow; deba@414: _forward_filter.setFlow(flow); deba@414: _backward_filter.setFlow(flow); deba@414: } deba@414: deba@414: public: deba@414: deba@414: /// \brief Constructor of the residual digraph. deba@414: /// deba@414: /// Constructor of the residual graph. The parameters are the digraph type, deba@414: /// the flow map, the capacity map and a tolerance object. deba@414: ResDigraphAdaptor(const Digraph& digraph, const CapacityMap& capacity, deba@414: FlowMap& flow, const Tolerance& tolerance = Tolerance()) deba@414: : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph), deba@414: _forward_filter(capacity, flow, tolerance), deba@414: _backward_filter(capacity, flow, tolerance), deba@414: _arc_filter(_forward_filter, _backward_filter) deba@414: { deba@414: Parent::setDigraph(_graph); deba@414: Parent::setArcFilterMap(_arc_filter); deba@414: } deba@414: deba@414: typedef typename Parent::Arc Arc; deba@414: deba@414: /// \brief Gives back the residual capacity of the arc. deba@414: /// deba@414: /// Gives back the residual capacity of the arc. deba@414: Value rescap(const Arc& arc) const { deba@414: if (UndirDigraph::direction(arc)) { deba@414: return (*_capacity)[arc] - (*_flow)[arc]; deba@414: } else { deba@414: return (*_flow)[arc]; deba@414: } deba@414: } deba@414: deba@414: /// \brief Augment on the given arc in the residual digraph. deba@414: /// deba@414: /// Augment on the given arc in the residual digraph. It increase deba@414: /// or decrease the flow on the original arc depend on the direction deba@414: /// of the residual arc. deba@414: void augment(const Arc& e, const Value& a) const { deba@414: if (UndirDigraph::direction(e)) { deba@414: _flow->set(e, (*_flow)[e] + a); deba@414: } else { deba@414: _flow->set(e, (*_flow)[e] - a); deba@414: } deba@414: } deba@414: deba@414: /// \brief Returns the direction of the arc. deba@414: /// deba@414: /// Returns true when the arc is same oriented as the original arc. deba@414: static bool forward(const Arc& e) { deba@414: return UndirDigraph::direction(e); deba@414: } deba@414: deba@414: /// \brief Returns the direction of the arc. deba@414: /// deba@414: /// Returns true when the arc is opposite oriented as the original arc. deba@414: static bool backward(const Arc& e) { deba@414: return !UndirDigraph::direction(e); deba@414: } deba@414: deba@414: /// \brief Gives back the forward oriented residual arc. deba@414: /// deba@414: /// Gives back the forward oriented residual arc. deba@414: static Arc forward(const typename Digraph::Arc& e) { deba@414: return UndirDigraph::direct(e, true); deba@414: } deba@414: deba@414: /// \brief Gives back the backward oriented residual arc. deba@414: /// deba@414: /// Gives back the backward oriented residual arc. deba@414: static Arc backward(const typename Digraph::Arc& e) { deba@414: return UndirDigraph::direct(e, false); deba@414: } deba@414: deba@414: /// \brief Residual capacity map. deba@414: /// deba@414: /// In generic residual digraphs the residual capacity can be obtained deba@414: /// as a map. deba@414: class ResCap { deba@414: protected: deba@414: const Adaptor* _adaptor; deba@414: public: deba@414: typedef Arc Key; deba@414: typedef typename _CapacityMap::Value Value; deba@414: deba@414: ResCap(const Adaptor& adaptor) : _adaptor(&adaptor) {} deba@414: deba@414: Value operator[](const Arc& e) const { deba@414: return _adaptor->rescap(e); deba@414: } deba@414: deba@414: }; deba@414: deba@414: }; deba@414: deba@414: /// \brief Base class for split digraph adaptor deba@414: /// deba@414: /// Base class of split digraph adaptor. In most case you do not need to deba@414: /// use it directly but the documented member functions of this class can deba@414: /// be used with the SplitDigraphAdaptor class. deba@414: /// \sa SplitDigraphAdaptor deba@414: template deba@414: class SplitDigraphAdaptorBase { deba@414: public: deba@414: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorBase Parent; deba@414: typedef SplitDigraphAdaptorBase Adaptor; deba@414: deba@414: typedef typename Digraph::Node DigraphNode; deba@414: typedef typename Digraph::Arc DigraphArc; deba@414: deba@414: class Node; deba@414: class Arc; deba@414: deba@414: private: deba@414: deba@414: template class NodeMapBase; deba@414: template class ArcMapBase; deba@414: deba@414: public: deba@414: deba@414: class Node : public DigraphNode { deba@414: friend class SplitDigraphAdaptorBase; deba@414: template friend class NodeMapBase; deba@414: private: deba@414: deba@414: bool _in; deba@414: Node(DigraphNode node, bool in) deba@414: : DigraphNode(node), _in(in) {} deba@414: deba@414: public: deba@414: deba@414: Node() {} deba@414: Node(Invalid) : DigraphNode(INVALID), _in(true) {} deba@414: deba@414: bool operator==(const Node& node) const { deba@414: return DigraphNode::operator==(node) && _in == node._in; deba@414: } deba@414: deba@414: bool operator!=(const Node& node) const { deba@414: return !(*this == node); deba@414: } deba@414: deba@414: bool operator<(const Node& node) const { deba@414: return DigraphNode::operator<(node) || deba@414: (DigraphNode::operator==(node) && _in < node._in); deba@414: } deba@414: }; deba@414: deba@414: class Arc { deba@414: friend class SplitDigraphAdaptorBase; deba@414: template friend class ArcMapBase; deba@414: private: deba@414: typedef BiVariant ArcImpl; deba@414: deba@414: explicit Arc(const DigraphArc& arc) : _item(arc) {} deba@414: explicit Arc(const DigraphNode& node) : _item(node) {} deba@414: deba@414: ArcImpl _item; deba@414: deba@414: public: deba@414: Arc() {} deba@414: Arc(Invalid) : _item(DigraphArc(INVALID)) {} deba@414: deba@414: bool operator==(const Arc& arc) const { deba@414: if (_item.firstState()) { deba@414: if (arc._item.firstState()) { deba@414: return _item.first() == arc._item.first(); deba@414: } deba@414: } else { deba@414: if (arc._item.secondState()) { deba@414: return _item.second() == arc._item.second(); deba@414: } deba@414: } deba@414: return false; deba@414: } deba@414: deba@414: bool operator!=(const Arc& arc) const { deba@414: return !(*this == arc); deba@414: } deba@414: deba@414: bool operator<(const Arc& arc) const { deba@414: if (_item.firstState()) { deba@414: if (arc._item.firstState()) { deba@414: return _item.first() < arc._item.first(); deba@414: } deba@414: return false; deba@414: } else { deba@414: if (arc._item.secondState()) { deba@414: return _item.second() < arc._item.second(); deba@414: } deba@414: return true; deba@414: } deba@414: } deba@414: deba@414: operator DigraphArc() const { return _item.first(); } deba@414: operator DigraphNode() const { return _item.second(); } deba@414: deba@414: }; deba@414: deba@414: void first(Node& n) const { deba@414: _digraph->first(n); deba@414: n._in = true; deba@414: } deba@414: deba@414: void next(Node& n) const { deba@414: if (n._in) { deba@414: n._in = false; deba@414: } else { deba@414: n._in = true; deba@414: _digraph->next(n); deba@414: } deba@414: } deba@414: deba@414: void first(Arc& e) const { deba@414: e._item.setSecond(); deba@414: _digraph->first(e._item.second()); deba@414: if (e._item.second() == INVALID) { deba@414: e._item.setFirst(); deba@414: _digraph->first(e._item.first()); deba@414: } deba@414: } deba@414: deba@414: void next(Arc& e) const { deba@414: if (e._item.secondState()) { deba@414: _digraph->next(e._item.second()); deba@414: if (e._item.second() == INVALID) { deba@414: e._item.setFirst(); deba@414: _digraph->first(e._item.first()); deba@414: } deba@414: } else { deba@414: _digraph->next(e._item.first()); deba@414: } deba@414: } deba@414: deba@414: void firstOut(Arc& e, const Node& n) const { deba@414: if (n._in) { deba@414: e._item.setSecond(n); deba@414: } else { deba@414: e._item.setFirst(); deba@414: _digraph->firstOut(e._item.first(), n); deba@414: } deba@414: } deba@414: deba@414: void nextOut(Arc& e) const { deba@414: if (!e._item.firstState()) { deba@414: e._item.setFirst(INVALID); deba@414: } else { deba@414: _digraph->nextOut(e._item.first()); deba@414: } deba@414: } deba@414: deba@414: void firstIn(Arc& e, const Node& n) const { deba@414: if (!n._in) { deba@414: e._item.setSecond(n); deba@414: } else { deba@414: e._item.setFirst(); deba@414: _digraph->firstIn(e._item.first(), n); deba@414: } deba@414: } deba@414: deba@414: void nextIn(Arc& e) const { deba@414: if (!e._item.firstState()) { deba@414: e._item.setFirst(INVALID); deba@414: } else { deba@414: _digraph->nextIn(e._item.first()); deba@414: } deba@414: } deba@414: deba@414: Node source(const Arc& e) const { deba@414: if (e._item.firstState()) { deba@414: return Node(_digraph->source(e._item.first()), false); deba@414: } else { deba@414: return Node(e._item.second(), true); deba@414: } deba@414: } deba@414: deba@414: Node target(const Arc& e) const { deba@414: if (e._item.firstState()) { deba@414: return Node(_digraph->target(e._item.first()), true); deba@414: } else { deba@414: return Node(e._item.second(), false); deba@414: } deba@414: } deba@414: deba@414: int id(const Node& n) const { deba@414: return (_digraph->id(n) << 1) | (n._in ? 0 : 1); deba@414: } deba@414: Node nodeFromId(int ix) const { deba@414: return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); deba@414: } deba@414: int maxNodeId() const { deba@414: return 2 * _digraph->maxNodeId() + 1; deba@414: } deba@414: deba@414: int id(const Arc& e) const { deba@414: if (e._item.firstState()) { deba@414: return _digraph->id(e._item.first()) << 1; deba@414: } else { deba@414: return (_digraph->id(e._item.second()) << 1) | 1; deba@414: } deba@414: } deba@414: Arc arcFromId(int ix) const { deba@414: if ((ix & 1) == 0) { deba@414: return Arc(_digraph->arcFromId(ix >> 1)); deba@414: } else { deba@414: return Arc(_digraph->nodeFromId(ix >> 1)); deba@414: } deba@414: } deba@414: int maxArcId() const { deba@414: return std::max(_digraph->maxNodeId() << 1, deba@414: (_digraph->maxArcId() << 1) | 1); deba@414: } deba@414: deba@414: /// \brief Returns true when the node is in-node. deba@414: /// deba@414: /// Returns true when the node is in-node. deba@414: static bool inNode(const Node& n) { deba@414: return n._in; deba@414: } deba@414: deba@414: /// \brief Returns true when the node is out-node. deba@414: /// deba@414: /// Returns true when the node is out-node. deba@414: static bool outNode(const Node& n) { deba@414: return !n._in; deba@414: } deba@414: deba@414: /// \brief Returns true when the arc is arc in the original digraph. deba@414: /// deba@414: /// Returns true when the arc is arc in the original digraph. deba@414: static bool origArc(const Arc& e) { deba@414: return e._item.firstState(); deba@414: } deba@414: deba@414: /// \brief Returns true when the arc binds an in-node and an out-node. deba@414: /// deba@414: /// Returns true when the arc binds an in-node and an out-node. deba@414: static bool bindArc(const Arc& e) { deba@414: return e._item.secondState(); deba@414: } deba@414: deba@414: /// \brief Gives back the in-node created from the \c node. deba@414: /// deba@414: /// Gives back the in-node created from the \c node. deba@414: static Node inNode(const DigraphNode& n) { deba@414: return Node(n, true); deba@414: } deba@414: deba@414: /// \brief Gives back the out-node created from the \c node. deba@414: /// deba@414: /// Gives back the out-node created from the \c node. deba@414: static Node outNode(const DigraphNode& n) { deba@414: return Node(n, false); deba@414: } deba@414: deba@414: /// \brief Gives back the arc binds the two part of the node. deba@414: /// deba@414: /// Gives back the arc binds the two part of the node. deba@414: static Arc arc(const DigraphNode& n) { deba@414: return Arc(n); deba@414: } deba@414: deba@414: /// \brief Gives back the arc of the original arc. deba@414: /// deba@414: /// Gives back the arc of the original arc. deba@414: static Arc arc(const DigraphArc& e) { deba@414: return Arc(e); deba@414: } deba@414: deba@414: typedef True NodeNumTag; deba@414: deba@414: int nodeNum() const { deba@414: return 2 * countNodes(*_digraph); deba@414: } deba@414: deba@414: typedef True EdgeNumTag; deba@414: int arcNum() const { deba@414: return countArcs(*_digraph) + countNodes(*_digraph); deba@414: } deba@414: deba@414: typedef True FindEdgeTag; deba@414: Arc findArc(const Node& u, const Node& v, deba@414: const Arc& prev = INVALID) const { deba@414: if (inNode(u)) { deba@414: if (outNode(v)) { deba@414: if (static_cast(u) == deba@414: static_cast(v) && prev == INVALID) { deba@414: return Arc(u); deba@414: } deba@414: } deba@414: } else { deba@414: if (inNode(v)) { deba@414: return Arc(::lemon::findArc(*_digraph, u, v, prev)); deba@414: } deba@414: } deba@414: return INVALID; deba@414: } deba@414: deba@414: private: deba@414: deba@414: template deba@414: class NodeMapBase deba@414: : public MapTraits > { deba@414: typedef typename Parent::template NodeMap<_Value> NodeImpl; deba@414: public: deba@414: typedef Node Key; deba@414: typedef _Value Value; deba@414: deba@414: NodeMapBase(const Adaptor& adaptor) deba@414: : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} deba@414: NodeMapBase(const Adaptor& adaptor, const Value& value) deba@414: : _in_map(*adaptor._digraph, value), deba@414: _out_map(*adaptor._digraph, value) {} deba@414: deba@414: void set(const Node& key, const Value& val) { deba@414: if (Adaptor::inNode(key)) { _in_map.set(key, val); } deba@414: else {_out_map.set(key, val); } deba@414: } deba@414: deba@414: typename MapTraits::ReturnValue deba@414: operator[](const Node& key) { deba@414: if (Adaptor::inNode(key)) { return _in_map[key]; } deba@414: else { return _out_map[key]; } deba@414: } deba@414: deba@414: typename MapTraits::ConstReturnValue deba@414: operator[](const Node& key) const { deba@414: if (Adaptor::inNode(key)) { return _in_map[key]; } deba@414: else { return _out_map[key]; } deba@414: } deba@414: deba@414: private: deba@414: NodeImpl _in_map, _out_map; deba@414: }; deba@414: deba@414: template deba@414: class ArcMapBase deba@414: : public MapTraits > { deba@414: typedef typename Parent::template ArcMap<_Value> ArcImpl; deba@414: typedef typename Parent::template NodeMap<_Value> NodeImpl; deba@414: public: deba@414: typedef Arc Key; deba@414: typedef _Value Value; deba@414: deba@414: ArcMapBase(const Adaptor& adaptor) deba@414: : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} deba@414: ArcMapBase(const Adaptor& adaptor, const Value& value) deba@414: : _arc_map(*adaptor._digraph, value), deba@414: _node_map(*adaptor._digraph, value) {} deba@414: deba@414: void set(const Arc& key, const Value& val) { deba@414: if (Adaptor::origArc(key)) { deba@414: _arc_map.set(key._item.first(), val); deba@414: } else { deba@414: _node_map.set(key._item.second(), val); deba@414: } deba@414: } deba@414: deba@414: typename MapTraits::ReturnValue deba@414: operator[](const Arc& key) { deba@414: if (Adaptor::origArc(key)) { deba@414: return _arc_map[key._item.first()]; deba@414: } else { deba@414: return _node_map[key._item.second()]; deba@414: } deba@414: } deba@414: deba@414: typename MapTraits::ConstReturnValue deba@414: operator[](const Arc& key) const { deba@414: if (Adaptor::origArc(key)) { deba@414: return _arc_map[key._item.first()]; deba@414: } else { deba@414: return _node_map[key._item.second()]; deba@414: } deba@414: } deba@414: deba@414: private: deba@414: ArcImpl _arc_map; deba@414: NodeImpl _node_map; deba@414: }; deba@414: deba@414: public: deba@414: deba@414: template deba@414: class NodeMap deba@414: : public SubMapExtender > deba@414: { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > Parent; deba@414: deba@414: NodeMap(const Adaptor& adaptor) deba@414: : Parent(adaptor) {} deba@414: deba@414: NodeMap(const Adaptor& adaptor, const Value& value) deba@414: : Parent(adaptor, value) {} deba@414: deba@414: private: deba@414: NodeMap& operator=(const NodeMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: NodeMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: template deba@414: class ArcMap deba@414: : public SubMapExtender > deba@414: { deba@414: public: deba@414: typedef _Value Value; deba@414: typedef SubMapExtender > Parent; deba@414: deba@414: ArcMap(const Adaptor& adaptor) deba@414: : Parent(adaptor) {} deba@414: deba@414: ArcMap(const Adaptor& adaptor, const Value& value) deba@414: : Parent(adaptor, value) {} deba@414: deba@414: private: deba@414: ArcMap& operator=(const ArcMap& cmap) { deba@414: return operator=(cmap); deba@414: } deba@414: deba@414: template deba@414: ArcMap& operator=(const CMap& cmap) { deba@414: Parent::operator=(cmap); deba@414: return *this; deba@414: } deba@414: }; deba@414: deba@414: protected: deba@414: deba@414: SplitDigraphAdaptorBase() : _digraph(0) {} deba@414: deba@414: Digraph* _digraph; deba@414: deba@414: void setDigraph(Digraph& digraph) { deba@414: _digraph = &digraph; deba@414: } deba@414: deba@414: }; deba@414: deba@414: /// \ingroup graph_adaptors deba@414: /// deba@414: /// \brief Split digraph adaptor class deba@414: /// deba@414: /// This is an digraph adaptor which splits all node into an in-node deba@414: /// and an out-node. Formaly, the adaptor replaces each \f$ u \f$ deba@414: /// node in the digraph with two node, \f$ u_{in} \f$ node and deba@414: /// \f$ u_{out} \f$ node. If there is an \f$ (v, u) \f$ arc in the deba@414: /// original digraph the new target of the arc will be \f$ u_{in} \f$ and deba@414: /// similarly the source of the original \f$ (u, v) \f$ arc will be deba@414: /// \f$ u_{out} \f$. The adaptor will add for each node in the deba@414: /// original digraph an additional arc which will connect deba@414: /// \f$ (u_{in}, u_{out}) \f$. deba@414: /// deba@414: /// The aim of this class is to run algorithm with node costs if the deba@414: /// algorithm can use directly just arc costs. In this case we should use deba@414: /// a \c SplitDigraphAdaptor and set the node cost of the digraph to the deba@414: /// bind arc in the adapted digraph. deba@414: /// deba@414: /// By example a maximum flow algoritm can compute how many arc deba@414: /// disjoint paths are in the digraph. But we would like to know how deba@414: /// many node disjoint paths are in the digraph. First we have to deba@414: /// adapt the digraph with the \c SplitDigraphAdaptor. Then run the flow deba@414: /// algorithm on the adapted digraph. The bottleneck of the flow will deba@414: /// be the bind arcs which bounds the flow with the count of the deba@414: /// node disjoint paths. deba@414: /// deba@414: ///\code deba@414: /// deba@414: /// typedef SplitDigraphAdaptor SDigraph; deba@414: /// deba@414: /// SDigraph sdigraph(digraph); deba@414: /// deba@414: /// typedef ConstMap SCapacity; deba@414: /// SCapacity scapacity(1); deba@414: /// deba@414: /// SDigraph::ArcMap sflow(sdigraph); deba@414: /// deba@414: /// Preflow deba@414: /// spreflow(sdigraph, scapacity, deba@414: /// SDigraph::outNode(source), SDigraph::inNode(target)); deba@414: /// deba@414: /// spreflow.run(); deba@414: /// deba@414: ///\endcode deba@414: /// deba@414: /// The result of the mamixum flow on the original digraph deba@414: /// shows the next figure: deba@414: /// deba@414: /// \image html arc_disjoint.png deba@414: /// \image latex arc_disjoint.eps "Arc disjoint paths" width=\textwidth deba@414: /// deba@414: /// And the maximum flow on the adapted digraph: deba@414: /// deba@414: /// \image html node_disjoint.png deba@414: /// \image latex node_disjoint.eps "Node disjoint paths" width=\textwidth deba@414: /// deba@414: /// The second solution contains just 3 disjoint paths while the first 4. deba@414: /// The full code can be found in the \ref disjoint_paths_demo.cc demo file. deba@414: /// deba@414: /// This digraph adaptor is fully conform to the deba@414: /// \ref concepts::Digraph "Digraph" concept and deba@414: /// contains some additional member functions and types. The deba@414: /// documentation of some member functions may be found just in the deba@414: /// SplitDigraphAdaptorBase class. deba@414: /// deba@414: /// \sa SplitDigraphAdaptorBase deba@414: template deba@414: class SplitDigraphAdaptor deba@414: : public DigraphAdaptorExtender > { deba@414: public: deba@414: typedef _Digraph Digraph; deba@414: typedef DigraphAdaptorExtender > Parent; deba@414: deba@414: typedef typename Parent::Node Node; deba@414: typedef typename Parent::Arc Arc; deba@414: deba@414: /// \brief Constructor of the adaptor. deba@414: /// deba@414: /// Constructor of the adaptor. deba@414: SplitDigraphAdaptor(Digraph& g) { deba@414: Parent::setDigraph(g); deba@414: } deba@414: deba@414: /// \brief NodeMap combined from two original NodeMap deba@414: /// deba@414: /// This class adapt two of the original digraph NodeMap to deba@414: /// get a node map on the adapted digraph. deba@414: template deba@414: class CombinedNodeMap { deba@414: public: deba@414: deba@414: typedef Node Key; deba@414: typedef typename InNodeMap::Value Value; deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// Constructor. deba@414: CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) deba@414: : _in_map(in_map), _out_map(out_map) {} deba@414: deba@414: /// \brief The subscript operator. deba@414: /// deba@414: /// The subscript operator. deba@414: Value& operator[](const Key& key) { deba@414: if (Parent::inNode(key)) { deba@414: return _in_map[key]; deba@414: } else { deba@414: return _out_map[key]; deba@414: } deba@414: } deba@414: deba@414: /// \brief The const subscript operator. deba@414: /// deba@414: /// The const subscript operator. deba@414: Value operator[](const Key& key) const { deba@414: if (Parent::inNode(key)) { deba@414: return _in_map[key]; deba@414: } else { deba@414: return _out_map[key]; deba@414: } deba@414: } deba@414: deba@414: /// \brief The setter function of the map. deba@414: /// deba@414: /// The setter function of the map. deba@414: void set(const Key& key, const Value& value) { deba@414: if (Parent::inNode(key)) { deba@414: _in_map.set(key, value); deba@414: } else { deba@414: _out_map.set(key, value); deba@414: } deba@414: } deba@414: deba@414: private: deba@414: deba@414: InNodeMap& _in_map; deba@414: OutNodeMap& _out_map; deba@414: deba@414: }; deba@414: deba@414: deba@414: /// \brief Just gives back a combined node map. deba@414: /// deba@414: /// Just gives back a combined node map. deba@414: template deba@414: static CombinedNodeMap deba@414: combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { deba@414: return CombinedNodeMap(in_map, out_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedNodeMap deba@414: combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { deba@414: return CombinedNodeMap(in_map, out_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedNodeMap deba@414: combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { deba@414: return CombinedNodeMap(in_map, out_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedNodeMap deba@414: combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { deba@414: return CombinedNodeMap(in_map, out_map); deba@414: } deba@414: deba@414: /// \brief ArcMap combined from an original ArcMap and NodeMap deba@414: /// deba@414: /// This class adapt an original digraph ArcMap and NodeMap to deba@414: /// get an arc map on the adapted digraph. deba@414: template deba@414: class CombinedArcMap { deba@414: public: deba@414: deba@414: typedef Arc Key; deba@414: typedef typename DigraphArcMap::Value Value; deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// Constructor. deba@414: CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) deba@414: : _arc_map(arc_map), _node_map(node_map) {} deba@414: deba@414: /// \brief The subscript operator. deba@414: /// deba@414: /// The subscript operator. deba@414: void set(const Arc& arc, const Value& val) { deba@414: if (Parent::origArc(arc)) { deba@414: _arc_map.set(arc, val); deba@414: } else { deba@414: _node_map.set(arc, val); deba@414: } deba@414: } deba@414: deba@414: /// \brief The const subscript operator. deba@414: /// deba@414: /// The const subscript operator. deba@414: Value operator[](const Key& arc) const { deba@414: if (Parent::origArc(arc)) { deba@414: return _arc_map[arc]; deba@414: } else { deba@414: return _node_map[arc]; deba@414: } deba@414: } deba@414: deba@414: /// \brief The const subscript operator. deba@414: /// deba@414: /// The const subscript operator. deba@414: Value& operator[](const Key& arc) { deba@414: if (Parent::origArc(arc)) { deba@414: return _arc_map[arc]; deba@414: } else { deba@414: return _node_map[arc]; deba@414: } deba@414: } deba@414: deba@414: private: deba@414: DigraphArcMap& _arc_map; deba@414: DigraphNodeMap& _node_map; deba@414: }; deba@414: deba@414: /// \brief Just gives back a combined arc map. deba@414: /// deba@414: /// Just gives back a combined arc map. deba@414: template deba@414: static CombinedArcMap deba@414: combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) { deba@414: return CombinedArcMap(arc_map, node_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedArcMap deba@414: combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) { deba@414: return CombinedArcMap(arc_map, node_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedArcMap deba@414: combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) { deba@414: return CombinedArcMap(arc_map, node_map); deba@414: } deba@414: deba@414: template deba@414: static CombinedArcMap deba@414: combinedArcMap(const DigraphArcMap& arc_map, deba@414: const DigraphNodeMap& node_map) { deba@414: return CombinedArcMap(arc_map, node_map); deba@414: } deba@414: deba@414: }; deba@414: deba@414: /// \brief Just gives back a split digraph adaptor deba@414: /// deba@414: /// Just gives back a split digraph adaptor deba@414: template deba@414: SplitDigraphAdaptor deba@414: splitDigraphAdaptor(const Digraph& digraph) { deba@414: return SplitDigraphAdaptor(digraph); deba@414: } deba@414: deba@414: deba@414: } //namespace lemon deba@414: deba@414: #endif //LEMON_DIGRAPH_ADAPTOR_H deba@414: