CMR  1.3.0
matroid.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include <iostream>
5 #include <set>
6 
7 #include <boost/numeric/ublas/matrix.hpp>
8 #include <boost/numeric/ublas/matrix_proxy.hpp>
9 
10 #include "permutations.hpp"
11 
12 namespace tu
13 {
14 
20  template <typename NameType>
21  class matroid
22  {
23  public:
24  typedef NameType name_type;
25  typedef size_t size_type;
26  typedef std::vector <name_type> name_vector_type;
30 
38  matroid(const name_vector_type& names1, const name_vector_type& names2) :
39  _names1(names1), _names2(names2)
40  {
41 
42  }
43 
51  matroid(size_t size1 = 0, size_t size2 = 0)
52  {
53  resize(size1, size2);
54  }
55 
63  void resize(size_t size1, size_t size2)
64  {
65  _names1.resize(size1);
66  for (size_t i = 0; i < size1; ++i)
67  _names1[i] = -(i + 1);
68  _names2.resize(size2);
69  for (size_t i = 0; i < size2; ++i)
70  _names2[i] = (i + 1);
71  }
72 
77  inline size_t size1() const
78  {
79  return _names1.size();
80  }
81 
86  inline size_t size2() const
87  {
88  return _names2.size();
89  }
90 
96  inline name_type& name1(size_t index)
97  {
98  return _names1[index];
99  }
100 
106  inline const name_type& name1(size_t index) const
107  {
108  return _names1[index];
109  }
110 
116  inline name_type& name2(size_t index)
117  {
118  return _names2[index];
119  }
120 
126  inline const name_type& name2(size_t index) const
127  {
128  return _names2[index];
129  }
130 
135  inline std::set <NameType> get_elements() const
136  {
137  std::set <NameType> result;
138 
139  std::copy(_names1.begin(), _names1.end(), std::inserter(result, result.end()));
140  std::copy(_names2.begin(), _names2.end(), std::inserter(result, result.end()));
141 
142  return result;
143  }
144 
145  private:
146  name_vector_type _names1;
147  name_vector_type _names2;
148  };
149 
152 
161  template <typename NameType>
162  inline void matroid_permute1(matroid <NameType>& matroid, size_t index1, size_t index2)
163  {
164  std::swap(matroid.name1(index1), matroid.name1(index2));
165  }
166 
176  template <typename MatroidType, typename MatrixType>
177  inline void matroid_permute1(MatroidType& matroid, MatrixType& matrix, size_t index1, size_t index2)
178  {
179  matroid_permute1(matroid, index1, index2);
180  matrix_permute1(matrix, index1, index2);
181  }
182 
191  template <typename NameType>
192  inline void matroid_permute2(matroid <NameType>& matroid, size_t index1, size_t index2)
193  {
194  std::swap(matroid.name2(index1), matroid.name2(index2));
195  }
196 
206  template <typename MatroidType, typename MatrixType>
207  inline void matroid_permute2(MatroidType& matroid, MatrixType& matrix, size_t index1, size_t index2)
208  {
209  matroid_permute2(matroid, index1, index2);
210  matrix_permute2(matrix, index1, index2);
211  }
212 
221  template <typename NameType>
223  {
224  std::swap(matroid.name1(i), matroid.name2(j));
225  }
226 
236  template <typename MatroidType, typename MatrixType>
237  void matroid_binary_pivot(MatroidType& matroid, MatrixType& matrix, size_t i, size_t j)
238  {
240  matrix_binary_pivot(matrix, i, j);
241  }
242 
250  template <typename MatroidType, typename MatrixType>
251  void matroid_print(const MatroidType& matroid, const MatrixType& matrix)
252  {
253  assert (matroid.size1() == matrix.size1());
254  assert (matroid.size2() == matrix.size2());
255 
256  std::cout << " ";
257  for (size_t column = 0; column < matroid.size2(); ++column)
258  {
259  std::cout << std::setw(3) << matroid.name2(column);
260  }
261  std::cout << "\n ";
262  for (size_t column = 0; column < matroid.size2(); ++column)
263  {
264  std::cout << "--";
265  }
266  for (size_t row = 0; row < matroid.size1(); ++row)
267  {
268  std::cout << "\n" << std::setw(3) << matroid.name1(row) << " |";
269  for (size_t column = 0; column < matroid.size2(); ++column)
270  {
271  std::cout << std::setw(3) << matrix(row, column);
272  }
273  }
274  std::cout << std::endl;
275  }
276 
286  template <typename MatroidType, typename MatrixType>
287  void matroid_print_minor(const MatroidType& matroid, const MatrixType& matrix, size_t height, size_t width)
288  {
289  assert (matroid.size1() == matrix.size1());
290  assert (matroid.size2() == matrix.size2());
291 
292  std::cout << " ";
293  for (size_t column = 0; column < width; ++column)
294  {
295  std::cout << std::setw(3) << matroid.name2(column);
296  }
297  std::cout << "\n ";
298  for (size_t column = 0; column < width; ++column)
299  {
300  std::cout << "--";
301  }
302  for (size_t row = 0; row < height; ++row)
303  {
304  std::cout << "\n" << std::setw(3) << matroid.name1(row) << " |";
305  for (size_t column = 0; column < width; ++column)
306  {
307  std::cout << std::setw(3) << matrix(row, column);
308  }
309  }
310  std::cout << std::endl;
311  }
312 
320  template <typename MatroidType>
321  std::set <int> matroid_elements(const MatroidType& matroid)
322  {
323  std::set <int> elements;
324  for (size_t row = 0; row < matroid.size1(); ++row)
325  elements.insert(matroid.name1(row));
326  for (size_t column = 0; column < matroid.size2(); ++column)
327  elements.insert(matroid.name2(column));
328 
329  return elements;
330  }
331 
332 } /* namespace tu */
Definition: matroid.hpp:22
name_type & reference_type
Definition: matroid.hpp:27
std::vector< name_type > name_vector_type
Definition: matroid.hpp:26
const name_type & name1(size_t index) const
Definition: matroid.hpp:106
void resize(size_t size1, size_t size2)
Definition: matroid.hpp:63
size_t size1() const
Definition: matroid.hpp:77
matroid(size_t size1=0, size_t size2=0)
Definition: matroid.hpp:51
const name_type & name2(size_t index) const
Definition: matroid.hpp:126
size_t size2() const
Definition: matroid.hpp:86
matroid(const name_vector_type &names1, const name_vector_type &names2)
Definition: matroid.hpp:38
NameType name_type
Definition: matroid.hpp:24
name_type & name2(size_t index)
Definition: matroid.hpp:116
matroid< name_type > self_type
Definition: matroid.hpp:29
const name_type & const_reference_type
Definition: matroid.hpp:28
size_t size_type
Definition: matroid.hpp:25
name_type & name1(size_t index)
Definition: matroid.hpp:96
std::set< NameType > get_elements() const
Definition: matroid.hpp:135
Definition: algorithm.hpp:14
std::set< int > matroid_elements(const MatroidType &matroid)
Definition: matroid.hpp:321
void matrix_permute1(MatrixType &matrix, size_t index1, size_t index2)
Definition: matrix.hpp:1723
void matroid_print(const MatroidType &matroid, const MatrixType &matrix)
Definition: matroid.hpp:251
void matroid_print_minor(const MatroidType &matroid, const MatrixType &matrix, size_t height, size_t width)
Definition: matroid.hpp:287
void matroid_permute1(matroid< NameType > &matroid, size_t index1, size_t index2)
Definition: matroid.hpp:162
void matroid_permute2(matroid< NameType > &matroid, size_t index1, size_t index2)
Definition: matroid.hpp:192
void matrix_binary_pivot(matrix_permuted< MatrixType > &matrix, size_t i, size_t j)
Definition: matrix_permuted.hpp:224
void matroid_binary_pivot(matroid< NameType > &matroid, size_t i, size_t j)
Definition: matroid.hpp:222
matroid< int > integer_matroid
A matroid with integer names.
Definition: matroid.hpp:151
void matrix_permute2(MatrixType &matrix, size_t index1, size_t index2)
Definition: matrix.hpp:1740