lemon/smart_graph.h
author deba
Fri, 30 Jun 2006 12:14:36 +0000
changeset 2115 4cd528a30ec1
parent 2111 ea1fa1bc3f6d
child 2116 b6a68c15a6a3
permissions -rw-r--r--
Splitted graph files
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@2115
    24
///\brief SmartGraph class.
alpar@242
    25
alpar@104
    26
#include <vector>
alpar@104
    27
deba@1993
    28
#include <lemon/bits/invalid.h>
alpar@157
    29
deba@1791
    30
#include <lemon/bits/graph_extender.h>
klao@1034
    31
deba@1993
    32
#include <lemon/bits/utility.h>
deba@782
    33
deba@1979
    34
#include <lemon/bits/graph_extender.h>
deba@1979
    35
alpar@921
    36
namespace lemon {
alpar@104
    37
alpar@973
    38
  class SmartGraph;
alpar@969
    39
  ///Base of SmartGraph
alpar@969
    40
alpar@969
    41
  ///Base of SmartGraph
alpar@969
    42
  ///
klao@946
    43
  class SmartGraphBase {
alpar@104
    44
alpar@973
    45
    friend class SmatGraph;
alpar@973
    46
alpar@973
    47
  protected:
alpar@104
    48
    struct NodeT 
alpar@104
    49
    {
alpar@104
    50
      int first_in,first_out;      
alpar@157
    51
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    52
    };
alpar@104
    53
    struct EdgeT 
alpar@104
    54
    {
alpar@986
    55
      int target, source, next_in, next_out;      
alpar@104
    56
      //FIXME: is this necessary?
alpar@157
    57
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    58
    };
alpar@104
    59
alpar@104
    60
    std::vector<NodeT> nodes;
alpar@129
    61
alpar@104
    62
    std::vector<EdgeT> edges;
alpar@104
    63
    
alpar@185
    64
    
alpar@104
    65
  public:
deba@782
    66
klao@946
    67
    typedef SmartGraphBase Graph;
alpar@104
    68
alpar@164
    69
    class Node;
alpar@164
    70
    class Edge;
alpar@108
    71
alpar@104
    72
    
alpar@104
    73
  public:
alpar@104
    74
klao@946
    75
    SmartGraphBase() : nodes(), edges() { }
deba@1718
    76
    SmartGraphBase(const SmartGraphBase &_g) 
deba@1718
    77
      : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    78
    
klao@977
    79
    typedef True NodeNumTag;
klao@977
    80
    typedef True EdgeNumTag;
klao@977
    81
alpar@813
    82
    ///Number of nodes.
alpar@813
    83
    int nodeNum() const { return nodes.size(); }
alpar@813
    84
    ///Number of edges.
alpar@813
    85
    int edgeNum() const { return edges.size(); }
alpar@104
    86
alpar@813
    87
    /// Maximum node ID.
alpar@813
    88
    
alpar@813
    89
    /// Maximum node ID.
alpar@813
    90
    ///\sa id(Node)
deba@1791
    91
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
    92
    /// Maximum edge ID.
alpar@813
    93
    
alpar@813
    94
    /// Maximum edge ID.
alpar@813
    95
    ///\sa id(Edge)
deba@1791
    96
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
    97
alpar@986
    98
    Node source(Edge e) const { return edges[e.n].source; }
alpar@986
    99
    Node target(Edge e) const { return edges[e.n].target; }
alpar@104
   100
alpar@813
   101
    /// Node ID.
alpar@813
   102
    
alpar@813
   103
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@1791
   104
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@1791
   105
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   106
    ///
alpar@813
   107
    /// The ID of the \ref INVALID node is -1.
alpar@813
   108
    ///\return The ID of the node \c v. 
alpar@713
   109
    static int id(Node v) { return v.n; }
alpar@813
   110
    /// Edge ID.
alpar@813
   111
    
alpar@813
   112
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@1791
   113
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@1791
   114
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   115
    ///
alpar@813
   116
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   117
    ///\return The ID of the edge \c e. 
alpar@713
   118
    static int id(Edge e) { return e.n; }
alpar@104
   119
deba@2076
   120
    /// \brief Returns the node from its \c id.
deba@2076
   121
    ///
deba@2076
   122
    /// Returns the node from its \c id. If there is not node
deba@2076
   123
    /// with the given id the effect of the function is undefinied.
deba@1791
   124
    static Node nodeFromId(int id) { return Node(id);}
deba@1106
   125
deba@2076
   126
    /// \brief Returns the edge from its \c id.
deba@2076
   127
    ///
deba@2076
   128
    /// Returns the edge from its \c id. If there is not edge
deba@2076
   129
    /// with the given id the effect of the function is undefinied.
deba@1791
   130
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   131
alpar@164
   132
    Node addNode() {
alpar@164
   133
      Node n; n.n=nodes.size();
alpar@104
   134
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@104
   135
      return n;
alpar@104
   136
    }
alpar@108
   137
    
alpar@164
   138
    Edge addEdge(Node u, Node v) {
alpar@164
   139
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@986
   140
      edges[e.n].source=u.n; edges[e.n].target=v.n;
alpar@104
   141
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   142
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   143
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   144
alpar@104
   145
      return e;
alpar@104
   146
    }
alpar@104
   147
deba@782
   148
    void clear() {
deba@782
   149
      edges.clear();
deba@782
   150
      nodes.clear();
deba@782
   151
    }
alpar@104
   152
klao@946
   153
alpar@164
   154
    class Node {
klao@946
   155
      friend class SmartGraphBase;
alpar@973
   156
      friend class SmartGraph;
alpar@104
   157
alpar@104
   158
    protected:
alpar@104
   159
      int n;
alpar@164
   160
      Node(int nn) {n=nn;}
alpar@104
   161
    public:
alpar@164
   162
      Node() {}
alpar@503
   163
      Node (Invalid) { n=-1; }
alpar@164
   164
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   165
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   166
      bool operator<(const Node i) const {return n<i.n;}
alpar@104
   167
    };
alpar@104
   168
    
alpar@104
   169
alpar@164
   170
    class Edge {
klao@946
   171
      friend class SmartGraphBase;
alpar@973
   172
      friend class SmartGraph;
alpar@185
   173
alpar@104
   174
    protected:
alpar@104
   175
      int n;
alpar@905
   176
      Edge(int nn) {n=nn;}
alpar@706
   177
    public:
alpar@164
   178
      Edge() { }
marci@174
   179
      Edge (Invalid) { n=-1; }
alpar@164
   180
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   181
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   182
      bool operator<(const Edge i) const {return n<i.n;}
klao@946
   183
    };
alpar@905
   184
klao@946
   185
    void first(Node& node) const {
klao@946
   186
      node.n = nodes.size() - 1;
klao@946
   187
    }
klao@946
   188
klao@946
   189
    static void next(Node& node) {
klao@946
   190
      --node.n;
klao@946
   191
    }
klao@946
   192
klao@946
   193
    void first(Edge& edge) const {
klao@946
   194
      edge.n = edges.size() - 1;
klao@946
   195
    }
klao@946
   196
klao@946
   197
    static void next(Edge& edge) {
klao@946
   198
      --edge.n;
klao@946
   199
    }
klao@946
   200
klao@946
   201
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   202
      edge.n = nodes[node.n].first_out;
klao@946
   203
    }
klao@946
   204
klao@946
   205
    void nextOut(Edge& edge) const {
klao@946
   206
      edge.n = edges[edge.n].next_out;
klao@946
   207
    }
klao@946
   208
klao@946
   209
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   210
      edge.n = nodes[node.n].first_in;
klao@946
   211
    }
