lemon/full_graph.h
author deba
Thu, 02 Mar 2006 14:22:03 +0000
changeset 1995 c1fc2c14a3ae
parent 1993 2115143eceea
child 1999 2ff283124dfc
permissions -rw-r--r--
Bugfix in bpugraph
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@591
    18
alpar@921
    19
#ifndef LEMON_FULL_GRAPH_H
alpar@921
    20
#define LEMON_FULL_GRAPH_H
alpar@591
    21
deba@983
    22
#include <cmath>
deba@983
    23
klao@946
    24
deba@1791
    25
#include <lemon/bits/graph_extender.h>
deba@1566
    26
deba@1979
    27
deba@1993
    28
#include <lemon/bits/invalid.h>
deba@1993
    29
#include <lemon/bits/utility.h>
klao@977
    30
klao@977
    31
alpar@591
    32
///\ingroup graphs
alpar@591
    33
///\file
klao@1909
    34
///\brief FullGraph and FullUGraph classes.
alpar@591
    35
alpar@591
    36
alpar@921
    37
namespace lemon {
alpar@591
    38
deba@1986
    39
  /// \brief Base of the FullGrpah.
deba@1986
    40
  ///
deba@1986
    41
  /// Base of the FullGrpah.
klao@946
    42
  class FullGraphBase {
deba@1566
    43
    int _nodeNum;
deba@1566
    44
    int _edgeNum;
alpar@591
    45
  public:
deba@782
    46
klao@946
    47
    typedef FullGraphBase Graph;
alpar@591
    48
alpar@591
    49
    class Node;
alpar@591
    50
    class Edge;
deba@782
    51
alpar@591
    52
  public:
alpar@591
    53
klao@946
    54
    FullGraphBase() {}
klao@946
    55
klao@946
    56
alpar@591
    57
    ///Creates a full graph with \c n nodes.
deba@1566
    58
    void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
alpar@591
    59
    
klao@977
    60
    typedef True NodeNumTag;
klao@977
    61
    typedef True EdgeNumTag;
klao@977
    62
deba@1986
    63
    /// \brief Returns the node with the given index.
deba@1986
    64
    ///
deba@1986
    65
    /// Returns the node with the given index. Because it is a
deba@1986
    66
    /// static size graph the node's of the graph can be indiced
deba@1986
    67
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
    68
    /// the node can accessed by the \e index() member.
deba@1986
    69
    Node operator()(int index) const { return Node(index); }
deba@1986
    70
deba@1986
    71
    /// \brief Returns the index of the node.
deba@1986
    72
    ///
deba@1986
    73
    /// Returns the index of the node. Because it is a
deba@1986
    74
    /// static size graph the node's of the graph can be indiced
deba@1986
    75
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
    76
    /// the node can accessed by the \e index() member.
deba@1986
    77
    int index(const Node& node) const { return node.id; }
deba@1986
    78
alpar@813
    79
    ///Number of nodes.
deba@1566
    80
    int nodeNum() const { return _nodeNum; }
alpar@813
    81
    ///Number of edges.
deba@1566
    82
    int edgeNum() const { return _edgeNum; }
alpar@591
    83
alpar@813
    84
    /// Maximum node ID.
alpar@813
    85
    
alpar@813
    86
    /// Maximum node ID.
alpar@813
    87
    ///\sa id(Node)
deba@1791
    88
    int maxNodeId() const { return _nodeNum-1; }
alpar@813
    89
    /// Maximum edge ID.
alpar@813
    90
    
alpar@813
    91
    /// Maximum edge ID.
alpar@813
    92
    ///\sa id(Edge)
deba@1791
    93
    int maxEdgeId() const { return _edgeNum-1; }
alpar@591
    94
deba@1566
    95
    Node source(Edge e) const { return e.id % _nodeNum; }
deba@1566
    96
    Node target(Edge e) const { return e.id / _nodeNum; }
alpar@591
    97
alpar@591
    98
alpar@813
    99
    /// Node ID.
alpar@813
   100
    
alpar@813
   101
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   102
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   103
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   104
    ///
alpar@813
   105
    /// The ID of the \ref INVALID node is -1.
alpar@813
   106
    ///\return The ID of the node \c v. 
klao@946
   107
klao@946
   108
    static int id(Node v) { return v.id; }
alpar@813
   109
    /// Edge ID.
alpar@813
   110
    
alpar@813
   111
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   112
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   113
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   114
    ///
alpar@813
   115
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   116
    ///\return The ID of the edge \c e. 
klao@946
   117
    static int id(Edge e) { return e.id; }
alpar@591
   118
deba@1791
   119
    static Node nodeFromId(int id) { return Node(id);}
deba@1106
   120
    
deba@1791
   121
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   122
deba@1566
   123
    typedef True FindEdgeTag;
deba@1566
   124
alpar@774
   125
    /// Finds an edge between two nodes.
alpar@774
   126
    
alpar@774
   127
    /// Finds an edge from node \c u to node \c v.
alpar@774
   128
    ///
alpar@774
   129
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   130
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   131
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   132
    /// \return The found edge or INVALID if there is no such an edge.
deba@1566
   133
    Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
klao@946
   134
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   135
    }
alpar@774
   136
    
alpar@774
   137
      
alpar@591
   138
    class Node {
klao@946
   139
      friend class FullGraphBase;
alpar@591
   140
alpar@591
   141
    protected:
klao@946
   142
      int id;
alpar@1643
   143
      Node(int _id) : id(_id) {}
alpar@591
   144
    public:
alpar@591
   145
      Node() {}
alpar@1643
   146
      Node (Invalid) : id(-1) {}
klao@946
   147
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   148
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   149
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   150
    };
alpar@591
   151
    
klao@946
   152
klao@946
   153
klao@946
   154
    class Edge {
klao@946
   155
      friend class FullGraphBase;
klao@946
   156
      
klao@946
   157
    protected:
deba@1566
   158
      int id;  // _nodeNum * target + source;
klao@946
   159
klao@946
   160
      Edge(int _id) : id(_id) {}
klao@946
   161
alpar@986
   162
      Edge(const FullGraphBase& _graph, int source, int target) 
deba@1566
   163
	: id(_graph._nodeNum * target+source) {}
alpar@591
   164
    public:
klao@946
   165
      Edge() { }
klao@946
   166
      Edge (Invalid) { id = -1; }
klao@946
   167
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   168
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   169
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   170
    };
alpar@591
   171
klao@946
   172
    void first(Node& node) const {
deba@1566
   173
      node.id = _nodeNum-1;
klao@946
   174
    }
alpar@591
   175
klao@946
   176
    static void next(Node& node) {
klao@946
   177
      --node.id;
klao@946
   178
    }
klao@946
   179
klao@946
   180
    void first(Edge& edge) const {
deba@1566
   181
      edge.id = _edgeNum-1;
klao@946
   182
    }
klao@946
   183
klao@946
   184
    static void next(Edge& edge) {
klao@946
   185
      --edge.id;
klao@946
   186
    }
klao@946
   187
klao@946
   188
    void firstOut(Edge& edge, const Node& node) const {
deba@1566
   189
      edge.id = _edgeNum + node.id - _nodeNum;
klao@946
   190
    }
klao@946
   191
klao@946
   192
    void nextOut(Edge& edge) const {
deba@1566
   193
      edge.id -= _nodeNum;
klao@946
   194
      if (edge.id < 0) edge.id = -1;
klao@946
   195
    }
klao@946
   196
klao@946
   197
    void firstIn(Edge& edge, const Node& node) const {
deba@1566
   198
      edge.id = node.id * _nodeNum;
klao@946
   199
    }
alpar@591
   200
    
klao@946
   201
    void nextIn(Edge& edge) const {
klao@946
   202
      ++edge.id;
deba@1566
   203
      if (edge.id % _nodeNum == 0) edge.id = -1;
klao@946
   204
    }
alpar@591
   205
alpar@591
   206
  };
