lemon/graph_utils.h
changeset 2286 1ef281b2b10e
parent 2235 48801095a410
child 2287 16954ac69517
equal deleted inserted replaced
55:2afbf0cc0b42 56:d4f1db41e646
    81 #define UGRAPH_TYPEDEFS(Graph)				\
    81 #define UGRAPH_TYPEDEFS(Graph)				\
    82   GRAPH_TYPEDEFS(Graph)						\
    82   GRAPH_TYPEDEFS(Graph)						\
    83     typedef Graph:: UEdge   UEdge;			\
    83     typedef Graph:: UEdge   UEdge;			\
    84     typedef Graph:: UEdgeIt UEdgeIt;			\
    84     typedef Graph:: UEdgeIt UEdgeIt;			\
    85     typedef Graph:: IncEdgeIt   IncEdgeIt;		       
    85     typedef Graph:: IncEdgeIt   IncEdgeIt;		       
    86 //     typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;	 
       
    87 //     typedef Graph::template UEdgeMap<int> IntUEdgeMap;
       
    88 //     typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
       
    89 
    86 
    90   ///\brief Creates convenience typedefs for the bipartite undirected graph 
    87   ///\brief Creates convenience typedefs for the bipartite undirected graph 
    91   ///types and iterators
    88   ///types and iterators
    92 
    89 
    93   ///This \c \#define creates the same convenience typedefs as defined by
    90   ///This \c \#define creates the same convenience typedefs as defined by
   101   ///
    98   ///
   102   ///\warning There are no typedefs for the graph maps because of the lack of
    99   ///\warning There are no typedefs for the graph maps because of the lack of
   103   ///template typedefs in C++.
   100   ///template typedefs in C++.
   104 #define BPUGRAPH_TYPEDEFS(Graph)            \
   101 #define BPUGRAPH_TYPEDEFS(Graph)            \
   105   UGRAPH_TYPEDEFS(Graph)                    \
   102   UGRAPH_TYPEDEFS(Graph)                    \
       
   103     typedef Graph::ANode ANode;             \
       
   104     typedef Graph::BNode BNode;             \
   106     typedef Graph::ANodeIt ANodeIt;	    \
   105     typedef Graph::ANodeIt ANodeIt;	    \
   107     typedef Graph::BNodeIt BNodeIt;
   106     typedef Graph::BNodeIt BNodeIt;
   108 
   107 
   109   /// \brief Function to count the items in the graph.
   108   /// \brief Function to count the items in the graph.
   110   ///
   109   ///
   377   ///
   376   ///
   378   ///\sa EdgeLookUp
   377   ///\sa EdgeLookUp
   379   ///\se AllEdgeLookup
   378   ///\se AllEdgeLookup
   380   ///\sa ConEdgeIt
   379   ///\sa ConEdgeIt
   381   template <typename Graph>
   380   template <typename Graph>
   382   inline typename Graph::Edge findEdge(const Graph &g,
   381   inline typename Graph::Edge 
   383 				       typename Graph::Node u, 
   382   findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
   384 				       typename Graph::Node v,
   383            typename Graph::Edge prev = INVALID) {
   385 				       typename Graph::Edge prev = INVALID) {
       
   386     return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
   384     return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
   387   }
   385   }
   388 
   386 
   389   /// \brief Iterator for iterating on edges connected the same nodes.
   387   /// \brief Iterator for iterating on edges connected the same nodes.
   390   ///
   388   ///
   504   ///\endcode
   502   ///\endcode
   505   ///
   503   ///
   506   ///\sa ConEdgeIt
   504   ///\sa ConEdgeIt
   507 
   505 
   508   template <typename Graph>
   506   template <typename Graph>
   509   inline typename Graph::UEdge findUEdge(const Graph &g,
   507   inline typename Graph::UEdge 
   510                                          typename Graph::Node u, 
   508   findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
   511                                          typename Graph::Node v,
   509             typename Graph::UEdge p = INVALID) {
   512                                          typename Graph::UEdge p = INVALID) {
       
   513     return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
   510     return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
   514   }
   511   }
   515 
   512 
   516   /// \brief Iterator for iterating on uedges connected the same nodes.
   513   /// \brief Iterator for iterating on uedges connected the same nodes.
   517   ///
   514   ///
   586     for (; it != INVALID; ++it) {
   583     for (; it != INVALID; ++it) {
   587       target[it] = source[it];
   584       target[it] = source[it];
   588     }
   585     }
   589   }
   586   }
   590 
   587 
       
   588   namespace _graph_utils_bits {
       
   589 
       
   590     template <typename Graph, typename Item, typename RefMap>
       
   591     class MapCopyBase {
       
   592     public:
       
   593       virtual void copy(const Graph& source, const RefMap& refMap) = 0;
       
   594       
       
   595       virtual ~MapCopyBase() {}
       
   596     };
       
   597 
       
   598     template <typename Graph, typename Item, typename RefMap, 
       
   599               typename TargetMap, typename SourceMap>
       
   600     class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
       
   601     public:
       
   602 
       
   603       MapCopy(TargetMap& tmap, const SourceMap& map) 
       
   604         : _tmap(tmap), _map(map) {}
       
   605       
       
   606       virtual void copy(const Graph& graph, const RefMap& refMap) {
       
   607         typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
       
   608         for (ItemIt it(graph); it != INVALID; ++it) {
       
   609           _tmap.set(refMap[it], _map[it]);
       
   610         }
       
   611       }
       
   612 
       
   613     private:
       
   614       TargetMap& _tmap;
       
   615       const SourceMap& _map;
       
   616     };
       
   617 
       
   618     template <typename Graph, typename Item, typename RefMap, typename Ref>
       
   619     class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
       
   620     public:
       
   621 
       
   622       RefCopy(Ref& map) : _map(map) {}
       
   623       
       
   624       virtual void copy(const Graph& graph, const RefMap& refMap) {
       
   625         typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
       
   626         for (ItemIt it(graph); it != INVALID; ++it) {
       
   627           _map.set(it, refMap[it]);
       
   628         }
       
   629       }
       
   630 
       
   631     private:
       
   632       Ref& _map;
       
   633     };
       
   634 
       
   635     template <typename Graph, typename Item, typename RefMap, 
       
   636               typename CrossRef>
       
   637     class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
       
   638     public:
       
   639 
       
   640       CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
       
   641       
       
   642       virtual void copy(const Graph& graph, const RefMap& refMap) {
       
   643         typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
       
   644         for (ItemIt it(graph); it != INVALID; ++it) {
       
   645           _cmap.set(refMap[it], it);
       
   646         }
       
   647       }
       
   648 
       
   649     private:
       
   650       CrossRef& _cmap;
       
   651     };
       
   652 
       
   653   }
       
   654 
   591   /// \brief Class to copy a graph.
   655   /// \brief Class to copy a graph.
   592   ///
   656   ///
   593   /// Class to copy a graph to another graph (duplicate a graph). The
   657   /// Class to copy a graph to another graph (duplicate a graph). The
   594   /// simplest way of using it is through the \c copyGraph() function.
   658   /// simplest way of using it is through the \c copyGraph() function.
   595   template <typename Target, typename Source>
   659   template <typename Target, typename Source>
   596   class GraphCopy {
   660   class GraphCopy {
   597   public: 
   661   private:
       
   662 
   598     typedef typename Source::Node Node;
   663     typedef typename Source::Node Node;
   599     typedef typename Source::NodeIt NodeIt;
   664     typedef typename Source::NodeIt NodeIt;
   600     typedef typename Source::Edge Edge;
   665     typedef typename Source::Edge Edge;
   601     typedef typename Source::EdgeIt EdgeIt;
   666     typedef typename Source::EdgeIt EdgeIt;
   602 
   667 
   603     typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
   668     typedef typename Target::Node TNode;
   604     typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
   669     typedef typename Target::Edge TEdge;
       
   670 
       
   671     typedef typename Source::template NodeMap<TNode> NodeRefMap;
       
   672     typedef typename Source::template EdgeMap<TEdge> EdgeRefMap;
       
   673     
       
   674     
       
   675   public: 
       
   676 
   605 
   677 
   606     /// \brief Constructor for the GraphCopy.
   678     /// \brief Constructor for the GraphCopy.
   607     ///
   679     ///
   608     /// It copies the content of the \c _source graph into the
   680     /// It copies the content of the \c _source graph into the
   609     /// \c _target graph. It creates also two references, one beetween
   681     /// \c _target graph.
   610     /// the two nodeset and one beetween the two edgesets.
       
   611     GraphCopy(Target& _target, const Source& _source) 
   682     GraphCopy(Target& _target, const Source& _source) 
   612       : source(_source), target(_target), 
   683       : source(_source), target(_target) {}
   613 	nodeRefMap(_source), edgeRefMap(_source) {
   684 
   614       for (NodeIt it(source); it != INVALID; ++it) {
   685     /// \brief Destructor of the GraphCopy
   615 	nodeRefMap[it] = target.addNode();
   686     ///
   616       }
   687     /// Destructor of the GraphCopy
   617       for (EdgeIt it(source); it != INVALID; ++it) {
   688     ~GraphCopy() {
   618 	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   689       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   619 					nodeRefMap[source.target(it)]);
   690         delete nodeMapCopies[i];
   620       }
   691       }
       
   692       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
       
   693         delete edgeMapCopies[i];
       
   694       }
       
   695 
   621     }
   696     }
   622 
   697 
   623     /// \brief Copies the node references into the given map.
   698     /// \brief Copies the node references into the given map.
   624     ///
   699     ///
   625     /// Copies the node references into the given map.
   700     /// Copies the node references into the given map.
   626     template <typename NodeRef>
   701     template <typename NodeRef>
   627     const GraphCopy& nodeRef(NodeRef& map) const {
   702     GraphCopy& nodeRef(NodeRef& map) {
   628       for (NodeIt it(source); it != INVALID; ++it) {
   703       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
   629 	map.set(it, nodeRefMap[it]);
   704                               NodeRefMap, NodeRef>(map));
   630       }
       
   631       return *this;
   705       return *this;
   632     }
   706     }
   633 
   707 
   634     /// \brief Reverse and copies the node references into the given map.
   708     /// \brief Reverse and copies the node references into the given map.
   635     ///
   709     ///
   636     /// Reverse and copies the node references into the given map.
   710     /// Reverse and copies the node references into the given map.
   637     template <typename NodeRef>
   711     template <typename NodeCrossRef>
   638     const GraphCopy& nodeCrossRef(NodeRef& map) const {
   712     GraphCopy& nodeCrossRef(NodeCrossRef& map) {
   639       for (NodeIt it(source); it != INVALID; ++it) {
   713       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
   640 	map.set(nodeRefMap[it], it);
   714                               NodeRefMap, NodeCrossRef>(map));
   641       }
       
   642       return *this;
       
   643     }
       
   644 
       
   645     /// \brief Copies the edge references into the given map.
       
   646     ///
       
   647     /// Copies the edge references into the given map.
       
   648     template <typename EdgeRef>
       
   649     const GraphCopy& edgeRef(EdgeRef& map) const {
       
   650       for (EdgeIt it(source); it != INVALID; ++it) {
       
   651 	map.set(it, edgeRefMap[it]);
       
   652       }
       
   653       return *this;
       
   654     }
       
   655 
       
   656     /// \brief Reverse and copies the edge references into the given map.
       
   657     ///
       
   658     /// Reverse and copies the edge references into the given map.
       
   659     template <typename EdgeRef>
       
   660     const GraphCopy& edgeCrossRef(EdgeRef& map) const {
       
   661       for (EdgeIt it(source); it != INVALID; ++it) {
       
   662 	map.set(edgeRefMap[it], it);
       
   663       }
       
   664       return *this;
   715       return *this;
   665     }
   716     }
   666 
   717 
   667     /// \brief Make copy of the given map.
   718     /// \brief Make copy of the given map.
   668     ///
   719     ///
   669     /// Makes copy of the given map for the newly created graph. 
   720     /// Makes copy of the given map for the newly created graph. 
   670     /// The new map's key type is the target graph's node type,
   721     /// The new map's key type is the target graph's node type,
   671     /// and the copied map's key type is the source graph's node
   722     /// and the copied map's key type is the source graph's node
   672     /// type.  
   723     /// type.  
   673     template <typename TargetMap, typename SourceMap>
   724     template <typename TargetMap, typename SourceMap>
   674     const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
   725     GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
   675       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
   726       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
       
   727                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
       
   728       return *this;
       
   729     }
       
   730 
       
   731     /// \brief Copies the edge references into the given map.
       
   732     ///
       
   733     /// Copies the edge references into the given map.
       
   734     template <typename EdgeRef>
       
   735     GraphCopy& edgeRef(EdgeRef& map) {
       
   736       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
       
   737                               EdgeRefMap, EdgeRef>(map));
       
   738       return *this;
       
   739     }
       
   740 
       
   741     /// \brief Reverse and copies the edge references into the given map.
       
   742     ///
       
   743     /// Reverse and copies the edge references into the given map.
       
   744     template <typename EdgeCrossRef>
       
   745     GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
       
   746       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
       
   747                               EdgeRefMap, EdgeCrossRef>(map));
   676       return *this;
   748       return *this;
   677     }
   749     }
   678 
   750 
   679     /// \brief Make copy of the given map.
   751     /// \brief Make copy of the given map.
   680     ///
   752     ///
   681     /// Makes copy of the given map for the newly created graph. 
   753     /// Makes copy of the given map for the newly created graph. 
   682     /// The new map's key type is the target graph's edge type,
   754     /// The new map's key type is the target graph's edge type,
   683     /// and the copied map's key type is the source graph's edge
   755     /// and the copied map's key type is the source graph's edge
   684     /// type.  
   756     /// type.  
   685     template <typename TargetMap, typename SourceMap>
   757     template <typename TargetMap, typename SourceMap>
   686     const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
   758     GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
   687       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
   759       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
       
   760                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
   688       return *this;
   761       return *this;
   689     }
   762     }
   690 
   763 
   691     /// \brief Gives back the stored node references.
   764     /// \brief Executes the copies.
   692     ///
   765     ///
   693     /// Gives back the stored node references.
   766     /// Executes the copies.
   694     const NodeRefMap& nodeRef() const {
   767     void run() {
   695       return nodeRefMap;
   768       NodeRefMap nodeRefMap(source);
   696     }
   769       for (NodeIt it(source); it != INVALID; ++it) {
   697 
   770 	nodeRefMap[it] = target.addNode();
   698     /// \brief Gives back the stored edge references.
   771       }
   699     ///
   772       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   700     /// Gives back the stored edge references.
   773         nodeMapCopies[i]->copy(source, nodeRefMap);
   701     const EdgeRefMap& edgeRef() const {
   774       }
   702       return edgeRefMap;
   775       EdgeRefMap edgeRefMap(source);
   703     }
   776       for (EdgeIt it(source); it != INVALID; ++it) {
   704 
   777 	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   705     void run() const {}
   778 					nodeRefMap[source.target(it)]);
       
   779       }
       
   780       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
       
   781         edgeMapCopies[i]->copy(source, edgeRefMap);
       
   782       }
       
   783     }
   706 
   784 
   707   private:
   785   private:
   708     
   786     
   709     const Source& source;
   787     const Source& source;
   710     Target& target;
   788     Target& target;
   711 
   789 
   712     NodeRefMap nodeRefMap;
   790     std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
   713     EdgeRefMap edgeRefMap;
   791     nodeMapCopies;
       
   792 
       
   793     std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
       
   794     edgeMapCopies;
       
   795 
   714   };
   796   };
   715 
   797 
   716   /// \brief Copy a graph to another graph.
   798   /// \brief Copy a graph to another graph.
   717   ///
   799   ///
   718   /// Copy a graph to another graph.
   800   /// Copy a graph to another graph.
   719   /// The usage of the function:
   801   /// The usage of the function:
   720   /// 
   802   /// 
   721   ///\code
   803   ///\code
   722   /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
   804   /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
   723   ///\endcode
   805   ///\endcode
   724   /// 
   806   /// 
   725   /// After the copy the \c nr map will contain the mapping from the
   807   /// After the copy the \c nr map will contain the mapping from the
   726   /// source graph's nodes to the target graph's nodes and the \c ecr will
   808   /// source graph's nodes to the target graph's nodes and the \c ecr will
   727   /// contain the mapping from the target graph's edges to the source's
   809   /// contain the mapping from the target graph's edges to the source's
   735   ///
   817   ///
   736   /// Class to copy an undirected graph to another graph (duplicate a graph).
   818   /// Class to copy an undirected graph to another graph (duplicate a graph).
   737   /// The simplest way of using it is through the \c copyUGraph() function.
   819   /// The simplest way of using it is through the \c copyUGraph() function.
   738   template <typename Target, typename Source>
   820   template <typename Target, typename Source>
   739   class UGraphCopy {
   821   class UGraphCopy {
   740   public: 
   822   private:
       
   823 
   741     typedef typename Source::Node Node;
   824     typedef typename Source::Node Node;
   742     typedef typename Source::NodeIt NodeIt;
   825     typedef typename Source::NodeIt NodeIt;
   743     typedef typename Source::Edge Edge;
   826     typedef typename Source::Edge Edge;
   744     typedef typename Source::EdgeIt EdgeIt;
   827     typedef typename Source::EdgeIt EdgeIt;
   745     typedef typename Source::UEdge UEdge;
   828     typedef typename Source::UEdge UEdge;
   746     typedef typename Source::UEdgeIt UEdgeIt;
   829     typedef typename Source::UEdgeIt UEdgeIt;
   747 
   830 
   748     typedef typename Source::
   831     typedef typename Target::Node TNode;
   749     template NodeMap<typename Target::Node> NodeRefMap;
   832     typedef typename Target::Edge TEdge;
   750     
   833     typedef typename Target::UEdge TUEdge;
   751     typedef typename Source::
   834 
   752     template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
   835     typedef typename Source::template NodeMap<TNode> NodeRefMap;
   753 
   836     typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
   754   private:
       
   755 
   837 
   756     struct EdgeRefMap {
   838     struct EdgeRefMap {
   757       EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
   839       EdgeRefMap(const Target& _target, const Source& _source,
       
   840                  const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
       
   841         : target(_target), source(_source), 
       
   842           uedge_ref(_uedge_ref), node_ref(_node_ref) {}
       
   843 
   758       typedef typename Source::Edge Key;
   844       typedef typename Source::Edge Key;
   759       typedef typename Target::Edge Value;
   845       typedef typename Target::Edge Value;
   760 
   846 
   761       Value operator[](const Key& key) {
   847       Value operator[](const Key& key) const {
   762 	return gc.target.direct(gc.uEdgeRef[key], 
   848         bool forward = (source.direction(key) == 
   763 				gc.target.direction(key));
   849                         (node_ref[source.source((UEdge)key)] == 
       
   850                          target.source(uedge_ref[(UEdge)key])));
       
   851 	return target.direct(uedge_ref[key], forward); 
   764       }
   852       }
   765       
   853       
   766       UGraphCopy& gc;
   854       const Target& target;
       
   855       const Source& source;
       
   856       const UEdgeRefMap& uedge_ref;
       
   857       const NodeRefMap& node_ref;
   767     };
   858     };
   768     
   859 
   769   public:
   860     
   770 
   861   public: 
   771     /// \brief Constructor for the UGraphCopy.
   862 
       
   863 
       
   864     /// \brief Constructor for the GraphCopy.
   772     ///
   865     ///
   773     /// It copies the content of the \c _source graph into the
   866     /// It copies the content of the \c _source graph into the
   774     /// \c _target graph. It creates also two references, one beetween
   867     /// \c _target graph.
   775     /// the two nodeset and one beetween the two edgesets.
       
   776     UGraphCopy(Target& _target, const Source& _source) 
   868     UGraphCopy(Target& _target, const Source& _source) 
   777       : source(_source), target(_target), 
   869       : source(_source), target(_target) {}
   778 	nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
   870 
   779       for (NodeIt it(source); it != INVALID; ++it) {
   871     /// \brief Destructor of the GraphCopy
   780 	nodeRefMap[it] = target.addNode();
   872     ///
   781       }
   873     /// Destructor of the GraphCopy
   782       for (UEdgeIt it(source); it != INVALID; ++it) {
   874     ~UGraphCopy() {
   783 	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   875       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   784 					nodeRefMap[source.target(it)]);
   876         delete nodeMapCopies[i];
   785       }
   877       }
       
   878       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
       
   879         delete edgeMapCopies[i];
       
   880       }
       
   881       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
       
   882         delete uEdgeMapCopies[i];
       
   883       }
       
   884 
   786     }
   885     }
   787 
   886 
   788     /// \brief Copies the node references into the given map.
   887     /// \brief Copies the node references into the given map.
   789     ///
   888     ///
   790     /// Copies the node references into the given map.
   889     /// Copies the node references into the given map.
   791     template <typename NodeRef>
   890     template <typename NodeRef>
   792     const UGraphCopy& nodeRef(NodeRef& map) const {
   891     UGraphCopy& nodeRef(NodeRef& map) {
   793       for (NodeIt it(source); it != INVALID; ++it) {
   892       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
   794 	map.set(it, nodeRefMap[it]);
   893                               NodeRefMap, NodeRef>(map));
   795       }
       
   796       return *this;
   894       return *this;
   797     }
   895     }
   798 
   896 
   799     /// \brief Reverse and copies the node references into the given map.
   897     /// \brief Reverse and copies the node references into the given map.
   800     ///
   898     ///
   801     /// Reverse and copies the node references into the given map.
   899     /// Reverse and copies the node references into the given map.
   802     template <typename NodeRef>
   900     template <typename NodeCrossRef>
   803     const UGraphCopy& nodeCrossRef(NodeRef& map) const {
   901     UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
   804       for (NodeIt it(source); it != INVALID; ++it) {
   902       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
   805 	map.set(nodeRefMap[it], it);
   903                               NodeRefMap, NodeCrossRef>(map));
   806       }
       
   807       return *this;
       
   808     }
       
   809 
       
   810     /// \brief Copies the edge references into the given map.
       
   811     ///
       
   812     /// Copies the edge references into the given map.
       
   813     template <typename EdgeRef>
       
   814     const UGraphCopy& edgeRef(EdgeRef& map) const {
       
   815       for (EdgeIt it(source); it != INVALID; ++it) {
       
   816 	map.set(edgeRefMap[it], 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& edgeCrossRef(EdgeRef& map) const {
       
   827       for (EdgeIt it(source); it != INVALID; ++it) {
       
   828 	map.set(it, edgeRefMap[it]);
       
   829       }
       
   830       return *this;
       
   831     }
       
   832 
       
   833     /// \brief Copies the undirected edge references into the given map.
       
   834     ///
       
   835     /// Copies the undirected edge references into the given map.
       
   836     template <typename EdgeRef>
       
   837     const UGraphCopy& uEdgeRef(EdgeRef& map) const {
       
   838       for (UEdgeIt it(source); it != INVALID; ++it) {
       
   839 	map.set(it, uEdgeRefMap[it]);
       
   840       }
       
   841       return *this;
       
   842     }
       
   843 
       
   844     /// \brief Reverse and copies the undirected edge references into the 
       
   845     /// given map.
       
   846     ///
       
   847     /// Reverse and copies the undirected edge references into the given map.
       
   848     template <typename EdgeRef>
       
   849     const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
       
   850       for (UEdgeIt it(source); it != INVALID; ++it) {
       
   851 	map.set(uEdgeRefMap[it], it);
       
   852       }
       
   853       return *this;
   904       return *this;
   854     }
   905     }
   855 
   906 
   856     /// \brief Make copy of the given map.
   907     /// \brief Make copy of the given map.
   857     ///
   908     ///
   858     /// Makes copy of the given map for the newly created graph. 
   909     /// Makes copy of the given map for the newly created graph. 
   859     /// The new map's key type is the target graph's node type,
   910     /// The new map's key type is the target graph's node type,
   860     /// and the copied map's key type is the source graph's node
   911     /// and the copied map's key type is the source graph's node
   861     /// type.  
   912     /// type.  
   862     template <typename TargetMap, typename SourceMap>
   913     template <typename TargetMap, typename SourceMap>
   863     const UGraphCopy& nodeMap(TargetMap& tMap, 
   914     UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
   864 				  const SourceMap& sMap) const {
   915       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
   865       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
   916                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
       
   917       return *this;
       
   918     }
       
   919 
       
   920     /// \brief Copies the edge references into the given map.
       
   921     ///
       
   922     /// Copies the edge references into the given map.
       
   923     template <typename EdgeRef>
       
   924     UGraphCopy& edgeRef(EdgeRef& map) {
       
   925       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
       
   926                               EdgeRefMap, EdgeRef>(map));
       
   927       return *this;
       
   928     }
       
   929 
       
   930     /// \brief Reverse and copies the edge references into the given map.
       
   931     ///
       
   932     /// Reverse and copies the edge references into the given map.
       
   933     template <typename EdgeCrossRef>
       
   934     UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
       
   935       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
       
   936                               EdgeRefMap, EdgeCrossRef>(map));
   866       return *this;
   937       return *this;
   867     }
   938     }
   868 
   939 
   869     /// \brief Make copy of the given map.
   940     /// \brief Make copy of the given map.
   870     ///
   941     ///
   871     /// Makes copy of the given map for the newly created graph. 
   942     /// Makes copy of the given map for the newly created graph. 
   872     /// The new map's key type is the target graph's edge type,
   943     /// The new map's key type is the target graph's edge type,
   873     /// and the copied map's key type is the source graph's edge
   944     /// and the copied map's key type is the source graph's edge
   874     /// type.  
   945     /// type.  
   875     template <typename TargetMap, typename SourceMap>
   946     template <typename TargetMap, typename SourceMap>
   876     const UGraphCopy& edgeMap(TargetMap& tMap, 
   947     UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
   877 				  const SourceMap& sMap) const {
   948       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
   878       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
   949                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
   879       return *this;
   950       return *this;
   880     }
   951     }
   881 
   952 
       
   953     /// \brief Copies the uEdge references into the given map.
       
   954     ///
       
   955     /// Copies the uEdge references into the given map.
       
   956     template <typename UEdgeRef>
       
   957     UGraphCopy& uEdgeRef(UEdgeRef& map) {
       
   958       uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
       
   959                                UEdgeRefMap, UEdgeRef>(map));
       
   960       return *this;
       
   961     }
       
   962 
       
   963     /// \brief Reverse and copies the uEdge references into the given map.
       
   964     ///
       
   965     /// Reverse and copies the uEdge references into the given map.
       
   966     template <typename UEdgeCrossRef>
       
   967     UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
       
   968       uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
       
   969                                UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
       
   970       return *this;
       
   971     }
       
   972 
   882     /// \brief Make copy of the given map.
   973     /// \brief Make copy of the given map.
   883     ///
   974     ///
   884     /// Makes copy of the given map for the newly created graph. 
   975     /// Makes copy of the given map for the newly created graph. 
   885     /// The new map's key type is the target graph's edge type,
   976     /// The new map's key type is the target graph's uEdge type,
   886     /// and the copied map's key type is the source graph's edge
   977     /// and the copied map's key type is the source graph's uEdge
   887     /// type.  
   978     /// type.  
   888     template <typename TargetMap, typename SourceMap>
   979     template <typename TargetMap, typename SourceMap>
   889     const UGraphCopy& uEdgeMap(TargetMap& tMap, 
   980     UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
   890 				  const SourceMap& sMap) const {
   981       uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
   891       copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
   982                                UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
   892       return *this;
   983       return *this;
   893     }
   984     }
   894 
   985 
   895     /// \brief Gives back the stored node references.
   986     /// \brief Executes the copies.
   896     ///
   987     ///
   897     /// Gives back the stored node references.
   988     /// Executes the copies.
   898     const NodeRefMap& nodeRef() const {
   989     void run() {
   899       return nodeRefMap;
   990       NodeRefMap nodeRefMap(source);
   900     }
   991       for (NodeIt it(source); it != INVALID; ++it) {
   901 
   992 	nodeRefMap[it] = target.addNode();
   902     /// \brief Gives back the stored edge references.
   993       }
   903     ///
   994       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   904     /// Gives back the stored edge references.
   995         nodeMapCopies[i]->copy(source, nodeRefMap);
   905     const EdgeRefMap& edgeRef() const {
   996       }
   906       return edgeRefMap;
   997       UEdgeRefMap uEdgeRefMap(source);
   907     }
   998       EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
   908 
   999       for (UEdgeIt it(source); it != INVALID; ++it) {
   909     /// \brief Gives back the stored uedge references.
  1000 	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
   910     ///
  1001                                          nodeRefMap[source.target(it)]);
   911     /// Gives back the stored uedge references.
  1002       }
   912     const UEdgeRefMap& uEdgeRef() const {
  1003       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
   913       return uEdgeRefMap;
  1004         uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
   914     }
  1005       }
   915 
  1006       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
   916     void run() const {}
  1007         edgeMapCopies[i]->copy(source, edgeRefMap);
       
  1008       }
       
  1009     }
   917 
  1010 
   918   private:
  1011   private:
   919     
  1012     
   920     const Source& source;
  1013     const Source& source;
   921     Target& target;
  1014     Target& target;
   922 
  1015 
   923     NodeRefMap nodeRefMap;
  1016     std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
   924     EdgeRefMap edgeRefMap;
  1017     nodeMapCopies;
   925     UEdgeRefMap uEdgeRefMap;
  1018 
       
  1019     std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
       
  1020     edgeMapCopies;
       
  1021 
       
  1022     std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
       
  1023     uEdgeMapCopies;
       
  1024 
   926   };
  1025   };
   927 
  1026 
   928   /// \brief Copy a graph to another graph.
  1027   /// \brief Copy a graph to another graph.
   929   ///
  1028   ///
   930   /// Copy a graph to another graph.
  1029   /// Copy a graph to another graph.
   931   /// The usage of the function:
  1030   /// The usage of the function:
   932   /// 
  1031   /// 
   933   ///\code
  1032   ///\code
   934   /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
  1033   /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
   935   ///\endcode
  1034   ///\endcode
   936   /// 
  1035   /// 
   937   /// After the copy the \c nr map will contain the mapping from the
  1036   /// After the copy the \c nr map will contain the mapping from the
   938   /// source graph's nodes to the target graph's nodes and the \c ecr will
  1037   /// source graph's nodes to the target graph's nodes and the \c ecr will
   939   /// contain the mapping from the target graph's edges to the source's
  1038   /// contain the mapping from the target graph's edges to the source's
   969     typedef _Item Key;
  1068     typedef _Item Key;
   970 
  1069 
   971     /// \brief Constructor.
  1070     /// \brief Constructor.
   972     ///
  1071     ///
   973     /// Constructor for creating id map.
  1072     /// Constructor for creating id map.
   974     IdMap(const Graph& _graph) : graph(&_graph) {}
  1073     explicit IdMap(const Graph& _graph) : graph(&_graph) {}
   975 
  1074 
   976     /// \brief Gives back the \e id of the item.
  1075     /// \brief Gives back the \e id of the item.
   977     ///
  1076     ///
   978     /// Gives back the immutable and unique \e id of the map.
  1077     /// Gives back the immutable and unique \e id of the map.
   979     int operator[](const Item& item) const { return graph->id(item);}
  1078     int operator[](const Item& item) const { return graph->id(item);}
   992     public:
  1091     public:
   993 
  1092 
   994       /// \brief Constructor.
  1093       /// \brief Constructor.
   995       ///
  1094       ///
   996       /// Constructor for creating an id-to-item map.
  1095       /// Constructor for creating an id-to-item map.
   997       InverseMap(const Graph& _graph) : graph(&_graph) {}
  1096       explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
   998 
  1097 
   999       /// \brief Constructor.
  1098       /// \brief Constructor.
  1000       ///
  1099       ///
  1001       /// Constructor for creating an id-to-item map.
  1100       /// Constructor for creating an id-to-item map.
  1002       InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
  1101       explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
  1003 
  1102 
  1004       /// \brief Gives back the given item from its id.
  1103       /// \brief Gives back the given item from its id.
  1005       ///
  1104       ///
  1006       /// Gives back the given item from its id.
  1105       /// Gives back the given item from its id.
  1007       /// 
  1106       /// 
  1064 
  1163 
  1065     /// \brief Constructor.
  1164     /// \brief Constructor.
  1066     ///
  1165     ///
  1067     /// Construct a new InvertableMap for the graph.
  1166     /// Construct a new InvertableMap for the graph.
  1068     ///
  1167     ///
  1069     InvertableMap(const Graph& graph) : Map(graph) {} 
  1168     explicit InvertableMap(const Graph& graph) : Map(graph) {} 
  1070 
  1169 
  1071     /// \brief Forward iterator for values.
  1170     /// \brief Forward iterator for values.
  1072     ///
  1171     ///
  1073     /// This iterator is an stl compatible forward
  1172     /// This iterator is an stl compatible forward
  1074     /// iterator on the values of the map. The values can
  1173     /// iterator on the values of the map. The values can
  1190     class InverseMap {
  1289     class InverseMap {
  1191     public:
  1290     public:
  1192       /// \brief Constructor of the InverseMap.
  1291       /// \brief Constructor of the InverseMap.
  1193       ///
  1292       ///
  1194       /// Constructor of the InverseMap.
  1293       /// Constructor of the InverseMap.
  1195       InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
  1294       explicit InverseMap(const InvertableMap& _inverted) 
       
  1295         : inverted(_inverted) {}
  1196 
  1296 
  1197       /// The value type of the InverseMap.
  1297       /// The value type of the InverseMap.
  1198       typedef typename InvertableMap::Key Value;
  1298       typedef typename InvertableMap::Key Value;
  1199       /// The key type of the InverseMap.
  1299       /// The key type of the InverseMap.
  1200       typedef typename InvertableMap::Value Key; 
  1300       typedef typename InvertableMap::Value Key; 
  1262     typedef typename _Map::Value Value;
  1362     typedef typename _Map::Value Value;
  1263 
  1363 
  1264     /// \brief Constructor.
  1364     /// \brief Constructor.
  1265     ///
  1365     ///
  1266     /// Constructor for descriptor map.
  1366     /// Constructor for descriptor map.
  1267     DescriptorMap(const Graph& _graph) : Map(_graph) {
  1367     explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
  1268       Item it;
  1368       Item it;
  1269       const typename Map::Notifier* notifier = Map::getNotifier(); 
  1369       const typename Map::Notifier* notifier = Map::getNotifier(); 
  1270       for (notifier->first(it); it != INVALID; notifier->next(it)) {
  1370       for (notifier->first(it); it != INVALID; notifier->next(it)) {
  1271 	Map::set(it, invMap.size());
  1371 	Map::set(it, invMap.size());
  1272 	invMap.push_back(it);	
  1372 	invMap.push_back(it);	
  1273       }      
  1373       }      
  1274     }
  1374     }
       
  1375 
  1275 
  1376 
  1276   protected:
  1377   protected:
  1277 
  1378 
  1278     /// \brief Add a new key to the map.
  1379     /// \brief Add a new key to the map.
  1279     ///
  1380     ///
  1384     class InverseMap {
  1485     class InverseMap {
  1385     public:
  1486     public:
  1386       /// \brief Constructor of the InverseMap.
  1487       /// \brief Constructor of the InverseMap.
  1387       ///
  1488       ///
  1388       /// Constructor of the InverseMap.
  1489       /// Constructor of the InverseMap.
  1389       InverseMap(const DescriptorMap& _inverted) 
  1490       explicit InverseMap(const DescriptorMap& _inverted) 
  1390 	: inverted(_inverted) {}
  1491 	: inverted(_inverted) {}
  1391 
  1492 
  1392 
  1493 
  1393       /// The value type of the InverseMap.
  1494       /// The value type of the InverseMap.
  1394       typedef typename DescriptorMap::Key Value;
  1495       typedef typename DescriptorMap::Key Value;
  1435 
  1536 
  1436     /// \brief Constructor
  1537     /// \brief Constructor
  1437     ///
  1538     ///
  1438     /// Constructor
  1539     /// Constructor
  1439     /// \param _graph The graph that the map belongs to.
  1540     /// \param _graph The graph that the map belongs to.
  1440     SourceMap(const Graph& _graph) : graph(_graph) {}
  1541     explicit SourceMap(const Graph& _graph) : graph(_graph) {}
  1441 
  1542 
  1442     /// \brief The subscript operator.
  1543     /// \brief The subscript operator.
  1443     ///
  1544     ///
  1444     /// The subscript operator.
  1545     /// The subscript operator.
  1445     /// \param edge The edge 
  1546     /// \param edge The edge 
  1474 
  1575 
  1475     /// \brief Constructor
  1576     /// \brief Constructor
  1476     ///
  1577     ///
  1477     /// Constructor
  1578     /// Constructor
  1478     /// \param _graph The graph that the map belongs to.
  1579     /// \param _graph The graph that the map belongs to.
  1479     TargetMap(const Graph& _graph) : graph(_graph) {}
  1580     explicit TargetMap(const Graph& _graph) : graph(_graph) {}
  1480 
  1581 
  1481     /// \brief The subscript operator.
  1582     /// \brief The subscript operator.
  1482     ///
  1583     ///
  1483     /// The subscript operator.
  1584     /// The subscript operator.
  1484     /// \param e The edge 
  1585     /// \param e The edge 
  1513 
  1614 
  1514     /// \brief Constructor
  1615     /// \brief Constructor
  1515     ///
  1616     ///
  1516     /// Constructor
  1617     /// Constructor
  1517     /// \param _graph The graph that the map belongs to.
  1618     /// \param _graph The graph that the map belongs to.
  1518     ForwardMap(const Graph& _graph) : graph(_graph) {}
  1619     explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
  1519 
  1620 
  1520     /// \brief The subscript operator.
  1621     /// \brief The subscript operator.
  1521     ///
  1622     ///
  1522     /// The subscript operator.
  1623     /// The subscript operator.
  1523     /// \param key An undirected edge 
  1624     /// \param key An undirected edge 
  1552 
  1653 
  1553     /// \brief Constructor
  1654     /// \brief Constructor
  1554     ///
  1655     ///
  1555     /// Constructor
  1656     /// Constructor
  1556     /// \param _graph The graph that the map belongs to.
  1657     /// \param _graph The graph that the map belongs to.
  1557     BackwardMap(const Graph& _graph) : graph(_graph) {}
  1658     explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
  1558 
  1659 
  1559     /// \brief The subscript operator.
  1660     /// \brief The subscript operator.
  1560     ///
  1661     ///
  1561     /// The subscript operator.
  1662     /// The subscript operator.
  1562     /// \param key An undirected edge 
  1663     /// \param key An undirected edge 
  1590     typedef typename NodeMap::Value Value;
  1691     typedef typename NodeMap::Value Value;
  1591 
  1692 
  1592     /// \brief Constructor
  1693     /// \brief Constructor
  1593     ///
  1694     ///
  1594     /// Contructor of the map
  1695     /// Contructor of the map
  1595     PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) 
  1696     explicit PotentialDifferenceMap(const Graph& _graph, 
       
  1697                                     const NodeMap& _potential) 
  1596       : graph(_graph), potential(_potential) {}
  1698       : graph(_graph), potential(_potential) {}
  1597 
  1699 
  1598     /// \brief Const subscription operator
  1700     /// \brief Const subscription operator
  1599     ///
  1701     ///
  1600     /// Const subscription operator
  1702     /// Const subscription operator
  1674   public:
  1776   public:
  1675 
  1777 
  1676     /// \brief Constructor.
  1778     /// \brief Constructor.
  1677     ///
  1779     ///
  1678     /// Constructor for creating in-degree map.
  1780     /// Constructor for creating in-degree map.
  1679     InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1781     explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1680       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1782       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1681       
  1783       
  1682       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1784       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1683 	deg[it] = countInEdges(graph, it);
  1785 	deg[it] = countInEdges(graph, it);
  1684       }
  1786       }
  1786   public:
  1888   public:
  1787 
  1889 
  1788     /// \brief Constructor.
  1890     /// \brief Constructor.
  1789     ///
  1891     ///
  1790     /// Constructor for creating out-degree map.
  1892     /// Constructor for creating out-degree map.
  1791     OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1893     explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
  1792       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1894       Parent::attach(graph.getNotifier(typename _Graph::Edge()));
  1793       
  1895       
  1794       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1896       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1795 	deg[it] = countOutEdges(graph, it);
  1897 	deg[it] = countOutEdges(graph, it);
  1796       }
  1898       }