lemon/full_graph.h
author deba
Mon, 03 Oct 2005 10:11:29 +0000
changeset 1694 6d81e6f7a88d
parent 1669 66ae78d29f1e
child 1703 eb90e3d6bddc
permissions -rw-r--r--
Fixing naming conventions
Temporarly bugfix with named-parameters
Removing dead codes
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
deba@1692
    35
///\brief FullGraph and UndirFullGraph 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;
alpar@1643
   128
      Node(int _id) : id(_id) {}
alpar@591
   129
    public:
alpar@591
   130
      Node() {}
alpar@1643
   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@1669
   198
  typedef MappableGraphExtender<
deba@1669
   199
    IterableGraphExtender<
deba@1669
   200
    AlterableGraphExtender<FullGraphBase> > > ExtendedFullGraphBase;
klao@946
   201
deba@1566
   202
  /// \ingroup graphs
alpar@951
   203
  ///
deba@1566
   204
  /// \brief A full graph class.
deba@1566
   205
  ///
deba@1566
   206
  /// This is a simple and fast directed full graph implementation.
deba@1566
   207
  /// It is completely static, so you can neither add nor delete either
deba@1566
   208
  /// edges or nodes.
deba@1566
   209
  /// Thus it conforms to
deba@1566
   210
  /// the \ref concept::StaticGraph "StaticGraph" concept
deba@1566
   211
  /// \sa concept::StaticGraph.
deba@1566
   212
  ///
deba@1566
   213
  /// \author Alpar Juttner
deba@1669
   214
  class FullGraph : public ExtendedFullGraphBase {
klao@946
   215
  public:
klao@946
   216
klao@946
   217
    FullGraph(int n) { construct(n); }
klao@946
   218
  };
klao@946
   219
alpar@1555
   220
  ///@}
deba@983
   221
