lemon/smart_graph.h
author deba
Fri, 24 Feb 2006 11:02:11 +0000
changeset 1983 a60527609489
parent 1956 a055123339d5
child 1993 2115143eceea
permissions -rw-r--r--
Bugfix
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@105
    18
alpar@921
    19
#ifndef LEMON_SMART_GRAPH_H
alpar@921
    20
#define LEMON_SMART_GRAPH_H
alpar@104
    21
klao@491
    22
///\ingroup graphs
alpar@242
    23
///\file
klao@1909
    24
///\brief SmartGraph and SmartUGraph classes.
alpar@242
    25
alpar@104
    26
#include <vector>
alpar@104
    27
alpar@921
    28
#include <lemon/invalid.h>
alpar@157
    29
deba@1791
    30
#include <lemon/bits/graph_extender.h>
klao@1034
    31
klao@977
    32
#include <lemon/utility.h>
deba@1820
    33
#include <lemon/error.h>
deba@782
    34
deba@1979
    35
#include <lemon/bits/graph_extender.h>
deba@1979
    36
alpar@921
    37
namespace lemon {
alpar@104
    38
alpar@973
    39
  class SmartGraph;
alpar@969
    40
  ///Base of SmartGraph
alpar@969
    41
alpar@969
    42
  ///Base of SmartGraph
alpar@969
    43
  ///
klao@946
    44
  class SmartGraphBase {
alpar@104
    45
alpar@973
    46
    friend class SmatGraph;
alpar@973
    47
alpar@973
    48
  protected:
alpar@104
    49
    struct NodeT 
alpar@104
    50
    {
alpar@104
    51
      int first_in,first_out;      
alpar@157
    52
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    53
    };
alpar@104
    54
    struct EdgeT 
alpar@104
    55
    {
alpar@986
    56
      int target, source, next_in, next_out;      
alpar@104
    57
      //FIXME: is this necessary?
alpar@157
    58
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    59
    };
alpar@104
    60
alpar@104
    61
    std::vector<NodeT> nodes;
alpar@129
    62
alpar@104
    63
    std::vector<EdgeT> edges;
alpar@104
    64
    
alpar@185
    65
    
alpar@104
    66
  public:
deba@782
    67
klao@946
    68
    typedef SmartGraphBase Graph;
alpar@104
    69
alpar@164
    70
    class Node;
alpar@164
    71
    class Edge;
alpar@108
    72
alpar@104
    73
    
alpar@104
    74
  public:
alpar@104
    75
klao@946
    76
    SmartGraphBase() : nodes(), edges() { }
deba@1718
    77
    SmartGraphBase(const SmartGraphBase &_g) 
deba@1718
    78
      : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    79
    
klao@977
    80
    typedef True NodeNumTag;
klao@977
    81
    typedef True EdgeNumTag;
klao@977
    82
alpar@813
    83
    ///Number of nodes.
alpar@813
    84
    int nodeNum() const { return nodes.size(); }
alpar@813
    85
    ///Number of edges.
alpar@813
    86
    int edgeNum() const { return edges.size(); }
alpar@104
    87
alpar@813
    88
    /// Maximum node ID.
alpar@813
    89
    
alpar@813
    90
    /// Maximum node ID.
alpar@813
    91
    ///\sa id(Node)
deba@1791
    92
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
    93
    /// Maximum edge ID.
alpar@813
    94
    
alpar@813
    95
    /// Maximum edge ID.
alpar@813
    96
    ///\sa id(Edge)
deba@1791
    97
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
    98
alpar@986
    99
    Node source(Edge e) const { return edges[e.n].source; }
alpar@986
   100
    Node target(Edge e) const { return edges[e.n].target; }
alpar@104
   101
alpar@813
   102
    /// Node ID.
alpar@813
   103
    
alpar@813
   104
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@1791
   105
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@1791
   106
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   107
    ///
alpar@813
   108
    /// The ID of the \ref INVALID node is -1.
alpar@813
   109
    ///\return The ID of the node \c v. 
alpar@713
   110
    static int id(Node v) { return v.n; }
alpar@813
   111
    /// Edge ID.
alpar@813
   112
    
alpar@813
   113
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@1791
   114
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@1791
   115
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   116
    ///
alpar@813
   117
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   118
    ///\return The ID of the edge \c e. 
alpar@713
   119
    static int id(Edge e) { return e.n; }
alpar@104
   120
deba@1791
   121
    static Node nodeFromId(int id) { return Node(id);}
deba@1106
   122
deba@1791
   123
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   124
alpar@164
   125
    Node addNode() {
alpar@164
   126
      Node n; n.n=nodes.size();
alpar@104
   127
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@104
   128
      return n;
alpar@104
   129
    }
alpar@108
   130
    
alpar@164
   131
    Edge addEdge(Node u, Node v) {
alpar@164
   132
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@986
   133
      edges[e.n].source=u.n; edges[e.n].target=v.n;
alpar@104
   134
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   135
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   136
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   137
alpar@104
   138
      return e;
alpar@104
   139
    }
alpar@104
   140
deba@782
   141
    void clear() {
deba@782
   142
      edges.clear();
deba@782
   143
      nodes.clear();
deba@782
   144
    }
alpar@104
   145
klao@946
   146
alpar@164
   147
    class Node {
klao@946
   148
      friend class SmartGraphBase;
alpar@973
   149
      friend class SmartGraph;
alpar@104
   150
alpar@104
   151
    protected:
alpar@104
   152
      int n;
alpar@164
   153
      Node(int nn) {n=nn;}
alpar@104
   154
    public:
alpar@164
   155
      Node() {}
alpar@503
   156
      Node (Invalid) { n=-1; }
alpar@164
   157
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   158
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   159
      bool operator<(const Node i) const {return n<i.n;}
alpar@104
   160
    };
alpar@104
   161
    
alpar@104
   162
alpar@164
   163
    class Edge {
klao@946
   164
      friend class SmartGraphBase;
alpar@973
   165
      friend class SmartGraph;
alpar@185
   166
alpar@104
   167
    protected:
alpar@104
   168
      int n;
alpar@905
   169
      Edge(int nn) {n=nn;}
alpar@706
   170
    public:
alpar@164
   171
      Edge() { }
marci@174
   172
      Edge (Invalid) { n=-1; }
alpar@164
   173
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   174
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   175
      bool operator<(const Edge i) const {return n<i.n;}
klao@946
   176
    };
alpar@905
   177
klao@946
   178
    void first(Node& node) const {
klao@946
   179
      node.n = nodes.size() - 1;
klao@946
   180
    }
klao@946
   181
klao@946
   182
    static void next(Node& node) {
klao@946
   183
      --node.n;
klao@946
   184
    }
klao@946
   185
klao@946
   186
    void first(Edge& edge) const {
klao@946
   187
      edge.n = edges.size() - 1;
klao@946
   188
    }
klao@946
   189
klao@946
   190
    static void next(Edge& edge) {
klao@946
   191
      --edge.n;
klao@946
   192
    }
klao@946
   193
klao@946
   194
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   195
      edge.n = nodes[node.n].first_out;
klao@946
   196
    }
klao@946
   197
klao@946
   198
    void nextOut(Edge& edge) const {
klao@946
   199
      edge.n = edges[edge.n].next_out;
klao@946
   200
    }
klao@946
   201
klao@946
   202
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   203
      edge.n = nodes[node.n].first_in;
klao@946
   204
    }
