lemon/smart_graph.h
author deba
Thu, 17 Nov 2005 10:46:38 +0000
changeset 1813 5c5d1574667d
parent 1770 657de7e5043c
child 1820 22099ef840d7
permissions -rw-r--r--
Bug fix
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@105
    16
alpar@921
    17
#ifndef LEMON_SMART_GRAPH_H
alpar@921
    18
#define LEMON_SMART_GRAPH_H
alpar@104
    19
klao@491
    20
///\ingroup graphs
alpar@242
    21
///\file
deba@1692
    22
///\brief SmartGraph and UndirSmartGraph classes.
alpar@242
    23
alpar@104
    24
#include <vector>
alpar@104
    25
alpar@921
    26
#include <lemon/invalid.h>
alpar@157
    27
deba@1307
    28
#include <lemon/bits/clearable_graph_extender.h>
deba@1307
    29
#include <lemon/bits/extendable_graph_extender.h>
deba@1307
    30
#include <lemon/bits/iterable_graph_extender.h>
deba@1307
    31
#include <lemon/bits/alteration_notifier.h>
deba@1307
    32
#include <lemon/bits/default_map.h>
deba@1791
    33
#include <lemon/bits/graph_extender.h>
klao@1034
    34
klao@977
    35
#include <lemon/utility.h>
deba@782
    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@1669
   222
  typedef ClearableGraphExtender<
deba@1669
   223
    ExtendableGraphExtender<
deba@1669
   224
    MappableGraphExtender<
deba@1669
   225
    IterableGraphExtender<
deba@1791
   226
    AlterableGraphExtender<
deba@1791
   227
    GraphExtender<SmartGraphBase> > > > > > ExtendedSmartGraphBase;
deba@937
   228
deba@1791
   229
  /// \ingroup graphs
alpar@1161
   230
alpar@950
   231
  ///A smart graph class.
deba@937
   232
alpar@950
   233
  ///This is a simple and fast graph implementation.
alpar@950
   234
  ///It is also quite memory efficient, but at the price
alpar@974
   235
  ///that <b> it does support only limited (only stack-like)
alpar@974
   236
  ///node and edge deletions</b>.
alpar@950
   237
  ///It conforms to 
klao@959
   238
  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
klao@959
   239
  ///\sa concept::ExtendableGraph.
alpar@950
   240
  ///
alpar@950
   241
  ///\author Alpar Juttner
