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