/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2006 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_GRAPH_UTILS_H #define LEMON_GRAPH_UTILS_H #include #include #include #include #include #include #include #include #include #include #include ///\ingroup gutils ///\file ///\brief Graph utilities. /// /// namespace lemon { /// \addtogroup gutils /// @{ ///Creates convenience typedefs for the graph types and iterators ///This \c \#define creates convenience typedefs for the following types ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt, ///\c OutEdgeIt ///\note If \c G it a template parameter, it should be used in this way. ///\code /// GRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define GRAPH_TYPEDEFS(Graph) \ typedef Graph:: Node Node; \ typedef Graph:: NodeIt NodeIt; \ typedef Graph:: Edge Edge; \ typedef Graph:: EdgeIt EdgeIt; \ typedef Graph:: InEdgeIt InEdgeIt; \ typedef Graph::OutEdgeIt OutEdgeIt; ///Creates convenience typedefs for the undirected graph types and iterators ///This \c \#define creates the same convenience typedefs as defined by ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates ///\c UEdge, \c UEdgeIt, \c IncEdgeIt, /// ///\note If \c G it a template parameter, it should be used in this way. ///\code /// UGRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define UGRAPH_TYPEDEFS(Graph) \ GRAPH_TYPEDEFS(Graph) \ typedef Graph:: UEdge UEdge; \ typedef Graph:: UEdgeIt UEdgeIt; \ typedef Graph:: IncEdgeIt IncEdgeIt; ///\brief Creates convenience typedefs for the bipartite undirected graph ///types and iterators ///This \c \#define creates the same convenience typedefs as defined by ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates ///\c ANodeIt, \c BNodeIt, /// ///\note If \c G it a template parameter, it should be used in this way. ///\code /// BPUGRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define BPUGRAPH_TYPEDEFS(Graph) \ UGRAPH_TYPEDEFS(Graph) \ typedef Graph::ANode ANode; \ typedef Graph::BNode BNode; \ typedef Graph::ANodeIt ANodeIt; \ typedef Graph::BNodeIt BNodeIt; /// \brief Function to count the items in the graph. /// /// This function counts the items (nodes, edges etc) in the graph. /// The complexity of the function is O(n) because /// it iterates on all of the items. template inline int countItems(const Graph& g) { typedef typename ItemSetTraits::ItemIt ItemIt; int num = 0; for (ItemIt it(g); it != INVALID; ++it) { ++num; } return num; } // Node counting: namespace _graph_utils_bits { template struct CountNodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountNodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.nodeNum(); } }; } /// \brief Function to count the nodes in the graph. /// /// This function counts the nodes in the graph. /// The complexity of the function is O(n) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countNodes(const Graph& g) { return _graph_utils_bits::CountNodesSelector::count(g); } namespace _graph_utils_bits { template struct CountANodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountANodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.aNodeNum(); } }; } /// \brief Function to count the anodes in the graph. /// /// This function counts the anodes in the graph. /// The complexity of the function is O(an) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countANodes(const Graph& g) { return _graph_utils_bits::CountANodesSelector::count(g); } namespace _graph_utils_bits { template struct CountBNodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountBNodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.bNodeNum(); } }; } /// \brief Function to count the bnodes in the graph. /// /// This function counts the bnodes in the graph. /// The complexity of the function is O(bn) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countBNodes(const Graph& g) { return _graph_utils_bits::CountBNodesSelector::count(g); } // Edge counting: namespace _graph_utils_bits { template struct CountEdgesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountEdgesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.edgeNum(); } }; } /// \brief Function to count the edges in the graph. /// /// This function counts the edges in the graph. /// The complexity of the function is O(e) but for some /// graph structures it is specialized to run in O(1). template inline int countEdges(const Graph& g) { return _graph_utils_bits::CountEdgesSelector::count(g); } // Undirected edge counting: namespace _graph_utils_bits { template struct CountUEdgesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountUEdgesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.uEdgeNum(); } }; } /// \brief Function to count the undirected edges in the graph. /// /// This function counts the undirected edges in the graph. /// The complexity of the function is O(e) but for some /// graph structures it is specialized to run in O(1). template inline int countUEdges(const Graph& g) { return _graph_utils_bits::CountUEdgesSelector::count(g); } template inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { int num = 0; for (DegIt it(_g, _n); it != INVALID; ++it) { ++num; } return num; } /// \brief Function to count the number of the out-edges from node \c n. /// /// This function counts the number of the out-edges from node \c n /// in the graph. template inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } /// \brief Function to count the number of the in-edges to node \c n. /// /// This function counts the number of the in-edges to node \c n /// in the graph. template inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } /// \brief Function to count the number of the inc-edges to node \c n. /// /// This function counts the number of the inc-edges to node \c n /// in the graph. template inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } namespace _graph_utils_bits { template struct FindEdgeSelector { typedef typename Graph::Node Node; typedef typename Graph::Edge Edge; static Edge find(const Graph &g, Node u, Node v, Edge e) { if (e == INVALID) { g.firstOut(e, u); } else { g.nextOut(e); } while (e != INVALID && g.target(e) != v) { g.nextOut(e); } return e; } }; template struct FindEdgeSelector< Graph, typename enable_if::type> { typedef typename Graph::Node Node; typedef typename Graph::Edge Edge; static Edge find(const Graph &g, Node u, Node v, Edge prev) { return g.findEdge(u, v, prev); } }; } /// \brief Finds an edge between two nodes of a graph. /// /// Finds an edge from node \c u to node \c v in graph \c g. /// /// If \c prev is \ref INVALID (this is the default value), then /// it finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or \ref INVALID if there is no such an edge. /// /// Thus you can iterate through each edge from \c u to \c v as it follows. ///\code /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) { /// ... /// } ///\endcode /// ///\sa EdgeLookUp ///\se AllEdgeLookup ///\sa ConEdgeIt template inline typename Graph::Edge findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, typename Graph::Edge prev = INVALID) { return _graph_utils_bits::FindEdgeSelector::find(g, u, v, prev); } /// \brief Iterator for iterating on edges connected the same nodes. /// /// Iterator for iterating on edges connected the same nodes. It is /// higher level interface for the findEdge() function. You can /// use it the following way: ///\code /// for (ConEdgeIt it(g, src, trg); it != INVALID; ++it) { /// ... /// } ///\endcode /// ///\sa findEdge() ///\sa EdgeLookUp ///\se AllEdgeLookup /// /// \author Balazs Dezso template class ConEdgeIt : public _Graph::Edge { public: typedef _Graph Graph; typedef typename Graph::Edge Parent; typedef typename Graph::Edge Edge; typedef typename Graph::Node Node; /// \brief Constructor. /// /// Construct a new ConEdgeIt iterating on the edges which /// connects the \c u and \c v node. ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) { Parent::operator=(findEdge(graph, u, v)); } /// \brief Constructor. /// /// Construct a new ConEdgeIt which continues the iterating from /// the \c e edge. ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {} /// \brief Increment operator. /// /// It increments the iterator and gives back the next edge. ConEdgeIt& operator++() { Parent::operator=(findEdge(graph, graph.source(*this), graph.target(*this), *this)); return *this; } private: const Graph& graph; }; namespace _graph_utils_bits { template struct FindUEdgeSelector { typedef typename Graph::Node Node; typedef typename Graph::UEdge UEdge; static UEdge find(const Graph &g, Node u, Node v, UEdge e) { bool b; if (u != v) { if (e == INVALID) { g.firstInc(e, b, u); } else { b = g.source(e) == u; g.nextInc(e, b); } while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { g.nextInc(e, b); } } else { if (e == INVALID) { g.firstInc(e, b, u); } else { b = true; g.nextInc(e, b); } while (e != INVALID && (!b || g.target(e) != v)) { g.nextInc(e, b); } } return e; } }; template struct FindUEdgeSelector< Graph, typename enable_if::type> { typedef typename Graph::Node Node; typedef typename Graph::UEdge UEdge; static UEdge find(const Graph &g, Node u, Node v, UEdge prev) { return g.findUEdge(u, v, prev); } }; } /// \brief Finds an uedge between two nodes of a graph. /// /// Finds an uedge from node \c u to node \c v in graph \c g. /// If the node \c u and node \c v is equal then each loop edge /// will be enumerated. /// /// If \c prev is \ref INVALID (this is the default value), then /// it finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or \ref INVALID if there is no such an edge. /// /// Thus you can iterate through each edge from \c u to \c v as it follows. ///\code /// for(UEdge e = findUEdge(g,u,v); e != INVALID; /// e = findUEdge(g,u,v,e)) { /// ... /// } ///\endcode /// ///\sa ConEdgeIt template inline typename Graph::UEdge findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, typename Graph::UEdge p = INVALID) { return _graph_utils_bits::FindUEdgeSelector::find(g, u, v, p); } /// \brief Iterator for iterating on uedges connected the same nodes. /// /// Iterator for iterating on uedges connected the same nodes. It is /// higher level interface for the findUEdge() function. You can /// use it the following way: ///\code /// for (ConUEdgeIt it(g, src, trg); it != INVALID; ++it) { /// ... /// } ///\endcode /// ///\sa findUEdge() /// /// \author Balazs Dezso template class ConUEdgeIt : public _Graph::UEdge { public: typedef _Graph Graph; typedef typename Graph::UEdge Parent; typedef typename Graph::UEdge UEdge; typedef typename Graph::Node Node; /// \brief Constructor. /// /// Construct a new ConUEdgeIt iterating on the edges which /// connects the \c u and \c v node. ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) { Parent::operator=(findUEdge(graph, u, v)); } /// \brief Constructor. /// /// Construct a new ConUEdgeIt which continues the iterating from /// the \c e edge. ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {} /// \brief Increment operator. /// /// It increments the iterator and gives back the next edge. ConUEdgeIt& operator++() { Parent::operator=(findUEdge(graph, graph.source(*this), graph.target(*this), *this)); return *this; } private: const Graph& graph; }; /// \brief Copy a map. /// /// This function copies the \c source map to the \c target map. It uses the /// given iterator to iterate on the data structure and it uses the \c ref /// mapping to convert the source's keys to the target's keys. template void copyMap(Target& target, const Source& source, ItemIt it, const Ref& ref) { for (; it != INVALID; ++it) { target[ref[it]] = source[it]; } } /// \brief Copy the source map to the target map. /// /// Copy the \c source map to the \c target map. It uses the given iterator /// to iterate on the data structure. template void copyMap(Target& target, const Source& source, ItemIt it) { for (; it != INVALID; ++it) { target[it] = source[it]; } } namespace _graph_utils_bits { template class MapCopyBase { public: virtual void copy(const Graph& source, const RefMap& refMap) = 0; virtual ~MapCopyBase() {} }; template class MapCopy : public MapCopyBase { public: MapCopy(TargetMap& tmap, const SourceMap& map) : _tmap(tmap), _map(map) {} virtual void copy(const Graph& graph, const RefMap& refMap) { typedef typename ItemSetTraits::ItemIt ItemIt; for (ItemIt it(graph); it != INVALID; ++it) { _tmap.set(refMap[it], _map[it]); } } private: TargetMap& _tmap; const SourceMap& _map; }; template class ItemCopy : public MapCopyBase { public: ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} virtual void copy(const Graph&, const RefMap& refMap) { _it = refMap[_item]; } private: It& _it; Item _item; }; template class RefCopy : public MapCopyBase { public: RefCopy(Ref& map) : _map(map) {} virtual void copy(const Graph& graph, const RefMap& refMap) { typedef typename ItemSetTraits::ItemIt ItemIt; for (ItemIt it(graph); it != INVALID; ++it) { _map.set(it, refMap[it]); } } private: Ref& _map; }; template class CrossRefCopy : public MapCopyBase { public: CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} virtual void copy(const Graph& graph, const RefMap& refMap) { typedef typename ItemSetTraits::ItemIt ItemIt; for (ItemIt it(graph); it != INVALID; ++it) { _cmap.set(refMap[it], it); } } private: CrossRef& _cmap; }; template struct GraphCopySelector { template static void copy(Graph &target, const Source& source, NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { for (typename Source::NodeIt it(source); it != INVALID; ++it) { nodeRefMap[it] = target.addNode(); } for (typename Source::EdgeIt it(source); it != INVALID; ++it) { edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], nodeRefMap[source.target(it)]); } } }; template struct GraphCopySelector< Graph, typename enable_if::type> { template static void copy(Graph &target, const Source& source, NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { target.build(source, nodeRefMap, edgeRefMap); } }; template struct UGraphCopySelector { template static void copy(UGraph &target, const Source& source, NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) { for (typename Source::NodeIt it(source); it != INVALID; ++it) { nodeRefMap[it] = target.addNode(); } for (typename Source::UEdgeIt it(source); it != INVALID; ++it) { uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], nodeRefMap[source.target(it)]); } } }; template struct UGraphCopySelector< UGraph, typename enable_if::type> { template static void copy(UGraph &target, const Source& source, NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) { target.build(source, nodeRefMap, uEdgeRefMap); } }; template struct BpUGraphCopySelector { template static void copy(BpUGraph &target, const Source& source, ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap, UEdgeRefMap& uEdgeRefMap) { for (typename Source::ANodeIt it(source); it != INVALID; ++it) { aNodeRefMap[it] = target.addANode(); } for (typename Source::BNodeIt it(source); it != INVALID; ++it) { bNodeRefMap[it] = target.addBNode(); } for (typename Source::UEdgeIt it(source); it != INVALID; ++it) { uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)], bNodeRefMap[source.bNode(it)]); } } }; template struct BpUGraphCopySelector< BpUGraph, typename enable_if::type> { template static void copy(BpUGraph &target, const Source& source, ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap, UEdgeRefMap& uEdgeRefMap) { target.build(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap); } }; } /// \brief Class to copy a graph. /// /// Class to copy a graph to another graph (duplicate a graph). The /// simplest way of using it is through the \c copyGraph() function. template class GraphCopy { private: typedef typename Source::Node Node; typedef typename Source::NodeIt NodeIt; typedef typename Source::Edge Edge; typedef typename Source::EdgeIt EdgeIt; typedef typename Target::Node TNode; typedef typename Target::Edge TEdge; typedef typename Source::template NodeMap NodeRefMap; typedef typename Source::template EdgeMap EdgeRefMap; public: /// \brief Constructor for the GraphCopy. /// /// It copies the content of the \c _source graph into the /// \c _target graph. GraphCopy(Target& _target, const Source& _source) : source(_source), target(_target) {} /// \brief Destructor of the GraphCopy /// /// Destructor of the GraphCopy ~GraphCopy() { for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { delete nodeMapCopies[i]; } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { delete edgeMapCopies[i]; } } /// \brief Copies the node references into the given map. /// /// Copies the node references into the given map. template GraphCopy& nodeRef(NodeRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the node cross references into the given map. /// /// Copies the node cross references (reverse references) into /// the given map. template GraphCopy& nodeCrossRef(NodeCrossRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) { nodeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given node. /// /// Make a copy of the given node. GraphCopy& node(TNode& tnode, const Node& node) { nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tnode, node)); return *this; } /// \brief Copies the edge references into the given map. /// /// Copies the edge references into the given map. template GraphCopy& edgeRef(EdgeRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the edge cross references into the given map. /// /// Copies the edge cross references (reverse references) into /// the given map. template GraphCopy& edgeCrossRef(EdgeCrossRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) { edgeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given edge. /// /// Make a copy of the given edge. GraphCopy& edge(TEdge& tedge, const Edge& edge) { edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tedge, edge)); return *this; } /// \brief Executes the copies. /// /// Executes the copies. void run() { NodeRefMap nodeRefMap(source); EdgeRefMap edgeRefMap(source); _graph_utils_bits::GraphCopySelector:: copy(target, source, nodeRefMap, edgeRefMap); for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { nodeMapCopies[i]->copy(source, nodeRefMap); } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { edgeMapCopies[i]->copy(source, edgeRefMap); } } protected: const Source& source; Target& target; std::vector<_graph_utils_bits::MapCopyBase* > nodeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > edgeMapCopies; }; /// \brief Copy a graph to another graph. /// /// Copy a graph to another graph. /// The usage of the function: /// ///\code /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run(); ///\endcode /// /// After the copy the \c nr map will contain the mapping from the /// source graph's nodes to the target graph's nodes and the \c ecr will /// contain the mapping from the target graph's edges to the source's /// edges. /// /// \see GraphCopy template GraphCopy copyGraph(Target& target, const Source& source) { return GraphCopy(target, source); } /// \brief Class to copy an undirected graph. /// /// Class to copy an undirected graph to another graph (duplicate a graph). /// The simplest way of using it is through the \c copyUGraph() function. template class UGraphCopy { private: typedef typename Source::Node Node; typedef typename Source::NodeIt NodeIt; typedef typename Source::Edge Edge; typedef typename Source::EdgeIt EdgeIt; typedef typename Source::UEdge UEdge; typedef typename Source::UEdgeIt UEdgeIt; typedef typename Target::Node TNode; typedef typename Target::Edge TEdge; typedef typename Target::UEdge TUEdge; typedef typename Source::template NodeMap NodeRefMap; typedef typename Source::template UEdgeMap UEdgeRefMap; struct EdgeRefMap { EdgeRefMap(const Target& _target, const Source& _source, const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) : target(_target), source(_source), uedge_ref(_uedge_ref), node_ref(_node_ref) {} typedef typename Source::Edge Key; typedef typename Target::Edge Value; Value operator[](const Key& key) const { bool forward = (source.direction(key) == (node_ref[source.source((UEdge)key)] == target.source(uedge_ref[(UEdge)key]))); return target.direct(uedge_ref[key], forward); } const Target& target; const Source& source; const UEdgeRefMap& uedge_ref; const NodeRefMap& node_ref; }; public: /// \brief Constructor for the GraphCopy. /// /// It copies the content of the \c _source graph into the /// \c _target graph. UGraphCopy(Target& _target, const Source& _source) : source(_source), target(_target) {} /// \brief Destructor of the GraphCopy /// /// Destructor of the GraphCopy ~UGraphCopy() { for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { delete nodeMapCopies[i]; } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { delete edgeMapCopies[i]; } for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) { delete uEdgeMapCopies[i]; } } /// \brief Copies the node references into the given map. /// /// Copies the node references into the given map. template UGraphCopy& nodeRef(NodeRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the node cross references into the given map. /// /// Copies the node cross references (reverse references) into /// the given map. template UGraphCopy& nodeCrossRef(NodeCrossRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) { nodeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given node. /// /// Make a copy of the given node. UGraphCopy& node(TNode& tnode, const Node& node) { nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tnode, node)); return *this; } /// \brief Copies the edge references into the given map. /// /// Copies the edge references into the given map. template UGraphCopy& edgeRef(EdgeRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the edge cross references into the given map. /// /// Copies the edge cross references (reverse references) into /// the given map. template UGraphCopy& edgeCrossRef(EdgeCrossRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) { edgeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given edge. /// /// Make a copy of the given edge. UGraphCopy& edge(TEdge& tedge, const Edge& edge) { edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tedge, edge)); return *this; } /// \brief Copies the undirected edge references into the given map. /// /// Copies the undirected edge references into the given map. template UGraphCopy& uEdgeRef(UEdgeRef& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the undirected edge cross references into the given map. /// /// Copies the undirected edge cross references (reverse /// references) into the given map. template UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's undirected edge type, /// and the copied map's key type is the source graph's undirected edge /// type. template UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given undirected edge. /// /// Make a copy of the given undirected edge. UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) { uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tuedge, uedge)); return *this; } /// \brief Executes the copies. /// /// Executes the copies. void run() { NodeRefMap nodeRefMap(source); UEdgeRefMap uEdgeRefMap(source); EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap); _graph_utils_bits::UGraphCopySelector:: copy(target, source, nodeRefMap, uEdgeRefMap); for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { nodeMapCopies[i]->copy(source, nodeRefMap); } for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) { uEdgeMapCopies[i]->copy(source, uEdgeRefMap); } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { edgeMapCopies[i]->copy(source, edgeRefMap); } } private: const Source& source; Target& target; std::vector<_graph_utils_bits::MapCopyBase* > nodeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > edgeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > uEdgeMapCopies; }; /// \brief Copy an undirected graph to another graph. /// /// Copy an undirected graph to another graph. /// The usage of the function: /// ///\code /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run(); ///\endcode /// /// After the copy the \c nr map will contain the mapping from the /// source graph's nodes to the target graph's nodes and the \c ecr will /// contain the mapping from the target graph's edges to the source's /// edges. /// /// \see UGraphCopy template UGraphCopy copyUGraph(Target& target, const Source& source) { return UGraphCopy(target, source); } /// \brief Class to copy a bipartite undirected graph. /// /// Class to copy a bipartite undirected graph to another graph /// (duplicate a graph). The simplest way of using it is through /// the \c copyBpUGraph() function. template class BpUGraphCopy { private: typedef typename Source::Node Node; typedef typename Source::ANode ANode; typedef typename Source::BNode BNode; typedef typename Source::NodeIt NodeIt; typedef typename Source::Edge Edge; typedef typename Source::EdgeIt EdgeIt; typedef typename Source::UEdge UEdge; typedef typename Source::UEdgeIt UEdgeIt; typedef typename Target::Node TNode; typedef typename Target::Edge TEdge; typedef typename Target::UEdge TUEdge; typedef typename Source::template ANodeMap ANodeRefMap; typedef typename Source::template BNodeMap BNodeRefMap; typedef typename Source::template UEdgeMap UEdgeRefMap; struct NodeRefMap { NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref, const BNodeRefMap& _bnode_ref) : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {} typedef typename Source::Node Key; typedef typename Target::Node Value; Value operator[](const Key& key) const { return source.aNode(key) ? anode_ref[key] : bnode_ref[key]; } const Source& source; const ANodeRefMap& anode_ref; const BNodeRefMap& bnode_ref; }; struct EdgeRefMap { EdgeRefMap(const Target& _target, const Source& _source, const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) : target(_target), source(_source), uedge_ref(_uedge_ref), node_ref(_node_ref) {} typedef typename Source::Edge Key; typedef typename Target::Edge Value; Value operator[](const Key& key) const { bool forward = (source.direction(key) == (node_ref[source.source((UEdge)key)] == target.source(uedge_ref[(UEdge)key]))); return target.direct(uedge_ref[key], forward); } const Target& target; const Source& source; const UEdgeRefMap& uedge_ref; const NodeRefMap& node_ref; }; public: /// \brief Constructor for the GraphCopy. /// /// It copies the content of the \c _source graph into the /// \c _target graph. BpUGraphCopy(Target& _target, const Source& _source) : source(_source), target(_target) {} /// \brief Destructor of the GraphCopy /// /// Destructor of the GraphCopy ~BpUGraphCopy() { for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) { delete aNodeMapCopies[i]; } for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) { delete bNodeMapCopies[i]; } for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { delete nodeMapCopies[i]; } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { delete edgeMapCopies[i]; } for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) { delete uEdgeMapCopies[i]; } } /// \brief Copies the A-node references into the given map. /// /// Copies the A-node references into the given map. template BpUGraphCopy& aNodeRef(ANodeRef& map) { aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the A-node cross references into the given map. /// /// Copies the A-node cross references (reverse references) into /// the given map. template BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) { aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given A-node map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) { aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Copies the B-node references into the given map. /// /// Copies the B-node references into the given map. template BpUGraphCopy& bNodeRef(BNodeRef& map) { bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the B-node cross references into the given map. /// /// Copies the B-node cross references (reverse references) into /// the given map. template BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) { bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given B-node map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) { bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Copies the node references into the given map. /// /// Copies the node references into the given map. template BpUGraphCopy& nodeRef(NodeRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the node cross references into the given map. /// /// Copies the node cross references (reverse references) into /// the given map. template BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) { nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) { nodeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given node. /// /// Make a copy of the given node. BpUGraphCopy& node(TNode& tnode, const Node& node) { nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tnode, node)); return *this; } /// \brief Copies the edge references into the given map. /// /// Copies the edge references into the given map. template BpUGraphCopy& edgeRef(EdgeRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the edge cross references into the given map. /// /// Copies the edge cross references (reverse references) into /// the given map. template BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) { edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) { edgeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given edge. /// /// Make a copy of the given edge. BpUGraphCopy& edge(TEdge& tedge, const Edge& edge) { edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tedge, edge)); return *this; } /// \brief Copies the undirected edge references into the given map. /// /// Copies the undirected edge references into the given map. template BpUGraphCopy& uEdgeRef(UEdgeRef& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy(map)); return *this; } /// \brief Copies the undirected edge cross references into the given map. /// /// Copies the undirected edge cross references (reverse /// references) into the given map. template BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy(map)); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's undirected edge type, /// and the copied map's key type is the source graph's undirected edge /// type. template BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) { uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy(tmap, map)); return *this; } /// \brief Make a copy of the given undirected edge. /// /// Make a copy of the given undirected edge. BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) { uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy(tuedge, uedge)); return *this; } /// \brief Executes the copies. /// /// Executes the copies. void run() { ANodeRefMap aNodeRefMap(source); BNodeRefMap bNodeRefMap(source); NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap); UEdgeRefMap uEdgeRefMap(source); EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap); _graph_utils_bits::BpUGraphCopySelector:: copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap); for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) { aNodeMapCopies[i]->copy(source, aNodeRefMap); } for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) { bNodeMapCopies[i]->copy(source, bNodeRefMap); } for (int i = 0; i < (int)nodeMapCopies.size(); ++i) { nodeMapCopies[i]->copy(source, nodeRefMap); } for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) { uEdgeMapCopies[i]->copy(source, uEdgeRefMap); } for (int i = 0; i < (int)edgeMapCopies.size(); ++i) { edgeMapCopies[i]->copy(source, edgeRefMap); } } private: const Source& source; Target& target; std::vector<_graph_utils_bits::MapCopyBase* > aNodeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > bNodeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > nodeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > edgeMapCopies; std::vector<_graph_utils_bits::MapCopyBase* > uEdgeMapCopies; }; /// \brief Copy a bipartite undirected graph to another graph. /// /// Copy a bipartite undirected graph to another graph. /// The usage of the function: /// ///\code /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run(); ///\endcode /// /// After the copy the \c nr map will contain the mapping from the /// source graph's nodes to the target graph's nodes and the \c ecr will /// contain the mapping from the target graph's edges to the source's /// edges. /// /// \see BpUGraphCopy template BpUGraphCopy copyBpUGraph(Target& target, const Source& source) { return BpUGraphCopy(target, source); } /// @} /// \addtogroup graph_maps /// @{ /// Provides an immutable and unique id for each item in the graph. /// The IdMap class provides a unique and immutable id for each item of the /// same type (e.g. node) in the graph. This id is
  • \b unique: /// different items (nodes) get different ids
  • \b immutable: the id of an /// item (node) does not change (even if you delete other nodes).
