lemon/full_graph.h
author deba
Fri, 30 Jun 2006 12:14:36 +0000
changeset 2115 4cd528a30ec1
parent 2111 ea1fa1bc3f6d
child 2116 b6a68c15a6a3
permissions -rw-r--r--
Splitted graph files
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@591
    18
alpar@921
    19
#ifndef LEMON_FULL_GRAPH_H
alpar@921
    20
#define LEMON_FULL_GRAPH_H
alpar@591
    21
deba@983
    22
#include <cmath>
deba@983
    23
deba@1791
    24
#include <lemon/bits/graph_extender.h>
deba@1566
    25
deba@1993
    26
#include <lemon/bits/invalid.h>
deba@1993
    27
#include <lemon/bits/utility.h>
klao@977
    28
klao@977
    29
alpar@591
    30
///\ingroup graphs
alpar@591
    31
///\file
deba@2115
    32
///\brief FullGraph class.
alpar@591
    33
alpar@591
    34
alpar@921
    35
namespace lemon {
alpar@591
    36
deba@1986
    37
  /// \brief Base of the FullGrpah.
deba@1986
    38
  ///
deba@1986
    39
  /// Base of the FullGrpah.
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@977
    58
    typedef True NodeNumTag;
klao@977
    59
    typedef True EdgeNumTag;
klao@977
    60
deba@1986
    61
    /// \brief Returns the node with the given index.
deba@1986
    62
    ///
deba@1986
    63
    /// Returns the node with the given index. Because it is a
deba@1986
    64
    /// static size graph the node's of the graph can be indiced
deba@1986
    65
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
    66
    /// the node can accessed by the \e index() member.
deba@1986
    67
    Node operator()(int index) const { return Node(index); }
deba@1986
    68
deba@1986
    69
    /// \brief Returns the index of the node.
deba@1986
    70
    ///
deba@1986
    71
    /// Returns the index of the node. Because it is a
deba@1986
    72
    /// static size graph the node's of the graph can be indiced
deba@1986
    73
    /// by the range from 0 to \e nodeNum()-1 and the index of
deba@1986
    74
    /// the node can accessed by the \e index() member.
deba@1986
    75
    int index(const Node& node) const { return node.id; }
deba@1986
    76
alpar@813
    77
    ///Number of nodes.
deba@1566
    78
    int nodeNum() const { return _nodeNum; }
alpar@813
    79
    ///Number of edges.
deba@1566
    80
    int edgeNum() const { return _edgeNum; }
alpar@591
    81
alpar@813
    82
    /// Maximum node ID.
alpar@813
    83
    
alpar@813
    84
    /// Maximum node ID.
alpar@813
    85
    ///\sa id(Node)
deba@1791
    86
    int maxNodeId() const { return _nodeNum-1; }
alpar@813
    87
    /// Maximum edge ID.
alpar@813
    88
    
alpar@813
    89
    /// Maximum edge ID.
alpar@813
    90
    ///\sa id(Edge)
deba@1791
    91
    int maxEdgeId() const { return _edgeNum-1; }
alpar@591
    92
deba@1566
    93
    Node source(Edge e) const { return e.id % _nodeNum; }
deba@1566
    94
    Node target(Edge e) const { return e.id / _nodeNum; }
alpar@591
    95
alpar@591
    96
alpar@813
    97
    /// Node ID.
alpar@813
    98
    
alpar@813
    99
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   100
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   101
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   102
    ///
alpar@813
   103
    /// The ID of the \ref INVALID node is -1.
alpar@813
   104
    ///\return The ID of the node \c v. 
klao@946
   105
klao@946
   106
    static int id(Node v) { return v.id; }
alpar@813
   107
    /// Edge ID.
alpar@813
   108
    
alpar@813
   109
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   110
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   111
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   112
    ///
alpar@813
   113
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   114
    ///\return The ID of the edge \c e. 
klao@946
   115
    static int id(Edge e) { return e.id; }
alpar@591
   116
deba@1791
   117
    static Node nodeFromId(int id) { return Node(id);}
deba@1106
   118
    
deba@1791
   119
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   120
deba@1566
   121
    typedef True FindEdgeTag;
deba@1566
   122
alpar@774
   123
    /// Finds an edge between two nodes.
alpar@774
   124
    
alpar@774
   125
    /// Finds an edge from node \c u to node \c v.
alpar@774
   126
    ///
alpar@774
   127
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   128
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   129
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   130
    /// \return The found edge or INVALID if there is no such an edge.
deba@1566
   131
    Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
klao@946
   132
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   133
    }
alpar@774
   134
    
alpar@774
   135
      
alpar@591
   136
    class Node {
klao@946
   137
      friend class FullGraphBase;
alpar@591
   138
alpar@591
   139
    protected:
klao@946
   140
      int id;
alpar@1643
   141
      Node(int _id) : id(_id) {}
alpar@591
   142
    public:
alpar@591
   143
      Node() {}
alpar@1643
   144
      Node (Invalid) : id(-1) {}
klao@946
   145
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   146
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   147
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   148
    };
