lemon/full_graph.h
author deba
Mon, 27 Feb 2006 10:36:01 +0000
changeset 1986 9b56cca61e2e
parent 1979 c2992fd74dad
child 1987 8cd6683382e0
permissions -rw-r--r--
An additional simplier interface for static size graphs.
Node operator()(int) for getting node by index
int index(Node node) for getting index by node
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
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@1979
   231
    ///
klao@946
   232
    FullGraph(int n) { construct(n); }
deba@1979
   233
deba@1979
   234
    /// \brief Resize the graph
deba@1979
   235
    ///
deba@1986
   236
    /// Resize the graph. The function will fully destroy and build the graph.
deba@1986
   237
    /// This cause that the maps of the graph will reallocated
deba@1986
   238
    /// automatically and the previous values will be lost.
deba@1979
   239
    void resize(int n) {
deba@1979
   240
      Parent::getNotifier(Edge()).clear();
deba@1979
   241
      Parent::getNotifier(Node()).clear();
deba@1979
   242
      construct(n);
deba@1979
   243
      Parent::getNotifier(Node()).build();
deba@1979
   244
      Parent::getNotifier(Edge()).build();
deba@1979
   245
    }
klao@946
   246
  };
klao@946
   247
deba@983
   248
deba@1986
   249
  /// \brief Base of the FullUGrpah.
deba@1986
   250
  ///
deba@1986
   251
  /// Base of the FullUGrpah.
klao@1909
   252
  class FullUGraphBase {
deba@1566
   253
    int _nodeNum;
deba@1566
   254
    int _edgeNum;
deba@983
   255
  public:
deba@983
   256
klao@1909
   257
    typedef FullUGraphBase Graph;
deba@983
   258
deba@983
   259
    class Node;
deba@983
   260
    class Edge;
deba@983
   261
deba@983
   262
  public:
deba@983
   263
klao@1909
   264
    FullUGraphBase() {}
deba@983
   265
deba@983
   266
deba@983
   267
    ///Creates a full graph with \c n nodes.
deba@1566
   268
    void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
deba@1986
   269
deba@1986
   270
    /// \brief Returns the node with the given index.
deba@983
   271
    ///
deba@1986
   272
    /// Returns the node with the given index. Because it is a
deba@1986
   273
    /// static size graph the node's of the graph can be indiced
deba@1986
   274
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
   275
    /// the node can accessed by the \e index() member.
deba@1986
   276
    Node operator()(int index) const { return Node(index); }
deba@1986
   277
deba@1986
   278
    /// \brief Returns the index of the node.
deba@1986
   279
    ///
deba@1986
   280
    /// Returns the index of the node. Because it is a
deba@1986
   281
    /// static size graph the node's of the graph can be indiced
deba@1986
   282
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
   283
    /// the node can accessed by the \e index() member.
deba@1986
   284
    int index(const Node& node) const { return node.id; }
deba@1986
   285
deba@983
   286
    typedef True NodeNumTag;
deba@983
   287
    typedef True EdgeNumTag;
deba@983
   288
deba@983
   289
    ///Number of nodes.
deba@1566
   290
    int nodeNum() const { return _nodeNum; }
deba@983
   291
    ///Number of edges.
deba@1566
   292
    int edgeNum() const { return _edgeNum; }
deba@983
   293
deba@983
   294
    /// Maximum node ID.
deba@983
   295
    
deba@983
   296
    /// Maximum node ID.
deba@983
   297
    ///\sa id(Node)
deba@1791
   298
    int maxNodeId() const { return _nodeNum-1; }
deba@983
   299
    /// Maximum edge ID.
deba@983
   300
    
deba@983
   301
    /// Maximum edge ID.
deba@983
   302
    ///\sa id(Edge)
deba@1791
   303
    int maxEdgeId() const { return _edgeNum-1; }
deba@983
   304
alpar@986
   305
    Node source(Edge e) const { 
deba@983
   306
      /// \todo we may do it faster
alpar@1643
   307
      return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
deba@983
   308
    }
deba@983
   309
alpar@986
   310
    Node target(Edge e) const { 
alpar@986
   311
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@1643
   312
      return Node(e.id - (source) * (source - 1) / 2);
deba@983
   313
    }
deba@983
   314
deba@983
   315
deba@1986
   316
    /// \brief Node ID.
deba@1986
   317
    ///
deba@983
   318
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   319
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   320
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   321
    ///
deba@983
   322
    /// The ID of the \ref INVALID node is -1.
deba@1986
   323
    /// \return The ID of the node \c v. 
deba@983
   324
deba@983
   325
    static int id(Node v) { return v.id; }
deba@1986
   326
deba@1986
   327
    /// \brief Edge ID.
deba@1986
   328
    ///
deba@983
   329
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   330
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   331
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   332
    ///
deba@983
   333
    /// The ID of the \ref INVALID edge is -1.
deba@983
   334
    ///\return The ID of the edge \c e. 
deba@983
   335
    static int id(Edge e) { return e.id; }
deba@983
   336
deba@1986
   337
    /// \brief Finds an edge between two nodes.
deba@1986
   338
    ///
deba@983
   339
    /// Finds an edge from node \c u to node \c v.
deba@983
   340
    ///
deba@983
   341
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   342
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   343
    /// the next edge from \c u to \c v after \c prev.
deba@983
   344
    /// \return The found edge or INVALID if there is no such an edge.
deba@1703
   345
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
deba@1986
   346
      if (prev.id != -1 || u.id <= v.id) return Edge(-1);
deba@1703
   347
      return Edge(u.id * (u.id - 1) / 2 + v.id);
deba@983
   348
    }