alpar@591
   207
deba@1979
   208
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
klao@946
   209
deba@1566
   210
  /// \ingroup graphs
alpar@951
   211
  ///
deba@1566
   212
  /// \brief A full graph class.
deba@1566
   213
  ///
deba@1566
   214
  /// This is a simple and fast directed full graph implementation.
deba@1566
   215
  /// It is completely static, so you can neither add nor delete either
deba@1566
   216
  /// edges or nodes.
deba@1566
   217
  /// Thus it conforms to
deba@1566
   218
  /// the \ref concept::StaticGraph "StaticGraph" concept
deba@1566
   219
  /// \sa concept::StaticGraph.
deba@1566
   220
  ///
deba@1986
   221
  /// \sa FullGraphBase
deba@1986
   222
  /// \sa FullUGraph
deba@1986
   223
  ///
deba@1566
   224
  /// \author Alpar Juttner
deba@1669
   225
  class FullGraph : public ExtendedFullGraphBase {
klao@946
   226
  public:
klao@946
   227
deba@1979
   228
    typedef ExtendedFullGraphBase Parent;
deba@1979
   229
deba@1979
   230
    /// \brief Constructor
deba@1987
   231
    FullGraph() { construct(0); }
deba@1987
   232
deba@1987
   233
    /// \brief Constructor
deba@1979
   234
    ///
klao@946
   235
    FullGraph(int n) { construct(n); }
deba@1979
   236
deba@1979
   237
    /// \brief Resize the graph
deba@1979
   238
    ///
deba@1986
   239
    /// Resize the graph. The function will fully destroy and build the graph.
deba@1986
   240
    /// This cause that the maps of the graph will reallocated
deba@1986
   241
    /// automatically and the previous values will be lost.
deba@1979
   242
    void resize(int n) {
deba@1979
   243
      Parent::getNotifier(Edge()).clear();
deba@1979
   244
      Parent::getNotifier(Node()).clear();
deba@1979
   245
      construct(n);
deba@1979
   246
      Parent::getNotifier(Node()).build();
deba@1979
   247
      Parent::getNotifier(Edge()).build();
deba@1979
   248
    }
klao@946
   249
  };
