49#include <lal/definitions.hpp>
50#include <lal/graphs/free_tree.hpp>
51#include <lal/graphs/rooted_tree.hpp>
52#include <lal/internal/data_array.hpp>
62inline head_vector from_tree_to_head_vector(
const graphs::rooted_tree& t)
66 assert(t.is_rooted_tree());
69 const uint32_t n = t.get_num_nodes();
71 for (
node u = 0; u < n; ++u) {
72 if (u == t.get_root()) {
77 hv[u] = t.get_in_neighbours(u)[0] + 1;
85inline head_vector from_tree_to_head_vector(
const graphs::free_tree& t,
node r)
91 return from_tree_to_head_vector(graphs::rooted_tree(t,r));
98 bool is_rooted = std::is_base_of_v<graphs::rooted_tree, tree_type>
103 std::pair<graphs::free_tree,node>
105from_head_vector_to_tree
109 if (hv.size() == 0) {
110 if constexpr (is_rooted) {
111 return graphs::rooted_tree(0);
114 return std::make_pair(graphs::free_tree(0), 0);
117 if (hv.size() == 1) {
122 if constexpr (is_rooted) {
123 return graphs::rooted_tree(1);
126 return std::make_pair(graphs::free_tree(1), 0);
130 const uint32_t n =
static_cast<uint32_t
>(hv.size());
136 std::optional<node> r;
139 uint32_t num_edges_added = 0;
142 for (uint32_t i = 0; i < n; ++i) {
156 t.add_edge_bulk(hv[i] - 1, i);
168 assert(num_edges_added == n - 1);
171 t.finish_bulk_add(normalise, check);
173 if constexpr (is_rooted) {
175 t.set_valid_orientation(
true);
177 assert(t.is_rooted_tree());
187 return std::make_pair(t, *r);
212inline graphs::free_tree level_sequence_to_ftree
213(
const uint32_t *
const L, uint32_t n,
bool normalise =
true,
bool check =
true)
223 graphs::free_tree t(n);
227 data_array<node> lev(n+1, 0);
228 uint32_t stack_it = 0;
233 for (
node i = 2; i <= n; ++i) {
236 if (lev[stack_it] + 2 > L[i]) {
237 stack_it = L[i] - 2 + 1;
241 const node r = lev[stack_it];
244 const auto [u,v] = (r == 0 ?
edge(r, i - 1) :
edge(r - 1, i - 1));
245 t.add_edge_bulk(u, v);
250 assert(stack_it == L[i]);
255 t.finish_bulk_add(normalise, check);
259inline graphs::free_tree level_sequence_to_ftree
260(
const std::vector<uint32_t>& L, uint32_t n,
bool normalise =
true,
bool check =
true)
262{
return level_sequence_to_ftree(&L[0], n, normalise, check); }
278inline graphs::free_tree Prufer_sequence_to_ftree
279(
const uint32_t *
const seq, uint32_t n,
bool normalise =
true,
bool check =
true)
283 const uint32_t L = n - 2;
284 data_array<uint32_t> degree(n, 1);
285 for (uint32_t i = 0; i < L; ++i) {
286 degree[ seq[i] ] += 1;
290 graphs::free_tree t(n);
296 for (uint32_t v = 0; v < L; ++v) {
297 const auto value = seq[v];
298 bool node_found =
false;
300 while (w < n and not node_found) {
301 if (degree[w] == 1) {
302 t.add_edge_bulk(value, w);
315 for (
node w = 0; w < n; ++w) {
316 if (degree[w] == 1) {
327 t.add_edge_bulk(u, v);
328 t.finish_bulk_add(normalise, check);
332inline graphs::free_tree Prufer_sequence_to_ftree
333(
const std::vector<uint32_t>& S, uint32_t n,
bool normalise =
true,
bool check =
true)
335{
return Prufer_sequence_to_ftree(&S[0], n, normalise, check); }
tree_type
Enumeration of several types of trees.
Definition tree_type.hpp:55
Main namespace of the library.
Definition definitions.hpp:48
std::pair< node, node > edge
Edge type.
Definition definitions.hpp:75
uint32_t node
Node type.
Definition definitions.hpp:51
std::vector< uint32_t > head_vector
A head vector representation of a (usually) rooted tree.
Definition definitions.hpp:114