alpar@104
   205
    
klao@946
   206
    void nextIn(Edge& edge) const {
klao@946
   207
      edge.n = edges[edge.n].next_in;
klao@946
   208
    }
alpar@105
   209
alpar@1284
   210
    Node _split(Node n, bool connect = true)
alpar@1284
   211
    {
alpar@1284
   212
      Node b = addNode();
alpar@1284
   213
      nodes[b.n].first_out=nodes[n.n].first_out;
alpar@1284
   214
      nodes[n.n].first_out=-1;
alpar@1284
   215
      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
alpar@1284
   216
      if(connect) addEdge(n,b);
alpar@1284
   217
      return b;
alpar@1284
   218
    }
alpar@1284
   219
alpar@104
   220
  };
alpar@185
   221
deba@1979
   222
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
deba@937
   223
deba@1791
   224
  /// \ingroup graphs
alpar@1161
   225
alpar@950
   226
  ///A smart graph class.
deba@937
   227
alpar@950
   228
  ///This is a simple and fast graph implementation.
alpar@950
   229
  ///It is also quite memory efficient, but at the price
alpar@974
   230
  ///that <b> it does support only limited (only stack-like)
alpar@974
   231
  ///node and edge deletions</b>.
alpar@950
   232
  ///It conforms to 
klao@959
   233
  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
