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