c - 最优二叉搜索树的重构动态方法

标签 c algorithm dynamic-programming

总的来说,我对动态规划和 CS 的概念还很陌生。我通过阅读在线发布的讲座、观看视频和解决发布在 GeeksforGeeks 和 Hacker Rank 等网站上的问题来自学。

问题

给定输入

3 25 30 5

where 3 =  #of keys

25 = frequency of key 1

30 = frequency of key 2

5  = frequency of key 3

如果每个键都以优化的方式排列,我将打印出最小的成本。这是一个最佳二叉搜索树问题,我在 Geeks for Geeks 上找到了一个解决方案,它做了类似的事情。

#include <stdio.h>
#include <limits.h>

// A utility function to get sum of array elements freq[i] to freq[j]
int sum(int freq[], int i, int j);

/* A Dynamic Programming based function that calculates minimum cost of
   a Binary Search Tree. */
int optimalSearchTree(int keys[], int freq[], int n)
{
    /* Create an auxiliary 2D matrix to store results of subproblems */
    int cost[n][n];

    /* cost[i][j] = Optimal cost of binary search tree that can be
       formed from keys[i] to keys[j].
       cost[0][n-1] will store the resultant cost */

    // For a single key, cost is equal to frequency of the key
    for (int i = 0; i < n; i++)
        cost[i][i] = freq[i];

    // Now we need to consider chains of length 2, 3, ... .
    // L is chain length.
    for (int L=2; L<=n; L++)
    {
        // i is row number in cost[][]
        for (int i=0; i<=n-L+1; i++)
        {
            // Get column number j from row number i and chain length L
            int j = i+L-1;
            cost[i][j] = INT_MAX;

            // Try making all keys in interval keys[i..j] as root
            for (int r=i; r<=j; r++)
            {
               // c = cost when keys[r] becomes root of this subtree
               int c = ((r > i)? cost[i][r-1]:0) + 
                       ((r < j)? cost[r+1][j]:0) + 
                       sum(freq, i, j);
               if (c < cost[i][j])
                  cost[i][j] = c;
            }
        }
    }
    return cost[0][n-1];
}

// A utility function to get sum of array elements freq[i] to freq[j]
int sum(int freq[], int i, int j)
{
    int s = 0;
    for (int k = i; k <=j; k++)
       s += freq[k];
    return s;
}

// Driver program to test above functions
int main()
{
    int keys[] = {0,1,2};
    int freq[] = {34, 8, 50};
    int n = sizeof(keys)/sizeof(keys[0]);
    printf("Cost of Optimal BST is %d ", optimalSearchTree(keys, freq, n));
    return 0;
}

然而,在这个解决方案中,他们也接受了“键”的输入,但似乎他们对最终答案没有影响,因为他们不应该。只有搜索每个键的次数的频率很重要。

为了简单起见和理解这种动态方法,我想知道如何修改这个解决方案,以便它以上面显示的格式接收输入并打印结果。

最佳答案

您提供的函数确实有一个 keys 参数,但它没有使用它。您可以将其完全删除。


编辑:特别是,由于函数 optimalSearchTree() 根本不使用其 keys 参数,删除该参数只需要更改函数签名(...

int optimalSearchTree(int freq[], int n)

...) 和该函数的一次调用。但是,由于您不需要此特定练习的 key ,因此您也可以将它们从主程序中完全删除,以便:

int main()
{
    int freq[] = {25, 30, 5};
    int n = sizeof(freq)/sizeof(freq[0]);
    printf("Cost of Optimal BST is %d ", optimalSearchTree(freq, n));
    return 0;
}

(将您指定的频率值替换为原始代码中的频率值)


然而,该函数确实假设频率是按增加键的顺序给出的。它至少需要相对键顺序才能完成其工作,否则您无法构建搜索 树。如果您对键值未知的想法感到不安,您可以将代码解释为使用 freq[] 数组中的索引作为键值的别名。这是有效的,因为上述假设的结果是 x -> keys[x] 是 1:1,order-preserving 映射从整数 0 ... n - 1 到任何实际的键。

如果该函数不能假定频率最初是按键按递增顺序给出的,那么它可以先使用键将频率按该顺序排序,然后像现在一样继续。

关于c - 最优二叉搜索树的重构动态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28988833/

相关文章:

java - 在寻找最长公共(public)子串 [DP] 时如何获得空间复杂度 O(n)?

c - 查找一个单词并替换文本文件中的整行

c - 相同的字符串。一个人行为不端

将十进制文字转换为 C 中 putchar 的 ASCII 等价物

arrays - 动态规划 : Number of moves to reduce the array to 1 number

.net - 具有动态模型的 Dapper ORM - 如何不返回任何字段而不是 'Field' = NULL?

c - 警告 : passing argument 2 of 'calcFx' makes integer from pointer without a cast

php - 重构循环?

arrays - 最小化Matlab中数组列的总和

c++ - 比较迭代器与 ptrdiff