Optimal Binary Search Tree for DP

Source: Internet
Author: User

As mentioned above, the most typical dynamic planning is to solve the optimization problem (with the optimal sub-structure optimization problem). The optimal binary search tree is a typical optimization problem.

Problem description:

Given the central sequence of an nelement, it can have several binary sorting trees of different shapes in catlan. (For definitions and proofs of catlan numbers, see composite mathematics ):

If we know the search probability of each key, how can we construct an optimal binary search tree with the minimum average search cost (successful search?

Bytes -------------------------------------------------------------------------------------------------------------

To solve the problem by using dynamic programming, we first need to find its optimal sub-structure, and then describe and portray the problem based on this optimal sub-structure to obtain the state transfer equation:

1) optimal sub-structure:

How does one obtain an optimal binary search tree? Reverse Thinking: if there is an optimal binary search tree, and the root is AK, it is easy to get the left and right sides of the AK.

The tree is also the optimal binary query tree (if its subtree is not optimal, it means that the subtree can be further adjusted, then the AK tree is not optimal.

).

2) describe and portray the problem based on the optimal sub-structure nature

Use C [I, j] to represent the cost of the optimal binary search tree from I to J. Then the problem is divided into N ^ 2 subproblems (vertex numbers are counted from 0 ), assume there are n headers.

Point, then our goal is to require C [0, n-1]. (It doesn't matter whether the number starts from 0 or 1. When programming, pay attention to the subscript range ).

Now we can find the state transition equation based on its optimal sub-structure:

How does one obtain an optimal binary search tree from I to J? (That is, how does a C [I, j] come from?) It selects a vertex from the vertex between I and j to do it.

Root, assuming that the selected root vertex is K (I <= k <= J), then obviously there are:

This formula can be directly thought of, without complicated derivation, it is to find a k that can make C [I, j] The minimum cost (the range of this K is from I to J

), But why should we add a probability from I to J? After K is picked out, it is used as the root, and the search length of each vertex increases by 1. Of course, there are also stricter

For more information, see:

3) With the state transition equation, you can draw a matrix to check the initial conditions and the values that each c [I, j] depends on (in the filling order ).

The initial conditions are: C [I, I] = Pi, C [I, I-1] = 0.

TEST How c [I, j] came from, and we can see that it should be entered along the diagonal line.

Note that when k = I or k = J in the state transition equation, C [I, I-1] or C [J + 1, J] is not defined, in programming, only special processing is required.

Row:For this undefined value, take 0 and the other values in the matrix.

Finally, for specific implementation, TMD books always like to draw a table not starting from 0, sometimes even starting from 0 and starting from 1.

It is convenient to fill in the matrix, but it looks like a dog. Generally, for n-scale problems, I open N * n matrices. The following table ranges from 0 to n-1, and some special points are made for those that exceed the boundary.

Just like C [I, I-1] above. Read the table on the book (understand the meaning, the specific implementation of the matrix is different, the subscript control is different ):

It is to draw the table in this way is to solve the C [I, I-1] is not within the defined range, in order to be able to directly from the matrix value to do this.

Bytes -------------------------------------------------------------------------------------------------------------

We have constructed a dynamic planning process for the optimal cost of the optimal binary search tree. We can use the above state transfer equation to fill out all C [I, j].

There is another question, as mentioned in the previous article, how can we not only get the price of C [I, j], but also know the shape of the Binary Tree corresponding to this price?

?

Still construct a matrix A [0... n-1, 0... n-1] to record the dynamic planning process. Each time a k is selected as the root, K is recorded.

A [I, j] = k indicates that the root of the optimal binary search tree from I to J is K. (It also contains the left subtree from I to k-1 and the right subtree from k + 1 to J. Note that we

The given vertex from 0 to N-1 is a central sequence !)

The initial value a [I, I] = I indicates that only the root of the optimal binary search tree is itself. Finally, a matrix A is obtained. It expresses the shape of the Binary Search Tree.

Of course, according to the meaning of a, obtain the optimal binary tree shape from I to J from.

The following algorithms can be used to output the shape of the optimal binary search tree from I to J from a (to output its forward sequence, because the central sequence is known ):

The shape of a binary tree is determined when the pre-order sequence and the middle sequence are known:

It also uses recursion (optimal sub-structure). In fact, the method is similar to that of Floyd in the previous article.

Bytes -------------------------------------------------------------------------------------------------------------

Implementation:

Package section8;


/* Chapter 8 optimal binary search tree for Dynamic Planning (difficult !!!) */

