lemon/smart_graph.h
author alpar
Fri, 19 Aug 2005 12:09:21 +0000
changeset 1644 62548b317e65
parent 1631 e15162d8eca1
child 1669 66ae78d29f1e
permissions -rw-r--r--
Put an end to every gcc-4.0 warning.
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
alpar@242
    22
///\brief SmartGraph and SymSmartGraph 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>
klao@946
    33
deba@1307
    34
#include <lemon/bits/undir_graph_extender.h>
klao@1034
    35
klao@977
    36
#include <lemon/utility.h>
deba@782
    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() { }
klao@946
    78
    SmartGraphBase(const SmartGraphBase &_g) : 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)
alpar@1631
    92
    int maxId(Node) 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)
alpar@1631
    97
    int maxId(Edge) 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
alpar@1631
   105
    /// \ref maxId(Node). The range of the ID's is not surely continuous
alpar@1631
   106
    /// and the greatest node ID can be actually less then \ref maxId(Node).
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
alpar@1631
   114
    /// \ref maxId(Edge). The range of the ID's is not surely continuous
alpar@1631
   115
    /// and the greatest edge ID can be actually less then \ref maxId(Edge).
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@1106
   121
    static Node fromId(int id, Node) { return Node(id);}
deba@1106
   122
deba@1106
   123
    static Edge fromId(int id, Edge) { 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@1641
   153
      ///\e
alpar@1641
   154
alpar@973
   155
      ///\todo It should be removed (or at least define a setToId() instead).
alpar@973
   156
      ///
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@973
   173
      ///\todo It should be removed (or at least define a setToId() instead).
alpar@973
   174
      ///
alpar@905
   175
      Edge(int nn) {n=nn;}
alpar@706
   176
    public:
alpar@164
   177
      Edge() { }
marci@174
   178
      Edge (Invalid) { n=-1; }
alpar@164
   179
      bool operator==(const Edge i) const {return n==i.n;}
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;}
klao@946
   182
    };
alpar@905
   183
klao@946
   184
    void first(Node& node) const {
klao@946
   185
      node.n = nodes.size() - 1;
klao@946
   186
    }
klao@946
   187
klao@946
   188
    static void next(Node& node) {
klao@946
   189
      --node.n;
klao@946
   190
    }
klao@946
   191
klao@946
   192
    void first(Edge& edge) const {
klao@946
   193
      edge.n = edges.size() - 1;
klao@946
   194
    }
klao@946
   195
klao@946
   196
    static void next(Edge& edge) {
klao@946
   197
      --edge.n;
klao@946
   198
    }
klao@946
   199
klao@946
   200
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   201
      edge.n = nodes[node.n].first_out;
klao@946
   202
    }
klao@946
   203
klao@946
   204
    void nextOut(Edge& edge) const {
klao@946
   205
      edge.n = edges[edge.n].next_out;
klao@946
   206
    }
klao@946
   207
klao@946
   208
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   209
      edge.n = nodes[node.n].first_in;
klao@946
   210
    }
alpar@104
   211
    
klao@946
   212
    void nextIn(Edge& edge) const {
klao@946
   213
      edge.n = edges[edge.n].next_in;
klao@946
   214
    }
alpar@105
   215
alpar@969
   216
    Edge _findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@969
   217
    {
alpar@969
   218
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@1082
   219
      while(e!=-1 && edges[e].target!=v.n) e = edges[e].next_out;
alpar@969
   220
      prev.n=e;
alpar@969
   221
      return prev;
alpar@969
   222
    }
alpar@973
   223
alpar@1284
   224
    Node _split(Node n, bool connect = true)
alpar@1284
   225
    {
alpar@1284
   226
      Node b = addNode();
alpar@1284
   227
      nodes[b.n].first_out=nodes[n.n].first_out;
alpar@1284
   228
      nodes[n.n].first_out=-1;
alpar@1284
   229
      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
alpar@1284
   230
      if(connect) addEdge(n,b);
alpar@1284
   231
      return b;
alpar@1284
   232
    }
alpar@1284
   233
alpar@104
   234
  };
alpar@185
   235
klao@946
   236
  typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
klao@946
   237
  typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