/// Through this map you get access (i.e. can read) the inner id values of /// the items stored in the graph. This map can be inverted with its member /// class \c InverseMap. /// template class IdMap { public: typedef _Graph Graph; typedef int Value; typedef _Item Item; typedef _Item Key; /// \brief Constructor. /// /// Constructor of the map. explicit IdMap(const Graph& _graph) : graph(&_graph) {} /// \brief Gives back the \e id of the item. /// /// Gives back the immutable and unique \e id of the item. int operator[](const Item& item) const { return graph->id(item);} /// \brief Gives back the item by its id. /// /// Gives back the item by its id. Item operator()(int id) { return graph->fromId(id, Item()); } private: const Graph* graph; public: /// \brief The class represents the inverse of its owner (IdMap). /// /// The class represents the inverse of its owner (IdMap). /// \see inverse() class InverseMap { public: /// \brief Constructor. /// /// Constructor for creating an id-to-item map. explicit InverseMap(const Graph& _graph) : graph(&_graph) {} /// \brief Constructor. /// /// Constructor for creating an id-to-item map. explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {} /// \brief Gives back the given item from its id. /// /// Gives back the given item from its id. /// Item operator[](int id) const { return graph->fromId(id, Item());} private: const Graph* graph; }; /// \brief Gives back the inverse of the map. /// /// Gives back the inverse of the IdMap. InverseMap inverse() const { return InverseMap(*graph);} }; /// \brief General invertable graph-map type. /// This type provides simple invertable graph-maps. /// The InvertableMap wraps an arbitrary ReadWriteMap /// and if a key is set to a new value then store it /// in the inverse map. /// /// The values of the map can be accessed /// with stl compatible forward iterator. /// /// \param _Graph The graph type. /// \param _Item The item type of the graph. /// \param _Value The value type of the map. /// /// \see IterableValueMap template class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> { private: typedef DefaultMap<_Graph, _Item, _Value> Map; typedef _Graph Graph; typedef std::map<_Value, _Item> Container; Container invMap; public: /// The key type of InvertableMap (Node, Edge, UEdge). typedef typename Map::Key Key; /// The value type of the InvertableMap. typedef typename Map::Value Value; /// \brief Constructor. /// /// Construct a new InvertableMap for the graph. /// explicit InvertableMap(const Graph& graph) : Map(graph) {} /// \brief Forward iterator for values. /// /// This iterator is an stl compatible forward /// iterator on the values of the map. The values can /// be accessed in the [beginValue, endValue) range. /// class ValueIterator : public std::iterator { friend class InvertableMap; private: ValueIterator(typename Container::const_iterator _it) : it(_it) {} public: ValueIterator() {} ValueIterator& operator++() { ++it; return *this; } ValueIterator operator++(int) { ValueIterator tmp(*this); operator++(); return tmp; } const Value& operator*() const { return it->first; } const Value* operator->() const { return &(it->first); } bool operator==(ValueIterator jt) const { return it == jt.it; } bool operator!=(ValueIterator jt) const { return it != jt.it; } private: typename Container::const_iterator it; }; /// \brief Returns an iterator to the first value. /// /// Returns an stl compatible iterator to the /// first value of the map. The values of the /// map can be accessed in the [beginValue, endValue) /// range. ValueIterator beginValue() const { return ValueIterator(invMap.begin()); } /// \brief Returns an iterator after the last value. /// /// Returns an stl compatible iterator after the /// last value of the map. The values of the /// map can be accessed in the [beginValue, endValue) /// range. ValueIterator endValue() const { return ValueIterator(invMap.end()); } /// \brief The setter function of the map. /// /// Sets the mapped value. void set(const Key& key, const Value& val) { Value oldval = Map::operator[](key); typename Container::iterator it = invMap.find(oldval); if (it != invMap.end() && it->second == key) { invMap.erase(it); } invMap.insert(make_pair(val, key)); Map::set(key, val); } /// \brief The getter function of the map. /// /// It gives back the value associated with the key. typename MapTraits::ConstReturnValue operator[](const Key& key) const { return Map::operator[](key); } /// \brief Gives back the item by its value. /// /// Gives back the item by its value. Key operator()(const Value& key) const { typename Container::const_iterator it = invMap.find(key); return it != invMap.end() ? it->second : INVALID; } protected: /// \brief Erase the key from the map. /// /// Erase the key to the map. It is called by the /// \c AlterationNotifier. virtual void erase(const Key& key) { Value val = Map::operator[](key); typename Container::iterator it = invMap.find(val); if (it != invMap.end() && it->second == key) { invMap.erase(it); } Map::erase(key); } /// \brief Erase more keys from the map. /// /// Erase more keys from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const std::vector& keys) { for (int i = 0; i < (int)keys.size(); ++i) { Value val = Map::operator[](keys[i]); typename Container::iterator it = invMap.find(val); if (it != invMap.end() && it->second == keys[i]) { invMap.erase(it); } } Map::erase(keys); } /// \brief Clear the keys from the map and inverse map. /// /// Clear the keys from the map and inverse map. It is called by the /// \c AlterationNotifier. virtual void clear() { invMap.clear(); Map::clear(); } public: /// \brief The inverse map type. /// /// The inverse of this map. The subscript operator of the map /// gives back always the item what was last assigned to the value. class InverseMap { public: /// \brief Constructor of the InverseMap. /// /// Constructor of the InverseMap. explicit InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {} /// The value type of the InverseMap. typedef typename InvertableMap::Key Value; /// The key type of the InverseMap. typedef typename InvertableMap::Value Key; /// \brief Subscript operator. /// /// Subscript operator. It gives back always the item /// what was last assigned to the value. Value operator[](const Key& key) const { return inverted(key); } private: const InvertableMap& inverted; }; /// \brief It gives back the just readable inverse map. /// /// It gives back the just readable inverse map. InverseMap inverse() const { return InverseMap(*this); } }; /// \brief Provides a mutable, continuous and unique descriptor for each /// item in the graph. /// /// The DescriptorMap class provides a unique and continuous (but mutable) /// descriptor (id) for each item of the same type (e.g. node) in the /// graph. This id is
  • \b unique: different items (nodes) get /// different ids
  • \b continuous: the range of the ids is the set of /// integers between 0 and \c n-1, where \c n is the number of the items of /// this type (e.g. nodes) (so the id of a node can change if you delete an /// other node, i.e. this id is mutable).
