为了保证:Block中,所有的叶子在所有的中间结点的前面。Static: Huffman coding

Dynamic: Adaptive Huffman


一些概念

压缩指标

• Compress a 10MB file to 2MB
• Compression ratio = 5 or 5:1
• Space savings = 0.8 or 80%

对称与非对称

• Symmetric compression 对称压缩
  – requires same time for encoding and decoding
  – used for live mode applications (teleconference)
• Asymmetric compression 非对称压缩,压缩慢,解压快
  – performed once when enough time is available
  – decompression performed frequently, must be fast
  – used for retrieval mode applications (e.g., an interactive CD-ROM)

压缩解压的唯一性 - Uniquely decodable

compression的基本条件。

Static or Dynamic codes

Static:

Huffman coding,需知道字符的编码。-->

Dynamic:

Adaptive Huffman。-->

Shannon’s Result


Huffman coding

前提是需知晓 Freq。

L

= ( 30*2 + 30*2 + 20*2 + 10*3 + 10*3 ) / 100
= 220 / 100
= 2.2

问题一

香农理论极限是:

H
= -0.3 * log 0.3 + -0.3 * log 0.3 + -0.2 * log 0.2 + -0.1 * log 0.1 + -0.1 * log 0.1
= -0.3*(-1.737) + -0.3*(-1.737) + -0.2 * (-2.322) + -0.1 * (-3.322) + -0.1 * (-3.322)
= 0.3 log 10/3 + 0.3 log 10/3 + 0.2 log 5 + 0.1 log 10 + 0.1 log 10
= 0.3*1.737 + 0.3*1.737 + 0.2* 2.322 + 0.1*3.322 + 0.1*3.322
= 2.17 < 2.2   // 说明未达到极限,还有压缩的余地

问题二

Freq不平均的话,压缩率越差。

L = (100000*1 + ...)/100010
≈ 1

H = 0.9999 log 1.0001 + 0.00006 log 16668.333
+ ... + 1/100010 log 100010
≈ 0.00


Adaptive Huffman

Problems of Static coding
• Need statistics & static: e.g., single pass over the data just to collect stat & stat unchanged during encoding
• To decode, the stat table need to be transmitted. Table size can be significant for small msg.
 => Adaptive compression e.g., adaptive huffman

两个阶段:

• FGK Algorithm
• Vitter's Invariant

FGK Algorithm.

Video: https://www.youtube.com/watch?v=N5pw_Z-oP-4

Rule: 

In the same block,中间结点的index总是大于叶子结点的index。<-- Vitter's Invariant

权重值大的结点,其index也较大。

Operation:

操作1:Leaf node: move first, then update

操作2:Internal node: update first, then move.

NYT node = null node.

Stream: abcbaaa

a = 0110 0001

b = 0110 0010

c = 0110 0011

Step 1

[0110 0001]

表示插入的位置是左枝

Step 2

插入b之后的样子如下。

Next,需要执行“操作2”。

原来的NYT变为1(孩子value之和),补充完编号。(update)

考虑move操作,画出block,所有标号为1的nodes。

为了保证:Block中,所有的叶子在所有的中间结点的前面。

但,目前满足这个要求么?显然不是,如下的1,a 比较碍眼。

252 253 254 255 256
NYT b a 1

那么,如何move?将上图中的254结点连带子树 与 跟它冲突的255交换。

可见,这样就重新满足了the Rule.

0110 0001]

Step 3

然后继续 insert c,当然还是在NYT这个位置。

插入效果如下:

Next,还是先 update internal node。

为了保证:Block中,所有的叶子在所有的中间结点的前面。

但,目前满足这个要求么?显然不是,如下的1,b,a 比较碍眼。

251 252 253 254 255
c b a 1

开始move:253,254左移,给252的1腾出地儿。

可见,这样就重新满足了the Rule.

0110 0001] 0110 0011]

Step 4

4th是b,已有b,所以挂在已有的node b下面。

0110 0001]

这次,先 update leaf node,也包括node b的个数++。

这里满足了the Rule的第一条,即叶子结点index较小。

但,the Rule的第二条未满足,即权重大的index较大。

c(1) b(2) a(1) (1)

所以,将b移动到最后位置254,如下。

可见,这样就重新满足了the Rule.

0110 0001] ]

Step 5