deba@983
   222
  class UndirFullGraphBase {
deba@1566
   223
    int _nodeNum;
deba@1566
   224
    int _edgeNum;
deba@983
   225
  public:
deba@983
   226
deba@984
   227
    typedef UndirFullGraphBase Graph;
deba@983
   228
deba@983
   229
    class Node;
deba@983
   230
    class Edge;
deba@983
   231
deba@983
   232
  public:
deba@983
   233
deba@984
   234
    UndirFullGraphBase() {}
deba@983
   235
deba@983
   236
deba@983
   237
    ///Creates a full graph with \c n nodes.
deba@1566
   238
    void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
deba@983
   239
    ///
deba@983
   240
    //    FullGraphBase(const FullGraphBase &_g)
deba@1566
   241
    //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
deba@983
   242
    
deba@983
   243
    typedef True NodeNumTag;
deba@983
   244
    typedef True EdgeNumTag;
deba@983
   245
deba@983
   246
    ///Number of nodes.
deba@1566
   247
    int nodeNum() const { return _nodeNum; }
deba@983
   248
    ///Number of edges.
deba@1566
   249
    int edgeNum() const { return _edgeNum; }
deba@983
   250
deba@983
   251
    /// Maximum node ID.
deba@983
   252
    
deba@983
   253
    /// Maximum node ID.
deba@983
   254
    ///\sa id(Node)
deba@1566
   255
    int maxId(Node = INVALID) const { return _nodeNum-1; }
deba@983
   256
    /// Maximum edge ID.
deba@983
   257
    
deba@983
   258
    /// Maximum edge ID.
deba@983
   259
    ///\sa id(Edge)
deba@1566
   260
    int maxId(Edge = INVALID) const { return _edgeNum-1; }
deba@983
   261
alpar@986
   262
    Node source(Edge e) const { 
deba@983
   263
      /// \todo we may do it faster
alpar@1643
   264
      return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
deba@983
   265
    }
deba@983
   266
alpar@986
   267
    Node target(Edge e) const { 
alpar@986
   268
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@1643
   269
      return Node(e.id - (source) * (source - 1) / 2);
deba@983
   270
    }
deba@983
   271
deba@983
   272
deba@983
   273
    /// Node ID.
deba@983
   274
    
deba@983
   275
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   276
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   277
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   278
    ///
deba@983
   279
    /// The ID of the \ref INVALID node is -1.
deba@983
   280
    ///\return The ID of the node \c v. 
deba@983
   281
deba@983
   282
    static int id(Node v) { return v.id; }
deba@983
   283
    /// Edge ID.
deba@983
   284
    
deba@983
   285
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   286
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   287
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   288
    ///
deba@983
   289
    /// The ID of the \ref INVALID edge is -1.
deba@983
   290
    ///\return The ID of the edge \c e. 
deba@983
   291
    static int id(Edge e) { return e.id; }
deba@983
   292
deba@983
   293
    /// Finds an edge between two nodes.
deba@983
   294
    
deba@983
   295
    /// Finds an edge from node \c u to node \c v.
deba@983
   296
    ///
deba@983
   297
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   298
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   299
    /// the next edge from \c u to \c v after \c prev.
deba@983
   300
    /// \return The found edge or INVALID if there is no such an edge.
deba@983
   301
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
deba@983
   302
    {
deba@983
   303
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
deba@983
   304
    }
deba@983
   305
    
deba@983
   306
      
deba@983
   307
    class Node {
alpar@985
   308
      friend class UndirFullGraphBase;
deba@983
   309
deba@983
   310
    protected:
deba@983
   311
      int id;
deba@983
   312
      Node(int _id) { id = _id;}
deba@983
   313
    public:
deba@983
   314
      Node() {}
deba@983
   315
      Node (Invalid) { id = -1; }
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
      bool operator<(const Node node) const {return id < node.id;}
deba@983
   319
    };
deba@983
   320
    
deba@983
   321
deba@983
   322
deba@983
   323
    class Edge {
alpar@985
   324
      friend class UndirFullGraphBase;
deba@983
   325
      
deba@983
   326
    protected:
deba@1566
   327
      int id;  // _nodeNum * target + source;
deba@983
   328
deba@983
   329
      Edge(int _id) : id(_id) {}
deba@983
   330
alpar@986
   331
      Edge(const UndirFullGraphBase& _graph, int source, int target) 
deba@1566
   332
	: id(_graph._nodeNum * target+source) {}
deba@983
   333
    public:
deba@983
   334
      Edge() { }
deba@983
   335
      Edge (Invalid) { id = -1; }
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
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@983
   339
    };
deba@983
   340
deba@983
   341
    void first(Node& node) const {
deba@1566
   342
      node.id = _nodeNum-1;
deba@983
   343
    }
deba@983
   344
deba@983
   345
    static void next(Node& node) {
deba@983
   346
      --node.id;
deba@983
   347
    }
deba@983
   348
deba@983
   349
    void first(Edge& edge) const {
deba@1566
   350
      edge.id = _edgeNum-1;
deba@983
   351
    }
deba@983
   352
deba@983
   353
    static void next(Edge& edge) {
deba@983
   354
      --edge.id;
deba@983
   355
    }
deba@983
   356
deba@983
   357
    void firstOut(Edge& edge, const Node& node) const {      
deba@983
   358
      edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
deba@983
   359
    }
deba@983
   360
deba@983
   361
    /// \todo with specialized iterators we can make faster iterating
alpar@985
   362
    void nextOut(Edge& e) const {
alpar@986
   363
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   364
      int target = e.id - (source) * (source - 1) / 2; 
alpar@986
   365
      ++target;
alpar@986
   366
      e.id = target < source ? source * (source - 1) / 2 + target : -1;
deba@983
   367
    }
deba@983
   368
deba@983
   369
    void firstIn(Edge& edge, const Node& node) const {
deba@983
   370
      edge.id = node.id * (node.id + 1) / 2 - 1;
deba@983
   371
    }
deba@983
   372
    
alpar@985
   373
    void nextIn(Edge& e) const {
alpar@986
   374
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   375
      int target = e.id - (source) * (source - 1) / 2; ++target;
alpar@986
   376
      ++source;
deba@1566
   377
      e.id = source < _nodeNum ? source * (source - 1) / 2 + target : -1;
deba@983
   378
    }
deba@983
   379
deba@983
   380
  };
deba@983
   381
deba@1669
   382
  typedef MappableUndirGraphExtender<
deba@1669
   383
    IterableUndirGraphExtender<
deba@1669
   384
    AlterableUndirGraphExtender<
deba@1669
   385
    UndirGraphExtender<UndirFullGraphBase> > > > ExtendedUndirFullGraphBase;
alpar@1555
   386
deba@1566
   387
  /// \ingroup graphs
deba@1566
   388
  ///
deba@1566
   389
  /// \brief An undirected full graph class.
deba@1566
   390
  ///
deba@1566
   391
  /// This is a simple and fast directed full graph implementation.
deba@1566
   392
  /// It is completely static, so you can neither add nor delete either
deba@1566
   393
  /// edges or nodes.
deba@1566
   394
  ///
deba@1566
   395
  /// The main difference beetween the \e FullGraph and \e UndirFullGraph class
deba@1566
   396
  /// is that this class conforms to the undirected graph concept and
deba@1566
   397
  /// it does not contain the hook edges.
deba@1566
   398
  ///
deba@1566
   399
  /// \sa FullGraph
deba@1566
   400
  ///
deba@1566
   401
  /// \author Balazs Dezso
deba@1669
   402
  class UndirFullGraph : public ExtendedUndirFullGraphBase {
deba@1566
   403
  public:
deba@1566
   404
    UndirFullGraph(int n) { construct(n); }
deba@1566
   405
  };
alpar@591
   406
alpar@921
   407
} //namespace lemon
alpar@591
   408
alpar@591
   409
alpar@921
   410
#endif //LEMON_FULL_GRAPH_H