LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
rand_ulab_free_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 <vector>
46#include <tuple>
47#include <map>
48
49// lal includes
50#include <lal/graphs/free_tree.hpp>
51#include <lal/generate/rand_ulab_rooted_trees.hpp>
52#include <lal/generate/tree_generator.hpp>
53#include <lal/numeric/integer.hpp>
54
55namespace lal {
56namespace generate {
57
75public:
76 /* CONSTRUCTORS */
77
80 // add the initial values to m_fn
81 init_fn();
82 }
83
92 _rand_ulab_free_trees(const uint64_t n, const uint64_t seed = 0) noexcept :
94 {
95 // add the initial values to m_fn
96 init_fn();
97
99 }
104 _rand_ulab_free_trees(const _rand_ulab_free_trees& Gen) noexcept = default;
105
111
114
119
120 /* INITIALIZE */
121
135 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
137 // force the addition of the necessary values for m_fn and m_rn
138 std::ignore = get_fn(n);
139 }
140
161 void clear() noexcept {
163 m_fn.clear();
164 m_alpha.clear();
165
166 // add the initial values to m_fn
167 init_fn();
168 }
169
170 /* GETTERS */
171
179 [[nodiscard]] graphs::free_tree get_tree() noexcept;
180
181private:
193 std::map<std::pair<uint64_t, uint64_t>, numeric::integer> m_alpha;
194
200 std::vector<numeric::integer> m_fn;
201
202private:
215 [[nodiscard]] uint64_t forest
216 (
217 const uint64_t m,
218 const uint64_t q,
219 uint64_t nt
220 )
221 noexcept;
222
224 void bicenter(const uint64_t n) noexcept;
225
237 [[nodiscard]] const numeric::integer& get_alpha_mq
238 (const uint64_t m, const uint64_t q)
239 noexcept;
240
242 void init_fn() noexcept {
243 // from the OEIS: https://oeis.org/A000055
244 m_fn =
245 std::vector<numeric::integer>{
246 1ull,
247 1ull,
248 1ull,
249 1ull,
250 2ull,
251 3ull,
252 6ull,
253 11ull,
254 23ull,
255 47ull,
256 106ull,
257 235ull,
258 551ull,
259 1301ull,
260 3159ull,
261 7741ull,
262 19320ull,
263 48629ull,
264 123867ull,
265 317955ull,
266 823065ull,
267 2144505ull,
268 5623756ull,
269 14828074ull,
270 39299897ull,
271 104636890ull,
272 279793450ull,
273 751065460ull,
274 2023443032ull,
275 5469566585ull,
276 14830871802ull
277 };
278 }
279
288 [[nodiscard]] const numeric::integer& get_fn(const uint64_t n) noexcept;
289
302 [[nodiscard]] std::pair<uint64_t,uint64_t> choose_jd_from_alpha
303 (const uint64_t m, const uint64_t q)
304 noexcept;
305};
306
331class rand_ulab_free_trees : public _tree_generator<graphs::free_tree> {
332public:
333 /* CONSTRUCTORS */
334
336 rand_ulab_free_trees() noexcept : _tree_generator<graphs::free_tree>() { }
337
346 rand_ulab_free_trees(const uint64_t n, const uint64_t seed = 0) noexcept :
347 _tree_generator<graphs::free_tree>(n), m_Gen(n, seed) { }
353
359
362
367
374 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
376 m_Gen.init(n, seed);
377 }
378
380 void clear() noexcept {
382 m_Gen.clear();
383 }
384
385 [[nodiscard]] graphs::free_tree yield_tree() noexcept {
386 return get_tree();
387 }
388
389protected:
396 [[nodiscard]] graphs::free_tree __get_tree() noexcept {
397 return m_Gen.get_tree();
398 }
399
400protected:
403};
404
405} // -- namespace generate
406} // -- namespace lal
Uniformly random selection of unlabelled free trees.
Definition rand_ulab_free_trees.hpp:74
_rand_ulab_free_trees & operator=(const _rand_ulab_free_trees &g) noexcept=default
Copy assignment operator.
const numeric::integer & get_fn(const uint64_t n) noexcept
Computes and returns the value .
void init_fn() noexcept
Initialiases m_fn with values from the OEIS (see OEIS_A000055).
Definition rand_ulab_free_trees.hpp:242
const numeric::integer & get_alpha_mq(const uint64_t m, const uint64_t q) noexcept
Computes and return the value .
~_rand_ulab_free_trees()=default
Default destructor.
std::vector< numeric::integer > m_fn
The number of free unlabelled trees.
Definition rand_ulab_free_trees.hpp:200
graphs::free_tree get_tree() noexcept
Generates uniformly at random a free unlabelled tree.
_rand_ulab_free_trees(const _rand_ulab_free_trees &Gen) noexcept=default
Copy constructor.
_rand_ulab_free_trees() noexcept
Empty constructor.
Definition rand_ulab_free_trees.hpp:79
uint64_t forest(const uint64_t m, const uint64_t q, uint64_t nt) noexcept
Generates uniformly at random a forest of m nodes.
std::pair< uint64_t, uint64_t > choose_jd_from_alpha(const uint64_t m, const uint64_t q) noexcept
Chooses uniformly at random a pair , according to some probability.
void init(const uint64_t n, const uint64_t seed=0) noexcept
Sets the size of the unlabelled trees to generate.
Definition rand_ulab_free_trees.hpp:135
void bicenter(const uint64_t n) noexcept
Generates a tree of n nodes with two centroids.
std::map< std::pair< uint64_t, uint64_t >, numeric::integer > m_alpha
Values .
Definition rand_ulab_free_trees.hpp:193
void clear() noexcept
Clears the memory used.
Definition rand_ulab_free_trees.hpp:161
_rand_ulab_free_trees(_rand_ulab_free_trees &&Gen) noexcept=default
Move constructor.
_rand_ulab_free_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_free_trees.hpp:92
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
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::free_tree get_tree() noexcept
Definition tree_generator.hpp:196
Uniformly random selection of unlabelled free trees.
Definition rand_ulab_free_trees.hpp:331
rand_ulab_free_trees & operator=(const rand_ulab_free_trees &g) noexcept=default
Copy assignment operator.
rand_ulab_free_trees(const rand_ulab_free_trees &Gen)=default
Copy constructor.
~rand_ulab_free_trees()=default
Default destructor.
void clear() noexcept
Clear the memory used by the generator.
Definition rand_ulab_free_trees.hpp:380
_rand_ulab_free_trees m_Gen
See _rand_ulab_free_trees.
Definition rand_ulab_free_trees.hpp:402
rand_ulab_free_trees() noexcept
Empty constructor.
Definition rand_ulab_free_trees.hpp:336
graphs::free_tree __get_tree() noexcept
Returns an unlabelled free tree chosen uniformly at random.
Definition rand_ulab_free_trees.hpp:396
void init(const uint64_t n, const uint64_t seed=0) noexcept
Initializes the generator with the number of nodes and a seed.
Definition rand_ulab_free_trees.hpp:374
rand_ulab_free_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_free_trees.hpp:346
graphs::free_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition rand_ulab_free_trees.hpp:385
rand_ulab_free_trees(rand_ulab_free_trees &&Gen)=default
Move constructor.
Free tree graph class.
Definition free_tree.hpp:60
Arbitrary precision integer.
Definition integer.hpp:60
Main namespace of the library.
Definition basic_types.hpp:48