/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2006 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_GRAPH_UTILS_H #define LEMON_GRAPH_UTILS_H #include #include #include #include #include #include #include #include #include #include ///\ingroup gutils ///\file ///\brief Graph utilities. /// /// namespace lemon { /// \addtogroup gutils /// @{ ///Creates convenience typedefs for the graph types and iterators ///This \c \#define creates convenience typedefs for the following types ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt, ///\c OutEdgeIt ///\note If \c G it a template parameter, it should be used in this way. ///\code /// GRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define GRAPH_TYPEDEFS(Graph) \ typedef Graph:: Node Node; \ typedef Graph:: NodeIt NodeIt; \ typedef Graph:: Edge Edge; \ typedef Graph:: EdgeIt EdgeIt; \ typedef Graph:: InEdgeIt InEdgeIt; \ typedef Graph::OutEdgeIt OutEdgeIt; ///Creates convenience typedefs for the undirected graph types and iterators ///This \c \#define creates the same convenience typedefs as defined by ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates ///\c UEdge, \c UEdgeIt, \c IncEdgeIt, /// ///\note If \c G it a template parameter, it should be used in this way. ///\code /// UGRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define UGRAPH_TYPEDEFS(Graph) \ GRAPH_TYPEDEFS(Graph) \ typedef Graph:: UEdge UEdge; \ typedef Graph:: UEdgeIt UEdgeIt; \ typedef Graph:: IncEdgeIt IncEdgeIt; // typedef Graph::template UEdgeMap BoolUEdgeMap; // typedef Graph::template UEdgeMap IntUEdgeMap; // typedef Graph::template UEdgeMap DoubleUEdgeMap; ///\brief Creates convenience typedefs for the bipartite undirected graph ///types and iterators ///This \c \#define creates the same convenience typedefs as defined by ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates ///\c ANodeIt, \c BNodeIt, /// ///\note If \c G it a template parameter, it should be used in this way. ///\code /// BPUGRAPH_TYPEDEFS(typename G) ///\endcode /// ///\warning There are no typedefs for the graph maps because of the lack of ///template typedefs in C++. #define BPUGRAPH_TYPEDEFS(Graph) \ UGRAPH_TYPEDEFS(Graph) \ typedef Graph::ANodeIt ANodeIt; \ typedef Graph::BNodeIt BNodeIt; /// \brief Function to count the items in the graph. /// /// This function counts the items (nodes, edges etc) in the graph. /// The complexity of the function is O(n) because /// it iterates on all of the items. template inline int countItems(const Graph& g) { typedef typename ItemSetTraits::ItemIt ItemIt; int num = 0; for (ItemIt it(g); it != INVALID; ++it) { ++num; } return num; } // Node counting: namespace _graph_utils_bits { template struct CountNodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountNodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.nodeNum(); } }; } /// \brief Function to count the nodes in the graph. /// /// This function counts the nodes in the graph. /// The complexity of the function is O(n) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countNodes(const Graph& g) { return _graph_utils_bits::CountNodesSelector::count(g); } namespace _graph_utils_bits { template struct CountANodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountANodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.aNodeNum(); } }; } /// \brief Function to count the anodes in the graph. /// /// This function counts the anodes in the graph. /// The complexity of the function is O(an) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countANodes(const Graph& g) { return _graph_utils_bits::CountANodesSelector::count(g); } namespace _graph_utils_bits { template struct CountBNodesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountBNodesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.bNodeNum(); } }; } /// \brief Function to count the bnodes in the graph. /// /// This function counts the bnodes in the graph. /// The complexity of the function is O(bn) but for some /// graph structures it is specialized to run in O(1). /// /// \todo refer how to specialize it template inline int countBNodes(const Graph& g) { return _graph_utils_bits::CountBNodesSelector::count(g); } // Edge counting: namespace _graph_utils_bits { template struct CountEdgesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountEdgesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.edgeNum(); } }; } /// \brief Function to count the edges in the graph. /// /// This function counts the edges in the graph. /// The complexity of the function is O(e) but for some /// graph structures it is specialized to run in O(1). template inline int countEdges(const Graph& g) { return _graph_utils_bits::CountEdgesSelector::count(g); } // Undirected edge counting: namespace _graph_utils_bits { template struct CountUEdgesSelector { static int count(const Graph &g) { return countItems(g); } }; template struct CountUEdgesSelector< Graph, typename enable_if::type> { static int count(const Graph &g) { return g.uEdgeNum(); } }; } /// \brief Function to count the undirected edges in the graph. /// /// This function counts the undirected edges in the graph. /// The complexity of the function is O(e) but for some /// graph structures it is specialized to run in O(1). template inline int countUEdges(const Graph& g) { return _graph_utils_bits::CountUEdgesSelector::count(g); } template inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { int num = 0; for (DegIt it(_g, _n); it != INVALID; ++it) { ++num; } return num; } /// \brief Function to count the number of the out-edges from node \c n. /// /// This function counts the number of the out-edges from node \c n /// in the graph. template inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } /// \brief Function to count the number of the in-edges to node \c n. /// /// This function counts the number of the in-edges to node \c n /// in the graph. template inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } /// \brief Function to count the number of the inc-edges to node \c n. /// /// This function counts the number of the inc-edges to node \c n /// in the graph. template inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { return countNodeDegree(_g, _n); } namespace _graph_utils_bits { template struct FindEdgeSelector { typedef typename Graph::Node Node; typedef typename Graph::Edge Edge; static Edge find(const Graph &g, Node u, Node v, Edge e) { if (e == INVALID) { g.firstOut(e, u); } else { g.nextOut(e); } while (e != INVALID && g.target(e) != v) { g.nextOut(e); } return e; } }; template struct FindEdgeSelector< Graph, typename enable_if::type> { typedef typename Graph::Node Node; typedef typename Graph::Edge Edge; static Edge find(const Graph &g, Node u, Node v, Edge prev) { return g.findEdge(u, v, prev); } }; } /// \brief Finds an edge between two nodes of a graph. /// /// Finds an edge from node \c u to node \c v in graph \c g. /// /// If \c prev is \ref INVALID (this is the default value), then /// it finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or \ref INVALID if there is no such an edge. /// /// Thus you can iterate through each edge from \c u to \c v as it follows. ///\code /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) { /// ... /// } ///\endcode /// ///\sa ConEdgeIt template inline typename Graph::Edge findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, typename Graph::Edge prev = INVALID) { return _graph_utils_bits::FindEdgeSelector::find(g, u, v, prev); } /// \brief Iterator for iterating on edges connected the same nodes. /// /// Iterator for iterating on edges connected the same nodes. It is /// higher level interface for the findEdge() function. You can /// use it the following way: ///\code /// for (ConEdgeIt it(g, src, trg); it != INVALID; ++it) { /// ... /// } ///\endcode /// ///\sa findEdge() /// /// \author Balazs Dezso template class ConEdgeIt : public _Graph::Edge { public: typedef _Graph Graph; typedef typename Graph::Edge Parent; typedef typename Graph::Edge Edge; typedef typename Graph::Node Node; /// \brief Constructor. /// /// Construct a new ConEdgeIt iterating on the edges which /// connects the \c u and \c v node. ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) { Parent::operator=(findEdge(graph, u, v)); } /// \brief Constructor. /// /// Construct a new ConEdgeIt which continues the iterating from /// the \c e edge. ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {} /// \brief Increment operator. /// /// It increments the iterator and gives back the next edge. ConEdgeIt& operator++() { Parent::operator=(findEdge(graph, graph.source(*this), graph.target(*this), *this)); return *this; } private: const Graph& graph; }; namespace _graph_utils_bits { template struct FindUEdgeSelector { typedef typename Graph::Node Node; typedef typename Graph::UEdge UEdge; static UEdge find(const Graph &g, Node u, Node v, UEdge e) { bool b; if (u != v) { if (e == INVALID) { g.firstInc(e, b, u); } else { b = g.source(e) == u; g.nextInc(e, b); } while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { g.nextInc(e, b); } } else { if (e == INVALID) { g.firstInc(e, b, u); } else { b = true; g.nextInc(e, b); } while (e != INVALID && (!b || g.target(e) != v)) { g.nextInc(e, b); } } return e; } }; template struct FindUEdgeSelector< Graph, typename enable_if::type> { typedef typename Graph::Node Node; typedef typename Graph::UEdge UEdge; static UEdge find(const Graph &g, Node u, Node v, UEdge prev) { return g.findUEdge(u, v, prev); } }; } /// \brief Finds an uedge between two nodes of a graph. /// /// Finds an uedge from node \c u to node \c v in graph \c g. /// If the node \c u and node \c v is equal then each loop edge /// will be enumerated. /// /// If \c prev is \ref INVALID (this is the default value), then /// it finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or \ref INVALID if there is no such an edge. /// /// Thus you can iterate through each edge from \c u to \c v as it follows. ///\code /// for(UEdge e = findUEdge(g,u,v); e != INVALID; /// e = findUEdge(g,u,v,e)) { /// ... /// } ///\endcode /// ///\sa ConEdgeIt template inline typename Graph::UEdge findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, typename Graph::UEdge p = INVALID) { return _graph_utils_bits::FindUEdgeSelector::find(g, u, v, p); } /// \brief Iterator for iterating on uedges connected the same nodes. /// /// Iterator for iterating on uedges connected the same nodes. It is /// higher level interface for the findUEdge() function. You can /// use it the following way: ///\code /// for (ConUEdgeIt it(g, src, trg); it != INVALID; ++it) { /// ... /// } ///\endcode /// ///\sa findUEdge() /// /// \author Balazs Dezso template class ConUEdgeIt : public _Graph::UEdge { public: typedef _Graph Graph; typedef typename Graph::UEdge Parent; typedef typename Graph::UEdge UEdge; typedef typename Graph::Node Node; /// \brief Constructor. /// /// Construct a new ConUEdgeIt iterating on the edges which /// connects the \c u and \c v node. ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) { Parent::operator=(findUEdge(graph, u, v)); } /// \brief Constructor. /// /// Construct a new ConUEdgeIt which continues the iterating from /// the \c e edge. ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {} /// \brief Increment operator. /// /// It increments the iterator and gives back the next edge. ConUEdgeIt& operator++() { Parent::operator=(findUEdge(graph, graph.source(*this), graph.target(*this), *this)); return *this; } private: const Graph& graph; }; /// \brief Copy a map. /// /// This function copies the \c source map to the \c target map. It uses the /// given iterator to iterate on the data structure and it uses the \c ref /// mapping to convert the source's keys to the target's keys. template void copyMap(Target& target, const Source& source, ItemIt it, const Ref& ref) { for (; it != INVALID; ++it) { target[ref[it]] = source[it]; } } /// \brief Copy the source map to the target map. /// /// Copy the \c source map to the \c target map. It uses the given iterator /// to iterate on the data structure. template void copyMap(Target& target, const Source& source, ItemIt it) { for (; it != INVALID; ++it) { target[it] = source[it]; } } /// \brief Class to copy a graph. /// /// Class to copy a graph to another graph (duplicate a graph). The /// simplest way of using it is through the \c copyGraph() function. template class GraphCopy { public: typedef typename Source::Node Node; typedef typename Source::NodeIt NodeIt; typedef typename Source::Edge Edge; typedef typename Source::EdgeIt EdgeIt; typedef typename Source::template NodeMapNodeRefMap; typedef typename Source::template EdgeMapEdgeRefMap; /// \brief Constructor for the GraphCopy. /// /// It copies the content of the \c _source graph into the /// \c _target graph. It creates also two references, one beetween /// the two nodeset and one beetween the two edgesets. GraphCopy(Target& _target, const Source& _source) : source(_source), target(_target), nodeRefMap(_source), edgeRefMap(_source) { for (NodeIt it(source); it != INVALID; ++it) { nodeRefMap[it] = target.addNode(); } for (EdgeIt it(source); it != INVALID; ++it) { edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], nodeRefMap[source.target(it)]); } } /// \brief Copies the node references into the given map. /// /// Copies the node references into the given map. template const GraphCopy& nodeRef(NodeRef& map) const { for (NodeIt it(source); it != INVALID; ++it) { map.set(it, nodeRefMap[it]); } return *this; } /// \brief Reverse and copies the node references into the given map. /// /// Reverse and copies the node references into the given map. template const GraphCopy& nodeCrossRef(NodeRef& map) const { for (NodeIt it(source); it != INVALID; ++it) { map.set(nodeRefMap[it], it); } return *this; } /// \brief Copies the edge references into the given map. /// /// Copies the edge references into the given map. template const GraphCopy& edgeRef(EdgeRef& map) const { for (EdgeIt it(source); it != INVALID; ++it) { map.set(it, edgeRefMap[it]); } return *this; } /// \brief Reverse and copies the edge references into the given map. /// /// Reverse and copies the edge references into the given map. template const GraphCopy& edgeCrossRef(EdgeRef& map) const { for (EdgeIt it(source); it != INVALID; ++it) { map.set(edgeRefMap[it], it); } return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const { copyMap(tMap, sMap, NodeIt(source), nodeRefMap); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const { copyMap(tMap, sMap, EdgeIt(source), edgeRefMap); return *this; } /// \brief Gives back the stored node references. /// /// Gives back the stored node references. const NodeRefMap& nodeRef() const { return nodeRefMap; } /// \brief Gives back the stored edge references. /// /// Gives back the stored edge references. const EdgeRefMap& edgeRef() const { return edgeRefMap; } void run() const {} private: const Source& source; Target& target; NodeRefMap nodeRefMap; EdgeRefMap edgeRefMap; }; /// \brief Copy a graph to another graph. /// /// Copy a graph to another graph. /// The usage of the function: /// ///\code /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr); ///\endcode /// /// After the copy the \c nr map will contain the mapping from the /// source graph's nodes to the target graph's nodes and the \c ecr will /// contain the mapping from the target graph's edges to the source's /// edges. template GraphCopy copyGraph(Target& target, const Source& source) { return GraphCopy(target, source); } /// \brief Class to copy an undirected graph. /// /// Class to copy an undirected graph to another graph (duplicate a graph). /// The simplest way of using it is through the \c copyUGraph() function. template class UGraphCopy { public: typedef typename Source::Node Node; typedef typename Source::NodeIt NodeIt; typedef typename Source::Edge Edge; typedef typename Source::EdgeIt EdgeIt; typedef typename Source::UEdge UEdge; typedef typename Source::UEdgeIt UEdgeIt; typedef typename Source:: template NodeMap NodeRefMap; typedef typename Source:: template UEdgeMap UEdgeRefMap; private: struct EdgeRefMap { EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {} typedef typename Source::Edge Key; typedef typename Target::Edge Value; Value operator[](const Key& key) { return gc.target.direct(gc.uEdgeRef[key], gc.target.direction(key)); } UGraphCopy& gc; }; public: /// \brief Constructor for the UGraphCopy. /// /// It copies the content of the \c _source graph into the /// \c _target graph. It creates also two references, one beetween /// the two nodeset and one beetween the two edgesets. UGraphCopy(Target& _target, const Source& _source) : source(_source), target(_target), nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) { for (NodeIt it(source); it != INVALID; ++it) { nodeRefMap[it] = target.addNode(); } for (UEdgeIt it(source); it != INVALID; ++it) { uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], nodeRefMap[source.target(it)]); } } /// \brief Copies the node references into the given map. /// /// Copies the node references into the given map. template const UGraphCopy& nodeRef(NodeRef& map) const { for (NodeIt it(source); it != INVALID; ++it) { map.set(it, nodeRefMap[it]); } return *this; } /// \brief Reverse and copies the node references into the given map. /// /// Reverse and copies the node references into the given map. template const UGraphCopy& nodeCrossRef(NodeRef& map) const { for (NodeIt it(source); it != INVALID; ++it) { map.set(nodeRefMap[it], it); } return *this; } /// \brief Copies the edge references into the given map. /// /// Copies the edge references into the given map. template const UGraphCopy& edgeRef(EdgeRef& map) const { for (EdgeIt it(source); it != INVALID; ++it) { map.set(edgeRefMap[it], it); } return *this; } /// \brief Reverse and copies the undirected edge references into the /// given map. /// /// Reverse and copies the undirected edge references into the given map. template const UGraphCopy& edgeCrossRef(EdgeRef& map) const { for (EdgeIt it(source); it != INVALID; ++it) { map.set(it, edgeRefMap[it]); } return *this; } /// \brief Copies the undirected edge references into the given map. /// /// Copies the undirected edge references into the given map. template const UGraphCopy& uEdgeRef(EdgeRef& map) const { for (UEdgeIt it(source); it != INVALID; ++it) { map.set(it, uEdgeRefMap[it]); } return *this; } /// \brief Reverse and copies the undirected edge references into the /// given map. /// /// Reverse and copies the undirected edge references into the given map. template const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const { for (UEdgeIt it(source); it != INVALID; ++it) { map.set(uEdgeRefMap[it], it); } return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's node type, /// and the copied map's key type is the source graph's node /// type. template const UGraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const { copyMap(tMap, sMap, NodeIt(source), nodeRefMap); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template const UGraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const { copyMap(tMap, sMap, EdgeIt(source), edgeRefMap); return *this; } /// \brief Make copy of the given map. /// /// Makes copy of the given map for the newly created graph. /// The new map's key type is the target graph's edge type, /// and the copied map's key type is the source graph's edge /// type. template const UGraphCopy& uEdgeMap(TargetMap& tMap, const SourceMap& sMap) const { copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap); return *this; } /// \brief Gives back the stored node references. /// /// Gives back the stored node references. const NodeRefMap& nodeRef() const { return nodeRefMap; } /// \brief Gives back the stored edge references. /// /// Gives back the stored edge references. const EdgeRefMap& edgeRef() const { return edgeRefMap; } /// \brief Gives back the stored uedge references. /// /// Gives back the stored uedge references. const UEdgeRefMap& uEdgeRef() const { return uEdgeRefMap; } void run() const {} private: const Source& source; Target& target; NodeRefMap nodeRefMap; EdgeRefMap edgeRefMap; UEdgeRefMap uEdgeRefMap; }; /// \brief Copy a graph to another graph. /// /// Copy a graph to another graph. /// The usage of the function: /// ///\code /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr); ///\endcode /// /// After the copy the \c nr map will contain the mapping from the /// source graph's nodes to the target graph's nodes and the \c ecr will /// contain the mapping from the target graph's edges to the source's /// edges. template UGraphCopy copyUGraph(Target& target, const Source& source) { return UGraphCopy(target, source); } /// @} /// \addtogroup graph_maps /// @{ /// Provides an immutable and unique id for each item in the graph. /// The IdMap class provides a unique and immutable id for each item of the /// same type (e.g. node) in the graph. This id is
  • \b unique: /// different items (nodes) get different ids
  • \b immutable: the id of an /// item (node) does not change (even if you delete other nodes).
