lemon/graph_utils.h
author deba
Mon, 03 Apr 2006 08:41:52 +0000
changeset 2029 e00114f165f5
parent 2022 0f3367da6104
child 2031 080d51024ac5
permissions -rw-r--r--
Count ANodes-BNodes
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_GRAPH_UTILS_H
    20 #define LEMON_GRAPH_UTILS_H
    21 
    22 #include <iterator>
    23 #include <vector>
    24 #include <map>
    25 #include <cmath>
    26 
    27 #include <lemon/bits/invalid.h>
    28 #include <lemon/bits/utility.h>
    29 #include <lemon/maps.h>
    30 #include <lemon/bits/traits.h>
    31 
    32 #include <lemon/bits/alteration_notifier.h>
    33 #include <lemon/bits/default_map.h>
    34 
    35 ///\ingroup gutils
    36 ///\file
    37 ///\brief Graph utilities.
    38 ///
    39 ///
    40 
    41 
    42 namespace lemon {
    43 
    44   /// \addtogroup gutils
    45   /// @{
    46 
    47   ///Creates convenience typedefs for the graph types and iterators
    48 
    49   ///This \c \#define creates convenience typedefs for the following types
    50   ///of \c Graph: \c Node,  \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
    51   ///\c OutEdgeIt,  \c BoolNodeMap,  \c IntNodeMap,  \c DoubleNodeMap,
    52   ///\c BoolEdgeMap, \c IntEdgeMap,  \c DoubleEdgeMap.  
    53   ///\note If \c G it a template parameter, it should be used in this way.
    54   ///\code
    55   ///  GRAPH_TYPEDEFS(typename G)
    56   ///\endcode
    57   ///
    58   ///\warning There are no typedefs for the graph maps because of the lack of
    59   ///template typedefs in C++.
    60 #define GRAPH_TYPEDEFS(Graph)				\
    61   typedef Graph::     Node      Node;			\
    62     typedef Graph::   NodeIt    NodeIt;			\
    63     typedef Graph::   Edge      Edge;			\
    64     typedef Graph::   EdgeIt    EdgeIt;			\
    65     typedef Graph:: InEdgeIt  InEdgeIt;			\
    66     typedef Graph::OutEdgeIt OutEdgeIt;			
    67 //     typedef Graph::template NodeMap<bool> BoolNodeMap;	       
    68 //     typedef Graph::template NodeMap<int> IntNodeMap;	       
    69 //     typedef Graph::template NodeMap<double> DoubleNodeMap;  
    70 //     typedef Graph::template EdgeMap<bool> BoolEdgeMap;	       
    71 //     typedef Graph::template EdgeMap<int> IntEdgeMap;	       
    72 //     typedef Graph::template EdgeMap<double> DoubleEdgeMap;
    73   
    74   ///Creates convenience typedefs for the undirected graph types and iterators
    75 
    76   ///This \c \#define creates the same convenience typedefs as defined by
    77   ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
    78   ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
    79   ///\c BoolUEdgeMap, \c IntUEdgeMap,  \c DoubleUEdgeMap.  
    80   ///
    81   ///\note If \c G it a template parameter, it should be used in this way.
    82   ///\code
    83   ///  UGRAPH_TYPEDEFS(typename G)
    84   ///\endcode
    85   ///
    86   ///\warning There are no typedefs for the graph maps because of the lack of
    87   ///template typedefs in C++.
    88 #define UGRAPH_TYPEDEFS(Graph)				\
    89   GRAPH_TYPEDEFS(Graph)						\
    90     typedef Graph:: UEdge   UEdge;			\
    91     typedef Graph:: UEdgeIt UEdgeIt;			\
    92     typedef Graph:: IncEdgeIt   IncEdgeIt;		       
    93 //     typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;	 
    94 //     typedef Graph::template UEdgeMap<int> IntUEdgeMap;
    95 //     typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
    96   
    97 
    98 
    99   /// \brief Function to count the items in the graph.
   100   ///
   101   /// This function counts the items (nodes, edges etc) in the graph.
   102   /// The complexity of the function is O(n) because
   103   /// it iterates on all of the items.
   104 
   105   template <typename Graph, typename Item>
   106   inline int countItems(const Graph& g) {
   107     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
   108     int num = 0;
   109     for (ItemIt it(g); it != INVALID; ++it) {
   110       ++num;
   111     }
   112     return num;
   113   }
   114 
   115   // Node counting:
   116 
   117   namespace _graph_utils_bits {
   118     
   119     template <typename Graph, typename Enable = void>
   120     struct CountNodesSelector {
   121       static int count(const Graph &g) {
   122         return countItems<Graph, typename Graph::Node>(g);
   123       }
   124     };
   125 
   126     template <typename Graph>
   127     struct CountNodesSelector<
   128       Graph, typename 
   129       enable_if<typename Graph::NodeNumTag, void>::type> 
   130     {
   131       static int count(const Graph &g) {
   132         return g.nodeNum();
   133       }
   134     };    
   135   }
   136 
   137   /// \brief Function to count the nodes in the graph.
   138   ///
   139   /// This function counts the nodes in the graph.
   140   /// The complexity of the function is O(n) but for some
   141   /// graph structures it is specialized to run in O(1).
   142   ///
   143   /// \todo refer how to specialize it
   144 
   145   template <typename Graph>
   146   inline int countNodes(const Graph& g) {
   147     return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
   148   }
   149 
   150   namespace _graph_utils_bits {
   151     
   152     template <typename Graph, typename Enable = void>
   153     struct CountANodesSelector {
   154       static int count(const Graph &g) {
   155         return countItems<Graph, typename Graph::ANode>(g);
   156       }
   157     };
   158 
   159     template <typename Graph>
   160     struct CountANodesSelector<
   161       Graph, typename 
   162       enable_if<typename Graph::NodeNumTag, void>::type> 
   163     {
   164       static int count(const Graph &g) {
   165         return g.nodeNum();
   166       }
   167     };    
   168   }
   169 
   170   /// \brief Function to count the anodes in the graph.
   171   ///
   172   /// This function counts the anodes in the graph.
   173   /// The complexity of the function is O(an) but for some
   174   /// graph structures it is specialized to run in O(1).
   175   ///
   176   /// \todo refer how to specialize it
   177 
   178   template <typename Graph>
   179   inline int countANodes(const Graph& g) {
   180     return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
   181   }
   182 
   183   namespace _graph_utils_bits {
   184     
   185     template <typename Graph, typename Enable = void>
   186     struct CountBNodesSelector {
   187       static int count(const Graph &g) {
   188         return countItems<Graph, typename Graph::BNode>(g);
   189       }
   190     };
   191 
   192     template <typename Graph>
   193     struct CountBNodesSelector<
   194       Graph, typename 
   195       enable_if<typename Graph::NodeNumTag, void>::type> 
   196     {
   197       static int count(const Graph &g) {
   198         return g.nodeNum();
   199       }
   200     };    
   201   }
   202 
   203   /// \brief Function to count the bnodes in the graph.
   204   ///
   205   /// This function counts the bnodes in the graph.
   206   /// The complexity of the function is O(bn) but for some
   207   /// graph structures it is specialized to run in O(1).
   208   ///
   209   /// \todo refer how to specialize it
   210 
   211   template <typename Graph>
   212   inline int countBNodes(const Graph& g) {
   213     return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
   214   }
   215 
   216 
   217   // Edge counting:
   218 
   219   namespace _graph_utils_bits {
   220     
   221     template <typename Graph, typename Enable = void>
   222     struct CountEdgesSelector {
   223       static int count(const Graph &g) {
   224         return countItems<Graph, typename Graph::Edge>(g);
   225       }
   226     };
   227 
   228     template <typename Graph>
   229     struct CountEdgesSelector<
   230       Graph, 
   231       typename enable_if<typename Graph::EdgeNumTag, void>::type> 
   232     {
   233       static int count(const Graph &g) {
   234         return g.edgeNum();
   235       }
   236     };    
   237   }
   238 
   239   /// \brief Function to count the edges in the graph.
   240   ///
   241   /// This function counts the edges in the graph.
   242   /// The complexity of the function is O(e) but for some
   243   /// graph structures it is specialized to run in O(1).
   244 
   245   template <typename Graph>
   246   inline int countEdges(const Graph& g) {
   247     return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
   248   }
   249 
   250   // Undirected edge counting:
   251   namespace _graph_utils_bits {
   252     
   253     template <typename Graph, typename Enable = void>
   254     struct CountUEdgesSelector {
   255       static int count(const Graph &g) {
   256         return countItems<Graph, typename Graph::UEdge>(g);
   257       }
   258     };
   259 
   260     template <typename Graph>
   261     struct CountUEdgesSelector<
   262       Graph, 
   263       typename enable_if<typename Graph::EdgeNumTag, void>::type> 
   264     {
   265       static int count(const Graph &g) {
   266         return g.uEdgeNum();
   267       }
   268     };    
   269   }
   270 
   271   /// \brief Function to count the undirected edges in the graph.
   272   ///
   273   /// This function counts the undirected edges in the graph.
   274   /// The complexity of the function is O(e) but for some
   275   /// graph structures it is specialized to run in O(1).
   276 
   277   template <typename Graph>
   278   inline int countUEdges(const Graph& g) {
   279     return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
   280 
   281   }
   282 
   283 
   284   template <typename Graph, typename DegIt>
   285   inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
   286     int num = 0;
   287     for (DegIt it(_g, _n); it != INVALID; ++it) {
   288       ++num;
   289     }
   290     return num;
   291   }
   292 
   293   /// \brief Function to count the number of the out-edges from node \c n.
   294   ///
   295   /// This function counts the number of the out-edges from node \c n
   296   /// in the graph.  
   297   template <typename Graph>
   298   inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
   299     return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
   300   }
   301 
   302   /// \brief Function to count the number of the in-edges to node \c n.
   303   ///
   304   /// This function counts the number of the in-edges to node \c n
   305   /// in the graph.  
   306   template <typename Graph>
   307   inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
   308     return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
   309   }
   310 
   311   /// \brief Function to count the number of the inc-edges to node \c n.
   312   ///
   313   /// This function counts the number of the inc-edges to node \c n
   314   /// in the graph.  
   315   template <typename Graph>
   316   inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
   317     return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
   318   }
   319 
   320   namespace _graph_utils_bits {
   321     
   322     template <typename Graph, typename Enable = void>
   323     struct FindEdgeSelector {
   324       typedef typename Graph::Node Node;
   325       typedef typename Graph::Edge Edge;
   326       static Edge find(const Graph &g, Node u, Node v, Edge e) {
   327         if (e == INVALID) {
   328           g.firstOut(e, u);
   329         } else {
   330           g.nextOut(e);
   331         }
   332         while (e != INVALID && g.target(e) != v) {
   333           g.nextOut(e);
   334         }
   335         return e;
   336       }
   337     };
   338 
   339     template <typename Graph>
   340     struct FindEdgeSelector<
   341       Graph, 
   342       typename enable_if<typename Graph::FindEdgeTag, void>::type> 
   343     {
   344       typedef typename Graph::Node Node;
   345       typedef typename Graph::Edge Edge;
   346       static Edge find(const Graph &g, Node u, Node v, Edge prev) {
   347         return g.findEdge(u, v, prev);
   348       }
   349     };    
   350   }
   351 
   352   /// \brief Finds an edge between two nodes of a graph.
   353   ///
   354   /// Finds an edge from node \c u to node \c v in graph \c g.
   355   ///
   356   /// If \c prev is \ref INVALID (this is the default value), then
   357   /// it finds the first edge from \c u to \c v. Otherwise it looks for
   358   /// the next edge from \c u to \c v after \c prev.
   359   /// \return The found edge or \ref INVALID if there is no such an edge.
   360   ///
   361   /// Thus you can iterate through each edge from \c u to \c v as it follows.
   362   ///\code
   363   /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
   364   ///   ...
   365   /// }
   366   ///\endcode
   367   template <typename Graph>
   368   inline typename Graph::Edge findEdge(const Graph &g,
   369 				       typename Graph::Node u, 
   370 				       typename Graph::Node v,
   371 				       typename Graph::Edge prev = INVALID) {
   372     return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
   373   }
   374 
   375   /// \brief Iterator for iterating on edges connected the same nodes.
   376   ///
   377   /// Iterator for iterating on edges connected the same nodes. It is 
   378   /// higher level interface for the findEdge() function. You can
   379   /// use it the following way:
   380   ///\code
   381   /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
   382   ///   ...
   383   /// }
   384   ///\endcode
   385   ///
   386   /// \author Balazs Dezso 
   387   template <typename _Graph>
   388   class ConEdgeIt : public _Graph::Edge {
   389   public:
   390 
   391     typedef _Graph Graph;
   392     typedef typename Graph::Edge Parent;
   393 
   394     typedef typename Graph::Edge Edge;
   395     typedef typename Graph::Node Node;
   396 
   397     /// \brief Constructor.
   398     ///
   399     /// Construct a new ConEdgeIt iterating on the edges which
   400     /// connects the \c u and \c v node.
   401     ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
   402       Parent::operator=(findEdge(graph, u, v));
   403     }
   404 
   405     /// \brief Constructor.
   406     ///
   407     /// Construct a new ConEdgeIt which continues the iterating from 
   408     /// the \c e edge.
   409     ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
   410     
   411     /// \brief Increment operator.
   412     ///
   413     /// It increments the iterator and gives back the next edge.
   414     ConEdgeIt& operator++() {
   415       Parent::operator=(findEdge(graph, graph.source(*this), 
   416 				 graph.target(*this), *this));
   417       return *this;
   418     }
   419   private:
   420     const Graph& graph;
   421   };
   422 
   423   namespace _graph_utils_bits {
   424     
   425     template <typename Graph, typename Enable = void>
   426     struct FindUEdgeSelector {
   427       typedef typename Graph::Node Node;
   428       typedef typename Graph::UEdge UEdge;
   429       static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
   430         bool b;
   431         if (u != v) {
   432           if (e == INVALID) {
   433             g.firstInc(e, u, b);
   434           } else {
   435             b = g.source(e) == u;
   436             g.nextInc(e, b);
   437           }
   438           while (e != INVALID && g.target(e) != v) {
   439             g.nextInc(e, b);
   440           }
   441         } else {
   442           if (e == INVALID) {
   443             g.firstInc(e, u, b);
   444           } else {
   445             b = true;
   446             g.nextInc(e, b);
   447           }
   448           while (e != INVALID && (!b || g.target(e) != v)) {
   449             g.nextInc(e, b);
   450           }
   451         }
   452         return e;
   453       }
   454     };
   455 
   456     template <typename Graph>
   457     struct FindUEdgeSelector<
   458       Graph, 
   459       typename enable_if<typename Graph::FindEdgeTag, void>::type> 
   460     {
   461       typedef typename Graph::Node Node;
   462       typedef typename Graph::UEdge UEdge;
   463       static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
   464         return g.findUEdge(u, v, prev);
   465       }
   466     };    
   467   }
   468 
   469   /// \brief Finds an uedge between two nodes of a graph.
   470   ///
   471   /// Finds an uedge from node \c u to node \c v in graph \c g.
   472   /// If the node \c u and node \c v is equal then each loop edge
   473   /// will be enumerated.
   474   ///
   475   /// If \c prev is \ref INVALID (this is the default value), then
   476   /// it finds the first edge from \c u to \c v. Otherwise it looks for
   477   /// the next edge from \c u to \c v after \c prev.
   478   /// \return The found edge or \ref INVALID if there is no such an edge.
   479   ///
   480   /// Thus you can iterate through each edge from \c u to \c v as it follows.
   481   ///\code
   482   /// for(UEdge e = findUEdge(g,u,v); e != INVALID; 
   483   ///     e = findUEdge(g,u,v,e)) {
   484   ///   ...
   485   /// }
   486   ///\endcode
   487   template <typename Graph>
   488   inline typename Graph::UEdge findEdge(const Graph &g,
   489                                         typename Graph::Node u, 
   490                                         typename Graph::Node v,
   491                                         typename Graph::UEdge prev = INVALID) {
   492     return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, prev);
   493   }
   494 
   495   /// \brief Iterator for iterating on uedges connected the same nodes.
   496   ///
   497   /// Iterator for iterating on uedges connected the same nodes. It is 
   498   /// higher level interface for the findUEdge() function. You can
   499   /// use it the following way:
   500   ///\code
   501   /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
   502   ///   ...
   503   /// }
   504   ///\endcode
   505   ///
   506   /// \author Balazs Dezso 
   507   template <typename _Graph>
   508   class ConUEdgeIt : public _Graph::UEdge {
   509   public:
   510 
   511     typedef _Graph Graph;
   512     typedef typename Graph::UEdge Parent;
   513 
   514     typedef typename Graph::UEdge UEdge;
   515     typedef typename Graph::Node Node;
   516 
   517     /// \brief Constructor.
   518     ///
   519     /// Construct a new ConUEdgeIt iterating on the edges which
   520     /// connects the \c u and \c v node.
   521     ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
   522       Parent::operator=(findUEdge(graph, u, v));
   523     }
   524 
   525     /// \brief Constructor.
   526     ///
   527     /// Construct a new ConUEdgeIt which continues the iterating from 
   528     /// the \c e edge.
   529     ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
   530     
   531     /// \brief Increment operator.
   532     ///
   533     /// It increments the iterator and gives back the next edge.
   534     ConUEdgeIt& operator++() {
   535       Parent::operator=(findUEdge(graph, graph.source(*this), 
   536 				      graph.target(*this), *this));
   537       return *this;
   538     }
   539   private:
   540     const Graph& graph;
   541   };
   542 
   543   /// \brief Copy a map.
   544   ///
   545   /// This function copies the \c source map to the \c target map. It uses the
   546   /// given iterator to iterate on the data structure and it uses the \c ref
   547   /// mapping to convert the source's keys to the target's keys.
   548   template <typename Target, typename Source, 
   549 	    typename ItemIt, typename Ref>	    
   550   void copyMap(Target& target, const Source& source, 
   551 	       ItemIt it, const Ref& ref) {
   552     for (; it != INVALID; ++it) {
   553       target[ref[it]] = source[it];
   554     }
   555   }
   556 
   557   /// \brief Copy the source map to the target map.
   558   ///
   559   /// Copy the \c source map to the \c target map. It uses the given iterator
   560   /// to iterate on the data structure.
   561   template <typename Target, typename Source, typename ItemIt>	    
   562   void copyMap(Target& target, const Source& source, ItemIt it) {
   563     for (; it != INVALID; ++it) {
   564       target[it] = source[it];
   565     }
   566   }
   567 
   568   /// \brief Class to copy a graph.
   569   ///
   570   /// Class to copy a graph to another graph (duplicate a graph). The
   571   /// simplest way of using it is through the \c copyGraph() function.
   572   template <typename Target, typename Source>
   573   class GraphCopy {
   574   public: 
   575     typedef typename Source::Node Node;
   576     typedef typename Source::NodeIt NodeIt;
   577     typedef typename Source::Edge Edge;
   578     typedef typename Source::EdgeIt EdgeIt;
   579 
   580     typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
   581     typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
   582 
   583     /// \brief Constructor for the GraphCopy.
   584     ///
   585     /// It copies the content of the \c _source graph into the
   586     /// \c _target graph. It creates also two references, one beetween
   587     /// the two nodeset and one beetween the two edgesets.
   588     GraphCopy(Target& _target, const Source& _source) 
   589       : source(_source), target(_target), 
   590 	nodeRefMap(_source), edgeRefMap(_source) {
   591       for (NodeIt it(source); it != INVALID; ++it) {
   592 	nodeRefMap[it] = target.addNode();
   593       }
   594       for (EdgeIt it(source); it != INVALID; ++it) {
   595 	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   596 					nodeRefMap[source.target(it)]);
   597       }
   598     }
   599 
   600     /// \brief Copies the node references into the given map.
   601     ///
   602     /// Copies the node references into the given map.
   603     template <typename NodeRef>
   604     const GraphCopy& nodeRef(NodeRef& map) const {
   605       for (NodeIt it(source); it != INVALID; ++it) {
   606 	map.set(it, nodeRefMap[it]);
   607       }
   608       return *this;
   609     }
   610 
   611     /// \brief Reverse and copies the node references into the given map.
   612     ///
   613     /// Reverse and copies the node references into the given map.
   614     template <typename NodeRef>
   615     const GraphCopy& nodeCrossRef(NodeRef& map) const {
   616       for (NodeIt it(source); it != INVALID; ++it) {
   617 	map.set(nodeRefMap[it], it);
   618       }
   619       return *this;
   620     }
   621 
   622     /// \brief Copies the edge references into the given map.
   623     ///
   624     /// Copies the edge references into the given map.
   625     template <typename EdgeRef>
   626     const GraphCopy& edgeRef(EdgeRef& map) const {
   627       for (EdgeIt it(source); it != INVALID; ++it) {
   628 	map.set(it, edgeRefMap[it]);
   629       }
   630       return *this;
   631     }
   632 
   633     /// \brief Reverse and copies the edge references into the given map.
   634     ///
   635     /// Reverse and copies the edge references into the given map.
   636     template <typename EdgeRef>
   637     const GraphCopy& edgeCrossRef(EdgeRef& map) const {
   638       for (EdgeIt it(source); it != INVALID; ++it) {
   639 	map.set(edgeRefMap[it], it);
   640       }
   641       return *this;
   642     }
   643 
   644     /// \brief Make copy of the given map.
   645     ///
   646     /// Makes copy of the given map for the newly created graph. 
   647     /// The new map's key type is the target graph's node type,
   648     /// and the copied map's key type is the source graph's node
   649     /// type.  
   650     template <typename TargetMap, typename SourceMap>
   651     const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
   652       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
   653       return *this;
   654     }
   655 
   656     /// \brief Make copy of the given map.
   657     ///
   658     /// Makes copy of the given map for the newly created graph. 
   659     /// The new map's key type is the target graph's edge type,
   660     /// and the copied map's key type is the source graph's edge
   661     /// type.  
   662     template <typename TargetMap, typename SourceMap>
   663     const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
   664       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
   665       return *this;
   666     }
   667 
   668     /// \brief Gives back the stored node references.
   669     ///
   670     /// Gives back the stored node references.
   671     const NodeRefMap& nodeRef() const {
   672       return nodeRefMap;
   673     }
   674 
   675     /// \brief Gives back the stored edge references.
   676     ///
   677     /// Gives back the stored edge references.
   678     const EdgeRefMap& edgeRef() const {
   679       return edgeRefMap;
   680     }
   681 
   682     void run() const {}
   683 
   684   private:
   685     
   686     const Source& source;
   687     Target& target;
   688 
   689     NodeRefMap nodeRefMap;
   690     EdgeRefMap edgeRefMap;
   691   };
   692 
   693   /// \brief Copy a graph to another graph.
   694   ///
   695   /// Copy a graph to another graph.
   696   /// The usage of the function:
   697   /// 
   698   ///\code
   699   /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
   700   ///\endcode
   701   /// 
   702   /// After the copy the \c nr map will contain the mapping from the
   703   /// source graph's nodes to the target graph's nodes and the \c ecr will
   704   /// contain the mapping from the target graph's edges to the source's
   705   /// edges.
   706   template <typename Target, typename Source>
   707   GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
   708     return GraphCopy<Target, Source>(target, source);
   709   }
   710 
   711   /// \brief Class to copy an undirected graph.
   712   ///
   713   /// Class to copy an undirected graph to another graph (duplicate a graph).
   714   /// The simplest way of using it is through the \c copyUGraph() function.
   715   template <typename Target, typename Source>
   716   class UGraphCopy {
   717   public: 
   718     typedef typename Source::Node Node;
   719     typedef typename Source::NodeIt NodeIt;
   720     typedef typename Source::Edge Edge;
   721     typedef typename Source::EdgeIt EdgeIt;
   722     typedef typename Source::UEdge UEdge;
   723     typedef typename Source::UEdgeIt UEdgeIt;
   724 
   725     typedef typename Source::
   726     template NodeMap<typename Target::Node> NodeRefMap;
   727     
   728     typedef typename Source::
   729     template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
   730 
   731   private:
   732 
   733     struct EdgeRefMap {
   734       EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
   735       typedef typename Source::Edge Key;
   736       typedef typename Target::Edge Value;
   737 
   738       Value operator[](const Key& key) {
   739 	return gc.target.direct(gc.uEdgeRef[key], 
   740 				gc.target.direction(key));
   741       }
   742       
   743       UGraphCopy& gc;
   744     };
   745     
   746   public:
   747 
   748     /// \brief Constructor for the UGraphCopy.
   749     ///
   750     /// It copies the content of the \c _source graph into the
   751     /// \c _target graph. It creates also two references, one beetween
   752     /// the two nodeset and one beetween the two edgesets.
   753     UGraphCopy(Target& _target, const Source& _source) 
   754       : source(_source), target(_target), 
   755 	nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
   756       for (NodeIt it(source); it != INVALID; ++it) {
   757 	nodeRefMap[it] = target.addNode();
   758       }
   759       for (UEdgeIt it(source); it != INVALID; ++it) {
   760 	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   761 					nodeRefMap[source.target(it)]);
   762       }
   763     }
   764 
   765     /// \brief Copies the node references into the given map.
   766     ///
   767     /// Copies the node references into the given map.
   768     template <typename NodeRef>
   769     const UGraphCopy& nodeRef(NodeRef& map) const {
   770       for (NodeIt it(source); it != INVALID; ++it) {
   771 	map.set(it, nodeRefMap[it]);
   772       }
   773       return *this;
   774     }
   775 
   776     /// \brief Reverse and copies the node references into the given map.
   777     ///
   778     /// Reverse and copies the node references into the given map.
   779     template <typename NodeRef>
   780     const UGraphCopy& nodeCrossRef(NodeRef& map) const {
   781       for (NodeIt it(source); it != INVALID; ++it) {
   782 	map.set(nodeRefMap[it], it);
   783       }
   784       return *this;
   785     }
   786 
   787     /// \brief Copies the edge references into the given map.
   788     ///
   789     /// Copies the edge references into the given map.
   790     template <typename EdgeRef>
   791     const UGraphCopy& edgeRef(EdgeRef& map) const {
   792       for (EdgeIt it(source); it != INVALID; ++it) {
   793 	map.set(edgeRefMap[it], it);
   794       }
   795       return *this;
   796     }
   797 
   798     /// \brief Reverse and copies the undirected edge references into the 
   799     /// given map.
   800     ///
   801     /// Reverse and copies the undirected edge references into the given map.
   802     template <typename EdgeRef>
   803     const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
   804       for (EdgeIt it(source); it != INVALID; ++it) {
   805 	map.set(it, edgeRefMap[it]);
   806       }
   807       return *this;
   808     }
   809 
   810     /// \brief Copies the undirected edge references into the given map.
   811     ///
   812     /// Copies the undirected edge references into the given map.
   813     template <typename EdgeRef>
   814     const UGraphCopy& uEdgeRef(EdgeRef& map) const {
   815       for (UEdgeIt it(source); it != INVALID; ++it) {
   816 	map.set(it, uEdgeRefMap[it]);
   817       }
   818       return *this;
   819     }
   820 
   821     /// \brief Reverse and copies the undirected edge references into the 
   822     /// given map.
   823     ///
   824     /// Reverse and copies the undirected edge references into the given map.
   825     template <typename EdgeRef>
   826     const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
   827       for (UEdgeIt it(source); it != INVALID; ++it) {
   828 	map.set(uEdgeRefMap[it], it);
   829       }
   830       return *this;
   831     }
   832 
   833     /// \brief Make copy of the given map.
   834     ///
   835     /// Makes copy of the given map for the newly created graph. 
   836     /// The new map's key type is the target graph's node type,
   837     /// and the copied map's key type is the source graph's node
   838     /// type.  
   839     template <typename TargetMap, typename SourceMap>
   840     const UGraphCopy& nodeMap(TargetMap& tMap, 
   841 				  const SourceMap& sMap) const {
   842       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
   843       return *this;
   844     }
   845 
   846     /// \brief Make copy of the given map.
   847     ///
   848     /// Makes copy of the given map for the newly created graph. 
   849     /// The new map's key type is the target graph's edge type,
   850     /// and the copied map's key type is the source graph's edge
   851     /// type.  
   852     template <typename TargetMap, typename SourceMap>
   853     const UGraphCopy& edgeMap(TargetMap& tMap, 
   854 				  const SourceMap& sMap) const {
   855       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
   856       return *this;
   857     }
   858 
   859     /// \brief Make copy of the given map.
   860     ///
   861     /// Makes copy of the given map for the newly created graph. 
   862     /// The new map's key type is the target graph's edge type,
   863     /// and the copied map's key type is the source graph's edge
   864     /// type.  
   865     template <typename TargetMap, typename SourceMap>
   866     const UGraphCopy& uEdgeMap(TargetMap& tMap, 
   867 				  const SourceMap& sMap) const {
   868       copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
   869       return *this;
   870     }
   871 
   872     /// \brief Gives back the stored node references.
   873     ///
   874     /// Gives back the stored node references.
   875     const NodeRefMap& nodeRef() const {
   876       return nodeRefMap;
   877     }
   878 
   879     /// \brief Gives back the stored edge references.
   880     ///
   881     /// Gives back the stored edge references.
   882     const EdgeRefMap& edgeRef() const {
   883       return edgeRefMap;
   884     }
   885 
   886     /// \brief Gives back the stored uedge references.
   887     ///
   888     /// Gives back the stored uedge references.
   889     const UEdgeRefMap& uEdgeRef() const {
   890       return uEdgeRefMap;
   891     }
   892 
   893     void run() const {}
   894 
   895   private:
   896     
   897     const Source& source;
   898     Target& target;
   899 
   900     NodeRefMap nodeRefMap;
   901     EdgeRefMap edgeRefMap;
   902     UEdgeRefMap uEdgeRefMap;
   903   };
   904 
   905   /// \brief Copy a graph to another graph.
   906   ///
   907   /// Copy a graph to another graph.
   908   /// The usage of the function:
   909   /// 
   910   ///\code
   911   /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
   912   ///\endcode
   913   /// 
   914   /// After the copy the \c nr map will contain the mapping from the
   915   /// source graph's nodes to the target graph's nodes and the \c ecr will
   916   /// contain the mapping from the target graph's edges to the source's
   917   /// edges.
   918   template <typename Target, typename Source>
   919   UGraphCopy<Target, Source> 
   920   copyUGraph(Target& target, const Source& source) {
   921     return UGraphCopy<Target, Source>(target, source);
   922   }
   923 
   924 
   925   /// @}
   926 
   927   /// \addtogroup graph_maps
   928   /// @{
   929 
   930   /// Provides an immutable and unique id for each item in the graph.
   931 
   932   /// The IdMap class provides a unique and immutable id for each item of the
   933   /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
   934   /// different items (nodes) get different ids <li>\b immutable: the id of an
   935   /// item (node) does not change (even if you delete other nodes).  </ul>
   936   /// Through this map you get access (i.e. can read) the inner id values of
   937   /// the items stored in the graph. This map can be inverted with its member
   938   /// class \c InverseMap.
   939   ///
   940   template <typename _Graph, typename _Item>
   941   class IdMap {
   942   public:
   943     typedef _Graph Graph;
   944     typedef int Value;
   945     typedef _Item Item;
   946     typedef _Item Key;
   947 
   948     /// \brief Constructor.
   949     ///
   950     /// Constructor for creating id map.
   951     IdMap(const Graph& _graph) : graph(&_graph) {}
   952 
   953     /// \brief Gives back the \e id of the item.
   954     ///
   955     /// Gives back the immutable and unique \e id of the map.
   956     int operator[](const Item& item) const { return graph->id(item);}
   957 
   958 
   959   private:
   960     const Graph* graph;
   961 
   962   public:
   963 
   964     /// \brief The class represents the inverse of its owner (IdMap).
   965     ///
   966     /// The class represents the inverse of its owner (IdMap).
   967     /// \see inverse()
   968     class InverseMap {
   969     public:
   970 
   971       /// \brief Constructor.
   972       ///
   973       /// Constructor for creating an id-to-item map.
   974       InverseMap(const Graph& _graph) : graph(&_graph) {}
   975 
   976       /// \brief Constructor.
   977       ///
   978       /// Constructor for creating an id-to-item map.
   979       InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
   980 
   981       /// \brief Gives back the given item from its id.
   982       ///
   983       /// Gives back the given item from its id.
   984       /// 
   985       Item operator[](int id) const { return graph->fromId(id, Item());}
   986     private:
   987       const Graph* graph;
   988     };
   989 
   990     /// \brief Gives back the inverse of the map.
   991     ///
   992     /// Gives back the inverse of the IdMap.
   993     InverseMap inverse() const { return InverseMap(*graph);} 
   994 
   995   };
   996 
   997   
   998   /// \brief General invertable graph-map type.
   999 
  1000   /// This type provides simple invertable graph-maps. 
  1001   /// The InvertableMap wraps an arbitrary ReadWriteMap 
  1002   /// and if a key is set to a new value then store it
  1003   /// in the inverse map.
  1004   ///
  1005   /// The values of the map can be accessed
  1006   /// with stl compatible forward iterator.
  1007   ///
  1008   /// \param _Graph The graph type.
  1009   /// \param _Item The item type of the graph.
  1010   /// \param _Value The value type of the map.
  1011   ///
  1012   /// \see IterableValueMap
  1013 #ifndef DOXYGEN
  1014   /// \param _Map A ReadWriteMap mapping from the item type to integer.
  1015   template <
  1016     typename _Graph, typename _Item, typename _Value, 
  1017     typename _Map = DefaultMap<_Graph, _Item, _Value>
  1018   >
  1019 #else
  1020   template <typename _Graph, typename _Item, typename _Value>
  1021 #endif
  1022   class InvertableMap : protected _Map {
  1023   public:
  1024 
  1025     /// The key type of InvertableMap (Node, Edge, UEdge).
  1026     typedef typename _Map::Key Key;
  1027     /// The value type of the InvertableMap.
  1028     typedef typename _Map::Value Value;
  1029 
  1030   private:
  1031     
  1032     typedef _Map Map;
  1033     typedef _Graph Graph;
  1034 
  1035     typedef std::map<Value, Key> Container;
  1036     Container invMap;    
  1037 
  1038   public:
  1039  
  1040 
  1041 
  1042     /// \brief Constructor.
  1043     ///
  1044     /// Construct a new InvertableMap for the graph.
  1045     ///
  1046     InvertableMap(const Graph& graph) : Map(graph) {} 
  1047 
  1048     /// \brief Forward iterator for values.
  1049     ///
  1050     /// This iterator is an stl compatible forward
  1051     /// iterator on the values of the map. The values can
  1052     /// be accessed in the [beginValue, endValue) range.
  1053     ///
  1054     class ValueIterator 
  1055       : public std::iterator<std::forward_iterator_tag, Value> {
  1056       friend class InvertableMap;
  1057     private:
  1058       ValueIterator(typename Container::const_iterator _it) 
  1059         : it(_it) {}
  1060     public:
  1061       
  1062       ValueIterator() {}
  1063 
  1064       ValueIterator& operator++() { ++it; return *this; }
  1065       ValueIterator operator++(int) { 
  1066         ValueIterator tmp(*this); 
  1067         operator++();
  1068         return tmp; 
  1069       }
  1070 
  1071       const Value& operator*() const { return it->first; }
  1072       const Value* operator->() const { return &(it->first); }
  1073 
  1074       bool operator==(ValueIterator jt) const { return it == jt.it; }
  1075       bool operator!=(ValueIterator jt) const { return it != jt.it; }
  1076       
  1077     private:
  1078       typename Container::const_iterator it;
  1079     };
  1080 
  1081     /// \brief Returns an iterator to the first value.
  1082     ///
  1083     /// Returns an stl compatible iterator to the 
  1084     /// first value of the map. The values of the
  1085     /// map can be accessed in the [beginValue, endValue)
  1086     /// range.
  1087     ValueIterator beginValue() const {
  1088       return ValueIterator(invMap.begin());
  1089     }
  1090 
  1091     /// \brief Returns an iterator after the last value.
  1092     ///
  1093     /// Returns an stl compatible iterator after the 
  1094     /// last value of the map. The values of the
  1095     /// map can be accessed in the [beginValue, endValue)
  1096     /// range.
  1097     ValueIterator endValue() const {
  1098       return ValueIterator(invMap.end());
  1099     }
  1100     
  1101     /// \brief The setter function of the map.
  1102     ///
  1103     /// Sets the mapped value.
  1104     void set(const Key& key, const Value& val) {
  1105       Value oldval = Map::operator[](key);
  1106       typename Container::iterator it = invMap.find(oldval);
  1107       if (it != invMap.end() && it->second == key) {
  1108 	invMap.erase(it);
  1109       }      
  1110       invMap.insert(make_pair(val, key));
  1111       Map::set(key, val);
  1112     }
  1113 
  1114     /// \brief The getter function of the map.
  1115     ///
  1116     /// It gives back the value associated with the key.
  1117     typename MapTraits<Map>::ConstReturnValue 
  1118     operator[](const Key& key) const {
  1119       return Map::operator[](key);
  1120     }
  1121 
  1122   protected:
  1123 
  1124     /// \brief Erase the key from the map.
  1125     ///
  1126     /// Erase the key to the map. It is called by the
  1127     /// \c AlterationNotifier.
  1128     virtual void erase(const Key& key) {
  1129       Value val = Map::operator[](key);
  1130       typename Container::iterator it = invMap.find(val);
  1131       if (it != invMap.end() && it->second == key) {
  1132 	invMap.erase(it);
  1133       }
  1134       Map::erase(key);
  1135     }
  1136 
  1137     /// \brief Erase more keys from the map.
  1138     ///
  1139     /// Erase more keys from the map. It is called by the
  1140     /// \c AlterationNotifier.
  1141     virtual void erase(const std::vector<Key>& keys) {
  1142       for (int i = 0; i < (int)keys.size(); ++i) {
  1143 	Value val = Map::operator[](keys[i]);
  1144 	typename Container::iterator it = invMap.find(val);
  1145 	if (it != invMap.end() && it->second == keys[i]) {
  1146 	  invMap.erase(it);
  1147 	}
  1148       }
  1149       Map::erase(keys);
  1150     }
  1151 
  1152     /// \brief Clear the keys from the map and inverse map.
  1153     ///
  1154     /// Clear the keys from the map and inverse map. It is called by the
  1155     /// \c AlterationNotifier.
  1156     virtual void clear() {
  1157       invMap.clear();
  1158       Map::clear();
  1159     }
  1160 
  1161   public:
  1162 
  1163     /// \brief The inverse map type.
  1164     ///
  1165     /// The inverse of this map. The subscript operator of the map
  1166     /// gives back always the item what was last assigned to the value. 
  1167     class InverseMap {
  1168     public:
  1169       /// \brief Constructor of the InverseMap.
  1170       ///
  1171       /// Constructor of the InverseMap.
  1172       InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
  1173 
  1174       /// The value type of the InverseMap.
  1175       typedef typename InvertableMap::Key Value;
  1176       /// The key type of the InverseMap.
  1177       typedef typename InvertableMap::Value Key; 
  1178 
  1179       /// \brief Subscript operator. 
  1180       ///
  1181       /// Subscript operator. It gives back always the item 
  1182       /// what was last assigned to the value.
  1183       Value operator[](const Key& key) const {
  1184 	typename Container::const_iterator it = inverted.invMap.find(key);
  1185 	return it->second;
  1186       }
  1187       
  1188     private:
  1189       const InvertableMap& inverted;
  1190     };
  1191 
  1192     /// \brief It gives back the just readeable inverse map.
  1193     ///
  1194     /// It gives back the just readeable inverse map.
  1195     InverseMap inverse() const {
  1196       return InverseMap(*this);
  1197     } 
  1198 
  1199 
  1200     
  1201   };
  1202 
  1203   /// \brief Provides a mutable, continuous and unique descriptor for each 
  1204   /// item in the graph.
  1205   ///
  1206   /// The DescriptorMap class provides a unique and continuous (but mutable)
  1207   /// descriptor (id) for each item of the same type (e.g. node) in the
  1208   /// graph. This id is <ul><li>\b unique: different items (nodes) get
  1209   /// different ids <li>\b continuous: the range of the ids is the set of
  1210   /// integers between 0 and \c n-1, where \c n is the number of the items of
  1211   /// this type (e.g. nodes) (so the id of a node can change if you delete an
  1212   /// other node, i.e. this id is mutable).  </ul> This map can be inverted
  1213   /// with its member class \c InverseMap.
  1214   ///
  1215   /// \param _Graph The graph class the \c DescriptorMap belongs to.
  1216   /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
  1217   /// UEdge.
  1218 #ifndef DOXYGEN
  1219   /// \param _Map A ReadWriteMap mapping from the item type to integer.
  1220   template <
  1221     typename _Graph, typename _Item,
  1222     typename _Map = DefaultMap<_Graph, _Item, int>
  1223   >
  1224 #else
  1225   template <typename _Graph, typename _Item>
  1226 #endif
  1227   class DescriptorMap : protected _Map {
  1228 
  1229     typedef _Item Item;
  1230     typedef _Map Map;
  1231 
  1232   public:
  1233     /// The graph class of DescriptorMap.
  1234     typedef _Graph Graph;
  1235 
  1236     /// The key type of DescriptorMap (Node, Edge, UEdge).
  1237     typedef typename _Map::Key Key;
  1238     /// The value type of DescriptorMap.
  1239     typedef typename _Map::Value Value;
  1240 
  1241     /// \brief Constructor.
  1242     ///
  1243     /// Constructor for descriptor map.
  1244     DescriptorMap(const Graph& _graph) : Map(_graph) {
  1245       build();
  1246     }
  1247 
  1248   protected:
  1249 
  1250     /// \brief Add a new key to the map.
  1251     ///
  1252     /// Add a new key to the map. It is called by the
  1253     /// \c AlterationNotifier.
  1254     virtual void add(const Item& item) {
  1255       Map::add(item);
  1256       Map::set(item, invMap.size());
  1257       invMap.push_back(item);
  1258     }
  1259 
  1260     /// \brief Add more new keys to the map.
  1261     ///
  1262     /// Add more new keys to the map. It is called by the
  1263     /// \c AlterationNotifier.
  1264     virtual void add(const std::vector<Item>& items) {
  1265       Map::add(items);
  1266       for (int i = 0; i < (int)items.size(); ++i) {
  1267 	Map::set(items[i], invMap.size());
  1268 	invMap.push_back(items[i]);
  1269       }
  1270     }
  1271 
  1272     /// \brief Erase the key from the map.
  1273     ///
  1274     /// Erase the key from the map. It is called by the
  1275     /// \c AlterationNotifier.
  1276     virtual void erase(const Item& item) {
  1277       Map::set(invMap.back(), Map::operator[](item));
  1278       invMap[Map::operator[](item)] = invMap.back();
  1279       invMap.pop_back();
  1280       Map::erase(item);
  1281     }
  1282 
  1283     /// \brief Erase more keys from the map.
  1284     ///
  1285     /// Erase more keys from the map. It is called by the
  1286     /// \c AlterationNotifier.
  1287     virtual void erase(const std::vector<Item>& items) {
  1288       for (int i = 0; i < (int)items.size(); ++i) {
  1289 	Map::set(invMap.back(), Map::operator[](items[i]));
  1290 	invMap[Map::operator[](items[i])] = invMap.back();
  1291 	invMap.pop_back();
  1292       }
  1293       Map::erase(items);
  1294     }
  1295 
  1296     /// \brief Build the unique map.
  1297     ///
  1298     /// Build the unique map. It is called by the
  1299     /// \c AlterationNotifier.
  1300     virtual void build() {
  1301       Map::build();
  1302       Item it;
  1303       const typename Map::Notifier* notifier = Map::getNotifier(); 
  1304       for (notifier->first(it); it != INVALID; notifier->next(it)) {
  1305 	Map::set(it, invMap.size());
  1306 	invMap.push_back(it);	
  1307       }      
  1308     }
  1309     
  1310     /// \brief Clear the keys from the map.
  1311     ///
  1312     /// Clear the keys from the map. It is called by the
  1313     /// \c AlterationNotifier.
  1314     virtual void clear() {
  1315       invMap.clear();
  1316       Map::clear();
  1317     }
  1318 
  1319   public:
  1320 
  1321     /// \brief Returns the maximal value plus one.
  1322     ///
  1323     /// Returns the maximal value plus one in the map.
  1324     unsigned int size() const {
  1325       return invMap.size();
  1326     }
  1327 
  1328     /// \brief Swaps the position of the two items in the map.
  1329     ///
  1330     /// Swaps the position of the two items in the map.
  1331     void swap(const Item& p, const Item& q) {
  1332       int pi = Map::operator[](p);
  1333       int qi = Map::operator[](q);
  1334       Map::set(p, qi);
  1335       invMap[qi] = p;
  1336       Map::set(q, pi);
  1337       invMap[pi] = q;
  1338     }
  1339 
  1340     /// \brief Gives back the \e descriptor of the item.
  1341     ///
  1342     /// Gives back the mutable and unique \e descriptor of the map.
  1343     int operator[](const Item& item) const {
  1344       return Map::operator[](item);
  1345     }
  1346     
  1347   private:
  1348 
  1349     typedef std::vector<Item> Container;
  1350     Container invMap;
  1351 
  1352   public:
  1353     /// \brief The inverse map type of DescriptorMap.
  1354     ///
  1355     /// The inverse map type of DescriptorMap.
  1356     class InverseMap {
  1357     public:
  1358       /// \brief Constructor of the InverseMap.
  1359       ///
  1360       /// Constructor of the InverseMap.
  1361       InverseMap(const DescriptorMap& _inverted) 
  1362 	: inverted(_inverted) {}
  1363 
  1364 
  1365       /// The value type of the InverseMap.
  1366       typedef typename DescriptorMap::Key Value;
  1367       /// The key type of the InverseMap.
  1368       typedef typename DescriptorMap::Value Key; 
  1369 
  1370       /// \brief Subscript operator. 
  1371       ///
  1372       /// Subscript operator. It gives back the item 
  1373       /// that the descriptor belongs to currently.
  1374       Value operator[](const Key& key) const {
  1375 	return inverted.invMap[key];
  1376       }
  1377 
  1378       /// \brief Size of the map.
  1379       ///
  1380       /// Returns the size of the map.
  1381       unsigned int size() const {
  1382 	return inverted.invMap.size();
  1383       }
  1384       
  1385     private:
  1386       const DescriptorMap& inverted;
  1387     };
  1388 
  1389     /// \brief Gives back the inverse of the map.
  1390     ///
  1391     /// Gives back the inverse of the map.
  1392     const InverseMap inverse() const {
  1393       return InverseMap(*this);
  1394     }
  1395   };
  1396 
  1397   /// \brief Returns the source of the given edge.
  1398   ///
  1399   /// The SourceMap gives back the source Node of the given edge. 
  1400   /// \author Balazs Dezso
  1401   template <typename Graph>
  1402   class SourceMap {
  1403   public:
  1404 
  1405     typedef typename Graph::Node Value;
  1406     typedef typename Graph::Edge Key;
  1407 
  1408     /// \brief Constructor
  1409     ///
  1410     /// Constructor
  1411     /// \param _graph The graph that the map belongs to.
  1412     SourceMap(const Graph& _graph) : graph(_graph) {}
  1413 
  1414     /// \brief The subscript operator.
  1415     ///
  1416     /// The subscript operator.
  1417     /// \param edge The edge 
  1418     /// \return The source of the edge 
  1419     Value operator[](const Key& edge) const {
  1420       return graph.source(edge);
  1421     }
  1422 
  1423   private:
  1424     const Graph& graph;
  1425   };
  1426 
  1427   /// \brief Returns a \ref SourceMap class
  1428   ///
  1429   /// This function just returns an \ref SourceMap class.
  1430   /// \relates SourceMap
  1431   template <typename Graph>
  1432   inline SourceMap<Graph> sourceMap(const Graph& graph) {
  1433     return SourceMap<Graph>(graph);
  1434   } 
  1435 
  1436   /// \brief Returns the target of the given edge.
  1437   ///
  1438   /// The TargetMap gives back the target Node of the given edge. 
  1439   /// \author Balazs Dezso
  1440   template <typename Graph>
  1441   class TargetMap {
  1442   public:
  1443 
  1444     typedef typename Graph::Node Value;
  1445     typedef typename Graph::Edge Key;
  1446 
  1447     /// \brief Constructor
  1448     ///
  1449     /// Constructor
  1450     /// \param _graph The graph that the map belongs to.
  1451     TargetMap(const Graph& _graph) : graph(_graph) {}
  1452 
  1453     /// \brief The subscript operator.
  1454     ///
  1455     /// The subscript operator.
  1456     /// \param e The edge 
  1457     /// \return The target of the edge 
  1458     Value operator[](const Key& e) const {
  1459       return graph.target(e);
  1460     }
  1461 
  1462   private:
  1463     const Graph& graph;
  1464   };
  1465 
  1466   /// \brief Returns a \ref TargetMap class
  1467   ///
  1468   /// This function just returns a \ref TargetMap class.
  1469   /// \relates TargetMap
  1470   template <typename Graph>
  1471   inline TargetMap<Graph> targetMap(const Graph& graph) {
  1472     return TargetMap<Graph>(graph);
  1473   }
  1474 
  1475   /// \brief Returns the "forward" directed edge view of an undirected edge.
  1476   ///
  1477   /// Returns the "forward" directed edge view of an undirected edge.
  1478   /// \author Balazs Dezso
  1479   template <typename Graph>
  1480   class ForwardMap {
  1481   public:
  1482 
  1483     typedef typename Graph::Edge Value;
  1484     typedef typename Graph::UEdge Key;
  1485 
  1486     /// \brief Constructor
  1487     ///
  1488     /// Constructor
  1489     /// \param _graph The graph that the map belongs to.
  1490     ForwardMap(const Graph& _graph) : graph(_graph) {}
  1491 
  1492     /// \brief The subscript operator.
  1493     ///
  1494     /// The subscript operator.
  1495     /// \param key An undirected edge 
  1496     /// \return The "forward" directed edge view of undirected edge 
  1497     Value operator[](const Key& key) const {
  1498       return graph.direct(key, true);
  1499     }
  1500 
  1501   private:
  1502     const Graph& graph;
  1503   };
  1504 
  1505   /// \brief Returns a \ref ForwardMap class
  1506   ///
  1507   /// This function just returns an \ref ForwardMap class.
  1508   /// \relates ForwardMap
  1509   template <typename Graph>
  1510   inline ForwardMap<Graph> forwardMap(const Graph& graph) {
  1511     return ForwardMap<Graph>(graph);
  1512   }
  1513 
  1514   /// \brief Returns the "backward" directed edge view of an undirected edge.
  1515   ///
  1516   /// Returns the "backward" directed edge view of an undirected edge.
  1517   /// \author Balazs Dezso
  1518   template <typename Graph>
  1519   class BackwardMap {
  1520   public:
  1521 
  1522     typedef typename Graph::Edge Value;
  1523     typedef typename Graph::UEdge Key;
  1524 
  1525     /// \brief Constructor
  1526     ///
  1527     /// Constructor
  1528     /// \param _graph The graph that the map belongs to.
  1529     BackwardMap(const Graph& _graph) : graph(_graph) {}
  1530 
  1531     /// \brief The subscript operator.
  1532     ///
  1533     /// The subscript operator.
  1534     /// \param key An undirected edge 
  1535     /// \return The "backward" directed edge view of undirected edge 
  1536     Value operator[](const Key& key) const {
  1537       return graph.direct(key, false);
  1538     }
  1539 
  1540   private:
  1541     const Graph& graph;
  1542   };
  1543 
  1544   /// \brief Returns a \ref BackwardMap class
  1545 
  1546   /// This function just returns a \ref BackwardMap class.
  1547   /// \relates BackwardMap
  1548   template <typename Graph>
  1549   inline BackwardMap<Graph> backwardMap(const Graph& graph) {
  1550     return BackwardMap<Graph>(graph);
  1551   }
  1552 
  1553   /// \brief Potential difference map
  1554   ///
  1555   /// If there is an potential map on the nodes then we
  1556   /// can get an edge map as we get the substraction of the
  1557   /// values of the target and source.
  1558   template <typename Graph, typename NodeMap>
  1559   class PotentialDifferenceMap {
  1560   public:
  1561     typedef typename Graph::Edge Key;
  1562     typedef typename NodeMap::Value Value;
  1563 
  1564     /// \brief Constructor
  1565     ///
  1566     /// Contructor of the map
  1567     PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) 
  1568       : graph(_graph), potential(_potential) {}
  1569 
  1570     /// \brief Const subscription operator
  1571     ///
  1572     /// Const subscription operator
  1573     Value operator[](const Key& edge) const {
  1574       return potential[graph.target(edge)] - potential[graph.source(edge)];
  1575     }
  1576 
  1577   private:
  1578     const Graph& graph;
  1579     const NodeMap& potential;
  1580   };
  1581 
  1582   /// \brief Just returns a PotentialDifferenceMap
  1583   ///
  1584   /// Just returns a PotentialDifferenceMap
  1585   /// \relates PotentialDifferenceMap
  1586   template <typename Graph, typename NodeMap>
  1587   PotentialDifferenceMap<Graph, NodeMap> 
  1588   potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
  1589     return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
  1590   }
  1591 
  1592   /// \brief Map of the node in-degrees.
  1593   ///
  1594   /// This map returns the in-degree of a node. Once it is constructed,
  1595   /// the degrees are stored in a standard NodeMap, so each query is done
  1596   /// in constant time. On the other hand, the values are updated automatically
  1597   /// whenever the graph changes.
  1598   ///
  1599   /// \warning Besides addNode() and addEdge(), a graph structure may provide
  1600   /// alternative ways to modify the graph. The correct behavior of InDegMap
  1601   /// is not guarantied if these additional features are used. For example
  1602   /// the functions \ref ListGraph::changeSource() "changeSource()",
  1603   /// \ref ListGraph::changeTarget() "changeTarget()" and
  1604   /// \ref ListGraph::reverseEdge() "reverseEdge()"
  1605   /// of \ref ListGraph will \e not update the degree values correctly.
  1606   ///
  1607   /// \sa OutDegMap
  1608 
  1609   template <typename _Graph>
  1610   class InDegMap  
  1611     : protected ItemSetTraits<_Graph, typename _Graph::Edge>
  1612       ::ItemNotifier::ObserverBase {
  1613 
  1614   public:
  1615     
  1616     typedef _Graph Graph;
  1617     typedef int Value;
  1618     typedef typename Graph::Node Key;
  1619 
  1620     typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
  1621     ::ItemNotifier::ObserverBase Parent;
  1622 
  1623   private:
  1624 
  1625     class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
  1626     public:
  1627 
  1628       typedef DefaultMap<_Graph, Key, int> Parent;
  1629       typedef typename Parent::Graph Graph;
  1630 
  1631       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
  1632       
  1633       virtual void add(const Key& key) {
  1634 	Parent::add(key);
  1635 	Parent::set(key, 0);
  1636       }
  1637 
  1638       virtual void add(const std::vector<Key>& keys) {
  1639 	Parent::add(keys);
  1640 	for (int i = 0; i < (int)keys.size(); ++i) {
  1641 	  Parent::set(keys[i], 0);
  1642 	}
  1643       }
  1644     };
  1645 
  1646   public:
  1647 
  1648     /// \brief Constructor.
  1649     ///
  1650     /// Constructor for creating in-degree map.
  1651     InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1652       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1653       
  1654       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1655 	deg[it] = countInEdges(graph, it);
  1656       }
  1657     }
  1658     
  1659     /// Gives back the in-degree of a Node.
  1660     int operator[](const Key& key) const {
  1661       return deg[key];
  1662     }
  1663 
  1664   protected:
  1665     
  1666     typedef typename Graph::Edge Edge;
  1667 
  1668     virtual void add(const Edge& edge) {
  1669       ++deg[graph.target(edge)];
  1670     }
  1671 
  1672     virtual void add(const std::vector<Edge>& edges) {
  1673       for (int i = 0; i < (int)edges.size(); ++i) {
  1674         ++deg[graph.target(edges[i])];
  1675       }
  1676     }
  1677 
  1678     virtual void erase(const Edge& edge) {
  1679       --deg[graph.target(edge)];
  1680     }
  1681 
  1682     virtual void erase(const std::vector<Edge>& edges) {
  1683       for (int i = 0; i < (int)edges.size(); ++i) {
  1684         --deg[graph.target(edges[i])];
  1685       }
  1686     }
  1687 
  1688     virtual void build() {
  1689       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1690 	deg[it] = countInEdges(graph, it);
  1691       }      
  1692     }
  1693 
  1694     virtual void clear() {
  1695       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1696 	deg[it] = 0;
  1697       }
  1698     }
  1699   private:
  1700     
  1701     const _Graph& graph;
  1702     AutoNodeMap deg;
  1703   };
  1704 
  1705   /// \brief Map of the node out-degrees.
  1706   ///
  1707   /// This map returns the out-degree of a node. Once it is constructed,
  1708   /// the degrees are stored in a standard NodeMap, so each query is done
  1709   /// in constant time. On the other hand, the values are updated automatically
  1710   /// whenever the graph changes.
  1711   ///
  1712   /// \warning Besides addNode() and addEdge(), a graph structure may provide
  1713   /// alternative ways to modify the graph. The correct behavior of OutDegMap
  1714   /// is not guarantied if these additional features are used. For example
  1715   /// the functions \ref ListGraph::changeSource() "changeSource()",
  1716   /// \ref ListGraph::changeTarget() "changeTarget()" and
  1717   /// \ref ListGraph::reverseEdge() "reverseEdge()"
  1718   /// of \ref ListGraph will \e not update the degree values correctly.
  1719   ///
  1720   /// \sa InDegMap
  1721 
  1722   template <typename _Graph>
  1723   class OutDegMap  
  1724     : protected ItemSetTraits<_Graph, typename _Graph::Edge>
  1725       ::ItemNotifier::ObserverBase {
  1726 
  1727   public:
  1728 
  1729     typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
  1730     ::ItemNotifier::ObserverBase Parent;
  1731     
  1732     typedef _Graph Graph;
  1733     typedef int Value;
  1734     typedef typename Graph::Node Key;
  1735 
  1736   private:
  1737 
  1738     class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
  1739     public:
  1740 
  1741       typedef DefaultMap<_Graph, Key, int> Parent;
  1742       typedef typename Parent::Graph Graph;
  1743 
  1744       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
  1745       
  1746       virtual void add(const Key& key) {
  1747 	Parent::add(key);
  1748 	Parent::set(key, 0);
  1749       }
  1750       virtual void add(const std::vector<Key>& keys) {
  1751 	Parent::add(keys);
  1752 	for (int i = 0; i < (int)keys.size(); ++i) {
  1753 	  Parent::set(keys[i], 0);
  1754 	}
  1755       }
  1756     };
  1757 
  1758   public:
  1759 
  1760     /// \brief Constructor.
  1761     ///
  1762     /// Constructor for creating out-degree map.
  1763     OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1764       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1765       
  1766       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1767 	deg[it] = countOutEdges(graph, it);
  1768       }
  1769     }
  1770 
  1771     /// Gives back the out-degree of a Node.
  1772     int operator[](const Key& key) const {
  1773       return deg[key];
  1774     }
  1775 
  1776   protected:
  1777     
  1778     typedef typename Graph::Edge Edge;
  1779 
  1780     virtual void add(const Edge& edge) {
  1781       ++deg[graph.source(edge)];
  1782     }
  1783 
  1784     virtual void add(const std::vector<Edge>& edges) {
  1785       for (int i = 0; i < (int)edges.size(); ++i) {
  1786         ++deg[graph.source(edges[i])];
  1787       }
  1788     }
  1789 
  1790     virtual void erase(const Edge& edge) {
  1791       --deg[graph.source(edge)];
  1792     }
  1793 
  1794     virtual void erase(const std::vector<Edge>& edges) {
  1795       for (int i = 0; i < (int)edges.size(); ++i) {
  1796         --deg[graph.source(edges[i])];
  1797       }
  1798     }
  1799 
  1800     virtual void build() {
  1801       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1802 	deg[it] = countOutEdges(graph, it);
  1803       }      
  1804     }
  1805 
  1806     virtual void clear() {
  1807       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1808 	deg[it] = 0;
  1809       }
  1810     }
  1811   private:
  1812     
  1813     const _Graph& graph;
  1814     AutoNodeMap deg;
  1815   };
  1816 
  1817 
  1818   /// @}
  1819 
  1820 } //END OF NAMESPACE LEMON
  1821 
  1822 #endif