lemon/smart_graph.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 14 Apr 2009 10:54:42 +0200
changeset 629 7a28e215f715
parent 606 c5fd2d996909
child 664 4137ef9aacc6
permissions -rw-r--r--
Remove notes about reference maps as extra features (#190)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2009
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     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_SMART_GRAPH_H
    20 #define LEMON_SMART_GRAPH_H
    21 
    22 ///\ingroup graphs
    23 ///\file
    24 ///\brief SmartDigraph and SmartGraph classes.
    25 
    26 #include <vector>
    27 
    28 #include <lemon/core.h>
    29 #include <lemon/error.h>
    30 #include <lemon/bits/graph_extender.h>
    31 
    32 namespace lemon {
    33 
    34   class SmartDigraph;
    35   ///Base of SmartDigraph
    36 
    37   ///Base of SmartDigraph
    38   ///
    39   class SmartDigraphBase {
    40   protected:
    41 
    42     struct NodeT
    43     {
    44       int first_in, first_out;
    45       NodeT() {}
    46     };
    47     struct ArcT
    48     {
    49       int target, source, next_in, next_out;
    50       ArcT() {}
    51     };
    52 
    53     std::vector<NodeT> nodes;
    54     std::vector<ArcT> arcs;
    55 
    56   public:
    57 
    58     typedef SmartDigraphBase Graph;
    59 
    60     class Node;
    61     class Arc;
    62 
    63   public:
    64 
    65     SmartDigraphBase() : nodes(), arcs() { }
    66     SmartDigraphBase(const SmartDigraphBase &_g)
    67       : nodes(_g.nodes), arcs(_g.arcs) { }
    68 
    69     typedef True NodeNumTag;
    70     typedef True ArcNumTag;
    71 
    72     int nodeNum() const { return nodes.size(); }
    73     int arcNum() const { return arcs.size(); }
    74 
    75     int maxNodeId() const { return nodes.size()-1; }
    76     int maxArcId() const { return arcs.size()-1; }
    77 
    78     Node addNode() {
    79       int n = nodes.size();
    80       nodes.push_back(NodeT());
    81       nodes[n].first_in = -1;
    82       nodes[n].first_out = -1;
    83       return Node(n);
    84     }
    85 
    86     Arc addArc(Node u, Node v) {
    87       int n = arcs.size();
    88       arcs.push_back(ArcT());
    89       arcs[n].source = u._id;
    90       arcs[n].target = v._id;
    91       arcs[n].next_out = nodes[u._id].first_out;
    92       arcs[n].next_in = nodes[v._id].first_in;
    93       nodes[u._id].first_out = nodes[v._id].first_in = n;
    94 
    95       return Arc(n);
    96     }
    97 
    98     void clear() {
    99       arcs.clear();
   100       nodes.clear();
   101     }
   102 
   103     Node source(Arc a) const { return Node(arcs[a._id].source); }
   104     Node target(Arc a) const { return Node(arcs[a._id].target); }
   105 
   106     static int id(Node v) { return v._id; }
   107     static int id(Arc a) { return a._id; }
   108 
   109     static Node nodeFromId(int id) { return Node(id);}
   110     static Arc arcFromId(int id) { return Arc(id);}
   111 
   112     bool valid(Node n) const {
   113       return n._id >= 0 && n._id < static_cast<int>(nodes.size());
   114     }
   115     bool valid(Arc a) const {
   116       return a._id >= 0 && a._id < static_cast<int>(arcs.size());
   117     }
   118 
   119     class Node {
   120       friend class SmartDigraphBase;
   121       friend class SmartDigraph;
   122 
   123     protected:
   124       int _id;
   125       explicit Node(int id) : _id(id) {}
   126     public:
   127       Node() {}
   128       Node (Invalid) : _id(-1) {}
   129       bool operator==(const Node i) const {return _id == i._id;}
   130       bool operator!=(const Node i) const {return _id != i._id;}
   131       bool operator<(const Node i) const {return _id < i._id;}
   132     };
   133 
   134 
   135     class Arc {
   136       friend class SmartDigraphBase;
   137       friend class SmartDigraph;
   138 
   139     protected:
   140       int _id;
   141       explicit Arc(int id) : _id(id) {}
   142     public:
   143       Arc() { }
   144       Arc (Invalid) : _id(-1) {}
   145       bool operator==(const Arc i) const {return _id == i._id;}
   146       bool operator!=(const Arc i) const {return _id != i._id;}
   147       bool operator<(const Arc i) const {return _id < i._id;}
   148     };
   149 
   150     void first(Node& node) const {
   151       node._id = nodes.size() - 1;
   152     }
   153 
   154     static void next(Node& node) {
   155       --node._id;
   156     }
   157 
   158     void first(Arc& arc) const {
   159       arc._id = arcs.size() - 1;
   160     }
   161 
   162     static void next(Arc& arc) {
   163       --arc._id;
   164     }
   165 
   166     void firstOut(Arc& arc, const Node& node) const {
   167       arc._id = nodes[node._id].first_out;
   168     }
   169 
   170     void nextOut(Arc& arc) const {
   171       arc._id = arcs[arc._id].next_out;
   172     }
   173 
   174     void firstIn(Arc& arc, const Node& node) const {
   175       arc._id = nodes[node._id].first_in;
   176     }
   177 
   178     void nextIn(Arc& arc) const {
   179       arc._id = arcs[arc._id].next_in;
   180     }
   181 
   182   };
   183 
   184   typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
   185 
   186   ///\ingroup graphs
   187   ///
   188   ///\brief A smart directed graph class.
   189   ///
   190   ///This is a simple and fast digraph implementation.
   191   ///It is also quite memory efficient, but at the price
   192   ///that <b> it does support only limited (only stack-like)
   193   ///node and arc deletions</b>.
   194   ///It fully conforms to the \ref concepts::Digraph "Digraph concept".
   195   ///
   196   ///\sa concepts::Digraph.
   197   class SmartDigraph : public ExtendedSmartDigraphBase {
   198   public:
   199 
   200     typedef ExtendedSmartDigraphBase Parent;
   201 
   202   private:
   203 
   204     ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
   205 
   206     ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
   207     ///
   208     SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
   209     ///\brief Assignment of SmartDigraph to another one is \e not allowed.
   210     ///Use DigraphCopy() instead.
   211 
   212     ///Assignment of SmartDigraph to another one is \e not allowed.
   213     ///Use DigraphCopy() instead.
   214     void operator=(const SmartDigraph &) {}
   215 
   216   public:
   217 
   218     /// Constructor
   219 
   220     /// Constructor.
   221     ///
   222     SmartDigraph() {};
   223 
   224     ///Add a new node to the digraph.
   225 
   226     /// Add a new node to the digraph.
   227     /// \return The new node.
   228     Node addNode() { return Parent::addNode(); }
   229 
   230     ///Add a new arc to the digraph.
   231 
   232     ///Add a new arc to the digraph with source node \c s
   233     ///and target node \c t.
   234     ///\return The new arc.
   235     Arc addArc(const Node& s, const Node& t) {
   236       return Parent::addArc(s, t);
   237     }
   238 
   239     /// \brief Using this it is possible to avoid the superfluous memory
   240     /// allocation.
   241 
   242     /// Using this it is possible to avoid the superfluous memory
   243     /// allocation: if you know that the digraph you want to build will
   244     /// be very large (e.g. it will contain millions of nodes and/or arcs)
   245     /// then it is worth reserving space for this amount before starting
   246     /// to build the digraph.
   247     /// \sa reserveArc
   248     void reserveNode(int n) { nodes.reserve(n); };
   249 
   250     /// \brief Using this it is possible to avoid the superfluous memory
   251     /// allocation.
   252 
   253     /// Using this it is possible to avoid the superfluous memory
   254     /// allocation: if you know that the digraph you want to build will
   255     /// be very large (e.g. it will contain millions of nodes and/or arcs)
   256     /// then it is worth reserving space for this amount before starting
   257     /// to build the digraph.
   258     /// \sa reserveNode
   259     void reserveArc(int m) { arcs.reserve(m); };
   260 
   261     /// \brief Node validity check
   262     ///
   263     /// This function gives back true if the given node is valid,
   264     /// ie. it is a real node of the graph.
   265     ///
   266     /// \warning A removed node (using Snapshot) could become valid again
   267     /// when new nodes are added to the graph.
   268     bool valid(Node n) const { return Parent::valid(n); }
   269 
   270     /// \brief Arc validity check
   271     ///
   272     /// This function gives back true if the given arc is valid,
   273     /// ie. it is a real arc of the graph.
   274     ///
   275     /// \warning A removed arc (using Snapshot) could become valid again
   276     /// when new arcs are added to the graph.
   277     bool valid(Arc a) const { return Parent::valid(a); }
   278 
   279     ///Clear the digraph.
   280 
   281     ///Erase all the nodes and arcs from the digraph.
   282     ///
   283     void clear() {
   284       Parent::clear();
   285     }
   286 
   287     ///Split a node.
   288 
   289     ///This function splits a node. First a new node is added to the digraph,
   290     ///then the source of each outgoing arc of \c n is moved to this new node.
   291     ///If \c connect is \c true (this is the default value), then a new arc
   292     ///from \c n to the newly created node is also added.
   293     ///\return The newly created node.
   294     ///
   295     ///\note The <tt>Arc</tt>s
   296     ///referencing a moved arc remain
   297     ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
   298     ///may be invalidated.
   299     ///\warning This functionality cannot be used together with the Snapshot
   300     ///feature.
   301     Node split(Node n, bool connect = true)
   302     {
   303       Node b = addNode();
   304       nodes[b._id].first_out=nodes[n._id].first_out;
   305       nodes[n._id].first_out=-1;
   306       for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
   307         arcs[i].source=b._id;
   308       }
   309       if(connect) addArc(n,b);
   310       return b;
   311     }
   312 
   313   public:
   314 
   315     class Snapshot;
   316 
   317   protected:
   318 
   319     void restoreSnapshot(const Snapshot &s)
   320     {
   321       while(s.arc_num<arcs.size()) {
   322         Arc arc = arcFromId(arcs.size()-1);
   323         Parent::notifier(Arc()).erase(arc);
   324         nodes[arcs.back().source].first_out=arcs.back().next_out;
   325         nodes[arcs.back().target].first_in=arcs.back().next_in;
   326         arcs.pop_back();
   327       }
   328       while(s.node_num<nodes.size()) {
   329         Node node = nodeFromId(nodes.size()-1);
   330         Parent::notifier(Node()).erase(node);
   331         nodes.pop_back();
   332       }
   333     }
   334 
   335   public:
   336 
   337     ///Class to make a snapshot of the digraph and to restrore to it later.
   338 
   339     ///Class to make a snapshot of the digraph and to restrore to it later.
   340     ///
   341     ///The newly added nodes and arcs can be removed using the
   342     ///restore() function.
   343     ///\note After you restore a state, you cannot restore
   344     ///a later state, in other word you cannot add again the arcs deleted
   345     ///by restore() using another one Snapshot instance.
   346     ///
   347     ///\warning If you do not use correctly the snapshot that can cause
   348     ///either broken program, invalid state of the digraph, valid but
   349     ///not the restored digraph or no change. Because the runtime performance
   350     ///the validity of the snapshot is not stored.
   351     class Snapshot
   352     {
   353       SmartDigraph *_graph;
   354     protected:
   355       friend class SmartDigraph;
   356       unsigned int node_num;
   357       unsigned int arc_num;
   358     public:
   359       ///Default constructor.
   360 
   361       ///Default constructor.
   362       ///To actually make a snapshot you must call save().
   363       ///
   364       Snapshot() : _graph(0) {}
   365       ///Constructor that immediately makes a snapshot
   366 
   367       ///This constructor immediately makes a snapshot of the digraph.
   368       ///\param graph The digraph we make a snapshot of.
   369       Snapshot(SmartDigraph &graph) : _graph(&graph) {
   370         node_num=_graph->nodes.size();
   371         arc_num=_graph->arcs.size();
   372       }
   373 
   374       ///Make a snapshot.
   375 
   376       ///Make a snapshot of the digraph.
   377       ///
   378       ///This function can be called more than once. In case of a repeated
   379       ///call, the previous snapshot gets lost.
   380       ///\param graph The digraph we make the snapshot of.
   381       void save(SmartDigraph &graph)
   382       {
   383         _graph=&graph;
   384         node_num=_graph->nodes.size();
   385         arc_num=_graph->arcs.size();
   386       }
   387 
   388       ///Undo the changes until a snapshot.
   389 
   390       ///Undo the changes until a snapshot created by save().
   391       ///
   392       ///\note After you restored a state, you cannot restore
   393       ///a later state, in other word you cannot add again the arcs deleted
   394       ///by restore().
   395       void restore()
   396       {
   397         _graph->restoreSnapshot(*this);
   398       }
   399     };
   400   };
   401 
   402 
   403   class SmartGraphBase {
   404 
   405   protected:
   406 
   407     struct NodeT {
   408       int first_out;
   409     };
   410 
   411     struct ArcT {
   412       int target;
   413       int next_out;
   414     };
   415 
   416     std::vector<NodeT> nodes;
   417     std::vector<ArcT> arcs;
   418 
   419     int first_free_arc;
   420 
   421   public:
   422 
   423     typedef SmartGraphBase Digraph;
   424 
   425     class Node;
   426     class Arc;
   427     class Edge;
   428 
   429     class Node {
   430       friend class SmartGraphBase;
   431     protected:
   432 
   433       int _id;
   434       explicit Node(int id) { _id = id;}
   435 
   436     public:
   437       Node() {}
   438       Node (Invalid) { _id = -1; }
   439       bool operator==(const Node& node) const {return _id == node._id;}
   440       bool operator!=(const Node& node) const {return _id != node._id;}
   441       bool operator<(const Node& node) const {return _id < node._id;}
   442     };
   443 
   444     class Edge {
   445       friend class SmartGraphBase;
   446     protected:
   447 
   448       int _id;
   449       explicit Edge(int id) { _id = id;}
   450 
   451     public:
   452       Edge() {}
   453       Edge (Invalid) { _id = -1; }
   454       bool operator==(const Edge& arc) const {return _id == arc._id;}
   455       bool operator!=(const Edge& arc) const {return _id != arc._id;}
   456       bool operator<(const Edge& arc) const {return _id < arc._id;}
   457     };
   458 
   459     class Arc {
   460       friend class SmartGraphBase;
   461     protected:
   462 
   463       int _id;
   464       explicit Arc(int id) { _id = id;}
   465 
   466     public:
   467       operator Edge() const {
   468         return _id != -1 ? edgeFromId(_id / 2) : INVALID;
   469       }
   470 
   471       Arc() {}
   472       Arc (Invalid) { _id = -1; }
   473       bool operator==(const Arc& arc) const {return _id == arc._id;}
   474       bool operator!=(const Arc& arc) const {return _id != arc._id;}
   475       bool operator<(const Arc& arc) const {return _id < arc._id;}
   476     };
   477 
   478 
   479 
   480     SmartGraphBase()
   481       : nodes(), arcs() {}
   482 
   483     typedef True NodeNumTag;
   484     typedef True EdgeNumTag;
   485     typedef True ArcNumTag;
   486 
   487     int nodeNum() const { return nodes.size(); }
   488     int edgeNum() const { return arcs.size() / 2; }
   489     int arcNum() const { return arcs.size(); }
   490 
   491     int maxNodeId() const { return nodes.size()-1; }
   492     int maxEdgeId() const { return arcs.size() / 2 - 1; }
   493     int maxArcId() const { return arcs.size()-1; }
   494 
   495     Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
   496     Node target(Arc e) const { return Node(arcs[e._id].target); }
   497 
   498     Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
   499     Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
   500 
   501     static bool direction(Arc e) {
   502       return (e._id & 1) == 1;
   503     }
   504 
   505     static Arc direct(Edge e, bool d) {
   506       return Arc(e._id * 2 + (d ? 1 : 0));
   507     }
   508 
   509     void first(Node& node) const {
   510       node._id = nodes.size() - 1;
   511     }
   512 
   513     void next(Node& node) const {
   514       --node._id;
   515     }
   516 
   517     void first(Arc& arc) const {
   518       arc._id = arcs.size() - 1;
   519     }
   520 
   521     void next(Arc& arc) const {
   522       --arc._id;
   523     }
   524 
   525     void first(Edge& arc) const {
   526       arc._id = arcs.size() / 2 - 1;
   527     }
   528 
   529     void next(Edge& arc) const {
   530       --arc._id;
   531     }
   532 
   533     void firstOut(Arc &arc, const Node& v) const {
   534       arc._id = nodes[v._id].first_out;
   535     }
   536     void nextOut(Arc &arc) const {
   537       arc._id = arcs[arc._id].next_out;
   538     }
   539 
   540     void firstIn(Arc &arc, const Node& v) const {
   541       arc._id = ((nodes[v._id].first_out) ^ 1);
   542       if (arc._id == -2) arc._id = -1;
   543     }
   544     void nextIn(Arc &arc) const {
   545       arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
   546       if (arc._id == -2) arc._id = -1;
   547     }
   548 
   549     void firstInc(Edge &arc, bool& d, const Node& v) const {
   550       int de = nodes[v._id].first_out;
   551       if (de != -1) {
   552         arc._id = de / 2;
   553         d = ((de & 1) == 1);
   554       } else {
   555         arc._id = -1;
   556         d = true;
   557       }
   558     }
   559     void nextInc(Edge &arc, bool& d) const {
   560       int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
   561       if (de != -1) {
   562         arc._id = de / 2;
   563         d = ((de & 1) == 1);
   564       } else {
   565         arc._id = -1;
   566         d = true;
   567       }
   568     }
   569 
   570     static int id(Node v) { return v._id; }
   571     static int id(Arc e) { return e._id; }
   572     static int id(Edge e) { return e._id; }
   573 
   574     static Node nodeFromId(int id) { return Node(id);}
   575     static Arc arcFromId(int id) { return Arc(id);}
   576     static Edge edgeFromId(int id) { return Edge(id);}
   577 
   578     bool valid(Node n) const {
   579       return n._id >= 0 && n._id < static_cast<int>(nodes.size());
   580     }
   581     bool valid(Arc a) const {
   582       return a._id >= 0 && a._id < static_cast<int>(arcs.size());
   583     }
   584     bool valid(Edge e) const {
   585       return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
   586     }
   587 
   588     Node addNode() {
   589       int n = nodes.size();
   590       nodes.push_back(NodeT());
   591       nodes[n].first_out = -1;
   592 
   593       return Node(n);
   594     }
   595 
   596     Edge addEdge(Node u, Node v) {
   597       int n = arcs.size();
   598       arcs.push_back(ArcT());
   599       arcs.push_back(ArcT());
   600 
   601       arcs[n].target = u._id;
   602       arcs[n | 1].target = v._id;
   603 
   604       arcs[n].next_out = nodes[v._id].first_out;
   605       nodes[v._id].first_out = n;
   606 
   607       arcs[n | 1].next_out = nodes[u._id].first_out;
   608       nodes[u._id].first_out = (n | 1);
   609 
   610       return Edge(n / 2);
   611     }
   612 
   613     void clear() {
   614       arcs.clear();
   615       nodes.clear();
   616     }
   617 
   618   };
   619 
   620   typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
   621 
   622   /// \ingroup graphs
   623   ///
   624   /// \brief A smart undirected graph class.
   625   ///
   626   /// This is a simple and fast graph implementation.
   627   /// It is also quite memory efficient, but at the price
   628   /// that <b> it does support only limited (only stack-like)
   629   /// node and arc deletions</b>.
   630   /// It fully conforms to the \ref concepts::Graph "Graph concept".
   631   ///
   632   /// \sa concepts::Graph.
   633   class SmartGraph : public ExtendedSmartGraphBase {
   634   private:
   635 
   636     ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
   637 
   638     ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
   639     ///
   640     SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
   641 
   642     ///\brief Assignment of SmartGraph to another one is \e not allowed.
   643     ///Use GraphCopy() instead.
   644 
   645     ///Assignment of SmartGraph to another one is \e not allowed.
   646     ///Use GraphCopy() instead.
   647     void operator=(const SmartGraph &) {}
   648 
   649   public:
   650 
   651     typedef ExtendedSmartGraphBase Parent;
   652 
   653     /// Constructor
   654 
   655     /// Constructor.
   656     ///
   657     SmartGraph() {}
   658 
   659     ///Add a new node to the graph.
   660 
   661     /// Add a new node to the graph.
   662     /// \return The new node.
   663     Node addNode() { return Parent::addNode(); }
   664 
   665     ///Add a new edge to the graph.
   666 
   667     ///Add a new edge to the graph with node \c s
   668     ///and \c t.
   669     ///\return The new edge.
   670     Edge addEdge(const Node& s, const Node& t) {
   671       return Parent::addEdge(s, t);
   672     }
   673 
   674     /// \brief Node validity check
   675     ///
   676     /// This function gives back true if the given node is valid,
   677     /// ie. it is a real node of the graph.
   678     ///
   679     /// \warning A removed node (using Snapshot) could become valid again
   680     /// when new nodes are added to the graph.
   681     bool valid(Node n) const { return Parent::valid(n); }
   682 
   683     /// \brief Arc validity check
   684     ///
   685     /// This function gives back true if the given arc is valid,
   686     /// ie. it is a real arc of the graph.
   687     ///
   688     /// \warning A removed arc (using Snapshot) could become valid again
   689     /// when new edges are added to the graph.
   690     bool valid(Arc a) const { return Parent::valid(a); }
   691 
   692     /// \brief Edge validity check
   693     ///
   694     /// This function gives back true if the given edge is valid,
   695     /// ie. it is a real edge of the graph.
   696     ///
   697     /// \warning A removed edge (using Snapshot) could become valid again
   698     /// when new edges are added to the graph.
   699     bool valid(Edge e) const { return Parent::valid(e); }
   700 
   701     ///Clear the graph.
   702 
   703     ///Erase all the nodes and edges from the graph.
   704     ///
   705     void clear() {
   706       Parent::clear();
   707     }
   708 
   709   public:
   710 
   711     class Snapshot;
   712 
   713   protected:
   714 
   715     void saveSnapshot(Snapshot &s)
   716     {
   717       s._graph = this;
   718       s.node_num = nodes.size();
   719       s.arc_num = arcs.size();
   720     }
   721 
   722     void restoreSnapshot(const Snapshot &s)
   723     {
   724       while(s.arc_num<arcs.size()) {
   725         int n=arcs.size()-1;
   726         Edge arc=edgeFromId(n/2);
   727         Parent::notifier(Edge()).erase(arc);
   728         std::vector<Arc> dir;
   729         dir.push_back(arcFromId(n));
   730         dir.push_back(arcFromId(n-1));
   731         Parent::notifier(Arc()).erase(dir);
   732         nodes[arcs[n-1].target].first_out=arcs[n].next_out;
   733         nodes[arcs[n].target].first_out=arcs[n-1].next_out;
   734         arcs.pop_back();
   735         arcs.pop_back();
   736       }
   737       while(s.node_num<nodes.size()) {
   738         int n=nodes.size()-1;
   739         Node node = nodeFromId(n);
   740         Parent::notifier(Node()).erase(node);
   741         nodes.pop_back();
   742       }
   743     }
   744 
   745   public:
   746 
   747     ///Class to make a snapshot of the digraph and to restrore to it later.
   748 
   749     ///Class to make a snapshot of the digraph and to restrore to it later.
   750     ///
   751     ///The newly added nodes and arcs can be removed using the
   752     ///restore() function.
   753     ///
   754     ///\note After you restore a state, you cannot restore
   755     ///a later state, in other word you cannot add again the arcs deleted
   756     ///by restore() using another one Snapshot instance.
   757     ///
   758     ///\warning If you do not use correctly the snapshot that can cause
   759     ///either broken program, invalid state of the digraph, valid but
   760     ///not the restored digraph or no change. Because the runtime performance
   761     ///the validity of the snapshot is not stored.
   762     class Snapshot
   763     {
   764       SmartGraph *_graph;
   765     protected:
   766       friend class SmartGraph;
   767       unsigned int node_num;
   768       unsigned int arc_num;
   769     public:
   770       ///Default constructor.
   771 
   772       ///Default constructor.
   773       ///To actually make a snapshot you must call save().
   774       ///
   775       Snapshot() : _graph(0) {}
   776       ///Constructor that immediately makes a snapshot
   777 
   778       ///This constructor immediately makes a snapshot of the digraph.
   779       ///\param graph The digraph we make a snapshot of.
   780       Snapshot(SmartGraph &graph) {
   781         graph.saveSnapshot(*this);
   782       }
   783 
   784       ///Make a snapshot.
   785 
   786       ///Make a snapshot of the graph.
   787       ///
   788       ///This function can be called more than once. In case of a repeated
   789       ///call, the previous snapshot gets lost.
   790       ///\param graph The digraph we make the snapshot of.
   791       void save(SmartGraph &graph)
   792       {
   793         graph.saveSnapshot(*this);
   794       }
   795 
   796       ///Undo the changes until a snapshot.
   797 
   798       ///Undo the changes until a snapshot created by save().
   799       ///
   800       ///\note After you restored a state, you cannot restore
   801       ///a later state, in other word you cannot add again the arcs deleted
   802       ///by restore().
   803       void restore()
   804       {
   805         _graph->restoreSnapshot(*this);
   806       }
   807     };
   808   };
   809 
   810 } //namespace lemon
   811 
   812 
   813 #endif //LEMON_SMART_GRAPH_H