klao@946
   250
deba@983
   251
deba@1986
   252
  /// \brief Base of the FullUGrpah.
deba@1986
   253
  ///
deba@1986
   254
  /// Base of the FullUGrpah.
klao@1909
   255
  class FullUGraphBase {
deba@1566
   256
    int _nodeNum;
deba@1566
   257
    int _edgeNum;
deba@983
   258
  public:
deba@983
   259
klao@1909
   260
    typedef FullUGraphBase Graph;
deba@983
   261
deba@983
   262
    class Node;
deba@983
   263
    class Edge;
deba@983
   264
deba@983
   265
  public:
deba@983
   266
klao@1909
   267
    FullUGraphBase() {}
deba@983
   268
deba@983
   269
deba@983
   270
    ///Creates a full graph with \c n nodes.
deba@1566
   271
    void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
deba@1986
   272
deba@1986
   273
    /// \brief Returns the node with the given index.
deba@983
   274
    ///
deba@1986
   275
    /// Returns the node with the given index. Because it is a
deba@1986
   276
    /// static size graph the node's of the graph can be indiced
deba@1986
   277
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
   278
    /// the node can accessed by the \e index() member.
deba@1986
   279
    Node operator()(int index) const { return Node(index); }
deba@1986
   280
deba@1986
   281
    /// \brief Returns the index of the node.
deba@1986
   282
    ///
deba@1986
   283
    /// Returns the index of the node. Because it is a
deba@1986
   284
    /// static size graph the node's of the graph can be indiced
deba@1986
   285
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
   286
    /// the node can accessed by the \e index() member.
deba@1986
   287
    int index(const Node& node) const { return node.id; }
deba@1986
   288
deba@983
   289
    typedef True NodeNumTag;
deba@983
   290
    typedef True EdgeNumTag;
deba@983
   291
deba@983
   292
    ///Number of nodes.
deba@1566
   293
    int nodeNum() const { return _nodeNum; }
deba@983
   294
    ///Number of edges.
deba@1566
   295
    int edgeNum() const { return _edgeNum; }
deba@983
   296
deba@983
   297
    /// Maximum node ID.
deba@983
   298
    
deba@983
   299
    /// Maximum node ID.
deba@983
   300
    ///\sa id(Node)
deba@1791
   301
    int maxNodeId() const { return _nodeNum-1; }
deba@983
   302
    /// Maximum edge ID.
deba@983
   303
    
deba@983
   304
    /// Maximum edge ID.
deba@983
   305
    ///\sa id(Edge)
deba@1791
   306
    int maxEdgeId() const { return _edgeNum-1; }
deba@983
   307
alpar@986
   308
    Node source(Edge e) const { 
deba@983
   309
      /// \todo we may do it faster
alpar@1643
   310
      return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
deba@983
   311
    }
deba@983
   312
alpar@986
   313
    Node target(Edge e) const { 
alpar@986
   314
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@1643
   315
      return Node(e.id - (source) * (source - 1) / 2);
deba@983
   316
    }
deba@983
   317
deba@983
   318
deba@1986
   319
    /// \brief Node ID.
deba@1986
   320
    ///
deba@983
   321
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   322
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   323
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   324
    ///
deba@983
   325
    /// The ID of the \ref INVALID node is -1.
deba@1986
   326
    /// \return The ID of the node \c v. 
deba@983
   327
deba@983
   328
    static int id(Node v) { return v.id; }
deba@1986
   329
deba@1986
   330
    /// \brief Edge ID.
deba@1986
   331
    ///
deba@983
   332
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   333
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   334
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   335
    ///
deba@983
   336
    /// The ID of the \ref INVALID edge is -1.
deba@983
   337
    ///\return The ID of the edge \c e. 
deba@983
   338
    static int id(Edge e) { return e.id; }
deba@983
   339
deba@1986
   340
    /// \brief Finds an edge between two nodes.
deba@1986
   341
    ///
deba@983
   342
    /// Finds an edge from node \c u to node \c v.
deba@983
   343
    ///
deba@983
   344
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   345
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   346
    /// the next edge from \c u to \c v after \c prev.
deba@983
   347
    /// \return The found edge or INVALID if there is no such an edge.
deba@1703
   348
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
deba@1986
   349
      if (prev.id != -1 || u.id <= v.id) return Edge(-1);
deba@1703
   350
      return Edge(u.id * (u.id - 1) / 2 + v.id);
deba@983
   351
    }
