LAL: Linear Arrangement Library 23.01.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 - 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 <vector>
46#include <map>
47
48// lal includes
49#include <lal/graphs/free_tree.hpp>
50#include <lal/generate/rand_ulab_rooted_trees.hpp>
51#include <lal/generate/tree_generator.hpp>
52#include <lal/numeric/integer.hpp>
53
54namespace lal {
55namespace generate {
56
74public:
75 /* CONSTRUCTORS */
76
79 // add the initial values to m_fn
80 init_fn();
81 }
82
91 _rand_ulab_free_trees(uint64_t n, uint64_t seed = 0) noexcept
93 {
94 // add the initial values to m_fn
95 init_fn();
96
98 }
104
110
113
118
119 /* INITIALIZE */
120
134 void init(uint64_t n, uint64_t seed = 0) noexcept {
136 // force the addition of the necessary values for m_fn and m_rn
137 get_fn(n);
138 }
139
160 void clear() noexcept {
162 m_fn.clear();
163 m_alpha.clear();
164
165 // add the initial values to m_fn
166 init_fn();
167 }
168
169 /* GETTERS */
170
179
180private:
192 std::map<std::pair<uint64_t, uint64_t>, numeric::integer> m_alpha;
193
199 std::vector<numeric::integer> m_fn;
200
201private:
214 uint64_t forest(uint64_t m, uint64_t q, uint64_t nt) noexcept;
215
217 void bicenter(uint64_t n) noexcept;
218
230 const numeric::integer&
231 get_alpha_mq(const uint64_t m, const uint64_t q) noexcept;
232
234 void init_fn() noexcept {
235 // from the OEIS: https://oeis.org/A000055
236 m_fn =
237 std::vector<numeric::integer>{
238 1ull,
239 1ull,
240 1ull,
241 1ull,
242 2ull,
243 3ull,
244 6ull,
245 11ull,
246 23ull,
247 47ull,
248 106ull,
249 235ull,
250 551ull,
251 1301ull,
252 3159ull,
253 7741ull,
254 19320ull,
255 48629ull,
256 123867ull,
257 317955ull,
258 823065ull,
259 2144505ull,
260 5623756ull,
261 14828074ull,
262 39299897ull,
263 104636890ull,
264 279793450ull,
265 751065460ull,
266 2023443032ull,
267 5469566585ull,
268 14830871802ull
269 };
270 }
271
280 const numeric::integer& get_fn(const uint64_t n) noexcept;
281
294 std::pair<uint64_t,uint64_t>
295 choose_jd_from_alpha(const uint64_t m, const uint64_t q) noexcept;
296};
297
322class rand_ulab_free_trees : public _tree_generator<graphs::free_tree> {
323public:
324 /* CONSTRUCTORS */
325
327 rand_ulab_free_trees() noexcept : _tree_generator<graphs::free_tree>() { }
328
337 rand_ulab_free_trees(uint64_t n, uint64_t seed = 0) noexcept
338 : _tree_generator<graphs::free_tree>(n), m_Gen(n, seed) { }
344
350
353
358
365 void init(uint64_t n, uint64_t seed = 0) noexcept {
367 m_Gen.init(n, seed);
368 }
369
371 void clear() noexcept {
373 m_Gen.clear();
374 }
375
377 return get_tree();
378 }
379
380protected:
388
389protected:
392};
393
394} // -- namespace generate
395} // -- namespace lal
Uniformly random generation of unlabelled free trees.
Definition: rand_ulab_free_trees.hpp:73
_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 ).
Definition: rand_ulab_free_trees.hpp:234
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:199
graphs::free_tree get_tree() noexcept
Generates uniformly at random a free unlabelled tree.
void init(uint64_t n, uint64_t seed=0) noexcept
Sets the size of the unlabelled trees to generate.
Definition: rand_ulab_free_trees.hpp:134
_rand_ulab_free_trees(_rand_ulab_free_trees &&Gen)=default
Move constructor.
_rand_ulab_free_trees(uint64_t n, uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition: rand_ulab_free_trees.hpp:91
_rand_ulab_free_trees() noexcept
Empty constructor.
Definition: rand_ulab_free_trees.hpp:78
uint64_t forest(uint64_t m, 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 bicenter(uint64_t n) noexcept
Generates a tree of n nodes with two centroids.
_rand_ulab_free_trees(const _rand_ulab_free_trees &Gen)=default
Copy constructor.
std::map< std::pair< uint64_t, uint64_t >, numeric::integer > m_alpha
Values .
Definition: rand_ulab_free_trees.hpp:192
void clear() noexcept
Clears the memory used.
Definition: rand_ulab_free_trees.hpp:160
Uniformly random generation of unlabelled rooted trees.
Definition: rand_ulab_rooted_trees.hpp:67
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
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::free_tree get_tree() noexcept
Retrieve the generated tree.
Definition: tree_generator.hpp:195
Uniformly random generation of unlabelled free trees.
Definition: rand_ulab_free_trees.hpp:322
void init(uint64_t n, uint64_t seed=0) noexcept
Initializes the generator with the number of nodes and a seed.
Definition: rand_ulab_free_trees.hpp:365
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(uint64_t n, uint64_t seed=0) noexcept
Constructor with size of tree and seed for the random number generator.
Definition: rand_ulab_free_trees.hpp:337
~rand_ulab_free_trees()=default
Default destructor.
void clear() noexcept
Clear the memory used by the generator.
Definition: rand_ulab_free_trees.hpp:371
_rand_ulab_free_trees m_Gen
See _rand_ulab_free_trees.
Definition: rand_ulab_free_trees.hpp:391
rand_ulab_free_trees() noexcept
Empty constructor.
Definition: rand_ulab_free_trees.hpp:327
graphs::free_tree __get_tree() noexcept
Returns an unlabelled free tree chosen uniformly at random.
Definition: rand_ulab_free_trees.hpp:387
graphs::free_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition: rand_ulab_free_trees.hpp:376
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:50