deba@1669
   242
  class SmartGraph : public ExtendedSmartGraphBase {
alpar@969
   243
  public:
alpar@973
   244
    
alpar@1770
   245
    class Snapshot;
alpar@1770
   246
    friend class Snapshot;
alpar@973
   247
alpar@1011
   248
  protected:
alpar@1770
   249
    void restoreSnapshot(const Snapshot &s)
alpar@973
   250
    {
alpar@1457
   251
      while(s.edge_num<edges.size()) {
deba@1040
   252
	Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
alpar@986
   253
	nodes[edges.back().target].first_in=edges.back().next_in;
alpar@986
   254
	nodes[edges.back().source].first_out=edges.back().next_out;
alpar@973
   255
	edges.pop_back();
alpar@973
   256
      }
alpar@973
   257
      //nodes.resize(s.nodes_num);
alpar@1457
   258
      while(s.node_num<nodes.size()) {
deba@1040
   259
	Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
alpar@973
   260
	nodes.pop_back();
alpar@973
   261
      }
alpar@1011
   262
    }    
alpar@1011
   263
alpar@1011
   264
  public:
alpar@1284
   265
alpar@1284
   266
    ///Split a node.
alpar@1284
   267
    
alpar@1284
   268
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   269
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1284
   270
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1284
   271
    ///from \c n to the newly created node is also added.
alpar@1284
   272
    ///\return The newly created node.
alpar@1284
   273
    ///
alpar@1284
   274
    ///\note The <tt>Edge</tt>s
alpar@1284
   275
    ///referencing a moved edge remain
alpar@1284
   276
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1284
   277
    ///may be invalidated.
alpar@1770
   278
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1284
   279
    ///feature.
alpar@1284
   280
    ///\todo It could be implemented in a bit faster way.
alpar@1284
   281
    Node split(Node n, bool connect = true) 
alpar@1284
   282
    {
deba@1718
   283
      Node b = _split(n,connect);
deba@1718
   284
      return b;
alpar@1284
   285
    }
alpar@1284
   286
  
alpar@1284
   287
alpar@1011
   288
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   289
alpar@1011
   290
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   291
    ///
alpar@1011
   292
    ///The newly added nodes and edges can be removed using the
alpar@1011
   293
    ///restore() function.
alpar@1011
   294
    ///\note After you restore a state, you cannot restore
alpar@1011
   295
    ///a later state, in other word you cannot add again the edges deleted
alpar@1770
   296
    ///by restore() using another Snapshot instance.
alpar@1011
   297
    ///
alpar@1770
   298
    class Snapshot 
alpar@1011
   299
    {
alpar@1011
   300
      SmartGraph *g;
alpar@1011
   301
    protected:
alpar@1011
   302
      friend class SmartGraph;
alpar@1011
   303
      unsigned int node_num;
alpar@1011
   304
      unsigned int edge_num;
alpar@1011
   305
    public:
zsuzska@1274
   306
      ///Default constructor.
alpar@1011
   307
      
zsuzska@1274
   308
      ///Default constructor.
alpar@1011
   309
      ///To actually make a snapshot you must call save().
alpar@1011
   310
      ///
alpar@1770
   311
      Snapshot() : g(0) {}
alpar@1011
   312
      ///Constructor that immediately makes a snapshot
alpar@1011
   313
      
alpar@1011
   314
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   315
      ///\param _g The graph we make a snapshot of.
alpar@1770
   316
      Snapshot(SmartGraph &_g) :g(&_g) {
alpar@1011
   317
	node_num=g->nodes.size();
alpar@1011
   318
	edge_num=g->edges.size();
alpar@1011
   319
      }
alpar@1011
   320
alpar@1011
   321
      ///Make a snapshot.
alpar@1011
   322
alpar@1011
   323
      ///Make a snapshot of the graph.
alpar@1011
   324
      ///
alpar@1011
   325
      ///This function can be called more than once. In case of a repeated
alpar@1011
   326
      ///call, the previous snapshot gets lost.
alpar@1011
   327
      ///\param _g The graph we make the snapshot of.
alpar@1011
   328
      void save(SmartGraph &_g) 
alpar@1011
   329
      {
alpar@1011
   330
	g=&_g;
alpar@1011
   331
	node_num=g->nodes.size();
alpar@1011
   332
	edge_num=g->edges.size();
alpar@1011
   333
      }
alpar@1011
   334
alpar@1011
   335
      ///Undo the changes until a snapshot.
alpar@1011
   336
      
alpar@1011
   337
      ///Undo the changes until a snapshot created by save().
alpar@1011
   338
      ///
alpar@1011
   339
      ///\note After you restored a state, you cannot restore
alpar@1011
   340
      ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   341
      ///by restore().
alpar@1011
   342
      ///
alpar@1011
   343
      ///\todo This function might be called undo().
alpar@1011
   344
      
alpar@1011
   345
      void restore()
alpar@1011
   346
      {
alpar@1770
   347
	g->restoreSnapshot(*this);
alpar@1011
   348
      }
alpar@1011
   349
    };
alpar@973
   350
  };
klao@1034
   351
klao@1034
   352
klao@1034
   353
  /**************** Undirected List Graph ****************/
klao@1034
   354
klao@1034
   355
  typedef ClearableUndirGraphExtender<
klao@1034
   356
    ExtendableUndirGraphExtender<
klao@1034
   357
    MappableUndirGraphExtender<
klao@1034
   358
    IterableUndirGraphExtender<
klao@1034
   359
    AlterableUndirGraphExtender<
deba@1669
   360
    UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
klao@1034
   361
alpar@1035
   362
  ///A smart undirected graph class.
alpar@1035
   363
alpar@1035
   364
  ///This is a simple and fast undirected graph implementation.
alpar@1035
   365
  ///It is also quite memory efficient, but at the price
alpar@1035
   366
  ///that <b> it does support only limited (only stack-like)
alpar@1035
   367
  ///node and edge deletions</b>.
alpar@1035
   368
  ///Except from this it conforms to 
alpar@1035
   369
  ///the \ref concept::UndirGraph "UndirGraph" concept.
alpar@1035
   370
  ///\sa concept::UndirGraph.
alpar@1035
   371
  ///
alpar@1770
   372
  ///\todo Snapshot hasn't been implemented yet.
alpar@1035
   373
  ///
deba@1669
   374
  class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
klao@1034
   375
  };
klao@1034
   376
alpar@950
   377
  
alpar@407
   378
  /// @}  
alpar@921
   379
} //namespace lemon
alpar@104
   380
alpar@157
   381
alpar@921
   382
#endif //LEMON_SMART_GRAPH_H