alpar@104
   212
    
klao@946
   213
    void nextIn(Edge& edge) const {
klao@946
   214
      edge.n = edges[edge.n].next_in;
klao@946
   215
    }
alpar@105
   216
alpar@1284
   217
    Node _split(Node n, bool connect = true)
alpar@1284
   218
    {
alpar@1284
   219
      Node b = addNode();
alpar@1284
   220
      nodes[b.n].first_out=nodes[n.n].first_out;
alpar@1284
   221
      nodes[n.n].first_out=-1;
alpar@1284
   222
      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
alpar@1284
   223
      if(connect) addEdge(n,b);
alpar@1284
   224
      return b;
alpar@1284
   225
    }
alpar@1284
   226
alpar@104
   227
  };
alpar@185
   228
deba@1979
   229
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
deba@937
   230
deba@1791
   231
  /// \ingroup graphs
alpar@1161
   232
alpar@950
   233
  ///A smart graph class.
deba@937
   234
alpar@950
   235
  ///This is a simple and fast graph implementation.
alpar@950
   236
  ///It is also quite memory efficient, but at the price
alpar@974
   237
  ///that <b> it does support only limited (only stack-like)
alpar@974
   238
  ///node and edge deletions</b>.
alpar@950
   239
  ///It conforms to 
deba@2111
   240
  ///the \ref concept::Graph "Graph" concept.
deba@2111
   241
  ///\sa concept::Graph.
alpar@950
   242
  ///
alpar@950
   243
  ///\author Alpar Juttner
