61# define COST(i,j) (cost[(i)*(n+1) + (j)])
62# define TREE(i,j) (tree[(i)*(n+1) + (j)])
86 for (
size_t i = 0; i < n; ++i)
90 auto sum_p = [&
prefix](
size_t i,
size_t j)
noexcept ->
double {
95 for (
size_t i = 1; i <= n + 1; ++i)
99 for (
size_t i = 1; i <= n; ++i)
101 TREE(i, i) =
static_cast<int>(i);
102 COST(i, i) = p[i - 1];
107 for (
size_t len = 2; len <= n; ++len)
109 for (
size_t i = 1; i + len - 1 <= n; ++i)
111 const size_t j = i + len - 1;
114 const size_t lo =
static_cast<size_t>(
TREE(i, j - 1));
115 const size_t hi =
static_cast<size_t>(
TREE(i + 1, j));
117 double min_cost = std::numeric_limits<double>::max();
120 for (
size_t r =
lo; r <=
hi; ++r)
122 const double c =
COST(i, r - 1) +
COST(r + 1, j);
146template <
class Node,
typename Key>
149 const size_t i,
const size_t j)
152 return Node::NullPtr;
190template <
class Node,
typename Key>
WeightedDigraph::Node Node
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Node * build_optimal_tree(Key keys[], double p[], const size_t n)
Build an optimal binary search tree based on access probabilities.
Main namespace for Aleph-w library functions.
static Node * compute_tree(Key keys[], DynArray< int > &tree, const size_t n, const size_t i, const size_t j)
Recursively construct the optimal BST from the tree matrix.
static void prefix(Node *root, DynList< Node * > &acc)
static void compute_optimal_costs(DynArray< double > &cost, double p[], const size_t n, DynArray< int > &tree)
Compute optimal costs and tree structure using dynamic programming.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Lazy and scalable dynamic array implementation.