src/lemon/full_graph.h
author alpar
Fri, 18 Feb 2005 14:46:04 +0000
changeset 1155 fe0fcdb5687b
parent 1039 bd01c5a3f989
child 1161 1c9658d51c8d
permissions -rw-r--r--
- Better addSource()
- More docs
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/full_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@591
    16
alpar@921
    17
#ifndef LEMON_FULL_GRAPH_H
alpar@921
    18
#define LEMON_FULL_GRAPH_H
alpar@591
    19
deba@983
    20
#include <cmath>
deba@983
    21
klao@946
    22
klao@946
    23
#include <lemon/iterable_graph_extender.h>
deba@1039
    24
#include <lemon/alteration_notifier.h>
klao@946
    25
#include <lemon/default_map.h>
klao@946
    26
klao@977
    27
#include <lemon/invalid.h>
klao@977
    28
#include <lemon/utility.h>
klao@977
    29
klao@977
    30
alpar@591
    31
///\ingroup graphs
alpar@591
    32
///\file
alpar@591
    33
///\brief FullGraph and SymFullGraph classes.
alpar@591
    34
alpar@591
    35
alpar@921
    36
namespace lemon {
alpar@591
    37
alpar@591
    38
/// \addtogroup graphs
alpar@591
    39
/// @{
alpar@591
    40
klao@946
    41
  class FullGraphBase {
alpar@591
    42
    int NodeNum;
alpar@591
    43
    int EdgeNum;
alpar@591
    44
  public:
deba@782
    45
klao@946
    46
    typedef FullGraphBase Graph;
alpar@591
    47
alpar@591
    48
    class Node;
alpar@591
    49
    class Edge;
deba@782
    50
alpar@591
    51
  public:
alpar@591
    52
klao@946
    53
    FullGraphBase() {}
klao@946
    54
klao@946
    55
alpar@591
    56
    ///Creates a full graph with \c n nodes.
klao@946
    57
    void construct(int n) { NodeNum = n; EdgeNum = n * n; }
alpar@591
    58
    ///
klao@946
    59
    //    FullGraphBase(const FullGraphBase &_g)
klao@946
    60
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    61
    
klao@977
    62
    typedef True NodeNumTag;
klao@977
    63
    typedef True EdgeNumTag;
klao@977
    64
alpar@813
    65
    ///Number of nodes.
alpar@813
    66
    int nodeNum() const { return NodeNum; }
alpar@813
    67
    ///Number of edges.
alpar@813
    68
    int edgeNum() const { return EdgeNum; }
alpar@591
    69
alpar@813
    70
    /// Maximum node ID.
alpar@813
    71
    
alpar@813
    72
    /// Maximum node ID.
alpar@813
    73
    ///\sa id(Node)
deba@980
    74
    int maxId(Node = INVALID) const { return NodeNum-1; }
alpar@813
    75
    /// Maximum edge ID.
alpar@813
    76
    
alpar@813
    77
    /// Maximum edge ID.
alpar@813
    78
    ///\sa id(Edge)
deba@980
    79
    int maxId(Edge = INVALID) const { return EdgeNum-1; }
alpar@591
    80
alpar@986
    81
    Node source(Edge e) const { return e.id % NodeNum; }
alpar@986
    82
    Node target(Edge e) const { return e.id / NodeNum; }
alpar@591
    83
alpar@591
    84
alpar@813
    85
    /// Node ID.
alpar@813
    86
    
alpar@813
    87
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    88
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    89
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    90
    ///
alpar@813
    91
    /// The ID of the \ref INVALID node is -1.
alpar@813
    92
    ///\return The ID of the node \c v. 
klao@946
    93
klao@946
    94
    static int id(Node v) { return v.id; }
alpar@813
    95
    /// Edge ID.
alpar@813
    96
    
alpar@813
    97
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
    98
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
    99
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   100
    ///
alpar@813
   101
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   102
    ///\return The ID of the edge \c e. 
klao@946
   103
    static int id(Edge e) { return e.id; }
alpar@591
   104
deba@1106
   105
    static Node fromId(int id, Node) { return Node(id);}
deba@1106
   106
    
deba@1106
   107
    static Edge fromId(int id, Edge) { return Edge(id);}
deba@1106
   108
alpar@774
   109
    /// Finds an edge between two nodes.
alpar@774
   110
    
alpar@774
   111
    /// Finds an edge from node \c u to node \c v.
alpar@774
   112
    ///
alpar@774
   113
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   114
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   115
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   116
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   117
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   118
    {
klao@946
   119
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   120
    }
alpar@774
   121
    
alpar@774
   122
      
alpar@591
   123
    class Node {
klao@946
   124
      friend class FullGraphBase;
alpar@591
   125
alpar@591
   126
    protected:
klao@946
   127
      int id;
klao@946
   128
      Node(int _id) { id = _id;}
alpar@591
   129
    public:
alpar@591
   130
      Node() {}
klao@946
   131
      Node (Invalid) { id = -1; }
klao@946
   132
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   133
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   134
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   135
    };
alpar@591
   136
    
klao@946
   137
klao@946
   138
klao@946
   139
    class Edge {
klao@946
   140
      friend class FullGraphBase;
klao@946
   141
      
klao@946
   142
    protected:
alpar@986
   143
      int id;  // NodeNum * target + source;
klao@946
   144
klao@946
   145
      Edge(int _id) : id(_id) {}
klao@946
   146
alpar@986
   147
      Edge(const FullGraphBase& _graph, int source, int target) 
alpar@986
   148
	: id(_graph.NodeNum * target+source) {}
alpar@591
   149
    public:
klao@946
   150
      Edge() { }
klao@946
   151
      Edge (Invalid) { id = -1; }
klao@946
   152
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   153
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   154
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   155
    };
alpar@591
   156
klao@946
   157
    void first(Node& node) const {
klao@946
   158
      node.id = NodeNum-1;
klao@946
   159
    }
alpar@591
   160
klao@946
   161
    static void next(Node& node) {
klao@946
   162
      --node.id;
klao@946
   163
    }
klao@946
   164
klao@946
   165
    void first(Edge& edge) const {
klao@946
   166
      edge.id = EdgeNum-1;
klao@946
   167
    }
klao@946
   168
klao@946
   169
    static void next(Edge& edge) {
klao@946
   170
      --edge.id;
klao@946
   171
    }
klao@946
   172
klao@946
   173
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   174
      edge.id = EdgeNum + node.id - NodeNum;
klao@946
   175
    }
klao@946
   176
klao@946
   177
    void nextOut(Edge& edge) const {
klao@946
   178
      edge.id -= NodeNum;
klao@946
   179
      if (edge.id < 0) edge.id = -1;
klao@946
   180
    }
klao@946
   181
klao@946
   182
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   183
      edge.id = node.id * NodeNum;
klao@946
   184
    }
