src/lemon/full_graph.h
author klao
Wed, 27 Oct 2004 22:38:50 +0000
changeset 946 c94ef40a22ce
parent 921 818510fa3d99
child 951 0f1fe84ff36c
permissions -rw-r--r--
The graph_factory branch (@ 1321) has been merged to trunk.
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
klao@946
    20
klao@946
    21
#include <lemon/idmappable_graph_extender.h>
klao@946
    22
klao@946
    23
#include <lemon/iterable_graph_extender.h>
klao@946
    24
klao@946
    25
#include <lemon/alteration_observer_registry.h>
klao@946
    26
#include <lemon/default_map.h>
klao@946
    27
alpar@591
    28
///\ingroup graphs
alpar@591
    29
///\file
alpar@591
    30
///\brief FullGraph and SymFullGraph classes.
alpar@591
    31
alpar@591
    32
alpar@921
    33
#include <lemon/invalid.h>
alpar@591
    34
alpar@921
    35
namespace lemon {
alpar@591
    36
alpar@591
    37
/// \addtogroup graphs
alpar@591
    38
/// @{
alpar@591
    39
alpar@591
    40
  ///A full graph class.
alpar@591
    41
alpar@591
    42
  ///This is a simple and fast directed full graph implementation.
marci@606
    43
  ///It is completely static, so you can neither add nor delete either
alpar@591
    44
  ///edges or nodes.
alpar@880
    45
  ///Thus it conforms to
alpar@880
    46
  ///the \ref skeleton::StaticGraph "StaticGraph" concept
alpar@880
    47
  ///\sa skeleton::StaticGraph.
alpar@752
    48
  ///\todo What about loops?
alpar@752
    49
  ///\todo Don't we need SymEdgeMap?
alpar@591
    50
  ///
alpar@591
    51
  ///\author Alpar Juttner
klao@946
    52
  class FullGraphBase {
alpar@591
    53
    int NodeNum;
alpar@591
    54
    int EdgeNum;
alpar@591
    55
  public:
deba@782
    56
klao@946
    57
    typedef FullGraphBase Graph;
alpar@591
    58
alpar@591
    59
    class Node;
alpar@591
    60
    class Edge;
deba@782
    61
alpar@591
    62
  public:
alpar@591
    63
klao@946
    64
    FullGraphBase() {}
klao@946
    65
klao@946
    66
alpar@591
    67
    ///Creates a full graph with \c n nodes.
klao@946
    68
    void construct(int n) { NodeNum = n; EdgeNum = n * n; }
alpar@591
    69
    ///
klao@946
    70
    //    FullGraphBase(const FullGraphBase &_g)
klao@946
    71
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    72
    
alpar@813
    73
    ///Number of nodes.
alpar@813
    74
    int nodeNum() const { return NodeNum; }
alpar@813
    75
    ///Number of edges.
alpar@813
    76
    int edgeNum() const { return EdgeNum; }
alpar@591
    77
alpar@813
    78
    /// Maximum node ID.
alpar@813
    79
    
alpar@813
    80
    /// Maximum node ID.
alpar@813
    81
    ///\sa id(Node)
alpar@813
    82
    int maxNodeId() const { return NodeNum-1; }
alpar@813
    83
    /// Maximum edge ID.
alpar@813
    84
    
alpar@813
    85
    /// Maximum edge ID.
alpar@813
    86
    ///\sa id(Edge)
alpar@813
    87
    int maxEdgeId() const { return EdgeNum-1; }
alpar@591
    88
klao@946
    89
    Node tail(Edge e) const { return e.id % NodeNum; }
klao@946
    90
    Node head(Edge e) const { return e.id / NodeNum; }
alpar@591
    91
alpar@591
    92
alpar@813
    93
    /// Node ID.
alpar@813
    94
    
alpar@813
    95
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    96
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    97
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    98
    ///
alpar@813
    99
    /// The ID of the \ref INVALID node is -1.
alpar@813
   100
    ///\return The ID of the node \c v. 
klao@946
   101
klao@946
   102
    static int id(Node v) { return v.id; }
alpar@813
   103
    /// Edge ID.
alpar@813
   104
    
alpar@813
   105
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   106
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   107
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   108
    ///
alpar@813
   109
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   110
    ///\return The ID of the edge \c e. 
klao@946
   111
    static int id(Edge e) { return e.id; }
alpar@591
   112
alpar@774
   113
    /// Finds an edge between two nodes.
alpar@774
   114
    
alpar@774
   115
    /// Finds an edge from node \c u to node \c v.
alpar@774
   116
    ///
alpar@774
   117
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   118
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   119
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   120
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   121
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   122
    {
klao@946
   123
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   124
    }
alpar@774
   125
    
alpar@774
   126
      
alpar@591
   127
    class Node {
klao@946
   128
      friend class FullGraphBase;
alpar@591
   129
alpar@591
   130
    protected:
klao@946
   131
      int id;
klao@946
   132
      Node(int _id) { id = _id;}
alpar@591
   133
    public:
alpar@591
   134
      Node() {}
klao@946
   135
      Node (Invalid) { id = -1; }
klao@946
   136
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   137
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   138
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   139
    };
alpar@591
   140
    
klao@946
   141
klao@946
   142
klao@946
   143
    class Edge {
klao@946
   144
      friend class FullGraphBase;
klao@946
   145
      
klao@946
   146
    protected:
klao@946
   147
      int id;  // NodeNum * head + tail;
klao@946
   148
klao@946
   149
      Edge(int _id) : id(_id) {}
klao@946
   150
klao@946
   151
      Edge(const FullGraphBase& _graph, int tail, int head) 
klao@946
   152
	: id(_graph.NodeNum * head+tail) {}
alpar@591
   153
    public:
klao@946
   154
      Edge() { }
klao@946
   155
      Edge (Invalid) { id = -1; }
klao@946
   156
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   157
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   158
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   159
    };
alpar@591
   160
klao@946
   161
    void first(Node& node) const {
klao@946
   162
      node.id = NodeNum-1;
klao@946
   163
    }
alpar@591
   164
klao@946
   165
    static void next(Node& node) {
klao@946
   166
      --node.id;
klao@946
   167
    }
klao@946
   168
klao@946
   169
    void first(Edge& edge) const {
klao@946
   170
      edge.id = EdgeNum-1;
klao@946
   171
    }
klao@946
   172
klao@946
   173
    static void next(Edge& edge) {
klao@946
   174
      --edge.id;
klao@946
   175
    }
klao@946
   176
klao@946
   177
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   178
      edge.id = EdgeNum + node.id - NodeNum;
klao@946
   179
    }
klao@946
   180
klao@946
   181
    void nextOut(Edge& edge) const {
klao@946
   182
      edge.id -= NodeNum;
klao@946
   183
      if (edge.id < 0) edge.id = -1;
klao@946
   184
    }
klao@946
   185
klao@946
   186
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   187
      edge.id = node.id * NodeNum;
klao@946
   188
    }
