1.1 --- a/lemon/full_graph.h Mon Jul 16 16:21:40 2018 +0200
1.2 +++ b/lemon/full_graph.h Wed Oct 17 19:14:07 2018 +0200
1.3 @@ -2,7 +2,7 @@
1.4 *
1.5 * This file is a part of LEMON, a generic C++ optimization library.
1.6 *
1.7 - * Copyright (C) 2003-2010
1.8 + * Copyright (C) 2003-2013
1.9 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 *
1.12 @@ -621,6 +621,460 @@
1.13
1.14 };
1.15
1.16 + class FullBpGraphBase {
1.17 +
1.18 + protected:
1.19 +
1.20 + int _red_num, _blue_num;
1.21 + int _node_num, _edge_num;
1.22 +
1.23 + public:
1.24 +
1.25 + typedef FullBpGraphBase Graph;
1.26 +
1.27 + class Node;
1.28 + class Arc;
1.29 + class Edge;
1.30 +
1.31 + class Node {
1.32 + friend class FullBpGraphBase;
1.33 + protected:
1.34 +
1.35 + int _id;
1.36 + explicit Node(int id) { _id = id;}
1.37 +
1.38 + public:
1.39 + Node() {}
1.40 + Node (Invalid) { _id = -1; }
1.41 + bool operator==(const Node& node) const {return _id == node._id;}
1.42 + bool operator!=(const Node& node) const {return _id != node._id;}
1.43 + bool operator<(const Node& node) const {return _id < node._id;}
1.44 + };
1.45 +
1.46 + class RedNode : public Node {
1.47 + friend class FullBpGraphBase;
1.48 + protected:
1.49 +
1.50 + explicit RedNode(int pid) : Node(pid) {}
1.51 +
1.52 + public:
1.53 + RedNode() {}
1.54 + RedNode(const RedNode& node) : Node(node) {}
1.55 + RedNode(Invalid) : Node(INVALID){}
1.56 + };
1.57 +
1.58 + class BlueNode : public Node {
1.59 + friend class FullBpGraphBase;
1.60 + protected:
1.61 +
1.62 + explicit BlueNode(int pid) : Node(pid) {}
1.63 +
1.64 + public:
1.65 + BlueNode() {}
1.66 + BlueNode(const BlueNode& node) : Node(node) {}
1.67 + BlueNode(Invalid) : Node(INVALID){}
1.68 + };
1.69 +
1.70 + class Edge {
1.71 + friend class FullBpGraphBase;
1.72 + protected:
1.73 +
1.74 + int _id;
1.75 + explicit Edge(int id) { _id = id;}
1.76 +
1.77 + public:
1.78 + Edge() {}
1.79 + Edge (Invalid) { _id = -1; }
1.80 + bool operator==(const Edge& arc) const {return _id == arc._id;}
1.81 + bool operator!=(const Edge& arc) const {return _id != arc._id;}
1.82 + bool operator<(const Edge& arc) const {return _id < arc._id;}
1.83 + };
1.84 +
1.85 + class Arc {
1.86 + friend class FullBpGraphBase;
1.87 + protected:
1.88 +
1.89 + int _id;
1.90 + explicit Arc(int id) { _id = id;}
1.91 +
1.92 + public:
1.93 + operator Edge() const {
1.94 + return _id != -1 ? edgeFromId(_id / 2) : INVALID;
1.95 + }
1.96 +
1.97 + Arc() {}
1.98 + Arc (Invalid) { _id = -1; }
1.99 + bool operator==(const Arc& arc) const {return _id == arc._id;}
1.100 + bool operator!=(const Arc& arc) const {return _id != arc._id;}
1.101 + bool operator<(const Arc& arc) const {return _id < arc._id;}
1.102 + };
1.103 +
1.104 +
1.105 + protected:
1.106 +
1.107 + FullBpGraphBase()
1.108 + : _red_num(0), _blue_num(0), _node_num(0), _edge_num(0) {}
1.109 +
1.110 + void construct(int redNum, int blueNum) {
1.111 + _red_num = redNum; _blue_num = blueNum;
1.112 + _node_num = redNum + blueNum; _edge_num = redNum * blueNum;
1.113 + }
1.114 +
1.115 + public:
1.116 +
1.117 + typedef True NodeNumTag;
1.118 + typedef True EdgeNumTag;
1.119 + typedef True ArcNumTag;
1.120 +
1.121 + int nodeNum() const { return _node_num; }
1.122 + int redNum() const { return _red_num; }
1.123 + int blueNum() const { return _blue_num; }
1.124 + int edgeNum() const { return _edge_num; }
1.125 + int arcNum() const { return 2 * _edge_num; }
1.126 +
1.127 + int maxNodeId() const { return _node_num - 1; }
1.128 + int maxRedId() const { return _red_num - 1; }
1.129 + int maxBlueId() const { return _blue_num - 1; }
1.130 + int maxEdgeId() const { return _edge_num - 1; }
1.131 + int maxArcId() const { return 2 * _edge_num - 1; }
1.132 +
1.133 + bool red(Node n) const { return n._id < _red_num; }
1.134 + bool blue(Node n) const { return n._id >= _red_num; }
1.135 +
1.136 + static RedNode asRedNodeUnsafe(Node n) { return RedNode(n._id); }
1.137 + static BlueNode asBlueNodeUnsafe(Node n) { return BlueNode(n._id); }
1.138 +
1.139 + Node source(Arc a) const {
1.140 + if (a._id & 1) {
1.141 + return Node((a._id >> 1) % _red_num);
1.142 + } else {
1.143 + return Node((a._id >> 1) / _red_num + _red_num);
1.144 + }
1.145 + }
1.146 + Node target(Arc a) const {
1.147 + if (a._id & 1) {
1.148 + return Node((a._id >> 1) / _red_num + _red_num);
1.149 + } else {
1.150 + return Node((a._id >> 1) % _red_num);
1.151 + }
1.152 + }
1.153 +
1.154 + RedNode redNode(Edge e) const {
1.155 + return RedNode(e._id % _red_num);
1.156 + }
1.157 + BlueNode blueNode(Edge e) const {
1.158 + return BlueNode(e._id / _red_num + _red_num);
1.159 + }
1.160 +
1.161 + static bool direction(Arc a) {
1.162 + return (a._id & 1) == 1;
1.163 + }
1.164 +
1.165 + static Arc direct(Edge e, bool d) {
1.166 + return Arc(e._id * 2 + (d ? 1 : 0));
1.167 + }
1.168 +
1.169 + void first(Node& node) const {
1.170 + node._id = _node_num - 1;
1.171 + }
1.172 +
1.173 + static void next(Node& node) {
1.174 + --node._id;
1.175 + }
1.176 +
1.177 + void first(RedNode& node) const {
1.178 + node._id = _red_num - 1;
1.179 + }
1.180 +
1.181 + static void next(RedNode& node) {
1.182 + --node._id;
1.183 + }
1.184 +
1.185 + void first(BlueNode& node) const {
1.186 + if (_red_num == _node_num) node._id = -1;
1.187 + else node._id = _node_num - 1;
1.188 + }
1.189 +
1.190 + void next(BlueNode& node) const {
1.191 + if (node._id == _red_num) node._id = -1;
1.192 + else --node._id;
1.193 + }
1.194 +
1.195 + void first(Arc& arc) const {
1.196 + arc._id = 2 * _edge_num - 1;
1.197 + }
1.198 +
1.199 + static void next(Arc& arc) {
1.200 + --arc._id;
1.201 + }
1.202 +
1.203 + void first(Edge& arc) const {
1.204 + arc._id = _edge_num - 1;
1.205 + }
1.206 +
1.207 + static void next(Edge& arc) {
1.208 + --arc._id;
1.209 + }
1.210 +
1.211 + void firstOut(Arc &a, const Node& v) const {
1.212 + if (v._id < _red_num) {
1.213 + a._id = 2 * (v._id + _red_num * (_blue_num - 1)) + 1;
1.214 + } else {
1.215 + a._id = 2 * (_red_num - 1 + _red_num * (v._id - _red_num));
1.216 + }
1.217 + }
1.218 + void nextOut(Arc &a) const {
1.219 + if (a._id & 1) {
1.220 + a._id -= 2 * _red_num;
1.221 + if (a._id < 0) a._id = -1;
1.222 + } else {
1.223 + if (a._id % (2 * _red_num) == 0) a._id = -1;
1.224 + else a._id -= 2;
1.225 + }
1.226 + }
1.227 +
1.228 + void firstIn(Arc &a, const Node& v) const {
1.229 + if (v._id < _red_num) {
1.230 + a._id = 2 * (v._id + _red_num * (_blue_num - 1));
1.231 + } else {
1.232 + a._id = 2 * (_red_num - 1 + _red_num * (v._id - _red_num)) + 1;
1.233 + }
1.234 + }
1.235 + void nextIn(Arc &a) const {
1.236 + if (a._id & 1) {
1.237 + if (a._id % (2 * _red_num) == 1) a._id = -1;
1.238 + else a._id -= 2;
1.239 + } else {
1.240 + a._id -= 2 * _red_num;
1.241 + if (a._id < 0) a._id = -1;
1.242 + }
1.243 + }
1.244 +
1.245 + void firstInc(Edge &e, bool& d, const Node& v) const {
1.246 + if (v._id < _red_num) {
1.247 + d = true;
1.248 + e._id = v._id + _red_num * (_blue_num - 1);
1.249 + } else {
1.250 + d = false;
1.251 + e._id = _red_num - 1 + _red_num * (v._id - _red_num);
1.252 + }
1.253 + }
1.254 + void nextInc(Edge &e, bool& d) const {
1.255 + if (d) {
1.256 + e._id -= _red_num;
1.257 + if (e._id < 0) e._id = -1;
1.258 + } else {
1.259 + if (e._id % _red_num == 0) e._id = -1;
1.260 + else --e._id;
1.261 + }
1.262 + }
1.263 +
1.264 + static int id(const Node& v) { return v._id; }
1.265 + int id(const RedNode& v) const { return v._id; }
1.266 + int id(const BlueNode& v) const { return v._id - _red_num; }
1.267 + static int id(Arc e) { return e._id; }
1.268 + static int id(Edge e) { return e._id; }
1.269 +
1.270 + static Node nodeFromId(int id) { return Node(id);}
1.271 + static Arc arcFromId(int id) { return Arc(id);}
1.272 + static Edge edgeFromId(int id) { return Edge(id);}
1.273 +
1.274 + bool valid(Node n) const {
1.275 + return n._id >= 0 && n._id < _node_num;
1.276 + }
1.277 + bool valid(Arc a) const {
1.278 + return a._id >= 0 && a._id < 2 * _edge_num;
1.279 + }
1.280 + bool valid(Edge e) const {
1.281 + return e._id >= 0 && e._id < _edge_num;
1.282 + }
1.283 +
1.284 + RedNode redNode(int index) const {
1.285 + return RedNode(index);
1.286 + }
1.287 +
1.288 + int index(RedNode n) const {
1.289 + return n._id;
1.290 + }
1.291 +
1.292 + BlueNode blueNode(int index) const {
1.293 + return BlueNode(index + _red_num);
1.294 + }
1.295 +
1.296 + int index(BlueNode n) const {
1.297 + return n._id - _red_num;
1.298 + }
1.299 +
1.300 + void clear() {
1.301 + _red_num = 0; _blue_num = 0;
1.302 + _node_num = 0; _edge_num = 0;
1.303 + }
1.304 +
1.305 + Edge edge(const Node& u, const Node& v) const {
1.306 + if (u._id < _red_num) {
1.307 + if (v._id < _red_num) {
1.308 + return Edge(-1);
1.309 + } else {
1.310 + return Edge(u._id + _red_num * (v._id - _red_num));
1.311 + }
1.312 + } else {
1.313 + if (v._id < _red_num) {
1.314 + return Edge(v._id + _red_num * (u._id - _red_num));
1.315 + } else {
1.316 + return Edge(-1);
1.317 + }
1.318 + }
1.319 + }
1.320 +
1.321 + Arc arc(const Node& u, const Node& v) const {
1.322 + if (u._id < _red_num) {
1.323 + if (v._id < _red_num) {
1.324 + return Arc(-1);
1.325 + } else {
1.326 + return Arc(2 * (u._id + _red_num * (v._id - _red_num)) + 1);
1.327 + }
1.328 + } else {
1.329 + if (v._id < _red_num) {
1.330 + return Arc(2 * (v._id + _red_num * (u._id - _red_num)));
1.331 + } else {
1.332 + return Arc(-1);
1.333 + }
1.334 + }
1.335 + }
1.336 +
1.337 + typedef True FindEdgeTag;
1.338 + typedef True FindArcTag;
1.339 +
1.340 + Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
1.341 + return prev != INVALID ? INVALID : edge(u, v);
1.342 + }
1.343 +
1.344 + Arc findArc(Node s, Node t, Arc prev = INVALID) const {
1.345 + return prev != INVALID ? INVALID : arc(s, t);
1.346 + }
1.347 +
1.348 + };
1.349 +
1.350 + typedef BpGraphExtender<FullBpGraphBase> ExtendedFullBpGraphBase;
1.351 +
1.352 + /// \ingroup graphs
1.353 + ///
1.354 + /// \brief An undirected full bipartite graph class.
1.355 + ///
1.356 + /// FullBpGraph is a simple and fast implmenetation of undirected
1.357 + /// full bipartite graphs. It contains an edge between every
1.358 + /// red-blue pairs of nodes, therefore the number of edges is
1.359 + /// <tt>nr*nb</tt>. This class is completely static and it needs
1.360 + /// constant memory space. Thus you can neither add nor delete
1.361 + /// nodes or edges, however the structure can be resized using
1.362 + /// resize().
1.363 + ///
1.364 + /// This type fully conforms to the \ref concepts::BpGraph "BpGraph concept".
1.365 + /// Most of its member functions and nested classes are documented
1.366 + /// only in the concept class.
1.367 + ///
1.368 + /// This class provides constant time counting for nodes, edges and arcs.
1.369 + ///
1.370 + /// \sa FullGraph
1.371 + class FullBpGraph : public ExtendedFullBpGraphBase {
1.372 + public:
1.373 +
1.374 + typedef ExtendedFullBpGraphBase Parent;
1.375 +
1.376 + /// \brief Default constructor.
1.377 + ///
1.378 + /// Default constructor. The number of nodes and edges will be zero.
1.379 + FullBpGraph() { construct(0, 0); }
1.380 +
1.381 + /// \brief Constructor
1.382 + ///
1.383 + /// Constructor.
1.384 + /// \param redNum The number of the red nodes.
1.385 + /// \param blueNum The number of the blue nodes.
1.386 + FullBpGraph(int redNum, int blueNum) { construct(redNum, blueNum); }
1.387 +
1.388 + /// \brief Resizes the graph
1.389 + ///
1.390 + /// This function resizes the graph. It fully destroys and
1.391 + /// rebuilds the structure, therefore the maps of the graph will be
1.392 + /// reallocated automatically and the previous values will be lost.
1.393 + void resize(int redNum, int blueNum) {
1.394 + Parent::notifier(Arc()).clear();
1.395 + Parent::notifier(Edge()).clear();
1.396 + Parent::notifier(Node()).clear();
1.397 + Parent::notifier(BlueNode()).clear();
1.398 + Parent::notifier(RedNode()).clear();
1.399 + construct(redNum, blueNum);
1.400 + Parent::notifier(RedNode()).build();
1.401 + Parent::notifier(BlueNode()).build();
1.402 + Parent::notifier(Node()).build();
1.403 + Parent::notifier(Edge()).build();
1.404 + Parent::notifier(Arc()).build();
1.405 + }
1.406 +
1.407 + using Parent::redNode;
1.408 + using Parent::blueNode;
1.409 +
1.410 + /// \brief Returns the red node with the given index.
1.411 + ///
1.412 + /// Returns the red node with the given index. Since this
1.413 + /// structure is completely static, the red nodes can be indexed
1.414 + /// with integers from the range <tt>[0..redNum()-1]</tt>.
1.415 + /// \sa redIndex()
1.416 + RedNode redNode(int index) const { return Parent::redNode(index); }
1.417 +
1.418 + /// \brief Returns the index of the given red node.
1.419 + ///
1.420 + /// Returns the index of the given red node. Since this structure
1.421 + /// is completely static, the red nodes can be indexed with
1.422 + /// integers from the range <tt>[0..redNum()-1]</tt>.
1.423 + ///
1.424 + /// \sa operator()()
1.425 + int index(RedNode node) const { return Parent::index(node); }
1.426 +
1.427 + /// \brief Returns the blue node with the given index.
1.428 + ///
1.429 + /// Returns the blue node with the given index. Since this
1.430 + /// structure is completely static, the blue nodes can be indexed
1.431 + /// with integers from the range <tt>[0..blueNum()-1]</tt>.
1.432 + /// \sa blueIndex()
1.433 + BlueNode blueNode(int index) const { return Parent::blueNode(index); }
1.434 +
1.435 + /// \brief Returns the index of the given blue node.
1.436 + ///
1.437 + /// Returns the index of the given blue node. Since this structure
1.438 + /// is completely static, the blue nodes can be indexed with
1.439 + /// integers from the range <tt>[0..blueNum()-1]</tt>.
1.440 + ///
1.441 + /// \sa operator()()
1.442 + int index(BlueNode node) const { return Parent::index(node); }
1.443 +
1.444 + /// \brief Returns the edge which connects the given nodes.
1.445 + ///
1.446 + /// Returns the edge which connects the given nodes.
1.447 + Edge edge(const Node& u, const Node& v) const {
1.448 + return Parent::edge(u, v);
1.449 + }
1.450 +
1.451 + /// \brief Returns the arc which connects the given nodes.
1.452 + ///
1.453 + /// Returns the arc which connects the given nodes.
1.454 + Arc arc(const Node& u, const Node& v) const {
1.455 + return Parent::arc(u, v);
1.456 + }
1.457 +
1.458 + /// \brief Number of nodes.
1.459 + int nodeNum() const { return Parent::nodeNum(); }
1.460 + /// \brief Number of red nodes.
1.461 + int redNum() const { return Parent::redNum(); }
1.462 + /// \brief Number of blue nodes.
1.463 + int blueNum() const { return Parent::blueNum(); }
1.464 + /// \brief Number of arcs.
1.465 + int arcNum() const { return Parent::arcNum(); }
1.466 + /// \brief Number of edges.
1.467 + int edgeNum() const { return Parent::edgeNum(); }
1.468 + };
1.469 +
1.470
1.471 } //namespace lemon
1.472