deba@980
   238
  typedef DefaultMappableGraphExtender<IterableSmartGraphBase> MappableSmartGraphBase;
klao@946
   239
  typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
klao@946
   240
  typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
deba@937
   241
alpar@1161
   242
  /// \addtogroup graphs
alpar@1161
   243
  /// @{
alpar@1161
   244
alpar@950
   245
  ///A smart graph class.
deba@937
   246
alpar@950
   247
  ///This is a simple and fast graph implementation.
alpar@950
   248
  ///It is also quite memory efficient, but at the price
alpar@974
   249
  ///that <b> it does support only limited (only stack-like)
alpar@974
   250
  ///node and edge deletions</b>.
alpar@950
   251
  ///It conforms to 
klao@959
   252
  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
klao@959
   253
  ///\sa concept::ExtendableGraph.
alpar@950
   254
  ///
alpar@950
   255
  ///\author Alpar Juttner
deba@980
   256
  class SmartGraph : public ClearableSmartGraphBase {
alpar@969
   257
  public:
alpar@969
   258
    /// Finds an edge between two nodes.
alpar@973
   259
    
alpar@969
   260
    /// Finds an edge from node \c u to node \c v.
alpar@969
   261
    ///
alpar@969
   262
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@969
   263
    /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@969
   264
    /// the next edge from \c u to \c v after \c prev.
alpar@969
   265
    /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@969
   266
    ///
alpar@969
   267
    /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@969
   268
    /// \code
alpar@969
   269
    /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
alpar@969
   270
    ///   ...
alpar@969
   271
    /// }
alpar@969
   272
    /// \endcode
alpar@969
   273
    /// \todo Possibly it should be a global function.
alpar@969
   274
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@969
   275
    {
alpar@969
   276
      return _findEdge(u,v,prev);
alpar@969
   277
    }
alpar@973
   278
    
alpar@1011
   279
    class SnapShot;
alpar@1011
   280
    friend class SnapShot;
alpar@973
   281
alpar@1011
   282
  protected:
alpar@1011
   283
    void restoreSnapShot(const SnapShot &s)
alpar@973
   284
    {
alpar@1457
   285
      while(s.edge_num<edges.size()) {
deba@1040
   286
	Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
alpar@986
   287
	nodes[edges.back().target].first_in=edges.back().next_in;
alpar@986
   288
	nodes[edges.back().source].first_out=edges.back().next_out;
alpar@973
   289
	edges.pop_back();
alpar@973
   290
      }
alpar@973
   291
      //nodes.resize(s.nodes_num);
alpar@1457
   292
      while(s.node_num<nodes.size()) {
deba@1040
   293
	Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
alpar@973
   294
	nodes.pop_back();
alpar@973
   295
      }
alpar@1011
   296
    }    
alpar@1011
   297
alpar@1011
   298
  public:
alpar@1284
   299
alpar@1284
   300
    ///Split a node.
alpar@1284
   301
    
alpar@1284
   302
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   303
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1284
   304
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1284
   305
    ///from \c n to the newly created node is also added.
alpar@1284
   306
    ///\return The newly created node.
alpar@1284
   307
    ///
alpar@1284
   308
    ///\note The <tt>Edge</tt>s
alpar@1284
   309
    ///referencing a moved edge remain
alpar@1284
   310
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1284
   311
    ///may be invalidated.
alpar@1284
   312
    ///\warning This functionality cannot be used together with the SnapShot
alpar@1284
   313
    ///feature.
alpar@1284
   314
    ///\todo It could be implemented in a bit faster way.
alpar@1284
   315
    Node split(Node n, bool connect = true) 
alpar@1284
   316
    {
alpar@1284
   317
      return _split(n,connect);
alpar@1284
   318
    }
alpar@1284
   319
  
alpar@1284
   320
alpar@1011
   321
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   322
alpar@1011
   323
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   324
    ///
alpar@1011
   325
    ///The newly added nodes and edges can be removed using the
alpar@1011
   326
    ///restore() function.
alpar@1011
   327
    ///\note After you restore a state, you cannot restore
alpar@1011
   328
    ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   329
    ///by restore() using another SnapShot instance.
alpar@1011
   330
    ///
alpar@1011
   331
    class SnapShot 
alpar@1011
   332
    {
alpar@1011
   333
      SmartGraph *g;
alpar@1011
   334
    protected:
alpar@1011
   335
      friend class SmartGraph;
alpar@1011
   336
      unsigned int node_num;
alpar@1011
   337
      unsigned int edge_num;
alpar@1011
   338
    public:
zsuzska@1274
   339
      ///Default constructor.
alpar@1011
   340
      
zsuzska@1274
   341
      ///Default constructor.
alpar@1011
   342
      ///To actually make a snapshot you must call save().
alpar@1011
   343
      ///
alpar@1011
   344
      SnapShot() : g(0) {}
alpar@1011
   345
      ///Constructor that immediately makes a snapshot
alpar@1011
   346
      
alpar@1011
   347
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   348
      ///\param _g The graph we make a snapshot of.
alpar@1011
   349
      SnapShot(SmartGraph &_g) :g(&_g) {
alpar@1011
   350
	node_num=g->nodes.size();
alpar@1011
   351
	edge_num=g->edges.size();
alpar@1011
   352
      }
alpar@1011
   353
alpar@1011
   354
      ///Make a snapshot.
alpar@1011
   355
alpar@1011
   356
      ///Make a snapshot of the graph.
alpar@1011
   357
      ///
alpar@1011
   358
      ///This function can be called more than once. In case of a repeated
alpar@1011
   359
      ///call, the previous snapshot gets lost.
alpar@1011
   360
      ///\param _g The graph we make the snapshot of.
alpar@1011
   361
      void save(SmartGraph &_g) 
alpar@1011
   362
      {
alpar@1011
   363
	g=&_g;
alpar@1011
   364
	node_num=g->nodes.size();
alpar@1011
   365
	edge_num=g->edges.size();
alpar@1011
   366
      }
alpar@1011
   367
alpar@1011
   368
      ///Undo the changes until a snapshot.
alpar@1011
   369
      
alpar@1011
   370
      ///Undo the changes until a snapshot created by save().
alpar@1011
   371
      ///
alpar@1011
   372
      ///\note After you restored a state, you cannot restore
alpar@1011
   373
      ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   374
      ///by restore().
alpar@1011
   375
      ///
alpar@1011
   376
      ///\todo This function might be called undo().
alpar@1011
   377
      
alpar@1011
   378
      void restore()
alpar@1011
   379
      {
alpar@1011
   380
	g->restoreSnapShot(*this);
alpar@1011
   381
      }
alpar@1011
   382
    };
alpar@973
   383
  };
