47#include <lal/linear_arrangement.hpp>
48#include <lal/detail/graphs/traversal.hpp>
49#include <lal/detail/graphs/size_subtrees.hpp>
50#include <lal/detail/properties/tree_centroid.hpp>
51#include <lal/detail/sorting/counting_sort.hpp>
52#include <lal/detail/array.hpp>
53#include <lal/detail/macros/basic_convert.hpp>
54#include <lal/detail/linarr/D/Dopt_utils.hpp>
59namespace unconstrained {
83template <
char anchored>
85(
const uint64_t n,
const ordering& ord, uint64_t& s_0, uint64_t& s_1)
96 const uint64_t k = ord.
size() - 1;
98 uint64_t n_0 = ord[0].size;
106 if (max_p == 0) {
return 0; }
109 for (uint64_t i = 0; i <= 2*max_p; ++i) { sum += ord[i].size; }
111 uint64_t n_star = n - sum;
112 uint64_t tricky_formula = (n_0 + 2)/2 + (n_star + 2)/2;
115 uint64_t n_p = ord[2*max_p].size;
116 while (max_p > 0 and n_p <= tricky_formula) {
117 sum -= ord[2*max_p].size + ord[2*max_p - 1].size;
121 tricky_formula = (n_0 + 2)/2 + (n_star + 2)/2;
125 n_p = (max_p > 0 ? ord[2*max_p].size : n_p);
128 s_0 = max_p*(n_star + 1 + n_0);
130 for (uint64_t i = 1; i < max_p; ++i) {
131 s_0 += i*(ord[2*i + 1].size + ord[2*i + 2].size);
139 if (max_p == 0) {
return 0; }
142 for (uint64_t i = 0; i <= 2*max_p - 1; ++i) { sum += ord[i].size; }
143 uint64_t n_star = n - sum;
144 uint64_t tricky_formula = (n_0 + 2)/2 + (n_star + 2)/2;
145 uint64_t n_p = ord[2*max_p - 1].size;
146 while (max_p > 0 and n_p <= tricky_formula) {
147 sum -= ord[2*max_p - 1].size;
148 sum -= ord[2*max_p - 2].size;
151 tricky_formula = (n_0 + 2)/2 + (n_star + 2)/2;
155 n_p = (max_p > 0 ? ord[2*max_p - 1].size : n_p);
159 s_1 = max_p*(n_star + 1 + n_0) - 1;
160 for (uint64_t i = 1; i < max_p; ++i) {
161 s_1 += i*(ord[2*i].size + ord[2*i + 1].size);
183template <
char alpha,
bool make_arrangement>
187 const node root_or_anchor,
202 const uint64_t size_tree = t.get_num_nodes_component(root_or_anchor);
205 assert(size_tree > 0);
209 if (size_tree == 1) {
211 if constexpr (make_arrangement) {
212 mla.assign(root_or_anchor, start);
234 const neighbourhood& v_star_neighs = t.get_neighbors(v_star);
235 for (std::size_t i = 0; i < v_star_neighs.size(); ++i) {
237 const node ui = v_star_neighs[i];
239 const uint64_t s_ui = s[ui];
242 M = std::max(M, s_ui);
249 [](
const node_size& p) {
return p.size; }
253 const node v_0 = ord[0].v;
254 const uint64_t n_0 = ord[0].
size;
257 t.remove_edge(v_star, v_0,
false,
false);
265 (t, v_star, start, end - n_0, mla, c2);
268 (t, v_0, end - n_0 + 1, end, mla, c1);
273 (t, v_0, start, start + n_0 - 1, mla, c1);
275 constexpr auto new_alpha =
281 (t, v_star, start + n_0, end, mla, c2);
286 else { cost = c1 + c2 + size_tree - n_0; }
289 t.add_edge(v_star, v_0,
false,
false);
295 constexpr auto anchored =
302 const uint64_t p_alpha =
309 std::vector<edge> edges(2*p_alpha - anchored);
312 for (uint64_t i = 1; i <= 2*p_alpha - anchored; ++i) {
313 edges[i - 1] = {v_star, ord[i].v};
315 t.remove_edges(edges,
false,
false);
319 for(uint64_t i = 1; i <= 2*p_alpha - anchored; ++i) {
322 const node r = ord[i].v;
323 const uint64_t n_i = ord[i].
size;
330 (t, r, start, start + n_i - 1, mla_B, c_aux);
337 (t, r, end - n_i + 1, end, mla_B, c_aux);
347 (t, v_star, start, end, mla_B, c_aux);
352 t.add_edges(edges,
false,
false);
367 if constexpr (make_arrangement) {
369 mla = std::move(mla_B);
388template <
bool make_arrangement>
389[[nodiscard]] std::conditional_t<
391 std::pair<uint64_t, linear_arrangement>,
407 (T, 0, 0, t.get_num_nodes() - 1, arrangement, Dmin);
409 if constexpr (make_arrangement) {
410 return {Dmin, std::move(arrangement)};
Free tree graph class.
Definition free_tree.hpp:60
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
void calculate_mla(graphs::free_tree &t, const node root_or_anchor, position start, position end, linear_arrangement &mla, uint64_t &cost) noexcept
Calculate a minimum linear arrangment using Shiloach's algorithm.
Definition Unconstrained_YS.hpp:185
uint64_t calculate_p_alpha(const uint64_t n, const ordering &ord, uint64_t &s_0, uint64_t &s_1) noexcept
Calculate .
Definition Unconstrained_YS.hpp:85
array< node_size > ordering
Typedef for a useful type.
Definition Unconstrained_YS.hpp:70
std::conditional_t< make_arrangement, std::pair< uint64_t, linear_arrangement >, uint64_t > YossiShiloach(const graphs::free_tree &t) noexcept
Calculates a minimum linear arrangment using Shiloach's algorithm.
Definition Unconstrained_YS.hpp:394
static constexpr char NO_ANCHOR
The tree is not anchored.
Definition Dopt_utils.hpp:101
static constexpr char ANCHOR
The tree is anchored.
Definition Dopt_utils.hpp:103
static constexpr char RIGHT_ANCHOR
The tree is right-anchored.
Definition Dopt_utils.hpp:99
static constexpr char LEFT_ANCHOR
The tree is left-anchored.
Definition Dopt_utils.hpp:97
@ non_increasing
Non-increasing sort.
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const Callable &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition counting_sort.hpp:152
void get_size_subtrees(const tree_t &t, const node u, const node v, uint64_t *const sizes) noexcept
Calculate the size of every subtree of the tree t.
Definition size_subtrees.hpp:74
constexpr uint64_t alpha(const int64_t n, const int64_t d1, const int64_t d2) noexcept
Amount of crossings pairs of edges of given lengths.
Definition predict.hpp:72
std::pair< node, node > retrieve_centroid(const tree_t &t, const node x=0) noexcept
Calculate the centroid of the connected component that has node x.
Definition tree_centroid.hpp:416
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t position
Node's position type.
Definition basic_types.hpp:53
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51
std::vector< node > neighbourhood
List of nodes.
Definition basic_types.hpp:64
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
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
Struct used in many algorithms to sort vertices according to some integer value.
Definition pairs_utils.hpp:54