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