src/lemon/smart_graph.h
author klao
Thu, 04 Nov 2004 20:24:59 +0000
changeset 959 c80ef5912903
parent 950 d74557d1f100
child 969 0631847b37e5
permissions -rw-r--r--
skeleton(s) -> concept renaming
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>
alpar@104
    25
alpar@921
    26
#include <lemon/invalid.h>
alpar@157
    27
klao@946
    28
#include <lemon/erasable_graph_extender.h>
klao@946
    29
#include <lemon/clearable_graph_extender.h>
klao@946
    30
#include <lemon/extendable_graph_extender.h>
deba@937
    31
klao@946
    32
#include <lemon/idmappable_graph_extender.h>
alpar@919
    33
klao@946
    34
#include <lemon/iterable_graph_extender.h>
deba@782
    35
klao@946
    36
#include <lemon/alteration_observer_registry.h>
klao@946
    37
#include <lemon/default_map.h>
klao@946
    38
klao@946
    39
klao@946
    40
#include <lemon/graph_utils.h>
klao@946
    41
deba@782
    42
alpar@921
    43
namespace lemon {
alpar@104
    44
klao@946
    45
  /// \addtogroup graphs
klao@946
    46
  /// @{
alpar@185
    47
klao@946
    48
  class SmartGraphBase {
alpar@104
    49
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@104
    57
      int head, tail, 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
    
alpar@813
    80
    ///Number of nodes.
alpar@813
    81
    int nodeNum() const { return nodes.size(); }
alpar@813
    82
    ///Number of edges.
alpar@813
    83
    int edgeNum() const { return edges.size(); }
alpar@104
    84
alpar@813
    85
    /// Maximum node ID.
alpar@813
    86
    
alpar@813
    87
    /// Maximum node ID.
alpar@813
    88
    ///\sa id(Node)
alpar@813
    89
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
    90
    /// Maximum edge ID.
alpar@813
    91
    
alpar@813
    92
    /// Maximum edge ID.
alpar@813
    93
    ///\sa id(Edge)
alpar@813
    94
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
    95
alpar@164
    96
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164
    97
    Node head(Edge e) const { return edges[e.n].head; }
alpar@104
    98
alpar@813
    99
    /// Node ID.
alpar@813
   100
    
alpar@813
   101
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   102
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   103
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   104
    ///
alpar@813
   105
    /// The ID of the \ref INVALID node is -1.
alpar@813
   106
    ///\return The ID of the node \c v. 
alpar@713
   107
    static int id(Node v) { return v.n; }
alpar@813
   108
    /// Edge ID.
alpar@813
   109
    
alpar@813
   110
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   111
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   112
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   113
    ///
alpar@813
   114
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   115
    ///\return The ID of the edge \c e. 
alpar@713
   116
    static int id(Edge e) { return e.n; }
alpar@104
   117
alpar@164
   118
    Node addNode() {
alpar@164
   119
      Node n; n.n=nodes.size();
alpar@104
   120
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@104
   121
      return n;
alpar@104
   122
    }
alpar@108
   123
    
alpar@164
   124
    Edge addEdge(Node u, Node v) {
alpar@164
   125
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104
   126
      edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104
   127
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   128
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   129
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   130
alpar@104
   131
      return e;
alpar@104
   132
    }
alpar@104
   133
alpar@774
   134
    /// Finds an edge between two nodes.
alpar@774
   135
alpar@774
   136
    /// Finds an edge from node \c u to node \c v.
alpar@774
   137
    ///
alpar@774
   138
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   139
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   140
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   141
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   142
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   143
    {
alpar@774
   144
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@774
   145
      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
alpar@774
   146
      prev.n=e;
alpar@774
   147
      return prev;
alpar@774
   148
    }
alpar@774
   149
    
deba@782
   150
    void clear() {
deba@782
   151
      edges.clear();
deba@782
   152
      nodes.clear();
deba@782
   153
    }
alpar@104
   154
klao@946
   155
alpar@164
   156
    class Node {
klao@946
   157
      friend class SmartGraphBase;
alpar@104
   158
alpar@104
   159
    protected:
alpar@104
   160
      int n;
alpar@164
   161
      Node(int nn) {n=nn;}
alpar@104
   162
    public:
alpar@164
   163
      Node() {}
alpar@503
   164
      Node (Invalid) { n=-1; }
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@164
   167
      bool operator<(const Node i) const {return n<i.n;}
alpar@104
   168
    };
alpar@104
   169
    
alpar@104
   170
alpar@164
   171
    class Edge {
klao@946
   172
      friend class SmartGraphBase;
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@104
   217
  };
alpar@185
   218
klao@946
   219
  typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
klao@946
   220
  typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
klao@946
   221
  typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
klao@946
   222
  typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
klao@946
   223
  typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
klao@946
   224
  typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
deba@937
   225
alpar@950
   226
  ///A smart graph class.
deba@937
   227
alpar@950
   228
  ///This is a simple and fast graph implementation.
alpar@950
   229
  ///It is also quite memory efficient, but at the price
alpar@950
   230
  ///that <b> it does not support node and edge deletion</b>.
alpar@950
   231
  ///It conforms to 
klao@959
   232
  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
klao@959
   233
  ///\sa concept::ExtendableGraph.
alpar@950
   234
  ///
alpar@950
   235
  ///\todo Some member functions could be \c static.
alpar@950
   236
  ///
alpar@950
   237
  ///\todo A possibly useful functionality: a function saveState()
alpar@950
   238
  ///(or snapshot() ) would
alpar@950
   239
  ///give back a data sturcture X and then the function restoreState(X)
alpar@950
   240
  ///(or rollBack() )
alpar@950
   241
  ///would remove the nodes and edges added after the call of saveState().
alpar@950
   242
  ///Of course it should be used as a stack. (Maybe X is not necessary.)
alpar@950
   243
  ///
alpar@950
   244
  ///\author Alpar Juttner
alpar@950
   245
  class SmartGraph :public ClearableSmartGraphBase { };
alpar@950
   246
  
klao@946
   247
  template <>
klao@946
   248
  int countNodes<SmartGraph>(const SmartGraph& graph) {
klao@946
   249
    return graph.nodeNum();
klao@946
   250
  }
deba@937
   251
klao@946
   252
  template <>
klao@946
   253
  int countEdges<SmartGraph>(const SmartGraph& graph) {
klao@946
   254
    return graph.edgeNum();
klao@946
   255
  }
deba@937
   256
alpar@407
   257
  /// @}  
alpar@921
   258
} //namespace lemon
alpar@104
   259
alpar@157
   260
alpar@157
   261
alpar@157
   262
alpar@921
   263
#endif //LEMON_SMART_GRAPH_H