Aleph-w 7.10.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
64// The same entries, read once they are written: the recurrence reads only
65// those of shorter intervals, which it filled before, so that it reads them
66// with access(), which neither tests whether the entry is there nor may
67// allocate it, as operator [] does
68# define COST_OF(i,j) (cost.access((i)*(n+1) + (j)))
69# define ROOT_OF(i,j) (tree.access((i)*(n+1) + (j)))
70
86static inline void compute_optimal_costs(DynArray<double> & cost, double p[],
87 const size_t n, DynArray<int> & tree)
88{
89 // Precompute prefix sums for O(1) range sum queries
90 // prefix[i] = p[0] + p[1] + ... + p[i-1], prefix[0] = 0
92 prefix[0] = 0.0;
93 for (size_t i = 0; i < n; ++i)
94 prefix[i + 1] = prefix[i] + p[i];
95
96 // sum_p(i, j) = prefix[j] - prefix[i-1] for 1-indexed i,j
97 auto sum_p = [&prefix](size_t i, size_t j) noexcept -> double {
98 // written above, up to n: read without the check of operator []
99 return prefix.access(j) - prefix.access(i - 1);
100 };
101
102 // Base cases: empty intervals have zero cost
103 for (size_t i = 1; i <= n + 1; ++i)
104 COST(i, i - 1) = 0;
105
106 // Base cases: single-element intervals - the only choice is the element itself
107 for (size_t i = 1; i <= n; ++i)
108 {
109 TREE(i, i) = static_cast<int>(i);
110 COST(i, i) = p[i - 1]; // Cost is just the probability (depth = 1)
111 }
112
113 // Fill DP table by increasing interval length
114 // Knuth's optimization: search only in [TREE(i, j-1), TREE(i+1, j)]
115 for (size_t len = 2; len <= n; ++len) // len = j - i + 1
116 {
117 for (size_t i = 1; i + len - 1 <= n; ++i)
118 {
119 const size_t j = i + len - 1;
120
121 // Knuth bounds: root[i,j-1] <= root[i,j] <= root[i+1,j]
122 const size_t lo = static_cast<size_t>(ROOT_OF(i, j - 1));
123 const size_t hi = static_cast<size_t>(ROOT_OF(i + 1, j));
124
125 double min_cost = std::numeric_limits<double>::max();
126 size_t best_root = lo;
127
128 for (size_t r = lo; r <= hi; ++r)
129 {
130 const double c = COST_OF(i, r - 1) + COST_OF(r + 1, j);
131 if (c < min_cost)
132 {
133 min_cost = c;
134 best_root = r;
135 }
136 }
137
138 TREE(i, j) = static_cast<int>(best_root);
139 COST(i, j) = min_cost + sum_p(i, j);
140 }
141 }
142}
143
154template <class Node, typename Key>
155[[nodiscard]] static inline Node * compute_tree(Key keys[], DynArray<int> & tree,
156 const size_t n,
157 const size_t i, const size_t j)
158{
159 if (i > j)
160 return Node::NullPtr;
161
162 const int root_idx = ROOT_OF(i, j);
163 Node * root = new Node(keys[root_idx - 1]);
164 LLINK(root) = compute_tree<Node, Key>(keys, tree, n, i, root_idx - 1);
165 RLINK(root) = compute_tree<Node, Key>(keys, tree, n, root_idx + 1, j);
166 return root;
167}
168
198template <class Node, typename Key>
199[[nodiscard]] Node * build_optimal_tree(Key keys[], double p[], const size_t n)
200{
201 DynArray<int> tree((n + 1) * (n + 1));
202 DynArray<double> cost((n + 1) * (n + 1));
203 compute_optimal_costs(cost, p, n, tree);
204 return compute_tree<Node, Key>(keys, tree, n, 1, n);
205}
206
207
208# undef COST
209# undef TREE
210# undef COST_OF
211# undef ROOT_OF
212} // end namespace Aleph
213# endif // OPBINTREE_H
214
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:4138
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:199
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.
Definition opBinTree.H:155
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:86
#define COST_OF(i, j)
Definition opBinTree.H:68
#define TREE(i, j)
Definition opBinTree.H:62
#define COST(i, j)
Definition opBinTree.H:61
#define ROOT_OF(i, j)
Definition opBinTree.H:69
#define RLINK(i, n)
int keys[]
#define LLINK(i, n)
Gsl_Compatible_Rng r
Lazy and scalable dynamic array implementation.