lemon/hypercube_graph.h
author deba
Fri, 24 Feb 2006 11:13:09 +0000
changeset 1984 d4cbd10e1256
parent 1963 f1ace6d02a32
child 1986 9b56cca61e2e
permissions -rw-r--r--
Bug fix
deba@1693
     1
/* -*- C++ -*-
deba@1693
     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
deba@1693
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1693
     8
 *
deba@1693
     9
 * Permission to use, modify and distribute this software is granted
deba@1693
    10
 * provided that this copyright notice appears in all copies. For
deba@1693
    11
 * precise terms see the accompanying LICENSE file.
deba@1693
    12
 *
deba@1693
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1693
    14
 * express or implied, and with no claim as to its suitability for any
deba@1693
    15
 * purpose.
deba@1693
    16
 *
deba@1693
    17
 */
deba@1693
    18
deba@1693
    19
#ifndef HYPERCUBE_GRAPH_H
deba@1693
    20
#define HYPERCUBE_GRAPH_H
deba@1693
    21
deba@1693
    22
#include <iostream>
deba@1693
    23
#include <vector>
deba@1693
    24
#include <lemon/invalid.h>
deba@1693
    25
#include <lemon/utility.h>
deba@1791
    26
#include <lemon/error.h>
deba@1693
    27
deba@1791
    28
#include <lemon/bits/graph_extender.h>
deba@1693
    29
deba@1693
    30
///\ingroup graphs
deba@1693
    31
///\file
deba@1693
    32
///\brief HyperCubeGraph class.
deba@1693
    33
deba@1693
    34
