src/lemon/smart_graph.h
author ladanyi
Thu, 30 Sep 2004 09:31:38 +0000
changeset 927 dec4eef5a65c
parent 919 6153d9cf78c6
child 937 d4e911acef3d
permissions -rw-r--r--
minor correction
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, 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>
deba@782
    25
#include <climits>
alpar@104
    26
alpar@921
    27
#include <lemon/invalid.h>
alpar@157
    28
alpar@921
    29
#include <lemon/array_map.h>
alpar@921
    30
#include <lemon/sym_map.h>
alpar@919
    31
alpar@921
    32
#include <lemon/map_registry.h>
deba@782
    33
alpar@921
    34
#include <lemon/map_defines.h>
deba@782
    35
alpar@921
    36
namespace lemon {
alpar@104
    37
alpar@407
    38
/// \addtogroup graphs
alpar@407
    39
/// @{
deba@782
    40
//  class SymSmartGraph;
alpar@185
    41
alpar@186
    42
  ///A smart graph class.
alpar@186
    43
alpar@186
    44
  ///This is a simple and fast graph implementation.
alpar@186
    45
  ///It is also quite memory efficient, but at the price
alpar@186
    46
  ///that <b> it does not support node and edge deletion</b>.
alpar@880
    47
  ///It conforms to 
alpar@880
    48
  ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept.
alpar@880
    49
  ///\sa skeleton::ExtendableGraph.
alpar@402
    50
  ///
alpar@402
    51
  ///\todo Some member functions could be \c static.
alpar@753
    52
  ///
alpar@753
    53
  ///\todo A possibly useful functionality: a function saveState() would
alpar@753
    54
  ///give back a data sturcture X and then the function restoreState(X)
alpar@753
    55
  ///would remove the nodes and edges added after the call of saveState().
alpar@753
    56
  ///Of course it should be used as a stack. (Maybe X is not necessary.)
alpar@753
    57
  ///
alpar@456
    58
  ///\author Alpar Juttner
alpar@104
    59
  class SmartGraph {
alpar@104
    60
alpar@104
    61
    struct NodeT 
alpar@104
    62
    {
alpar@104
    63
      int first_in,first_out;      
alpar@157
    64
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    65
    };
alpar@104
    66
    struct EdgeT 
alpar@104
    67
    {
alpar@104
    68
      int head, tail, next_in, next_out;      
alpar@104
    69
      //FIXME: is this necessary?
alpar@157
    70
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    71
    };
alpar@104
    72
alpar@104
    73
    std::vector<NodeT> nodes;
alpar@129
    74
alpar@104
    75
    std::vector<EdgeT> edges;
alpar@104
    76
    
alpar@185
    77
    
alpar@104
    78
  public:
deba@782
    79
deba@782
    80
    typedef SmartGraph Graph;
alpar@104
    81
alpar@164
    82
    class Node;
alpar@164
    83
    class Edge;
alpar@108
    84
alpar@164
    85
    class NodeIt;
alpar@164
    86
    class EdgeIt;
alpar@104
    87
    class OutEdgeIt;
alpar@104
    88
    class InEdgeIt;
alpar@104
    89
    
alpar@904
    90
    // Create map registries.
deba@782
    91
    CREATE_MAP_REGISTRIES;
alpar@904
    92
    // Create node and edge maps.
deba@897
    93
    CREATE_MAPS(ArrayMap);
alpar@104
    94
    
alpar@104
    95
  public:
alpar@104
    96
alpar@104
    97
    SmartGraph() : nodes(), edges() { }
alpar@136
    98
    SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    99
    
alpar@813
   100
    ///Number of nodes.
alpar@813
   101
    int nodeNum() const { return nodes.size(); }
alpar@813
   102
    ///Number of edges.
alpar@813
   103
    int edgeNum() const { return edges.size(); }
alpar@104
   104
alpar@813
   105
    /// Maximum node ID.
alpar@813
   106
    
alpar@813
   107
    /// Maximum node ID.
alpar@813
   108
    ///\sa id(Node)
alpar@813
   109
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
   110
    /// Maximum edge ID.
alpar@813
   111
    
alpar@813
   112
    /// Maximum edge ID.
alpar@813
   113
    ///\sa id(Edge)
alpar@813
   114
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
   115
alpar@164
   116
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164
   117
    Node head(Edge e) const { return edges[e.n].head; }
alpar@104
   118
alpar@164
   119
    NodeIt& first(NodeIt& v) const { 
alpar@164
   120
      v=NodeIt(*this); return v; }
alpar@164
   121
    EdgeIt& first(EdgeIt& e) const { 
alpar@164
   122
      e=EdgeIt(*this); return e; }
alpar@164
   123
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@104
   124
      e=OutEdgeIt(*this,v); return e; }
alpar@164
   125
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@104
   126
      e=InEdgeIt(*this,v); return e; }
alpar@104
   127
alpar@813
   128
    /// Node ID.
alpar@813
   129
    
