Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
opBinTree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33# ifndef OPBINTREE_H
34# define OPBINTREE_H
35
36# include <limits>
37# include <tpl_dynArray.H>
38
39namespace Aleph {
40
60// Internal macros for 2D array access using 1D storage
61# define COST(i,j) (cost[(i)*(n+1) + (j)])
62# define TREE(i,j) (tree[(i)*(n+1) + (j)])
63
79static inline void compute_optimal_costs(DynArray<double> & cost, double p[],
80 const size_t n, DynArray<int> & tree)
81{
82 // Precompute prefix sums for O(1) range sum queries
83 // prefix[i] = p[0] + p[1] + ... + p[i-1], prefix[0] = 0
85 prefix[0] = 0.0;
86 for (size_t i = 0; i < n; ++i)
87 prefix[i + 1] = prefix[i] + p[i];
88
89 // sum_p(i, j) = prefix[j] - prefix[i-1] for 1-indexed i,j
90 auto sum_p = [&prefix](size_t i, size_t j) noexcept -> double {
91 return prefix[j] - prefix[i - 1];
92 };
93
94 // Base cases: empty intervals have zero cost
95 for (size_t i = 1; i <= n + 1; ++i)
96 COST(i, i - 1) = 0;
97
98 // Base cases: single-element intervals - the only choice is the element itself
99 for (size_t i = 1; i <= n; ++i)
100 {
101 TREE(i, i) = static_cast<int>(i);
102 COST(i, i) = p[i - 1]; // Cost is just the probability (depth = 1)
103 }
104
105 // Fill DP table by increasing interval length
106 // Knuth's optimization: search only in [TREE(i, j-1), TREE(i+1, j)]
107 for (size_t len = 2; len <= n; ++len) // len = j - i + 1
108 {
109 for (size_t i = 1; i + len - 1 <= n; ++i)
110 {
111 const size_t j = i + len - 1;
112
113 // Knuth bounds: root[i,j-1] <= root[i,j] <= root[i+1,j]
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));
116
117 double min_cost = std::numeric_limits<double>::max();
118 size_t best_root = lo;
119
120 for (size_t r = lo; r <= hi; ++r)
121 {
122 const double c = COST(i, r - 1) + COST(r + 1, j);
123 if (c < min_cost)
124 {
125 min_cost = c;
126 best_root = r;
127 }
128 }
129
130 TREE(i, j) = static_cast<int>(best_root);
131 COST(i, j) = min_cost + sum_p(i, j);
132 }
133 }
134}
135
146template <class Node, typename Key>
147[[nodiscard]] static inline Node * compute_tree(Key keys[], DynArray<int> & tree,
148 const size_t n,
149 const size_t i, const size_t j)
150{
151 if (i > j)
152 return Node::NullPtr;
153
154 const int root_idx = TREE(i, j);
155 Node * root = new Node(keys[root_idx - 1]);
156 LLINK(root) = compute_tree<Node, Key>(keys, tree, n, i, root_idx - 1);
157 RLINK(root) = compute_tree<Node, Key>(keys, tree, n, root_idx + 1, j);
158 return root;
159}
160
190template <class Node, typename Key>
191[[nodiscard]] Node * build_optimal_tree(Key keys[], double p[], const size_t n)
192{
193 DynArray<int> tree((n + 1) * (n + 1));
194 DynArray<double> cost((n + 1) * (n + 1));
195 compute_optimal_costs(cost, p, n, tree);
196 return compute_tree<Node, Key>(keys, tree, n, 1, n);
197}
198
199
200# undef COST
201# undef TREE
202} // end namespace Aleph
203# endif // OPBINTREE_H
204
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)
Definition gmpfrxx.h:4060
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.
Definition opBinTree.H:191
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
Definition opBinTree.H:147
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.
Definition opBinTree.H:79
DynList< T > maps(const C &c, Op op)
Classic map operation.
#define TREE(i, j)
Definition opBinTree.H:62
#define COST(i, j)
Definition opBinTree.H:61
Lazy and scalable dynamic array implementation.