5th是a,已有a,所以挂在已有的node a下面。

0110 0001] ] 10

这里满足了the Rule的第一条,即叶子结点index较小。

但,the Rule的第二条未满足,即权重大的index较大。

252 252 253
c(1) a(2) (1)

交换252与 253及其子树后,如下:

可见,这样就重新满足了the Rule.

0110 0001] 0110 0011]] ]

Step 6

6th是a,已有a,所以挂在已有的node a下面。

0110 0001] 0110 0011] ] ]

此时,253的a的权重变为3,根据the Rule,权重大的index较大。

所以,253:a 应该在254:b的后面。交换后,如下:

0110 0001] 0110 0011] ] ] ]

Step 7 

7th是a,已有a,所以挂在已有的node a下面。

0110 0001] 0110 0011] ] ]

此时,254的a的权重变为4,根据the Rule,权重大的index较大。

所以,254:a 应该在255的后面。交换后如下:

0110 0001] 0110 0011] ] ] ]

[IR] Huffman Coding的更多相关文章

  1. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  2. [IR] Arithmetic Coding

    Statistical methods的除了huffman外的另一种常见压缩方式. Huffman coding的非连续数值特性成为了无法达到香农极限的先天无法弥补的缺陷,但Arithmetic co ...

  3. 霍夫曼编码(Huffman Coding)

    霍夫曼编码(Huffman Coding)是一种编码方法,霍夫曼编码是可变字长编码(VLC)的一种. 霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符 ...

  4. 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)

    前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...

  5. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  6. <Sicily>Huffman coding

    一.题目描述 In computer science and information theory, a Huffman code is an optimal prefix code algorith ...

  7. 哈夫曼编码的理解(Huffman Coding)

    哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最 ...

  8. Huffman coding & Huffman tree

    Huffman coding & Huffman tree Huffman coding 哈夫曼编码 / 最优二元前缀码 Huffman tree 哈夫曼树 / 最优二叉树 https://w ...

  9. jpeg huffman coding table

    亮度DC系数的取值范围及序号:                                                               序号(size) 取值范围 0 0  1 - ...

随机推荐

  1. cocos creator 入门理解点

    简单解释, [来源:官方文档] Cocos是触控科技推出的游戏开发一站式解决方案,包含了从新建立项.游戏制作.到打包上线的全套流程.开发者可以通过cocos快速生成代码.编辑资源和动画,最终输出适合于 ...

  2. linux find命令用法

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  3. WinForm EF+MySql企业管理软件C/S项目实战演练

    一.课程介绍 我们都知道在软件架构方式分为:C/S和B/S两类.这里阿笨不谈论两种软件架构的优劣之分,因为它们各有千秋,用于不同场合.一位伟大的讲师曾经说过一句话:事物存在即合理!录制这堂课程的目的就 ...

  4. uboot的常用命令及用法

    转自:https://blog.csdn.net/jklinux/article/details/72638830 https://blog.csdn.net/dagefeijiqumeiguo/ar ...

  5. Socket编程的UDP与TCP,应用在哪些地方

    随着网络技术飞速发展,网速已不再是传输的瓶颈,UDP协议以其简单.传输快的优势,在越来越多场景下取代了TCP,如网页浏览.流媒体.实时游戏.物联网. 1,网速的提升给UDP稳定性提供可靠网络保障 CD ...

  6. boost 线程安全队列

    threadnullmethodsprocessingobjectsignal // QueueImplementation.cpp : Defines the entry point for the ...

  7. Java高编译低运行错误(ConcurrentHashMap.keySet)

    Java高编译低运行错误(ConcurrentHashMap.keySet) 调了一天: https://www.jianshu.com/p/f4996b1ccf2f

  8. RPC简述

    在某种意义上,WebService.REST均是RPC的实现,那么RPC的发展过程如何呢?本文参考了wikipedia,对RPC做一下简要摘记. RPC(RemoteProcedureCall),是进 ...

  9. Java线程池 / Executor / Callable / Future

    为什么需要线程池?   每次都要new一个thread,开销大,性能差:不能统一管理:功能少(没有定时执行.中断等).   使用线程池的好处是,可重用,可管理.   Executor     4种线程 ...

  10. WIN10平板 如何设置不允许切换竖屏

    点击右下角的通知,然后点击旋转锁定,即可禁止自动竖屏切换