namespace lemon {
deba@1693
    35
deba@1791
    36
  /// \brief Base graph for HyperCubeGraph.
deba@1693
    37
  ///
deba@1693
    38
  /// Base graph for hyper-cube graph. It describes some member functions
deba@1693
    39
  /// which can be used in the HyperCubeGraph.
deba@1693
    40
  ///
deba@1693
    41
  /// \warning Always use the HyperCubeGraph instead of this.
deba@1693
    42
  /// \see HyperCubeGraph
deba@1693
    43
  class HyperCubeGraphBase {
deba@1693
    44
deba@1693
    45
  public:
deba@1693
    46
deba@1693
    47
    typedef HyperCubeGraphBase Graph;
deba@1693
    48
deba@1693
    49
    class Node;
deba@1693
    50
    class Edge;
deba@1693
    51
deba@1693
    52
  public:
deba@1693
    53
deba@1693
    54
    HyperCubeGraphBase() {}
deba@1693
    55
deba@1693
    56
  protected:
deba@1693
    57
deba@1693
    58
    /// \brief Creates a hypercube graph with the given size.
deba@1693
    59
    ///
deba@1693
    60
    /// Creates a hypercube graph with the given size.
deba@1693
    61
    void construct(int dim) {
deba@1693
    62
      _dim = dim;
deba@1693
    63
      _nodeNum = 1 << dim;
deba@1693
    64
    }
deba@1693
    65
deba@1693
    66
  public:
deba@1693
    67
    
deba@1693
    68
deba@1693
    69
    typedef True NodeNumTag;
deba@1693
    70
    typedef True EdgeNumTag;
deba@1693
    71
deba@1693
    72
    ///Number of nodes.
deba@1693
    73
    int nodeNum() const { return _nodeNum; }
deba@1693
    74
    ///Number of edges.
deba@1693
    75
    int edgeNum() const { return _nodeNum * _dim; }
deba@1693
    76
deba@1693
    77
    /// Maximum node ID.
deba@1693
    78
    
deba@1693
    79
    /// Maximum node ID.
deba@1693
    80
    ///\sa id(Node)
deba@1791
    81
    int maxNodeId() const { return nodeNum() - 1; }
deba@1693
    82
    /// Maximum edge ID.
deba@1693
    83
    
deba@1693
    84
    /// Maximum edge ID.
deba@1693
    85
    ///\sa id(Edge)
deba@1791
    86
    int maxEdgeId() const { return edgeNum() - 1; }
deba@1693
    87
deba@1693
    88
    /// \brief Gives back the source node of an edge.
deba@1693
    89
    ///    
deba@1693
    90
    /// Gives back the source node of an edge.
deba@1693
    91
    Node source(Edge e) const {
deba@1693
    92
      return e.id / _dim;
deba@1693
    93
    }
deba@1693
    94
deba@1693
    95
    /// \brief Gives back the target node of an edge.
deba@1693
    96
    ///    
deba@1693
    97
    /// Gives back the target node of an edge.
deba@1693
    98
    Node target(Edge e) const {
deba@1693
    99
      return (e.id / _dim) ^ ( 1 << (e.id % _dim));
deba@1693
   100
    }
deba@1693
   101
deba@1693
   102
    /// Node ID.
deba@1693
   103
    
deba@1693
   104
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@1693
   105
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@1693
   106
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@1693
   107
    ///
deba@1693
   108
    /// The ID of the \ref INVALID node is -1.
deba@1693
   109
    ///\return The ID of the node \c v. 
deba@1693
   110
deba@1693
   111
    static int id(Node v) { return v.id; }
deba@1693
   112
    /// Edge ID.
deba@1693
   113
    
deba@1693
   114
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@1693
   115
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@1693
   116
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@1693
   117
    ///
deba@1693
   118
    /// The ID of the \ref INVALID edge is -1.
deba@1693
   119
    ///\return The ID of the edge \c e. 
deba@1693
   120
    static int id(Edge e) { return e.id; }
deba@1693
   121
deba@1791
   122
    static Node nodeFromId(int id) { return Node(id);}
deba@1693
   123
    
deba@1791
   124
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1693
   125
deba@1693
   126
    class Node {
deba@1693
   127
      friend class HyperCubeGraphBase;
deba@1693
   128
deba@1693
   129
    protected:
deba@1693
   130
      int id;
deba@1693
   131
      Node(int _id) { id = _id;}
deba@1693
   132
    public:
deba@1693
   133
      Node() {}
deba@1693
   134
      Node (Invalid) { id = -1; }
deba@1693
   135
      bool operator==(const Node node) const {return id == node.id;}
deba@1693
   136
      bool operator!=(const Node node) const {return id != node.id;}
deba@1693
   137
      bool operator<(const Node node) const {return id < node.id;}
deba@1693
   138
    };
deba@1693
   139
    
deba@1693
   140
    class Edge {
deba@1693
   141
      friend class HyperCubeGraphBase;
deba@1693
   142
      
deba@1693
   143
    protected:
deba@1693
   144
      int id; 
deba@1693
   145
deba@1693
   146
      Edge(int _id) : id(_id) {}
deba@1693
   147
deba@1693
   148
    public:
deba@1693
   149
      Edge() { }
deba@1693
   150
      Edge (Invalid) { id = -1; }
deba@1693
   151
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@1693
   152
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@1693
   153
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@1693
   154
    };
deba@1693
   155
deba@1693
   156
    void first(Node& node) const {
deba@1693
   157
      node.id = nodeNum() - 1;
deba@1693
   158
    }
deba@1693
   159
deba@1693
   160
    static void next(Node& node) {
deba@1693
   161
      --node.id;
deba@1693
   162
    }
deba@1693
   163
deba@1693
   164
    void first(Edge& edge) const {
deba@1693
   165
      edge.id = edgeNum() - 1;
deba@1693
   166
    }
deba@1693
   167
deba@1693
   168
    static void next(Edge& edge) {
deba@1693
   169
      --edge.id;
deba@1693
   170
    }
deba@1693
   171
deba@1693
   172
    void firstOut(Edge& edge, const Node& node) const {
deba@1693
   173
      edge.id = node.id * _dim;
deba@1693
   174
    }
deba@1693
   175
deba@1693
   176
    void nextOut(Edge& edge) const {
deba@1693
   177
      ++edge.id;
deba@1693
   178
      if (edge.id % _dim == 0) edge.id = -1;
deba@1693
   179
    }
deba@1693
   180
deba@1693
   181
    void firstIn(Edge& edge, const Node& node) const {
deba@1693
   182
      edge.id = (node.id ^ 1) * _dim;
deba@1693
   183
    }
deba@1693
   184
    
deba@1693
   185
    void nextIn(Edge& edge) const {
deba@1693
   186
      int cnt = edge.id % _dim;
deba@1693
   187
      if ((cnt + 1) % _dim == 0) {
deba@1693
   188
	edge.id = -1;
deba@1693
   189
      } else {
deba@1693
   190
	edge.id = ((edge.id / _dim) ^ ((1 << cnt) * 3)) * _dim + cnt + 1; 
deba@1693
   191
      }
deba@1693
   192
    }
deba@1693
   193
deba@1693
   194
    /// \brief Gives back the number of the dimensions.
deba@1693
   195
    ///
deba@1693
   196
    /// Gives back the number of the dimensions.
deba@1693
   197
    int dimension() const {
deba@1693
   198
      return _dim;
deba@1693
   199
    }
deba@1693
   200
deba@1693
   201
    /// \brief Returns true if the n'th bit of the node is one.
deba@1693
   202
    ///
deba@1693
   203
    /// Returns true if the n'th bit of the node is one. 
deba@1693
   204
    bool projection(Node node, int n) const {
deba@1693
   205
      return (bool)(node.id & (1 << n));
deba@1693
   206
    }
deba@1693
   207
deba@1693
   208
    /// \brief The dimension id of the edge.
deba@1693
   209
    ///
deba@1693
   210
    /// It returns the dimension id of the edge. It can
deba@1693
   211
    /// be in the ${0, 1, dim-1}$ intervall.
deba@1693
   212
    int dimension(Edge edge) const {
deba@1693
   213
      return edge.id % _dim;
deba@1693
   214
    }
deba@1693
   215
deba@1693
   216
    /// \brief Gives back the index of the node.
deba@1693
   217
    ///
deba@1693
   218
    /// Gives back the index of the node. The lower bits of the
deba@1693
   219
    /// integer describe the node.
deba@1693
   220
    int index(Node node) const {
deba@1693
   221
      return node.id;
deba@1693
   222
    }
deba@1693
   223
deba@1693
   224
    /// \brief Gives back the node by its index.
deba@1693
   225
    ///
deba@1693
   226
    ///  Gives back the node by its index.
deba@1693
   227
    Node node(int index) const {
deba@1693
   228
      return Node(index);
deba@1693
   229
    }
deba@1693
   230
    
deba@1693
   231
  private:
deba@1693
   232
    int _dim, _nodeNum;
deba@1693
   233
  };
deba@1693
   234
deba@1693
   235
deba@1979
   236
  typedef GraphExtender<HyperCubeGraphBase> ExtendedHyperCubeGraphBase;
deba@1693
   237
deba@1693
   238
  /// \ingroup graphs
deba@1693
   239
  ///
deba@1693
   240
  /// \brief HyperCube graph class
deba@1693
   241
  ///
deba@1693
   242
  /// This class implements a special graph type. The nodes of the
deba@1693
   243
  /// graph can be indiced with integers with at most \c dim binary length.
deba@1693
   244
  /// Two nodes are connected in the graph if the indices differ only
deba@1693
   245
  /// on one position in the binary form. 
deba@1693
   246
  ///
deba@1693
   247
  /// \note The type of the \c ids is chosen to \c int because efficiency
deba@1693
   248
  /// reasons. This way the maximal dimension of this implementation
deba@1693
   249
  /// is 26. 
deba@1693
   250
  ///
deba@1693
   251
  /// The graph type is fully conform to the \ref concept::StaticGraph
klao@1909
   252
  /// concept but it does not conform to the \ref concept::UGraph.
deba@1693
   253
  ///
deba@1693
   254
  /// \see HyperCubeGraphBase
deba@1693
   255
  /// \author Balazs Dezso
deba@1693
   256
  class HyperCubeGraph : public ExtendedHyperCubeGraphBase {
deba@1693
   257
  public:
deba@1693
   258
deba@1693
   259
    /// \brief Construct a graph with \c dim dimension.
deba@1693
   260
    ///
deba@1693
   261
    /// Construct a graph with \c dim dimension.
deba@1693
   262
    HyperCubeGraph(int dim) { construct(dim); }
deba@1693
   263
deba@1693
   264
    /// \brief Linear combination map.
deba@1693
   265
    ///
deba@1693
   266
    /// It makes possible to give back a linear combination
deba@1693
   267
    /// for each node. This function works like the \c std::accumulate
deba@1693
   268
    /// so it accumulates the \c bf binary function with the \c fv
deba@1693
   269
    /// first value. The map accumulates only on that dimensions where
deba@1693
   270
    /// the node's index is one. The accumulated values should be
deba@1693
   271
    /// given by the \c begin and \c end iterators and this range's length
deba@1693
   272
    /// should be the dimension number of the graph.
deba@1693
   273
    /// 
alpar@1946
   274
    ///\code
deba@1693
   275
    /// const int DIM = 3;
deba@1693
   276
    /// HyperCubeGraph graph(DIM);
deba@1693
   277
    /// xy<double> base[DIM];
deba@1693
   278
    /// for (int k = 0; k < DIM; ++k) {
deba@1693
   279
    ///   base[k].x = rand() / (RAND_MAX + 1.0);
deba@1693
   280
    ///   base[k].y = rand() / (RAND_MAX + 1.0);
deba@1693
   281
    /// } 
deba@1693
   282
    /// HyperCubeGraph::HyperMap<xy<double> > 
deba@1693
   283
    ///   pos(graph, base, base + DIM, xy<double>(0.0, 0.0));
alpar@1946
   284
    ///\endcode
deba@1693
   285
    ///
deba@1693
   286
    /// \see HyperCubeGraph
deba@1693
   287
    template <typename T, typename BF = std::plus<T> >
deba@1693
   288
    class HyperMap {
deba@1693
   289
    public:
deba@1693
   290
deba@1693
   291
      typedef Node Key;
deba@1693
   292
      typedef T Value;
deba@1693
   293
    
deba@1693
   294
      
deba@1693
   295
      /// \brief Constructor for HyperMap. 
deba@1693
   296
      ///
deba@1693
   297
      /// Construct a HyperMap for the given graph. The accumulated values 
deba@1693
   298
      /// should be given by the \c begin and \c end iterators and this 
deba@1693
   299
      /// range's length should be the dimension number of the graph.
deba@1693
   300
      ///
deba@1693
   301
      /// This function accumulates the \c bf binary function with 
deba@1693
   302
      /// the \c fv first value. The map accumulates only on that dimensions 
deba@1693
   303
      /// where the node's index is one.           
deba@1693
   304
      template <typename It>
deba@1693
   305
      HyperMap(const Graph& graph, It begin, It end, 
deba@1693
   306
		   T fv = 0.0, const BF& bf = BF()) 
deba@1693
   307
	: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) {
deba@1963
   308
	LEMON_ASSERT(_values.size() == graph.dimension(), 
deba@1791
   309
		     "Wrong size of dimension");
deba@1693
   310
      }
deba@1693
   311
deba@1693
   312
      /// \brief Gives back the partial accumulated value.
deba@1693
   313
      ///
deba@1693
   314
      /// Gives back the partial accumulated value.
deba@1693
   315
      Value operator[](Key k) const {
deba@1693
   316
	Value val = _first_value;
deba@1693
   317
	int id = _graph.index(k); 
deba@1693
   318
	int n = 0;
deba@1693
   319
	while (id != 0) {
deba@1693
   320
	  if (id & 1) {
deba@1693
   321
	    val = _bin_func(_values[n], _first_value);
deba@1693
   322
	  }
deba@1693
   323
	  id >>= 1;
deba@1693
   324
	  ++n;
deba@1693
   325
	}
deba@1693
   326
	return val;
deba@1693
   327
      }
deba@1693
   328
      
deba@1693
   329
    private:
deba@1693
   330
      const Graph& _graph;
deba@1693
   331
      std::vector<T> _values;
deba@1693
   332
      T _first_value;
deba@1693
   333
      BF _bin_func;
deba@1693
   334
    };    
deba@1693
   335
  };
deba@1693
   336
}
deba@1693
   337
#endif
deba@1693
   338