Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

graph_utils.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_GRAPH_UTILS_H
00018 #define LEMON_GRAPH_UTILS_H
00019 
00020 #include <iterator>
00021 #include <vector>
00022 #include <map>
00023 
00024 #include <lemon/invalid.h>
00025 #include <lemon/utility.h>
00026 #include <lemon/maps.h>
00027 #include <lemon/bits/alteration_notifier.h>
00028 
00034 
00035 
00036 namespace lemon {
00037 
00040 
00046 
00047   template <typename Graph, typename ItemIt>
00048   inline int countItems(const Graph& g) {
00049     int num = 0;
00050     for (ItemIt it(g); it != INVALID; ++it) {
00051       ++num;
00052     }
00053     return num;
00054   }
00055 
00056   // Node counting:
00057 
00058   template <typename Graph>
00059   inline
00060   typename enable_if<typename Graph::NodeNumTag, int>::type
00061   _countNodes(const Graph &g) {
00062     return g.nodeNum();
00063   }
00064 
00065   template <typename Graph>
00066   inline int _countNodes(Wrap<Graph> w) {
00067     return countItems<Graph, typename Graph::NodeIt>(w.value);
00068   }
00069 
00077 
00078   template <typename Graph>
00079   inline int countNodes(const Graph& g) {
00080     return _countNodes<Graph>(g);
00081   }
00082 
00083   // Edge counting:
00084 
00085   template <typename Graph>
00086   inline
00087   typename enable_if<typename Graph::EdgeNumTag, int>::type
00088   _countEdges(const Graph &g) {
00089     return g.edgeNum();
00090   }
00091 
00092   template <typename Graph>
00093   inline int _countEdges(Wrap<Graph> w) {
00094     return countItems<Graph, typename Graph::EdgeIt>(w.value);
00095   }
00096 
00102 
00103   template <typename Graph>
00104   inline int countEdges(const Graph& g) {
00105     return _countEdges<Graph>(g);
00106   }
00107 
00108   // Undirected edge counting:
00109 
00110   template <typename Graph>
00111   inline
00112   typename enable_if<typename Graph::EdgeNumTag, int>::type
00113   _countUndirEdges(const Graph &g) {
00114     return g.undirEdgeNum();
00115   }
00116 
00117   template <typename Graph>
00118   inline int _countUndirEdges(Wrap<Graph> w) {
00119     return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
00120   }
00121 
00127 
00128   template <typename Graph>
00129   inline int countUndirEdges(const Graph& g) {
00130     return _countUndirEdges<Graph>(g);
00131   }
00132 
00133 
00134 
00135   template <typename Graph, typename DegIt>
00136   inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
00137     int num = 0;
00138     for (DegIt it(_g, _n); it != INVALID; ++it) {
00139       ++num;
00140     }
00141     return num;
00142   }
00143 
00148   template <typename Graph>
00149   inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
00150     return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
00151   }
00152 
00157   template <typename Graph>
00158   inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
00159     return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
00160   }
00161 
00162 
00163   template <typename Graph>
00164   inline
00165   typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type 
00166   _findEdge(const Graph &g,
00167             typename Graph::Node u, typename Graph::Node v,
00168             typename Graph::Edge prev = INVALID) {
00169     return g.findEdge(u, v, prev);
00170   }
00171 
00172   template <typename Graph>
00173   inline typename Graph::Edge 
00174   _findEdge(Wrap<Graph> w,
00175             typename Graph::Node u, 
00176             typename Graph::Node v,
00177             typename Graph::Edge prev = INVALID) {
00178     const Graph& g = w.value;
00179     if (prev == INVALID) {
00180       typename Graph::OutEdgeIt e(g, u);
00181       while (e != INVALID && g.target(e) != v) ++e;
00182       return e;
00183     } else {
00184       typename Graph::OutEdgeIt e(g, prev); ++e;
00185       while (e != INVALID && g.target(e) != v) ++e;
00186       return e;
00187     }
00188   }
00189 
00205   // /// \todo We may want to use the "GraphBase" 
00206   // /// interface here...
00207   // /// It would not work with the undirected graphs.
00208   template <typename Graph>
00209   inline typename Graph::Edge findEdge(const Graph &g,
00210                                        typename Graph::Node u, 
00211                                        typename Graph::Node v,
00212                                        typename Graph::Edge prev = INVALID) {
00213     return _findEdge<Graph>(g, u, v, prev);
00214   }
00215 
00228   template <typename _Graph>
00229   class ConEdgeIt : public _Graph::Edge {
00230   public:
00231 
00232     typedef _Graph Graph;
00233     typedef typename Graph::Edge Parent;
00234 
00235     typedef typename Graph::Edge Edge;
00236     typedef typename Graph::Node Node;
00237 
00242     ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
00243       Parent::operator=(findEdge(graph, u, v));
00244     }
00245 
00250     ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
00251     
00255     ConEdgeIt& operator++() {
00256       Parent::operator=(findEdge(graph, graph.source(*this), 
00257                                  graph.target(*this), *this));
00258       return *this;
00259     }
00260   private:
00261     const Graph& graph;
00262   };
00263 
00269   template <typename Target, typename Source, 
00270             typename ItemIt, typename Ref>          
00271   void copyMap(Target& target, const Source& source, 
00272                ItemIt it, const Ref& ref) {
00273     for (; it != INVALID; ++it) {
00274       target[ref[it]] = source[it];
00275     }
00276   }
00277 
00282   template <typename Target, typename Source, 
00283             typename ItemIt>        
00284   void copyMap(Target& target, const Source& source, ItemIt it) {
00285     for (; it != INVALID; ++it) {
00286       target[it] = source[it];
00287     }
00288   }
00289 
00290 
00295   template <typename Target, typename Source>
00296   class GraphCopy {
00297   public: 
00298     typedef typename Source::Node Node;
00299     typedef typename Source::NodeIt NodeIt;
00300     typedef typename Source::Edge Edge;
00301     typedef typename Source::EdgeIt EdgeIt;
00302 
00303     typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
00304     typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
00305 
00311     GraphCopy(Target& _target, const Source& _source) 
00312       : source(_source), target(_target), 
00313         nodeRefMap(_source), edgeRefMap(_source) {
00314       for (NodeIt it(source); it != INVALID; ++it) {
00315         nodeRefMap[it] = target.addNode();
00316       }
00317       for (EdgeIt it(source); it != INVALID; ++it) {
00318         edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
00319                                         nodeRefMap[source.target(it)]);
00320       }
00321     }
00322 
00326     template <typename NodeRef>
00327     const GraphCopy& nodeRef(NodeRef& map) const {
00328       for (NodeIt it(source); it != INVALID; ++it) {
00329         map.set(it, nodeRefMap[it]);
00330       }
00331       return *this;
00332     }
00333 
00337     template <typename NodeRef>
00338     const GraphCopy& nodeCrossRef(NodeRef& map) const {
00339       for (NodeIt it(source); it != INVALID; ++it) {
00340         map.set(nodeRefMap[it], it);
00341       }
00342       return *this;
00343     }
00344 
00348     template <typename EdgeRef>
00349     const GraphCopy& edgeRef(EdgeRef& map) const {
00350       for (EdgeIt it(source); it != INVALID; ++it) {
00351         map.set(it, edgeRefMap[it]);
00352       }
00353       return *this;
00354     }
00355 
00359     template <typename EdgeRef>
00360     const GraphCopy& edgeCrossRef(EdgeRef& map) const {
00361       for (EdgeIt it(source); it != INVALID; ++it) {
00362         map.set(edgeRefMap[it], it);
00363       }
00364       return *this;
00365     }
00366 
00373     template <typename TargetMap, typename SourceMap>
00374     const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
00375       copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
00376       return *this;
00377     }
00378 
00385     template <typename TargetMap, typename SourceMap>
00386     const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
00387       copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
00388       return *this;
00389     }
00390 
00394     const NodeRefMap& nodeRef() const {
00395       return nodeRefMap;
00396     }
00397 
00401     const EdgeRefMap& edgeRef() const {
00402       return edgeRefMap;
00403     }
00404 
00405   private:
00406     
00407     const Source& source;
00408     Target& target;
00409 
00410     NodeRefMap nodeRefMap;
00411     EdgeRefMap edgeRefMap;
00412   };
00413 
00427   template <typename Target, typename Source>
00428   GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
00429     return GraphCopy<Target, Source>(target, source);
00430   }
00431 
00432   template <typename _Graph, typename _Item>
00433   class ItemSetTraits {};
00434   
00435   template <typename _Graph>
00436   class ItemSetTraits<_Graph, typename _Graph::Node> {
00437   public:
00438     
00439     typedef _Graph Graph;
00440 
00441     typedef typename Graph::Node Item;
00442     typedef typename Graph::NodeIt ItemIt;
00443 
00444     template <typename _Value>
00445     class Map : public Graph::template NodeMap<_Value> {
00446     public:
00447       typedef typename Graph::template NodeMap<_Value> Parent; 
00448       typedef typename Parent::Value Value;
00449 
00450       Map(const Graph& _graph) : Parent(_graph) {}
00451       Map(const Graph& _graph, const Value& _value) 
00452         : Parent(_graph, _value) {}
00453     };
00454 
00455   };
00456 
00457   template <typename _Graph>
00458   class ItemSetTraits<_Graph, typename _Graph::Edge> {
00459   public:
00460     
00461     typedef _Graph Graph;
00462 
00463     typedef typename Graph::Edge Item;
00464     typedef typename Graph::EdgeIt ItemIt;
00465 
00466     template <typename _Value>
00467     class Map : public Graph::template EdgeMap<_Value> {
00468     public:
00469       typedef typename Graph::template EdgeMap<_Value> Parent; 
00470       typedef typename Parent::Value Value;
00471 
00472       Map(const Graph& _graph) : Parent(_graph) {}
00473       Map(const Graph& _graph, const Value& _value) 
00474         : Parent(_graph, _value) {}
00475     };
00476 
00477   };
00478 
00479   template <typename _Graph>
00480   class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
00481   public:
00482     
00483     typedef _Graph Graph;
00484 
00485     typedef typename Graph::UndirEdge Item;
00486     typedef typename Graph::UndirEdgeIt ItemIt;
00487 
00488     template <typename _Value>
00489     class Map : public Graph::template UndirEdgeMap<_Value> {
00490     public:
00491       typedef typename Graph::template UndirEdgeMap<_Value> Parent; 
00492       typedef typename Parent::Value Value;
00493 
00494       Map(const Graph& _graph) : Parent(_graph) {}
00495       Map(const Graph& _graph, const Value& _value) 
00496         : Parent(_graph, _value) {}
00497     };
00498 
00499   };
00500 
00502 
00505 
00506   template <typename Map, typename Enable = void>
00507   struct ReferenceMapTraits {
00508     typedef typename Map::Value Value;
00509     typedef typename Map::Value& Reference;
00510     typedef const typename Map::Value& ConstReference;
00511     typedef typename Map::Value* Pointer;
00512     typedef const typename Map::Value* ConstPointer;
00513   };
00514 
00515   template <typename Map>
00516   struct ReferenceMapTraits<
00517     Map, 
00518     typename enable_if<typename Map::FullTypeTag, void>::type
00519   > {
00520     typedef typename Map::Value Value;
00521     typedef typename Map::Reference Reference;
00522     typedef typename Map::ConstReference ConstReference;
00523     typedef typename Map::Pointer Pointer;
00524     typedef typename Map::ConstPointer ConstPointer;
00525   };
00526 
00528 
00537   template <typename _Graph, typename _Item>
00538   class IdMap {
00539   public:
00540     typedef _Graph Graph;
00541     typedef int Value;
00542     typedef _Item Item;
00543     typedef _Item Key;
00544 
00545     typedef True NeedCopy;
00546 
00550     IdMap(const Graph& _graph) : graph(&_graph) {}
00551 
00555     int operator[](const Item& item) const { return graph->id(item);}
00556 
00557 
00558   private:
00559     const Graph* graph;
00560 
00561   public:
00562 
00567     class InverseMap {
00568     public:
00569 
00570       typedef True NeedCopy;
00571 
00575       InverseMap(const Graph& _graph) : graph(&_graph) {}
00576 
00580       InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
00581 
00586       Item operator[](int id) const { return graph->fromId(id, Item());}
00587     private:
00588       const Graph* graph;
00589     };
00590 
00594     InverseMap inverse() const { return InverseMap(*graph);} 
00595 
00596   };
00597 
00598   
00600 
00607   template <
00608     typename _Graph,
00609     typename _Item, 
00610     typename _Value,
00611     typename _Map 
00612     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
00613   >
00614   class InvertableMap : protected _Map {
00615 
00616   public:
00617  
00618     typedef _Map Map;
00619     typedef _Graph Graph;
00620 
00622     typedef typename _Map::Key Key;
00624     typedef typename _Map::Value Value;
00625 
00630     InvertableMap(const Graph& graph) : Map(graph) {} 
00631     
00635     void set(const Key& key, const Value& val) {
00636       Value oldval = Map::operator[](key);
00637       typename Container::iterator it = invMap.find(oldval);
00638       if (it != invMap.end() && it->second == key) {
00639         invMap.erase(it);
00640       }      
00641       invMap.insert(make_pair(val, key));
00642       Map::set(key, val);
00643     }
00644 
00648     const Value operator[](const Key& key) const {
00649       return Map::operator[](key);
00650     }
00651 
00652   protected:
00653 
00658     virtual void add(const Key& key) {
00659       Map::add(key);
00660     }
00661 
00666     virtual void erase(const Key& key) {
00667       Value val = Map::operator[](key);
00668       typename Container::iterator it = invMap.find(val);
00669       if (it != invMap.end() && it->second == key) {
00670         invMap.erase(it);
00671       }
00672       Map::erase(key);
00673     }
00674 
00679     virtual void clear() {
00680       invMap.clear();
00681       Map::clear();
00682     }
00683 
00684   private:
00685     
00686     typedef std::map<Value, Key> Container;
00687     Container invMap;    
00688 
00689   public:
00690 
00695     class InverseMap {
00696     public:
00700       InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
00701 
00703       typedef typename InvertableMap::Key Value;
00705       typedef typename InvertableMap::Value Key; 
00706 
00711       Value operator[](const Key& key) const {
00712         typename Container::const_iterator it = inverted.invMap.find(key);
00713         return it->second;
00714       }
00715       
00716     private:
00717       const InvertableMap& inverted;
00718     };
00719 
00723     InverseMap inverse() const {
00724       return InverseMap(*this);
00725     } 
00726 
00727 
00728     
00729   };
00730 
00747   template <
00748     typename _Graph,   
00749     typename _Item,
00750     typename _Map 
00751     = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
00752   >
00753   class DescriptorMap : protected _Map {
00754 
00755     typedef _Item Item;
00756     typedef _Map Map;
00757 
00758   public:
00760     typedef _Graph Graph;
00761 
00763     typedef typename _Map::Key Key;
00765     typedef typename _Map::Value Value;
00766 
00770     DescriptorMap(const Graph& _graph) : Map(_graph) {
00771       build();
00772     }
00773 
00774   protected:
00775 
00780     virtual void add(const Item& item) {
00781       Map::add(item);
00782       Map::set(item, invMap.size());
00783       invMap.push_back(item);
00784     }
00785 
00790     virtual void erase(const Item& item) {
00791       Map::set(invMap.back(), Map::operator[](item));
00792       invMap[Map::operator[](item)] = invMap.back();
00793       invMap.pop_back();
00794       Map::erase(item);
00795     }
00796 
00801     virtual void build() {
00802       Map::build();
00803       Item it;
00804       const typename Map::Graph* graph = Map::getGraph(); 
00805       for (graph->first(it); it != INVALID; graph->next(it)) {
00806         Map::set(it, invMap.size());
00807         invMap.push_back(it);   
00808       }      
00809     }
00810     
00815     virtual void clear() {
00816       invMap.clear();
00817       Map::clear();
00818     }
00819 
00820   public:
00821 
00825     void swap(const Item& p, const Item& q) {
00826       int pi = Map::operator[](p);
00827       int qi = Map::operator[](q);
00828       Map::set(p, qi);
00829       invMap[qi] = p;
00830       Map::set(q, pi);
00831       invMap[pi] = q;
00832     }
00833 
00837     int operator[](const Item& item) const {
00838       return Map::operator[](item);
00839     }
00840     
00841   private:
00842 
00843     typedef std::vector<Item> Container;
00844     Container invMap;
00845 
00846   public:
00850     class InverseMap {
00851     public:
00855       InverseMap(const DescriptorMap& _inverted) 
00856         : inverted(_inverted) {}
00857 
00858 
00860       typedef typename DescriptorMap::Key Value;
00862       typedef typename DescriptorMap::Value Key; 
00863 
00868       Value operator[](const Key& key) const {
00869         return inverted.invMap[key];
00870       }
00871 
00875       int size() const {
00876         return inverted.invMap.size();
00877       }
00878       
00879     private:
00880       const DescriptorMap& inverted;
00881     };
00882 
00886     const InverseMap inverse() const {
00887       return InverseMap(*this);
00888     }
00889   };
00890 
00895   template <typename Graph>
00896   class SourceMap {
00897   public:
00898 
00899     typedef True NeedCopy;
00900 
00901     typedef typename Graph::Node Value;
00902     typedef typename Graph::Edge Key;
00903 
00908     SourceMap(const Graph& _graph) : graph(_graph) {}
00909 
00915     Value operator[](const Key& edge) {
00916       return graph.source(edge);
00917     }
00918 
00919   private:
00920     const Graph& graph;
00921   };
00922 
00927   template <typename Graph>
00928   inline SourceMap<Graph> sourceMap(const Graph& graph) {
00929     return SourceMap<Graph>(graph);
00930   } 
00931 
00936   template <typename Graph>
00937   class TargetMap {
00938   public:
00939 
00940     typedef True NeedCopy;
00941 
00942     typedef typename Graph::Node Value;
00943     typedef typename Graph::Edge Key;
00944 
00949     TargetMap(const Graph& _graph) : graph(_graph) {}
00950 
00956     Value operator[](const Key& e) {
00957       return graph.target(e);
00958     }
00959 
00960   private:
00961     const Graph& graph;
00962   };
00963 
00968   template <typename Graph>
00969   inline TargetMap<Graph> targetMap(const Graph& graph) {
00970     return TargetMap<Graph>(graph);
00971   }
00972 
00977   template <typename Graph>
00978   class ForwardMap {
00979   public:
00980 
00981     typedef True NeedCopy;
00982 
00983     typedef typename Graph::Edge Value;
00984     typedef typename Graph::UndirEdge Key;
00985 
00990     ForwardMap(const Graph& _graph) : graph(_graph) {}
00991 
00997     Value operator[](const Key& key) const {
00998       return graph.direct(key, true);
00999     }
01000 
01001   private:
01002     const Graph& graph;
01003   };
01004 
01009   template <typename Graph>
01010   inline ForwardMap<Graph> forwardMap(const Graph& graph) {
01011     return ForwardMap<Graph>(graph);
01012   }
01013 
01018   template <typename Graph>
01019   class BackwardMap {
01020   public:
01021     typedef True NeedCopy;
01022 
01023     typedef typename Graph::Edge Value;
01024     typedef typename Graph::UndirEdge Key;
01025 
01030     BackwardMap(const Graph& _graph) : graph(_graph) {}
01031 
01037     Value operator[](const Key& key) const {
01038       return graph.direct(key, false);
01039     }
01040 
01041   private:
01042     const Graph& graph;
01043   };
01044 
01046 
01049   template <typename Graph>
01050   inline BackwardMap<Graph> backwardMap(const Graph& graph) {
01051     return BackwardMap<Graph>(graph);
01052   }
01053 
01054   template <typename _Graph>
01055   class DegMapBase {
01056   public:
01057     
01058     typedef _Graph Graph;
01059 
01060   protected:
01061 
01062     typedef typename Graph::template NodeMap<int> IntNodeMap;
01063     
01064   };
01065 
01074 
01075   template <typename _Graph>
01076   class InDegMap  
01077     : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
01078 
01079   public:
01080     
01081     typedef _Graph Graph;
01082     typedef int Value;
01083     typedef typename Graph::Node Key;
01084 
01085   private:
01086 
01087     class AutoNodeMap : public Graph::template NodeMap<int> {
01088     public:
01089 
01090       typedef typename Graph::template NodeMap<int> Parent;
01091 
01092       typedef typename Parent::Key Key;
01093       typedef typename Parent::Value Value;
01094       
01095       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
01096       
01097       void add(const Key& key) {
01098         Parent::add(key);
01099         Parent::set(key, 0);
01100       }
01101     };
01102 
01103   public:
01104 
01108     InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
01109       AlterationNotifier<typename _Graph::Edge>
01110 	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
01111       
01112       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01113         deg[it] = countInEdges(graph, it);
01114       }
01115     }
01116 
01117     virtual ~InDegMap() {
01118       AlterationNotifier<typename _Graph::Edge>::
01119 	ObserverBase::detach();
01120     }
01121     
01123     int operator[](const Key& key) const {
01124       return deg[key];
01125     }
01126 
01127   protected:
01128     
01129     typedef typename Graph::Edge Edge;
01130 
01131     virtual void add(const Edge& edge) {
01132       ++deg[graph.target(edge)];
01133     }
01134 
01135     virtual void erase(const Edge& edge) {
01136       --deg[graph.target(edge)];
01137     }
01138 
01139 
01140     virtual void build() {
01141       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01142         deg[it] = countInEdges(graph, it);
01143       }      
01144     }
01145 
01146     virtual void clear() {
01147       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01148         deg[it] = 0;
01149       }
01150     }
01151   private:
01152     
01153     const _Graph& graph;
01154     AutoNodeMap deg;
01155   };
01156 
01157 
01166 
01167   template <typename _Graph>
01168   class OutDegMap  
01169     : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
01170 
01171   public:
01172     
01173     typedef _Graph Graph;
01174     typedef int Value;
01175     typedef typename Graph::Node Key;
01176 
01177   private:
01178 
01179     class AutoNodeMap : public Graph::template NodeMap<int> {
01180     public:
01181 
01182       typedef typename Graph::template NodeMap<int> Parent;
01183 
01184       typedef typename Parent::Key Key;
01185       typedef typename Parent::Value Value;
01186       
01187       AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
01188       
01189       void add(const Key& key) {
01190         Parent::add(key);
01191         Parent::set(key, 0);
01192       }
01193     };
01194 
01195   public:
01196 
01200     OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
01201       AlterationNotifier<typename _Graph::Edge>
01202 	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
01203       
01204       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01205         deg[it] = countOutEdges(graph, it);
01206       }
01207     }
01208 
01209     virtual ~OutDegMap() {
01210       AlterationNotifier<typename _Graph::Edge>::
01211 	ObserverBase::detach();
01212     }
01213     
01215     int operator[](const Key& key) const {
01216       return deg[key];
01217     }
01218 
01219   protected:
01220     
01221     typedef typename Graph::Edge Edge;
01222 
01223     virtual void add(const Edge& edge) {
01224       ++deg[graph.source(edge)];
01225     }
01226 
01227     virtual void erase(const Edge& edge) {
01228       --deg[graph.source(edge)];
01229     }
01230 
01231 
01232     virtual void build() {
01233       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01234         deg[it] = countOutEdges(graph, it);
01235       }      
01236     }
01237 
01238     virtual void clear() {
01239       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
01240         deg[it] = 0;
01241       }
01242     }
01243   private:
01244     
01245     const _Graph& graph;
01246     AutoNodeMap deg;
01247   };
01248 
01250 
01251 } //END OF NAMESPACE LEMON
01252 
01253 #endif

Generated on Sat Aug 27 14:14:52 2005 for LEMON by  doxygen 1.4.4