alpar@591
   149
    
klao@946
   150
klao@946
   151
klao@946
   152
    class Edge {
klao@946
   153
      friend class FullGraphBase;
klao@946
   154
      
klao@946
   155
    protected:
deba@1566
   156
      int id;  // _nodeNum * target + source;
klao@946
   157
klao@946
   158
      Edge(int _id) : id(_id) {}
klao@946
   159
alpar@986
   160
      Edge(const FullGraphBase& _graph, int source, int target) 
deba@1566
   161
	: id(_graph._nodeNum * target+source) {}
alpar@591
   162
    public:
klao@946
   163
      Edge() { }
klao@946
   164
      Edge (Invalid) { id = -1; }
klao@946
   165
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   166
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   167
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   168
    };
alpar@591
   169
klao@946
   170
    void first(Node& node) const {
deba@1566
   171
      node.id = _nodeNum-1;
klao@946
   172
    }
alpar@591
   173
klao@946
   174
    static void next(Node& node) {
klao@946
   175
      --node.id;
klao@946
   176
    }
klao@946
   177
klao@946
   178
    void first(Edge& edge) const {
deba@1566
   179
      edge.id = _edgeNum-1;
klao@946
   180
    }
klao@946
   181
klao@946
   182
    static void next(Edge& edge) {
klao@946
   183
      --edge.id;
klao@946
   184
    }
klao@946
   185
klao@946
   186
    void firstOut(Edge& edge, const Node& node) const {
deba@1566
   187
      edge.id = _edgeNum + node.id - _nodeNum;
klao@946
   188
    }
klao@946
   189
klao@946
   190
    void nextOut(Edge& edge) const {
deba@1566
   191
      edge.id -= _nodeNum;
klao@946
   192
      if (edge.id < 0) edge.id = -1;
klao@946
   193
    }
klao@946
   194
klao@946
   195
    void firstIn(Edge& edge, const Node& node) const {
deba@1566
   196
      edge.id = node.id * _nodeNum;
klao@946
   197
    }
alpar@591
   198
    
klao@946
   199
    void nextIn(Edge& edge) const {
klao@946
   200
      ++edge.id;
deba@1566
   201
      if (edge.id % _nodeNum == 0) edge.id = -1;
klao@946
   202
    }
alpar@591
   203
alpar@591
   204
  };
alpar@591
   205
deba@1979
   206
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
klao@946
   207
deba@1566
   208
  /// \ingroup graphs
alpar@951
   209
  ///
deba@1566
   210
  /// \brief A full graph class.
deba@1566
   211
  ///
deba@1566
   212
  /// This is a simple and fast directed full graph implementation.
deba@1566
   213
  /// It is completely static, so you can neither add nor delete either
deba@1566
   214
  /// edges or nodes.
deba@1566
   215
  /// Thus it conforms to
deba@2111
   216
  /// the \ref concept::Graph "Graph" concept
deba@2111
   217
  /// \sa concept::Graph.
deba@1566
   218
  ///
deba@1986
   219
  /// \sa FullGraphBase
deba@1986
   220
  /// \sa FullUGraph
deba@1986
   221
  ///
deba@1566
   222
  /// \author Alpar Juttner
deba@1669
   223
  class FullGraph : public ExtendedFullGraphBase {
klao@946
   224
  public:
klao@946
   225
deba@1979
   226
    typedef ExtendedFullGraphBase Parent;
deba@1979
   227
deba@1979
   228
    /// \brief Constructor
deba@1987
   229
    FullGraph() { construct(0); }
deba@1987
   230
deba@1987
   231
    /// \brief Constructor
deba@1979
   232
    ///
klao@946
   233
    FullGraph(int n) { construct(n); }
deba@1979
   234
deba@1979
   235
    /// \brief Resize the graph
deba@1979
   236
    ///
deba@1986
   237
    /// Resize the graph. The function will fully destroy and build the graph.
deba@1986
   238
    /// This cause that the maps of the graph will reallocated
deba@1986
   239
    /// automatically and the previous values will be lost.
deba@1979
   240
    void resize(int n) {
deba@1979
   241
      Parent::getNotifier(Edge()).clear();
deba@1979
   242
      Parent::getNotifier(Node()).clear();
deba@1979
   243
      construct(n);
deba@1979
   244
      Parent::getNotifier(Node()).build();
deba@1979
   245
      Parent::getNotifier(Edge()).build();
deba@1979
   246
    }
klao@946
   247
  };
klao@946
   248
alpar@921
   249
} //namespace lemon
alpar@591
   250
alpar@591
   251
alpar@921
   252
#endif //LEMON_FULL_GRAPH_H