lemon/full_graph.h
author alpar
Wed, 13 Jul 2005 20:02:29 +0000
changeset 1556 caf0f91e16a7
parent 1435 8e85e6bbefdf
child 1566 12a3101cf3ab
permissions -rw-r--r--
Avoid ambiguity.
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
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
klao@946
    38
  class FullGraphBase {
alpar@591
    39
    int NodeNum;
alpar@591
    40
    int EdgeNum;
alpar@591
    41
  public:
deba@782
    42
klao@946
    43
    typedef FullGraphBase Graph;
alpar@591
    44
alpar@591
    45
    class Node;
alpar@591
    46
    class Edge;
deba@782
    47
alpar@591
    48
  public:
alpar@591
    49
klao@946
    50
    FullGraphBase() {}
klao@946
    51
klao@946
    52
alpar@591
    53
    ///Creates a full graph with \c n nodes.
klao@946
    54
    void construct(int n) { NodeNum = n; EdgeNum = n * n; }
alpar@591
    55
    ///
klao@946
    56
    //    FullGraphBase(const FullGraphBase &_g)
klao@946
    57
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    58
    
klao@977
    59
    typedef True NodeNumTag;
klao@977
    60
    typedef True EdgeNumTag;
klao@977
    61
alpar@813
    62
    ///Number of nodes.
alpar@813
    63
    int nodeNum() const { return NodeNum; }
alpar@813
    64
    ///Number of edges.
alpar@813
    65
    int edgeNum() const { return EdgeNum; }
alpar@591
    66
alpar@813
    67
    /// Maximum node ID.
alpar@813
    68
    
alpar@813
    69
    /// Maximum node ID.
alpar@813
    70
    ///\sa id(Node)
deba@980
    71
    int maxId(Node = INVALID) const { return NodeNum-1; }
alpar@813
    72
    /// Maximum edge ID.
alpar@813
    73
    
alpar@813
    74
    /// Maximum edge ID.
alpar@813
    75
    ///\sa id(Edge)
deba@980
    76
    int maxId(Edge = INVALID) const { return EdgeNum-1; }
alpar@591
    77
alpar@986
    78
    Node source(Edge e) const { return e.id % NodeNum; }
alpar@986
    79
    Node target(Edge e) const { return e.id / NodeNum; }
alpar@591
    80
alpar@591
    81
alpar@813
    82
    /// Node ID.
alpar@813
    83
    
alpar@813
    84
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    85
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    86
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    87
    ///
alpar@813
    88
    /// The ID of the \ref INVALID node is -1.
alpar@813
    89
    ///\return The ID of the node \c v. 
klao@946
    90
klao@946
    91
    static int id(Node v) { return v.id; }
alpar@813
    92
    /// Edge ID.
alpar@813
    93
    
alpar@813
    94
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
    95
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
    96
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
    97
    ///
alpar@813
    98
    /// The ID of the \ref INVALID edge is -1.
alpar@813
    99
    ///\return The ID of the edge \c e. 
klao@946
   100
    static int id(Edge e) { return e.id; }
alpar@591
   101
deba@1106
   102
    static Node fromId(int id, Node) { return Node(id);}
deba@1106
   103
    
deba@1106
   104
    static Edge fromId(int id, Edge) { return Edge(id);}
deba@1106
   105
alpar@774
   106
    /// Finds an edge between two nodes.
alpar@774
   107
    
alpar@774
   108
    /// Finds an edge from node \c u to node \c v.
alpar@774
   109
    ///
alpar@774
   110
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   111
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   112
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   113
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   114
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   115
    {
klao@946
   116
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   117
    }
alpar@774
   118
    
alpar@774
   119
      
alpar@591
   120
    class Node {
klao@946
   121
      friend class FullGraphBase;
alpar@591
   122
alpar@591
   123
    protected:
klao@946
   124
      int id;
klao@946
   125
      Node(int _id) { id = _id;}
alpar@591
   126
    public:
alpar@591
   127
      Node() {}
klao@946
   128
      Node (Invalid) { id = -1; }
klao@946
   129
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   130
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   131
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   132
    };
alpar@591
   133
    
klao@946
   134
klao@946
   135
klao@946
   136
    class Edge {
klao@946
   137
      friend class FullGraphBase;
klao@946
   138
      
klao@946
   139
    protected:
alpar@986
   140
      int id;  // NodeNum * target + source;
klao@946
   141
klao@946
   142
      Edge(int _id) : id(_id) {}
klao@946
   143
alpar@986
   144
      Edge(const FullGraphBase& _graph, int source, int target) 
alpar@986
   145
	: id(_graph.NodeNum * target+source) {}
alpar@591
   146
    public:
klao@946
   147
      Edge() { }
klao@946
   148
      Edge (Invalid) { id = -1; }
klao@946
   149
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   150
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   151
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   152
    };
alpar@591
   153
klao@946
   154
    void first(Node& node) const {
klao@946
   155
      node.id = NodeNum-1;
klao@946
   156
    }
alpar@591
   157
klao@946
   158
    static void next(Node& node) {
klao@946
   159
      --node.id;
klao@946
   160
    }
klao@946
   161
klao@946
   162
    void first(Edge& edge) const {
klao@946
   163
      edge.id = EdgeNum-1;
klao@946
   164
    }
klao@946
   165
klao@946
   166
    static void next(Edge& edge) {
klao@946
   167
      --edge.id;
klao@946
   168
    }
klao@946
   169
klao@946
   170
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   171
      edge.id = EdgeNum + node.id - NodeNum;
klao@946
   172
    }
klao@946
   173
klao@946
   174
    void nextOut(Edge& edge) const {
klao@946
   175
      edge.id -= NodeNum;
klao@946
   176
      if (edge.id < 0) edge.id = -1;
klao@946
   177
    }
klao@946
   178
klao@946
   179
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   180
      edge.id = node.id * NodeNum;
klao@946
   181
    }
alpar@591
   182
    
klao@946
   183
    void nextIn(Edge& edge) const {
klao@946
   184
      ++edge.id;
klao@946
   185
      if (edge.id % NodeNum == 0) edge.id = -1;
klao@946
   186
    }
alpar@591
   187
alpar@591
   188
  };
alpar@591
   189
klao@946
   190
klao@946
   191
  typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
klao@946
   192
  typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
deba@980
   193
  typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
klao@946
   194
alpar@1555
   195
  /// \addtogroup graphs
alpar@1555
   196
  /// @{
alpar@1555
   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
alpar@1555
   214
  ///@}
deba@983
   215
alpar@1161
   216
  // Base graph class for UndirFullGraph.
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
alpar@1555
   377
  /// \addtogroup graphs
alpar@1555
   378
  /// @{
alpar@1555
   379
alpar@1555
   380
  
deba@983
   381
  /// \todo UndirFullGraph from the UndirFullGraphBase
deba@983
   382
deba@983
   383
alpar@591
   384
  /// @}  
alpar@591
   385
alpar@921
   386
} //namespace lemon
alpar@591
   387
alpar@591
   388
alpar@921
   389
#endif //LEMON_FULL_GRAPH_H