"Introduction to Algorithms" dynamic programming-optimal binary search tree

Source: Internet
Author: User

Case

If we are now designing an English translation program to translate English into Chinese, obviously we need to know the Chinese meaning of each word. We can set up a binary search tree to realize the association between English and Chinese. In order to translate more quickly, we can use the AVL tree or red-black tree to make the time complexity of each query θ (LGN), in fact, for the dictionary translator to do so there is a problem, such as the word "the" is often used, but it is very likely to exist in a very far away from the roots of the location, and " Machicolation "This less commonly used word is likely to exist very close to the root of the location, which causes the query frequency of the word needs longer query time, and the query frequency is very low word query time is very short. This is obviously not in line with our expectations, we hope that the high-frequency words in less time to query, low-frequency words with a relatively long time to query, that is, high-frequency words near the root, and low-frequency words away from the roots. At this point, we need an optimal binary search tree to solve this problem.

The best binary search tree and the optimal binary search tree

Given an ordered sequence of group-length n keywords k=< k1, k2, K3 KN >
and the probability of the occurrence of the corresponding keyword p=< p1, p2, p3 PN >
Since the keyword we are retrieving may not be in the tree, we need to n+1 a pseudo-keyword
Given a set of pseudo-keywords with a length of n+1 d=< D0, D1, D2 DN >
where D0 represents a value less than any one keyword, the DN represents a value greater than any one of the keywords when I! = 0 && I! = N, di
Represents all sets of values that are greater than Ki and less than ki+1 that do not exist in the sequence K (regardless of the value equality) (for example, D5 represents all collections that are greater than K5 and less than K6 that do not exist in the sequence K)

The binary search tree is an optimal binary search tree if the following conditions are met

    • is a binary search tree (nonsense)
    • The following conditions are met
Attention
    1. The best binary search tree is not necessarily a balanced binary search tree
    2. The root node of the optimal binary search tree is not necessarily the most frequent node

Example


How to construct an optimal binary search tree exhaustive method

? If you want to be a one-off to cite all the patterns and then choose the complexity of space-time, a node with n two-tree shape is very obvious if the poor lift is very not worth it.

Whether dynamic planning can be used

? Since the poor lifting method is not good then is the dynamic planning applicable? Two conditions are required to use a dynamic plan

    • has optimized sub-structure
    • Have overlapping sub-problems

? Here you can directly give the optimal sub-structure of the best binary search tree problem: For an optimal binary search tree T, any of its subtree T1 must be an optimal binary search tree. Here can be proved by contradiction, T is an optimal binary search tree, if there is a subtree T1 is not the optimal binary search tree, then replace this subtree with the same keyword but the shape of the other sub-tree T2, it is likely to make the whole tree T's expected search cost is lower, This does not satisfy the premise that T is an optimal binary search tree.

According to the above analysis, we can know that the optimal binary search tree problem is overlapping sub-problem, because if we want to construct an optimal binary search tree, we must first construct its sub-tree, the sub-tree to construct its sub-tree, so go on, there are overlapping sub-problems.

? So the optimal binary search tree problem can be solved by using dynamic programming.

Problem analysis

? It is worth noting that if i=j-1, this subtree does not actually contain any actual keywords, but it will contain the keyword di-1 (all sets of numbers less than Ki that do not exist in sequence K). We have no obvious rules to follow, so we can only look for a key KR (i <= R <= J), and when KR is the root of the subtree, the search cost is minimal and can only be tested one by one.

    • We use cost[i][j] to denote the inclusion of the keyword ki The search cost of the subtree of KJ
    • We use w[i][j] to denote the inclusion of the keyword ki KJ and pseudo-keyword di-1 DJ's subtree of each keyword and pseudo-keyword probabilities and
Recursive type

Sub-Problem solving order (Matrix fill mode)

? sub-Problem matrix

? It should be populated diagonally, because each time a sub-problem is solved on a diagonal, the result of the previous diagonal line is required

This article covers two matrices and two matrices are filled in the same way

C + + code
#include <iostream> #include <string> #include <algorithm> #include <map>using namespace std;    Const double INF = 99999;void optimal_bst (double* p, double* q, int n); int main () {int n;    CIN >> N;    double* p = new Double[n + 1];    double* q = new Double[n];    for (int i = 1; I <= n; i++) cin >> P[i];    for (int i = 0; I <= N; i++) cin >> Q[i];    Optimal_bst (P, q, N);    Delete p, q; System ("Pause");}    void Optimal_bst (double* p, double* q, int n) {double** e = new Double*[n + 2];    double** w = new Double*[n + 2];    Double T = 0.0;    int** root = new Int*[n + 2];    int j = 0;        for (int i = 0; I <= n + 1; i++) {E[i] = new Double[n + 1];        W[i] = new Double[n + 1];        Root[i] = new Int[n + 1];        Matrix 0 memset (E[i], 0, sizeof (double) * (n + 1));        memset (W[i], 0, sizeof (double) * (n + 1));    memset (Root[i], 0, sizeof (int) * (n + 1)); }//fills the first diagonal for (int i =1; I <= n + 1;        i++) {E[i][i-1] = q[i-1];    W[I][I-1] = q[i-1]; } for (int k = 1, k <= N; k++) {for (int i = 1; I <= n-k + 1; i++) {j = i + k-1            ;            E[I][J] = INF;            W[I][J] = W[i][j-1] + p[j] + q[j];                for (int r = i; r <= J; r++) {t = E[i][r-1] + e[r + 1][j] + w[i][j];                    if (E[i][j]-T > 0.0001) {e[i][j] = t;                ROOT[I][J] = r;    }}}} cout << Endl << Endl; for (int i = 1, i <= n + 1; i++) {for (int j = 0; J <= N; j + +) cout << E[i][j] <<        ' \ t ';    cout << Endl << endl << Endl;    } delete e, w; Delete root;}

Introduction to algorithms dynamic programming-optimal binary search tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.