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