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