deba@1703
   352
deba@1703
   353
    typedef True FindEdgeTag;
deba@983
   354
    
deba@983
   355
      
deba@983
   356
    class Node {
klao@1909
   357
      friend class FullUGraphBase;
deba@983
   358
deba@983
   359
    protected:
deba@983
   360
      int id;
deba@983
   361
      Node(int _id) { id = _id;}
deba@983
   362
    public:
deba@983
   363
      Node() {}
deba@983
   364
      Node (Invalid) { id = -1; }
deba@983
   365
      bool operator==(const Node node) const {return id == node.id;}
deba@983
   366
      bool operator!=(const Node node) const {return id != node.id;}
deba@983
   367
      bool operator<(const Node node) const {return id < node.id;}
deba@983
   368
    };
deba@983
   369
    
deba@983
   370
deba@983
   371
deba@983
   372
    class Edge {
klao@1909
   373
      friend class FullUGraphBase;
deba@983
   374
      
deba@983
   375
    protected:
deba@1566
   376
      int id;  // _nodeNum * target + source;
deba@983
   377
deba@983
   378
      Edge(int _id) : id(_id) {}
deba@983
   379
deba@983
   380
    public:
deba@983
   381
      Edge() { }
deba@983
   382
      Edge (Invalid) { id = -1; }
deba@983
   383
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@983
   384
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@983
   385
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@983
   386
    };
