Huffman Codes and Data Compression

哈夫曼编码让不同字符之间代码长度不同,保证经常出现的字符的代码要短。

Prefix Code

浅层定义:没有字符代码是别的字符代码的前缀。

定义:for a set S S S of letters, a prefix code is a function γ \gamma γ that maps each letter x ∈ S x \in S xS to some sequence of zeros and ones, in such way that for distinct x , y ∈ S x, y \in S x,yS, the sequence γ ( x ) \gamma(x) γ(x) is not a prefix of the sequence γ ( y ) \gamma(y) γ(y).

Optimal Prefix Codes

假设对于任意一个字母 x ∈ S x \in S xS, f x f_x fx 代表字母 x x x 出现的频率。

  • 假设总共有 n n n 个字母,其中 n f x n f_x nfx 个字母是 x x x.
  • ∑ x ∈ S f x = 1 \sum_{x\in S} f_x = 1 xSfx=1

∣ γ ( x ) ∣ |\gamma(x)| γ(x) 表示 γ ( x ) \gamma(x) γ(x) 的长度,那么
encoding length = ∑ x ∈ S n f x ∣ γ ( x ) ∣ = n ∑ x ∈ S f x ∣ γ ( x ) ∣ \text{encoding length}=\sum_{x \in S} n f_x |\gamma(x)| = n \sum_{x \in S} f_x |\gamma(x)| encoding length=xSnfxγ(x)=nxSfxγ(x)

定义: the average number of bits required per letter A B L ( γ ) = ∑ x ∈ S f x ∣ γ ( x ) ∣ ABL(\gamma)=\sum_{x \in S} f_x |\gamma(x)| ABL(γ)=xSfxγ(x)

目标:minimize A B L ( γ ) ABL(\gamma) ABL(γ)

算法设计

A binary tree T T T with the number of leaves equal to the size of the alphabet S S S, and we label each leaf with a distinct letter in S S S.

定理 (4.27): The encoding of S S S constructed from T T T is a prefix code.
证明:(反证法)
γ ( x ) \gamma(x) γ(x) γ ( y ) \gamma(y) γ(y) 的 prefix,the path from the root to x x x would have to be a prefix of the path from
the root to y y y
但是,这等同于说 x x x would lie on the path from root to y y y
x x x 是 leaf 矛盾

A B L ( T ) = ∑ x ∈ S f x ⋅ depth T ( x ) ABL(T) = \sum_{x \in S} f_x \cdot \text{depth}_T(x) ABL(T)=xSfxdepthT(x)