klao@959
   234
  ///\sa concept::ExtendableGraph.
alpar@950
   235
  ///
alpar@950
   236
  ///\author Alpar Juttner
deba@1669
   237
  class SmartGraph : public ExtendedSmartGraphBase {
alpar@969
   238
  public:
deba@1979
   239
deba@1979
   240
    typedef ExtendedSmartGraphBase Parent;
deba@1979
   241
alpar@1770
   242
    class Snapshot;
alpar@1770
   243
    friend class Snapshot;
alpar@973
   244
alpar@1011
   245
  protected:
alpar@1770
   246
    void restoreSnapshot(const Snapshot &s)
alpar@973
   247
    {
alpar@1457
   248
      while(s.edge_num<edges.size()) {
deba@1040
   249
	Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
alpar@986
   250
	nodes[edges.back().target].first_in=edges.back().next_in;
alpar@986
   251
	nodes[edges.back().source].first_out=edges.back().next_out;
alpar@973
   252
	edges.pop_back();
alpar@973
   253
      }
alpar@973
   254
      //nodes.resize(s.nodes_num);
alpar@1457
   255
      while(s.node_num<nodes.size()) {
deba@1040
   256
	Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
alpar@973
   257
	nodes.pop_back();
alpar@973
   258
      }
alpar@1011
   259
    }    
alpar@1011
   260
alpar@1011
   261
  public:
alpar@1284
   262
alpar@1284
   263
    ///Split a node.
alpar@1284
   264
    
alpar@1284
   265
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   266
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1284
   267
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1284
   268
    ///from \c n to the newly created node is also added.
alpar@1284
   269
    ///\return The newly created node.
alpar@1284
   270
    ///
alpar@1284
   271
    ///\note The <tt>Edge</tt>s
alpar@1284
   272
    ///referencing a moved edge remain
alpar@1284
   273
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1284
   274
    ///may be invalidated.
alpar@1770
   275
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1284
   276
    ///feature.
alpar@1284
   277
    ///\todo It could be implemented in a bit faster way.
alpar@1284
   278
    Node split(Node n, bool connect = true) 
alpar@1284
   279
    {
deba@1718
   280
      Node b = _split(n,connect);
deba@1718
   281
      return b;
alpar@1284
   282
    }
alpar@1284
   283
  
alpar@1284
   284
alpar@1011
   285
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   286
alpar@1011
   287
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   288
    ///
alpar@1011
   289
    ///The newly added nodes and edges can be removed using the
alpar@1011
   290
    ///restore() function.
alpar@1011
   291
    ///\note After you restore a state, you cannot restore
alpar@1011
   292
    ///a later state, in other word you cannot add again the edges deleted
alpar@1770
   293
    ///by restore() using another Snapshot instance.
alpar@1011
   294
    ///
alpar@1770
   295
    class Snapshot 
alpar@1011
   296
    {
alpar@1011
   297
      SmartGraph *g;
alpar@1011
   298
    protected:
alpar@1011
   299
      friend class SmartGraph;
alpar@1011
   300
      unsigned int node_num;
alpar@1011
   301
      unsigned int edge_num;
alpar@1011
   302
    public:
zsuzska@1274
   303
      ///Default constructor.
alpar@1011
   304
      
zsuzska@1274
   305
      ///Default constructor.
alpar@1011
   306
      ///To actually make a snapshot you must call save().
alpar@1011
   307
      ///
alpar@1770
   308
      Snapshot() : g(0) {}
alpar@1011
   309
      ///Constructor that immediately makes a snapshot
alpar@1011
   310
      
alpar@1011
   311
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   312
      ///\param _g The graph we make a snapshot of.
alpar@1770
   313
      Snapshot(SmartGraph &_g) :g(&_g) {
alpar@1011
   314
	node_num=g->nodes.size();
alpar@1011
   315
	edge_num=g->edges.size();
alpar@1011
   316
      }
alpar@1011
   317
alpar@1011
   318
      ///Make a snapshot.
alpar@1011
   319
alpar@1011
   320
      ///Make a snapshot of the graph.
alpar@1011
   321
      ///
alpar@1011
   322
      ///This function can be called more than once. In case of a repeated
alpar@1011
   323
      ///call, the previous snapshot gets lost.
alpar@1011
   324
      ///\param _g The graph we make the snapshot of.
alpar@1011
   325
      void save(SmartGraph &_g) 
alpar@1011
   326
      {
alpar@1011
   327
	g=&_g;
alpar@1011
   328
	node_num=g->nodes.size();
alpar@1011
   329
	edge_num=g->edges.size();
alpar@1011
   330
      }
alpar@1011
   331
alpar@1011
   332
      ///Undo the changes until a snapshot.
alpar@1011
   333
      
alpar@1011
   334
      ///Undo the changes until a snapshot created by save().
alpar@1011
   335
      ///
alpar@1011
   336
      ///\note After you restored a state, you cannot restore
alpar@1011
   337
      ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   338
      ///by restore().
alpar@1011
   339
      ///
alpar@1011
   340
      ///\todo This function might be called undo().
alpar@1011
   341
      
alpar@1011
   342
      void restore()
alpar@1011
   343
      {
alpar@1770
   344
	g->restoreSnapshot(*this);
alpar@1011
   345
      }
alpar@1011
   346
    };
alpar@973
   347
  };