Public class optbst {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Float [] P = {(float) 0.1, (float) 0.2, (float) 0.4, (float) 0.3 };

// If the returned value is the minimum price, check whether the minimum price is correct.
// System. Out. println ("minimum cost of the output optimal binary decision tree: \ n ");
// Float result = optbst (P );
// System. Out. println (result );

// If the returned value is a matrix that expresses the shape of the optimal binary sorting tree, check whether the matrix is correct.
System. Out. println ("output matrix expressing the optimal binary sorting tree shape: \ n ");
Int [] [] r = optbst (P );
For (INT I = 0; I <R. length; I ++)
{
For (Int J = 0; j <R. length; j ++)
System. Out. Print (R [I] [J] + "");
System. Out. println ();
}

}


Public static int [] [] optbst (float [] P ){
// Returns the cost of the optimal binary search tree by accepting the search probability array of the point in the middle sequence (note that the probability in P corresponds to the middle sequence of the point in order)
Int n = P. length; // number of nodes
Float [] [] result = new float [N] [N];

Int [] [] r = new int [N] [N]; // represents a matrix of binary tree shapes.

For (INT I = 0; I <n; I ++)
{
Result [I] [I] = P [I]; // fill in the main diagonal C [I, I] = P [I]
R [I] [I] = I; // R [I] [J] indicates that if only the tree from I to J is constructed, the root is R [I] [J].
}

For (INT d = 1; D <= n-1; D ++) // fill in a total of N-1 diagonal lines
{
For (INT I = 0; I <= n-d-1; I ++) // The relationship between the range of the abscissa and the diagonal number d
{
Int J = I + D; // once the abscissa is determined, the ordinate can be expressed by the abscissa and diagonal number.
Float min = 1000000;

Int root = 0;

For (int K = I; k <= J; k ++)
{
Float C1 = 0, C2 = 0; // C1, C2 indicates result [I, k-1] and result [k + 1, J]
If (k> I)
C1 = Result [I] [k-1];
If (k <j)
C2 = Result [k + 1] [J];

If (C1 + C2 <min)
{
Min = C1 + C2;
Root = K;
}
}

R [I] [J] = root; // R [I] [J] indicates the root of the optimal binary search tree from I to J.

Float sum = 0;
For (int s = I; S <= J; s ++)
// Sum = sum + P [I]; // you mom, I found a small error for a long time.
Sum = sum + P [s];

Result [I] [J] = sum + min;
}
}

// Return result [0] [n-1]; // return C [1, N], minimum cost
Return R; // returns a matrix that expresses the shape of the optimal binary sorting tree.
}

}

The matrix of the optimal cost and the matrix of the expression shapes are obtained together. For details, see the code.

It is easy to see that the time complexity is n ^ 3 (a loop is required for K selection), and the space complexity is n ^ 2.

Running result (returns the matrix R that expresses the shape of the binary search tree ):

The output matrix expresses the optimal binary sorting tree shape:

0 1 2 2
0 1 2 2
0 0 2 2
0 0 0 3

Bytes -------------------------------------------------------------------------------------------------------------

Thoughts:

1. Analyze the time complexity and space complexity of the optimal binary search tree. (See analysis)

2. Write a pseudo code of Linear Time to generate the optimal binary search tree from the root table (matrix A) (See analysis)

3. Positive and false judgment: the root of an optimal binary search tree always contains the key with the highest probability (wrong, it is easy to give a back example. If this sentence is correct, dynamic planning is required.

Then we can use a simpler method to construct the optimal binary sorting tree-simply select the maximum value based on the probability)

4. Extend the optimal binary search tree (with the lowest cost for successful search) to the situations where the total cost of successful and unsuccessful search is the smallest (easy, changing the meaning of C [I, j ].

The probability of power)

5. proof:

Important: in fact, the catlan number is defined as this. (if there is a chance later, I will go into the catlan number.) The solution of this recursive relationship is given by the catlan number

(I forgot it now. For more information, see "combined mathematics" Lu kaicheng Peking University Press ).

If strict proof is not required, it is easy to write the above recursive formula according to the meaning of B (n. (Think about it)

Bytes -------------------------------------------------------------------------------------------------------------

Last question: matrix concatenation. See the next article as a solution report.

Bytes ------------------------------------------------------------------------------------------------------------

Summary:

1) how to analyze a problem and try to solve it with dynamic planning? (First, find the optimal sub-structure to describe and describe the problem from the most sub-structure.)

2)Reverse Thinking to find the state transition equation!

3) compared with the previous dynamic planning algorithms, the matrix sequence of the optimal binary search tree is slightly more complex (along the diagonal line). It may be difficult to see the recursive mathematical form directly.

Come out, draw a matrix, and try out how a C [I, j] is obtained.

4) catlan numbers are very important. Many problems are related to catlan numbers (see what is professional ?), Exercise 5 provides a good proof of guidance.

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.