ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OptimalSearchTree.h File Reference

The optimal binary search tree: the keys \( 0 , \ldots , n - 1 \) are searched with the weights \( p_i \) (frequencies or probabilities), and the gaps between them, the unsuccessful searches, with the weights \( q_0 , \ldots , q_n \); the tree of least weighted path length \( \sum_i p_i ( d_i + 1 ) + \sum_j q_j d'_j \), \( d_i \) the depth of the key \( i \) and \( d'_j \) that of the external node \( j \), the root at depth 0. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "ADS/optimization/impl/OptimalSearchTree.tpp"

Go to the source code of this file.

Classes

struct  ADS::OptimalSearchTree< T >
 an optimal binary search tree on the keys \( 0 , \ldots , n - 1 \): its cost, its root and the children of each key, \( -1 \) for none (and for the root of the empty tree) More...

Namespaces

namespace  ADS

Functions

template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree (const std::vector< T > &p, const std::vector< T > &q)
 the optimal binary search tree of the keys of weights \( p \) and of the gaps of weights \( q \)
template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree (const std::vector< T > &p)
 the tree of optimalSearchTree() above, with the gaps of weight 0

Detailed description

The optimal binary search tree: the keys \( 0 , \ldots , n - 1 \) are searched with the weights \( p_i \) (frequencies or probabilities), and the gaps between them, the unsuccessful searches, with the weights \( q_0 , \ldots , q_n \); the tree of least weighted path length \( \sum_i p_i ( d_i + 1 ) + \sum_j q_j d'_j \), \( d_i \) the depth of the key \( i \) and \( d'_j \) that of the external node \( j \), the root at depth 0.

With \( W( i , j ) = \sum_{i \le l < j} p_l + \sum_{i \le l \le j} q_l \) the cost of the keys \( i , \ldots , j - 1 \) is \( C[ i ][ i ] = 0 \) and

\[ C[ i ][ j ] = W( i , j ) + \min_{i \le r < j} C[ i ][ r ] + C[ r + 1 ][ j ] , \]

\( O( n^3 ) \) time as written. Knuth proved that a best root \( R[ i ][ j ] \) can be looked for between \( R[ i ][ j - 1 ] \) and \( R[ i + 1 ][ j ] \), which makes the total time \( O( n^2 ) \) by a telescoping sum, with \( O( n^2 ) \) space; Yao derived it from the quadrangle inequality of \( W \), the largest best root being monotone. optimalSearchTree() does so.

In the literature: Hu and Tucker, and Garsia and Wachs, build in \( O( n \log n ) \) the optimal alphabetic tree, where only the \( q_j \) count (the keys in the leaves); a tree within a constant of the optimum comes in \( O( n ) \) from the weight-balancing rule of Mehlhorn; the splay trees are within a constant of the optimal static tree without knowing the weights [see SplayTree.h].

The contracts [see Contract.h]: the weights are not negative and \( |q| = |p| + 1 \) when \( q \) is given; the tree has \( 0 , \ldots , n - 1 \) as its symmetric visit and the cost found (checked at level 3). The functions are defined in OptimalSearchTree.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13 and the Esercizi Programmazione dinamica, exercise B4 of 11/01/2012, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
D. E. Knuth, Optimum binary search trees, Acta Informatica 1(1), 14-25, 1971, doi:10.1007/BF00264289
F. F. Yao, Efficient dynamic programming using quadrangle inequalities, Proc. 12th ACM Symposium on Theory of Computing, 429-435, 1980, doi:10.1145/800141.804691
T. C. Hu, A. C. Tucker, Optimal computer search trees and variable-length alphabetical codes, SIAM Journal on Applied Mathematics 21(4), 514-532, 1971, doi:10.1137/0121057
K. Mehlhorn, Nearly optimal binary search trees, Acta Informatica 5(4), 287-295, 1975, doi:10.1007/BF00264563
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa