lemon/graph_utils.h
author athos
Thu, 07 Jul 2005 15:58:19 +0000
changeset 1543 a88ccf686a61
parent 1538 777834118f73
child 1547 dd57a540ff5f
permissions -rw-r--r--
lp_test: error messages with appropriate status/value info
     1 /* -*- C++ -*-
     2  * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_GRAPH_UTILS_H
    18 #define LEMON_GRAPH_UTILS_H
    19 
    20 #include <iterator>
    21 #include <vector>
    22 #include <map>
    23 
    24 #include <lemon/invalid.h>
    25 #include <lemon/utility.h>
    26 #include <lemon/maps.h>
    27 #include <lemon/bits/alteration_notifier.h>
    28 
    29 ///\ingroup gutils
    30 ///\file
    31 ///\brief Graph utilities.
    32 ///
    33 ///
    34 
    35 
    36 namespace lemon {
    37 
    38   /// \addtogroup gutils
    39   /// @{
    40 
    41   /// \brief Function to count the items in the graph.
    42   ///
    43   /// This function counts the items (nodes, edges etc) in the graph.
    44   /// The complexity of the function is O(n) because
    45   /// it iterates on all of the items.
    46 
    47   template <typename Graph, typename ItemIt>
    48   inline int countItems(const Graph& g) {
    49     int num = 0;
    50     for (ItemIt it(g); it != INVALID; ++it) {
    51       ++num;
    52     }
    53     return num;
    54   }
    55 
    56   // Node counting:
    57 
    58   template <typename Graph>
    59   inline
    60   typename enable_if<typename Graph::NodeNumTag, int>::type
    61   _countNodes(const Graph &g) {
    62     return g.nodeNum();
    63   }
    64 
    65   template <typename Graph>
    66   inline int _countNodes(Wrap<Graph> w) {
    67     return countItems<Graph, typename Graph::NodeIt>(w.value);
    68   }
    69 
    70   /// \brief Function to count the nodes in the graph.
    71   ///
    72   /// This function counts the nodes in the graph.
    73   /// The complexity of the function is O(n) but for some
    74   /// graph structures it is specialized to run in O(1).
    75   ///
    76   /// \todo refer how to specialize it
    77 
    78   template <typename Graph>
    79   inline int countNodes(const Graph& g) {
    80     return _countNodes<Graph>(g);
    81   }
    82 
    83   // Edge counting:
    84 
    85   template <typename Graph>
    86   inline
    87   typename enable_if<typename Graph::EdgeNumTag, int>::type
    88   _countEdges(const Graph &g) {
    89     return g.edgeNum();
    90   }
    91 
    92   template <typename Graph>
    93   inline int _countEdges(Wrap<Graph> w) {
    94     return countItems<Graph, typename Graph::EdgeIt>(w.value);
    95   }
    96 
    97   /// \brief Function to count the edges in the graph.
    98   ///
    99   /// This function counts the edges in the graph.
   100   /// The complexity of the function is O(e) but for some
   101   /// graph structures it is specialized to run in O(1).
   102 
   103   template <typename Graph>
   104   inline int countEdges(const Graph& g) {
   105     return _countEdges<Graph>(g);
   106   }
   107 
   108   // Undirected edge counting:
   109 
   110   template <typename Graph>
   111   inline
   112   typename enable_if<typename Graph::EdgeNumTag, int>::type
   113   _countUndirEdges(const Graph &g) {
   114     return g.undirEdgeNum();
   115   }
   116 
   117   template <typename Graph>
   118   inline int _countUndirEdges(Wrap<Graph> w) {
   119     return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
   120   }
   121 
   122   /// \brief Function to count the undirected edges in the graph.
   123   ///
   124   /// This function counts the undirected edges in the graph.
   125   /// The complexity of the function is O(e) but for some
   126   /// graph structures it is specialized to run in O(1).
   127 
   128   template <typename Graph>
   129   inline int countUndirEdges(const Graph& g) {
   130     return _countUndirEdges<Graph>(g);
   131   }
   132 
   133 
   134 
   135   template <typename Graph, typename DegIt>
   136   inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
   137     int num = 0;
   138     for (DegIt it(_g, _n); it != INVALID; ++it) {
   139       ++num;
   140     }
   141     return num;
   142   }
   143 
   144   /// \brief Function to count the number of the out-edges from node \c n.
   145   ///
   146   /// This function counts the number of the out-edges from node \c n
   147   /// in the graph.  
   148   template <typename Graph>
   149   inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
   150     return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
   151   }
   152 
   153   /// \brief Function to count the number of the in-edges to node \c n.
   154   ///
   155   /// This function counts the number of the in-edges to node \c n
   156   /// in the graph.  
   157   template <typename Graph>
   158   inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
   159     return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
   160   }
   161 
   162 
   163   /// Finds an edge between two nodes of a graph.
   164 
   165   /// Finds an edge from node \c u to node \c v in graph \c g.
   166   ///
   167   /// If \c prev is \ref INVALID (this is the default value), then
   168   /// it finds the first edge from \c u to \c v. Otherwise it looks for
   169   /// the next edge from \c u to \c v after \c prev.
   170   /// \return The found edge or \ref INVALID if there is no such an edge.
   171   ///
   172   /// Thus you can iterate through each edge from \c u to \c v as it follows.
   173   /// \code
   174   /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
   175   ///   ...
   176   /// }
   177   /// \endcode
   178   /// \todo We may want to use the "GraphBase"
   179   /// interface here...
   180   /// \bug Untested ...
   181   template <typename Graph>
   182   typename Graph::Edge findEdge(const Graph &g,
   183 				typename Graph::Node u, typename Graph::Node v,
   184 				typename Graph::Edge prev = INVALID) 
   185   {
   186     typename Graph::OutEdgeIt e(g,prev);
   187     //    if(prev==INVALID) g.first(e,u);
   188     if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
   189     else ++e;
   190     while(e!=INVALID && g.target(e)!=v) ++e;
   191     return e;
   192   }
   193 
   194   /// \brief Copy a map.
   195   ///
   196   /// Thsi function copies the \c source map to the \c target map. It uses the
   197   /// given iterator to iterate on the data structure and it uses the \c ref
   198   /// mapping to convert the source's keys to the target's keys.
   199   template <typename Target, typename Source, 
   200 	    typename ItemIt, typename Ref>	    
   201   void copyMap(Target& target, const Source& source, 
   202 	       ItemIt it, const Ref& ref) {
   203     for (; it != INVALID; ++it) {
   204       target[ref[it]] = source[it];
   205     }
   206   }
   207 
   208   /// \brief Copy the source map to the target map.
   209   ///
   210   /// Copy the \c source map to the \c target map. It uses the given iterator
   211   /// to iterate on the data structure.
   212   template <typename Target, typename Source, 
   213 	    typename ItemIt>	    
   214   void copyMap(Target& target, const Source& source, ItemIt it) {
   215     for (; it != INVALID; ++it) {
   216       target[it] = source[it];
   217     }
   218   }
   219 
   220 
   221   /// \brief Class to copy a graph.
   222   ///
   223   /// Class to copy a graph to an other graph (duplicate a graph). The
   224   /// simplest way of using it is through the \c copyGraph() function.
   225   template <typename Target, typename Source>
   226   class GraphCopy {
   227   public: 
   228     typedef typename Source::Node Node;
   229     typedef typename Source::NodeIt NodeIt;
   230     typedef typename Source::Edge Edge;
   231     typedef typename Source::EdgeIt EdgeIt;
   232 
   233     typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
   234     typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
   235 
   236     /// \brief Constructor for the GraphCopy.
   237     ///
   238     /// It copies the content of the \c _source graph into the
   239     /// \c _target graph. It creates also two references, one beetween
   240     /// the two nodeset and one beetween the two edgesets.
   241     GraphCopy(Target& _target, const Source& _source) 
   242       : source(_source), target(_target), 
   243 	nodeRefMap(_source), edgeRefMap(_source) {
   244       for (NodeIt it(source); it != INVALID; ++it) {
   245 	nodeRefMap[it] = target.addNode();
   246       }
   247       for (EdgeIt it(source); it != INVALID; ++it) {
   248 	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   249 					nodeRefMap[source.target(it)]);
   250       }
   251     }
   252 
   253     /// \brief Copies the node references into the given map.
   254     ///
   255     /// Copies the node references into the given map.
   256     template <typename NodeRef>
   257     const GraphCopy& nodeRef(NodeRef& map) const {
   258       for (NodeIt it(source); it != INVALID; ++it) {
   259 	map.set(it, nodeRefMap[it]);
   260       }
   261       return *this;
   262     }
   263 
   264     /// \brief Reverse and copies the node references into the given map.
   265     ///
   266     /// Reverse and copies the node references into the given map.
   267     template <typename NodeRef>
   268     const GraphCopy& nodeCrossRef(NodeRef& map) const {
   269       for (NodeIt it(source); it != INVALID; ++it) {
   270 	map.set(nodeRefMap[it], it);
   271       }
   272       return *this;
   273     }
   274 
   275     /// \brief Copies the edge references into the given map.
   276     ///
   277     /// Copies the edge references into the given map.
   278     template <typename EdgeRef>
   279     const GraphCopy& edgeRef(EdgeRef& map) const {
   280       for (EdgeIt it(source); it != INVALID; ++it) {
   281 	map.set(it, edgeRefMap[it]);
   282       }
   283       return *this;
   284     }
   285 
   286     /// \brief Reverse and copies the edge references into the given map.
   287     ///
   288     /// Reverse and copies the edge references into the given map.
   289     template <typename EdgeRef>
   290     const GraphCopy& edgeCrossRef(EdgeRef& map) const {
   291       for (EdgeIt it(source); it != INVALID; ++it) {
   292 	map.set(edgeRefMap[it], it);
   293       }
   294       return *this;
   295     }
   296 
   297     /// \brief Make copy of the given map.
   298     ///
   299     /// Makes copy of the given map for the newly created graph. 
   300     /// The new map's key type is the target graph's node type,
   301     /// and the copied map's key type is the source graph's node
   302     /// type.  
   303     template <typename TargetMap, typename SourceMap>
   304     const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
   305       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
   306       return *this;
   307     }
   308 
   309     /// \brief Make copy of the given map.
   310     ///
   311     /// Makes copy of the given map for the newly created graph. 
   312     /// The new map's key type is the target graph's edge type,
   313     /// and the copied map's key type is the source graph's edge
   314     /// type.  
   315     template <typename TargetMap, typename SourceMap>
   316     const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
   317       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
   318       return *this;
   319     }
   320 
   321     /// \brief Gives back the stored node references.
   322     ///
   323     /// Gives back the stored node references.
   324     const NodeRefMap& nodeRef() const {
   325       return nodeRefMap;
   326     }
   327 
   328     /// \brief Gives back the stored edge references.
   329     ///
   330     /// Gives back the stored edge references.
   331     const EdgeRefMap& edgeRef() const {
   332       return edgeRefMap;
   333     }
   334 
   335   private:
   336     
   337     const Source& source;
   338     Target& target;
   339 
   340     NodeRefMap nodeRefMap;
   341     EdgeRefMap edgeRefMap;
   342   };
   343 
   344   /// \brief Copy a graph to an other graph.
   345   ///
   346   /// Copy a graph to an other graph.
   347   /// The usage of the function:
   348   /// 
   349   /// \code
   350   /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
   351   /// \endcode
   352   /// 
   353   /// After the copy the \c nr map will contain the mapping from the
   354   /// source graph's nodes to the target graph's nodes and the \c ecr will
   355   /// contain the mapping from the target graph's edges to the source's
   356   /// edges.
   357   template <typename Target, typename Source>
   358   GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
   359     return GraphCopy<Target, Source>(target, source);
   360   }
   361 
   362   template <typename _Graph, typename _Item>
   363   class ItemSetTraits {};
   364   
   365   template <typename _Graph>
   366   class ItemSetTraits<_Graph, typename _Graph::Node> {
   367   public:
   368     
   369     typedef _Graph Graph;
   370 
   371     typedef typename Graph::Node Item;
   372     typedef typename Graph::NodeIt ItemIt;
   373 
   374     template <typename _Value>
   375     class Map : public Graph::template NodeMap<_Value> {
   376     public:
   377       typedef typename Graph::template NodeMap<_Value> Parent; 
   378       typedef typename Parent::Value Value;
   379 
   380       Map(const Graph& _graph) : Parent(_graph) {}
   381       Map(const Graph& _graph, const Value& _value) 
   382 	: Parent(_graph, _value) {}
   383     };
   384 
   385   };
   386 
   387   template <typename _Graph>
   388   class ItemSetTraits<_Graph, typename _Graph::Edge> {
   389   public:
   390     
   391     typedef _Graph Graph;
   392 
   393     typedef typename Graph::Edge Item;
   394     typedef typename Graph::EdgeIt ItemIt;
   395 
   396     template <typename _Value>
   397     class Map : public Graph::template EdgeMap<_Value> {
   398     public:
   399       typedef typename Graph::template EdgeMap<_Value> Parent; 
   400       typedef typename Parent::Value Value;
   401 
   402       Map(const Graph& _graph) : Parent(_graph) {}
   403       Map(const Graph& _graph, const Value& _value) 
   404 	: Parent(_graph, _value) {}
   405     };
   406 
   407   };
   408 
   409   template <typename _Graph>
   410   class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
   411   public:
   412     
   413     typedef _Graph Graph;
   414 
   415     typedef typename Graph::UndirEdge Item;
   416     typedef typename Graph::UndirEdgeIt ItemIt;
   417 
   418     template <typename _Value>
   419     class Map : public Graph::template UndirEdgeMap<_Value> {
   420     public:
   421       typedef typename Graph::template UndirEdgeMap<_Value> Parent; 
   422       typedef typename Parent::Value Value;
   423 
   424       Map(const Graph& _graph) : Parent(_graph) {}
   425       Map(const Graph& _graph, const Value& _value) 
   426 	: Parent(_graph, _value) {}
   427     };
   428 
   429   };
   430 
   431   /// @}
   432 
   433   /// \addtogroup graph_maps
   434   /// @{
   435 
   436   template <typename Map, typename Enable = void>
   437   struct ReferenceMapTraits {
   438     typedef typename Map::Value Value;
   439     typedef typename Map::Value& Reference;
   440     typedef const typename Map::Value& ConstReference;
   441     typedef typename Map::Value* Pointer;
   442     typedef const typename Map::Value* ConstPointer;
   443   };
   444 
   445   template <typename Map>
   446   struct ReferenceMapTraits<
   447     Map, 
   448     typename enable_if<typename Map::FullTypeTag, void>::type
   449   > {
   450     typedef typename Map::Value Value;
   451     typedef typename Map::Reference Reference;
   452     typedef typename Map::ConstReference ConstReference;
   453     typedef typename Map::Pointer Pointer;
   454     typedef typename Map::ConstPointer ConstPointer;
   455   };
   456 
   457   /// Provides an immutable and unique id for each item in the graph.
   458 
   459   /// The IdMap class provides a unique and immutable id for each item of the
   460   /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
   461   /// different items (nodes) get different ids <li>\b immutable: the id of an
   462   /// item (node) does not change (even if you delete other nodes).  </ul>
   463   /// Through this map you get access (i.e. can read) the inner id values of
   464   /// the items stored in the graph. This map can be inverted with its member
   465   /// class \c InverseMap.
   466   ///
   467   template <typename _Graph, typename _Item>
   468   class IdMap {
   469   public:
   470     typedef _Graph Graph;
   471     typedef int Value;
   472     typedef _Item Item;
   473     typedef _Item Key;
   474 
   475     typedef True NeedCopy;
   476 
   477     /// \brief Constructor.
   478     ///
   479     /// Constructor for creating id map.
   480     IdMap(const Graph& _graph) : graph(&_graph) {}
   481 
   482     /// \brief Gives back the \e id of the item.
   483     ///
   484     /// Gives back the immutable and unique \e id of the map.
   485     int operator[](const Item& item) const { return graph->id(item);}
   486 
   487 
   488   private:
   489     const Graph* graph;
   490 
   491   public:
   492 
   493     /// \brief The class represents the inverse of its owner (IdMap).
   494     ///
   495     /// The class represents the inverse of its owner (IdMap).
   496     /// \see inverse()
   497     class InverseMap {
   498     public:
   499 
   500       typedef True NeedCopy;
   501 
   502       /// \brief Constructor.
   503       ///
   504       /// Constructor for creating an id-to-item map.
   505       InverseMap(const Graph& _graph) : graph(&_graph) {}
   506 
   507       /// \brief Constructor.
   508       ///
   509       /// Constructor for creating an id-to-item map.
   510       InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
   511 
   512       /// \brief Gives back the given item from its id.
   513       ///
   514       /// Gives back the given item from its id.
   515       /// 
   516       Item operator[](int id) const { return graph->fromId(id, Item());}
   517     private:
   518       const Graph* graph;
   519     };
   520 
   521     /// \brief Gives back the inverse of the map.
   522     ///
   523     /// Gives back the inverse of the IdMap.
   524     InverseMap inverse() const { return InverseMap(*graph);} 
   525 
   526   };
   527 
   528   
   529   /// \brief General invertable graph-map type.
   530 
   531   /// This type provides simple invertable graph-maps. 
   532   /// The InvertableMap wraps an arbitrary ReadWriteMap 
   533   /// and if a key is set to a new value then store it
   534   /// in the inverse map.
   535   /// \param _Graph The graph type.
   536   /// \param _Map The map to extend with invertable functionality. 
   537   template <
   538     typename _Graph,
   539     typename _Item, 
   540     typename _Value,
   541     typename _Map 
   542     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
   543   >
   544   class InvertableMap : protected _Map {
   545 
   546   public:
   547  
   548     typedef _Map Map;
   549     typedef _Graph Graph;
   550 
   551     /// The key type of InvertableMap (Node, Edge, UndirEdge).
   552     typedef typename _Map::Key Key;
   553     /// The value type of the InvertableMap.
   554     typedef typename _Map::Value Value;
   555 
   556     /// \brief Constructor.
   557     ///
   558     /// Construct a new InvertableMap for the graph.
   559     ///
   560     InvertableMap(const Graph& graph) : Map(graph) {} 
   561     
   562     /// \brief The setter function of the map.
   563     ///
   564     /// Sets the mapped value.
   565     void set(const Key& key, const Value& val) {
   566       Value oldval = Map::operator[](key);
   567       typename Container::iterator it = invMap.find(oldval);
   568       if (it != invMap.end() && it->second == key) {
   569 	invMap.erase(it);
   570       }      
   571       invMap.insert(make_pair(val, key));
   572       Map::set(key, val);
   573     }
   574 
   575     /// \brief The getter function of the map.
   576     ///
   577     /// It gives back the value associated with the key.
   578     const Value operator[](const Key& key) const {
   579       return Map::operator[](key);
   580     }
   581 
   582   protected:
   583 
   584     /// \brief Add a new key to the map.
   585     ///
   586     /// Add a new key to the map. It is called by the
   587     /// \c AlterationNotifier.
   588     virtual void add(const Key& key) {
   589       Map::add(key);
   590     }
   591 
   592     /// \brief Erase the key from the map.
   593     ///
   594     /// Erase the key to the map. It is called by the
   595     /// \c AlterationNotifier.
   596     virtual void erase(const Key& key) {
   597       Value val = Map::operator[](key);
   598       typename Container::iterator it = invMap.find(val);
   599       if (it != invMap.end() && it->second == key) {
   600 	invMap.erase(it);
   601       }
   602       Map::erase(key);
   603     }
   604 
   605     /// \brief Clear the keys from the map and inverse map.
   606     ///
   607     /// Clear the keys from the map and inverse map. It is called by the
   608     /// \c AlterationNotifier.
   609     virtual void clear() {
   610       invMap.clear();
   611       Map::clear();
   612     }
   613 
   614   private:
   615     
   616     typedef std::map<Value, Key> Container;
   617     Container invMap;    
   618 
   619   public:
   620 
   621     /// \brief The inverse map type.
   622     ///
   623     /// The inverse of this map. The subscript operator of the map
   624     /// gives back always the item what was last assigned to the value. 
   625     class InverseMap {
   626     public:
   627       /// \brief Constructor of the InverseMap.
   628       ///
   629       /// Constructor of the InverseMap.
   630       InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
   631 
   632       /// The value type of the InverseMap.
   633       typedef typename InvertableMap::Key Value;
   634       /// The key type of the InverseMap.
   635       typedef typename InvertableMap::Value Key; 
   636 
   637       /// \brief Subscript operator. 
   638       ///
   639       /// Subscript operator. It gives back always the item 
   640       /// what was last assigned to the value.
   641       Value operator[](const Key& key) const {
   642 	typename Container::const_iterator it = inverted.invMap.find(key);
   643 	return it->second;
   644       }
   645       
   646     private:
   647       const InvertableMap& inverted;
   648     };
   649 
   650     /// \brief It gives back the just readeable inverse map.
   651     ///
   652     /// It gives back the just readeable inverse map.
   653     InverseMap inverse() const {
   654       return InverseMap(*this);
   655     } 
   656 
   657 
   658     
   659   };
   660 
   661   /// \brief Provides a mutable, continuous and unique descriptor for each 
   662   /// item in the graph.
   663   ///
   664   /// The DescriptorMap class provides a unique and continuous (but mutable)
   665   /// descriptor (id) for each item of the same type (e.g. node) in the
   666   /// graph. This id is <ul><li>\b unique: different items (nodes) get
   667   /// different ids <li>\b continuous: the range of the ids is the set of
   668   /// integers between 0 and \c n-1, where \c n is the number of the items of
   669   /// this type (e.g. nodes) (so the id of a node can change if you delete an
   670   /// other node, i.e. this id is mutable).  </ul> This map can be inverted
   671   /// with its member class \c InverseMap.
   672   ///
   673   /// \param _Graph The graph class the \c DescriptorMap belongs to.
   674   /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
   675   /// UndirEdge.
   676   /// \param _Map A ReadWriteMap mapping from the item type to integer.
   677   template <
   678     typename _Graph,   
   679     typename _Item,
   680     typename _Map 
   681     = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
   682   >
   683   class DescriptorMap : protected _Map {
   684 
   685     typedef _Item Item;
   686     typedef _Map Map;
   687 
   688   public:
   689     /// The graph class of DescriptorMap.
   690     typedef _Graph Graph;
   691 
   692     /// The key type of DescriptorMap (Node, Edge, UndirEdge).
   693     typedef typename _Map::Key Key;
   694     /// The value type of DescriptorMap.
   695     typedef typename _Map::Value Value;
   696 
   697     /// \brief Constructor.
   698     ///
   699     /// Constructor for descriptor map.
   700     DescriptorMap(const Graph& _graph) : Map(_graph) {
   701       build();
   702     }
   703 
   704   protected:
   705 
   706     /// \brief Add a new key to the map.
   707     ///
   708     /// Add a new key to the map. It is called by the
   709     /// \c AlterationNotifier.
   710     virtual void add(const Item& item) {
   711       Map::add(item);
   712       Map::set(item, invMap.size());
   713       invMap.push_back(item);
   714     }
   715 
   716     /// \brief Erase the key from the map.
   717     ///
   718     /// Erase the key to the map. It is called by the
   719     /// \c AlterationNotifier.
   720     virtual void erase(const Item& item) {
   721       Map::set(invMap.back(), Map::operator[](item));
   722       invMap[Map::operator[](item)] = invMap.back();
   723       invMap.pop_back();
   724       Map::erase(item);
   725     }
   726 
   727     /// \brief Build the unique map.
   728     ///
   729     /// Build the unique map. It is called by the
   730     /// \c AlterationNotifier.
   731     virtual void build() {
   732       Map::build();
   733       Item it;
   734       const typename Map::Graph* graph = Map::getGraph(); 
   735       for (graph->first(it); it != INVALID; graph->next(it)) {
   736 	Map::set(it, invMap.size());
   737 	invMap.push_back(it);	
   738       }      
   739     }
   740     
   741     /// \brief Clear the keys from the map.
   742     ///
   743     /// Clear the keys from the map. It is called by the
   744     /// \c AlterationNotifier.
   745     virtual void clear() {
   746       invMap.clear();
   747       Map::clear();
   748     }
   749 
   750   public:
   751 
   752     /// \brief Gives back the \e descriptor of the item.
   753     ///
   754     /// Gives back the mutable and unique \e descriptor of the map.
   755     int operator[](const Item& item) const {
   756       return Map::operator[](item);
   757     }
   758     
   759   private:
   760 
   761     typedef std::vector<Item> Container;
   762     Container invMap;
   763 
   764   public:
   765     /// \brief The inverse map type of DescriptorMap.
   766     ///
   767     /// The inverse map type of DescriptorMap.
   768     class InverseMap {
   769     public:
   770       /// \brief Constructor of the InverseMap.
   771       ///
   772       /// Constructor of the InverseMap.
   773       InverseMap(const DescriptorMap& _inverted) 
   774 	: inverted(_inverted) {}
   775 
   776 
   777       /// The value type of the InverseMap.
   778       typedef typename DescriptorMap::Key Value;
   779       /// The key type of the InverseMap.
   780       typedef typename DescriptorMap::Value Key; 
   781 
   782       /// \brief Subscript operator. 
   783       ///
   784       /// Subscript operator. It gives back the item 
   785       /// that the descriptor belongs to currently.
   786       Value operator[](const Key& key) const {
   787 	return inverted.invMap[key];
   788       }
   789 
   790       /// \brief Size of the map.
   791       ///
   792       /// Returns the size of the map.
   793       unsigned size() const {
   794 	return inverted.invMap.size();
   795       }
   796       
   797     private:
   798       const DescriptorMap& inverted;
   799     };
   800 
   801     /// \brief Gives back the inverse of the map.
   802     ///
   803     /// Gives back the inverse of the map.
   804     const InverseMap inverse() const {
   805       return InverseMap(*this);
   806     }
   807   };
   808 
   809   /// \brief Returns the source of the given edge.
   810   ///
   811   /// The SourceMap gives back the source Node of the given edge. 
   812   /// \author Balazs Dezso
   813   template <typename Graph>
   814   class SourceMap {
   815   public:
   816 
   817     typedef True NeedCopy;
   818 
   819     typedef typename Graph::Node Value;
   820     typedef typename Graph::Edge Key;
   821 
   822     /// \brief Constructor
   823     ///
   824     /// Constructor
   825     /// \param _graph The graph that the map belongs to.
   826     SourceMap(const Graph& _graph) : graph(_graph) {}
   827 
   828     /// \brief The subscript operator.
   829     ///
   830     /// The subscript operator.
   831     /// \param edge The edge 
   832     /// \return The source of the edge 
   833     Value operator[](const Key& edge) {
   834       return graph.source(edge);
   835     }
   836 
   837   private:
   838     const Graph& graph;
   839   };
   840 
   841   /// \brief Returns a \ref SourceMap class
   842   ///
   843   /// This function just returns an \ref SourceMap class.
   844   /// \relates SourceMap
   845   template <typename Graph>
   846   inline SourceMap<Graph> sourceMap(const Graph& graph) {
   847     return SourceMap<Graph>(graph);
   848   } 
   849 
   850   /// \brief Returns the target of the given edge.
   851   ///
   852   /// The TargetMap gives back the target Node of the given edge. 
   853   /// \author Balazs Dezso
   854   template <typename Graph>
   855   class TargetMap {
   856   public:
   857 
   858     typedef True NeedCopy;
   859 
   860     typedef typename Graph::Node Value;
   861     typedef typename Graph::Edge Key;
   862 
   863     /// \brief Constructor
   864     ///
   865     /// Constructor
   866     /// \param _graph The graph that the map belongs to.
   867     TargetMap(const Graph& _graph) : graph(_graph) {}
   868 
   869     /// \brief The subscript operator.
   870     ///
   871     /// The subscript operator.
   872     /// \param e The edge 
   873     /// \return The target of the edge 
   874     Value operator[](const Key& e) {
   875       return graph.target(e);
   876     }
   877 
   878   private:
   879     const Graph& graph;
   880   };
   881 
   882   /// \brief Returns a \ref TargetMap class
   883   ///
   884   /// This function just returns a \ref TargetMap class.
   885   /// \relates TargetMap
   886   template <typename Graph>
   887   inline TargetMap<Graph> targetMap(const Graph& graph) {
   888     return TargetMap<Graph>(graph);
   889   }
   890 
   891   /// \brief Returns the "forward" directed edge view of an undirected edge.
   892   ///
   893   /// Returns the "forward" directed edge view of an undirected edge.
   894   /// \author Balazs Dezso
   895   template <typename Graph>
   896   class ForwardMap {
   897   public:
   898 
   899     typedef True NeedCopy;
   900 
   901     typedef typename Graph::Edge Value;
   902     typedef typename Graph::UndirEdge Key;
   903 
   904     /// \brief Constructor
   905     ///
   906     /// Constructor
   907     /// \param _graph The graph that the map belongs to.
   908     ForwardMap(const Graph& _graph) : graph(_graph) {}
   909 
   910     /// \brief The subscript operator.
   911     ///
   912     /// The subscript operator.
   913     /// \param key An undirected edge 
   914     /// \return The "forward" directed edge view of undirected edge 
   915     Value operator[](const Key& key) const {
   916       return graph.edgeWithSource(key, graph.source(key));
   917     }
   918 
   919   private:
   920     const Graph& graph;
   921   };
   922 
   923   /// \brief Returns a \ref ForwardMap class
   924   ///
   925   /// This function just returns an \ref ForwardMap class.
   926   /// \relates ForwardMap
   927   template <typename Graph>
   928   inline ForwardMap<Graph> forwardMap(const Graph& graph) {
   929     return ForwardMap<Graph>(graph);
   930   }
   931 
   932   /// \brief Returns the "backward" directed edge view of an undirected edge.
   933   ///
   934   /// Returns the "backward" directed edge view of an undirected edge.
   935   /// \author Balazs Dezso
   936   template <typename Graph>
   937   class BackwardMap {
   938   public:
   939     typedef True NeedCopy;
   940 
   941     typedef typename Graph::Edge Value;
   942     typedef typename Graph::UndirEdge Key;
   943 
   944     /// \brief Constructor
   945     ///
   946     /// Constructor
   947     /// \param _graph The graph that the map belongs to.
   948     BackwardMap(const Graph& _graph) : graph(_graph) {}
   949 
   950     /// \brief The subscript operator.
   951     ///
   952     /// The subscript operator.
   953     /// \param key An undirected edge 
   954     /// \return The "backward" directed edge view of undirected edge 
   955     Value operator[](const Key& key) const {
   956       return graph.edgeWithSource(key, graph.target(key));
   957     }
   958 
   959   private:
   960     const Graph& graph;
   961   };
   962 
   963   /// \brief Returns a \ref BackwardMap class
   964 
   965   /// This function just returns a \ref BackwardMap class.
   966   /// \relates BackwardMap
   967   template <typename Graph>
   968   inline BackwardMap<Graph> backwardMap(const Graph& graph) {
   969     return BackwardMap<Graph>(graph);
   970   }
   971 
   972   template <typename _Graph>
   973   class DegMapBase {
   974   public:
   975     
   976     typedef _Graph Graph;
   977 
   978   protected:
   979 
   980     typedef typename Graph::template NodeMap<int> IntNodeMap;
   981     
   982   };
   983 
   984   /// \brief Map of the node in-degrees.
   985   ///
   986   /// This map returns the in-degree of a node. Once it is constructed,
   987   /// the degrees are stored in a standard NodeMap, so each query is done
   988   /// in constant time. On the other hand, the values are updated automatically
   989   /// whenever the graph changes.
   990   ///
   991   /// \sa OutDegMap
   992 
   993   template <typename _Graph>
   994   class InDegMap  
   995     : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
   996 
   997   public:
   998     
   999     typedef _Graph Graph;
  1000     typedef int Value;
  1001     typedef typename Graph::Node Key;
  1002 
  1003   private:
  1004 
  1005     class AutoNodeMap : public Graph::template NodeMap<int> {
  1006     public:
  1007 
  1008       typedef typename Graph::template NodeMap<int> Parent;
  1009 
  1010       typedef typename Parent::Key Key;
  1011       typedef typename Parent::Value Value;
  1012       
  1013       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
  1014       
  1015       void add(const Key& key) {
  1016 	Parent::add(key);
  1017 	Parent::set(key, 0);
  1018       }
  1019     };
  1020 
  1021   public:
  1022 
  1023     /// \brief Constructor.
  1024     ///
  1025     /// Constructor for creating in-degree map.
  1026     InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1027       AlterationNotifier<typename _Graph::Edge>
  1028 	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
  1029       
  1030       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1031 	deg[it] = countInEdges(graph, it);
  1032       }
  1033     }
  1034 
  1035     virtual ~InDegMap() {
  1036       AlterationNotifier<typename _Graph::Edge>::
  1037 	ObserverBase::detach();
  1038     }
  1039     
  1040     /// Gives back the in-degree of a Node.
  1041     int operator[](const Key& key) const {
  1042       return deg[key];
  1043     }
  1044 
  1045   protected:
  1046     
  1047     typedef typename Graph::Edge Edge;
  1048 
  1049     virtual void add(const Edge& edge) {
  1050       ++deg[graph.target(edge)];
  1051     }
  1052 
  1053     virtual void erase(const Edge& edge) {
  1054       --deg[graph.target(edge)];
  1055     }
  1056 
  1057 
  1058     virtual void build() {
  1059       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1060 	deg[it] = countInEdges(graph, it);
  1061       }      
  1062     }
  1063 
  1064     virtual void clear() {
  1065       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1066 	deg[it] = 0;
  1067       }
  1068     }
  1069   private:
  1070     
  1071     const _Graph& graph;
  1072     AutoNodeMap deg;
  1073   };
  1074 
  1075 
  1076   /// \brief Map of the node out-degrees.
  1077   ///
  1078   /// This map returns the out-degree of a node. Once it is constructed,
  1079   /// the degrees are stored in a standard NodeMap, so each query is done
  1080   /// in constant time. On the other hand, the values are updated automatically
  1081   /// whenever the graph changes.
  1082   ///
  1083   /// \sa OutDegMap
  1084 
  1085   template <typename _Graph>
  1086   class OutDegMap  
  1087     : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
  1088 
  1089   public:
  1090     
  1091     typedef _Graph Graph;
  1092     typedef int Value;
  1093     typedef typename Graph::Node Key;
  1094 
  1095   private:
  1096 
  1097     class AutoNodeMap : public Graph::template NodeMap<int> {
  1098     public:
  1099 
  1100       typedef typename Graph::template NodeMap<int> Parent;
  1101 
  1102       typedef typename Parent::Key Key;
  1103       typedef typename Parent::Value Value;
  1104       
  1105       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
  1106       
  1107       void add(const Key& key) {
  1108 	Parent::add(key);
  1109 	Parent::set(key, 0);
  1110       }
  1111     };
  1112 
  1113   public:
  1114 
  1115     /// \brief Constructor.
  1116     ///
  1117     /// Constructor for creating out-degree map.
  1118     OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1119       AlterationNotifier<typename _Graph::Edge>
  1120 	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
  1121       
  1122       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1123 	deg[it] = countOutEdges(graph, it);
  1124       }
  1125     }
  1126 
  1127     virtual ~OutDegMap() {
  1128       AlterationNotifier<typename _Graph::Edge>::
  1129 	ObserverBase::detach();
  1130     }
  1131     
  1132     /// Gives back the in-degree of a Node.
  1133     int operator[](const Key& key) const {
  1134       return deg[key];
  1135     }
  1136 
  1137   protected:
  1138     
  1139     typedef typename Graph::Edge Edge;
  1140 
  1141     virtual void add(const Edge& edge) {
  1142       ++deg[graph.source(edge)];
  1143     }
  1144 
  1145     virtual void erase(const Edge& edge) {
  1146       --deg[graph.source(edge)];
  1147     }
  1148 
  1149 
  1150     virtual void build() {
  1151       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1152 	deg[it] = countOutEdges(graph, it);
  1153       }      
  1154     }
  1155 
  1156     virtual void clear() {
  1157       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1158 	deg[it] = 0;
  1159       }
  1160     }
  1161   private:
  1162     
  1163     const _Graph& graph;
  1164     AutoNodeMap deg;
  1165   };
  1166 
  1167   /// @}
  1168 
  1169 } //END OF NAMESPACE LEMON
  1170 
  1171 #endif