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