LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
all_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// lal includes
45#include <lal/basic_types.hpp>
46#include <lal/graphs/free_tree.hpp>
47#include <lal/generate/tree_generator.hpp>
48#include <lal/detail/array.hpp>
49
50namespace lal {
51namespace generate {
52
99class all_lab_free_trees : public _tree_generator<graphs::free_tree> {
100public:
101 /* CONSTRUCTORS */
102
104 all_lab_free_trees() noexcept : _tree_generator<graphs::free_tree>() { }
105
110 all_lab_free_trees(const uint64_t n) noexcept {
111 init(n);
112 }
113
118 all_lab_free_trees(const all_lab_free_trees& Gen) noexcept = default;
119
124 all_lab_free_trees(all_lab_free_trees&& Gen) noexcept = default;
125
128
130 all_lab_free_trees& operator= (const all_lab_free_trees& g) noexcept = default;
133
134 /* INITIALIZE */
135
140 void init(const uint64_t n) noexcept {
142 m_Prufer_seq.resize(m_n <= 2 ? 1 : m_n - 2, 0);
143 m_sm.resize(m_n <= 2 ? 1 : m_n - 2, 0);
144 reset();
145 }
146
151 void clear() noexcept {
154 m_sm.clear();
155 }
156
157 /* GETTERS */
158
160 [[nodiscard]] bool end() const noexcept { return m_reached_end; }
161
162 /* MODIFIERS */
163
171 void next() noexcept {
172 if (not has_next() or m_reached_end) {
173 m_reached_end = true;
174 return;
175 }
176
177 if (m_n <= 2) {
178 // there is only one tree we can make
179 m_sm[0] = 1;
180 return;
181 }
182
183 while (m_it > 0 and m_Prufer_seq[m_it] == m_n - 1) {
184 --m_it;
185 }
187
188 m_sm[m_it] =
189 (m_Prufer_seq[m_it] == m_n - 1 ?
190 (m_it == 0) or (m_sm[m_it - 1] and m_Prufer_seq[m_it - 1] == m_n - 1)
191 :
192 m_sm[m_it]
193 );
194
195 ++m_it;
196 if (m_it < m_Prufer_seq.size()) {
197 std::fill(m_Prufer_seq.begin() + m_it, m_Prufer_seq.end(), 0);
198 }
199 m_it = m_n - 3;
200 }
201
203 void reset() noexcept {
205 __reset();
206 next();
207 }
208
209 [[nodiscard]] graphs::free_tree yield_tree() noexcept {
210 const auto t = get_tree();
211 next();
212 return t;
213 }
214
215protected:
223
225 void __reset() noexcept {
226 m_reached_end = false;
227
228 if (m_n <= 2) {
229 m_sm[0] = 0;
230 // there is only one tree we can make
231 return;
232 }
233
234 m_it = 0;
235 m_sm.fill(false);
237 // place 'it' at the end of the sequence
238 m_it = m_n - 3;
239 // make sure that the first call to next()
240 // produces the sequence 0 0 ... 0
241 m_Prufer_seq[m_it] = static_cast<uint64_t>(-1);
242 m_L = m_n - 2;
243 }
244
246 [[nodiscard]] bool has_next() const noexcept {
247 return m_sm[(m_n <= 2 ? 0 : m_n - 3)] == 0;
248 }
249
250private:
252 uint64_t m_it;
254 uint64_t m_L;
260 bool m_reached_end = false;
261};
262
263} // -- namespace generate
264} // -- namespace lal
Base class for tree generators.
Definition tree_generator.hpp:123
uint64_t m_n
Definition tree_generator.hpp:300
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
void activate_all_postprocessing_actions() noexcept
Definition tree_generator.hpp:243
graphs::free_tree get_tree() noexcept
Definition tree_generator.hpp:196
Exhaustive enumeration of labelled free trees.
Definition all_lab_free_trees.hpp:99
detail::array< uint64_t > m_Prufer_seq
Prüfer sequence.
Definition all_lab_free_trees.hpp:256
bool end() const noexcept
Returns true if the end of the iteration was reached.
Definition all_lab_free_trees.hpp:160
void clear() noexcept
Clears the memory used.
Definition all_lab_free_trees.hpp:151
detail::array< char > m_sm
If sm[i] = true iff sm[0..i-1] = true and seq[0..i] = n-2.
Definition all_lab_free_trees.hpp:258
graphs::free_tree yield_tree() noexcept
Yields a tree, advancing the generator if necessary.
Definition all_lab_free_trees.hpp:209
~all_lab_free_trees()=default
Default destructor.
void reset() noexcept
Sets the generator to its initial state.
Definition all_lab_free_trees.hpp:203
bool m_reached_end
Has the end of the generation been reached?
Definition all_lab_free_trees.hpp:260
all_lab_free_trees(all_lab_free_trees &&Gen) noexcept=default
Move constructor.
bool has_next() const noexcept
Returns whether there are more trees to generate.
Definition all_lab_free_trees.hpp:246
uint64_t m_it
Iterator on the sequence.
Definition all_lab_free_trees.hpp:252
all_lab_free_trees(const all_lab_free_trees &Gen) noexcept=default
Copy constructor.
graphs::free_tree __get_tree() noexcept
Constructs the current tree.
all_lab_free_trees & operator=(const all_lab_free_trees &g) noexcept=default
Copy assignment operator.
uint64_t m_L
Left-most position with value .
Definition all_lab_free_trees.hpp:254
all_lab_free_trees(const uint64_t n) noexcept
Constructor with number of nodes.
Definition all_lab_free_trees.hpp:110
void next() noexcept
Generates next tree.
Definition all_lab_free_trees.hpp:171
void init(const uint64_t n) noexcept
Initializes the generator with a given number of vertices.
Definition all_lab_free_trees.hpp:140
void __reset() noexcept
Sets the generator to its initial state.
Definition all_lab_free_trees.hpp:225
all_lab_free_trees() noexcept
Empty constructor.
Definition all_lab_free_trees.hpp:104
Free tree graph class.
Definition free_tree.hpp:60
Main namespace of the library.
Definition basic_types.hpp:48
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
void fill(const T &v) noexcept
Assign the same value to every element in the data.
Definition array.hpp:272
T * begin() noexcept
Non-constant raw pointer to first element.
Definition array.hpp:300
T * end() noexcept
Non-constant raw pointer to last+1 element.
Definition array.hpp:302
std::size_t size() const noexcept
Size of the array.
Definition array.hpp:215
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