1.1 --- a/lemon/smart_graph.h Sun Oct 04 10:15:32 2009 +0200
1.2 +++ b/lemon/smart_graph.h Wed Dec 09 11:14:06 2009 +0100
1.3 @@ -32,10 +32,7 @@
1.4 namespace lemon {
1.5
1.6 class SmartDigraph;
1.7 - ///Base of SmartDigraph
1.8
1.9 - ///Base of SmartDigraph
1.10 - ///
1.11 class SmartDigraphBase {
1.12 protected:
1.13
1.14 @@ -187,28 +184,28 @@
1.15 ///
1.16 ///\brief A smart directed graph class.
1.17 ///
1.18 - ///This is a simple and fast digraph implementation.
1.19 - ///It is also quite memory efficient, but at the price
1.20 - ///that <b> it does support only limited (only stack-like)
1.21 - ///node and arc deletions</b>.
1.22 - ///It fully conforms to the \ref concepts::Digraph "Digraph concept".
1.23 + ///\ref SmartDigraph is a simple and fast digraph implementation.
1.24 + ///It is also quite memory efficient but at the price
1.25 + ///that it does not support node and arc deletion
1.26 + ///(except for the Snapshot feature).
1.27 ///
1.28 - ///\sa concepts::Digraph.
1.29 + ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
1.30 + ///and it also provides some additional functionalities.
1.31 + ///Most of its member functions and nested classes are documented
1.32 + ///only in the concept class.
1.33 + ///
1.34 + ///This class provides constant time counting for nodes and arcs.
1.35 + ///
1.36 + ///\sa concepts::Digraph
1.37 + ///\sa SmartGraph
1.38 class SmartDigraph : public ExtendedSmartDigraphBase {
1.39 typedef ExtendedSmartDigraphBase Parent;
1.40
1.41 private:
1.42 -
1.43 - ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
1.44 -
1.45 - ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
1.46 - ///
1.47 + /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
1.48 SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
1.49 - ///\brief Assignment of SmartDigraph to another one is \e not allowed.
1.50 - ///Use DigraphCopy() instead.
1.51 -
1.52 - ///Assignment of SmartDigraph to another one is \e not allowed.
1.53 - ///Use DigraphCopy() instead.
1.54 + /// \brief Assignment of a digraph to another one is \e not allowed.
1.55 + /// Use DigraphCopy instead.
1.56 void operator=(const SmartDigraph &) {}
1.57
1.58 public:
1.59 @@ -221,79 +218,49 @@
1.60
1.61 ///Add a new node to the digraph.
1.62
1.63 - /// Add a new node to the digraph.
1.64 - /// \return The new node.
1.65 + ///This function adds a new node to the digraph.
1.66 + ///\return The new node.
1.67 Node addNode() { return Parent::addNode(); }
1.68
1.69 ///Add a new arc to the digraph.
1.70
1.71 - ///Add a new arc to the digraph with source node \c s
1.72 + ///This function adds a new arc to the digraph with source node \c s
1.73 ///and target node \c t.
1.74 ///\return The new arc.
1.75 - Arc addArc(const Node& s, const Node& t) {
1.76 + Arc addArc(Node s, Node t) {
1.77 return Parent::addArc(s, t);
1.78 }
1.79
1.80 - /// \brief Using this it is possible to avoid the superfluous memory
1.81 - /// allocation.
1.82 -
1.83 - /// Using this it is possible to avoid the superfluous memory
1.84 - /// allocation: if you know that the digraph you want to build will
1.85 - /// be very large (e.g. it will contain millions of nodes and/or arcs)
1.86 - /// then it is worth reserving space for this amount before starting
1.87 - /// to build the digraph.
1.88 - /// \sa reserveArc
1.89 - void reserveNode(int n) { nodes.reserve(n); };
1.90 -
1.91 - /// \brief Using this it is possible to avoid the superfluous memory
1.92 - /// allocation.
1.93 -
1.94 - /// Using this it is possible to avoid the superfluous memory
1.95 - /// allocation: if you know that the digraph you want to build will
1.96 - /// be very large (e.g. it will contain millions of nodes and/or arcs)
1.97 - /// then it is worth reserving space for this amount before starting
1.98 - /// to build the digraph.
1.99 - /// \sa reserveNode
1.100 - void reserveArc(int m) { arcs.reserve(m); };
1.101 -
1.102 /// \brief Node validity check
1.103 ///
1.104 - /// This function gives back true if the given node is valid,
1.105 - /// ie. it is a real node of the graph.
1.106 + /// This function gives back \c true if the given node is valid,
1.107 + /// i.e. it is a real node of the digraph.
1.108 ///
1.109 /// \warning A removed node (using Snapshot) could become valid again
1.110 - /// when new nodes are added to the graph.
1.111 + /// if new nodes are added to the digraph.
1.112 bool valid(Node n) const { return Parent::valid(n); }
1.113
1.114 /// \brief Arc validity check
1.115 ///
1.116 - /// This function gives back true if the given arc is valid,
1.117 - /// ie. it is a real arc of the graph.
1.118 + /// This function gives back \c true if the given arc is valid,
1.119 + /// i.e. it is a real arc of the digraph.
1.120 ///
1.121 /// \warning A removed arc (using Snapshot) could become valid again
1.122 - /// when new arcs are added to the graph.
1.123 + /// if new arcs are added to the graph.
1.124 bool valid(Arc a) const { return Parent::valid(a); }
1.125
1.126 - ///Clear the digraph.
1.127 -
1.128 - ///Erase all the nodes and arcs from the digraph.
1.129 - ///
1.130 - void clear() {
1.131 - Parent::clear();
1.132 - }
1.133 -
1.134 ///Split a node.
1.135
1.136 - ///This function splits a node. First a new node is added to the digraph,
1.137 - ///then the source of each outgoing arc of \c n is moved to this new node.
1.138 - ///If \c connect is \c true (this is the default value), then a new arc
1.139 - ///from \c n to the newly created node is also added.
1.140 + ///This function splits the given node. First, a new node is added
1.141 + ///to the digraph, then the source of each outgoing arc of node \c n
1.142 + ///is moved to this new node.
1.143 + ///If the second parameter \c connect is \c true (this is the default
1.144 + ///value), then a new arc from node \c n to the newly created node
1.145 + ///is also added.
1.146 ///\return The newly created node.
1.147 ///
1.148 - ///\note The <tt>Arc</tt>s
1.149 - ///referencing a moved arc remain
1.150 - ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
1.151 - ///may be invalidated.
1.152 + ///\note All iterators remain valid.
1.153 + ///
1.154 ///\warning This functionality cannot be used together with the Snapshot
1.155 ///feature.
1.156 Node split(Node n, bool connect = true)
1.157 @@ -308,6 +275,34 @@
1.158 return b;
1.159 }
1.160
1.161 + ///Clear the digraph.
1.162 +
1.163 + ///This function erases all nodes and arcs from the digraph.
1.164 + ///
1.165 + void clear() {
1.166 + Parent::clear();
1.167 + }
1.168 +
1.169 + /// Reserve memory for nodes.
1.170 +
1.171 + /// Using this function, it is possible to avoid superfluous memory
1.172 + /// allocation: if you know that the digraph you want to build will
1.173 + /// be large (e.g. it will contain millions of nodes and/or arcs),
1.174 + /// then it is worth reserving space for this amount before starting
1.175 + /// to build the digraph.
1.176 + /// \sa reserveArc()
1.177 + void reserveNode(int n) { nodes.reserve(n); };
1.178 +
1.179 + /// Reserve memory for arcs.
1.180 +
1.181 + /// Using this function, it is possible to avoid superfluous memory
1.182 + /// allocation: if you know that the digraph you want to build will
1.183 + /// be large (e.g. it will contain millions of nodes and/or arcs),
1.184 + /// then it is worth reserving space for this amount before starting
1.185 + /// to build the digraph.
1.186 + /// \sa reserveNode()
1.187 + void reserveArc(int m) { arcs.reserve(m); };
1.188 +
1.189 public:
1.190
1.191 class Snapshot;
1.192 @@ -332,20 +327,23 @@
1.193
1.194 public:
1.195
1.196 - ///Class to make a snapshot of the digraph and to restrore to it later.
1.197 + ///Class to make a snapshot of the digraph and to restore it later.
1.198
1.199 - ///Class to make a snapshot of the digraph and to restrore to it later.
1.200 + ///Class to make a snapshot of the digraph and to restore it later.
1.201 ///
1.202 ///The newly added nodes and arcs can be removed using the
1.203 - ///restore() function.
1.204 - ///\note After you restore a state, you cannot restore
1.205 - ///a later state, in other word you cannot add again the arcs deleted
1.206 - ///by restore() using another one Snapshot instance.
1.207 + ///restore() function. This is the only way for deleting nodes and/or
1.208 + ///arcs from a SmartDigraph structure.
1.209 ///
1.210 - ///\warning If you do not use correctly the snapshot that can cause
1.211 - ///either broken program, invalid state of the digraph, valid but
1.212 - ///not the restored digraph or no change. Because the runtime performance
1.213 - ///the validity of the snapshot is not stored.
1.214 + ///\note After a state is restored, you cannot restore a later state,
1.215 + ///i.e. you cannot add the removed nodes and arcs again using
1.216 + ///another Snapshot instance.
1.217 + ///
1.218 + ///\warning Node splitting cannot be restored.
1.219 + ///\warning The validity of the snapshot is not stored due to
1.220 + ///performance reasons. If you do not use the snapshot correctly,
1.221 + ///it can cause broken program, invalid or not restored state of
1.222 + ///the digraph or no change.
1.223 class Snapshot
1.224 {
1.225 SmartDigraph *_graph;
1.226 @@ -357,39 +355,32 @@
1.227 ///Default constructor.
1.228
1.229 ///Default constructor.
1.230 - ///To actually make a snapshot you must call save().
1.231 - ///
1.232 + ///You have to call save() to actually make a snapshot.
1.233 Snapshot() : _graph(0) {}
1.234 ///Constructor that immediately makes a snapshot
1.235
1.236 - ///This constructor immediately makes a snapshot of the digraph.
1.237 - ///\param graph The digraph we make a snapshot of.
1.238 - Snapshot(SmartDigraph &graph) : _graph(&graph) {
1.239 + ///This constructor immediately makes a snapshot of the given digraph.
1.240 + ///
1.241 + Snapshot(SmartDigraph &gr) : _graph(&gr) {
1.242 node_num=_graph->nodes.size();
1.243 arc_num=_graph->arcs.size();
1.244 }
1.245
1.246 ///Make a snapshot.
1.247
1.248 - ///Make a snapshot of the digraph.
1.249 - ///
1.250 - ///This function can be called more than once. In case of a repeated
1.251 + ///This function makes a snapshot of the given digraph.
1.252 + ///It can be called more than once. In case of a repeated
1.253 ///call, the previous snapshot gets lost.
1.254 - ///\param graph The digraph we make the snapshot of.
1.255 - void save(SmartDigraph &graph)
1.256 - {
1.257 - _graph=&graph;
1.258 + void save(SmartDigraph &gr) {
1.259 + _graph=&gr;
1.260 node_num=_graph->nodes.size();
1.261 arc_num=_graph->arcs.size();
1.262 }
1.263
1.264 ///Undo the changes until a snapshot.
1.265
1.266 - ///Undo the changes until a snapshot created by save().
1.267 - ///
1.268 - ///\note After you restored a state, you cannot restore
1.269 - ///a later state, in other word you cannot add again the arcs deleted
1.270 - ///by restore().
1.271 + ///This function undos the changes until the last snapshot
1.272 + ///created by save() or Snapshot(SmartDigraph&).
1.273 void restore()
1.274 {
1.275 _graph->restoreSnapshot(*this);
1.276 @@ -508,7 +499,7 @@
1.277 node._id = nodes.size() - 1;
1.278 }
1.279
1.280 - void next(Node& node) const {
1.281 + static void next(Node& node) {
1.282 --node._id;
1.283 }
1.284
1.285 @@ -516,7 +507,7 @@
1.286 arc._id = arcs.size() - 1;
1.287 }
1.288
1.289 - void next(Arc& arc) const {
1.290 + static void next(Arc& arc) {
1.291 --arc._id;
1.292 }
1.293
1.294 @@ -524,7 +515,7 @@
1.295 arc._id = arcs.size() / 2 - 1;
1.296 }
1.297
1.298 - void next(Edge& arc) const {
1.299 + static void next(Edge& arc) {
1.300 --arc._id;
1.301 }
1.302
1.303 @@ -621,29 +612,28 @@
1.304 ///
1.305 /// \brief A smart undirected graph class.
1.306 ///
1.307 - /// This is a simple and fast graph implementation.
1.308 - /// It is also quite memory efficient, but at the price
1.309 - /// that <b> it does support only limited (only stack-like)
1.310 - /// node and arc deletions</b>.
1.311 - /// It fully conforms to the \ref concepts::Graph "Graph concept".
1.312 + /// \ref SmartGraph is a simple and fast graph implementation.
1.313 + /// It is also quite memory efficient but at the price
1.314 + /// that it does not support node and edge deletion
1.315 + /// (except for the Snapshot feature).
1.316 ///
1.317 - /// \sa concepts::Graph.
1.318 + /// This type fully conforms to the \ref concepts::Graph "Graph concept"
1.319 + /// and it also provides some additional functionalities.
1.320 + /// Most of its member functions and nested classes are documented
1.321 + /// only in the concept class.
1.322 + ///
1.323 + /// This class provides constant time counting for nodes, edges and arcs.
1.324 + ///
1.325 + /// \sa concepts::Graph
1.326 + /// \sa SmartDigraph
1.327 class SmartGraph : public ExtendedSmartGraphBase {
1.328 typedef ExtendedSmartGraphBase Parent;
1.329
1.330 private:
1.331 -
1.332 - ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
1.333 -
1.334 - ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
1.335 - ///
1.336 + /// Graphs are \e not copy constructible. Use GraphCopy instead.
1.337 SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
1.338 -
1.339 - ///\brief Assignment of SmartGraph to another one is \e not allowed.
1.340 - ///Use GraphCopy() instead.
1.341 -
1.342 - ///Assignment of SmartGraph to another one is \e not allowed.
1.343 - ///Use GraphCopy() instead.
1.344 + /// \brief Assignment of a graph to another one is \e not allowed.
1.345 + /// Use GraphCopy instead.
1.346 void operator=(const SmartGraph &) {}
1.347
1.348 public:
1.349 @@ -654,56 +644,77 @@
1.350 ///
1.351 SmartGraph() {}
1.352
1.353 - ///Add a new node to the graph.
1.354 -
1.355 - /// Add a new node to the graph.
1.356 + /// \brief Add a new node to the graph.
1.357 + ///
1.358 + /// This function adds a new node to the graph.
1.359 /// \return The new node.
1.360 Node addNode() { return Parent::addNode(); }
1.361
1.362 - ///Add a new edge to the graph.
1.363 -
1.364 - ///Add a new edge to the graph with node \c s
1.365 - ///and \c t.
1.366 - ///\return The new edge.
1.367 - Edge addEdge(const Node& s, const Node& t) {
1.368 - return Parent::addEdge(s, t);
1.369 + /// \brief Add a new edge to the graph.
1.370 + ///
1.371 + /// This function adds a new edge to the graph between nodes
1.372 + /// \c u and \c v with inherent orientation from node \c u to
1.373 + /// node \c v.
1.374 + /// \return The new edge.
1.375 + Edge addEdge(Node u, Node v) {
1.376 + return Parent::addEdge(u, v);
1.377 }
1.378
1.379 /// \brief Node validity check
1.380 ///
1.381 - /// This function gives back true if the given node is valid,
1.382 - /// ie. it is a real node of the graph.
1.383 + /// This function gives back \c true if the given node is valid,
1.384 + /// i.e. it is a real node of the graph.
1.385 ///
1.386 /// \warning A removed node (using Snapshot) could become valid again
1.387 - /// when new nodes are added to the graph.
1.388 + /// if new nodes are added to the graph.
1.389 bool valid(Node n) const { return Parent::valid(n); }
1.390
1.391 + /// \brief Edge validity check
1.392 + ///
1.393 + /// This function gives back \c true if the given edge is valid,
1.394 + /// i.e. it is a real edge of the graph.
1.395 + ///
1.396 + /// \warning A removed edge (using Snapshot) could become valid again
1.397 + /// if new edges are added to the graph.
1.398 + bool valid(Edge e) const { return Parent::valid(e); }
1.399 +
1.400 /// \brief Arc validity check
1.401 ///
1.402 - /// This function gives back true if the given arc is valid,
1.403 - /// ie. it is a real arc of the graph.
1.404 + /// This function gives back \c true if the given arc is valid,
1.405 + /// i.e. it is a real arc of the graph.
1.406 ///
1.407 /// \warning A removed arc (using Snapshot) could become valid again
1.408 - /// when new edges are added to the graph.
1.409 + /// if new edges are added to the graph.
1.410 bool valid(Arc a) const { return Parent::valid(a); }
1.411
1.412 - /// \brief Edge validity check
1.413 - ///
1.414 - /// This function gives back true if the given edge is valid,
1.415 - /// ie. it is a real edge of the graph.
1.416 - ///
1.417 - /// \warning A removed edge (using Snapshot) could become valid again
1.418 - /// when new edges are added to the graph.
1.419 - bool valid(Edge e) const { return Parent::valid(e); }
1.420 -
1.421 ///Clear the graph.
1.422
1.423 - ///Erase all the nodes and edges from the graph.
1.424 + ///This function erases all nodes and arcs from the graph.
1.425 ///
1.426 void clear() {
1.427 Parent::clear();
1.428 }
1.429
1.430 + /// Reserve memory for nodes.
1.431 +
1.432 + /// Using this function, it is possible to avoid superfluous memory
1.433 + /// allocation: if you know that the graph you want to build will
1.434 + /// be large (e.g. it will contain millions of nodes and/or edges),
1.435 + /// then it is worth reserving space for this amount before starting
1.436 + /// to build the graph.
1.437 + /// \sa reserveEdge()
1.438 + void reserveNode(int n) { nodes.reserve(n); };
1.439 +
1.440 + /// Reserve memory for edges.
1.441 +
1.442 + /// Using this function, it is possible to avoid superfluous memory
1.443 + /// allocation: if you know that the graph you want to build will
1.444 + /// be large (e.g. it will contain millions of nodes and/or edges),
1.445 + /// then it is worth reserving space for this amount before starting
1.446 + /// to build the graph.
1.447 + /// \sa reserveNode()
1.448 + void reserveEdge(int m) { arcs.reserve(2 * m); };
1.449 +
1.450 public:
1.451
1.452 class Snapshot;
1.453 @@ -742,21 +753,22 @@
1.454
1.455 public:
1.456
1.457 - ///Class to make a snapshot of the digraph and to restrore to it later.
1.458 + ///Class to make a snapshot of the graph and to restore it later.
1.459
1.460 - ///Class to make a snapshot of the digraph and to restrore to it later.
1.461 + ///Class to make a snapshot of the graph and to restore it later.
1.462 ///
1.463 - ///The newly added nodes and arcs can be removed using the
1.464 - ///restore() function.
1.465 + ///The newly added nodes and edges can be removed using the
1.466 + ///restore() function. This is the only way for deleting nodes and/or
1.467 + ///edges from a SmartGraph structure.
1.468 ///
1.469 - ///\note After you restore a state, you cannot restore
1.470 - ///a later state, in other word you cannot add again the arcs deleted
1.471 - ///by restore() using another one Snapshot instance.
1.472 + ///\note After a state is restored, you cannot restore a later state,
1.473 + ///i.e. you cannot add the removed nodes and edges again using
1.474 + ///another Snapshot instance.
1.475 ///
1.476 - ///\warning If you do not use correctly the snapshot that can cause
1.477 - ///either broken program, invalid state of the digraph, valid but
1.478 - ///not the restored digraph or no change. Because the runtime performance
1.479 - ///the validity of the snapshot is not stored.
1.480 + ///\warning The validity of the snapshot is not stored due to
1.481 + ///performance reasons. If you do not use the snapshot correctly,
1.482 + ///it can cause broken program, invalid or not restored state of
1.483 + ///the graph or no change.
1.484 class Snapshot
1.485 {
1.486 SmartGraph *_graph;
1.487 @@ -768,36 +780,30 @@
1.488 ///Default constructor.
1.489
1.490 ///Default constructor.
1.491 - ///To actually make a snapshot you must call save().
1.492 - ///
1.493 + ///You have to call save() to actually make a snapshot.
1.494 Snapshot() : _graph(0) {}
1.495 ///Constructor that immediately makes a snapshot
1.496
1.497 - ///This constructor immediately makes a snapshot of the digraph.
1.498 - ///\param graph The digraph we make a snapshot of.
1.499 - Snapshot(SmartGraph &graph) {
1.500 - graph.saveSnapshot(*this);
1.501 + /// This constructor immediately makes a snapshot of the given graph.
1.502 + ///
1.503 + Snapshot(SmartGraph &gr) {
1.504 + gr.saveSnapshot(*this);
1.505 }
1.506
1.507 ///Make a snapshot.
1.508
1.509 - ///Make a snapshot of the graph.
1.510 - ///
1.511 - ///This function can be called more than once. In case of a repeated
1.512 + ///This function makes a snapshot of the given graph.
1.513 + ///It can be called more than once. In case of a repeated
1.514 ///call, the previous snapshot gets lost.
1.515 - ///\param graph The digraph we make the snapshot of.
1.516 - void save(SmartGraph &graph)
1.517 + void save(SmartGraph &gr)
1.518 {
1.519 - graph.saveSnapshot(*this);
1.520 + gr.saveSnapshot(*this);
1.521 }
1.522
1.523 - ///Undo the changes until a snapshot.
1.524 + ///Undo the changes until the last snapshot.
1.525
1.526 - ///Undo the changes until a snapshot created by save().
1.527 - ///
1.528 - ///\note After you restored a state, you cannot restore
1.529 - ///a later state, in other word you cannot add again the arcs deleted
1.530 - ///by restore().
1.531 + ///This function undos the changes until the last snapshot
1.532 + ///created by save() or Snapshot(SmartGraph&).
1.533 void restore()
1.534 {
1.535 _graph->restoreSnapshot(*this);