lemon/list_graph.h
changeset 1095 ad40f7d32846
parent 788 c92296660262
child 1021 a12cca3ad15a
child 1146 81f70097df81
     1.1 --- a/lemon/list_graph.h	Fri Aug 09 11:07:27 2013 +0200
     1.2 +++ b/lemon/list_graph.h	Sun Aug 11 15:28:12 2013 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2009
     1.8 + * Copyright (C) 2003-2010
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -21,7 +21,7 @@
    1.13  
    1.14  ///\ingroup graphs
    1.15  ///\file
    1.16 -///\brief ListDigraph, ListGraph classes.
    1.17 +///\brief ListDigraph and ListGraph classes.
    1.18  
    1.19  #include <lemon/core.h>
    1.20  #include <lemon/error.h>
    1.21 @@ -32,6 +32,8 @@
    1.22  
    1.23  namespace lemon {
    1.24  
    1.25 +  class ListDigraph;
    1.26 +
    1.27    class ListDigraphBase {
    1.28  
    1.29    protected:
    1.30 @@ -62,6 +64,7 @@
    1.31  
    1.32      class Node {
    1.33        friend class ListDigraphBase;
    1.34 +      friend class ListDigraph;
    1.35      protected:
    1.36  
    1.37        int id;
    1.38 @@ -77,6 +80,7 @@
    1.39  
    1.40      class Arc {
    1.41        friend class ListDigraphBase;
    1.42 +      friend class ListDigraph;
    1.43      protected:
    1.44  
    1.45        int id;
    1.46 @@ -116,20 +120,20 @@
    1.47      void first(Arc& arc) const {
    1.48        int n;
    1.49        for(n = first_node;
    1.50 -          n!=-1 && nodes[n].first_in == -1;
    1.51 +          n != -1 && nodes[n].first_out == -1;
    1.52            n = nodes[n].next) {}
    1.53 -      arc.id = (n == -1) ? -1 : nodes[n].first_in;
    1.54 +      arc.id = (n == -1) ? -1 : nodes[n].first_out;
    1.55      }
    1.56  
    1.57      void next(Arc& arc) const {
    1.58 -      if (arcs[arc.id].next_in != -1) {
    1.59 -        arc.id = arcs[arc.id].next_in;
    1.60 +      if (arcs[arc.id].next_out != -1) {
    1.61 +        arc.id = arcs[arc.id].next_out;
    1.62        } else {
    1.63          int n;
    1.64 -        for(n = nodes[arcs[arc.id].target].next;
    1.65 -            n!=-1 && nodes[n].first_in == -1;
    1.66 +        for(n = nodes[arcs[arc.id].source].next;
    1.67 +            n != -1 && nodes[n].first_out == -1;
    1.68              n = nodes[n].next) {}
    1.69 -        arc.id = (n == -1) ? -1 : nodes[n].first_in;
    1.70 +        arc.id = (n == -1) ? -1 : nodes[n].first_out;
    1.71        }
    1.72      }
    1.73  
    1.74 @@ -311,31 +315,27 @@
    1.75  
    1.76    ///A general directed graph structure.
    1.77  
    1.78 -  ///\ref ListDigraph is a simple and fast <em>directed graph</em>
    1.79 -  ///implementation based on static linked lists that are stored in
    1.80 +  ///\ref ListDigraph is a versatile and fast directed graph
    1.81 +  ///implementation based on linked lists that are stored in
    1.82    ///\c std::vector structures.
    1.83    ///
    1.84 -  ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
    1.85 -  ///also provides several useful additional functionalities.
    1.86 -  ///Most of the member functions and nested classes are documented
    1.87 +  ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
    1.88 +  ///and it also provides several useful additional functionalities.
    1.89 +  ///Most of its member functions and nested classes are documented
    1.90    ///only in the concept class.
    1.91    ///
    1.92 +  ///This class provides only linear time counting for nodes and arcs.
    1.93 +  ///
    1.94    ///\sa concepts::Digraph
    1.95 -
    1.96 +  ///\sa ListGraph
    1.97    class ListDigraph : public ExtendedListDigraphBase {
    1.98      typedef ExtendedListDigraphBase Parent;
    1.99  
   1.100    private:
   1.101 -    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
   1.102 -
   1.103 -    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
   1.104 -    ///
   1.105 +    /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
   1.106      ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
   1.107 -    ///\brief Assignment of ListDigraph to another one is \e not allowed.
   1.108 -    ///Use copyDigraph() instead.
   1.109 -
   1.110 -    ///Assignment of ListDigraph to another one is \e not allowed.
   1.111 -    ///Use copyDigraph() instead.
   1.112 +    /// \brief Assignment of a digraph to another one is \e not allowed.
   1.113 +    /// Use DigraphCopy instead.
   1.114      void operator=(const ListDigraph &) {}
   1.115    public:
   1.116  
   1.117 @@ -347,71 +347,72 @@
   1.118  
   1.119      ///Add a new node to the digraph.
   1.120  
   1.121 -    ///Add a new node to the digraph.
   1.122 +    ///This function adds a new node to the digraph.
   1.123      ///\return The new node.
   1.124      Node addNode() { return Parent::addNode(); }
   1.125  
   1.126      ///Add a new arc to the digraph.
   1.127  
   1.128 -    ///Add a new arc to the digraph with source node \c s
   1.129 +    ///This function adds a new arc to the digraph with source node \c s
   1.130      ///and target node \c t.
   1.131      ///\return The new arc.
   1.132 -    Arc addArc(const Node& s, const Node& t) {
   1.133 +    Arc addArc(Node s, Node t) {
   1.134        return Parent::addArc(s, t);
   1.135      }
   1.136  
   1.137      ///\brief Erase a node from the digraph.
   1.138      ///
   1.139 -    ///Erase a node from the digraph.
   1.140 +    ///This function erases the given node along with its outgoing and
   1.141 +    ///incoming arcs from the digraph.
   1.142      ///
   1.143 -    void erase(const Node& n) { Parent::erase(n); }
   1.144 +    ///\note All iterators referencing the removed node or the connected
   1.145 +    ///arcs are invalidated, of course.
   1.146 +    void erase(Node n) { Parent::erase(n); }
   1.147  
   1.148      ///\brief Erase an arc from the digraph.
   1.149      ///
   1.150 -    ///Erase an arc from the digraph.
   1.151 +    ///This function erases the given arc from the digraph.
   1.152      ///
   1.153 -    void erase(const Arc& a) { Parent::erase(a); }
   1.154 +    ///\note All iterators referencing the removed arc are invalidated,
   1.155 +    ///of course.
   1.156 +    void erase(Arc a) { Parent::erase(a); }
   1.157  
   1.158      /// Node validity check
   1.159  
   1.160 -    /// This function gives back true if the given node is valid,
   1.161 -    /// ie. it is a real node of the graph.
   1.162 +    /// This function gives back \c true if the given node is valid,
   1.163 +    /// i.e. it is a real node of the digraph.
   1.164      ///
   1.165 -    /// \warning A Node pointing to a removed item
   1.166 -    /// could become valid again later if new nodes are
   1.167 -    /// added to the graph.
   1.168 +    /// \warning A removed node could become valid again if new nodes are
   1.169 +    /// added to the digraph.
   1.170      bool valid(Node n) const { return Parent::valid(n); }
   1.171  
   1.172      /// Arc validity check
   1.173  
   1.174 -    /// This function gives back true if the given arc is valid,
   1.175 -    /// ie. it is a real arc of the graph.
   1.176 +    /// This function gives back \c true if the given arc is valid,
   1.177 +    /// i.e. it is a real arc of the digraph.
   1.178      ///
   1.179 -    /// \warning An Arc pointing to a removed item
   1.180 -    /// could become valid again later if new nodes are
   1.181 -    /// added to the graph.
   1.182 +    /// \warning A removed arc could become valid again if new arcs are
   1.183 +    /// added to the digraph.
   1.184      bool valid(Arc a) const { return Parent::valid(a); }
   1.185  
   1.186 -    /// Change the target of \c a to \c n
   1.187 +    /// Change the target node of an arc
   1.188  
   1.189 -    /// Change the target of \c a to \c n
   1.190 +    /// This function changes the target node of the given arc \c a to \c n.
   1.191      ///
   1.192 -    ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
   1.193 -    ///the changed arc remain valid. However <tt>InArcIt</tt>s are
   1.194 -    ///invalidated.
   1.195 +    ///\note \c ArcIt and \c OutArcIt iterators referencing the changed
   1.196 +    ///arc remain valid, but \c InArcIt iterators are invalidated.
   1.197      ///
   1.198      ///\warning This functionality cannot be used together with the Snapshot
   1.199      ///feature.
   1.200      void changeTarget(Arc a, Node n) {
   1.201        Parent::changeTarget(a,n);
   1.202      }
   1.203 -    /// Change the source of \c a to \c n
   1.204 +    /// Change the source node of an arc
   1.205  
   1.206 -    /// Change the source of \c a to \c n
   1.207 +    /// This function changes the source node of the given arc \c a to \c n.
   1.208      ///
   1.209 -    ///\note The <tt>InArcIt</tt>s referencing the changed arc remain
   1.210 -    ///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are
   1.211 -    ///invalidated.
   1.212 +    ///\note \c InArcIt iterators referencing the changed arc remain
   1.213 +    ///valid, but \c ArcIt and \c OutArcIt iterators are invalidated.
   1.214      ///
   1.215      ///\warning This functionality cannot be used together with the Snapshot
   1.216      ///feature.
   1.217 @@ -419,94 +420,76 @@
   1.218        Parent::changeSource(a,n);
   1.219      }
   1.220  
   1.221 -    /// Invert the direction of an arc.
   1.222 +    /// Reverse the direction of an arc.
   1.223  
   1.224 -    ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
   1.225 -    ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
   1.226 -    ///invalidated.
   1.227 +    /// This function reverses the direction of the given arc.
   1.228 +    ///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing
   1.229 +    ///the changed arc are invalidated.
   1.230      ///
   1.231      ///\warning This functionality cannot be used together with the Snapshot
   1.232      ///feature.
   1.233 -    void reverseArc(Arc e) {
   1.234 -      Node t=target(e);
   1.235 -      changeTarget(e,source(e));
   1.236 -      changeSource(e,t);
   1.237 +    void reverseArc(Arc a) {
   1.238 +      Node t=target(a);
   1.239 +      changeTarget(a,source(a));
   1.240 +      changeSource(a,t);
   1.241      }
   1.242  
   1.243 -    /// Reserve memory for nodes.
   1.244 -
   1.245 -    /// Using this function it is possible to avoid the superfluous memory
   1.246 -    /// allocation: if you know that the digraph you want to build will
   1.247 -    /// be very large (e.g. it will contain millions of nodes and/or arcs)
   1.248 -    /// then it is worth reserving space for this amount before starting
   1.249 -    /// to build the digraph.
   1.250 -    /// \sa reserveArc
   1.251 -    void reserveNode(int n) { nodes.reserve(n); };
   1.252 -
   1.253 -    /// Reserve memory for arcs.
   1.254 -
   1.255 -    /// Using this function it is possible to avoid the superfluous memory
   1.256 -    /// allocation: if you know that the digraph you want to build will
   1.257 -    /// be very large (e.g. it will contain millions of nodes and/or arcs)
   1.258 -    /// then it is worth reserving space for this amount before starting
   1.259 -    /// to build the digraph.
   1.260 -    /// \sa reserveNode
   1.261 -    void reserveArc(int m) { arcs.reserve(m); };
   1.262 -
   1.263      ///Contract two nodes.
   1.264  
   1.265 -    ///This function contracts two nodes.
   1.266 -    ///Node \p b will be removed but instead of deleting
   1.267 -    ///incident arcs, they will be joined to \p a.
   1.268 -    ///The last parameter \p r controls whether to remove loops. \c true
   1.269 -    ///means that loops will be removed.
   1.270 +    ///This function contracts the given two nodes.
   1.271 +    ///Node \c v is removed, but instead of deleting its
   1.272 +    ///incident arcs, they are joined to node \c u.
   1.273 +    ///If the last parameter \c r is \c true (this is the default value),
   1.274 +    ///then the newly created loops are removed.
   1.275      ///
   1.276 -    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
   1.277 -    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
   1.278 -    ///may be invalidated.
   1.279 +    ///\note The moved arcs are joined to node \c u using changeSource()
   1.280 +    ///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are
   1.281 +    ///invalidated for the outgoing arcs of node \c v and \c InArcIt
   1.282 +    ///iterators are invalidated for the incomming arcs of \c v.
   1.283 +    ///Moreover all iterators referencing node \c v or the removed
   1.284 +    ///loops are also invalidated. Other iterators remain valid.
   1.285      ///
   1.286      ///\warning This functionality cannot be used together with the Snapshot
   1.287      ///feature.
   1.288 -    void contract(Node a, Node b, bool r = true)
   1.289 +    void contract(Node u, Node v, bool r = true)
   1.290      {
   1.291 -      for(OutArcIt e(*this,b);e!=INVALID;) {
   1.292 +      for(OutArcIt e(*this,v);e!=INVALID;) {
   1.293          OutArcIt f=e;
   1.294          ++f;
   1.295 -        if(r && target(e)==a) erase(e);
   1.296 -        else changeSource(e,a);
   1.297 +        if(r && target(e)==u) erase(e);
   1.298 +        else changeSource(e,u);
   1.299          e=f;
   1.300        }
   1.301 -      for(InArcIt e(*this,b);e!=INVALID;) {
   1.302 +      for(InArcIt e(*this,v);e!=INVALID;) {
   1.303          InArcIt f=e;
   1.304          ++f;
   1.305 -        if(r && source(e)==a) erase(e);
   1.306 -        else changeTarget(e,a);
   1.307 +        if(r && source(e)==u) erase(e);
   1.308 +        else changeTarget(e,u);
   1.309          e=f;
   1.310        }
   1.311 -      erase(b);
   1.312 +      erase(v);
   1.313      }
   1.314  
   1.315      ///Split a node.
   1.316  
   1.317 -    ///This function splits a node. First a new node is added to the digraph,
   1.318 -    ///then the source of each outgoing arc of \c n is moved to this new node.
   1.319 -    ///If \c connect is \c true (this is the default value), then a new arc
   1.320 -    ///from \c n to the newly created node is also added.
   1.321 +    ///This function splits the given node. First, a new node is added
   1.322 +    ///to the digraph, then the source of each outgoing arc of node \c n
   1.323 +    ///is moved to this new node.
   1.324 +    ///If the second parameter \c connect is \c true (this is the default
   1.325 +    ///value), then a new arc from node \c n to the newly created node
   1.326 +    ///is also added.
   1.327      ///\return The newly created node.
   1.328      ///
   1.329 -    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
   1.330 -    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
   1.331 -    ///be invalidated.
   1.332 +    ///\note All iterators remain valid.
   1.333      ///
   1.334 -    ///\warning This functionality cannot be used in conjunction with the
   1.335 +    ///\warning This functionality cannot be used together with the
   1.336      ///Snapshot feature.
   1.337      Node split(Node n, bool connect = true) {
   1.338        Node b = addNode();
   1.339 -      for(OutArcIt e(*this,n);e!=INVALID;) {
   1.340 -        OutArcIt f=e;
   1.341 -        ++f;
   1.342 -        changeSource(e,b);
   1.343 -        e=f;
   1.344 +      nodes[b.id].first_out=nodes[n.id].first_out;
   1.345 +      nodes[n.id].first_out=-1;
   1.346 +      for(int i=nodes[b.id].first_out; i!=-1; i=arcs[i].next_out) {
   1.347 +        arcs[i].source=b.id;
   1.348        }
   1.349        if (connect) addArc(n,b);
   1.350        return b;
   1.351 @@ -514,21 +497,53 @@
   1.352  
   1.353      ///Split an arc.
   1.354  
   1.355 -    ///This function splits an arc. First a new node \c b is added to
   1.356 -    ///the digraph, then the original arc is re-targeted to \c
   1.357 -    ///b. Finally an arc from \c b to the original target is added.
   1.358 +    ///This function splits the given arc. First, a new node \c v is
   1.359 +    ///added to the digraph, then the target node of the original arc
   1.360 +    ///is set to \c v. Finally, an arc from \c v to the original target
   1.361 +    ///is added.
   1.362 +    ///\return The newly created node.
   1.363      ///
   1.364 -    ///\return The newly created node.
   1.365 +    ///\note \c InArcIt iterators referencing the original arc are
   1.366 +    ///invalidated. Other iterators remain valid.
   1.367      ///
   1.368      ///\warning This functionality cannot be used together with the
   1.369      ///Snapshot feature.
   1.370 -    Node split(Arc e) {
   1.371 -      Node b = addNode();
   1.372 -      addArc(b,target(e));
   1.373 -      changeTarget(e,b);
   1.374 -      return b;
   1.375 +    Node split(Arc a) {
   1.376 +      Node v = addNode();
   1.377 +      addArc(v,target(a));
   1.378 +      changeTarget(a,v);
   1.379 +      return v;
   1.380      }
   1.381  
   1.382 +    ///Clear the digraph.
   1.383 +
   1.384 +    ///This function erases all nodes and arcs from the digraph.
   1.385 +    ///
   1.386 +    ///\note All iterators of the digraph are invalidated, of course.
   1.387 +    void clear() {
   1.388 +      Parent::clear();
   1.389 +    }
   1.390 +
   1.391 +    /// Reserve memory for nodes.
   1.392 +
   1.393 +    /// Using this function, it is possible to avoid superfluous memory
   1.394 +    /// allocation: if you know that the digraph you want to build will
   1.395 +    /// be large (e.g. it will contain millions of nodes and/or arcs),
   1.396 +    /// then it is worth reserving space for this amount before starting
   1.397 +    /// to build the digraph.
   1.398 +    /// \sa reserveArc()
   1.399 +    void reserveNode(int n) { nodes.reserve(n); };
   1.400 +
   1.401 +    /// Reserve memory for arcs.
   1.402 +
   1.403 +    /// Using this function, it is possible to avoid superfluous memory
   1.404 +    /// allocation: if you know that the digraph you want to build will
   1.405 +    /// be large (e.g. it will contain millions of nodes and/or arcs),
   1.406 +    /// then it is worth reserving space for this amount before starting
   1.407 +    /// to build the digraph.
   1.408 +    /// \sa reserveNode()
   1.409 +    void reserveArc(int m) { arcs.reserve(m); };
   1.410 +
   1.411      /// \brief Class to make a snapshot of the digraph and restore
   1.412      /// it later.
   1.413      ///
   1.414 @@ -537,9 +552,15 @@
   1.415      /// The newly added nodes and arcs can be removed using the
   1.416      /// restore() function.
   1.417      ///
   1.418 -    /// \warning Arc and node deletions and other modifications (e.g.
   1.419 -    /// contracting, splitting, reversing arcs or nodes) cannot be
   1.420 +    /// \note After a state is restored, you cannot restore a later state,
   1.421 +    /// i.e. you cannot add the removed nodes and arcs again using
   1.422 +    /// another Snapshot instance.
   1.423 +    ///
   1.424 +    /// \warning Node and arc deletions and other modifications (e.g.
   1.425 +    /// reversing, contracting, splitting arcs or nodes) cannot be
   1.426      /// restored. These events invalidate the snapshot.
   1.427 +    /// However, the arcs and nodes that were added to the digraph after
   1.428 +    /// making the current snapshot can be removed without invalidating it.
   1.429      class Snapshot {
   1.430      protected:
   1.431  
   1.432 @@ -709,39 +730,40 @@
   1.433        /// \brief Default constructor.
   1.434        ///
   1.435        /// Default constructor.
   1.436 -      /// To actually make a snapshot you must call save().
   1.437 +      /// You have to call save() to actually make a snapshot.
   1.438        Snapshot()
   1.439          : digraph(0), node_observer_proxy(*this),
   1.440            arc_observer_proxy(*this) {}
   1.441  
   1.442        /// \brief Constructor that immediately makes a snapshot.
   1.443        ///
   1.444 -      /// This constructor immediately makes a snapshot of the digraph.
   1.445 -      /// \param _digraph The digraph we make a snapshot of.
   1.446 -      Snapshot(ListDigraph &_digraph)
   1.447 +      /// This constructor immediately makes a snapshot of the given digraph.
   1.448 +      Snapshot(ListDigraph &gr)
   1.449          : node_observer_proxy(*this),
   1.450            arc_observer_proxy(*this) {
   1.451 -        attach(_digraph);
   1.452 +        attach(gr);
   1.453        }
   1.454  
   1.455        /// \brief Make a snapshot.
   1.456        ///
   1.457 -      /// Make a snapshot of the digraph.
   1.458 -      ///
   1.459 -      /// This function can be called more than once. In case of a repeated
   1.460 +      /// This function makes a snapshot of the given digraph.
   1.461 +      /// It can be called more than once. In case of a repeated
   1.462        /// call, the previous snapshot gets lost.
   1.463 -      /// \param _digraph The digraph we make the snapshot of.
   1.464 -      void save(ListDigraph &_digraph) {
   1.465 +      void save(ListDigraph &gr) {
   1.466          if (attached()) {
   1.467            detach();
   1.468            clear();
   1.469          }
   1.470 -        attach(_digraph);
   1.471 +        attach(gr);
   1.472        }
   1.473  
   1.474        /// \brief Undo the changes until the last snapshot.
   1.475 -      //
   1.476 -      /// Undo the changes until the last snapshot created by save().
   1.477 +      ///
   1.478 +      /// This function undos the changes until the last snapshot
   1.479 +      /// created by save() or Snapshot(ListDigraph&).
   1.480 +      ///
   1.481 +      /// \warning This method invalidates the snapshot, i.e. repeated
   1.482 +      /// restoring is not supported unless you call save() again.
   1.483        void restore() {
   1.484          detach();
   1.485          for(std::list<Arc>::iterator it = added_arcs.begin();
   1.486 @@ -755,9 +777,9 @@
   1.487          clear();
   1.488        }
   1.489  
   1.490 -      /// \brief Gives back true when the snapshot is valid.
   1.491 +      /// \brief Returns \c true if the snapshot is valid.
   1.492        ///
   1.493 -      /// Gives back true when the snapshot is valid.
   1.494 +      /// This function returns \c true if the snapshot is valid.
   1.495        bool valid() const {
   1.496          return attached();
   1.497        }
   1.498 @@ -795,10 +817,6 @@
   1.499  
   1.500      typedef ListGraphBase Graph;
   1.501  
   1.502 -    class Node;
   1.503 -    class Arc;
   1.504 -    class Edge;
   1.505 -
   1.506      class Node {
   1.507        friend class ListGraphBase;
   1.508      protected:
   1.509 @@ -848,8 +866,6 @@
   1.510        bool operator<(const Arc& arc) const {return id < arc.id;}
   1.511      };
   1.512  
   1.513 -
   1.514 -
   1.515      ListGraphBase()
   1.516        : nodes(), first_node(-1),
   1.517          first_free_node(-1), arcs(), first_free_arc(-1) {}
   1.518 @@ -1164,31 +1180,27 @@
   1.519  
   1.520    ///A general undirected graph structure.
   1.521  
   1.522 -  ///\ref ListGraph is a simple and fast <em>undirected graph</em>
   1.523 -  ///implementation based on static linked lists that are stored in
   1.524 +  ///\ref ListGraph is a versatile and fast undirected graph
   1.525 +  ///implementation based on linked lists that are stored in
   1.526    ///\c std::vector structures.
   1.527    ///
   1.528 -  ///It conforms to the \ref concepts::Graph "Graph concept" and it
   1.529 -  ///also provides several useful additional functionalities.
   1.530 -  ///Most of the member functions and nested classes are documented
   1.531 +  ///This type fully conforms to the \ref concepts::Graph "Graph concept"
   1.532 +  ///and it also provides several useful additional functionalities.
   1.533 +  ///Most of its member functions and nested classes are documented
   1.534    ///only in the concept class.
   1.535    ///
   1.536 +  ///This class provides only linear time counting for nodes, edges and arcs.
   1.537 +  ///
   1.538    ///\sa concepts::Graph
   1.539 -
   1.540 +  ///\sa ListDigraph
   1.541    class ListGraph : public ExtendedListGraphBase {
   1.542      typedef ExtendedListGraphBase Parent;
   1.543  
   1.544    private:
   1.545 -    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
   1.546 -
   1.547 -    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
   1.548 -    ///
   1.549 +    /// Graphs are \e not copy constructible. Use GraphCopy instead.
   1.550      ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
   1.551 -    ///\brief Assignment of ListGraph to another one is \e not allowed.
   1.552 -    ///Use copyGraph() instead.
   1.553 -
   1.554 -    ///Assignment of ListGraph to another one is \e not allowed.
   1.555 -    ///Use copyGraph() instead.
   1.556 +    /// \brief Assignment of a graph to another one is \e not allowed.
   1.557 +    /// Use GraphCopy instead.
   1.558      void operator=(const ListGraph &) {}
   1.559    public:
   1.560      /// Constructor
   1.561 @@ -1201,94 +1213,102 @@
   1.562  
   1.563      /// \brief Add a new node to the graph.
   1.564      ///
   1.565 -    /// Add a new node to the graph.
   1.566 +    /// This function adds a new node to the graph.
   1.567      /// \return The new node.
   1.568      Node addNode() { return Parent::addNode(); }
   1.569  
   1.570      /// \brief Add a new edge to the graph.
   1.571      ///
   1.572 -    /// Add a new edge to the graph with source node \c s
   1.573 -    /// and target node \c t.
   1.574 +    /// This function adds a new edge to the graph between nodes
   1.575 +    /// \c u and \c v with inherent orientation from node \c u to
   1.576 +    /// node \c v.
   1.577      /// \return The new edge.
   1.578 -    Edge addEdge(const Node& s, const Node& t) {
   1.579 -      return Parent::addEdge(s, t);
   1.580 +    Edge addEdge(Node u, Node v) {
   1.581 +      return Parent::addEdge(u, v);
   1.582      }
   1.583  
   1.584 -    /// \brief Erase a node from the graph.
   1.585 +    ///\brief Erase a node from the graph.
   1.586      ///
   1.587 -    /// Erase a node from the graph.
   1.588 +    /// This function erases the given node along with its incident arcs
   1.589 +    /// from the graph.
   1.590      ///
   1.591 -    void erase(const Node& n) { Parent::erase(n); }
   1.592 +    /// \note All iterators referencing the removed node or the incident
   1.593 +    /// edges are invalidated, of course.
   1.594 +    void erase(Node n) { Parent::erase(n); }
   1.595  
   1.596 -    /// \brief Erase an edge from the graph.
   1.597 +    ///\brief Erase an edge from the graph.
   1.598      ///
   1.599 -    /// Erase an edge from the graph.
   1.600 +    /// This function erases the given edge from the graph.
   1.601      ///
   1.602 -    void erase(const Edge& e) { Parent::erase(e); }
   1.603 +    /// \note All iterators referencing the removed edge are invalidated,
   1.604 +    /// of course.
   1.605 +    void erase(Edge e) { Parent::erase(e); }
   1.606      /// Node validity check
   1.607  
   1.608 -    /// This function gives back true if the given node is valid,
   1.609 -    /// ie. it is a real node of the graph.
   1.610 +    /// This function gives back \c true if the given node is valid,
   1.611 +    /// i.e. it is a real node of the graph.
   1.612      ///
   1.613 -    /// \warning A Node pointing to a removed item
   1.614 -    /// could become valid again later if new nodes are
   1.615 +    /// \warning A removed node could become valid again if new nodes are
   1.616      /// added to the graph.
   1.617      bool valid(Node n) const { return Parent::valid(n); }
   1.618 +    /// Edge validity check
   1.619 +
   1.620 +    /// This function gives back \c true if the given edge is valid,
   1.621 +    /// i.e. it is a real edge of the graph.
   1.622 +    ///
   1.623 +    /// \warning A removed edge could become valid again if new edges are
   1.624 +    /// added to the graph.
   1.625 +    bool valid(Edge e) const { return Parent::valid(e); }
   1.626      /// Arc validity check
   1.627  
   1.628 -    /// This function gives back true if the given arc is valid,
   1.629 -    /// ie. it is a real arc of the graph.
   1.630 +    /// This function gives back \c true if the given arc is valid,
   1.631 +    /// i.e. it is a real arc of the graph.
   1.632      ///
   1.633 -    /// \warning An Arc pointing to a removed item
   1.634 -    /// could become valid again later if new edges are
   1.635 +    /// \warning A removed arc could become valid again if new edges are
   1.636      /// added to the graph.
   1.637      bool valid(Arc a) const { return Parent::valid(a); }
   1.638 -    /// Edge validity check
   1.639  
   1.640 -    /// This function gives back true if the given edge is valid,
   1.641 -    /// ie. it is a real arc of the graph.
   1.642 +    /// \brief Change the first node of an edge.
   1.643      ///
   1.644 -    /// \warning A Edge pointing to a removed item
   1.645 -    /// could become valid again later if new edges are
   1.646 -    /// added to the graph.
   1.647 -    bool valid(Edge e) const { return Parent::valid(e); }
   1.648 -    /// \brief Change the end \c u of \c e to \c n
   1.649 +    /// This function changes the first node of the given edge \c e to \c n.
   1.650      ///
   1.651 -    /// This function changes the end \c u of \c e to node \c n.
   1.652 -    ///
   1.653 -    ///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the
   1.654 -    ///changed edge are invalidated and if the changed node is the
   1.655 -    ///base node of an iterator then this iterator is also
   1.656 -    ///invalidated.
   1.657 +    ///\note \c EdgeIt and \c ArcIt iterators referencing the
   1.658 +    ///changed edge are invalidated and all other iterators whose
   1.659 +    ///base node is the changed node are also invalidated.
   1.660      ///
   1.661      ///\warning This functionality cannot be used together with the
   1.662      ///Snapshot feature.
   1.663      void changeU(Edge e, Node n) {
   1.664        Parent::changeU(e,n);
   1.665      }
   1.666 -    /// \brief Change the end \c v of \c e to \c n
   1.667 +    /// \brief Change the second node of an edge.
   1.668      ///
   1.669 -    /// This function changes the end \c v of \c e to \c n.
   1.670 +    /// This function changes the second node of the given edge \c e to \c n.
   1.671      ///
   1.672 -    ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
   1.673 -    ///valid, however <tt>ArcIt</tt>s and if the changed node is the
   1.674 -    ///base node of an iterator then this iterator is invalidated.
   1.675 +    ///\note \c EdgeIt iterators referencing the changed edge remain
   1.676 +    ///valid, but \c ArcIt iterators referencing the changed edge and
   1.677 +    ///all other iterators whose base node is the changed node are also
   1.678 +    ///invalidated.
   1.679      ///
   1.680      ///\warning This functionality cannot be used together with the
   1.681      ///Snapshot feature.
   1.682      void changeV(Edge e, Node n) {
   1.683        Parent::changeV(e,n);
   1.684      }
   1.685 +
   1.686      /// \brief Contract two nodes.
   1.687      ///
   1.688 -    /// This function contracts two nodes.
   1.689 -    /// Node \p b will be removed but instead of deleting
   1.690 -    /// its neighboring arcs, they will be joined to \p a.
   1.691 -    /// The last parameter \p r controls whether to remove loops. \c true
   1.692 -    /// means that loops will be removed.
   1.693 +    /// This function contracts the given two nodes.
   1.694 +    /// Node \c b is removed, but instead of deleting
   1.695 +    /// its incident edges, they are joined to node \c a.
   1.696 +    /// If the last parameter \c r is \c true (this is the default value),
   1.697 +    /// then the newly created loops are removed.
   1.698      ///
   1.699 -    /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
   1.700 -    /// valid.
   1.701 +    /// \note The moved edges are joined to node \c a using changeU()
   1.702 +    /// or changeV(), thus all edge and arc iterators whose base node is
   1.703 +    /// \c b are invalidated.
   1.704 +    /// Moreover all iterators referencing node \c b or the removed
   1.705 +    /// loops are also invalidated. Other iterators remain valid.
   1.706      ///
   1.707      ///\warning This functionality cannot be used together with the
   1.708      ///Snapshot feature.
   1.709 @@ -1307,6 +1327,34 @@
   1.710        erase(b);
   1.711      }
   1.712  
   1.713 +    ///Clear the graph.
   1.714 +
   1.715 +    ///This function erases all nodes and arcs from the graph.
   1.716 +    ///
   1.717 +    ///\note All iterators of the graph are invalidated, of course.
   1.718 +    void clear() {
   1.719 +      Parent::clear();
   1.720 +    }
   1.721 +
   1.722 +    /// Reserve memory for nodes.
   1.723 +
   1.724 +    /// Using this function, it is possible to avoid superfluous memory
   1.725 +    /// allocation: if you know that the graph you want to build will
   1.726 +    /// be large (e.g. it will contain millions of nodes and/or edges),
   1.727 +    /// then it is worth reserving space for this amount before starting
   1.728 +    /// to build the graph.
   1.729 +    /// \sa reserveEdge()
   1.730 +    void reserveNode(int n) { nodes.reserve(n); };
   1.731 +
   1.732 +    /// Reserve memory for edges.
   1.733 +
   1.734 +    /// Using this function, it is possible to avoid superfluous memory
   1.735 +    /// allocation: if you know that the graph you want to build will
   1.736 +    /// be large (e.g. it will contain millions of nodes and/or edges),
   1.737 +    /// then it is worth reserving space for this amount before starting
   1.738 +    /// to build the graph.
   1.739 +    /// \sa reserveNode()
   1.740 +    void reserveEdge(int m) { arcs.reserve(2 * m); };
   1.741  
   1.742      /// \brief Class to make a snapshot of the graph and restore
   1.743      /// it later.
   1.744 @@ -1316,9 +1364,15 @@
   1.745      /// The newly added nodes and edges can be removed
   1.746      /// using the restore() function.
   1.747      ///
   1.748 -    /// \warning Edge and node deletions and other modifications
   1.749 -    /// (e.g. changing nodes of edges, contracting nodes) cannot be
   1.750 -    /// restored. These events invalidate the snapshot.
   1.751 +    /// \note After a state is restored, you cannot restore a later state,
   1.752 +    /// i.e. you cannot add the removed nodes and edges again using
   1.753 +    /// another Snapshot instance.
   1.754 +    ///
   1.755 +    /// \warning Node and edge deletions and other modifications
   1.756 +    /// (e.g. changing the end-nodes of edges or contracting nodes)
   1.757 +    /// cannot be restored. These events invalidate the snapshot.
   1.758 +    /// However, the edges and nodes that were added to the graph after
   1.759 +    /// making the current snapshot can be removed without invalidating it.
   1.760      class Snapshot {
   1.761      protected:
   1.762  
   1.763 @@ -1488,39 +1542,40 @@
   1.764        /// \brief Default constructor.
   1.765        ///
   1.766        /// Default constructor.
   1.767 -      /// To actually make a snapshot you must call save().
   1.768 +      /// You have to call save() to actually make a snapshot.
   1.769        Snapshot()
   1.770          : graph(0), node_observer_proxy(*this),
   1.771            edge_observer_proxy(*this) {}
   1.772  
   1.773        /// \brief Constructor that immediately makes a snapshot.
   1.774        ///
   1.775 -      /// This constructor immediately makes a snapshot of the graph.
   1.776 -      /// \param _graph The graph we make a snapshot of.
   1.777 -      Snapshot(ListGraph &_graph)
   1.778 +      /// This constructor immediately makes a snapshot of the given graph.
   1.779 +      Snapshot(ListGraph &gr)
   1.780          : node_observer_proxy(*this),
   1.781            edge_observer_proxy(*this) {
   1.782 -        attach(_graph);
   1.783 +        attach(gr);
   1.784        }
   1.785  
   1.786        /// \brief Make a snapshot.
   1.787        ///
   1.788 -      /// Make a snapshot of the graph.
   1.789 -      ///
   1.790 -      /// This function can be called more than once. In case of a repeated
   1.791 +      /// This function makes a snapshot of the given graph.
   1.792 +      /// It can be called more than once. In case of a repeated
   1.793        /// call, the previous snapshot gets lost.
   1.794 -      /// \param _graph The graph we make the snapshot of.
   1.795 -      void save(ListGraph &_graph) {
   1.796 +      void save(ListGraph &gr) {
   1.797          if (attached()) {
   1.798            detach();
   1.799            clear();
   1.800          }
   1.801 -        attach(_graph);
   1.802 +        attach(gr);
   1.803        }
   1.804  
   1.805        /// \brief Undo the changes until the last snapshot.
   1.806 -      //
   1.807 -      /// Undo the changes until the last snapshot created by save().
   1.808 +      ///
   1.809 +      /// This function undos the changes until the last snapshot
   1.810 +      /// created by save() or Snapshot(ListGraph&).
   1.811 +      ///
   1.812 +      /// \warning This method invalidates the snapshot, i.e. repeated
   1.813 +      /// restoring is not supported unless you call save() again.
   1.814        void restore() {
   1.815          detach();
   1.816          for(std::list<Edge>::iterator it = added_edges.begin();
   1.817 @@ -1534,9 +1589,9 @@
   1.818          clear();
   1.819        }
   1.820  
   1.821 -      /// \brief Gives back true when the snapshot is valid.
   1.822 +      /// \brief Returns \c true if the snapshot is valid.
   1.823        ///
   1.824 -      /// Gives back true when the snapshot is valid.
   1.825 +      /// This function returns \c true if the snapshot is valid.
   1.826        bool valid() const {
   1.827          return attached();
   1.828        }