alpar@591
   189
    
klao@946
   190
    void nextIn(Edge& edge) const {
klao@946
   191
      ++edge.id;
klao@946
   192
      if (edge.id % NodeNum == 0) edge.id = -1;
klao@946
   193
    }
alpar@591
   194
alpar@591
   195
  };
alpar@591
   196
klao@946
   197
klao@946
   198
  typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
klao@946
   199
  typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
klao@946
   200
  typedef IdMappableGraphExtender<IterableFullGraphBase> IdMappableFullGraphBase;
klao@946
   201
  typedef DefaultMappableGraphExtender<IdMappableFullGraphBase> MappableFullGraphBase;
klao@946
   202
klao@946
   203
  class FullGraph : public MappableFullGraphBase {
klao@946
   204
  public:
klao@946
   205
klao@946
   206
    FullGraph(int n) { construct(n); }
klao@946
   207
  };
klao@946
   208
klao@946
   209
  template <>
klao@946
   210
  int countNodes<FullGraph>(const FullGraph& graph) {
klao@946
   211
    return graph.nodeNum();
klao@946
   212
  }
klao@946
   213
klao@946
   214
  template <>
klao@946
   215
  int countEdges<FullGraph>(const FullGraph& graph) {
klao@946
   216
    return graph.edgeNum();
klao@946
   217
  }
klao@946
   218
alpar@591
   219
  /// @}  
alpar@591
   220
alpar@921
   221
} //namespace lemon
alpar@591
   222
alpar@591
   223
alpar@591
   224
alpar@591
   225
alpar@921
   226
#endif //LEMON_FULL_GRAPH_H