deba@983
   387
deba@983
   388
    void first(Node& node) const {
deba@1703
   389
      node.id = _nodeNum - 1;
deba@983
   390
    }
deba@983
   391
deba@983
   392
    static void next(Node& node) {
deba@983
   393
      --node.id;
deba@983
   394
    }
deba@983
   395
deba@983
   396
    void first(Edge& edge) const {
deba@1703
   397
      edge.id = _edgeNum - 1;
deba@983
   398
    }
deba@983
   399
deba@983
   400
    static void next(Edge& edge) {
deba@983
   401
      --edge.id;
deba@983
   402
    }
deba@983
   403
deba@983
   404
    void firstOut(Edge& edge, const Node& node) const {      
deba@1703
   405
      int src = node.id;
deba@1703
   406
      int trg = 0;
deba@1703
   407
      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
deba@983
   408
    }
deba@983
   409
deba@983
   410
    /// \todo with specialized iterators we can make faster iterating
deba@1703
   411
    void nextOut(Edge& edge) const {
deba@1703
   412
      int src = source(edge).id;
deba@1703
   413
      int trg = target(edge).id;
deba@1703
   414
      ++trg;
deba@1703
   415
      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
deba@983
   416
    }
deba@983
   417
deba@983
   418
    void firstIn(Edge& edge, const Node& node) const {
deba@1703
   419
      int src = node.id + 1;
deba@1703
   420
      int trg = node.id;
deba@1703
   421
      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
deba@983
   422
    }
deba@983
   423
    
deba@1703
   424
    void nextIn(Edge& edge) const {
deba@1703
   425
      int src = source(edge).id;
deba@1703
   426
      int trg = target(edge).id;
deba@1703
   427
      ++src;
deba@1703
   428
      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
deba@983
   429
    }
deba@983
   430
deba@983
   431
  };
deba@983
   432
deba@1979
   433
  typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> > 
deba@1979
   434
  ExtendedFullUGraphBase;
alpar@1555
   435
deba@1566
   436
  /// \ingroup graphs
deba@1566
   437
  ///
deba@1566
   438
  /// \brief An undirected full graph class.
deba@1566
   439
  ///
deba@1726
   440
  /// This is a simple and fast undirected full graph implementation.
deba@1566
   441
  /// It is completely static, so you can neither add nor delete either
deba@1566
   442
  /// edges or nodes.
deba@1566
   443
  ///
klao@1909
   444
  /// The main difference beetween the \e FullGraph and \e FullUGraph class
deba@1566
   445
  /// is that this class conforms to the undirected graph concept and
deba@1726
   446
  /// it does not contain the loop edges.
deba@1566
   447
  ///
deba@1986
   448
  /// \sa FullUGraphBase
deba@1566
   449
  /// \sa FullGraph
deba@1566
   450
  ///
deba@1566
   451
  /// \author Balazs Dezso
