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