/// Through this map you get access (i.e. can read) the inner id values of /// the items stored in the graph. This map can be inverted with its member /// class \c InverseMap. /// template class IdMap { public: typedef _Graph Graph; typedef int Value; typedef _Item Item; typedef _Item Key; /// \brief Constructor. /// /// Constructor for creating id map. IdMap(const Graph& _graph) : graph(&_graph) {} /// \brief Gives back the \e id of the item. /// /// Gives back the immutable and unique \e id of the map. int operator[](const Item& item) const { return graph->id(item);} private: const Graph* graph; public: /// \brief The class represents the inverse of its owner (IdMap). /// /// The class represents the inverse of its owner (IdMap). /// \see inverse() class InverseMap { public: /// \brief Constructor. /// /// Constructor for creating an id-to-item map. InverseMap(const Graph& _graph) : graph(&_graph) {} /// \brief Constructor. /// /// Constructor for creating an id-to-item map. InverseMap(const IdMap& idMap) : graph(idMap.graph) {} /// \brief Gives back the given item from its id. /// /// Gives back the given item from its id. /// Item operator[](int id) const { return graph->fromId(id, Item());} private: const Graph* graph; }; /// \brief Gives back the inverse of the map. /// /// Gives back the inverse of the IdMap. InverseMap inverse() const { return InverseMap(*graph);} }; /// \brief General invertable graph-map type. /// This type provides simple invertable graph-maps. /// The InvertableMap wraps an arbitrary ReadWriteMap /// and if a key is set to a new value then store it /// in the inverse map. /// /// The values of the map can be accessed /// with stl compatible forward iterator. /// /// \param _Graph The graph type. /// \param _Item The item type of the graph. /// \param _Value The value type of the map. /// /// \see IterableValueMap #ifndef DOXYGEN /// \param _Map A ReadWriteMap mapping from the item type to integer. template < typename _Graph, typename _Item, typename _Value, typename _Map = DefaultMap<_Graph, _Item, _Value> > #else template #endif class InvertableMap : protected _Map { public: /// The key type of InvertableMap (Node, Edge, UEdge). typedef typename _Map::Key Key; /// The value type of the InvertableMap. typedef typename _Map::Value Value; private: typedef _Map Map; typedef _Graph Graph; typedef std::map Container; Container invMap; public: /// \brief Constructor. /// /// Construct a new InvertableMap for the graph. /// InvertableMap(const Graph& graph) : Map(graph) {} /// \brief Forward iterator for values. /// /// This iterator is an stl compatible forward /// iterator on the values of the map. The values can /// be accessed in the [beginValue, endValue) range. /// class ValueIterator : public std::iterator { friend class InvertableMap; private: ValueIterator(typename Container::const_iterator _it) : it(_it) {} public: ValueIterator() {} ValueIterator& operator++() { ++it; return *this; } ValueIterator operator++(int) { ValueIterator tmp(*this); operator++(); return tmp; } const Value& operator*() const { return it->first; } const Value* operator->() const { return &(it->first); } bool operator==(ValueIterator jt) const { return it == jt.it; } bool operator!=(ValueIterator jt) const { return it != jt.it; } private: typename Container::const_iterator it; }; /// \brief Returns an iterator to the first value. /// /// Returns an stl compatible iterator to the /// first value of the map. The values of the /// map can be accessed in the [beginValue, endValue) /// range. ValueIterator beginValue() const { return ValueIterator(invMap.begin()); } /// \brief Returns an iterator after the last value. /// /// Returns an stl compatible iterator after the /// last value of the map. The values of the /// map can be accessed in the [beginValue, endValue) /// range. ValueIterator endValue() const { return ValueIterator(invMap.end()); } /// \brief The setter function of the map. /// /// Sets the mapped value. void set(const Key& key, const Value& val) { Value oldval = Map::operator[](key); typename Container::iterator it = invMap.find(oldval); if (it != invMap.end() && it->second == key) { invMap.erase(it); } invMap.insert(make_pair(val, key)); Map::set(key, val); } /// \brief The getter function of the map. /// /// It gives back the value associated with the key. typename MapTraits::ConstReturnValue operator[](const Key& key) const { return Map::operator[](key); } protected: /// \brief Erase the key from the map. /// /// Erase the key to the map. It is called by the /// \c AlterationNotifier. virtual void erase(const Key& key) { Value val = Map::operator[](key); typename Container::iterator it = invMap.find(val); if (it != invMap.end() && it->second == key) { invMap.erase(it); } Map::erase(key); } /// \brief Erase more keys from the map. /// /// Erase more keys from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const std::vector& keys) { for (int i = 0; i < (int)keys.size(); ++i) { Value val = Map::operator[](keys[i]); typename Container::iterator it = invMap.find(val); if (it != invMap.end() && it->second == keys[i]) { invMap.erase(it); } } Map::erase(keys); } /// \brief Clear the keys from the map and inverse map. /// /// Clear the keys from the map and inverse map. It is called by the /// \c AlterationNotifier. virtual void clear() { invMap.clear(); Map::clear(); } public: /// \brief The inverse map type. /// /// The inverse of this map. The subscript operator of the map /// gives back always the item what was last assigned to the value. class InverseMap { public: /// \brief Constructor of the InverseMap. /// /// Constructor of the InverseMap. InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {} /// The value type of the InverseMap. typedef typename InvertableMap::Key Value; /// The key type of the InverseMap. typedef typename InvertableMap::Value Key; /// \brief Subscript operator. /// /// Subscript operator. It gives back always the item /// what was last assigned to the value. Value operator[](const Key& key) const { typename Container::const_iterator it = inverted.invMap.find(key); return it->second; } private: const InvertableMap& inverted; }; /// \brief It gives back the just readable inverse map. /// /// It gives back the just readable inverse map. InverseMap inverse() const { return InverseMap(*this); } }; /// \brief Provides a mutable, continuous and unique descriptor for each /// item in the graph. /// /// The DescriptorMap class provides a unique and continuous (but mutable) /// descriptor (id) for each item of the same type (e.g. node) in the /// graph. This id is
  • \b unique: different items (nodes) get /// different ids
  • \b continuous: the range of the ids is the set of /// integers between 0 and \c n-1, where \c n is the number of the items of /// this type (e.g. nodes) (so the id of a node can change if you delete an /// other node, i.e. this id is mutable).