deba@1703
   349
deba@1703
   350
    typedef True FindEdgeTag;
deba@983
   351
    
deba@983
   352
      
deba@983
   353
    class Node {
klao@1909
   354
      friend class FullUGraphBase;
deba@983
   355
deba@983
   356
    protected:
deba@983
   357
      int id;
deba@983
   358
      Node(int _id) { id = _id;}
deba@983
   359
    public:
deba@983
   360
      Node() {}
deba@983
   361
      Node (Invalid) { id = -1; }
deba@983
   362
      bool operator==(const Node node) const {return id == node.id;}
deba@983
   363
      bool operator!=(const Node node) const {return id != node.id;}
deba@983
   364
      bool operator<(const Node node) const {return id < node.id;}
deba@983
   365
    };
deba@983
   366
    
deba@983
   367
deba@983
   368
deba@983
   369
    class Edge {
klao@1909
   370
      friend class FullUGraphBase;
deba@983
   371
      
deba@983
   372
    protected:
deba@1566
   373
      int id;  // _nodeNum * target + source;
deba@983
   374
deba@983
   375
      Edge(int _id) : id(_id) {}
deba@983
   376
deba@983
   377
    public:
deba@983
   378
      Edge() { }
deba@983
   379
      Edge (Invalid) { id = -1; }
deba@983
   380
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@983
   381
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@983
   382
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@983
   383
    };
deba@983
   384
deba@983
   385
    void first(Node& node) const {
deba@1703
   386
      node.id = _nodeNum - 1;
deba@983
   387
    }
deba@983
   388
deba@983
   389
    static void next(Node& node) {
deba@983
   390
      --node.id;
deba@983
   391
    }
deba@983
   392
deba@983
   393
    void first(Edge& edge) const {
deba@1703
   394
      edge.id = _edgeNum - 1;
deba@983
   395
    }
deba@983
   396
deba@983
   397
    static void next(Edge& edge) {
deba@983
   398
      --edge.id;
deba@983
   399
    }
deba@983
   400
deba@983
   401
    void firstOut(Edge& edge, const Node& node) const {      
deba@1703
   402
      int src = node.id;
deba@1703
   403
      int trg = 0;
deba@1703
   404
      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
deba@983
   405
    }
deba@983
   406
deba@983
   407
    /// \todo with specialized iterators we can make faster iterating
deba@1703
   408
    void nextOut(Edge& edge) const {
deba@1703
   409
      int src = source(edge).id;
deba@1703
   410
      int trg = target(edge).id;
deba@1703
   411
      ++trg;
deba@1703
   412
      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
deba@983
   413
    }
deba@983
   414
deba@983
   415
    void firstIn(Edge& edge, const Node& node) const {
deba@1703
   416
      int src = node.id + 1;
deba@1703
   417
      int trg = node.id;
deba@1703
   418
      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
deba@983
   419
    }
deba@983
   420
    
