src/lemon/full_graph.h
author zsuzska
Fri, 25 Mar 2005 22:05:54 +0000
changeset 1265 47c60ae9c26a
parent 1161 1c9658d51c8d
child 1307 d4acebef7276
permissions -rw-r--r--
zsuzska's own work directory
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/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@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
alpar@1161
   215
  // Base graph class for UndirFullGraph.
deba@983
   216
  class UndirFullGraphBase {
deba@983
   217
    int NodeNum;
deba@983
   218
    int EdgeNum;
deba@983
   219
  public:
deba@983
   220
deba@984
   221
    typedef UndirFullGraphBase Graph;
deba@983
   222
deba@983
   223
    class Node;
deba@983
   224
    class Edge;
deba@983
   225
deba@983
   226
  public:
deba@983
   227
deba@984
   228
    UndirFullGraphBase() {}
deba@983
   229
deba@983
   230
deba@983
   231
    ///Creates a full graph with \c n nodes.
deba@983
   232
    void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; }
deba@983
   233
    ///
deba@983
   234
    //    FullGraphBase(const FullGraphBase &_g)
deba@983
   235
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
deba@983
   236
    
deba@983
   237
    typedef True NodeNumTag;
deba@983
   238
    typedef True EdgeNumTag;
deba@983
   239
deba@983
   240
    ///Number of nodes.
deba@983
   241
    int nodeNum() const { return NodeNum; }
deba@983
   242
    ///Number of edges.
deba@983
   243
    int edgeNum() const { return EdgeNum; }
deba@983
   244
deba@983
   245
    /// Maximum node ID.
deba@983
   246
    
deba@983
   247
    /// Maximum node ID.
deba@983
   248
    ///\sa id(Node)
deba@983
   249
    int maxId(Node = INVALID) const { return NodeNum-1; }
deba@983
   250
    /// Maximum edge ID.
deba@983
   251
    
deba@983
   252
    /// Maximum edge ID.
deba@983
   253
    ///\sa id(Edge)
deba@983
   254
    int maxId(Edge = INVALID) const { return EdgeNum-1; }
deba@983
   255
alpar@986
   256
    Node source(Edge e) const { 
deba@983
   257
      /// \todo we may do it faster
deba@983
   258
      return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; 
deba@983
   259
    }
deba@983
   260
alpar@986
   261
    Node target(Edge e) const { 
alpar@986
   262
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   263
      return e.id - (source) * (source - 1) / 2; 
deba@983
   264
    }
deba@983
   265
deba@983
   266
deba@983
   267
    /// Node ID.
deba@983
   268
    
deba@983
   269
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@983
   270
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@983
   271
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@983
   272
    ///
deba@983
   273
    /// The ID of the \ref INVALID node is -1.
deba@983
   274
    ///\return The ID of the node \c v. 
deba@983
   275
deba@983
   276
    static int id(Node v) { return v.id; }
deba@983
   277
    /// Edge ID.
deba@983
   278
    
deba@983
   279
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@983
   280
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@983
   281
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@983
   282
    ///
deba@983
   283
    /// The ID of the \ref INVALID edge is -1.
deba@983
   284
    ///\return The ID of the edge \c e. 
deba@983
   285
    static int id(Edge e) { return e.id; }
deba@983
   286
deba@983
   287
    /// Finds an edge between two nodes.
deba@983
   288
    
deba@983
   289
    /// Finds an edge from node \c u to node \c v.
deba@983
   290
    ///
deba@983
   291
    /// If \c prev is \ref INVALID (this is the default value), then
deba@983
   292
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@983
   293
    /// the next edge from \c u to \c v after \c prev.
deba@983
   294
    /// \return The found edge or INVALID if there is no such an edge.
deba@983
   295
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
deba@983
   296
    {
deba@983
   297
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
deba@983
   298
    }
deba@983
   299
    
deba@983
   300
      
deba@983
   301
    class Node {
alpar@985
   302
      friend class UndirFullGraphBase;
deba@983
   303
deba@983
   304
    protected:
deba@983
   305
      int id;
deba@983
   306
      Node(int _id) { id = _id;}
deba@983
   307
    public:
deba@983
   308
      Node() {}
deba@983
   309
      Node (Invalid) { id = -1; }
deba@983
   310
      bool operator==(const Node node) const {return id == node.id;}
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
    };
deba@983
   314
    
deba@983
   315
deba@983
   316
deba@983
   317
    class Edge {
alpar@985
   318
      friend class UndirFullGraphBase;
deba@983
   319
      
deba@983
   320
    protected:
alpar@986
   321
      int id;  // NodeNum * target + source;
deba@983
   322
deba@983
   323
      Edge(int _id) : id(_id) {}
deba@983
   324
alpar@986
   325
      Edge(const UndirFullGraphBase& _graph, int source, int target) 
alpar@986
   326
	: id(_graph.NodeNum * target+source) {}
deba@983
   327
    public:
deba@983
   328
      Edge() { }
deba@983
   329
      Edge (Invalid) { id = -1; }
deba@983
   330
      bool operator==(const Edge edge) const {return id == edge.id;}
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
    };
deba@983
   334
deba@983
   335
    void first(Node& node) const {
deba@983
   336
      node.id = NodeNum-1;
deba@983
   337
    }
deba@983
   338
deba@983
   339
    static void next(Node& node) {
deba@983
   340
      --node.id;
deba@983
   341
    }
deba@983
   342
deba@983
   343
    void first(Edge& edge) const {
deba@983
   344
      edge.id = EdgeNum-1;
deba@983
   345
    }
deba@983
   346
deba@983
   347
    static void next(Edge& edge) {
deba@983
   348
      --edge.id;
deba@983
   349
    }
deba@983
   350
deba@983
   351
    void firstOut(Edge& edge, const Node& node) const {      
deba@983
   352
      edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
deba@983
   353
    }
deba@983
   354
deba@983
   355
    /// \todo with specialized iterators we can make faster iterating
alpar@985
   356
    void nextOut(Edge& e) const {
alpar@986
   357
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   358
      int target = e.id - (source) * (source - 1) / 2; 
alpar@986
   359
      ++target;
alpar@986
   360
      e.id = target < source ? source * (source - 1) / 2 + target : -1;
deba@983
   361
    }
deba@983
   362
deba@983
   363
    void firstIn(Edge& edge, const Node& node) const {
deba@983
   364
      edge.id = node.id * (node.id + 1) / 2 - 1;
deba@983
   365
    }
deba@983
   366
    
alpar@985
   367
    void nextIn(Edge& e) const {
alpar@986
   368
      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
alpar@986
   369
      int target = e.id - (source) * (source - 1) / 2; ++target;
alpar@986
   370
      ++source;
alpar@986
   371
      e.id = source < NodeNum ? source * (source - 1) / 2 + target : -1;
deba@983
   372
    }
deba@983
   373
deba@983
   374
  };
deba@983
   375
deba@983
   376
  /// \todo UndirFullGraph from the UndirFullGraphBase
deba@983
   377
deba@983
   378
  
deba@983
   379
alpar@591
   380
  /// @}  
alpar@591
   381
alpar@921
   382
} //namespace lemon
alpar@591
   383
alpar@591
   384
alpar@921
   385
#endif //LEMON_FULL_GRAPH_H