klao@1034
   348
klao@1034
   349
klao@1034
   350
  /**************** Undirected List Graph ****************/
klao@1034
   351
deba@1979
   352
  typedef UGraphExtender<UGraphBaseExtender<SmartGraphBase> >
deba@1979
   353
  ExtendedSmartUGraphBase;
klao@1034
   354
klao@1909
   355
  /// \ingroup graphs
alpar@1035
   356
  ///
klao@1909
   357
  /// \brief A smart undirected graph class.
alpar@1035
   358
  ///
klao@1909
   359
  /// This is a simple and fast undirected graph implementation.
klao@1909
   360
  /// It is also quite memory efficient, but at the price
klao@1909
   361
  /// that <b> it does support only limited (only stack-like)
klao@1909
   362
  /// node and edge deletions</b>.
klao@1909
   363
  /// Except from this it conforms to 
klao@1909
   364
  /// the \ref concept::UGraph "UGraph" concept.
klao@1909
   365
  /// \sa concept::UGraph.
klao@1909
   366
  ///
klao@1909
   367
  /// \todo Snapshot hasn't been implemented yet.
klao@1909
   368
  ///
klao@1909
   369
  class SmartUGraph : public ExtendedSmartUGraphBase {
klao@1034
   370
  };
klao@1034
   371
deba@1820
   372