alpar@813
   130
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   131
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   132
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   133
    ///
alpar@813
   134
    /// The ID of the \ref INVALID node is -1.
alpar@813
   135
    ///\return The ID of the node \c v. 
alpar@713
   136
    static int id(Node v) { return v.n; }
alpar@813
   137
    /// Edge ID.
alpar@813
   138
    
alpar@813
   139
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   140
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   141
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   142
    ///
alpar@813
   143
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   144
    ///\return The ID of the edge \c e. 
alpar@713
   145
    static int id(Edge e) { return e.n; }
alpar@104
   146
alpar@164
   147
    Node addNode() {
alpar@164
   148
      Node n; n.n=nodes.size();
alpar@104
   149
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@108
   150
deba@782
   151
      
deba@782
   152
      node_maps.add(n);
alpar@104
   153
      return n;
alpar@104
   154
    }
alpar@108
   155
    
alpar@164
   156
    Edge addEdge(Node u, Node v) {
alpar@164
   157
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104
   158
      edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104
   159
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   160
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   161
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   162
deba@782
   163
      edge_maps.add(e);
alpar@108
   164
alpar@104
   165
      return e;
alpar@104
   166
    }
alpar@104
   167
alpar@774
   168
    /// Finds an edge between two nodes.
alpar@774
   169
alpar@774
   170
    /// Finds an edge from node \c u to node \c v.
alpar@774
   171
    ///
alpar@774
   172
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   173
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   174
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   175
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   176
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   177
    {
alpar@774
   178
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@774
   179
      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
alpar@774
   180
      prev.n=e;
alpar@774
   181
      return prev;
alpar@774
   182
    }
alpar@774
   183
    
deba@782
   184
    void clear() {
deba@782
   185
      edge_maps.clear();
deba@782
   186
      edges.clear();
deba@782
   187
      node_maps.clear();
deba@782
   188
      nodes.clear();
deba@782
   189
    }
alpar@104
   190
alpar@164
   191
    class Node {
alpar@104
   192
      friend class SmartGraph;
alpar@104
   193
      template <typename T> friend class NodeMap;
alpar@104
   194
      
alpar@164
   195
      friend class Edge;
alpar@104
   196
      friend class OutEdgeIt;
alpar@104
   197
      friend class InEdgeIt;
alpar@164
   198
      friend class SymEdge;
alpar@104
   199
alpar@104
   200
    protected:
alpar@104
   201
      int n;
alpar@722
   202
      friend int SmartGraph::id(Node v); 
alpar@164
   203
      Node(int nn) {n=nn;}
alpar@104
   204
    public:
alpar@164
   205
      Node() {}
alpar@503
   206
      Node (Invalid) { n=-1; }
alpar@164
   207
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   208
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   209
      bool operator<(const Node i) const {return n<i.n;}
alpar@774
   210
      //      ///Validity check
alpar@774
   211
      //      operator bool() { return n!=-1; }
alpar@104
   212
    };
alpar@104
   213
    
alpar@164
   214
    class NodeIt : public Node {
alpar@774
   215
      const SmartGraph *G;
alpar@104
   216
      friend class SmartGraph;
alpar@104
   217
    public:
alpar@402
   218
      NodeIt() : Node() { }
alpar@774
   219
      NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@402
   220
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   221
      NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
alpar@774
   222
      NodeIt &operator++() {
alpar@774
   223
	n=(n+2)%(G->nodes.size()+1)-1; 
alpar@774
   224
	return *this; 
alpar@774
   225
      }
alpar@774
   226
//       ///Validity check
alpar@774
   227
//       operator bool() { return Node::operator bool(); }      
alpar@104
   228
    };
alpar@104
   229
alpar@164
   230
    class Edge {
alpar@104
   231
      friend class SmartGraph;
alpar@104
   232
      template <typename T> friend class EdgeMap;
alpar@185
   233
alpar@905
   234
      friend class SymSmartGraph;
alpar@104
   235
      
alpar@164
   236
      friend class Node;
alpar@104
   237
      friend class NodeIt;
alpar@104
   238
    protected:
alpar@104
   239
      int n;
alpar@722
   240
      friend int SmartGraph::id(Edge e);
alpar@905
   241
      Edge(int nn) {n=nn;}
alpar@706
   242
    public:
alpar@706
   243
      /// An Edge with id \c n.
alpar@706
   244
alpar@164
   245
      Edge() { }
marci@174
   246
      Edge (Invalid) { n=-1; }
alpar@164
   247
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   248
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   249
      bool operator<(const Edge i) const {return n<i.n;}
alpar@774
   250
//       ///Validity check
alpar@774
   251
//       operator bool() { return n!=-1; }
alpar@905
   252
alpar@905
   253
      ///Set the edge to that have ID \c ID.
alpar@905
   254
      void setToId(int id) { n=id; }
alpar@774
   255
   };
alpar@104
   256
    
