Bellman algorithm

from Wikipedia, the free encyclopedia

The Bellman algorithm constructs an optimal binary search tree from a given key list and a corresponding search probability . The algorithm is based on the sentence found by Richard Bellman in 1957 about optimal mean search times in binary search trees and uses the dynamic programming method .

algorithm

input

Search keys that are ordered in a sequence . In addition, the search probability is given for each key . For each denotes the probability that a non-existent key , with for or for , is searched for.

Since and are probabilities, the sum of all and 1 must be :

output

The minimum expected search time in an optimal binary search tree for the key set and the optimal search tree under which the minimum expected search time is achieved.

However, if there are geometrically falling probabilities, then the search time for the associated very rare keys cannot be logarithmically restricted.

Calculation of the search time

The search time for a key search or the search costs for a key search is the number of nodes visited on a path from the root to the key node in a binary search tree. So if a key a depth of has in the tree, then its search costs .

In order to model the search time for non-existent keys, each leaf is given two child nodes and . If a -sheet is reached during the search , then the node is not contained in the binary search tree.

For a given search tree , the expected search time can be calculated:

Recursive computation

The Bellman algorithm calculates the expected search time under an optimal binary search tree recursively on the sequence of search keys. The algorithm is specified by means of matrix recurrences.

Initialization:

Recursion:

In each cell, there is the minimum search duration under an optimal search tree for the partial sequence of the search key sequence , the sum of all search probabilities denoting the keys in the tree for the partial sequence. So the minimum search time for the entire sequence is stored in the cell .

In the recursion, each choice for choosing as the root of the tree corresponds to the subsequence . The creation of the root increases the depth of each node in this tree by 1. So the expected search time in this tree must be increased by.

is defined as

and can be calculated efficiently with a matrix recurrence.

Backtracking

In order to construct an optimal search tree with the minimum expected search duration, the calculation of the optimal value must be traced back using backtracking . Alternatively, an additional auxiliary matrix can be used in an implementation of the algorithm, which is filled with the optimal values ​​of for each during the calculation of and is evaluated after the calculation of has been completed.

complexity

The runtime of the calculation of the matrix for the values ​​is in . The matrix contains entries and each entry must be optimized using elements. So the runtime complexity of the algorithm is in and the memory requirement in .

The iteration over in the recursion can be further restricted so that the total runtime of all iterations is in . So the total runtime of the algorithm modified in this way is then in .

literature

swell

  1. ^ Donald Ervin Knuth : The Art of Computer Programming 3. Sorting and Searching . 2nd Edition. Addison-Wesley Longman, Amsterdam 1998, ISBN 0-201-89685-0 , pp. 436-442 .