deba@1910
   373
  class SmartBpUGraphBase {
deba@1820
   374
  public:
deba@1820
   375
deba@1820
   376
    class NodeSetError : public LogicError {
deba@1820
   377
      virtual const char* exceptionName() const { 
deba@1910
   378
	return "lemon::SmartBpUGraph::NodeSetError";
deba@1820
   379
      }
deba@1820
   380
    };
deba@1820
   381
deba@1820
   382
  protected:
deba@1820
   383
deba@1820
   384
    struct NodeT {
deba@1820
   385
      int first;
deba@1820
   386
      NodeT() {}
deba@1820
   387
      NodeT(int _first) : first(_first) {}
deba@1820
   388
    };
deba@1820
   389
deba@1820
   390
    struct EdgeT {
deba@1910
   391
      int aNode, next_out;
deba@1910
   392
      int bNode, next_in;
deba@1820
   393
    };
deba@1820
   394
deba@1910
   395
    std::vector<NodeT> aNodes;
deba@1910
   396
    std::vector<NodeT> bNodes;
deba@1820
   397
deba@1820
   398
    std::vector<EdgeT> edges;
deba@1820
   399
deba@1820
   400
  public:
deba@1820
   401
  
deba@1820
   402
    class Node {
deba@1910
   403
      friend class SmartBpUGraphBase;
deba@1820
   404
    protected:
deba@1820
   405
      int id;
deba@1820
   406
deba@1820
   407
      Node(int _id) : id(_id) {}
deba@1820
   408
    public:
deba@1820
   409
      Node() {}
deba@1820
   410
      Node(Invalid) { id = -1; }
deba@1820
   411
      bool operator==(const Node i) const {return id==i.id;}
deba@1820
   412
      bool operator!=(const Node i) const {return id!=i.id;}
deba@1820
   413
      bool operator<(const Node i) const {return id<i.id;}
deba@1820
   414
    };
deba@1820
   415
deba@1820
   416
    class Edge {
deba@1910
   417
      friend class SmartBpUGraphBase;
deba@1820
   418
    protected:
deba@1820
   419
      int id;
deba@1820
   420
deba@1820
   421
      Edge(int _id) { id = _id;}
deba@1820
   422
    public:
deba@1820
   423
      Edge() {}
deba@1820
   424
      Edge (Invalid) { id = -1; }
deba@1820
   425
      bool operator==(const Edge i) const {return id==i.id;}
deba@1820
   426
      bool operator!=(const Edge i) const {return id!=i.id;}
deba@1820
   427
      bool operator<(const Edge i) const {return id<i.id;}
deba@1820
   428
    };
deba@1820
   429
deba@1910
   430
    void firstANode(Node& node) const {
deba@1910
   431
      node.id = 2 * aNodes.size() - 2;
deba@1820
   432
      if (node.id < 0) node.id = -1; 
deba@1820
   433
    }
deba@1910
   434
    void nextANode(Node& node) const {
deba@1820
   435
      node.id -= 2;
deba@1820
   436
      if (node.id < 0) node.id = -1; 
deba@1820
   437
    }
deba@1820
   438
deba@1910
   439
    void firstBNode(Node& node) const {
deba@1910
   440
      node.id = 2 * bNodes.size() - 1;
deba@1820
   441
    }
deba@1910
   442
    void nextBNode(Node& node) const {
deba@1820
   443
      node.id -= 2;
deba@1820
   444
    }
deba@1820
   445
deba@1820
   446
    void first(Node& node) const {
deba@1910
   447
      if (aNodes.size() > 0) {
deba@1910
   448
	node.id = 2 * aNodes.size() - 2;
deba@1820
   449
      } else {
deba@1910
   450
	node.id = 2 * bNodes.size() - 1;
deba@1820
   451
      }
deba@1820
   452
    }
deba@1820
   453
    void next(Node& node) const {
deba@1820
   454
      node.id -= 2;
deba@1820
   455
      if (node.id == -2) {
deba@1910
   456
	node.id = 2 * bNodes.size() - 1;
deba@1820
   457
      }
deba@1820
   458
    }
deba@1820
   459
  
deba@1820
   460
    void first(Edge& edge) const {
deba@1820
   461
      edge.id = edges.size() - 1;
deba@1820
   462
    }
deba@1820
   463
    void next(Edge& edge) const {
deba@1820
   464
      --edge.id;
deba@1820
   465
    }
deba@1820
   466
deba@1910
   467
    void firstOut(Edge& edge, const Node& node) const {
deba@1820
   468
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@1910
   469
      edge.id = aNodes[node.id >> 1].first;
deba@1820
   470
    }
deba@1910
   471
    void nextOut(Edge& edge) const {
deba@1910
   472
      edge.id = edges[edge.id].next_out;
deba@1820
   473
    }
deba@1820
   474
deba@1910
   475
    void firstIn(Edge& edge, const Node& node) const {
deba@1820
   476
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@1910
   477
      edge.id = bNodes[node.id >> 1].first;
deba@1820
   478
    }
deba@1910
   479
    void nextIn(Edge& edge) const {
deba@1910
   480
      edge.id = edges[edge.id].next_in;
deba@1820
   481
    }
deba@1820
   482
deba@1820
   483
    static int id(const Node& node) {
deba@1820
   484
      return node.id;
deba@1820
   485
    }
deba@1820
   486
    static Node nodeFromId(int id) {
deba@1820
   487
      return Node(id);
deba@1820
   488
    }
deba@1820
   489
    int maxNodeId() const {
deba@1910
   490
      return aNodes.size() > bNodes.size() ?
deba@1910
   491
	aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
deba@1820
   492
    }
deba@1820
   493
  
deba@1820
   494
    static int id(const Edge& edge) {
deba@1820
   495
      return edge.id;
deba@1820
   496
    }
deba@1820
   497
    static Edge edgeFromId(int id) {
deba@1820
   498
      return Edge(id);
deba@1820
   499
    }
deba@1820
   500
    int maxEdgeId() const {
deba@1820
   501
      return edges.size();
deba@1820
   502
    }
deba@1820
   503
  
deba@1910
   504
    static int aNodeId(const Node& node) {
deba@1820
   505
      return node.id >> 1;
deba@1820
   506
    }
deba@1910
   507
    static Node fromANodeId(int id, Node) {
deba@1820
   508
      return Node(id << 1);
deba@1820
   509
    }
deba@1910
   510
    int maxANodeId() const {
deba@1910
   511
      return aNodes.size();
deba@1820
   512
    }
deba@1820
   513
deba@1910
   514
    static int bNodeId(const Node& node) {
deba@1820
   515
      return node.id >> 1;
deba@1820
   516
    }
deba@1910
   517
    static Node fromBNodeId(int id) {
deba@1820
   518
      return Node((id << 1) + 1);
deba@1820
   519
    }
deba@1910
   520
    int maxBNodeId() const {
deba@1910
   521
      return bNodes.size();
deba@1820
   522
    }
deba@1820
   523
deba@1910
   524
    Node aNode(const Edge& edge) const {
deba@1910
   525
      return Node(edges[edge.id].aNode);
deba@1820
   526
    }
deba@1910
   527
    Node bNode(const Edge& edge) const {
deba@1910
   528
      return Node(edges[edge.id].bNode);
deba@1820
   529
    }
deba@1820
   530
deba@1910
   531
    static bool aNode(const Node& node) {
deba@1820
   532
      return (node.id & 1) == 0;
deba@1820
   533
    }
deba@1820
   534
deba@1910
   535
    static bool bNode(const Node& node) {
deba@1820
   536
      return (node.id & 1) == 1;
deba@1820
   537
    }
deba@1820
   538
deba@1910
   539
    Node addANode() {
deba@1820
   540
      NodeT nodeT;
deba@1820
   541
      nodeT.first = -1;
deba@1910
   542
      aNodes.push_back(nodeT);
deba@1910
   543
      return Node(aNodes.size() * 2 - 2);
deba@1820
   544
    }
deba@1820
   545
deba@1910
   546
    Node addBNode() {
deba@1820
   547
      NodeT nodeT;
deba@1820
   548
      nodeT.first = -1;
deba@1910
   549
      bNodes.push_back(nodeT);
deba@1910
   550
      return Node(bNodes.size() * 2 - 1);
deba@1820
   551
    }
deba@1820
   552
deba@1820
   553
    Edge addEdge(const Node& source, const Node& target) {
deba@1820
   554
      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
deba@1820
   555
      EdgeT edgeT;
deba@1820
   556
      if ((source.id & 1) == 0) {
deba@1910
   557
	edgeT.aNode = source.id;
deba@1910
   558
	edgeT.bNode = target.id;
deba@1820
   559
      } else {
deba@1910
   560
	edgeT.aNode = target.id;
deba@1910
   561
	edgeT.bNode = source.id;
deba@1820
   562
      }
deba@1910
   563
      edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
deba@1910
   564
      aNodes[edgeT.aNode >> 1].first = edges.size();
deba@1910
   565
      edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
deba@1910
   566
      bNodes[edgeT.bNode >> 1].first = edges.size();
deba@1820
   567
      edges.push_back(edgeT);
deba@1820
   568
      return Edge(edges.size() - 1);
deba@1820
   569
    }
deba@1820
   570
deba@1820
   571
    void clear() {
deba@1910
   572
      aNodes.clear();
deba@1910
   573
      bNodes.clear();
deba@1820
   574
      edges.clear();
deba@1820
   575
    }
deba@1820
   576
deba@1820
   577
  };
deba@1820
   578
deba@1820
   579
deba@1979
   580
  typedef BpUGraphExtender< BpUGraphBaseExtender<
deba@1979
   581
    SmartBpUGraphBase> > ExtendedSmartBpUGraphBase;
deba@1820
   582
deba@1910
   583
  /// \ingroup graphs
deba@1910
   584
  ///
deba@1910
   585
  /// \brief A smart bipartite undirected graph class.
deba@1910
   586
  ///
deba@1910
   587
  /// This is a simple and fast bipartite undirected graph implementation.
deba@1910
   588
  /// It is also quite memory efficient, but at the price
deba@1910
   589
  /// that <b> it does not support node and edge deletions</b>.
deba@1910
   590
  /// Except from this it conforms to 
deba@1910
   591
  /// the \ref concept::BpUGraph "BpUGraph" concept.
deba@1910
   592
  /// \sa concept::BpUGraph.
deba@1910
   593
  ///
deba@1910
   594
  class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
deba@1820
   595
alpar@950
   596
  
alpar@407
   597
  /// @}  
alpar@921
   598
} //namespace lemon
alpar@104
   599
alpar@157
   600
alpar@921
   601
#endif //LEMON_SMART_GRAPH_H