1.1 --- a/lemon/smart_graph.h Sun Nov 14 16:35:31 2010 +0100
1.2 +++ b/lemon/smart_graph.h Sun Nov 14 20:06:23 2010 +0100
1.3 @@ -405,8 +405,6 @@
1.4 std::vector<NodeT> nodes;
1.5 std::vector<ArcT> arcs;
1.6
1.7 - int first_free_arc;
1.8 -
1.9 public:
1.10
1.11 typedef SmartGraphBase Graph;
1.12 @@ -811,6 +809,514 @@
1.13 };
1.14 };
1.15
1.16 + class SmartBpGraphBase {
1.17 +
1.18 + protected:
1.19 +
1.20 + struct NodeT {
1.21 + int first_out;
1.22 + int partition_next;
1.23 + int partition_index;
1.24 + bool red;
1.25 + };
1.26 +
1.27 + struct ArcT {
1.28 + int target;
1.29 + int next_out;
1.30 + };
1.31 +
1.32 + std::vector<NodeT> nodes;
1.33 + std::vector<ArcT> arcs;
1.34 +
1.35 + int first_red, first_blue;
1.36 +
1.37 + public:
1.38 +
1.39 + typedef SmartBpGraphBase Graph;
1.40 +
1.41 + class Node;
1.42 + class Arc;
1.43 + class Edge;
1.44 +
1.45 + class Node {
1.46 + friend class SmartBpGraphBase;
1.47 + protected:
1.48 +
1.49 + int _id;
1.50 + explicit Node(int id) { _id = id;}
1.51 +
1.52 + public:
1.53 + Node() {}
1.54 + Node (Invalid) { _id = -1; }
1.55 + bool operator==(const Node& node) const {return _id == node._id;}
1.56 + bool operator!=(const Node& node) const {return _id != node._id;}
1.57 + bool operator<(const Node& node) const {return _id < node._id;}
1.58 + };
1.59 +
1.60 + class Edge {
1.61 + friend class SmartBpGraphBase;
1.62 + protected:
1.63 +
1.64 + int _id;
1.65 + explicit Edge(int id) { _id = id;}
1.66 +
1.67 + public:
1.68 + Edge() {}
1.69 + Edge (Invalid) { _id = -1; }
1.70 + bool operator==(const Edge& arc) const {return _id == arc._id;}
1.71 + bool operator!=(const Edge& arc) const {return _id != arc._id;}
1.72 + bool operator<(const Edge& arc) const {return _id < arc._id;}
1.73 + };
1.74 +
1.75 + class Arc {
1.76 + friend class SmartBpGraphBase;
1.77 + protected:
1.78 +
1.79 + int _id;
1.80 + explicit Arc(int id) { _id = id;}
1.81 +
1.82 + public:
1.83 + operator Edge() const {
1.84 + return _id != -1 ? edgeFromId(_id / 2) : INVALID;
1.85 + }
1.86 +
1.87 + Arc() {}
1.88 + Arc (Invalid) { _id = -1; }
1.89 + bool operator==(const Arc& arc) const {return _id == arc._id;}
1.90 + bool operator!=(const Arc& arc) const {return _id != arc._id;}
1.91 + bool operator<(const Arc& arc) const {return _id < arc._id;}
1.92 + };
1.93 +
1.94 +
1.95 +
1.96 + SmartBpGraphBase()
1.97 + : nodes(), arcs(), first_red(-1), first_blue(-1) {}
1.98 +
1.99 + typedef True NodeNumTag;
1.100 + typedef True EdgeNumTag;
1.101 + typedef True ArcNumTag;
1.102 +
1.103 + int nodeNum() const { return nodes.size(); }
1.104 + int redNum() const {
1.105 + return first_red == -1 ? 0 : nodes[first_red].partition_index + 1;
1.106 + }
1.107 + int blueNum() const {
1.108 + return first_blue == -1 ? 0 : nodes[first_blue].partition_index + 1;
1.109 + }
1.110 + int edgeNum() const { return arcs.size() / 2; }
1.111 + int arcNum() const { return arcs.size(); }
1.112 +
1.113 + int maxNodeId() const { return nodes.size()-1; }
1.114 + int maxRedId() const {
1.115 + return first_red == -1 ? -1 : nodes[first_red].partition_index;
1.116 + }
1.117 + int maxBlueId() const {
1.118 + return first_blue == -1 ? -1 : nodes[first_blue].partition_index;
1.119 + }
1.120 + int maxEdgeId() const { return arcs.size() / 2 - 1; }
1.121 + int maxArcId() const { return arcs.size()-1; }
1.122 +
1.123 + bool red(Node n) const { return nodes[n._id].red; }
1.124 + bool blue(Node n) const { return !nodes[n._id].red; }
1.125 +
1.126 + Node source(Arc a) const { return Node(arcs[a._id ^ 1].target); }
1.127 + Node target(Arc a) const { return Node(arcs[a._id].target); }
1.128 +
1.129 + Node redNode(Edge e) const { return Node(arcs[2 * e._id].target); }
1.130 + Node blueNode(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
1.131 +
1.132 + Node u(Edge e) const { return redNode(e); }
1.133 + Node v(Edge e) const { return blueNode(e); }
1.134 +
1.135 + static bool direction(Arc a) {
1.136 + return (a._id & 1) == 1;
1.137 + }
1.138 +
1.139 + static Arc direct(Edge e, bool d) {
1.140 + return Arc(e._id * 2 + (d ? 1 : 0));
1.141 + }
1.142 +
1.143 + void first(Node& node) const {
1.144 + node._id = nodes.size() - 1;
1.145 + }
1.146 +
1.147 + static void next(Node& node) {
1.148 + --node._id;
1.149 + }
1.150 +
1.151 + void firstRed(Node& node) const {
1.152 + node._id = first_red;
1.153 + }
1.154 +
1.155 + void nextRed(Node& node) const {
1.156 + node._id = nodes[node._id].partition_next;
1.157 + }
1.158 +
1.159 + void firstBlue(Node& node) const {
1.160 + node._id = first_blue;
1.161 + }
1.162 +
1.163 + void nextBlue(Node& node) const {
1.164 + node._id = nodes[node._id].partition_next;
1.165 + }
1.166 +
1.167 + void first(Arc& arc) const {
1.168 + arc._id = arcs.size() - 1;
1.169 + }
1.170 +
1.171 + static void next(Arc& arc) {
1.172 + --arc._id;
1.173 + }
1.174 +
1.175 + void first(Edge& arc) const {
1.176 + arc._id = arcs.size() / 2 - 1;
1.177 + }
1.178 +
1.179 + static void next(Edge& arc) {
1.180 + --arc._id;
1.181 + }
1.182 +
1.183 + void firstOut(Arc &arc, const Node& v) const {
1.184 + arc._id = nodes[v._id].first_out;
1.185 + }
1.186 + void nextOut(Arc &arc) const {
1.187 + arc._id = arcs[arc._id].next_out;
1.188 + }
1.189 +
1.190 + void firstIn(Arc &arc, const Node& v) const {
1.191 + arc._id = ((nodes[v._id].first_out) ^ 1);
1.192 + if (arc._id == -2) arc._id = -1;
1.193 + }
1.194 + void nextIn(Arc &arc) const {
1.195 + arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
1.196 + if (arc._id == -2) arc._id = -1;
1.197 + }
1.198 +
1.199 + void firstInc(Edge &arc, bool& d, const Node& v) const {
1.200 + int de = nodes[v._id].first_out;
1.201 + if (de != -1) {
1.202 + arc._id = de / 2;
1.203 + d = ((de & 1) == 1);
1.204 + } else {
1.205 + arc._id = -1;
1.206 + d = true;
1.207 + }
1.208 + }
1.209 + void nextInc(Edge &arc, bool& d) const {
1.210 + int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
1.211 + if (de != -1) {
1.212 + arc._id = de / 2;
1.213 + d = ((de & 1) == 1);
1.214 + } else {
1.215 + arc._id = -1;
1.216 + d = true;
1.217 + }
1.218 + }
1.219 +
1.220 + static int id(Node v) { return v._id; }
1.221 + int redId(Node v) const {
1.222 + LEMON_DEBUG(nodes[v._id].red, "Node has to be red");
1.223 + return nodes[v._id].partition_index;
1.224 + }
1.225 + int blueId(Node v) const {
1.226 + LEMON_DEBUG(nodes[v._id].red, "Node has to be blue");
1.227 + return nodes[v._id].partition_index;
1.228 + }
1.229 + static int id(Arc e) { return e._id; }
1.230 + static int id(Edge e) { return e._id; }
1.231 +
1.232 + static Node nodeFromId(int id) { return Node(id);}
1.233 + static Arc arcFromId(int id) { return Arc(id);}
1.234 + static Edge edgeFromId(int id) { return Edge(id);}
1.235 +
1.236 + bool valid(Node n) const {
1.237 + return n._id >= 0 && n._id < static_cast<int>(nodes.size());
1.238 + }
1.239 + bool valid(Arc a) const {
1.240 + return a._id >= 0 && a._id < static_cast<int>(arcs.size());
1.241 + }
1.242 + bool valid(Edge e) const {
1.243 + return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
1.244 + }
1.245 +
1.246 + Node addRedNode() {
1.247 + int n = nodes.size();
1.248 + nodes.push_back(NodeT());
1.249 + nodes[n].first_out = -1;
1.250 + nodes[n].red = true;
1.251 + if (first_red == -1) {
1.252 + nodes[n].partition_index = 0;
1.253 + } else {
1.254 + nodes[n].partition_index = nodes[first_red].partition_index + 1;
1.255 + }
1.256 + nodes[n].partition_next = first_red;
1.257 + first_red = n;
1.258 +
1.259 + return Node(n);
1.260 + }
1.261 +
1.262 + Node addBlueNode() {
1.263 + int n = nodes.size();
1.264 + nodes.push_back(NodeT());
1.265 + nodes[n].first_out = -1;
1.266 + nodes[n].red = false;
1.267 + if (first_blue == -1) {
1.268 + nodes[n].partition_index = 0;
1.269 + } else {
1.270 + nodes[n].partition_index = nodes[first_blue].partition_index + 1;
1.271 + }
1.272 + nodes[n].partition_next = first_blue;
1.273 + first_blue = n;
1.274 +
1.275 + return Node(n);
1.276 + }
1.277 +
1.278 + Edge addEdge(Node u, Node v) {
1.279 + int n = arcs.size();
1.280 + arcs.push_back(ArcT());
1.281 + arcs.push_back(ArcT());
1.282 +
1.283 + arcs[n].target = u._id;
1.284 + arcs[n | 1].target = v._id;
1.285 +
1.286 + arcs[n].next_out = nodes[v._id].first_out;
1.287 + nodes[v._id].first_out = n;
1.288 +
1.289 + arcs[n | 1].next_out = nodes[u._id].first_out;
1.290 + nodes[u._id].first_out = (n | 1);
1.291 +
1.292 + return Edge(n / 2);
1.293 + }
1.294 +
1.295 + void clear() {
1.296 + arcs.clear();
1.297 + nodes.clear();
1.298 + first_red = -1;
1.299 + first_blue = -1;
1.300 + }
1.301 +
1.302 + };
1.303 +
1.304 + typedef BpGraphExtender<SmartBpGraphBase> ExtendedSmartBpGraphBase;
1.305 +
1.306 + /// \ingroup graphs
1.307 + ///
1.308 + /// \brief A smart undirected graph class.
1.309 + ///
1.310 + /// \ref SmartBpGraph is a simple and fast graph implementation.
1.311 + /// It is also quite memory efficient but at the price
1.312 + /// that it does not support node and edge deletion
1.313 + /// (except for the Snapshot feature).
1.314 + ///
1.315 + /// This type fully conforms to the \ref concepts::Graph "Graph concept"
1.316 + /// and it also provides some additional functionalities.
1.317 + /// Most of its member functions and nested classes are documented
1.318 + /// only in the concept class.
1.319 + ///
1.320 + /// This class provides constant time counting for nodes, edges and arcs.
1.321 + ///
1.322 + /// \sa concepts::Graph
1.323 + /// \sa SmartDigraph
1.324 + class SmartBpGraph : public ExtendedSmartBpGraphBase {
1.325 + typedef ExtendedSmartBpGraphBase Parent;
1.326 +
1.327 + private:
1.328 + /// Graphs are \e not copy constructible. Use GraphCopy instead.
1.329 + SmartBpGraph(const SmartBpGraph &) : ExtendedSmartBpGraphBase() {};
1.330 + /// \brief Assignment of a graph to another one is \e not allowed.
1.331 + /// Use GraphCopy instead.
1.332 + void operator=(const SmartBpGraph &) {}
1.333 +
1.334 + public:
1.335 +
1.336 + /// Constructor
1.337 +
1.338 + /// Constructor.
1.339 + ///
1.340 + SmartBpGraph() {}
1.341 +
1.342 + /// \brief Add a new red node to the graph.
1.343 + ///
1.344 + /// This function adds a red new node to the graph.
1.345 + /// \return The new node.
1.346 + Node addRedNode() { return Parent::addRedNode(); }
1.347 +
1.348 + /// \brief Add a new blue node to the graph.
1.349 + ///
1.350 + /// This function adds a blue new node to the graph.
1.351 + /// \return The new node.
1.352 + Node addBlueNode() { return Parent::addBlueNode(); }
1.353 +
1.354 + /// \brief Add a new edge to the graph.
1.355 + ///
1.356 + /// This function adds a new edge to the graph between nodes
1.357 + /// \c u and \c v with inherent orientation from node \c u to
1.358 + /// node \c v.
1.359 + /// \return The new edge.
1.360 + Edge addEdge(Node red, Node blue) {
1.361 + LEMON_DEBUG(Parent::red(red) && Parent::blue(blue),
1.362 + "Edge has to be formed by a red and a blue nodes");
1.363 + return Parent::addEdge(red, blue);
1.364 + }
1.365 +
1.366 + /// \brief Node validity check
1.367 + ///
1.368 + /// This function gives back \c true if the given node is valid,
1.369 + /// i.e. it is a real node of the graph.
1.370 + ///
1.371 + /// \warning A removed node (using Snapshot) could become valid again
1.372 + /// if new nodes are added to the graph.
1.373 + bool valid(Node n) const { return Parent::valid(n); }
1.374 +
1.375 + /// \brief Edge validity check
1.376 + ///
1.377 + /// This function gives back \c true if the given edge is valid,
1.378 + /// i.e. it is a real edge of the graph.
1.379 + ///
1.380 + /// \warning A removed edge (using Snapshot) could become valid again
1.381 + /// if new edges are added to the graph.
1.382 + bool valid(Edge e) const { return Parent::valid(e); }
1.383 +
1.384 + /// \brief Arc validity check
1.385 + ///
1.386 + /// This function gives back \c true if the given arc is valid,
1.387 + /// i.e. it is a real arc of the graph.
1.388 + ///
1.389 + /// \warning A removed arc (using Snapshot) could become valid again
1.390 + /// if new edges are added to the graph.
1.391 + bool valid(Arc a) const { return Parent::valid(a); }
1.392 +
1.393 + ///Clear the graph.
1.394 +
1.395 + ///This function erases all nodes and arcs from the graph.
1.396 + ///
1.397 + void clear() {
1.398 + Parent::clear();
1.399 + }
1.400 +
1.401 + /// Reserve memory for nodes.
1.402 +
1.403 + /// Using this function, it is possible to avoid superfluous memory
1.404 + /// allocation: if you know that the graph you want to build will
1.405 + /// be large (e.g. it will contain millions of nodes and/or edges),
1.406 + /// then it is worth reserving space for this amount before starting
1.407 + /// to build the graph.
1.408 + /// \sa reserveEdge()
1.409 + void reserveNode(int n) { nodes.reserve(n); };
1.410 +
1.411 + /// Reserve memory for edges.
1.412 +
1.413 + /// Using this function, it is possible to avoid superfluous memory
1.414 + /// allocation: if you know that the graph you want to build will
1.415 + /// be large (e.g. it will contain millions of nodes and/or edges),
1.416 + /// then it is worth reserving space for this amount before starting
1.417 + /// to build the graph.
1.418 + /// \sa reserveNode()
1.419 + void reserveEdge(int m) { arcs.reserve(2 * m); };
1.420 +
1.421 + public:
1.422 +
1.423 + class Snapshot;
1.424 +
1.425 + protected:
1.426 +
1.427 + void saveSnapshot(Snapshot &s)
1.428 + {
1.429 + s._graph = this;
1.430 + s.node_num = nodes.size();
1.431 + s.arc_num = arcs.size();
1.432 + }
1.433 +
1.434 + void restoreSnapshot(const Snapshot &s)
1.435 + {
1.436 + while(s.arc_num<arcs.size()) {
1.437 + int n=arcs.size()-1;
1.438 + Edge arc=edgeFromId(n/2);
1.439 + Parent::notifier(Edge()).erase(arc);
1.440 + std::vector<Arc> dir;
1.441 + dir.push_back(arcFromId(n));
1.442 + dir.push_back(arcFromId(n-1));
1.443 + Parent::notifier(Arc()).erase(dir);
1.444 + nodes[arcs[n-1].target].first_out=arcs[n].next_out;
1.445 + nodes[arcs[n].target].first_out=arcs[n-1].next_out;
1.446 + arcs.pop_back();
1.447 + arcs.pop_back();
1.448 + }
1.449 + while(s.node_num<nodes.size()) {
1.450 + int n=nodes.size()-1;
1.451 + Node node = nodeFromId(n);
1.452 + if (Parent::red(node)) {
1.453 + first_red = nodes[n].partition_next;
1.454 + Parent::notifier(RedNode()).erase(node);
1.455 + } else {
1.456 + first_blue = nodes[n].partition_next;
1.457 + Parent::notifier(BlueNode()).erase(node);
1.458 + }
1.459 + Parent::notifier(Node()).erase(node);
1.460 + nodes.pop_back();
1.461 + }
1.462 + }
1.463 +
1.464 + public:
1.465 +
1.466 + ///Class to make a snapshot of the graph and to restore it later.
1.467 +
1.468 + ///Class to make a snapshot of the graph and to restore it later.
1.469 + ///
1.470 + ///The newly added nodes and edges can be removed using the
1.471 + ///restore() function. This is the only way for deleting nodes and/or
1.472 + ///edges from a SmartBpGraph structure.
1.473 + ///
1.474 + ///\note After a state is restored, you cannot restore a later state,
1.475 + ///i.e. you cannot add the removed nodes and edges again using
1.476 + ///another Snapshot instance.
1.477 + ///
1.478 + ///\warning The validity of the snapshot is not stored due to
1.479 + ///performance reasons. If you do not use the snapshot correctly,
1.480 + ///it can cause broken program, invalid or not restored state of
1.481 + ///the graph or no change.
1.482 + class Snapshot
1.483 + {
1.484 + SmartBpGraph *_graph;
1.485 + protected:
1.486 + friend class SmartBpGraph;
1.487 + unsigned int node_num;
1.488 + unsigned int arc_num;
1.489 + public:
1.490 + ///Default constructor.
1.491 +
1.492 + ///Default constructor.
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 given graph.
1.498 + ///
1.499 + Snapshot(SmartBpGraph &gr) {
1.500 + gr.saveSnapshot(*this);
1.501 + }
1.502 +
1.503 + ///Make a snapshot.
1.504 +
1.505 + ///This function makes a snapshot of the given graph.
1.506 + ///It can be called more than once. In case of a repeated
1.507 + ///call, the previous snapshot gets lost.
1.508 + void save(SmartBpGraph &gr)
1.509 + {
1.510 + gr.saveSnapshot(*this);
1.511 + }
1.512 +
1.513 + ///Undo the changes until the last snapshot.
1.514 +
1.515 + ///This function undos the changes until the last snapshot
1.516 + ///created by save() or Snapshot(SmartBpGraph&).
1.517 + void restore()
1.518 + {
1.519 + _graph->restoreSnapshot(*this);
1.520 + }
1.521 + };
1.522 + };
1.523 +
1.524 } //namespace lemon
1.525
1.526