deba@1703
   421
    void nextIn(Edge& edge) const {
deba@1703
   422
      int src = source(edge).id;
deba@1703
   423
      int trg = target(edge).id;
deba@1703
   424
      ++src;
deba@1703
   425
      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
deba@983
   426
    }
deba@983
   427
deba@983
   428
  };
deba@983
   429
deba@1979
   430
  typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> > 
deba@1979
   431
  ExtendedFullUGraphBase;
alpar@1555
   432
deba@1566
   433
  /// \ingroup graphs
deba@1566
   434
  ///
deba@1566
   435
  /// \brief An undirected full graph class.
deba@1566
   436
  ///
deba@1726
   437
  /// This is a simple and fast undirected full graph implementation.
deba@1566
   438
  /// It is completely static, so you can neither add nor delete either
deba@1566
   439
  /// edges or nodes.
deba@1566
   440
  ///
klao@1909
   441
  /// The main difference beetween the \e FullGraph and \e FullUGraph class
deba@1566
   442
  /// is that this class conforms to the undirected graph concept and
deba@1726
   443
  /// it does not contain the loop edges.
deba@1566
   444
  ///
deba@1986
   445
  /// \sa FullUGraphBase
deba@1566
   446
  /// \sa FullGraph
deba@1566
   447
  ///
deba@1566
   448
  /// \author Balazs Dezso
klao@1909
   449
  class FullUGraph : public ExtendedFullUGraphBase {
deba@1566
   450
  public:
deba@1979
   451
deba@1979
   452
    typedef ExtendedFullUGraphBase Parent;
deba@1979
   453
deba@1979
   454
    /// \brief Constructor
klao@1909
   455
    FullUGraph(int n) { construct(n); }
deba@1979
   456
deba@1979
   457
    /// \brief Resize the graph
deba@1979
   458
    ///
deba@1986
   459
    /// Resize the graph. The function will fully destroy and build the graph.
deba@1986
   460
    /// This cause that the maps of the graph will reallocated
deba@1986
   461
    /// automatically and the previous values will be lost.
deba@1979
   462
    void resize(int n) {
deba@1979
   463
      Parent::getNotifier(Edge()).clear();
deba@1979
   464
      Parent::getNotifier(UEdge()).clear();
deba@1979
   465
      Parent::getNotifier(Node()).clear();
deba@1979
   466
      construct(n);
deba@1979
   467
      Parent::getNotifier(Node()).build();
deba@1979
   468
      Parent::getNotifier(UEdge()).build();
deba@1979
   469
      Parent::getNotifier(Edge()).build();
deba@1979
   470
    }
deba@1566
   471
  };
alpar@591
   472
deba@1820
   473