klao@1034
   384
klao@1034
   385
klao@1034
   386
  /**************** Undirected List Graph ****************/
klao@1034
   387
klao@1034
   388
  typedef ClearableUndirGraphExtender<
klao@1034
   389
    ExtendableUndirGraphExtender<
klao@1034
   390
    MappableUndirGraphExtender<
klao@1034
   391
    IterableUndirGraphExtender<
klao@1034
   392
    AlterableUndirGraphExtender<
klao@1034
   393
    UndirGraphExtender<SmartGraphBase> > > > > > UndirSmartGraphBase;
klao@1034
   394
alpar@1035
   395
  ///A smart undirected graph class.
alpar@1035
   396
alpar@1035
   397
  ///This is a simple and fast undirected graph implementation.
alpar@1035
   398
  ///It is also quite memory efficient, but at the price
alpar@1035
   399
  ///that <b> it does support only limited (only stack-like)
alpar@1035
   400
  ///node and edge deletions</b>.
alpar@1035
   401
  ///Except from this it conforms to 
alpar@1035
   402
  ///the \ref concept::UndirGraph "UndirGraph" concept.
alpar@1035
   403
  ///\sa concept::UndirGraph.
alpar@1035
   404
  ///
alpar@1035
   405
  ///\todo SnapShot hasn't been implemented yet.
alpar@1035
   406
  ///
klao@1034
   407
  class UndirSmartGraph : public UndirSmartGraphBase {
klao@1034
   408
  };
klao@1034
   409
alpar@950
   410
  
alpar@407
   411
  /// @}  
alpar@921
   412
} //namespace lemon
alpar@104
   413
alpar@157
   414
alpar@921
   415
#endif //LEMON_SMART_GRAPH_H