lemon/smart_graph.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 05 Nov 2009 10:23:16 +0100
changeset 780 580af8cf2f6a
parent 778 a143f19f465b
parent 736 2e20aad15754
child 787 c2230649a493
permissions -rw-r--r--
Merge #68 (Port static graph implementation)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@109
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@109
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
deba@109
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@109
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@109
     8
 *
deba@109
     9
 * Permission to use, modify and distribute this software is granted
deba@109
    10
 * provided that this copyright notice appears in all copies. For
deba@109
    11
 * precise terms see the accompanying LICENSE file.
deba@109
    12
 *
deba@109
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@109
    14
 * express or implied, and with no claim as to its suitability for any
deba@109
    15
 * purpose.
deba@109
    16
 *
deba@109
    17
 */
deba@109
    18
deba@109
    19
#ifndef LEMON_SMART_GRAPH_H
deba@109
    20
#define LEMON_SMART_GRAPH_H
deba@109
    21
deba@109
    22
///\ingroup graphs
deba@109
    23
///\file
deba@109
    24
///\brief SmartDigraph and SmartGraph classes.
deba@109
    25
deba@109
    26
#include <vector>
deba@109
    27
deba@220
    28
#include <lemon/core.h>
deba@109
    29
#include <lemon/error.h>
deba@109
    30
#include <lemon/bits/graph_extender.h>
deba@109
    31
deba@109
    32