alpar@591
   185
    
klao@946
   186
    void nextIn(Edge& edge) const {
klao@946
   187
      ++edge.id;
klao@946
   188
      if (edge.id % NodeNum == 0) edge.id = -1;
klao@946
   189
    }
alpar@591
   190
alpar@591
   191
  };
alpar@591
   192
klao@946
   193
klao@946
   194
  typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
klao@946
   195
  typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
deba@980
   196
  typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
klao@946
   197
alpar@951
   198
  ///A full graph class.
alpar@951
   199
alpar@951
   200
  ///This is a simple and fast directed full graph implementation.
alpar@951
   201
  ///It is completely static, so you can neither add nor delete either
alpar@951
   202
  ///edges or nodes.
alpar@951
   203
  ///Thus it conforms to
klao@959
   204
  ///the \ref concept::StaticGraph "StaticGraph" concept
klao@959
   205
  ///\sa concept::StaticGraph.
alpar@951
   206
  ///
alpar@951
   207
  ///\author Alpar Juttner
klao@946
   208
  class FullGraph : public MappableFullGraphBase {
klao@946
   209
  public:
klao@946
   210
klao@946
   211
    FullGraph(int n) { construct(n); }
klao@946
   212
  };
klao@946
   213
deba@983
   214
deba@983
   215
  /// Base graph class for UndirFullGraph.
deba@983
   216
deba@983
   217
  class UndirFullGraphBase {
deba@983
   218
    int NodeNum;
deba@983
   219
    int EdgeNum;
deba@983
   220
  public:
deba@983
   221
deba@984
   222
    typedef UndirFullGraphBase Graph;
deba@983
   223
deba@983
   224
    class Node;
deba@983
   225
    class Edge;
deba@983
   226
deba@983
   227
  public:
deba@983
   228
deba@984
   229
    UndirFullGraphBase() {}
deba@983
   230
deba@983
   231
deba@983
   232
    ///Creates a full graph with \c n nodes.
deba@983
   233
    void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; }
deba@983
   234
    ///
deba@983
   235
    //    FullGraphBase(const FullGraphBase &_g)
deba@983
   236
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
deba@983
   237
    
deba@983
   238
    typedef True NodeNumTag;
deba@983
   239
    typedef True EdgeNumTag;
deba@983
   240
deba@983
   241
    ///Number of nodes.
deba@983
   242
    int nodeNum() const { return NodeNum; }
deba@983
   243
    ///Number of edges.
deba@983
   244
    int edgeNum() const { return EdgeNum; }
deba@983
   245
deba@983
   246
    /// Maximum node ID.
deba@983
   247
    
deba@983
   248
    /// Maximum node ID.
deba@983
   249
    ///\sa id(Node)
deba@983
   250
    int maxId(Node = INVALID) const { return NodeNum-1; }
deba@983
   251
    /// Maximum edge ID.
deba@983
   252
    
deba@983
   253
    /// Maximum edge ID.
deba@983
   254
    ///\sa id(Edge)
deba@983
   255
    int maxId(Edge = INVALID) const { return EdgeNum-1; }
deba@983
   256
alpar@986
   257
    Node source(Edge e) const { 
deba@983
   258
      /// \todo we may do it faster
deba@983
   259
      return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; 
deba@983
   260
    }