deba@1910
   474
  class FullBpUGraphBase {
deba@1820
   475
  protected:
deba@1820
   476
deba@1910
   477
    int _aNodeNum;
deba@1910
   478
    int _bNodeNum;
deba@1820
   479
deba@1820
   480
    int _edgeNum;
deba@1820
   481
deba@1820
   482
  public:
deba@1820
   483
deba@1820
   484
    class NodeSetError : public LogicError {
deba@1820
   485
      virtual const char* exceptionName() const { 
deba@1910
   486
	return "lemon::FullBpUGraph::NodeSetError";
deba@1820
   487
      }
deba@1820
   488
    };
deba@1820
   489
  
deba@1820
   490
    class Node {
deba@1910
   491
      friend class FullBpUGraphBase;
deba@1820
   492
    protected:
deba@1820
   493
      int id;
deba@1820
   494
deba@1820
   495
      Node(int _id) : id(_id) {}
deba@1820
   496
    public:
deba@1820
   497
      Node() {}
deba@1820
   498
      Node(Invalid) { id = -1; }
deba@1820
   499
      bool operator==(const Node i) const {return id==i.id;}
deba@1820
   500
      bool operator!=(const Node i) const {return id!=i.id;}
deba@1820
   501
      bool operator<(const Node i) const {return id<i.id;}
deba@1820
   502
    };
deba@1820
   503
deba@1820
   504
    class Edge {
deba@1910
   505
      friend class FullBpUGraphBase;
deba@1820
   506
    protected:
deba@1820
   507
      int id;
deba@1820
   508
deba@1820
   509
      Edge(int _id) { id = _id;}
deba@1820
   510
    public:
deba@1820
   511
      Edge() {}
deba@1820
   512
      Edge (Invalid) { id = -1; }
deba@1820
   513
      bool operator==(const Edge i) const {return id==i.id;}
deba@1820
   514
      bool operator!=(const Edge i) const {return id!=i.id;}
deba@1820
   515
      bool operator<(const Edge i) const {return id<i.id;}
deba@1820
   516
    };
deba@1820
   517
deba@1910
   518
    void construct(int aNodeNum, int bNodeNum) {
deba@1910
   519
      _aNodeNum = aNodeNum;
deba@1910
   520
      _bNodeNum = bNodeNum;
deba@1910
   521
      _edgeNum = aNodeNum * bNodeNum;
deba@1820
   522
    }
deba@1820
   523
deba@1910
   524
    void firstANode(Node& node) const {
deba@1910
   525
      node.id = 2 * _aNodeNum - 2;
deba@1820
   526
      if (node.id < 0) node.id = -1; 
deba@1820
   527
    }
deba@1910
   528
    void nextANode(Node& node) const {
deba@1820
   529
      node.id -= 2;
deba@1820
   530
      if (node.id < 0) node.id = -1; 
deba@1820
   531
    }
deba@1820
   532
deba@1910
   533
    void firstBNode(Node& node) const {
deba@1910
   534
      node.id = 2 * _bNodeNum - 1;
deba@1820
   535
    }
deba@1910
   536
    void nextBNode(Node& node) const {
deba@1820
   537
      node.id -= 2;
deba@1820
   538
    }
deba@1820
   539
deba@1820
   540
    void first(Node& node) const {
deba@1910
   541
      if (_aNodeNum > 0) {
deba@1910
   542
	node.id = 2 * _aNodeNum - 2;
deba@1820
   543
      } else {
deba@1910
   544
	node.id = 2 * _bNodeNum - 1;
deba@1820
   545
      }
deba@1820
   546
    }
deba@1820
   547
    void next(Node& node) const {
deba@1820
   548
      node.id -= 2;
deba@1820
   549
      if (node.id == -2) {
deba@1910
   550
	node.id = 2 * _bNodeNum - 1;
deba@1820
   551
      }
deba@1820
   552
    }
deba@1820
   553
  
deba@1820
   554
    void first(Edge& edge) const {
deba@1820
   555
      edge.id = _edgeNum - 1;
deba@1820
   556
    }
deba@1820
   557
    void next(Edge& edge) const {
deba@1820
   558
      --edge.id;
deba@1820
   559
    }
deba@1820
   560
deba@1910
   561
    void firstOut(Edge& edge, const Node& node) const {
deba@1820
   562
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@1910
   563
      edge.id = (node.id >> 1) * _bNodeNum;
deba@1820
   564
    }
deba@1910
   565
    void nextOut(Edge& edge) const {
deba@1820
   566
      ++(edge.id);
deba@1910
   567
      if (edge.id % _bNodeNum == 0) edge.id = -1;
deba@1820
   568
    }
deba@1820
   569
deba@1910
   570
    void firstIn(Edge& edge, const Node& node) const {
deba@1820
   571
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@1820
   572
      edge.id = (node.id >> 1);
deba@1820
   573
    }
deba@1910
   574
    void nextIn(Edge& edge) const {
deba@1910
   575
      edge.id += _bNodeNum;
deba@1820
   576
      if (edge.id >= _edgeNum) edge.id = -1;
deba@1820
   577
    }
deba@1820
   578
deba@1820
   579
    static int id(const Node& node) {
deba@1820
   580
      return node.id;
deba@1820
   581
    }
deba@1820
   582
    static Node nodeFromId(int id) {
deba@1820
   583
      return Node(id);
deba@1820
   584
    }
deba@1820
   585
    int maxNodeId() const {
deba@1910
   586
      return _aNodeNum > _bNodeNum ? 
deba@1910
   587
	_aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
deba@1820
   588
    }
deba@1820
   589
  
deba@1820
   590
    static int id(const Edge& edge) {
deba@1820
   591
      return edge.id;
deba@1820
   592
    }
deba@1820
   593
    static Edge edgeFromId(int id) {
deba@1820
   594
      return Edge(id);
deba@1820
   595
    }
deba@1820
   596
    int maxEdgeId() const {
deba@1820
   597
      return _edgeNum - 1;
deba@1820
   598
    }
deba@1820
   599
  
deba@1910
   600
    static int aNodeId(const Node& node) {
deba@1820
   601
      return node.id >> 1;
deba@1820
   602
    }
deba@1910
   603
    static Node fromANodeId(int id, Node) {
deba@1820
   604
      return Node(id << 1);
deba@1820
   605
    }
deba@1910
   606
    int maxANodeId() const {
deba@1910
   607
      return _aNodeNum;
deba@1820
   608
    }
deba@1820
   609
deba@1910
   610
    static int bNodeId(const Node& node) {
deba@1820
   611
      return node.id >> 1;
deba@1820
   612
    }
deba@1910
   613
    static Node fromBNodeId(int id) {
deba@1820
   614
      return Node((id << 1) + 1);
deba@1820
   615
    }
deba@1910
   616
    int maxBNodeId() const {
deba@1910
   617
      return _bNodeNum;
deba@1820
   618
    }
deba@1820
   619
deba@1910
   620
    Node aNode(const Edge& edge) const {
deba@1910
   621
      return Node((edge.id / _bNodeNum) << 1);
deba@1820
   622
    }
deba@1910
   623
    Node bNode(const Edge& edge) const {
deba@1910
   624
      return Node(((edge.id % _bNodeNum) << 1) + 1);
deba@1820
   625
    }
deba@1820
   626
deba@1910
   627
    static bool aNode(const Node& node) {
deba@1820
   628
      return (node.id & 1) == 0;
deba@1820
   629
    }
deba@1820
   630
deba@1910
   631
    static bool bNode(const Node& node) {
deba@1820
   632
      return (node.id & 1) == 1;
deba@1820
   633
    }
deba@1820
   634
deba@1910
   635
    static Node aNode(int index) {
deba@1820
   636
      return Node(index << 1);
deba@1820
   637
    }
deba@1820
   638
deba@1910
   639
    static Node bNode(int index) {
deba@1820
   640
      return Node((index << 1) + 1);
deba@1820
   641
    }
deba@1820
   642
deba@1820
   643
  };