namespace lemon {
deba@109
    33
deba@109
    34
  class SmartDigraph;
deba@109
    35
deba@109
    36
  class SmartDigraphBase {
deba@109
    37
  protected:
deba@109
    38
alpar@209
    39
    struct NodeT
deba@109
    40
    {
alpar@209
    41
      int first_in, first_out;
deba@109
    42
      NodeT() {}
deba@109
    43
    };
alpar@209
    44
    struct ArcT
deba@109
    45
    {
alpar@209
    46
      int target, source, next_in, next_out;
alpar@209
    47
      ArcT() {}
deba@109
    48
    };
deba@109
    49
deba@109
    50
    std::vector<NodeT> nodes;
deba@109
    51
    std::vector<ArcT> arcs;
alpar@209
    52
deba@109
    53
  public:
deba@109
    54
kpeter@617
    55
    typedef SmartDigraphBase Digraph;
deba@109
    56
deba@109
    57
    class Node;
deba@109
    58
    class Arc;
deba@109
    59
deba@109
    60
  public:
deba@109
    61
deba@109
    62
    SmartDigraphBase() : nodes(), arcs() { }
alpar@209
    63
    SmartDigraphBase(const SmartDigraphBase &_g)
deba@109
    64
      : nodes(_g.nodes), arcs(_g.arcs) { }
alpar@209
    65
deba@109
    66
    typedef True NodeNumTag;
kpeter@360
    67
    typedef True ArcNumTag;
deba@109
    68
deba@109
    69
    int nodeNum() const { return nodes.size(); }
deba@109
    70
    int arcNum() const { return arcs.size(); }
deba@109
    71
deba@109
    72
    int maxNodeId() const { return nodes.size()-1; }
deba@109
    73
    int maxArcId() const { return arcs.size()-1; }
deba@109
    74
deba@109
    75
    Node addNode() {
alpar@209
    76
      int n = nodes.size();
deba@109
    77
      nodes.push_back(NodeT());
deba@109
    78
      nodes[n].first_in = -1;
deba@109
    79
      nodes[n].first_out = -1;
deba@109
    80
      return Node(n);
deba@109
    81
    }
alpar@209
    82
deba@109
    83
    Arc addArc(Node u, Node v) {
alpar@209
    84
      int n = arcs.size();
deba@109
    85
      arcs.push_back(ArcT());
alpar@209
    86
      arcs[n].source = u._id;
deba@109
    87
      arcs[n].target = v._id;
deba@109
    88
      arcs[n].next_out = nodes[u._id].first_out;
deba@109
    89
      arcs[n].next_in = nodes[v._id].first_in;
deba@109
    90
      nodes[u._id].first_out = nodes[v._id].first_in = n;
deba@109
    91
deba@109
    92
      return Arc(n);
deba@109
    93
    }
deba@109
    94
deba@109
    95
    void clear() {
deba@109
    96
      arcs.clear();
deba@109
    97
      nodes.clear();
deba@109
    98
    }
deba@109
    99
deba@109
   100
    Node source(Arc a) const { return Node(arcs[a._id].source); }
deba@109
   101
    Node target(Arc a) const { return Node(arcs[a._id].target); }
deba@109
   102
deba@109
   103
    static int id(Node v) { return v._id; }
deba@109
   104
    static int id(Arc a) { return a._id; }
deba@109
   105
deba@109
   106
    static Node nodeFromId(int id) { return Node(id);}
deba@109
   107
    static Arc arcFromId(int id) { return Arc(id);}
deba@109
   108
alpar@209
   109
    bool valid(Node n) const {
alpar@209
   110
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
deba@149
   111
    }
alpar@209
   112
    bool valid(Arc a) const {
alpar@209
   113
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
deba@149
   114
    }
deba@149
   115
deba@109
   116
    class Node {
deba@109
   117
      friend class SmartDigraphBase;
deba@109
   118
      friend class SmartDigraph;
deba@109
   119
deba@109
   120
    protected:
deba@109
   121
      int _id;
deba@109
   122
      explicit Node(int id) : _id(id) {}
deba@109
   123
    public:
deba@109
   124
      Node() {}
deba@109
   125
      Node (Invalid) : _id(-1) {}
deba@109
   126
      bool operator==(const Node i) const {return _id == i._id;}
deba@109
   127
      bool operator!=(const Node i) const {return _id != i._id;}
deba@109
   128
      bool operator<(const Node i) const {return _id < i._id;}
deba@109
   129
    };
alpar@209
   130
deba@109
   131
deba@109
   132
    class Arc {
deba@109
   133
      friend class SmartDigraphBase;
deba@109
   134
      friend class SmartDigraph;
deba@109
   135
deba@109
   136
    protected:
deba@109
   137
      int _id;
deba@109
   138
      explicit Arc(int id) : _id(id) {}
deba@109
   139
    public:
deba@109
   140
      Arc() { }
deba@109
   141
      Arc (Invalid) : _id(-1) {}
deba@109
   142
      bool operator==(const Arc i) const {return _id == i._id;}
deba@109
   143
      bool operator!=(const Arc i) const {return _id != i._id;}
deba@109
   144
      bool operator<(const Arc i) const {return _id < i._id;}
deba@109
   145
    };
deba@109
   146
deba@109
   147
    void first(Node& node) const {
deba@109
   148
      node._id = nodes.size() - 1;
deba@109
   149
    }
deba@109
   150
deba@109
   151
    static void next(Node& node) {
deba@109
   152
      --node._id;
deba@109
   153
    }
deba@109
   154
deba@109
   155
    void first(Arc& arc) const {
deba@109
   156
      arc._id = arcs.size() - 1;
deba@109
   157
    }
deba@109
   158
deba@109
   159
    static void next(Arc& arc) {
deba@109
   160
      --arc._id;
deba@109
   161
    }
deba@109
   162
deba@109
   163
    void firstOut(Arc& arc, const Node& node) const {
deba@109
   164
      arc._id = nodes[node._id].first_out;
deba@109
   165
    }
deba@109
   166
deba@109
   167
    void nextOut(Arc& arc) const {
deba@109
   168
      arc._id = arcs[arc._id].next_out;
deba@109
   169
    }
deba@109
   170
deba@109
   171
    void firstIn(Arc& arc, const Node& node) const {
deba@109
   172
      arc._id = nodes[node._id].first_in;
deba@109
   173
    }
alpar@209
   174
deba@109
   175
    void nextIn(Arc& arc) const {
deba@109
   176
      arc._id = arcs[arc._id].next_in;
deba@109
   177
    }
deba@109
   178
deba@109
   179
  };
deba@109
   180
deba@109
   181
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
deba@109
   182
deba@109
   183
  ///\ingroup graphs
deba@109
   184
  ///
deba@109
   185
  ///\brief A smart directed graph class.
deba@109
   186
  ///
kpeter@735
   187
  ///\ref SmartDigraph is a simple and fast digraph implementation.
kpeter@735
   188
  ///It is also quite memory efficient but at the price
kpeter@735
   189
  ///that it does not support node and arc deletion 
kpeter@735
   190
  ///(except for the Snapshot feature).
deba@109
   191
  ///
kpeter@735
   192
  ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
kpeter@735
   193
  ///and it also provides some additional functionalities.
kpeter@735
   194
  ///Most of its member functions and nested classes are documented
kpeter@735
   195
  ///only in the concept class.
kpeter@735
   196
  ///
kpeter@735
   197
  ///\sa concepts::Digraph
kpeter@735
   198
  ///\sa SmartGraph
deba@109
   199
  class SmartDigraph : public ExtendedSmartDigraphBase {
deba@109
   200
    typedef ExtendedSmartDigraphBase Parent;
deba@109
   201
deba@109
   202
  private:
kpeter@735
   203
    /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
deba@109
   204
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
kpeter@735
   205
    /// \brief Assignment of a digraph to another one is \e not allowed.
kpeter@735
   206
    /// Use DigraphCopy instead.
deba@109
   207
    void operator=(const SmartDigraph &) {}
deba@109
   208
deba@109
   209
  public:
alpar@209
   210
deba@109
   211
    /// Constructor
alpar@209
   212
deba@109
   213
    /// Constructor.
deba@109
   214
    ///
deba@109
   215
    SmartDigraph() {};
alpar@209
   216
deba@109
   217
    ///Add a new node to the digraph.
alpar@209
   218
kpeter@735
   219
    ///This function adds a new node to the digraph.
kpeter@735
   220
    ///\return The new node.
deba@109
   221
    Node addNode() { return Parent::addNode(); }
alpar@209
   222
deba@109
   223
    ///Add a new arc to the digraph.
alpar@209
   224
kpeter@735
   225
    ///This function adds a new arc to the digraph with source node \c s
deba@109
   226
    ///and target node \c t.
kpeter@559
   227
    ///\return The new arc.
kpeter@735
   228
    Arc addArc(Node s, Node t) {
alpar@209
   229
      return Parent::addArc(s, t);
deba@109
   230
    }
deba@109
   231
deba@149
   232
    /// \brief Node validity check
deba@149
   233
    ///
kpeter@735
   234
    /// This function gives back \c true if the given node is valid,
kpeter@735
   235
    /// i.e. it is a real node of the digraph.
deba@149
   236
    ///
deba@149
   237
    /// \warning A removed node (using Snapshot) could become valid again
kpeter@735
   238
    /// if new nodes are added to the digraph.
deba@149
   239
    bool valid(Node n) const { return Parent::valid(n); }
deba@149
   240
deba@149
   241
    /// \brief Arc validity check
deba@149
   242
    ///
kpeter@735
   243
    /// This function gives back \c true if the given arc is valid,
kpeter@735
   244
    /// i.e. it is a real arc of the digraph.
deba@149
   245
    ///
deba@149
   246
    /// \warning A removed arc (using Snapshot) could become valid again
kpeter@735
   247
    /// if new arcs are added to the graph.
deba@149
   248
    bool valid(Arc a) const { return Parent::valid(a); }
deba@149
   249
deba@109
   250
    ///Split a node.
alpar@209
   251
kpeter@735
   252
    ///This function splits the given node. First, a new node is added
kpeter@735
   253
    ///to the digraph, then the source of each outgoing arc of node \c n
kpeter@735
   254
    ///is moved to this new node.
kpeter@735
   255
    ///If the second parameter \c connect is \c true (this is the default
kpeter@735
   256
    ///value), then a new arc from node \c n to the newly created node
kpeter@735
   257
    ///is also added.
deba@109
   258
    ///\return The newly created node.
deba@109
   259
    ///
kpeter@735
   260
    ///\note All iterators remain valid.
kpeter@735
   261
    ///
deba@109
   262
    ///\warning This functionality cannot be used together with the Snapshot
deba@109
   263
    ///feature.
deba@109
   264
    Node split(Node n, bool connect = true)
deba@109
   265
    {
deba@109
   266
      Node b = addNode();
deba@109
   267
      nodes[b._id].first_out=nodes[n._id].first_out;
deba@109
   268
      nodes[n._id].first_out=-1;
kpeter@370
   269
      for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
kpeter@370
   270
        arcs[i].source=b._id;
kpeter@370
   271
      }
deba@109
   272
      if(connect) addArc(n,b);
deba@109
   273
      return b;
deba@109
   274
    }
deba@109
   275
kpeter@735
   276
    ///Clear the digraph.
kpeter@735
   277
kpeter@735
   278
    ///This function erases all nodes and arcs from the digraph.
kpeter@735
   279
    ///
kpeter@735
   280
    void clear() {
kpeter@735
   281
      Parent::clear();
kpeter@735
   282
    }
kpeter@735
   283
kpeter@735
   284
    /// Reserve memory for nodes.
kpeter@735
   285
kpeter@735
   286
    /// Using this function, it is possible to avoid superfluous memory
kpeter@735
   287
    /// allocation: if you know that the digraph you want to build will
kpeter@735
   288
    /// be large (e.g. it will contain millions of nodes and/or arcs),
kpeter@735
   289
    /// then it is worth reserving space for this amount before starting
kpeter@735
   290
    /// to build the digraph.
kpeter@735
   291
    /// \sa reserveArc()
kpeter@735
   292
    void reserveNode(int n) { nodes.reserve(n); };
kpeter@735
   293
kpeter@735
   294
    /// Reserve memory for arcs.
kpeter@735
   295
kpeter@735
   296
    /// Using this function, it is possible to avoid superfluous memory
kpeter@735
   297
    /// allocation: if you know that the digraph you want to build will
kpeter@735
   298
    /// be large (e.g. it will contain millions of nodes and/or arcs),
kpeter@735
   299
    /// then it is worth reserving space for this amount before starting
kpeter@735
   300
    /// to build the digraph.
kpeter@735
   301
    /// \sa reserveNode()
kpeter@735
   302
    void reserveArc(int m) { arcs.reserve(m); };
kpeter@735
   303
deba@109
   304
  public:
alpar@209
   305
deba@109
   306
    class Snapshot;
deba@109
   307
deba@109
   308
  protected:
deba@109
   309
deba@109
   310
    void restoreSnapshot(const Snapshot &s)
deba@109
   311
    {
deba@109
   312
      while(s.arc_num<arcs.size()) {
deba@109
   313
        Arc arc = arcFromId(arcs.size()-1);
alpar@209
   314
        Parent::notifier(Arc()).erase(arc);
alpar@209
   315
        nodes[arcs.back().source].first_out=arcs.back().next_out;
alpar@209
   316
        nodes[arcs.back().target].first_in=arcs.back().next_in;
alpar@209
   317
        arcs.pop_back();
deba@109
   318
      }
deba@109
   319
      while(s.node_num<nodes.size()) {
deba@109
   320
        Node node = nodeFromId(nodes.size()-1);
alpar@209
   321
        Parent::notifier(Node()).erase(node);
alpar@209
   322
        nodes.pop_back();
deba@109
   323
      }
alpar@209
   324
    }
deba@109
   325
deba@109
   326
  public:
deba@109
   327
kpeter@735
   328
    ///Class to make a snapshot of the digraph and to restore it later.
deba@109
   329
kpeter@735
   330
    ///Class to make a snapshot of the digraph and to restore it later.
deba@109
   331
    ///
deba@109
   332
    ///The newly added nodes and arcs can be removed using the
kpeter@735
   333
    ///restore() function. This is the only way for deleting nodes and/or
kpeter@735
   334
    ///arcs from a SmartDigraph structure.
deba@109
   335
    ///
kpeter@735
   336
    ///\note After a state is restored, you cannot restore a later state, 
kpeter@735
   337
    ///i.e. you cannot add the removed nodes and arcs again using
kpeter@735
   338
    ///another Snapshot instance.
kpeter@735
   339
    ///
kpeter@735
   340
    ///\warning Node splitting cannot be restored.
kpeter@735
   341
    ///\warning The validity of the snapshot is not stored due to
kpeter@735
   342
    ///performance reasons. If you do not use the snapshot correctly,
kpeter@735
   343
    ///it can cause broken program, invalid or not restored state of
kpeter@735
   344
    ///the digraph or no change.
alpar@209
   345
    class Snapshot
deba@109
   346
    {
deba@109
   347
      SmartDigraph *_graph;
deba@109
   348
    protected:
deba@109
   349
      friend class SmartDigraph;
deba@109
   350
      unsigned int node_num;
deba@109
   351
      unsigned int arc_num;
deba@109
   352
    public:
deba@109
   353
      ///Default constructor.
alpar@209
   354
deba@109
   355
      ///Default constructor.
kpeter@735
   356
      ///You have to call save() to actually make a snapshot.
deba@109
   357
      Snapshot() : _graph(0) {}
deba@109
   358
      ///Constructor that immediately makes a snapshot
alpar@209
   359
kpeter@735
   360
      ///This constructor immediately makes a snapshot of the given digraph.
kpeter@735
   361
      ///
kpeter@735
   362
      Snapshot(SmartDigraph &gr) : _graph(&gr) {
alpar@209
   363
        node_num=_graph->nodes.size();
alpar@209
   364
        arc_num=_graph->arcs.size();
deba@109
   365
      }
deba@109
   366
deba@109
   367
      ///Make a snapshot.
deba@109
   368
kpeter@735
   369
      ///This function makes a snapshot of the given digraph.
kpeter@735
   370
      ///It can be called more than once. In case of a repeated
deba@109
   371
      ///call, the previous snapshot gets lost.
kpeter@735
   372
      void save(SmartDigraph &gr) {
kpeter@735
   373
        _graph=&gr;
alpar@209
   374
        node_num=_graph->nodes.size();
alpar@209
   375
        arc_num=_graph->arcs.size();
deba@109
   376
      }
deba@109
   377
deba@109
   378
      ///Undo the changes until a snapshot.
alpar@209
   379
kpeter@735
   380
      ///This function undos the changes until the last snapshot
kpeter@735
   381
      ///created by save() or Snapshot(SmartDigraph&).
deba@109
   382
      void restore()
deba@109
   383
      {
alpar@209
   384
        _graph->restoreSnapshot(*this);
deba@109
   385
      }
deba@109
   386
    };
deba@109
   387
  };
