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