简体   繁体   English

霍夫曼编码基于贪婪方法或动态编程

[英]Huffman coding is based on what Greedy Approach or Dynamic Programming

我们可以通过动态编程来解决霍夫曼编码问题,有没有算法

Huffman Coding works by creating a binary tree of nodes. 霍夫曼编码通过创建节点的二叉树来工作。 These can be stored in a regular array, the size of which depends on the number of symbols, n. 这些可以存储在常规数组中,其大小取决于符号数n。 Huffman Coding can be implemented in O(n logn) time by using the Greedy Algorithm approach. 通过使用Greedy Algorithm方法,可以在O(n logn)时间内实现霍夫曼编码。 Huffman Coding is not suitable for a Dynamic Programming solution as the problem does not contain overlapping sub problems. 霍夫曼编码不适用于动态编程解决方案,因为该问题不包含重叠的子问题。

As per my knowledge about algorithms, I beleive huffman coding is based on Greedy approach. 根据我对算法的了解,我相信霍夫曼编码是基于贪婪的方法。 As we do in Activity selection problem. 正如我们在Activity选择问题中那样做。 Task scheduling and knapsack problem are another examples of it. 任务调度和背包问题是它的另一个例子。

[ Update ]: I think the solution I mentioned below generates an optimal prefix code, but not necessarily same as Huffman code. [ 更新 ]:我认为我在下面提到的解决方案会生成最佳前缀代码,但不一定与霍夫曼代码相同。 Huffman code, by definition, is generated by taking the greedy approach. 根据定义,霍夫曼代码是通过贪婪的方法生成的。 While it is optimal, it is not the only solution. 虽然它是最佳的,但它不是唯一的解决方案。 (For example, once the Huffman tree is generated, you could interchange the leaves in the same level to give them different codes while still being optimal) (例如,一旦生成了霍夫曼树,您就可以在同一级别中交换叶子,以便在保持最佳状态时为它们提供不同的代码)


I think this can be solved using Dynamic Programming, although it won't be that efficient. 我认为这可以使用动态编程来解决,尽管效率不高。 The approach here is very similar to finding the Optimal Binary Search Tree, since as you go one level down you add one more bit to the leaves. 这里的方法非常类似于找到最佳二进制搜索树,因为当你向下一级时,你又向叶子添加一个位。

在此输入图像描述

Here is the link for the code to calculate the minimum number of total bits. 以下是代码计算最小总位数的链接。 Of course this is exponential, but if you use DP it can be solved in O(n^2) time. 当然这是指数级的,但如果你使用DP,它可以在O(n ^ 2)时间内解决。 I haven't tried generating the encoding, but should be possible I believe. 我没有尝试生成编码,但我相信应该是可能的。

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

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