lemon/list_graph.h
changeset 735 853fcddcf282
parent 617 4137ef9aacc6
child 736 2e20aad15754
equal deleted inserted replaced
19:712c74846880 20:242c20895a03
    19 #ifndef LEMON_LIST_GRAPH_H
    19 #ifndef LEMON_LIST_GRAPH_H
    20 #define LEMON_LIST_GRAPH_H
    20 #define LEMON_LIST_GRAPH_H
    21 
    21 
    22 ///\ingroup graphs
    22 ///\ingroup graphs
    23 ///\file
    23 ///\file
    24 ///\brief ListDigraph, ListGraph classes.
    24 ///\brief ListDigraph and ListGraph classes.
    25 
    25 
    26 #include <lemon/core.h>
    26 #include <lemon/core.h>
    27 #include <lemon/error.h>
    27 #include <lemon/error.h>
    28 #include <lemon/bits/graph_extender.h>
    28 #include <lemon/bits/graph_extender.h>
    29 
    29 
   309   /// \addtogroup graphs
   309   /// \addtogroup graphs
   310   /// @{
   310   /// @{
   311 
   311 
   312   ///A general directed graph structure.
   312   ///A general directed graph structure.
   313 
   313 
   314   ///\ref ListDigraph is a simple and fast <em>directed graph</em>
   314   ///\ref ListDigraph is a versatile and fast directed graph
   315   ///implementation based on static linked lists that are stored in
   315   ///implementation based on linked lists that are stored in
   316   ///\c std::vector structures.
   316   ///\c std::vector structures.
   317   ///
   317   ///
   318   ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
   318   ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
   319   ///also provides several useful additional functionalities.
   319   ///and it also provides several useful additional functionalities.
   320   ///Most of the member functions and nested classes are documented
   320   ///Most of its member functions and nested classes are documented
   321   ///only in the concept class.
   321   ///only in the concept class.
   322   ///
   322   ///
   323   ///\sa concepts::Digraph
   323   ///\sa concepts::Digraph
   324 
   324   ///\sa ListGraph
   325   class ListDigraph : public ExtendedListDigraphBase {
   325   class ListDigraph : public ExtendedListDigraphBase {
   326     typedef ExtendedListDigraphBase Parent;
   326     typedef ExtendedListDigraphBase Parent;
   327 
   327 
   328   private:
   328   private:
   329     ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
   329     /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
   330 
       
   331     ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
       
   332     ///
       
   333     ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
   330     ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
   334     ///\brief Assignment of ListDigraph to another one is \e not allowed.
   331     /// \brief Assignment of a digraph to another one is \e not allowed.
   335     ///Use copyDigraph() instead.
   332     /// Use DigraphCopy instead.
   336 
       
   337     ///Assignment of ListDigraph to another one is \e not allowed.
       
   338     ///Use copyDigraph() instead.
       
   339     void operator=(const ListDigraph &) {}
   333     void operator=(const ListDigraph &) {}
   340   public:
   334   public:
   341 
   335 
   342     /// Constructor
   336     /// Constructor
   343 
   337 
   345     ///
   339     ///
   346     ListDigraph() {}
   340     ListDigraph() {}
   347 
   341 
   348     ///Add a new node to the digraph.
   342     ///Add a new node to the digraph.
   349 
   343 
   350     ///Add a new node to the digraph.
   344     ///This function adds a new node to the digraph.
   351     ///\return The new node.
   345     ///\return The new node.
   352     Node addNode() { return Parent::addNode(); }
   346     Node addNode() { return Parent::addNode(); }
   353 
   347 
   354     ///Add a new arc to the digraph.
   348     ///Add a new arc to the digraph.
   355 
   349 
   356     ///Add a new arc to the digraph with source node \c s
   350     ///This function adds a new arc to the digraph with source node \c s
   357     ///and target node \c t.
   351     ///and target node \c t.
   358     ///\return The new arc.
   352     ///\return The new arc.
   359     Arc addArc(const Node& s, const Node& t) {
   353     Arc addArc(Node s, Node t) {
   360       return Parent::addArc(s, t);
   354       return Parent::addArc(s, t);
   361     }
   355     }
   362 
   356 
   363     ///\brief Erase a node from the digraph.
   357     ///\brief Erase a node from the digraph.
   364     ///
   358     ///
   365     ///Erase a node from the digraph.
   359     ///This function erases the given node from the digraph.
   366     ///
   360     void erase(Node n) { Parent::erase(n); }
   367     void erase(const Node& n) { Parent::erase(n); }
       
   368 
   361 
   369     ///\brief Erase an arc from the digraph.
   362     ///\brief Erase an arc from the digraph.
   370     ///
   363     ///
   371     ///Erase an arc from the digraph.
   364     ///This function erases the given arc from the digraph.
   372     ///
   365     void erase(Arc a) { Parent::erase(a); }
   373     void erase(const Arc& a) { Parent::erase(a); }
       
   374 
   366 
   375     /// Node validity check
   367     /// Node validity check
   376 
   368 
   377     /// This function gives back true if the given node is valid,
   369     /// This function gives back \c true if the given node is valid,
   378     /// ie. it is a real node of the graph.
   370     /// i.e. it is a real node of the digraph.
   379     ///
   371     ///
   380     /// \warning A Node pointing to a removed item
   372     /// \warning A removed node could become valid again if new nodes are
   381     /// could become valid again later if new nodes are
   373     /// added to the digraph.
   382     /// added to the graph.
       
   383     bool valid(Node n) const { return Parent::valid(n); }
   374     bool valid(Node n) const { return Parent::valid(n); }
   384 
   375 
   385     /// Arc validity check
   376     /// Arc validity check
   386 
   377 
   387     /// This function gives back true if the given arc is valid,
   378     /// This function gives back \c true if the given arc is valid,
   388     /// ie. it is a real arc of the graph.
   379     /// i.e. it is a real arc of the digraph.
   389     ///
   380     ///
   390     /// \warning An Arc pointing to a removed item
   381     /// \warning A removed arc could become valid again if new arcs are
   391     /// could become valid again later if new nodes are
   382     /// added to the digraph.
   392     /// added to the graph.
       
   393     bool valid(Arc a) const { return Parent::valid(a); }
   383     bool valid(Arc a) const { return Parent::valid(a); }
   394 
   384 
   395     /// Change the target of \c a to \c n
   385     /// Change the target node of an arc
   396 
   386 
   397     /// Change the target of \c a to \c n
   387     /// This function changes the target node of the given arc \c a to \c n.
   398     ///
   388     ///
   399     ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
   389     ///\note \c ArcIt and \c OutArcIt iterators referencing the changed
   400     ///the changed arc remain valid. However <tt>InArcIt</tt>s are
   390     ///arc remain valid, however \c InArcIt iterators are invalidated.
   401     ///invalidated.
       
   402     ///
   391     ///
   403     ///\warning This functionality cannot be used together with the Snapshot
   392     ///\warning This functionality cannot be used together with the Snapshot
   404     ///feature.
   393     ///feature.
   405     void changeTarget(Arc a, Node n) {
   394     void changeTarget(Arc a, Node n) {
   406       Parent::changeTarget(a,n);
   395       Parent::changeTarget(a,n);
   407     }
   396     }
   408     /// Change the source of \c a to \c n
   397     /// Change the source node of an arc
   409 
   398 
   410     /// Change the source of \c a to \c n
   399     /// This function changes the source node of the given arc \c a to \c n.
   411     ///
   400     ///
   412     ///\note The <tt>InArcIt</tt>s referencing the changed arc remain
   401     ///\note \c InArcIt iterators referencing the changed arc remain
   413     ///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are
   402     ///valid, however \c ArcIt and \c OutArcIt iterators are invalidated.
   414     ///invalidated.
       
   415     ///
   403     ///
   416     ///\warning This functionality cannot be used together with the Snapshot
   404     ///\warning This functionality cannot be used together with the Snapshot
   417     ///feature.
   405     ///feature.
   418     void changeSource(Arc a, Node n) {
   406     void changeSource(Arc a, Node n) {
   419       Parent::changeSource(a,n);
   407       Parent::changeSource(a,n);
   420     }
   408     }
   421 
   409 
   422     /// Invert the direction of an arc.
   410     /// Reverse the direction of an arc.
   423 
   411 
   424     ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
   412     /// This function reverses the direction of the given arc.
   425     ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
   413     ///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing
   426     ///invalidated.
   414     ///the changed arc are invalidated.
   427     ///
   415     ///
   428     ///\warning This functionality cannot be used together with the Snapshot
   416     ///\warning This functionality cannot be used together with the Snapshot
   429     ///feature.
   417     ///feature.
   430     void reverseArc(Arc e) {
   418     void reverseArc(Arc a) {
   431       Node t=target(e);
   419       Node t=target(a);
   432       changeTarget(e,source(e));
   420       changeTarget(a,source(a));
   433       changeSource(e,t);
   421       changeSource(a,t);
   434     }
   422     }
   435 
       
   436     /// Reserve memory for nodes.
       
   437 
       
   438     /// Using this function it is possible to avoid the superfluous memory
       
   439     /// allocation: if you know that the digraph you want to build will
       
   440     /// be very large (e.g. it will contain millions of nodes and/or arcs)
       
   441     /// then it is worth reserving space for this amount before starting
       
   442     /// to build the digraph.
       
   443     /// \sa reserveArc
       
   444     void reserveNode(int n) { nodes.reserve(n); };
       
   445 
       
   446     /// Reserve memory for arcs.
       
   447 
       
   448     /// Using this function it is possible to avoid the superfluous memory
       
   449     /// allocation: if you know that the digraph you want to build will
       
   450     /// be very large (e.g. it will contain millions of nodes and/or arcs)
       
   451     /// then it is worth reserving space for this amount before starting
       
   452     /// to build the digraph.
       
   453     /// \sa reserveNode
       
   454     void reserveArc(int m) { arcs.reserve(m); };
       
   455 
   423 
   456     ///Contract two nodes.
   424     ///Contract two nodes.
   457 
   425 
   458     ///This function contracts two nodes.
   426     ///This function contracts the given two nodes.
   459     ///Node \p b will be removed but instead of deleting
   427     ///Node \c v is removed, but instead of deleting its
   460     ///incident arcs, they will be joined to \p a.
   428     ///incident arcs, they are joined to node \c u.
   461     ///The last parameter \p r controls whether to remove loops. \c true
   429     ///If the last parameter \c r is \c true (this is the default value),
   462     ///means that loops will be removed.
   430     ///then the newly created loops are removed.
   463     ///
   431     ///
   464     ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
   432     ///\note The moved arcs are joined to node \c u using changeSource()
   465     ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
   433     ///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are
   466     ///may be invalidated.
   434     ///invalidated for the outgoing arcs of node \c v and \c InArcIt
       
   435     ///iterators are invalidated for the incomming arcs of \c v.
       
   436     ///Moreover all iterators referencing node \c v or the removed 
       
   437     ///loops are also invalidated. Other iterators remain valid.
   467     ///
   438     ///
   468     ///\warning This functionality cannot be used together with the Snapshot
   439     ///\warning This functionality cannot be used together with the Snapshot
   469     ///feature.
   440     ///feature.
   470     void contract(Node a, Node b, bool r = true)
   441     void contract(Node u, Node v, bool r = true)
   471     {
   442     {
   472       for(OutArcIt e(*this,b);e!=INVALID;) {
   443       for(OutArcIt e(*this,v);e!=INVALID;) {
   473         OutArcIt f=e;
   444         OutArcIt f=e;
   474         ++f;
   445         ++f;
   475         if(r && target(e)==a) erase(e);
   446         if(r && target(e)==u) erase(e);
   476         else changeSource(e,a);
   447         else changeSource(e,u);
   477         e=f;
   448         e=f;
   478       }
   449       }
   479       for(InArcIt e(*this,b);e!=INVALID;) {
   450       for(InArcIt e(*this,v);e!=INVALID;) {
   480         InArcIt f=e;
   451         InArcIt f=e;
   481         ++f;
   452         ++f;
   482         if(r && source(e)==a) erase(e);
   453         if(r && source(e)==u) erase(e);
   483         else changeTarget(e,a);
   454         else changeTarget(e,u);
   484         e=f;
   455         e=f;
   485       }
   456       }
   486       erase(b);
   457       erase(v);
   487     }
   458     }
   488 
   459 
   489     ///Split a node.
   460     ///Split a node.
   490 
   461 
   491     ///This function splits a node. First a new node is added to the digraph,
   462     ///This function splits the given node. First, a new node is added
   492     ///then the source of each outgoing arc of \c n is moved to this new node.
   463     ///to the digraph, then the source of each outgoing arc of node \c n
   493     ///If \c connect is \c true (this is the default value), then a new arc
   464     ///is moved to this new node.
   494     ///from \c n to the newly created node is also added.
   465     ///If the second parameter \c connect is \c true (this is the default
       
   466     ///value), then a new arc from node \c n to the newly created node
       
   467     ///is also added.
   495     ///\return The newly created node.
   468     ///\return The newly created node.
   496     ///
   469     ///
   497     ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
   470     ///\note \c ArcIt and \c OutArcIt iterators referencing the outgoing
   498     ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
   471     ///arcs of node \c n are invalidated. Other iterators remain valid.
   499     ///be invalidated.
   472     ///
   500     ///
   473     ///\warning This functionality cannot be used together with the
   501     ///\warning This functionality cannot be used in conjunction with the
       
   502     ///Snapshot feature.
   474     ///Snapshot feature.
   503     Node split(Node n, bool connect = true) {
   475     Node split(Node n, bool connect = true) {
   504       Node b = addNode();
   476       Node b = addNode();
   505       for(OutArcIt e(*this,n);e!=INVALID;) {
   477       for(OutArcIt e(*this,n);e!=INVALID;) {
   506         OutArcIt f=e;
   478         OutArcIt f=e;
   512       return b;
   484       return b;
   513     }
   485     }
   514 
   486 
   515     ///Split an arc.
   487     ///Split an arc.
   516 
   488 
   517     ///This function splits an arc. First a new node \c b is added to
   489     ///This function splits the given arc. First, a new node \c v is
   518     ///the digraph, then the original arc is re-targeted to \c
   490     ///added to the digraph, then the target node of the original arc
   519     ///b. Finally an arc from \c b to the original target is added.
   491     ///is set to \c v. Finally, an arc from \c v to the original target
   520     ///
   492     ///is added.
   521     ///\return The newly created node.
   493     ///\return The newly created node.
       
   494     ///
       
   495     ///\note \c InArcIt iterators referencing the original arc are
       
   496     ///invalidated. Other iterators remain valid.
   522     ///
   497     ///
   523     ///\warning This functionality cannot be used together with the
   498     ///\warning This functionality cannot be used together with the
   524     ///Snapshot feature.
   499     ///Snapshot feature.
   525     Node split(Arc e) {
   500     Node split(Arc a) {
   526       Node b = addNode();
   501       Node v = addNode();
   527       addArc(b,target(e));
   502       addArc(v,target(a));
   528       changeTarget(e,b);
   503       changeTarget(a,v);
   529       return b;
   504       return v;
   530     }
   505     }
       
   506 
       
   507     ///Clear the digraph.
       
   508 
       
   509     ///This function erases all nodes and arcs from the digraph.
       
   510     ///
       
   511     void clear() {
       
   512       Parent::clear();
       
   513     }
       
   514 
       
   515     /// Reserve memory for nodes.
       
   516 
       
   517     /// Using this function, it is possible to avoid superfluous memory
       
   518     /// allocation: if you know that the digraph you want to build will
       
   519     /// be large (e.g. it will contain millions of nodes and/or arcs),
       
   520     /// then it is worth reserving space for this amount before starting
       
   521     /// to build the digraph.
       
   522     /// \sa reserveArc()
       
   523     void reserveNode(int n) { nodes.reserve(n); };
       
   524 
       
   525     /// Reserve memory for arcs.
       
   526 
       
   527     /// Using this function, it is possible to avoid superfluous memory
       
   528     /// allocation: if you know that the digraph you want to build will
       
   529     /// be large (e.g. it will contain millions of nodes and/or arcs),
       
   530     /// then it is worth reserving space for this amount before starting
       
   531     /// to build the digraph.
       
   532     /// \sa reserveNode()
       
   533     void reserveArc(int m) { arcs.reserve(m); };
   531 
   534 
   532     /// \brief Class to make a snapshot of the digraph and restore
   535     /// \brief Class to make a snapshot of the digraph and restore
   533     /// it later.
   536     /// it later.
   534     ///
   537     ///
   535     /// Class to make a snapshot of the digraph and restore it later.
   538     /// Class to make a snapshot of the digraph and restore it later.
   536     ///
   539     ///
   537     /// The newly added nodes and arcs can be removed using the
   540     /// The newly added nodes and arcs can be removed using the
   538     /// restore() function.
   541     /// restore() function.
   539     ///
   542     ///
   540     /// \warning Arc and node deletions and other modifications (e.g.
   543     /// \note After a state is restored, you cannot restore a later state, 
   541     /// contracting, splitting, reversing arcs or nodes) cannot be
   544     /// i.e. you cannot add the removed nodes and arcs again using
       
   545     /// another Snapshot instance.
       
   546     ///
       
   547     /// \warning Node and arc deletions and other modifications (e.g.
       
   548     /// reversing, contracting, splitting arcs or nodes) cannot be
   542     /// restored. These events invalidate the snapshot.
   549     /// restored. These events invalidate the snapshot.
       
   550     /// However the arcs and nodes that were added to the digraph after
       
   551     /// making the current snapshot can be removed without invalidating it.
   543     class Snapshot {
   552     class Snapshot {
   544     protected:
   553     protected:
   545 
   554 
   546       typedef Parent::NodeNotifier NodeNotifier;
   555       typedef Parent::NodeNotifier NodeNotifier;
   547 
   556 
   707     public:
   716     public:
   708 
   717 
   709       /// \brief Default constructor.
   718       /// \brief Default constructor.
   710       ///
   719       ///
   711       /// Default constructor.
   720       /// Default constructor.
   712       /// To actually make a snapshot you must call save().
   721       /// You have to call save() to actually make a snapshot.
   713       Snapshot()
   722       Snapshot()
   714         : digraph(0), node_observer_proxy(*this),
   723         : digraph(0), node_observer_proxy(*this),
   715           arc_observer_proxy(*this) {}
   724           arc_observer_proxy(*this) {}
   716 
   725 
   717       /// \brief Constructor that immediately makes a snapshot.
   726       /// \brief Constructor that immediately makes a snapshot.
   718       ///
   727       ///
   719       /// This constructor immediately makes a snapshot of the digraph.
   728       /// This constructor immediately makes a snapshot of the given digraph.
   720       /// \param _digraph The digraph we make a snapshot of.
   729       Snapshot(ListDigraph &gr)
   721       Snapshot(ListDigraph &_digraph)
       
   722         : node_observer_proxy(*this),
   730         : node_observer_proxy(*this),
   723           arc_observer_proxy(*this) {
   731           arc_observer_proxy(*this) {
   724         attach(_digraph);
   732         attach(gr);
   725       }
   733       }
   726 
   734 
   727       /// \brief Make a snapshot.
   735       /// \brief Make a snapshot.
   728       ///
   736       ///
   729       /// Make a snapshot of the digraph.
   737       /// This function makes a snapshot of the given digraph.
   730       ///
   738       /// It can be called more than once. In case of a repeated
   731       /// This function can be called more than once. In case of a repeated
       
   732       /// call, the previous snapshot gets lost.
   739       /// call, the previous snapshot gets lost.
   733       /// \param _digraph The digraph we make the snapshot of.
   740       void save(ListDigraph &gr) {
   734       void save(ListDigraph &_digraph) {
       
   735         if (attached()) {
   741         if (attached()) {
   736           detach();
   742           detach();
   737           clear();
   743           clear();
   738         }
   744         }
   739         attach(_digraph);
   745         attach(gr);
   740       }
   746       }
   741 
   747 
   742       /// \brief Undo the changes until the last snapshot.
   748       /// \brief Undo the changes until the last snapshot.
   743       //
   749       ///
   744       /// Undo the changes until the last snapshot created by save().
   750       /// This function undos the changes until the last snapshot
       
   751       /// created by save() or Snapshot(ListDigraph&).
   745       void restore() {
   752       void restore() {
   746         detach();
   753         detach();
   747         for(std::list<Arc>::iterator it = added_arcs.begin();
   754         for(std::list<Arc>::iterator it = added_arcs.begin();
   748             it != added_arcs.end(); ++it) {
   755             it != added_arcs.end(); ++it) {
   749           digraph->erase(*it);
   756           digraph->erase(*it);
   753           digraph->erase(*it);
   760           digraph->erase(*it);
   754         }
   761         }
   755         clear();
   762         clear();
   756       }
   763       }
   757 
   764 
   758       /// \brief Gives back true when the snapshot is valid.
   765       /// \brief Returns \c true if the snapshot is valid.
   759       ///
   766       ///
   760       /// Gives back true when the snapshot is valid.
   767       /// This function returns \c true if the snapshot is valid.
   761       bool valid() const {
   768       bool valid() const {
   762         return attached();
   769         return attached();
   763       }
   770       }
   764     };
   771     };
   765 
   772 
   792     int first_free_arc;
   799     int first_free_arc;
   793 
   800 
   794   public:
   801   public:
   795 
   802 
   796     typedef ListGraphBase Graph;
   803     typedef ListGraphBase Graph;
   797 
       
   798     class Node;
       
   799     class Arc;
       
   800     class Edge;
       
   801 
   804 
   802     class Node {
   805     class Node {
   803       friend class ListGraphBase;
   806       friend class ListGraphBase;
   804     protected:
   807     protected:
   805 
   808 
   845       Arc (Invalid) { id = -1; }
   848       Arc (Invalid) { id = -1; }
   846       bool operator==(const Arc& arc) const {return id == arc.id;}
   849       bool operator==(const Arc& arc) const {return id == arc.id;}
   847       bool operator!=(const Arc& arc) const {return id != arc.id;}
   850       bool operator!=(const Arc& arc) const {return id != arc.id;}
   848       bool operator<(const Arc& arc) const {return id < arc.id;}
   851       bool operator<(const Arc& arc) const {return id < arc.id;}
   849     };
   852     };
   850 
       
   851 
       
   852 
   853 
   853     ListGraphBase()
   854     ListGraphBase()
   854       : nodes(), first_node(-1),
   855       : nodes(), first_node(-1),
   855         first_free_node(-1), arcs(), first_free_arc(-1) {}
   856         first_free_node(-1), arcs(), first_free_arc(-1) {}
   856 
   857 
  1162   /// \addtogroup graphs
  1163   /// \addtogroup graphs
  1163   /// @{
  1164   /// @{
  1164 
  1165 
  1165   ///A general undirected graph structure.
  1166   ///A general undirected graph structure.
  1166 
  1167 
  1167   ///\ref ListGraph is a simple and fast <em>undirected graph</em>
  1168   ///\ref ListGraph is a versatile and fast undirected graph
  1168   ///implementation based on static linked lists that are stored in
  1169   ///implementation based on linked lists that are stored in
  1169   ///\c std::vector structures.
  1170   ///\c std::vector structures.
  1170   ///
  1171   ///
  1171   ///It conforms to the \ref concepts::Graph "Graph concept" and it
  1172   ///This type fully conforms to the \ref concepts::Graph "Graph concept"
  1172   ///also provides several useful additional functionalities.
  1173   ///and it also provides several useful additional functionalities.
  1173   ///Most of the member functions and nested classes are documented
  1174   ///Most of its member functions and nested classes are documented
  1174   ///only in the concept class.
  1175   ///only in the concept class.
  1175   ///
  1176   ///
  1176   ///\sa concepts::Graph
  1177   ///\sa concepts::Graph
  1177 
  1178   ///\sa ListDigraph
  1178   class ListGraph : public ExtendedListGraphBase {
  1179   class ListGraph : public ExtendedListGraphBase {
  1179     typedef ExtendedListGraphBase Parent;
  1180     typedef ExtendedListGraphBase Parent;
  1180 
  1181 
  1181   private:
  1182   private:
  1182     ///ListGraph is \e not copy constructible. Use copyGraph() instead.
  1183     /// Graphs are \e not copy constructible. Use GraphCopy instead.
  1183 
       
  1184     ///ListGraph is \e not copy constructible. Use copyGraph() instead.
       
  1185     ///
       
  1186     ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
  1184     ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
  1187     ///\brief Assignment of ListGraph to another one is \e not allowed.
  1185     /// \brief Assignment of a graph to another one is \e not allowed.
  1188     ///Use copyGraph() instead.
  1186     /// Use GraphCopy instead.
  1189 
       
  1190     ///Assignment of ListGraph to another one is \e not allowed.
       
  1191     ///Use copyGraph() instead.
       
  1192     void operator=(const ListGraph &) {}
  1187     void operator=(const ListGraph &) {}
  1193   public:
  1188   public:
  1194     /// Constructor
  1189     /// Constructor
  1195 
  1190 
  1196     /// Constructor.
  1191     /// Constructor.
  1199 
  1194 
  1200     typedef Parent::OutArcIt IncEdgeIt;
  1195     typedef Parent::OutArcIt IncEdgeIt;
  1201 
  1196 
  1202     /// \brief Add a new node to the graph.
  1197     /// \brief Add a new node to the graph.
  1203     ///
  1198     ///
  1204     /// Add a new node to the graph.
  1199     /// This function adds a new node to the graph.
  1205     /// \return The new node.
  1200     /// \return The new node.
  1206     Node addNode() { return Parent::addNode(); }
  1201     Node addNode() { return Parent::addNode(); }
  1207 
  1202 
  1208     /// \brief Add a new edge to the graph.
  1203     /// \brief Add a new edge to the graph.
  1209     ///
  1204     ///
  1210     /// Add a new edge to the graph with source node \c s
  1205     /// This function adds a new edge to the graph between nodes
  1211     /// and target node \c t.
  1206     /// \c u and \c v with inherent orientation from node \c u to
       
  1207     /// node \c v.
  1212     /// \return The new edge.
  1208     /// \return The new edge.
  1213     Edge addEdge(const Node& s, const Node& t) {
  1209     Edge addEdge(Node u, Node v) {
  1214       return Parent::addEdge(s, t);
  1210       return Parent::addEdge(u, v);
  1215     }
  1211     }
  1216 
  1212 
  1217     /// \brief Erase a node from the graph.
  1213     ///\brief Erase a node from the graph.
  1218     ///
  1214     ///
  1219     /// Erase a node from the graph.
  1215     /// This function erases the given node from the graph.
  1220     ///
  1216     void erase(Node n) { Parent::erase(n); }
  1221     void erase(const Node& n) { Parent::erase(n); }
  1217 
  1222 
  1218     ///\brief Erase an edge from the graph.
  1223     /// \brief Erase an edge from the graph.
  1219     ///
  1224     ///
  1220     /// This function erases the given edge from the graph.
  1225     /// Erase an edge from the graph.
  1221     void erase(Edge e) { Parent::erase(e); }
  1226     ///
       
  1227     void erase(const Edge& e) { Parent::erase(e); }
       
  1228     /// Node validity check
  1222     /// Node validity check
  1229 
  1223 
  1230     /// This function gives back true if the given node is valid,
  1224     /// This function gives back \c true if the given node is valid,
  1231     /// ie. it is a real node of the graph.
  1225     /// i.e. it is a real node of the graph.
  1232     ///
  1226     ///
  1233     /// \warning A Node pointing to a removed item
  1227     /// \warning A removed node could become valid again if new nodes are
  1234     /// could become valid again later if new nodes are
       
  1235     /// added to the graph.
  1228     /// added to the graph.
  1236     bool valid(Node n) const { return Parent::valid(n); }
  1229     bool valid(Node n) const { return Parent::valid(n); }
       
  1230     /// Edge validity check
       
  1231 
       
  1232     /// This function gives back \c true if the given edge is valid,
       
  1233     /// i.e. it is a real edge of the graph.
       
  1234     ///
       
  1235     /// \warning A removed edge could become valid again if new edges are
       
  1236     /// added to the graph.
       
  1237     bool valid(Edge e) const { return Parent::valid(e); }
  1237     /// Arc validity check
  1238     /// Arc validity check
  1238 
  1239 
  1239     /// This function gives back true if the given arc is valid,
  1240     /// This function gives back \c true if the given arc is valid,
  1240     /// ie. it is a real arc of the graph.
  1241     /// i.e. it is a real arc of the graph.
  1241     ///
  1242     ///
  1242     /// \warning An Arc pointing to a removed item
  1243     /// \warning A removed arc could become valid again if new edges are
  1243     /// could become valid again later if new edges are
       
  1244     /// added to the graph.
  1244     /// added to the graph.
  1245     bool valid(Arc a) const { return Parent::valid(a); }
  1245     bool valid(Arc a) const { return Parent::valid(a); }
  1246     /// Edge validity check
  1246 
  1247 
  1247     /// \brief Change the first node of an edge.
  1248     /// This function gives back true if the given edge is valid,
  1248     ///
  1249     /// ie. it is a real arc of the graph.
  1249     /// This function changes the first node of the given edge \c e to \c n.
  1250     ///
  1250     ///
  1251     /// \warning A Edge pointing to a removed item
  1251     ///\note \c EdgeIt and \c ArcIt iterators referencing the
  1252     /// could become valid again later if new edges are
  1252     ///changed edge are invalidated and all other iterators whose
  1253     /// added to the graph.
  1253     ///base node is the changed node are also invalidated.
  1254     bool valid(Edge e) const { return Parent::valid(e); }
       
  1255     /// \brief Change the end \c u of \c e to \c n
       
  1256     ///
       
  1257     /// This function changes the end \c u of \c e to node \c n.
       
  1258     ///
       
  1259     ///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the
       
  1260     ///changed edge are invalidated and if the changed node is the
       
  1261     ///base node of an iterator then this iterator is also
       
  1262     ///invalidated.
       
  1263     ///
  1254     ///
  1264     ///\warning This functionality cannot be used together with the
  1255     ///\warning This functionality cannot be used together with the
  1265     ///Snapshot feature.
  1256     ///Snapshot feature.
  1266     void changeU(Edge e, Node n) {
  1257     void changeU(Edge e, Node n) {
  1267       Parent::changeU(e,n);
  1258       Parent::changeU(e,n);
  1268     }
  1259     }
  1269     /// \brief Change the end \c v of \c e to \c n
  1260     /// \brief Change the second node of an edge.
  1270     ///
  1261     ///
  1271     /// This function changes the end \c v of \c e to \c n.
  1262     /// This function changes the second node of the given edge \c e to \c n.
  1272     ///
  1263     ///
  1273     ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
  1264     ///\note \c EdgeIt iterators referencing the changed edge remain
  1274     ///valid, however <tt>ArcIt</tt>s and if the changed node is the
  1265     ///valid, however \c ArcIt iterators referencing the changed edge and
  1275     ///base node of an iterator then this iterator is invalidated.
  1266     ///all other iterators whose base node is the changed node are also
       
  1267     ///invalidated.
  1276     ///
  1268     ///
  1277     ///\warning This functionality cannot be used together with the
  1269     ///\warning This functionality cannot be used together with the
  1278     ///Snapshot feature.
  1270     ///Snapshot feature.
  1279     void changeV(Edge e, Node n) {
  1271     void changeV(Edge e, Node n) {
  1280       Parent::changeV(e,n);
  1272       Parent::changeV(e,n);
  1281     }
  1273     }
       
  1274 
  1282     /// \brief Contract two nodes.
  1275     /// \brief Contract two nodes.
  1283     ///
  1276     ///
  1284     /// This function contracts two nodes.
  1277     /// This function contracts the given two nodes.
  1285     /// Node \p b will be removed but instead of deleting
  1278     /// Node \c b is removed, but instead of deleting
  1286     /// its neighboring arcs, they will be joined to \p a.
  1279     /// its incident edges, they are joined to node \c a.
  1287     /// The last parameter \p r controls whether to remove loops. \c true
  1280     /// If the last parameter \c r is \c true (this is the default value),
  1288     /// means that loops will be removed.
  1281     /// then the newly created loops are removed.
  1289     ///
  1282     ///
  1290     /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
  1283     /// \note The moved edges are joined to node \c a using changeU()
  1291     /// valid.
  1284     /// or changeV(), thus all edge and arc iterators whose base node is
       
  1285     /// \c b are invalidated.
       
  1286     /// Moreover all iterators referencing node \c b or the removed 
       
  1287     /// loops are also invalidated. Other iterators remain valid.
  1292     ///
  1288     ///
  1293     ///\warning This functionality cannot be used together with the
  1289     ///\warning This functionality cannot be used together with the
  1294     ///Snapshot feature.
  1290     ///Snapshot feature.
  1295     void contract(Node a, Node b, bool r = true) {
  1291     void contract(Node a, Node b, bool r = true) {
  1296       for(IncEdgeIt e(*this, b); e!=INVALID;) {
  1292       for(IncEdgeIt e(*this, b); e!=INVALID;) {
  1305         e = f;
  1301         e = f;
  1306       }
  1302       }
  1307       erase(b);
  1303       erase(b);
  1308     }
  1304     }
  1309 
  1305 
       
  1306     ///Clear the graph.
       
  1307 
       
  1308     ///This function erases all nodes and arcs from the graph.
       
  1309     ///
       
  1310     void clear() {
       
  1311       Parent::clear();
       
  1312     }
  1310 
  1313 
  1311     /// \brief Class to make a snapshot of the graph and restore
  1314     /// \brief Class to make a snapshot of the graph and restore
  1312     /// it later.
  1315     /// it later.
  1313     ///
  1316     ///
  1314     /// Class to make a snapshot of the graph and restore it later.
  1317     /// Class to make a snapshot of the graph and restore it later.
  1315     ///
  1318     ///
  1316     /// The newly added nodes and edges can be removed
  1319     /// The newly added nodes and edges can be removed
  1317     /// using the restore() function.
  1320     /// using the restore() function.
  1318     ///
  1321     ///
  1319     /// \warning Edge and node deletions and other modifications
  1322     /// \note After a state is restored, you cannot restore a later state, 
  1320     /// (e.g. changing nodes of edges, contracting nodes) cannot be
  1323     /// i.e. you cannot add the removed nodes and edges again using
  1321     /// restored. These events invalidate the snapshot.
  1324     /// another Snapshot instance.
       
  1325     ///
       
  1326     /// \warning Node and edge deletions and other modifications
       
  1327     /// (e.g. changing the end-nodes of edges or contracting nodes)
       
  1328     /// cannot be restored. These events invalidate the snapshot.
       
  1329     /// However the edges and nodes that were added to the graph after
       
  1330     /// making the current snapshot can be removed without invalidating it.
  1322     class Snapshot {
  1331     class Snapshot {
  1323     protected:
  1332     protected:
  1324 
  1333 
  1325       typedef Parent::NodeNotifier NodeNotifier;
  1334       typedef Parent::NodeNotifier NodeNotifier;
  1326 
  1335 
  1486     public:
  1495     public:
  1487 
  1496 
  1488       /// \brief Default constructor.
  1497       /// \brief Default constructor.
  1489       ///
  1498       ///
  1490       /// Default constructor.
  1499       /// Default constructor.
  1491       /// To actually make a snapshot you must call save().
  1500       /// You have to call save() to actually make a snapshot.
  1492       Snapshot()
  1501       Snapshot()
  1493         : graph(0), node_observer_proxy(*this),
  1502         : graph(0), node_observer_proxy(*this),
  1494           edge_observer_proxy(*this) {}
  1503           edge_observer_proxy(*this) {}
  1495 
  1504 
  1496       /// \brief Constructor that immediately makes a snapshot.
  1505       /// \brief Constructor that immediately makes a snapshot.
  1497       ///
  1506       ///
  1498       /// This constructor immediately makes a snapshot of the graph.
  1507       /// This constructor immediately makes a snapshot of the given graph.
  1499       /// \param _graph The graph we make a snapshot of.
  1508       Snapshot(ListGraph &gr)
  1500       Snapshot(ListGraph &_graph)
       
  1501         : node_observer_proxy(*this),
  1509         : node_observer_proxy(*this),
  1502           edge_observer_proxy(*this) {
  1510           edge_observer_proxy(*this) {
  1503         attach(_graph);
  1511         attach(gr);
  1504       }
  1512       }
  1505 
  1513 
  1506       /// \brief Make a snapshot.
  1514       /// \brief Make a snapshot.
  1507       ///
  1515       ///
  1508       /// Make a snapshot of the graph.
  1516       /// This function makes a snapshot of the given graph.
  1509       ///
  1517       /// It can be called more than once. In case of a repeated
  1510       /// This function can be called more than once. In case of a repeated
       
  1511       /// call, the previous snapshot gets lost.
  1518       /// call, the previous snapshot gets lost.
  1512       /// \param _graph The graph we make the snapshot of.
  1519       void save(ListGraph &gr) {
  1513       void save(ListGraph &_graph) {
       
  1514         if (attached()) {
  1520         if (attached()) {
  1515           detach();
  1521           detach();
  1516           clear();
  1522           clear();
  1517         }
  1523         }
  1518         attach(_graph);
  1524         attach(gr);
  1519       }
  1525       }
  1520 
  1526 
  1521       /// \brief Undo the changes until the last snapshot.
  1527       /// \brief Undo the changes until the last snapshot.
  1522       //
  1528       ///
  1523       /// Undo the changes until the last snapshot created by save().
  1529       /// This function undos the changes until the last snapshot
       
  1530       /// created by save() or Snapshot(ListGraph&).
  1524       void restore() {
  1531       void restore() {
  1525         detach();
  1532         detach();
  1526         for(std::list<Edge>::iterator it = added_edges.begin();
  1533         for(std::list<Edge>::iterator it = added_edges.begin();
  1527             it != added_edges.end(); ++it) {
  1534             it != added_edges.end(); ++it) {
  1528           graph->erase(*it);
  1535           graph->erase(*it);
  1532           graph->erase(*it);
  1539           graph->erase(*it);
  1533         }
  1540         }
  1534         clear();
  1541         clear();
  1535       }
  1542       }
  1536 
  1543 
  1537       /// \brief Gives back true when the snapshot is valid.
  1544       /// \brief Returns \c true if the snapshot is valid.
  1538       ///
  1545       ///
  1539       /// Gives back true when the snapshot is valid.
  1546       /// This function returns \c true if the snapshot is valid.
  1540       bool valid() const {
  1547       bool valid() const {
  1541         return attached();
  1548         return attached();
  1542       }
  1549       }
  1543     };
  1550     };
  1544   };
  1551   };