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