lemon/hypercube_graph.h
author deba
Wed, 02 Nov 2005 15:25:13 +0000
changeset 1752 dce1f28ac595
parent 1693 269f0cbfbcc8
child 1791 62e7d237e1fb
permissions -rw-r--r--
IterableIntMap

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