This map can be inverted /// with its member class \c InverseMap. /// /// \param _Graph The graph class the \c DescriptorMap belongs to. /// \param _Item The Item is the Key of the Map. It may be Node, Edge or /// UEdge. template class DescriptorMap : protected DefaultMap<_Graph, _Item, int> { typedef _Item Item; typedef DefaultMap<_Graph, _Item, int> Map; public: /// The graph class of DescriptorMap. typedef _Graph Graph; /// The key type of DescriptorMap (Node, Edge, UEdge). typedef typename Map::Key Key; /// The value type of DescriptorMap. typedef typename Map::Value Value; /// \brief Constructor. /// /// Constructor for descriptor map. explicit DescriptorMap(const Graph& _graph) : Map(_graph) { Item it; const typename Map::Notifier* notifier = Map::getNotifier(); for (notifier->first(it); it != INVALID; notifier->next(it)) { Map::set(it, invMap.size()); invMap.push_back(it); } } protected: /// \brief Add a new key to the map. /// /// Add a new key to the map. It is called by the /// \c AlterationNotifier. virtual void add(const Item& item) { Map::add(item); Map::set(item, invMap.size()); invMap.push_back(item); } /// \brief Add more new keys to the map. /// /// Add more new keys to the map. It is called by the /// \c AlterationNotifier. virtual void add(const std::vector& items) { Map::add(items); for (int i = 0; i < (int)items.size(); ++i) { Map::set(items[i], invMap.size()); invMap.push_back(items[i]); } } /// \brief Erase the key from the map. /// /// Erase the key from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const Item& item) { Map::set(invMap.back(), Map::operator[](item)); invMap[Map::operator[](item)] = invMap.back(); invMap.pop_back(); Map::erase(item); } /// \brief Erase more keys from the map. /// /// Erase more keys from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const std::vector& items) { for (int i = 0; i < (int)items.size(); ++i) { Map::set(invMap.back(), Map::operator[](items[i])); invMap[Map::operator[](items[i])] = invMap.back(); invMap.pop_back(); } Map::erase(items); } /// \brief Build the unique map. /// /// Build the unique map. It is called by the /// \c AlterationNotifier. virtual void build() { Map::build(); Item it; const typename Map::Notifier* notifier = Map::getNotifier(); for (notifier->first(it); it != INVALID; notifier->next(it)) { Map::set(it, invMap.size()); invMap.push_back(it); } } /// \brief Clear the keys from the map. /// /// Clear the keys from the map. It is called by the /// \c AlterationNotifier. virtual void clear() { invMap.clear(); Map::clear(); } public: /// \brief Returns the maximal value plus one. /// /// Returns the maximal value plus one in the map. unsigned int size() const { return invMap.size(); } /// \brief Swaps the position of the two items in the map. /// /// Swaps the position of the two items in the map. void swap(const Item& p, const Item& q) { int pi = Map::operator[](p); int qi = Map::operator[](q); Map::set(p, qi); invMap[qi] = p; Map::set(q, pi); invMap[pi] = q; } /// \brief Gives back the \e descriptor of the item. /// /// Gives back the mutable and unique \e descriptor of the map. int operator[](const Item& item) const { return Map::operator[](item); } /// \brief Gives back the item by its descriptor. /// /// Gives back th item by its descriptor. Item operator()(int id) const { return invMap[id]; } private: typedef std::vector Container; Container invMap; public: /// \brief The inverse map type of DescriptorMap. /// /// The inverse map type of DescriptorMap. class InverseMap { public: /// \brief Constructor of the InverseMap. /// /// Constructor of the InverseMap. explicit InverseMap(const DescriptorMap& _inverted) : inverted(_inverted) {} /// The value type of the InverseMap. typedef typename DescriptorMap::Key Value; /// The key type of the InverseMap. typedef typename DescriptorMap::Value Key; /// \brief Subscript operator. /// /// Subscript operator. It gives back the item /// that the descriptor belongs to currently. Value operator[](const Key& key) const { return inverted(key); } /// \brief Size of the map. /// /// Returns the size of the map. unsigned int size() const { return inverted.size(); } private: const DescriptorMap& inverted; }; /// \brief Gives back the inverse of the map. /// /// Gives back the inverse of the map. const InverseMap inverse() const { return InverseMap(*this); } }; /// \brief Returns the source of the given edge. /// /// The SourceMap gives back the source Node of the given edge. /// \author Balazs Dezso template class SourceMap { public: typedef typename Graph::Node Value; typedef typename Graph::Edge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. explicit SourceMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param edge The edge /// \return The source of the edge Value operator[](const Key& edge) const { return graph.source(edge); } private: const Graph& graph; }; /// \brief Returns a \ref SourceMap class /// /// This function just returns an \ref SourceMap class. /// \relates SourceMap template inline SourceMap sourceMap(const Graph& graph) { return SourceMap(graph); } /// \brief Returns the target of the given edge. /// /// The TargetMap gives back the target Node of the given edge. /// \author Balazs Dezso template class TargetMap { public: typedef typename Graph::Node Value; typedef typename Graph::Edge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. explicit TargetMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param e The edge /// \return The target of the edge Value operator[](const Key& e) const { return graph.target(e); } private: const Graph& graph; }; /// \brief Returns a \ref TargetMap class /// /// This function just returns a \ref TargetMap class. /// \relates TargetMap template inline TargetMap targetMap(const Graph& graph) { return TargetMap(graph); } /// \brief Returns the "forward" directed edge view of an undirected edge. /// /// Returns the "forward" directed edge view of an undirected edge. /// \author Balazs Dezso template class ForwardMap { public: typedef typename Graph::Edge Value; typedef typename Graph::UEdge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. explicit ForwardMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param key An undirected edge /// \return The "forward" directed edge view of undirected edge Value operator[](const Key& key) const { return graph.direct(key, true); } private: const Graph& graph; }; /// \brief Returns a \ref ForwardMap class /// /// This function just returns an \ref ForwardMap class. /// \relates ForwardMap template inline ForwardMap forwardMap(const Graph& graph) { return ForwardMap(graph); } /// \brief Returns the "backward" directed edge view of an undirected edge. /// /// Returns the "backward" directed edge view of an undirected edge. /// \author Balazs Dezso template class BackwardMap { public: typedef typename Graph::Edge Value; typedef typename Graph::UEdge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. explicit BackwardMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param key An undirected edge /// \return The "backward" directed edge view of undirected edge Value operator[](const Key& key) const { return graph.direct(key, false); } private: const Graph& graph; }; /// \brief Returns a \ref BackwardMap class /// This function just returns a \ref BackwardMap class. /// \relates BackwardMap template inline BackwardMap backwardMap(const Graph& graph) { return BackwardMap(graph); } /// \brief Potential difference map /// /// If there is an potential map on the nodes then we /// can get an edge map as we get the substraction of the /// values of the target and source. template class PotentialDifferenceMap { public: typedef typename Graph::Edge Key; typedef typename NodeMap::Value Value; /// \brief Constructor /// /// Contructor of the map explicit PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) : graph(_graph), potential(_potential) {} /// \brief Const subscription operator /// /// Const subscription operator Value operator[](const Key& edge) const { return potential[graph.target(edge)] - potential[graph.source(edge)]; } private: const Graph& graph; const NodeMap& potential; }; /// \brief Just returns a PotentialDifferenceMap /// /// Just returns a PotentialDifferenceMap /// \relates PotentialDifferenceMap template PotentialDifferenceMap potentialDifferenceMap(const Graph& graph, const NodeMap& potential) { return PotentialDifferenceMap(graph, potential); } /// \brief Map of the node in-degrees. /// /// This map returns the in-degree of a node. Once it is constructed, /// the degrees are stored in a standard NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the graph changes. /// /// \warning Besides addNode() and addEdge(), a graph structure may provide /// alternative ways to modify the graph. The correct behavior of InDegMap /// is not guarantied if these additional features are used. For example /// the functions \ref ListGraph::changeSource() "changeSource()", /// \ref ListGraph::changeTarget() "changeTarget()" and /// \ref ListGraph::reverseEdge() "reverseEdge()" /// of \ref ListGraph will \e not update the degree values correctly. /// /// \sa OutDegMap template class InDegMap : protected ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase { public: typedef _Graph Graph; typedef int Value; typedef typename Graph::Node Key; typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase Parent; private: class AutoNodeMap : public DefaultMap<_Graph, Key, int> { public: typedef DefaultMap<_Graph, Key, int> Parent; typedef typename Parent::Graph Graph; AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} virtual void add(const Key& key) { Parent::add(key); Parent::set(key, 0); } virtual void add(const std::vector& keys) { Parent::add(keys); for (int i = 0; i < (int)keys.size(); ++i) { Parent::set(keys[i], 0); } } }; public: /// \brief Constructor. /// /// Constructor for creating in-degree map. explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { Parent::attach(graph.getNotifier(typename _Graph::Edge())); for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countInEdges(graph, it); } } /// Gives back the in-degree of a Node. int operator[](const Key& key) const { return deg[key]; } protected: typedef typename Graph::Edge Edge; virtual void add(const Edge& edge) { ++deg[graph.target(edge)]; } virtual void add(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { ++deg[graph.target(edges[i])]; } } virtual void erase(const Edge& edge) { --deg[graph.target(edge)]; } virtual void erase(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { --deg[graph.target(edges[i])]; } } virtual void build() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countInEdges(graph, it); } } virtual void clear() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = 0; } } private: const _Graph& graph; AutoNodeMap deg; }; /// \brief Map of the node out-degrees. /// /// This map returns the out-degree of a node. Once it is constructed, /// the degrees are stored in a standard NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the graph changes. /// /// \warning Besides addNode() and addEdge(), a graph structure may provide /// alternative ways to modify the graph. The correct behavior of OutDegMap /// is not guarantied if these additional features are used. For example /// the functions \ref ListGraph::changeSource() "changeSource()", /// \ref ListGraph::changeTarget() "changeTarget()" and /// \ref ListGraph::reverseEdge() "reverseEdge()" /// of \ref ListGraph will \e not update the degree values correctly. /// /// \sa InDegMap template class OutDegMap : protected ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase { public: typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase Parent; typedef _Graph Graph; typedef int Value; typedef typename Graph::Node Key; private: class AutoNodeMap : public DefaultMap<_Graph, Key, int> { public: typedef DefaultMap<_Graph, Key, int> Parent; typedef typename Parent::Graph Graph; AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} virtual void add(const Key& key) { Parent::add(key); Parent::set(key, 0); } virtual void add(const std::vector& keys) { Parent::add(keys); for (int i = 0; i < (int)keys.size(); ++i) { Parent::set(keys[i], 0); } } }; public: /// \brief Constructor. /// /// Constructor for creating out-degree map. explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { Parent::attach(graph.getNotifier(typename _Graph::Edge())); for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countOutEdges(graph, it); } } /// Gives back the out-degree of a Node. int operator[](const Key& key) const { return deg[key]; } protected: typedef typename Graph::Edge Edge; virtual void add(const Edge& edge) { ++deg[graph.source(edge)]; } virtual void add(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { ++deg[graph.source(edges[i])]; } } virtual void erase(const Edge& edge) { --deg[graph.source(edge)]; } virtual void erase(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { --deg[graph.source(edges[i])]; } } virtual void build() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countOutEdges(graph, it); } } virtual void clear() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = 0; } } private: const _Graph& graph; AutoNodeMap deg; }; ///Fast edge look up between given endpoints. ///\ingroup gutils ///Using this class, you can find an edge in a graph from a given ///source to a given target in time O(log d), ///where d is the out-degree of the source node. /// ///It is not possible to find \e all parallel edges between two nodes. ///Use \ref AllEdgeLookUp for this purpose. /// ///\warning This class is static, so you should refresh() (or at least ///refresh(Node)) this data structure ///whenever the graph changes. This is a time consuming (superlinearly ///proportional (O(mlogm)) to the number of edges). /// ///\param G The type of the underlying graph. /// ///\sa AllEdgeLookUp template class EdgeLookUp { public: GRAPH_TYPEDEFS(typename G) typedef G Graph; protected: const Graph &_g; typename Graph::template NodeMap _head; typename Graph::template EdgeMap _left; typename Graph::template EdgeMap _right; class EdgeLess { const Graph &g; public: EdgeLess(const Graph &_g) : g(_g) {} bool operator()(Edge a,Edge b) const { return g.target(a) &v,int a,int b) { int m=(a+b)/2; Edge me=v[m]; _left[me] = aO(dlogd), where d is ///the number of the outgoing edges of \c n. void refresh(Node n) { std::vector v; for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e); if(v.size()) { std::sort(v.begin(),v.end(),EdgeLess(_g)); _head[n]=refresh_rec(v,0,v.size()-1); } else _head[n]=INVALID; } ///Refresh the full data structure. ///Build up the full search database. In fact, it simply calls ///\ref refresh(Node) "refresh(n)" for each node \c n. /// ///It runs in time O(mlogD), where m is ///the number of the edges of \c n and D is the maximum ///out-degree of the graph. void refresh() { for(NodeIt n(_g);n!=INVALID;++n) refresh(n); } ///Find an edge between two nodes. ///Find an edge between two nodes in time O(logd), where /// d is the number of outgoing edges of \c s. ///\param s The source node ///\param t The target node ///\return An edge from \c s to \c t if there exists, ///\ref INVALID otherwise. /// ///\warning If you change the graph, refresh() must be called before using ///this operator. If you change the outgoing edges of ///a single node \c n, then ///\ref refresh(Node) "refresh(n)" is enough. /// Edge operator()(Node s, Node t) const { Edge e; for(e=_head[s]; e!=INVALID&&_g.target(e)!=t; e = t < _g.target(e)?_left[e]:_right[e]) ; return e; } }; ///Fast look up of all edges between given endpoints. ///\ingroup gutils ///This class is the same as \ref EdgeLookUp, with the addition ///that it makes it possible to find all edges between given endpoints. /// ///\warning This class is static, so you should refresh() (or at least ///refresh(Node)) this data structure ///whenever the graph changes. This is a time consuming (superlinearly ///proportional (O(mlogm)) to the number of edges). /// ///\param G The type of the underlying graph. /// ///\sa EdgeLookUp template class AllEdgeLookUp : public EdgeLookUp { using EdgeLookUp::_g; using EdgeLookUp::_right; using EdgeLookUp::_left; using EdgeLookUp::_head; GRAPH_TYPEDEFS(typename G) typedef G Graph; typename Graph::template EdgeMap _next; Edge refreshNext(Edge head,Edge next=INVALID) { if(head==INVALID) return next; else { next=refreshNext(_right[head],next); // _next[head]=next; _next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) ? next : INVALID; return refreshNext(_left[head],head); } } void refreshNext() { for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); } public: ///Constructor ///Constructor. /// ///It builds up the search database, which remains valid until the graph ///changes. AllEdgeLookUp(const Graph &g) : EdgeLookUp(g), _next(g) {refreshNext();} ///Refresh the data structure at a node. ///Build up the search database of node \c n. /// ///It runs in time O(dlogd), where d is ///the number of the outgoing edges of \c n. void refresh(Node n) { EdgeLookUp::refresh(n); refreshNext(_head[n]); } ///Refresh the full data structure. ///Build up the full search database. In fact, it simply calls ///\ref refresh(Node) "refresh(n)" for each node \c n. /// ///It runs in time O(mlogD), where m is ///the number of the edges of \c n and D is the maximum ///out-degree of the graph. void refresh() { for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); } ///Find an edge between two nodes. ///Find an edge between two nodes. ///\param s The source node ///\param t The target node ///\param prev The previous edge between \c s and \c t. It it is INVALID or ///not given, the operator finds the first appropriate edge. ///\return An edge from \c s to \c t after \prev or ///\ref INVALID if there is no more. /// ///For example, you can count the number of edges from \c u to \c v in the ///following way. ///\code ///AllEdgeLookUp ae(g); ///... ///int n=0; ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; ///\endcode /// ///Finding the first edge take O(logd) time, where /// d is the number of outgoing edges of \c s. Then, the ///consecutive edges are found in constant time. /// ///\warning If you change the graph, refresh() must be called before using ///this operator. If you change the outgoing edges of ///a single node \c n, then ///\ref refresh(Node) "refresh(n)" is enough. /// #ifdef DOXYGEN Edge operator()(Node s, Node t, Edge prev=INVALID) const {} #else using EdgeLookUp::operator() ; Edge operator()(Node s, Node t, Edge prev) const { return prev==INVALID?(*this)(s,t):_next[prev]; } #endif }; /// @} } //END OF NAMESPACE LEMON #endif