src/lemon/full_graph.h
author ladanyi
Thu, 04 Nov 2004 18:52:31 +0000
changeset 958 75f749682240
parent 946 c94ef40a22ce
child 959 c80ef5912903
permissions -rw-r--r--
Updated because of the recent changes in simann.h.
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
klao@946
    40
  class FullGraphBase {
alpar@591
    41
    int NodeNum;
alpar@591
    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.
klao@946
    56
    void construct(int n) { NodeNum = n; EdgeNum = n * n; }
alpar@591
    57
    ///
klao@946
    58
    //    FullGraphBase(const FullGraphBase &_g)
klao@946
    59
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    60
    
alpar@813
    61
    ///Number of nodes.
alpar@813
    62
    int nodeNum() const { return NodeNum; }
alpar@813
    63
    ///Number of edges.
alpar@813
    64
    int edgeNum() const { return EdgeNum; }
alpar@591
    65
alpar@813
    66
    /// Maximum node ID.
alpar@813
    67
    
alpar@813
    68
    /// Maximum node ID.
alpar@813
    69
    ///\sa id(Node)
alpar@813
    70
    int maxNodeId() const { return NodeNum-1; }
alpar@813
    71
    /// Maximum edge ID.
alpar@813
    72
    
alpar@813
    73
    /// Maximum edge ID.
alpar@813
    74
    ///\sa id(Edge)
alpar@813
    75
    int maxEdgeId() const { return EdgeNum-1; }
alpar@591
    76
klao@946
    77
    Node tail(Edge e) const { return e.id % NodeNum; }
klao@946
    78
    Node head(Edge e) const { return e.id / NodeNum; }
alpar@591
    79
alpar@591
    80
alpar@813
    81
    /// Node ID.
alpar@813
    82
    
alpar@813
    83
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    84
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    85
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    86
    ///
alpar@813
    87
    /// The ID of the \ref INVALID node is -1.
alpar@813
    88
    ///\return The ID of the node \c v. 
klao@946
    89
klao@946
    90
    static int id(Node v) { return v.id; }
alpar@813
    91
    /// Edge ID.
alpar@813
    92
    
alpar@813
    93
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
    94
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
    95
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
    96
    ///
alpar@813
    97
    /// The ID of the \ref INVALID edge is -1.
alpar@813
    98
    ///\return The ID of the edge \c e. 
klao@946
    99
    static int id(Edge e) { return e.id; }
alpar@591
   100
alpar@774
   101
    /// Finds an edge between two nodes.
alpar@774
   102
    
alpar@774
   103
    /// Finds an edge from node \c u to node \c v.
alpar@774
   104
    ///
alpar@774
   105
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   106
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   107
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   108
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   109
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   110
    {
klao@946
   111
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   112
    }
alpar@774
   113
    
alpar@774
   114
      
alpar@591
   115
    class Node {
klao@946
   116
      friend class FullGraphBase;
alpar@591
   117
alpar@591
   118
    protected:
klao@946
   119
      int id;
klao@946
   120
      Node(int _id) { id = _id;}
alpar@591
   121
    public:
alpar@591
   122
      Node() {}
klao@946
   123
      Node (Invalid) { id = -1; }
klao@946
   124
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   125
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   126
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   127
    };
alpar@591
   128
    
klao@946
   129
klao@946
   130
klao@946
   131
    class Edge {
klao@946
   132
      friend class FullGraphBase;
klao@946
   133
      
klao@946
   134
    protected:
klao@946
   135
      int id;  // NodeNum * head + tail;
klao@946
   136
klao@946
   137
      Edge(int _id) : id(_id) {}
klao@946
   138
klao@946
   139
      Edge(const FullGraphBase& _graph, int tail, int head) 
klao@946
   140
	: id(_graph.NodeNum * head+tail) {}
alpar@591
   141
    public:
klao@946
   142
      Edge() { }
klao@946
   143
      Edge (Invalid) { id = -1; }
klao@946
   144
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   145
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   146
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   147
    };
alpar@591
   148
klao@946
   149
    void first(Node& node) const {
klao@946
   150
      node.id = NodeNum-1;
klao@946
   151
    }
alpar@591
   152
klao@946
   153
    static void next(Node& node) {
klao@946
   154
      --node.id;
klao@946
   155
    }
klao@946
   156
klao@946
   157
    void first(Edge& edge) const {
klao@946
   158
      edge.id = EdgeNum-1;
klao@946
   159
    }
klao@946
   160
klao@946
   161
    static void next(Edge& edge) {
klao@946
   162
      --edge.id;
klao@946
   163
    }
klao@946
   164
klao@946
   165
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   166
      edge.id = EdgeNum + node.id - NodeNum;
klao@946
   167
    }
klao@946
   168
klao@946
   169
    void nextOut(Edge& edge) const {
klao@946
   170
      edge.id -= NodeNum;
klao@946
   171
      if (edge.id < 0) edge.id = -1;
klao@946
   172
    }
klao@946
   173
klao@946
   174
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   175
      edge.id = node.id * NodeNum;
klao@946
   176
    }
alpar@591
   177
    
klao@946
   178
    void nextIn(Edge& edge) const {
klao@946
   179
      ++edge.id;
klao@946
   180
      if (edge.id % NodeNum == 0) edge.id = -1;
klao@946
   181
    }
alpar@591
   182
alpar@591
   183
  };
alpar@591
   184
klao@946
   185
klao@946
   186
  typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
klao@946
   187
  typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
klao@946
   188
  typedef IdMappableGraphExtender<IterableFullGraphBase> IdMappableFullGraphBase;
klao@946
   189
  typedef DefaultMappableGraphExtender<IdMappableFullGraphBase> MappableFullGraphBase;
klao@946
   190
alpar@951
   191
  ///A full graph class.
alpar@951
   192
alpar@951
   193
  ///This is a simple and fast directed full graph implementation.
alpar@951
   194
  ///It is completely static, so you can neither add nor delete either
alpar@951
   195
  ///edges or nodes.
alpar@951
   196
  ///Thus it conforms to
alpar@951
   197
  ///the \ref skeleton::StaticGraph "StaticGraph" concept
alpar@951
   198
  ///\sa skeleton::StaticGraph.
alpar@951
   199
  ///\todo What about loops?
alpar@951
   200
  ///\todo Don't we need SymEdgeMap?
alpar@951
   201
  ///
alpar@951
   202
  ///\author Alpar Juttner
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