alpar@164
   257
    class EdgeIt : public Edge {
alpar@774
   258
      const SmartGraph *G;
alpar@104
   259
      friend class SmartGraph;
alpar@104
   260
    public:
alpar@774
   261
      EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
alpar@774
   262
      EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   263
      EdgeIt (Invalid i) : Edge(i) { }
alpar@164
   264
      EdgeIt() : Edge() { }
alpar@774
   265
      EdgeIt &operator++() { --n; return *this; }
alpar@774
   266
//       ///Validity check
alpar@774
   267
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   268
    };
alpar@104
   269
    
alpar@164
   270
    class OutEdgeIt : public Edge {
alpar@774
   271
      const SmartGraph *G;
alpar@104
   272
      friend class SmartGraph;
alpar@104
   273
    public: 
alpar@164
   274
      OutEdgeIt() : Edge() { }
alpar@774
   275
      OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   276
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@157
   277
alpar@774
   278
      OutEdgeIt(const SmartGraph& _G,const Node v)
alpar@774
   279
	: Edge(_G.nodes[v.n].first_out), G(&_G) {}
alpar@774
   280
      OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
alpar@774
   281
//       ///Validity check
alpar@774
   282
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   283
    };
alpar@104
   284
    
alpar@164
   285
    class InEdgeIt : public Edge {
alpar@774
   286
      const SmartGraph *G;
alpar@104
   287
      friend class SmartGraph;
alpar@104
   288
    public: 
alpar@164
   289
      InEdgeIt() : Edge() { }
alpar@774
   290
      InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   291
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   292
      InEdgeIt(const SmartGraph& _G,Node v)
alpar@774
   293
	: Edge(_G.nodes[v.n].first_in), G(&_G) { }
alpar@774
   294
      InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
alpar@774
   295
//       ///Validity check
alpar@774
   296
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   297
    };
alpar@105
   298
alpar@104
   299
  };
alpar@185
   300
alpar@185
   301
  ///Graph for bidirectional edges.
alpar@185
   302
alpar@185
   303
  ///The purpose of this graph structure is to handle graphs
alpar@185
   304
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@186
   305
  ///of oppositely directed edges.
alpar@186
   306
  ///There is a new edge map type called
alpar@186
   307
  ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
alpar@186
   308
  ///that complements this
alpar@186
   309
  ///feature by
alpar@186
   310
  ///storing shared values for the edge pairs. The usual
alpar@880
   311
  ///\ref Graph::EdgeMap "EdgeMap"
alpar@186
   312
  ///can be used
alpar@185
   313
  ///as well.
alpar@185
   314
  ///
alpar@186
   315
  ///The oppositely directed edge can also be obtained easily
alpar@186
   316
  ///using \ref opposite.
alpar@186
   317
  ///\warning It shares the similarity with \ref SmartGraph that
alpar@186
   318
  ///it is not possible to delete edges or nodes from the graph.
alpar@880
   319
  //\sa SmartGraph.
alpar@185
   320
alpar@919
   321
  class SymSmartGraph : public SmartGraph
alpar@185
   322
  {
alpar@185
   323
  public:
deba@782
   324
    typedef SymSmartGraph Graph;
deba@782
   325
alpar@904
   326
    // Create symmetric map registry.
deba@782
   327
    CREATE_SYM_EDGE_MAP_REGISTRY;
alpar@904
   328
    // Create symmetric edge map.
deba@897
   329
    CREATE_SYM_EDGE_MAP(ArrayMap);
deba@822
   330
alpar@186
   331
alpar@185
   332
    SymSmartGraph() : SmartGraph() { }
alpar@185
   333
    SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
alpar@398
   334
    ///Adds a pair of oppositely directed edges to the graph.
alpar@185
   335
    Edge addEdge(Node u, Node v)
alpar@185
   336
    {
alpar@185
   337
      Edge e = SmartGraph::addEdge(u,v);
deba@798
   338
      Edge f = SmartGraph::addEdge(v,u);
deba@798
   339
      sym_edge_maps.add(e);
deba@798
   340
      sym_edge_maps.add(f);
alpar@185
   341
      return e;
alpar@185
   342
    }
alpar@185
   343
alpar@186
   344
    ///The oppositely directed edge.
alpar@186
   345
alpar@186
   346
    ///Returns the oppositely directed
alpar@186
   347
    ///pair of the edge \c e.
alpar@713
   348
    static Edge opposite(Edge e)
alpar@185
   349
    {
alpar@185
   350
      Edge f;
alpar@905
   351
      f.n = e.n - 2*(e.n%2) + 1;
alpar@185
   352
      return f;
alpar@185
   353
    }
alpar@185
   354
    
alpar@185
   355
alpar@919
   356
  };
alpar@185
   357
  
alpar@407
   358
  /// @}  
alpar@921
   359
} //namespace lemon
alpar@104
   360
alpar@157
   361
alpar@157
   362
alpar@157
   363
alpar@921
   364
#endif //LEMON_SMART_GRAPH_H