LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
rand_lab_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 <random>
46
47// lal includes
48#include <lal/basic_types.hpp>
49#include <lal/graphs/free_tree.hpp>
50#include <lal/generate/tree_generator.hpp>
51#include <lal/detail/array.hpp>
52
53namespace lal {
54namespace generate {
55
69public:
70 /* CONSTRUCTORS */
71
73 _rand_lab_free_trees() noexcept { }
74
83 _rand_lab_free_trees(const uint64_t n, const uint64_t seed = 0) noexcept {
84 init(n, seed);
85 }
86
91 _rand_lab_free_trees(const _rand_lab_free_trees& Gen) noexcept = default;
92
97 _rand_lab_free_trees(_rand_lab_free_trees&& Gen) noexcept = default;
98
101
106
107 /* INITIALIZE */
108
115 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
116 m_n = n;
117 m_Prufer_seq.resize(m_n <= 2 ? 2 : m_n - 2);
118
119 if (seed == 0) {
120 std::random_device rd;
121 m_gen = std::mt19937(rd());
122 }
123 else {
124 m_gen = std::mt19937(seed);
125 }
126 m_unif = std::uniform_int_distribution<uint64_t>(0, m_n - 1);
127 }
128
133 void clear() noexcept {
135 }
136
137 /* GETTERS */
138
140 [[nodiscard]] graphs::free_tree get_tree() noexcept;
141
142protected:
144 uint64_t m_n;
145
147 std::mt19937 m_gen;
149 std::uniform_int_distribution<uint64_t> m_unif;
151 detail::array<uint64_t> m_Prufer_seq;
152};
153
178class rand_lab_free_trees : public _tree_generator<graphs::free_tree> {
179public:
180 /* CONSTRUCTORS */
181
183 rand_lab_free_trees() noexcept : _tree_generator<graphs::free_tree>() { }
184
193 rand_lab_free_trees(const uint64_t n, const uint64_t seed = 0) noexcept :
194 _tree_generator<graphs::free_tree>(n), m_Gen(n, seed) { }
200
206
209
214
221 void init(const uint64_t n, const uint64_t seed = 0) noexcept {
223 m_Gen.init(n, seed);
224 }
225
227 void clear() noexcept {
229 m_Gen.clear();
230 }
231
232 [[nodiscard]] graphs::free_tree yield_tree() noexcept {
233 return get_tree();
234 }
235
236protected:
243 [[nodiscard]] graphs::free_tree __get_tree() noexcept
244 { return m_Gen.get_tree(); }
245
246protected:
249};
250
251} // -- namespace generate
252} // -- namespace lal
Uniformly random selection of labelled free trees.
Definition rand_lab_free_trees.hpp:68
_rand_lab_free_trees(_rand_lab_free_trees &&Gen) noexcept=default
Move constructor.
_rand_lab_free_trees() noexcept
Default constructor.
Definition rand_lab_free_trees.hpp:73
_rand_lab_free_trees(const _rand_lab_free_trees &Gen) noexcept=default
Copy constructor.
_rand_lab_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_lab_free_trees.hpp:83
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_lab_free_trees.hpp:115
graphs::free_tree get_tree() noexcept
Returns a labelled free tree chosen uniformly at random.
void clear() noexcept
Clears the memory used.
Definition rand_lab_free_trees.hpp:133
_rand_lab_free_trees & operator=(const _rand_lab_free_trees &g) noexcept=default
Copy assignment operator.
std::uniform_int_distribution< uint64_t > m_unif
Distribution of the numbers.
Definition rand_lab_free_trees.hpp:149
~_rand_lab_free_trees()=default
Default destructor.
uint64_t m_n
Number of nodes of the tree.
Definition rand_lab_free_trees.hpp:144
std::mt19937 m_gen
Random number generator.
Definition rand_lab_free_trees.hpp:147
detail::array< uint64_t > m_Prufer_seq
Prüfer sequence.
Definition rand_lab_free_trees.hpp:151
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
Uniformly random selection of labelled free trees.
Definition rand_lab_free_trees.hpp:178
void clear() noexcept
Clear the memory used by the generator.
Definition rand_lab_free_trees.hpp:227
rand_lab_free_trees(const rand_lab_free_trees &Gen)=default
Copy constructor.
_rand_lab_free_trees m_Gen
See _rand_lab_free_trees.
Definition rand_lab_free_trees.hpp:248
graphs::free_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition rand_lab_free_trees.hpp:232
rand_lab_free_trees(rand_lab_free_trees &&Gen)=default
Move constructor.
graphs::free_tree __get_tree() noexcept
Returns a labelled free tree chosen uniformly at random.
Definition rand_lab_free_trees.hpp:243
rand_lab_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_lab_free_trees.hpp:193
rand_lab_free_trees() noexcept
Empty constructor.
Definition rand_lab_free_trees.hpp:183
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_lab_free_trees.hpp:221
~rand_lab_free_trees()=default
Default destructor.
Free tree graph class.
Definition free_tree.hpp:60
Main namespace of the library.
Definition basic_types.hpp:48
void resize(const std::size_t new_size) noexcept
Resize the array.
Definition array.hpp:187
void clear() noexcept
Clear the contents of the array.
Definition array.hpp:174