LAL: Linear Arrangement Library 23.01.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 - 2023
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 (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
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 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, 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
47// lal includes
48#include <lal/graphs/rooted_tree.hpp>
49#include <lal/generate/tree_generator.hpp>
50#include <lal/numeric/integer.hpp>
51#include <lal/detail/data_array.hpp>
52
53namespace lal {
54namespace generate {
55
68public:
69 /* CONSTRUCTORS */
70
73 // add the initial values to m_rn
74 init_rn();
75 }
76
85 _rand_ulab_rooted_trees(uint64_t n, uint64_t seed = 0) noexcept
87 {
88 // add the initial values to m_rn
89 init_rn();
90
91 init(n, seed);
92 }
93
99
105
107 virtual ~_rand_ulab_rooted_trees() = default;
108
113
114 /* INITIALIZE */
115
126 void init(uint64_t n, uint64_t seed = 0) noexcept {
127 // setup memory
128 m_n = n;
130
131 if (m_n <= 1) { return; }
132
133 // initialize the random number generators
134 if (seed == 0) {
135 std::random_device rd;
136 m_gen = std::mt19937(rd());
137 }
138 else {
139 m_gen = std::mt19937(seed);
140 }
141 m_unif = std::uniform_real_distribution<double>(0, 1);
142
143 // force the addition of the necessary values for m_rn
144 get_rn(n);
145 }
146
166 void clear() noexcept {
167 // clear the values in m_rn
168 m_rn.clear();
169 // add the initial values to m_rn
170 init_rn();
171 // clear the other memory
173 }
174
175 /* GETTERS */
176
183
184protected:
186 uint64_t m_n;
187
189 std::mt19937 m_gen;
191 std::uniform_real_distribution<double> m_unif;
192
198 std::vector<numeric::integer> m_rn;
199
209 detail::data_array<uint64_t> m_head_vector;
210
211protected:
212
224 std::pair<uint64_t,uint64_t> ranrut
225 (uint64_t n, uint64_t lr, uint64_t nt) noexcept;
226
228 void init_rn() noexcept {
229 // from the OEIS: https://oeis.org/A000081
230 m_rn =
231 std::vector<numeric::integer>{
232 0ull,
233 1ull,
234 1ull,
235 2ull,
236 4ull,
237 9ull,
238 20ull,
239 48ull,
240 115ull,
241 286ull,
242 719ull,
243 1842ull,
244 4766ull,
245 12486ull,
246 32973ull,
247 87811ull,
248 235381ull,
249 634847ull,
250 1721159ull,
251 4688676ull,
252 12826228ull,
253 35221832ull,
254 97055181ull,
255 268282855ull,
256 743724984ull,
257 2067174645ull,
258 5759636510ull,
259 16083734329ull,
260 45007066269ull,
261 126186554308ull,
262 354426847597ull
263 };
264 }
265
272 const numeric::integer& get_rn(uint64_t n) noexcept;
273
284 std::pair<uint64_t,uint64_t> choose_jd_from_T(uint64_t n) noexcept;
285};
286
311class rand_ulab_rooted_trees : public _tree_generator<graphs::rooted_tree> {
312public:
313 /* CONSTRUCTORS */
314
317
326 rand_ulab_rooted_trees(uint64_t n, uint64_t seed = 0) noexcept
327 : _tree_generator<graphs::rooted_tree>(n), m_Gen(n, seed) { }
333
339
341 ~rand_ulab_rooted_trees() noexcept = default;
342
344 rand_ulab_rooted_trees& operator= (const rand_ulab_rooted_trees& g) noexcept = default;
346 rand_ulab_rooted_trees& operator= (rand_ulab_rooted_trees&& g) noexcept = default;
347
354 void init(uint64_t n, uint64_t seed = 0) noexcept {
356 m_Gen.init(n, seed);
357 }
358
360 void clear() noexcept {
362 m_Gen.clear();
363 }
364
366 return get_tree();
367 }
368
369protected:
377
378protected:
381};
382
383} // -- namespace generate
384} // -- namespace lal
Uniformly random generation of unlabelled rooted trees.
Definition: rand_ulab_rooted_trees.hpp:67
void init_rn() noexcept
Initialiases m_rn with values from the OEIS (see ).
Definition: rand_ulab_rooted_trees.hpp:228
_rand_ulab_rooted_trees(const _rand_ulab_rooted_trees &Gen)=default
Copy constructor.
std::pair< uint64_t, uint64_t > ranrut(uint64_t n, uint64_t lr, uint64_t nt) noexcept
Generates uniformly at random a rooted unlabelled tree of n nodes.
virtual ~_rand_ulab_rooted_trees()=default
Destructor.
_rand_ulab_rooted_trees & operator=(const _rand_ulab_rooted_trees &g) noexcept=default
Copy assignment operator.
void init(uint64_t n, uint64_t seed=0) noexcept
Sets the size of the unlabelled trees to generate.
Definition: rand_ulab_rooted_trees.hpp:126
_rand_ulab_rooted_trees() noexcept
Empty constructor.
Definition: rand_ulab_rooted_trees.hpp:72
std::vector< numeric::integer > m_rn
The number of unlabelled rooted trees.
Definition: rand_ulab_rooted_trees.hpp:198
_rand_ulab_rooted_trees(uint64_t n, uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition: rand_ulab_rooted_trees.hpp:85
const numeric::integer & get_rn(uint64_t n) noexcept
Computes all the values for .
std::pair< uint64_t, uint64_t > choose_jd_from_T(uint64_t n) noexcept
Chooses uniformly at random a pair , according to some probability.
_rand_ulab_rooted_trees(_rand_ulab_rooted_trees &&Gen)=default
Move constructor.
std::mt19937 m_gen
Random number generator.
Definition: rand_ulab_rooted_trees.hpp:189
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:191
uint64_t m_n
Number of nodes of the tree.
Definition: rand_ulab_rooted_trees.hpp:186
detail::data_array< uint64_t > m_head_vector
The head vector of the tree under construction.
Definition: rand_ulab_rooted_trees.hpp:209
void clear() noexcept
Clears the memory used.
Definition: rand_ulab_rooted_trees.hpp:166
Base class for tree generators.
Definition: tree_generator.hpp:123
void init(uint64_t n) noexcept
Initializes the tree generator.
Definition: tree_generator.hpp:159
void clear() noexcept
Clears the memory used by the generator.
Definition: tree_generator.hpp:165
graphs::rooted_tree get_tree() noexcept
Retrieve the generated tree.
Definition: tree_generator.hpp:195
Uniformly random generation of unlabelled rooted trees.
Definition: rand_ulab_rooted_trees.hpp:311
graphs::rooted_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition: rand_ulab_rooted_trees.hpp:365
void init(uint64_t n, uint64_t seed=0) noexcept
Initialise the generator.
Definition: rand_ulab_rooted_trees.hpp:354
rand_ulab_rooted_trees(uint64_t n, uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition: rand_ulab_rooted_trees.hpp:326
rand_ulab_rooted_trees(rand_ulab_rooted_trees &&Gen)=default
Move constructor.
~rand_ulab_rooted_trees() noexcept=default
Default destructor.
rand_ulab_rooted_trees(const rand_ulab_rooted_trees &Gen)=default
Copy constructor.
void clear() noexcept
Clear the memory used by the generator.
Definition: rand_ulab_rooted_trees.hpp:360
graphs::rooted_tree __get_tree() noexcept
Returns an unlabelled rooted tree chosen uniformly at random.
Definition: rand_ulab_rooted_trees.hpp:376
_rand_ulab_rooted_trees m_Gen
See _rand_ulab_rooted_trees for details.
Definition: rand_ulab_rooted_trees.hpp:380
rand_ulab_rooted_trees() noexcept
Empty constructor.
Definition: rand_ulab_rooted_trees.hpp:316
Rooted tree graph class.
Definition: rooted_tree.hpp:103
Arbitrary precision integer.
Definition: integer.hpp:60
Main namespace of the library.
Definition: basic_types.hpp:50
void resize(std::size_t new_size) noexcept
Resize the array.
Definition: data_array.hpp:175
void clear() noexcept
Clear the contents of the array.
Definition: data_array.hpp:162