deba@109
   388
deba@109
   389
deba@109
   390
  class SmartGraphBase {
deba@109
   391
deba@109
   392
  protected:
deba@109
   393
deba@109
   394
    struct NodeT {
deba@109
   395
      int first_out;
deba@109
   396
    };
alpar@209
   397
deba@109
   398
    struct ArcT {
deba@109
   399
      int target;
deba@109
   400
      int next_out;
deba@109
   401
    };
deba@109
   402
deba@109
   403
    std::vector<NodeT> nodes;
deba@109
   404
    std::vector<ArcT> arcs;
deba@109
   405
deba@109
   406
    int first_free_arc;
alpar@209
   407
deba@109
   408
  public:
alpar@209
   409
kpeter@617
   410
    typedef SmartGraphBase Graph;
deba@109
   411
deba@109
   412
    class Node;
deba@109
   413
    class Arc;
deba@109
   414
    class Edge;
alpar@209
   415
deba@109
   416
    class Node {
deba@109
   417
      friend class SmartGraphBase;
deba@109
   418
    protected:
deba@109
   419
deba@109
   420
      int _id;
deba@109
   421
      explicit Node(int id) { _id = id;}
deba@109
   422
deba@109
   423
    public:
deba@109
   424
      Node() {}
deba@109
   425
      Node (Invalid) { _id = -1; }
deba@109
   426
      bool operator==(const Node& node) const {return _id == node._id;}
deba@109
   427
      bool operator!=(const Node& node) const {return _id != node._id;}
deba@109
   428
      bool operator<(const Node& node) const {return _id < node._id;}
deba@109
   429
    };
deba@109
   430
deba@109
   431
    class Edge {
deba@109
   432
      friend class SmartGraphBase;
deba@109
   433
    protected:
deba@109
   434
deba@109
   435
      int _id;
deba@109
   436
      explicit Edge(int id) { _id = id;}
deba@109
   437
deba@109
   438
    public:
deba@109
   439
      Edge() {}
deba@109
   440
      Edge (Invalid) { _id = -1; }
deba@109
   441
      bool operator==(const Edge& arc) const {return _id == arc._id;}
deba@109
   442
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
deba@109
   443
      bool operator<(const Edge& arc) const {return _id < arc._id;}
deba@109
   444
    };
deba@109
   445
deba@109
   446
    class Arc {
deba@109
   447
      friend class SmartGraphBase;
deba@109
   448
    protected:
deba@109
   449
deba@109
   450
      int _id;
deba@109
   451
      explicit Arc(int id) { _id = id;}
deba@109
   452
deba@109
   453
    public:
kpeter@329
   454
      operator Edge() const {
kpeter@329
   455
        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
deba@238
   456
      }
deba@109
   457
deba@109
   458
      Arc() {}
deba@109
   459
      Arc (Invalid) { _id = -1; }
deba@109
   460
      bool operator==(const Arc& arc) const {return _id == arc._id;}
deba@109
   461
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
deba@109
   462
      bool operator<(const Arc& arc) const {return _id < arc._id;}
deba@109
   463
    };
deba@109
   464
deba@109
   465
deba@109
   466
deba@109
   467
    SmartGraphBase()
deba@109
   468
      : nodes(), arcs() {}
deba@109
   469
kpeter@368
   470
    typedef True NodeNumTag;
kpeter@368
   471
    typedef True EdgeNumTag;
kpeter@368
   472
    typedef True ArcNumTag;
kpeter@368
   473
kpeter@368
   474
    int nodeNum() const { return nodes.size(); }
kpeter@368
   475
    int edgeNum() const { return arcs.size() / 2; }
kpeter@368
   476
    int arcNum() const { return arcs.size(); }
alpar@209
   477
alpar@209
   478
    int maxNodeId() const { return nodes.size()-1; }
deba@109
   479
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
deba@109
   480
    int maxArcId() const { return arcs.size()-1; }
deba@109
   481
deba@109
   482
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
deba@109
   483
    Node target(Arc e) const { return Node(arcs[e._id].target); }
deba@109
   484
deba@125
   485
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
deba@125
   486
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
deba@109
   487
deba@109
   488
    static bool direction(Arc e) {
deba@109
   489
      return (e._id & 1) == 1;
deba@109
   490
    }
deba@109
   491
deba@109
   492
    static Arc direct(Edge e, bool d) {
deba@109
   493
      return Arc(e._id * 2 + (d ? 1 : 0));
deba@109
   494
    }
deba@109
   495
alpar@209
   496
    void first(Node& node) const {
deba@109
   497
      node._id = nodes.size() - 1;
deba@109
   498
    }
deba@109
   499
kpeter@778
   500
    static void next(Node& node) {
deba@109
   501
      --node._id;
deba@109
   502
    }
deba@109
   503
alpar@209
   504
    void first(Arc& arc) const {
deba@109
   505
      arc._id = arcs.size() - 1;
deba@109
   506
    }
deba@109
   507
kpeter@778
   508
    static void next(Arc& arc) {
deba@109
   509
      --arc._id;
deba@109
   510
    }
deba@109
   511
alpar@209
   512
    void first(Edge& arc) const {
deba@109
   513
      arc._id = arcs.size() / 2 - 1;
deba@109
   514
    }
deba@109
   515
kpeter@778
   516
    static void next(Edge& arc) {
deba@109
   517
      --arc._id;
deba@109
   518
    }
deba@109
   519
deba@109
   520
    void firstOut(Arc &arc, const Node& v) const {
deba@109
   521
      arc._id = nodes[v._id].first_out;
deba@109
   522
    }
deba@109
   523
    void nextOut(Arc &arc) const {
deba@109
   524
      arc._id = arcs[arc._id].next_out;
deba@109
   525
    }
deba@109
   526
deba@109
   527
    void firstIn(Arc &arc, const Node& v) const {
deba@109
   528
      arc._id = ((nodes[v._id].first_out) ^ 1);
deba@109
   529
      if (arc._id == -2) arc._id = -1;
deba@109
   530
    }
deba@109
   531
    void nextIn(Arc &arc) const {
deba@109
   532
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
deba@109
   533
      if (arc._id == -2) arc._id = -1;
deba@109
   534
    }
deba@109
   535
deba@109
   536
    void firstInc(Edge &arc, bool& d, const Node& v) const {
deba@109
   537
      int de = nodes[v._id].first_out;
deba@109
   538
      if (de != -1) {
deba@109
   539
        arc._id = de / 2;
deba@109
   540
        d = ((de & 1) == 1);
deba@109
   541
      } else {
deba@109
   542
        arc._id = -1;
deba@109
   543
        d = true;
deba@109
   544
      }
deba@109
   545
    }
deba@109
   546
    void nextInc(Edge &arc, bool& d) const {
deba@109
   547
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
deba@109
   548
      if (de != -1) {
deba@109
   549
        arc._id = de / 2;
deba@109
   550
        d = ((de & 1) == 1);
deba@109
   551
      } else {
deba@109
   552
        arc._id = -1;
alpar@209
   553
        d = true;
deba@109
   554
      }
deba@109
   555
    }
alpar@209
   556
deba@109
   557
    static int id(Node v) { return v._id; }
deba@109
   558
    static int id(Arc e) { return e._id; }
deba@109
   559
    static int id(Edge e) { return e._id; }
deba@109
   560
deba@109
   561
    static Node nodeFromId(int id) { return Node(id);}
deba@109
   562
    static Arc arcFromId(int id) { return Arc(id);}
deba@109
   563
    static Edge edgeFromId(int id) { return Edge(id);}
deba@109
   564
alpar@209
   565
    bool valid(Node n) const {
alpar@209
   566
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
deba@149
   567
    }
alpar@209
   568
    bool valid(Arc a) const {
deba@149
   569
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
deba@149
   570
    }
alpar@209
   571
    bool valid(Edge e) const {
alpar@209
   572
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
deba@149
   573
    }
deba@149
   574
alpar@209
   575
    Node addNode() {
deba@109
   576
      int n = nodes.size();
deba@109
   577
      nodes.push_back(NodeT());
deba@109
   578
      nodes[n].first_out = -1;
alpar@209
   579
deba@109
   580
      return Node(n);
deba@109
   581
    }
alpar@209
   582
deba@138
   583
    Edge addEdge(Node u, Node v) {
deba@109
   584
      int n = arcs.size();
deba@109
   585
      arcs.push_back(ArcT());
deba@109
   586
      arcs.push_back(ArcT());
alpar@209
   587
deba@109
   588
      arcs[n].target = u._id;
deba@109
   589
      arcs[n | 1].target = v._id;
deba@109
   590
deba@109
   591
      arcs[n].next_out = nodes[v._id].first_out;
deba@109
   592
      nodes[v._id].first_out = n;
deba@109
   593
alpar@209
   594
      arcs[n | 1].next_out = nodes[u._id].first_out;
deba@109
   595
      nodes[u._id].first_out = (n | 1);
deba@109
   596
deba@109
   597
      return Edge(n / 2);
deba@109
   598
    }
alpar@209
   599
deba@109
   600
    void clear() {
deba@109
   601
      arcs.clear();
deba@109
   602
      nodes.clear();
deba@109
   603
    }
deba@109
   604
deba@109
   605
  };
