lemon/full_graph.h
author deba
Tue, 18 Apr 2006 09:14:38 +0000
changeset 2061 7ab148f53d66
parent 2031 080d51024ac5
child 2076 10681ee9d8ae
permissions -rw-r--r--
Bug fix

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