lemon/full_graph.h
author ladanyi
Wed, 17 Aug 2005 20:38:32 +0000
changeset 1638 4e50ed042394
parent 1555 48769ac7ec32
child 1643 9285f3777553
permissions -rw-r--r--
less stupid title
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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
deba@1307
    23
#include <lemon/bits/iterable_graph_extender.h>
deba@1307
    24
#include <lemon/bits/alteration_notifier.h>
deba@1307
    25
#include <lemon/bits/default_map.h>
klao@946
    26
deba@1566
    27
#include <lemon/bits/undir_graph_extender.h>
deba@1566
    28
klao@977
    29
#include <lemon/invalid.h>
klao@977
    30
#include <lemon/utility.h>
klao@977
    31
klao@977
    32
alpar@591
    33
///\ingroup graphs
alpar@591
    34
///\file
alpar@591
    35
///\brief FullGraph and SymFullGraph classes.
alpar@591
    36
alpar@591
    37
alpar@921
    38
namespace lemon {
alpar@591
    39
klao@946
    40
  class FullGraphBase {
deba@1566
    41
    int _nodeNum;
deba@1566
    42
    int _edgeNum;
alpar@591
    43
  public:
deba@782
    44
klao@946
    45
    typedef FullGraphBase Graph;
alpar@591
    46
alpar@591
    47
    class Node;
alpar@591
    48
    class Edge;
deba@782
    49
alpar@591
    50
  public:
alpar@591
    51
klao@946
    52
    FullGraphBase() {}
klao@946
    53
klao@946
    54
alpar@591
    55
    ///Creates a full graph with \c n nodes.
deba@1566
    56
    void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
alpar@591
    57
    ///
klao@946
    58
    //    FullGraphBase(const FullGraphBase &_g)
deba@1566
    59
    //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
alpar@591
    60
    
klao@977
    61
    typedef True NodeNumTag;
klao@977
    62
    typedef True EdgeNumTag;
klao@977
    63
alpar@813
    64
    ///Number of nodes.
deba@1566
    65
    int nodeNum() const { return _nodeNum; }
alpar@813
    66
    ///Number of edges.
deba@1566
    67
    int edgeNum() const { return _edgeNum; }
alpar@591
    68
alpar@813
    69
    /// Maximum node ID.
alpar@813
    70
    
alpar@813
    71
    /// Maximum node ID.
alpar@813
    72
    ///\sa id(Node)
deba@1566
    73
    int maxId(Node = INVALID) const { return _nodeNum-1; }
alpar@813
    74
    /// Maximum edge ID.
alpar@813
    75
    
alpar@813
    76
    /// Maximum edge ID.
alpar@813
    77
    ///\sa id(Edge)
deba@1566
    78
    int maxId(Edge = INVALID) const { return _edgeNum-1; }
alpar@591
    79
deba@1566
    80
    Node source(Edge e) const { return e.id % _nodeNum; }
deba@1566
    81
    Node target(Edge e) const { return e.id / _nodeNum; }
alpar@591
    82
alpar@591
    83
alpar@813
    84
    /// Node ID.
alpar@813
    85
    
alpar@813
    86
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    87
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    88
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    89
    ///
alpar@813
    90
    /// The ID of the \ref INVALID node is -1.
alpar@813
    91
    ///\return The ID of the node \c v. 
klao@946
    92
klao@946
    93
    static int id(Node v) { return v.id; }
alpar@813
    94
    /// Edge ID.
alpar@813
    95
    
alpar@813
    96
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
    97
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
    98
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
    99
    ///
alpar@813
   100
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   101
    ///\return The ID of the edge \c e. 
klao@946
   102
    static int id(Edge e) { return e.id; }
alpar@591
   103
deba@1106
   104
    static Node fromId(int id, Node) { return Node(id);}
deba@1106
   105
    
deba@1106
   106
    static Edge fromId(int id, Edge) { return Edge(id);}
deba@1106
   107
deba@1566
   108
    typedef True FindEdgeTag;
deba@1566
   109
alpar@774
   110
    /// Finds an edge between two nodes.
alpar@774
   111
    
alpar@774
   112
    /// Finds an edge from node \c u to node \c v.
alpar@774
   113
    ///
alpar@774
   114
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   115
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   116
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   117
    /// \return The found edge or INVALID if there is no such an edge.
deba@1566
   118
    Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
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:
deba@1566
   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) 
deba@1566
   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 {
deba@1566
   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 {
deba@1566
   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 {
deba@1566
   174
      edge.id = _edgeNum + node.id - _nodeNum;
klao@946
   175
    }
klao@946
   176
klao@946
   177
    void nextOut(Edge& edge) const {
deba@1566
   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 {
deba@1566
   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;
deba@1566
   188
      if (edge.id % _nodeNum == 0) edge.id = -1;
klao@946
   189
    }
alpar@591
   190
alpar@591
   191
  };
alpar@591
   192
klao@946
   193
deba@1566
   194
  typedef AlterableGraphExtender<FullGraphBase> 
deba@1566
   195
  AlterableFullGraphBase;
deba@1566
   196
  typedef IterableGraphExtender<AlterableFullGraphBase> 
deba@1566
   197
  IterableFullGraphBase;
deba@1566
   198
  typedef DefaultMappableGraphExtender<IterableFullGraphBase> 
deba@1566
   199
  MappableFullGraphBase;
klao@946
   200
deba@1566
   201
  /// \ingroup graphs
alpar@951
   202
  ///
deba@1566
   203
  /// \brief A full graph class.
deba@1566
   204
  ///
deba@1566
   205
  /// This is a simple and fast directed full graph implementation.
deba@1566
   206
  /// It is completely static, so you can neither add nor delete either
deba@1566
   207
  /// edges or nodes.
deba@1566
   208
  /// Thus it conforms to
deba@1566
   209
  /// the \ref concept::StaticGraph "StaticGraph" concept
deba@1566
   210
  /// \sa concept::StaticGraph.
deba@1566
   211
  ///
deba@1566
   212
  /// \author Alpar Juttner
klao@946
   213
  class FullGraph : public MappableFullGraphBase {
klao@946
   214
  public:
klao@946
   215
klao@946
   216
    FullGraph(int n) { construct(n); }
klao@946
   217
  };
klao@946
   218
alpar@1555
   219
  ///@}
deba@983
   220
deba@983
   221
  class UndirFullGraphBase {
deba@1566
   222
    int _nodeNum;
deba@1566
   223
    int _edgeNum;
deba@983
   224
  public:
deba@983
   225
deba@984
   226
    typedef UndirFullGraphBase Graph;
deba@983
   227
deba@983
   228
    class Node;
deba@983
   229
    class Edge;
deba@983
   230
deba@983
   231
  public:
deba@983
   232
deba@984
   233
    UndirFullGraphBase() {}
deba@983
   234
deba@983
   235
deba@983
   236
    ///Creates a full graph with \c n nodes.
deba@1566
   237
    void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
deba@983
   238
    ///
deba@983
   239
    //    FullGraphBase(const FullGraphBase &_g)
deba@1566
   240
    //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
deba@983
   241
    
deba@983
   242
    typedef True NodeNumTag;
deba@983
   243
    typedef True EdgeNumTag;
deba@983
   244
deba@983
   245
    ///Number of nodes.
deba@1566
   246
    int nodeNum() const { return _nodeNum; }
deba@983
   247
    ///Number of edges.
deba@1566
   248
    int edgeNum() const { return _edgeNum; }
deba@983
   249
deba@983
   250
    /// Maximum node ID.
deba@983
   251
    
deba@983
   252
    /// Maximum node ID.
deba@983
   253
    ///\sa id(Node)
deba@1566
   254
    int maxId(Node = INVALID) const { return _nodeNum-1; }
deba@983
   255
    /// Maximum edge ID.
deba@983
   256
    
deba@983
   257
    /// Maximum edge ID.
deba@983
   258
    ///\sa id(Edge)
deba@1566
   259
    int maxId(Edge = INVALID) const { return _edgeNum-1; }
deba@983
   260
alpar@986
   261
    Node source(Edge e) const { 
deba@983
   262
      /// \todo we may do it faster
deba@983
   263
      return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; 
deba@983
   264
    }
deba@983
   265
alpar@986
   266
    Node target(Edge e) const { 
alpar@986
   267
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   268
      return e.id - (source) * (source - 1) / 2; 
deba@983
   269
    }
deba@983
   270
deba@983
   271
deba@983
   272
    /// Node ID.
deba@983
   273
    
deba@983
   274
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   275
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   276
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   277
    ///
deba@983
   278
    /// The ID of the \ref INVALID node is -1.
deba@983
   279
    ///\return The ID of the node \c v. 
deba@983
   280
deba@983
   281
    static int id(Node v) { return v.id; }
deba@983
   282
    /// Edge ID.
deba@983
   283
    
deba@983
   284
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   285
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   286
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   287
    ///
deba@983
   288
    /// The ID of the \ref INVALID edge is -1.
deba@983
   289
    ///\return The ID of the edge \c e. 
deba@983
   290
    static int id(Edge e) { return e.id; }
deba@983
   291
deba@983
   292
    /// Finds an edge between two nodes.
deba@983
   293
    
deba@983
   294
    /// Finds an edge from node \c u to node \c v.
deba@983
   295
    ///
deba@983
   296
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   297
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   298
    /// the next edge from \c u to \c v after \c prev.
deba@983
   299
    /// \return The found edge or INVALID if there is no such an edge.
deba@983
   300
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
deba@983
   301
    {
deba@983
   302
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
deba@983
   303
    }
deba@983
   304
    
deba@983
   305
      
deba@983
   306
    class Node {
alpar@985
   307
      friend class UndirFullGraphBase;
deba@983
   308
deba@983
   309
    protected:
deba@983
   310
      int id;
deba@983
   311
      Node(int _id) { id = _id;}
deba@983
   312
    public:
deba@983
   313
      Node() {}
deba@983
   314
      Node (Invalid) { id = -1; }
deba@983
   315
      bool operator==(const Node node) const {return id == node.id;}
deba@983
   316
      bool operator!=(const Node node) const {return id != node.id;}
deba@983
   317
      bool operator<(const Node node) const {return id < node.id;}
deba@983
   318
    };
deba@983
   319
    
deba@983
   320
deba@983
   321
deba@983
   322
    class Edge {
alpar@985
   323
      friend class UndirFullGraphBase;
deba@983
   324
      
deba@983
   325
    protected:
deba@1566
   326
      int id;  // _nodeNum * target + source;
deba@983
   327
deba@983
   328
      Edge(int _id) : id(_id) {}
deba@983
   329
alpar@986
   330
      Edge(const UndirFullGraphBase& _graph, int source, int target) 
deba@1566
   331
	: id(_graph._nodeNum * target+source) {}
deba@983
   332
    public:
deba@983
   333
      Edge() { }
deba@983
   334
      Edge (Invalid) { id = -1; }
deba@983
   335
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@983
   336
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@983
   337
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@983
   338
    };
deba@983
   339
deba@983
   340
    void first(Node& node) const {
deba@1566
   341
      node.id = _nodeNum-1;
deba@983
   342
    }
deba@983
   343
deba@983
   344
    static void next(Node& node) {
deba@983
   345
      --node.id;
deba@983
   346
    }
deba@983
   347
deba@983
   348
    void first(Edge& edge) const {
deba@1566
   349
      edge.id = _edgeNum-1;
deba@983
   350
    }
deba@983
   351
deba@983
   352
    static void next(Edge& edge) {
deba@983
   353
      --edge.id;
deba@983
   354
    }
deba@983
   355
deba@983
   356
    void firstOut(Edge& edge, const Node& node) const {      
deba@983
   357
      edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
deba@983
   358
    }
deba@983
   359
deba@983
   360
    /// \todo with specialized iterators we can make faster iterating
alpar@985
   361
    void nextOut(Edge& e) const {
alpar@986
   362
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   363
      int target = e.id - (source) * (source - 1) / 2; 
alpar@986
   364
      ++target;
alpar@986
   365
      e.id = target < source ? source * (source - 1) / 2 + target : -1;
deba@983
   366
    }
deba@983
   367
deba@983
   368
    void firstIn(Edge& edge, const Node& node) const {
deba@983
   369
      edge.id = node.id * (node.id + 1) / 2 - 1;
deba@983
   370
    }
deba@983
   371
    
alpar@985
   372
    void nextIn(Edge& e) const {
alpar@986
   373
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   374
      int target = e.id - (source) * (source - 1) / 2; ++target;
alpar@986
   375
      ++source;
deba@1566
   376
      e.id = source < _nodeNum ? source * (source - 1) / 2 + target : -1;
deba@983
   377
    }
deba@983
   378
deba@983
   379
  };
deba@983
   380
deba@1566
   381
  typedef UndirGraphExtender<UndirFullGraphBase>
deba@1566
   382
  UndirUndirFullGraphBase;
deba@1566
   383
  typedef AlterableUndirGraphExtender<UndirUndirFullGraphBase> 
deba@1566
   384
  AlterableUndirFullGraphBase;
deba@1566
   385
  typedef IterableUndirGraphExtender<AlterableUndirFullGraphBase> 
deba@1566
   386
  IterableUndirFullGraphBase;
deba@1566
   387
  typedef MappableUndirGraphExtender<IterableUndirFullGraphBase> 
deba@1566
   388
  MappableUndirFullGraphBase;
alpar@1555
   389
deba@1566
   390
  /// \ingroup graphs
deba@1566
   391
  ///
deba@1566
   392
  /// \brief An undirected full graph class.
deba@1566
   393
  ///
deba@1566
   394
  /// This is a simple and fast directed full graph implementation.
deba@1566
   395
  /// It is completely static, so you can neither add nor delete either
deba@1566
   396
  /// edges or nodes.
deba@1566
   397
  ///
deba@1566
   398
  /// The main difference beetween the \e FullGraph and \e UndirFullGraph class
deba@1566
   399
  /// is that this class conforms to the undirected graph concept and
deba@1566
   400
  /// it does not contain the hook edges.
deba@1566
   401
  ///
deba@1566
   402
  /// \sa FullGraph
deba@1566
   403
  ///
deba@1566
   404
  /// \author Balazs Dezso
deba@1566
   405
  class UndirFullGraph : public MappableUndirFullGraphBase {
deba@1566
   406
  public:
deba@1566
   407
    UndirFullGraph(int n) { construct(n); }
deba@1566
   408
  };
alpar@591
   409
alpar@921
   410
} //namespace lemon
alpar@591
   411
alpar@591
   412
alpar@921
   413
#endif //LEMON_FULL_GRAPH_H