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