klao@1909
   452
  class FullUGraph : public ExtendedFullUGraphBase {
deba@1566
   453
  public:
deba@1979
   454
deba@1979
   455
    typedef ExtendedFullUGraphBase Parent;
deba@1979
   456
deba@1979
   457
    /// \brief Constructor
deba@1987
   458
    FullUGraph() { construct(0); }
deba@1987
   459
deba@1987
   460
    /// \brief Constructor
klao@1909
   461
    FullUGraph(int n) { construct(n); }
deba@1979
   462
deba@1979
   463
    /// \brief Resize the graph
deba@1979
   464
    ///
deba@1986
   465
    /// Resize the graph. The function will fully destroy and build the graph.
deba@1986
   466
    /// This cause that the maps of the graph will reallocated
deba@1986
   467
    /// automatically and the previous values will be lost.
deba@1979
   468
    void resize(int n) {
deba@1979
   469
      Parent::getNotifier(Edge()).clear();
deba@1979
   470
      Parent::getNotifier(UEdge()).clear();
deba@1979
   471
      Parent::getNotifier(Node()).clear();
deba@1979
   472
      construct(n);
deba@1979
   473
      Parent::getNotifier(Node()).build();
deba@1979
   474
      Parent::getNotifier(UEdge()).build();
deba@1979
   475
      Parent::getNotifier(Edge()).build();
deba@1979
   476
    }
deba@1566
   477
  };
alpar@591
   478
deba@1820
   479
