LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
rand_ulab_rooted_trees.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2024
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#include <random>
46#include <tuple>
47
48// lal includes
49#include <lal/graphs/rooted_tree.hpp>
50#include <lal/generate/tree_generator.hpp>
51#include <lal/numeric/integer.hpp>
52#include <lal/detail/array.hpp>
53
54namespace lal {
55namespace generate {
56
69public:
70 /* CONSTRUCTORS */
71
74 // add the initial values to m_rn
75 init_rn();
76 }
77
86 _rand_ulab_rooted_trees(const uint64_t n, const uint64_t seed = 0) noexcept :
88 {
89 // add the initial values to m_rn
90 init_rn();
91
92 init(n, seed);
93 }
94
100
106
108 virtual ~_rand_ulab_rooted_trees() = default;
109
114
115 /* INITIALIZE */
116
127 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
128 // setup memory
129 m_n = n;
130 m_head_vector.resize(m_n);
131
132 if (m_n <= 1) { return; }
133
134 // initialize the random number generators
135 if (seed == 0) {
136 std::random_device rd;
137 m_gen = std::mt19937(rd());
138 }
139 else {
140 m_gen = std::mt19937(seed);
141 }
142 m_unif = std::uniform_real_distribution<double>(0, 1);
143
144 // force the addition of the necessary values for m_rn
145 std::ignore = get_rn(n);
146 }
147
167 void clear() noexcept {
168 // clear the values in m_rn
169 m_rn.clear();
170 // add the initial values to m_rn
171 init_rn();
172 // clear the other memory
173 m_head_vector.clear();
174 }
175
176 /* GETTERS */
177
183 [[nodiscard]] graphs::rooted_tree get_tree() noexcept;
184
185protected:
187 uint64_t m_n;
188
190 std::mt19937 m_gen;
192 std::uniform_real_distribution<double> m_unif;
193
199 std::vector<numeric::integer> m_rn;
200
206 std::vector<numeric::integer> m_rn_times_n;
212 std::vector<numeric::integer> m_rn_times_n_minus_1;
213
222
223protected:
224
236 [[nodiscard]] std::pair<uint64_t,uint64_t> ranrut
237 (
238 const uint64_t n,
239 const uint64_t lr,
240 uint64_t nt
241 )
242 noexcept;
243
245 void init_rn() noexcept {
246 // from the OEIS: https://oeis.org/A000081
247 m_rn =
248 std::vector<numeric::integer>{
249 0ull,
250 1ull,
251 1ull,
252 2ull,
253 4ull,
254 9ull,
255 20ull,
256 48ull,
257 115ull,
258 286ull,
259 719ull,
260 1842ull,
261 4766ull,
262 12486ull,
263 32973ull,
264 87811ull,
265 235381ull,
266 634847ull,
267 1721159ull,
268 4688676ull,
269 12826228ull,
270 35221832ull,
271 97055181ull,
272 268282855ull,
273 743724984ull,
274 2067174645ull,
275 5759636510ull,
276 16083734329ull,
277 45007066269ull,
278 126186554308ull,
279 354426847597ull
280 };
281 m_rn_times_n.resize(m_rn.size());
282 m_rn_times_n[0] = 0;
283 m_rn_times_n_minus_1.resize(m_rn.size());
285 for (uint64_t n = 1; n < m_rn.size(); ++n) {
286 m_rn_times_n[n] = m_rn[n]*n;
288 }
289 }
290
297 [[nodiscard]] const numeric::integer& get_rn(const uint64_t n) noexcept;
298
300 [[nodiscard]] bool has_rn(const uint64_t n) const noexcept { return m_rn.size() >= n + 1; }
301
312 [[nodiscard]] std::pair<uint64_t,uint64_t> choose_jd_from_T(const uint64_t n) noexcept;
313};
314
339class rand_ulab_rooted_trees : public _tree_generator<graphs::rooted_tree> {
340public:
341 /* CONSTRUCTORS */
342
345
354 rand_ulab_rooted_trees(const uint64_t n, const uint64_t seed = 0) noexcept :
355 _tree_generator<graphs::rooted_tree>(n), m_Gen(n, seed) { }
360 rand_ulab_rooted_trees(const rand_ulab_rooted_trees& Gen) noexcept = default;
361
367
369 ~rand_ulab_rooted_trees() noexcept = default;
370
372 rand_ulab_rooted_trees& operator= (const rand_ulab_rooted_trees& g) noexcept = default;
374 rand_ulab_rooted_trees& operator= (rand_ulab_rooted_trees&& g) noexcept = default;
375
382 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
384 m_Gen.init(n, seed);
385 }
386
388 void clear() noexcept {
390 m_Gen.clear();
391 }
392
393 [[nodiscard]] graphs::rooted_tree yield_tree() noexcept {
394 return get_tree();
395 }
396
397protected:
404 [[nodiscard]] graphs::rooted_tree __get_tree() noexcept { return m_Gen.get_tree(); }
405
406protected:
409};
410
411} // -- namespace generate
412} // -- namespace lal
Uniformly random selection of unlabelled rooted trees.
Definition rand_ulab_rooted_trees.hpp:68
void init(const uint64_t n, const uint64_t seed=0) noexcept
Sets the size of the unlabelled trees to generate.
Definition rand_ulab_rooted_trees.hpp:127
head_vector m_head_vector
The head vector of the tree under construction.
Definition rand_ulab_rooted_trees.hpp:221
void init_rn() noexcept
Initialiases m_rn with values from the OEIS (see OEIS_A000081).
Definition rand_ulab_rooted_trees.hpp:245
_rand_ulab_rooted_trees(const _rand_ulab_rooted_trees &Gen)=default
Copy constructor.
virtual ~_rand_ulab_rooted_trees()=default
Destructor.
_rand_ulab_rooted_trees(const uint64_t n, const uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition rand_ulab_rooted_trees.hpp:86
_rand_ulab_rooted_trees & operator=(const _rand_ulab_rooted_trees &g) noexcept=default
Copy assignment operator.
std::pair< uint64_t, uint64_t > choose_jd_from_T(const uint64_t n) noexcept
Chooses uniformly at random a pair , according to some probability.
_rand_ulab_rooted_trees() noexcept
Empty constructor.
Definition rand_ulab_rooted_trees.hpp:73
std::vector< numeric::integer > m_rn
The number of unlabelled rooted trees.
Definition rand_ulab_rooted_trees.hpp:199
std::pair< uint64_t, uint64_t > ranrut(const uint64_t n, const uint64_t lr, uint64_t nt) noexcept
Generates uniformly at random a rooted unlabelled tree of n nodes.
bool has_rn(const uint64_t n) const noexcept
Returns whether or not the value (m_rn) has been computed.
Definition rand_ulab_rooted_trees.hpp:300
_rand_ulab_rooted_trees(_rand_ulab_rooted_trees &&Gen)=default
Move constructor.
std::vector< numeric::integer > m_rn_times_n
The number of unlabelled rooted trees times number of vertices.
Definition rand_ulab_rooted_trees.hpp:206
std::mt19937 m_gen
Random number generator.
Definition rand_ulab_rooted_trees.hpp:190
graphs::rooted_tree get_tree() noexcept
Generates uniformly at random a free unlabelled tree.
std::uniform_real_distribution< double > m_unif
Distribution of the numbers.
Definition rand_ulab_rooted_trees.hpp:192
std::vector< numeric::integer > m_rn_times_n_minus_1
The number of unlabelled rooted trees times number of vertices minus 1.
Definition rand_ulab_rooted_trees.hpp:212
const numeric::integer & get_rn(const uint64_t n) noexcept
Computes all the values for .
uint64_t m_n
Number of nodes of the tree.
Definition rand_ulab_rooted_trees.hpp:187
void clear() noexcept
Clears the memory used.
Definition rand_ulab_rooted_trees.hpp:167
Base class for tree generators.
Definition tree_generator.hpp:123
void clear() noexcept
Clears the memory used by the generator.
Definition tree_generator.hpp:166
void init(const uint64_t n) noexcept
Initializes the tree generator.
Definition tree_generator.hpp:160
graphs::rooted_tree get_tree() noexcept
Definition tree_generator.hpp:196
Uniformly random selection of unlabelled rooted trees.
Definition rand_ulab_rooted_trees.hpp:339
rand_ulab_rooted_trees(const uint64_t n, const uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition rand_ulab_rooted_trees.hpp:354
graphs::rooted_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition rand_ulab_rooted_trees.hpp:393
~rand_ulab_rooted_trees() noexcept=default
Default destructor.
rand_ulab_rooted_trees(rand_ulab_rooted_trees &&Gen) noexcept=default
Move constructor.
void clear() noexcept
Clear the memory used by the generator.
Definition rand_ulab_rooted_trees.hpp:388
void init(const uint64_t n, const uint64_t seed=0) noexcept
Initialize the generator.
Definition rand_ulab_rooted_trees.hpp:382
graphs::rooted_tree __get_tree() noexcept
Returns an unlabelled rooted tree chosen uniformly at random.
Definition rand_ulab_rooted_trees.hpp:404
rand_ulab_rooted_trees(const rand_ulab_rooted_trees &Gen) noexcept=default
Copy constructor.
_rand_ulab_rooted_trees m_Gen
See _rand_ulab_rooted_trees for details.
Definition rand_ulab_rooted_trees.hpp:408
rand_ulab_rooted_trees() noexcept
Empty constructor.
Definition rand_ulab_rooted_trees.hpp:344
Rooted tree graph class.
Definition rooted_tree.hpp:109
Arbitrary precision integer.
Definition integer.hpp:60
Main namespace of the library.
Definition basic_types.hpp:48
std::vector< uint64_t > head_vector
See Head vector page for further details.
Definition basic_types.hpp:58