定理 (4.28): The binary tree corresponding to the optimal prefix code is full.
证明:(exchange argument)
Let T T T denote the binary tree corresponding to the optimal prefix code, and suppose it contains
a node u u u with exactly one child v v v.
\quad
Convert T T T into a tree T ′ T' T by replacing node u u u with v v v.
分两类情况讨论:
1.    u \; u u 是 root: 删除 u u u, 并将 v v v 变成 root。
2.    u \; u u 不是 root:
\quad w w w u u u 的 parent in T T T.
\quad 删除 u u u,并让 v v v 成为 w w w 的 child.
\quad 这个改变 decrease the number of bits needed to encode any leaf in the subtree rooted at node u u u,
\quad and it does not affect other leaves
   ∴ A B L ( T ′ ) < A B L ( T ) \quad \; \therefore ABL(T')<ABL(T) ABL(T)<ABL(T)
\quad 与 optimality of T T T 矛盾

定理 (4.29): 假设已知 optimal binary tree T ∗ T^* T。Suppose that u u u and v v v are lives of T ∗ T^* T, such that d e p t h ( u ) < d e p t h ( v ) depth(u) < depth(v) depth(u)<depth(v). Further, suppose that in a labeling of T ∗ T^* T corresponding to an optimal prefix code, leaf u u u is labeled with y ∈ S y\in S yS and leaf v v v is labeled with z ∈ S z \in S zS. Then, f y ≥ f z f_y \geq f_z fyfz.
证明:用 exchange argument 证明,交换 u , v u, v u,v 的 label。

定理 (4.30): Consider a leaf v v v in T ∗ T^* T with largest depth. Leaf v v v has a parent u u u, and by (4.28) T ∗ T^* T is a full binary tree, so u u u has another child w w w. Then, w w w is a leaf of T ∗ T^* T. 反证法

定理 (4.31): There is an optimal prefix code, with corresponding tree T ∗ T^* T, in which the two lowest-frequency letters are assigned to leaves that are siblings in T ∗ T^* T.

算法伪代码

To construct a prefix code for an alphabet S S S, with given frequencies:

  1. If S S S has two letters then
  2. \quad Encode one letter using 0 and the other letter using 1
  3. Else
  4. \quad Let y ∗ y^* y and z ∗ z^* z be the two lowest-frequency letters
  5. \quad Form a new alphabet S ′ S' S by deleting y ∗ y* y and z ∗ z^* z and replacing them with a new letter w w w of frequency f y ∗ + f z ∗ f_{y^*}+f_{z^*} fy+fz
  6. \quad Recursively construct a prefix code γ ′ \gamma' γ for S ′ S' S, with tree T ′ T' T
  7. \quad Define a prefix code for S S S as follows:
  8. \quad \quad Start with T ′ T' T
  9. \quad \quad Take the leaf labeled w w w and add two children below it, labeled y ∗ y^* y and z ∗ z^* z
  10. EndIf
    Huffman Codes Example:图片来自《数据结构与算法分析 Java语言描述》作者:Mark Allen Weiss 译者:冯舜玺、陈越
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 作为一名程序员,我在2023年对自己的计划包括: 1. 不断学习和提升技能:我会继续学习新的编程语言和框架,以及不断提高编码能力。 2. 参加行业相关的培训和会议:我会参加各种行业相关的培训课程和会议,以便获得最新的行业知识和技能。 3. 参加项目并积累经验:我会参加各种项目,以便积累丰富的经验,并提升个人能力。 4. 在业内建立良好的人脉关系:我会努力与业内的同行建立良好的人脉关系,并与他们保持联系。 5. 寻找更好的发展机会:我会不断寻找更好的发展机会,以便在职业生涯中取得更大的成功。 总之,我在2023年会努力工作,不断学习和提升,并寻找更好的发展机会,以在职业生涯中取得更大的成功。 ### 回答2: 2023年的计划对于我作为一名程序员来说十分重要,我希望能够提升自己的技能水平并取得更好的职业发展。以下是我的计划: 首先,我计划继续学习并掌握新的编程语言和技术。随着科技的不断发展,编程语言和技术也在不断更新换代,作为一名程序员,保持学习的状态是必不可少的。我将花费更多的时间学习目前主流的编程语言,如Python、Java等,并尝试掌握最新的前端和后端技术。通过不断学习,我希望能够拥有更广泛的技术视野和更强大的技术能力。 其次,我计划参与更多的项目和实践。在理论学习之外,实践是提升技能的关键。我计划积极寻找项目机会,无论是个人项目还是团队项目,都可以提供宝贵的实践机会。通过参与各种项目,我可以锻炼解决问题的能力,提高编码和协作能力。同时,我也希望通过实践中的挑战和失败,不断完善自己,进一步提高自己的技术水平和经验。 第三,我计划参加相关的培训和技术交流活动。参加培训和技术交流活动可以与其他程序员交流和学习,了解行业最新动态和趋势。我计划参加各种技术研讨会、讲座和培训班,通过与行业专家和其他程序员的交流,深入了解各种编程技术和最佳实践。同时,我也希望能够积极参与技术社区,与其他程序员分享自己的经验和见解,不断提高自己的影响力和口碑。 最后,我计划在个人项目和开源社区上做出更多的贡献。通过自己的努力,我希望能够在个人项目中实现一些有意义的功能或解决一些实际问题,并将其开源。通过开源社区的贡献,我可以帮助他人解决问题,同时也能够借助其他人的反馈和指导,不断改进自己的代码和设计能力。 总之,2023年对于我作为一名程序员来说是充满挑战和机遇的一年。我将不懈努力,持续学习和实践,不断提升自己的技能水平和职业发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值