deba@1910
   480
  class FullBpUGraphBase {
deba@1820
   481
  protected:
deba@1820
   482
deba@1910
   483
    int _aNodeNum;
deba@1910
   484
    int _bNodeNum;
deba@1820
   485
deba@1820
   486
    int _edgeNum;
deba@1820
   487
deba@1820
   488
  public:
deba@1820
   489
deba@1820
   490
    class NodeSetError : public LogicError {
deba@1820
   491
      virtual const char* exceptionName() const { 
deba@1910
   492
	return "lemon::FullBpUGraph::NodeSetError";
deba@1820
   493
      }
deba@1820
   494
    };
deba@1820
   495
  
deba@1820
   496
    class Node {
deba@1910
   497
      friend class FullBpUGraphBase;
deba@1820
   498
    protected:
deba@1820
   499
      int id;
deba@1820
   500
deba@1820
   501
      Node(int _id) : id(_id) {}
deba@1820
   502
    public:
deba@1820
   503
      Node() {}
deba@1820
   504
      Node(Invalid) { id = -1; }
deba@1820
   505
      bool operator==(const Node i) const {return id==i.id;}
deba@1820
   506
      bool operator!=(const Node i) const {return id!=i.id;}
deba@1820
   507
      bool operator<(const Node i) const {return id<i.id;}
deba@1820
   508
    };
deba@1820
   509
deba@1820
   510
    class Edge {
deba@1910
   511
      friend class FullBpUGraphBase;
deba@1820
   512
    protected:
deba@1820
   513
      int id;
deba@1820
   514
deba@1820
   515
      Edge(int _id) { id = _id;}
deba@1820
   516
    public:
deba@1820
   517
      Edge() {}
deba@1820
   518
      Edge (Invalid) { id = -1; }
deba@1820
   519
      bool operator==(const Edge i) const {return id==i.id;}
deba@1820
   520
      bool operator!=(const Edge i) const {return id!=i.id;}
deba@1820
   521
      bool operator<(const Edge i) const {return id<i.id;}
deba@1820
   522
    };
deba@1820
   523
deba@1910
   524
    void construct(int aNodeNum, int bNodeNum) {
deba@1910
   525
      _aNodeNum = aNodeNum;
deba@1910
   526
      _bNodeNum = bNodeNum;
deba@1910
   527
      _edgeNum = aNodeNum * bNodeNum;
deba@1820
   528
    }
deba@1820
   529
deba@1910
   530
    void firstANode(Node& node) const {
deba@1910
   531
      node.id = 2 * _aNodeNum - 2;
deba@1820
   532
      if (node.id < 0) node.id = -1; 
deba@1820
   533
    }
deba@1910
   534
    void nextANode(Node& node) const {
deba@1820
   535
      node.id -= 2;
deba@1820
   536
      if (node.id < 0) node.id = -1; 
deba@1820
   537
    }
deba@1820
   538
deba@1910
   539
    void firstBNode(Node& node) const {
deba@1910
   540
      node.id = 2 * _bNodeNum - 1;
deba@1820
   541
    }
deba@1910
   542
    void nextBNode(Node& node) const {
deba@1820
   543
      node.id -= 2;
deba@1820
   544
    }
deba@1820
   545
deba@1820
   546
    void first(Node& node) const {
deba@1910
   547
      if (_aNodeNum > 0) {
deba@1910
   548
	node.id = 2 * _aNodeNum - 2;
deba@1820
   549
      } else {
deba@1910
   550
	node.id = 2 * _bNodeNum - 1;
deba@1820
   551
      }
deba@1820
   552
    }
deba@1820
   553
    void next(Node& node) const {
deba@1820
   554
      node.id -= 2;
deba@1820
   555
      if (node.id == -2) {
deba@1910
   556
	node.id = 2 * _bNodeNum - 1;
deba@1820
   557
      }
deba@1820
   558
    }
deba@1820
   559
  
deba@1820
   560
    void first(Edge& edge) const {
deba@1820
   561
      edge.id = _edgeNum - 1;
deba@1820
   562
    }
deba@1820
   563
    void next(Edge& edge) const {
deba@1820
   564
      --edge.id;
deba@1820
   565
    }
deba@1820
   566
deba@1910
   567
    void firstOut(Edge& edge, const Node& node) const {
deba@1820
   568
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@1910
   569
      edge.id = (node.id >> 1) * _bNodeNum;
deba@1820
   570
    }
deba@1910
   571
    void nextOut(Edge& edge) const {
deba@1820
   572
      ++(edge.id);
deba@1910
   573
      if (edge.id % _bNodeNum == 0) edge.id = -1;
deba@1820
   574
    }
deba@1820
   575
deba@1910
   576
    void firstIn(Edge& edge, const Node& node) const {
deba@1820
   577
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@1820
   578
      edge.id = (node.id >> 1);
deba@1820
   579
    }
deba@1910
   580
    void nextIn(Edge& edge) const {
deba@1910
   581
      edge.id += _bNodeNum;
deba@1820
   582
      if (edge.id >= _edgeNum) edge.id = -1;
deba@1820
   583
    }
deba@1820
   584
deba@1820
   585
    static int id(const Node& node) {
deba@1820
   586
      return node.id;
deba@1820
   587
    }
deba@1820
   588
    static Node nodeFromId(int id) {
deba@1820
   589
      return Node(id);
deba@1820
   590
    }
deba@1820
   591
    int maxNodeId() const {
deba@1910
   592
      return _aNodeNum > _bNodeNum ? 
deba@1910
   593
	_aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
deba@1820
   594
    }
deba@1820
   595
  
deba@1820
   596
    static int id(const Edge& edge) {
deba@1820
   597
      return edge.id;
deba@1820
   598
    }
deba@1820
   599
    static Edge edgeFromId(int id) {
deba@1820
   600
      return Edge(id);
deba@1820
   601
    }
deba@1820
   602
    int maxEdgeId() const {
deba@1820
   603
      return _edgeNum - 1;
deba@1820
   604
    }
deba@1820
   605
  
deba@1910
   606
    static int aNodeId(const Node& node) {
deba@1820
   607
      return node.id >> 1;
deba@1820
   608
    }
deba@1995
   609
    static Node fromANodeId(int id) {
deba@1820
   610
      return Node(id << 1);
deba@1820
   611
    }
deba@1910
   612
    int maxANodeId() const {
deba@1910
   613
      return _aNodeNum;
deba@1820
   614
    }
deba@1820
   615
deba@1910
   616
    static int bNodeId(const Node& node) {
deba@1820
   617
      return node.id >> 1;
deba@1820
   618
    }
deba@1910
   619
    static Node fromBNodeId(int id) {
deba@1820
   620
      return Node((id << 1) + 1);
deba@1820
   621
    }
deba@1910
   622
    int maxBNodeId() const {
deba@1910
   623
      return _bNodeNum;
deba@1820
   624
    }
deba@1820
   625
deba@1910
   626
    Node aNode(const Edge& edge) const {
deba@1910
   627
      return Node((edge.id / _bNodeNum) << 1);
deba@1820
   628
    }
deba@1910
   629
    Node bNode(const Edge& edge) const {
deba@1910
   630
      return Node(((edge.id % _bNodeNum) << 1) + 1);
deba@1820
   631
    }
deba@1820
   632
deba@1910
   633
    static bool aNode(const Node& node) {
deba@1820
   634
      return (node.id & 1) == 0;
deba@1820
   635
    }
deba@1820
   636
deba@1910
   637
    static bool bNode(const Node& node) {
deba@1820
   638
      return (node.id & 1) == 1;
deba@1820
   639
    }
deba@1820
   640
deba@1910
   641
    static Node aNode(int index) {
deba@1820
   642
      return Node(index << 1);
deba@1820
   643
    }
deba@1820
   644
deba@1910
   645
    static Node bNode(int index) {
deba@1820
   646
      return Node((index << 1) + 1);
deba@1820
   647
    }
deba@1820
   648
deba@1820
   649
  };