deba@983
   261
alpar@986
   262
    Node target(Edge e) const { 
alpar@986
   263
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   264
      return e.id - (source) * (source - 1) / 2; 
deba@983
   265
    }
deba@983
   266
deba@983
   267
deba@983
   268
    /// Node ID.
deba@983
   269
    
deba@983
   270
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   271
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   272
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   273
    ///
deba@983
   274
    /// The ID of the \ref INVALID node is -1.
deba@983
   275
    ///\return The ID of the node \c v. 
deba@983
   276
deba@983
   277
    static int id(Node v) { return v.id; }
deba@983
   278
    /// Edge ID.
deba@983
   279
    
deba@983
   280
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   281
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   282
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   283
    ///
deba@983
   284
    /// The ID of the \ref INVALID edge is -1.
deba@983
   285
    ///\return The ID of the edge \c e. 
deba@983
   286
    static int id(Edge e) { return e.id; }
deba@983
   287
deba@983
   288
    /// Finds an edge between two nodes.
deba@983
   289
    
deba@983
   290
    /// Finds an edge from node \c u to node \c v.
deba@983
   291
    ///
deba@983
   292
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   293
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   294
    /// the next edge from \c u to \c v after \c prev.
deba@983
   295
    /// \return The found edge or INVALID if there is no such an edge.
deba@983
   296
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
deba@983
   297
    {
deba@983
   298
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
deba@983
   299
    }
deba@983
   300
    
deba@983
   301
      
deba@983
   302
    class Node {
alpar@985
   303
      friend class UndirFullGraphBase;
deba@983
   304
deba@983
   305
    protected:
deba@983
   306
      int id;
deba@983
   307
      Node(int _id) { id = _id;}
deba@983
   308
    public:
deba@983
   309
      Node() {}
deba@983
   310
      Node (Invalid) { id = -1; }
deba@983
   311
      bool operator==(const Node node) const {return id == node.id;}
deba@983
   312
      bool operator!=(const Node node) const {return id != node.id;}
deba@983
   313
      bool operator<(const Node node) const {return id < node.id;}
deba@983
   314
    };
deba@983
   315
    
deba@983
   316
deba@983
   317
deba@983
   318
    class Edge {
alpar@985
   319
      friend class UndirFullGraphBase;
deba@983
   320
      
deba@983
   321
    protected:
alpar@986
   322
      int id;  // NodeNum * target + source;
deba@983
   323
deba@983
   324
      Edge(int _id) : id(_id) {}
deba@983
   325
alpar@986
   326
      Edge(const UndirFullGraphBase& _graph, int source, int target) 
alpar@986
   327
	: id(_graph.NodeNum * target+source) {}
deba@983
   328
    public:
deba@983
   329
      Edge() { }
deba@983
   330
      Edge (Invalid) { id = -1; }
deba@983
   331
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@983
   332
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@983
   333
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@983
   334
    };
deba@983
   335
deba@983
   336
    void first(Node& node) const {
deba@983
   337
      node.id = NodeNum-1;
deba@983
   338
    }
deba@983
   339
deba@983
   340
    static void next(Node& node) {
deba@983
   341
      --node.id;
deba@983
   342
    }
deba@983
   343
deba@983
   344
    void first(Edge& edge) const {
deba@983
   345
      edge.id = EdgeNum-1;
deba@983
   346
    }
deba@983
   347
deba@983
   348
    static void next(Edge& edge) {
deba@983
   349
      --edge.id;
deba@983
   350
    }
deba@983
   351
deba@983
   352
    void firstOut(Edge& edge, const Node& node) const {      
deba@983
   353
      edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
deba@983
   354
    }
deba@983
   355
deba@983
   356
    /// \todo with specialized iterators we can make faster iterating
alpar@985
   357
    void nextOut(Edge& e) const {
alpar@986
   358
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   359
      int target = e.id - (source) * (source - 1) / 2; 
alpar@986
   360
      ++target;
alpar@986
   361
      e.id = target < source ? source * (source - 1) / 2 + target : -1;
deba@983
   362
    }
deba@983
   363
deba@983
   364
    void firstIn(Edge& edge, const Node& node) const {
deba@983
   365
      edge.id = node.id * (node.id + 1) / 2 - 1;
deba@983
   366
    }
deba@983
   367
    
alpar@985
   368
    void nextIn(Edge& e) const {
alpar@986
   369
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   370
      int target = e.id - (source) * (source - 1) / 2; ++target;
alpar@986
   371
      ++source;
alpar@986
   372
      e.id = source < NodeNum ? source * (source - 1) / 2 + target : -1;
deba@983
   373
    }
deba@983
   374
deba@983
   375
  };
deba@983
   376
deba@983
   377
  /// \todo UndirFullGraph from the UndirFullGraphBase
deba@983
   378
deba@983
   379
  
deba@983
   380
alpar@591
   381
  /// @}  
alpar@591
   382
alpar@921
   383
} //namespace lemon
alpar@591
   384
alpar@591
   385
alpar@921
   386
#endif //LEMON_FULL_GRAPH_H