deba@1669
   244
  class SmartGraph : public ExtendedSmartGraphBase {
alpar@969
   245
  public:
deba@1979
   246
deba@1979
   247
    typedef ExtendedSmartGraphBase Parent;
deba@1979
   248
alpar@1770
   249
    class Snapshot;
alpar@1770
   250
    friend class Snapshot;
alpar@973
   251
alpar@1011
   252
  protected:
alpar@1770
   253
    void restoreSnapshot(const Snapshot &s)
alpar@973
   254
    {
alpar@1457
   255
      while(s.edge_num<edges.size()) {
deba@1040
   256
	Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
alpar@986
   257
	nodes[edges.back().target].first_in=edges.back().next_in;
alpar@986
   258
	nodes[edges.back().source].first_out=edges.back().next_out;
alpar@973
   259
	edges.pop_back();
alpar@973
   260
      }
alpar@973
   261
      //nodes.resize(s.nodes_num);
alpar@1457
   262
      while(s.node_num<nodes.size()) {
deba@1040
   263
	Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
alpar@973
   264
	nodes.pop_back();
alpar@973
   265
      }
alpar@1011
   266
    }    
alpar@1011
   267
alpar@1011
   268
  public:
alpar@1284
   269
alpar@1284
   270
    ///Split a node.
alpar@1284
   271
    
alpar@1284
   272
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   273
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1284
   274
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1284
   275
    ///from \c n to the newly created node is also added.
alpar@1284
   276
    ///\return The newly created node.
alpar@1284
   277
    ///
alpar@1284
   278
    ///\note The <tt>Edge</tt>s
alpar@1284
   279
    ///referencing a moved edge remain
alpar@1284
   280
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1284
   281
    ///may be invalidated.
alpar@1770
   282
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1284
   283
    ///feature.
alpar@1284
   284
    ///\todo It could be implemented in a bit faster way.
alpar@1284
   285
    Node split(Node n, bool connect = true) 
alpar@1284
   286
    {
deba@1718
   287
      Node b = _split(n,connect);
deba@1718
   288
      return b;
alpar@1284
   289
    }
alpar@1284
   290
  
alpar@1284
   291
alpar@1011
   292
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   293
alpar@1011
   294
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   295
    ///
alpar@1011
   296
    ///The newly added nodes and edges can be removed using the
alpar@1011
   297
    ///restore() function.
alpar@1011
   298
    ///\note After you restore a state, you cannot restore
alpar@1011
   299
    ///a later state, in other word you cannot add again the edges deleted
alpar@1770
   300
    ///by restore() using another Snapshot instance.
alpar@1011
   301
    ///
alpar@1770
   302
    class Snapshot 
alpar@1011
   303
    {
alpar@1011
   304
      SmartGraph *g;
alpar@1011
   305
    protected:
alpar@1011
   306
      friend class SmartGraph;
alpar@1011
   307
      unsigned int node_num;
alpar@1011
   308
      unsigned int edge_num;
alpar@1011
   309
    public:
zsuzska@1274
   310
      ///Default constructor.
alpar@1011
   311
      
zsuzska@1274
   312
      ///Default constructor.
alpar@1011
   313
      ///To actually make a snapshot you must call save().
alpar@1011
   314
      ///
alpar@1770
   315
      Snapshot() : g(0) {}
alpar@1011
   316
      ///Constructor that immediately makes a snapshot
alpar@1011
   317
      
alpar@1011
   318
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   319
      ///\param _g The graph we make a snapshot of.
alpar@1770
   320
      Snapshot(SmartGraph &_g) :g(&_g) {
alpar@1011
   321
	node_num=g->nodes.size();
alpar@1011
   322
	edge_num=g->edges.size();
alpar@1011
   323
      }
alpar@1011
   324
alpar@1011
   325
      ///Make a snapshot.
alpar@1011
   326
alpar@1011
   327
      ///Make a snapshot of the graph.
alpar@1011
   328
      ///
alpar@1011
   329
      ///This function can be called more than once. In case of a repeated
alpar@1011
   330
      ///call, the previous snapshot gets lost.
alpar@1011
   331
      ///\param _g The graph we make the snapshot of.
alpar@1011
   332
      void save(SmartGraph &_g) 
alpar@1011
   333
      {
alpar@1011
   334
	g=&_g;
alpar@1011
   335
	node_num=g->nodes.size();
alpar@1011
   336
	edge_num=g->edges.size();
alpar@1011
   337
      }
alpar@1011
   338
alpar@1011
   339
      ///Undo the changes until a snapshot.
alpar@1011
   340
      
alpar@1011
   341
      ///Undo the changes until a snapshot created by save().
alpar@1011
   342
      ///
alpar@1011
   343
      ///\note After you restored a state, you cannot restore
alpar@1011
   344
      ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   345
      ///by restore().
alpar@1011
   346
      ///
alpar@1011
   347
      ///\todo This function might be called undo().
alpar@1011
   348
      
alpar@1011
   349
      void restore()
alpar@1011
   350
      {
alpar@1770
   351
	g->restoreSnapshot(*this);
alpar@1011
   352
      }
alpar@1011
   353
    };
alpar@973
   354
  };
klao@1034
   355
klao@1034
   356
alpar@921
   357
} //namespace lemon
alpar@104
   358
alpar@157
   359
alpar@921
   360
#endif //LEMON_SMART_GRAPH_H