deba@1820
   644
deba@1820
   645
deba@1979
   646
  typedef BpUGraphExtender< BpUGraphBaseExtender<
deba@1979
   647
    FullBpUGraphBase> > ExtendedFullBpUGraphBase;
deba@1820
   648
deba@1820
   649
deba@1910
   650
  /// \ingroup graphs
deba@1910
   651
  ///
deba@1910
   652
  /// \brief An undirected full bipartite graph class.
deba@1910
   653
  ///
deba@1910
   654
  /// This is a simple and fast bipartite undirected full graph implementation.
deba@1910
   655
  /// It is completely static, so you can neither add nor delete either
deba@1910
   656
  /// edges or nodes.
deba@1910
   657
  ///
deba@1986
   658
  /// \sa FullUGraphBase
deba@1910
   659
  /// \sa FullGraph
deba@1910
   660
  ///
deba@1910
   661
  /// \author Balazs Dezso
deba@1910
   662
  class FullBpUGraph : 
deba@1910
   663
    public ExtendedFullBpUGraphBase {
deba@1820
   664
  public:
deba@1979
   665
deba@1910
   666
    typedef ExtendedFullBpUGraphBase Parent;
deba@1979
   667
deba@1910
   668
    FullBpUGraph(int aNodeNum, int bNodeNum) {
deba@1910
   669
      Parent::construct(aNodeNum, bNodeNum);
deba@1820
   670
    }
deba@1979
   671
    /// \brief Resize the graph
deba@1979
   672
    ///
deba@1979
   673
    void resize(int n, int m) {
deba@1979
   674
      Parent::getNotifier(Edge()).clear();
deba@1979
   675
      Parent::getNotifier(UEdge()).clear();
deba@1979
   676
      Parent::getNotifier(Node()).clear();
deba@1979
   677
      construct(n, m);
deba@1979
   678
      Parent::getNotifier(Node()).build();
deba@1979
   679
      Parent::getNotifier(UEdge()).build();
deba@1979
   680
      Parent::getNotifier(Edge()).build();
deba@1979
   681
    }
deba@1820
   682
  };
deba@1820
   683
alpar@921
   684
} //namespace lemon
alpar@591
   685
alpar@591
   686
alpar@921
   687
#endif //LEMON_FULL_GRAPH_H