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