deba@109
   606
deba@109
   607
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
deba@109
   608
deba@109
   609
  /// \ingroup graphs
deba@109
   610
  ///
deba@109
   611
  /// \brief A smart undirected graph class.
deba@109
   612
  ///
kpeter@735
   613
  /// \ref SmartGraph is a simple and fast graph implementation.
kpeter@735
   614
  /// It is also quite memory efficient but at the price
kpeter@735
   615
  /// that it does not support node and edge deletion 
kpeter@735
   616
  /// (except for the Snapshot feature).
deba@109
   617
  ///
kpeter@735
   618
  /// This type fully conforms to the \ref concepts::Graph "Graph concept"
kpeter@735
   619
  /// and it also provides some additional functionalities.
kpeter@735
   620
  /// Most of its member functions and nested classes are documented
kpeter@735
   621
  /// only in the concept class.
kpeter@735
   622
  ///
kpeter@735
   623
  /// \sa concepts::Graph
kpeter@735
   624
  /// \sa SmartDigraph
deba@109
   625
  class SmartGraph : public ExtendedSmartGraphBase {
kpeter@617
   626
    typedef ExtendedSmartGraphBase Parent;
kpeter@617
   627
deba@109
   628
  private:
kpeter@735
   629
    /// Graphs are \e not copy constructible. Use GraphCopy instead.
deba@109
   630
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
kpeter@735
   631
    /// \brief Assignment of a graph to another one is \e not allowed.
kpeter@735
   632
    /// Use GraphCopy instead.
deba@109
   633
    void operator=(const SmartGraph &) {}
deba@109
   634
deba@109
   635
  public:
deba@109
   636
deba@109
   637
    /// Constructor
alpar@209
   638
deba@109
   639
    /// Constructor.
deba@109
   640
    ///
deba@109
   641
    SmartGraph() {}
deba@109
   642
kpeter@735
   643
    /// \brief Add a new node to the graph.
kpeter@735
   644
    ///
kpeter@735
   645
    /// This function adds a new node to the graph.
kpeter@559
   646
    /// \return The new node.
deba@109
   647
    Node addNode() { return Parent::addNode(); }
alpar@209
   648
kpeter@735
   649
    /// \brief Add a new edge to the graph.
kpeter@735
   650
    ///
kpeter@735
   651
    /// This function adds a new edge to the graph between nodes
kpeter@735
   652
    /// \c u and \c v with inherent orientation from node \c u to
kpeter@735
   653
    /// node \c v.
kpeter@735
   654
    /// \return The new edge.
kpeter@735
   655
    Edge addEdge(Node u, Node v) {
kpeter@735
   656
      return Parent::addEdge(u, v);
deba@109
   657
    }
deba@109
   658
deba@149
   659
    /// \brief Node validity check
deba@149
   660
    ///
kpeter@735
   661
    /// This function gives back \c true if the given node is valid,
kpeter@735
   662
    /// i.e. it is a real node of the graph.
deba@149
   663
    ///
deba@149
   664
    /// \warning A removed node (using Snapshot) could become valid again
kpeter@735
   665
    /// if new nodes are added to the graph.
deba@149
   666
    bool valid(Node n) const { return Parent::valid(n); }
deba@149
   667
kpeter@735
   668
    /// \brief Edge validity check
kpeter@735
   669
    ///
kpeter@735
   670
    /// This function gives back \c true if the given edge is valid,
kpeter@735
   671
    /// i.e. it is a real edge of the graph.
kpeter@735
   672
    ///
kpeter@735
   673
    /// \warning A removed edge (using Snapshot) could become valid again
kpeter@735
   674
    /// if new edges are added to the graph.
kpeter@735
   675
    bool valid(Edge e) const { return Parent::valid(e); }
kpeter@735
   676
deba@149
   677
    /// \brief Arc validity check
deba@149
   678
    ///
kpeter@735
   679
    /// This function gives back \c true if the given arc is valid,
kpeter@735
   680
    /// i.e. it is a real arc of the graph.
deba@149
   681
    ///
deba@149
   682
    /// \warning A removed arc (using Snapshot) could become valid again
kpeter@735
   683
    /// if new edges are added to the graph.
deba@149
   684
    bool valid(Arc a) const { return Parent::valid(a); }
deba@149
   685
deba@109
   686
    ///Clear the graph.
alpar@209
   687
kpeter@735
   688
    ///This function erases all nodes and arcs from the graph.
deba@109
   689
    ///
deba@109
   690
    void clear() {
deba@109
   691
      Parent::clear();
deba@109
   692
    }
deba@109
   693
kpeter@736
   694
    /// Reserve memory for nodes.
kpeter@736
   695
kpeter@736
   696
    /// Using this function, it is possible to avoid superfluous memory
kpeter@736
   697
    /// allocation: if you know that the graph you want to build will
kpeter@736
   698
    /// be large (e.g. it will contain millions of nodes and/or edges),
kpeter@736
   699
    /// then it is worth reserving space for this amount before starting
kpeter@736
   700
    /// to build the graph.
kpeter@736
   701
    /// \sa reserveEdge()
kpeter@736
   702
    void reserveNode(int n) { nodes.reserve(n); };
kpeter@736
   703
kpeter@736
   704
    /// Reserve memory for edges.
kpeter@736
   705
kpeter@736
   706
    /// Using this function, it is possible to avoid superfluous memory
kpeter@736
   707
    /// allocation: if you know that the graph you want to build will
kpeter@736
   708
    /// be large (e.g. it will contain millions of nodes and/or edges),
kpeter@736
   709
    /// then it is worth reserving space for this amount before starting
kpeter@736
   710
    /// to build the graph.
kpeter@736
   711
    /// \sa reserveNode()
kpeter@736
   712
    void reserveEdge(int m) { arcs.reserve(2 * m); };
kpeter@736
   713
deba@109
   714
  public:
alpar@209
   715
deba@109
   716
    class Snapshot;
deba@109
   717
deba@109
   718
  protected:
deba@109
   719
deba@109
   720
    void saveSnapshot(Snapshot &s)
deba@109
   721
    {
deba@109
   722
      s._graph = this;
deba@109
   723
      s.node_num = nodes.size();
deba@109
   724
      s.arc_num = arcs.size();
deba@109
   725
    }
deba@109
   726
deba@109
   727
    void restoreSnapshot(const Snapshot &s)
deba@109
   728
    {
deba@109
   729
      while(s.arc_num<arcs.size()) {
deba@109
   730
        int n=arcs.size()-1;
deba@109
   731
        Edge arc=edgeFromId(n/2);
alpar@209
   732
        Parent::notifier(Edge()).erase(arc);
deba@109
   733
        std::vector<Arc> dir;
deba@109
   734
        dir.push_back(arcFromId(n));
deba@109
   735
        dir.push_back(arcFromId(n-1));
alpar@209
   736
        Parent::notifier(Arc()).erase(dir);
kpeter@373
   737
        nodes[arcs[n-1].target].first_out=arcs[n].next_out;
kpeter@373
   738
        nodes[arcs[n].target].first_out=arcs[n-1].next_out;
alpar@209
   739
        arcs.pop_back();
alpar@209
   740
        arcs.pop_back();
deba@109
   741
      }
deba@109
   742
      while(s.node_num<nodes.size()) {
deba@109
   743
        int n=nodes.size()-1;
deba@109
   744
        Node node = nodeFromId(n);
alpar@209
   745
        Parent::notifier(Node()).erase(node);
alpar@209
   746
        nodes.pop_back();
deba@109
   747
      }
alpar@209
   748
    }
deba@109
   749
deba@109
   750
  public:
deba@109
   751
kpeter@735
   752
    ///Class to make a snapshot of the graph and to restore it later.
deba@109
   753
kpeter@735
   754
    ///Class to make a snapshot of the graph and to restore it later.
deba@109
   755
    ///
kpeter@735
   756
    ///The newly added nodes and edges can be removed using the
kpeter@735
   757
    ///restore() function. This is the only way for deleting nodes and/or
kpeter@735
   758
    ///edges from a SmartGraph structure.
deba@109
   759
    ///
kpeter@735
   760
    ///\note After a state is restored, you cannot restore a later state, 
kpeter@735
   761
    ///i.e. you cannot add the removed nodes and edges again using
kpeter@735
   762
    ///another Snapshot instance.
deba@109
   763
    ///
kpeter@735
   764
    ///\warning The validity of the snapshot is not stored due to
kpeter@735
   765
    ///performance reasons. If you do not use the snapshot correctly,
kpeter@735
   766
    ///it can cause broken program, invalid or not restored state of
kpeter@735
   767
    ///the graph or no change.
alpar@209
   768
    class Snapshot
deba@109
   769
    {
deba@109
   770
      SmartGraph *_graph;
deba@109
   771
    protected:
deba@109
   772
      friend class SmartGraph;
deba@109
   773
      unsigned int node_num;
deba@109
   774
      unsigned int arc_num;
deba@109
   775
    public:
deba@109
   776
      ///Default constructor.
alpar@209
   777
deba@109
   778
      ///Default constructor.
kpeter@735
   779
      ///You have to call save() to actually make a snapshot.
deba@109
   780
      Snapshot() : _graph(0) {}
deba@109
   781
      ///Constructor that immediately makes a snapshot
alpar@209
   782
kpeter@735
   783
      /// This constructor immediately makes a snapshot of the given graph.
kpeter@735
   784
      ///
kpeter@735
   785
      Snapshot(SmartGraph &gr) {
kpeter@735
   786
        gr.saveSnapshot(*this);
deba@109
   787
      }
deba@109
   788
deba@109
   789
      ///Make a snapshot.
deba@109
   790
kpeter@735
   791
      ///This function makes a snapshot of the given graph.
kpeter@735
   792
      ///It can be called more than once. In case of a repeated
deba@109
   793
      ///call, the previous snapshot gets lost.
kpeter@735
   794
      void save(SmartGraph &gr)
deba@109
   795
      {
kpeter@735
   796
        gr.saveSnapshot(*this);
deba@109
   797
      }
deba@109
   798
kpeter@735
   799
      ///Undo the changes until the last snapshot.
alpar@209
   800
kpeter@735
   801
      ///This function undos the changes until the last snapshot
kpeter@735
   802
      ///created by save() or Snapshot(SmartGraph&).
deba@109
   803
      void restore()
deba@109
   804
      {
deba@109
   805
        _graph->restoreSnapshot(*this);
deba@109
   806
      }
deba@109
   807
    };
deba@109
   808
  };
alpar@209
   809
deba@109
   810
} //namespace lemon
deba@109
   811
deba@109
   812
deba@109
   813
#endif //LEMON_SMART_GRAPH_H