This map can be inverted /// with its member class \c InverseMap. /// /// \param _Graph The graph class the \c DescriptorMap belongs to. /// \param _Item The Item is the Key of the Map. It may be Node, Edge or /// UEdge. #ifndef DOXYGEN /// \param _Map A ReadWriteMap mapping from the item type to integer. template < typename _Graph, typename _Item, typename _Map = DefaultMap<_Graph, _Item, int> > #else template #endif class DescriptorMap : protected _Map { typedef _Item Item; typedef _Map Map; public: /// The graph class of DescriptorMap. typedef _Graph Graph; /// The key type of DescriptorMap (Node, Edge, UEdge). typedef typename _Map::Key Key; /// The value type of DescriptorMap. typedef typename _Map::Value Value; /// \brief Constructor. /// /// Constructor for descriptor map. DescriptorMap(const Graph& _graph) : Map(_graph) { build(); } protected: /// \brief Add a new key to the map. /// /// Add a new key to the map. It is called by the /// \c AlterationNotifier. virtual void add(const Item& item) { Map::add(item); Map::set(item, invMap.size()); invMap.push_back(item); } /// \brief Add more new keys to the map. /// /// Add more new keys to the map. It is called by the /// \c AlterationNotifier. virtual void add(const std::vector& items) { Map::add(items); for (int i = 0; i < (int)items.size(); ++i) { Map::set(items[i], invMap.size()); invMap.push_back(items[i]); } } /// \brief Erase the key from the map. /// /// Erase the key from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const Item& item) { Map::set(invMap.back(), Map::operator[](item)); invMap[Map::operator[](item)] = invMap.back(); invMap.pop_back(); Map::erase(item); } /// \brief Erase more keys from the map. /// /// Erase more keys from the map. It is called by the /// \c AlterationNotifier. virtual void erase(const std::vector& items) { for (int i = 0; i < (int)items.size(); ++i) { Map::set(invMap.back(), Map::operator[](items[i])); invMap[Map::operator[](items[i])] = invMap.back(); invMap.pop_back(); } Map::erase(items); } /// \brief Build the unique map. /// /// Build the unique map. It is called by the /// \c AlterationNotifier. virtual void build() { Map::build(); Item it; const typename Map::Notifier* notifier = Map::getNotifier(); for (notifier->first(it); it != INVALID; notifier->next(it)) { Map::set(it, invMap.size()); invMap.push_back(it); } } /// \brief Clear the keys from the map. /// /// Clear the keys from the map. It is called by the /// \c AlterationNotifier. virtual void clear() { invMap.clear(); Map::clear(); } public: /// \brief Returns the maximal value plus one. /// /// Returns the maximal value plus one in the map. unsigned int size() const { return invMap.size(); } /// \brief Swaps the position of the two items in the map. /// /// Swaps the position of the two items in the map. void swap(const Item& p, const Item& q) { int pi = Map::operator[](p); int qi = Map::operator[](q); Map::set(p, qi); invMap[qi] = p; Map::set(q, pi); invMap[pi] = q; } /// \brief Gives back the \e descriptor of the item. /// /// Gives back the mutable and unique \e descriptor of the map. int operator[](const Item& item) const { return Map::operator[](item); } private: typedef std::vector Container; Container invMap; public: /// \brief The inverse map type of DescriptorMap. /// /// The inverse map type of DescriptorMap. class InverseMap { public: /// \brief Constructor of the InverseMap. /// /// Constructor of the InverseMap. InverseMap(const DescriptorMap& _inverted) : inverted(_inverted) {} /// The value type of the InverseMap. typedef typename DescriptorMap::Key Value; /// The key type of the InverseMap. typedef typename DescriptorMap::Value Key; /// \brief Subscript operator. /// /// Subscript operator. It gives back the item /// that the descriptor belongs to currently. Value operator[](const Key& key) const { return inverted.invMap[key]; } /// \brief Size of the map. /// /// Returns the size of the map. unsigned int size() const { return inverted.invMap.size(); } private: const DescriptorMap& inverted; }; /// \brief Gives back the inverse of the map. /// /// Gives back the inverse of the map. const InverseMap inverse() const { return InverseMap(*this); } }; /// \brief Returns the source of the given edge. /// /// The SourceMap gives back the source Node of the given edge. /// \author Balazs Dezso template class SourceMap { public: typedef typename Graph::Node Value; typedef typename Graph::Edge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. SourceMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param edge The edge /// \return The source of the edge Value operator[](const Key& edge) const { return graph.source(edge); } private: const Graph& graph; }; /// \brief Returns a \ref SourceMap class /// /// This function just returns an \ref SourceMap class. /// \relates SourceMap template inline SourceMap sourceMap(const Graph& graph) { return SourceMap(graph); } /// \brief Returns the target of the given edge. /// /// The TargetMap gives back the target Node of the given edge. /// \author Balazs Dezso template class TargetMap { public: typedef typename Graph::Node Value; typedef typename Graph::Edge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. TargetMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param e The edge /// \return The target of the edge Value operator[](const Key& e) const { return graph.target(e); } private: const Graph& graph; }; /// \brief Returns a \ref TargetMap class /// /// This function just returns a \ref TargetMap class. /// \relates TargetMap template inline TargetMap targetMap(const Graph& graph) { return TargetMap(graph); } /// \brief Returns the "forward" directed edge view of an undirected edge. /// /// Returns the "forward" directed edge view of an undirected edge. /// \author Balazs Dezso template class ForwardMap { public: typedef typename Graph::Edge Value; typedef typename Graph::UEdge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. ForwardMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param key An undirected edge /// \return The "forward" directed edge view of undirected edge Value operator[](const Key& key) const { return graph.direct(key, true); } private: const Graph& graph; }; /// \brief Returns a \ref ForwardMap class /// /// This function just returns an \ref ForwardMap class. /// \relates ForwardMap template inline ForwardMap forwardMap(const Graph& graph) { return ForwardMap(graph); } /// \brief Returns the "backward" directed edge view of an undirected edge. /// /// Returns the "backward" directed edge view of an undirected edge. /// \author Balazs Dezso template class BackwardMap { public: typedef typename Graph::Edge Value; typedef typename Graph::UEdge Key; /// \brief Constructor /// /// Constructor /// \param _graph The graph that the map belongs to. BackwardMap(const Graph& _graph) : graph(_graph) {} /// \brief The subscript operator. /// /// The subscript operator. /// \param key An undirected edge /// \return The "backward" directed edge view of undirected edge Value operator[](const Key& key) const { return graph.direct(key, false); } private: const Graph& graph; }; /// \brief Returns a \ref BackwardMap class /// This function just returns a \ref BackwardMap class. /// \relates BackwardMap template inline BackwardMap backwardMap(const Graph& graph) { return BackwardMap(graph); } /// \brief Potential difference map /// /// If there is an potential map on the nodes then we /// can get an edge map as we get the substraction of the /// values of the target and source. template class PotentialDifferenceMap { public: typedef typename Graph::Edge Key; typedef typename NodeMap::Value Value; /// \brief Constructor /// /// Contructor of the map PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) : graph(_graph), potential(_potential) {} /// \brief Const subscription operator /// /// Const subscription operator Value operator[](const Key& edge) const { return potential[graph.target(edge)] - potential[graph.source(edge)]; } private: const Graph& graph; const NodeMap& potential; }; /// \brief Just returns a PotentialDifferenceMap /// /// Just returns a PotentialDifferenceMap /// \relates PotentialDifferenceMap template PotentialDifferenceMap potentialDifferenceMap(const Graph& graph, const NodeMap& potential) { return PotentialDifferenceMap(graph, potential); } /// \brief Map of the node in-degrees. /// /// This map returns the in-degree of a node. Once it is constructed, /// the degrees are stored in a standard NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the graph changes. /// /// \warning Besides addNode() and addEdge(), a graph structure may provide /// alternative ways to modify the graph. The correct behavior of InDegMap /// is not guarantied if these additional features are used. For example /// the functions \ref ListGraph::changeSource() "changeSource()", /// \ref ListGraph::changeTarget() "changeTarget()" and /// \ref ListGraph::reverseEdge() "reverseEdge()" /// of \ref ListGraph will \e not update the degree values correctly. /// /// \sa OutDegMap template class InDegMap : protected ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase { public: typedef _Graph Graph; typedef int Value; typedef typename Graph::Node Key; typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase Parent; private: class AutoNodeMap : public DefaultMap<_Graph, Key, int> { public: typedef DefaultMap<_Graph, Key, int> Parent; typedef typename Parent::Graph Graph; AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} virtual void add(const Key& key) { Parent::add(key); Parent::set(key, 0); } virtual void add(const std::vector& keys) { Parent::add(keys); for (int i = 0; i < (int)keys.size(); ++i) { Parent::set(keys[i], 0); } } }; public: /// \brief Constructor. /// /// Constructor for creating in-degree map. InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { Parent::attach(graph.getNotifier(typename _Graph::Edge())); for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countInEdges(graph, it); } } /// Gives back the in-degree of a Node. int operator[](const Key& key) const { return deg[key]; } protected: typedef typename Graph::Edge Edge; virtual void add(const Edge& edge) { ++deg[graph.target(edge)]; } virtual void add(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { ++deg[graph.target(edges[i])]; } } virtual void erase(const Edge& edge) { --deg[graph.target(edge)]; } virtual void erase(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { --deg[graph.target(edges[i])]; } } virtual void build() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countInEdges(graph, it); } } virtual void clear() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = 0; } } private: const _Graph& graph; AutoNodeMap deg; }; /// \brief Map of the node out-degrees. /// /// This map returns the out-degree of a node. Once it is constructed, /// the degrees are stored in a standard NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the graph changes. /// /// \warning Besides addNode() and addEdge(), a graph structure may provide /// alternative ways to modify the graph. The correct behavior of OutDegMap /// is not guarantied if these additional features are used. For example /// the functions \ref ListGraph::changeSource() "changeSource()", /// \ref ListGraph::changeTarget() "changeTarget()" and /// \ref ListGraph::reverseEdge() "reverseEdge()" /// of \ref ListGraph will \e not update the degree values correctly. /// /// \sa InDegMap template class OutDegMap : protected ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase { public: typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> ::ItemNotifier::ObserverBase Parent; typedef _Graph Graph; typedef int Value; typedef typename Graph::Node Key; private: class AutoNodeMap : public DefaultMap<_Graph, Key, int> { public: typedef DefaultMap<_Graph, Key, int> Parent; typedef typename Parent::Graph Graph; AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} virtual void add(const Key& key) { Parent::add(key); Parent::set(key, 0); } virtual void add(const std::vector& keys) { Parent::add(keys); for (int i = 0; i < (int)keys.size(); ++i) { Parent::set(keys[i], 0); } } }; public: /// \brief Constructor. /// /// Constructor for creating out-degree map. OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { Parent::attach(graph.getNotifier(typename _Graph::Edge())); for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countOutEdges(graph, it); } } /// Gives back the out-degree of a Node. int operator[](const Key& key) const { return deg[key]; } protected: typedef typename Graph::Edge Edge; virtual void add(const Edge& edge) { ++deg[graph.source(edge)]; } virtual void add(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { ++deg[graph.source(edges[i])]; } } virtual void erase(const Edge& edge) { --deg[graph.source(edge)]; } virtual void erase(const std::vector& edges) { for (int i = 0; i < (int)edges.size(); ++i) { --deg[graph.source(edges[i])]; } } virtual void build() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = countOutEdges(graph, it); } } virtual void clear() { for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { deg[it] = 0; } } private: const _Graph& graph; AutoNodeMap deg; }; /// @} } //END OF NAMESPACE LEMON #endif