deba@1820
   650
deba@1820
   651
deba@1979
   652
  typedef BpUGraphExtender< BpUGraphBaseExtender<
deba@1979
   653
    FullBpUGraphBase> > ExtendedFullBpUGraphBase;
deba@1820
   654
deba@1820
   655
deba@1910
   656
  /// \ingroup graphs
deba@1910
   657
  ///
deba@1910
   658
  /// \brief An undirected full bipartite graph class.
deba@1910
   659
  ///
deba@1910
   660
  /// This is a simple and fast bipartite undirected full graph implementation.
deba@1910
   661
  /// It is completely static, so you can neither add nor delete either
deba@1910
   662
  /// edges or nodes.
deba@1910
   663
  ///
deba@1986
   664
  /// \sa FullUGraphBase
deba@1910
   665
  /// \sa FullGraph
deba@1910
   666
  ///
deba@1910
   667
  /// \author Balazs Dezso
deba@1910
   668
  class FullBpUGraph : 
deba@1910
   669
    public ExtendedFullBpUGraphBase {
deba@1820
   670
  public:
deba@1979
   671
deba@1910
   672
    typedef ExtendedFullBpUGraphBase Parent;
deba@1979
   673
deba@1987
   674
    FullBpUGraph() {
deba@1987
   675
      Parent::construct(0, 0);
deba@1987
   676
    }
deba@1987
   677
deba@1910
   678
    FullBpUGraph(int aNodeNum, int bNodeNum) {
deba@1910
   679
      Parent::construct(aNodeNum, bNodeNum);
deba@1820
   680
    }
deba@1987
   681
deba@1979
   682
    /// \brief Resize the graph
deba@1979
   683
    ///
deba@1979
   684
    void resize(int n, int m) {
deba@1979
   685
      Parent::getNotifier(Edge()).clear();
deba@1979
   686
      Parent::getNotifier(UEdge()).clear();
deba@1979
   687
      Parent::getNotifier(Node()).clear();
deba@1987
   688
      Parent::getNotifier(ANode()).clear();
deba@1987
   689
      Parent::getNotifier(BNode()).clear();
deba@1979
   690
      construct(n, m);
deba@1987
   691
      Parent::getNotifier(ANode()).build();
deba@1987
   692
      Parent::getNotifier(BNode()).build();
deba@1979
   693
      Parent::getNotifier(Node()).build();
deba@1979
   694
      Parent::getNotifier(UEdge()).build();
deba@1979
   695
      Parent::getNotifier(Edge()).build();
deba@1979
   696
    }
deba@1820
   697
  };
deba@1820
   698
alpar@921
   699
} //namespace lemon
alpar@591
   700
alpar@591
   701
alpar@921
   702
#endif //LEMON_FULL_GRAPH_H