COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/graph_utils.h @ 1979:c2992fd74dad

Last change on this file since 1979:c2992fd74dad was 1956:a055123339d5, checked in by Alpar Juttner, 18 years ago

Unified copyright notices

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