简体   繁体   English

成功和不成功搜索的最佳二叉搜索树

[英]Optimal Binary Search Tree for Successful and Unsuccessful Search

I am studying Dynamic Programming Algorithms for optimizing Binary Search Tree in C++ language.我正在研究用 C++ 语言优化二叉搜索树的动态编程算法。 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如果你不明白这个问题,你可以在这里阅读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根据给定的数据,我需要计算在二叉搜索树中搜索任意 object 的最小成本

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.我的程序返回的值是 2.45,但也许“真正的”答案是 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.据我所知,计算新候选子根 E_{i,j} = E_{i,r-1} + E_{r+1,j} + W_{i,j 的成本时,两种算法有所不同您的代码在 k = 1 时不添加左子树值,并且在 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:如果不是,在 DP 算法的其他实现或您提供的链接中听起来就是这种情况,重复出现应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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