简体   繁体   中英

Optimal Binary Search Tree for Successful and Unsuccessful Search

I am studying Dynamic Programming Algorithms for optimizing Binary Search Tree in C++ language. I have built my own program but I do not know whether my program finds out the correct answer or not. I have made an attempt to find sample code on the internet but I just found sample one for Successful search, therefore, I do not know the correct answer. More than that, I think I have a mistake in the way I code but I am not able to point it out.

If you do not understand the problem, you can read here Optimal Binary Search Tree

Brief description: This is a problem that builds an optimal Binary search Tree. The problem is given two sets to record the probability of found and unfound objects in a binary search tree. From that given data, I need to calculate the minimum cost of searching an arbitrary object in the binary search tree

Below is my source code:

double OptimalBinarySearchTree(double Found[], double Unfound[], int n)
{
    double Cost[n + 2][n + 1], Freq[n + 2][n + 1];
    int i, j, k, l;
    double temp = 0;
    memset(Cost, 0, sizeof(Cost));
    memset(Freq, 0, sizeof(Freq));
    for (i = 1; i <= n; i++)
    {
        Cost[i][i - 1] = Unfound[i - 1];
        Freq[i][i - 1] = Unfound[i - 1];
    }
    for (l = 1; l <= n; l++)
    {
        for (i = 1; i <= n - l + 1; i++)
        {
            j = l + i - 1;
            Freq[i][j] = Freq[i][j - 1] + Found[j] + Unfound[j];
            Cost[i][j] = INT32_MAX;
            for (k = i; k <= j; k++)
            {
                temp = 0;
                if (k > i)
                    temp += Cost[i][k - 1];
                if (k < j)
                    temp += Cost[k + 1][j];
                temp += Freq[i][j];
                if (temp < Cost[i][j])
                    Cost[i][j] = temp;
            }
        }
    }
    return Cost[1][n];
}

For example, when I run my program with

    double Found[7] = {0, 0.15, 0.10, 0.05, 0.10, 0.20};
    double Unfound[7] = {0.05, 0.10, 0.05, 0.05, 0.05, 0.10};

My program returns the value is 2.45 but maybe the "real" answer is 2.85. I do not know where I get wrong with my algorithms. I really need someone to check the correctness of my program or algorithm. I really appreciate it if you can point it out for me.

From what I can see the 2 algorithms differ when calculating the cost of the new candidate sub-root E_{i,j} = E_{i,r-1} + E_{r+1,j} + W_{i,j} Your code is not adding the left sub-tree value when k = 1 and not adding the right sub-tree value when k=j.

        temp = 0;
        if (k > i)
            temp += Cost[i][k - 1];
        if (k < j)
            temp += Cost[k + 1][j];
        temp += Freq[i][j];
        if (temp < Cost[i][j])
            Cost[i][j] = temp;

Is there any reason why you have a specific implementation of the recurence for these 2 cases? If no, which sounds to be the case in the other implementation of the DP algorithm, or in the link you provided, the recurrence should be:

        temp = Cost[i][k - 1] + Cost[k + 1][j] + Freq[i][j];
        if (temp < Cost[i][j])
            Cost[i][j] = temp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM