[IR] Huffman Coding

 

爲了保證:Block中,全部的葉子在全部的中間結點的前面。Static: Huffman codingnode

Dynamic: Adaptive Huffmanapp


 

一些概念

壓縮指標ide

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

 

對稱與非對稱編碼

• 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)spa

 

壓縮解壓的惟一性 - Uniquely decodable3d

compression的基本條件。code

 

Static or Dynamic codesorm

Static:blog

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] 

[0110 0001] 0 表示插入的位置是左枝

 

Step 2

插入b以後的樣子以下。

Next,須要執行「操做2」。

原來的NYT變爲1(孩子value之和),補充完編號。(update)

考慮move操做,畫出block,全部標號爲1的nodes。

爲了保證:Block中,全部的葉子在全部的中間結點的前面。

但,目前知足這個要求麼?顯然不是,以下的1,a 比較礙眼。

252 253 254 255 256
NYT b 1 a 1

那麼,如何move?將上圖中的254結點連帶子樹 與 跟它衝突的255交換。

可見,這樣就從新知足了the Rule.

[0110 0001] [00110 0001]

 

Step 3

而後繼續 insert c,固然仍是在NYT這個位置。

[0110 0001] [00110 0001] 10

插入效果以下:

Next,仍是先 update internal node。

爲了保證:Block中,全部的葉子在全部的中間結點的前面。

但,目前知足這個要求麼?顯然不是,以下的1,b,a 比較礙眼。

251 252 253 254 255
c 1 b a 1

開始move:253,254左移,給252的1騰出地兒。

可見,這樣就從新知足了the Rule.

[0110 0001] [00110 0001] [100110 0011]

 

Step 4

4th是b,已有b,因此掛在已有的node b下面。

[0110 0001] [00110 0001] [100110 0011] 10

此次,先 update leaf node,也包括node b的個數++。

這裏知足了the Rule的第一條,即葉子結點index較小。

但,the Rule的第二條未知足,即權重大的index較大。

251 252 253 254
c(1) b(2) a(1) (1)

因此,將b移動到最後位置254,以下。

可見,這樣就從新知足了the Rule.

[0110 0001] [00110 0001] [100110 0011] [10]

 

Step 5

5th是a,已有a,因此掛在已有的node a下面。

[0110 0001] [00110 0001] [100110 0011] [10] 10

這裏知足了the Rule的第一條,即葉子結點index較小。

但,the Rule的第二條未知足,即權重大的index較大。

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

交換252與 253及其子樹後,以下:

可見,這樣就從新知足了the Rule.

[0110 0001] [00110 0001] [100110 0011] [10] [10]

 

Step 6

6th是a,已有a,因此掛在已有的node a下面。

[0110 0001] [00110 0001] [100110 0011] [10] [10] 11

此時,253的a的權重變爲3,根據the Rule,權重大的index較大。

因此,253:a 應該在254:b的後面。交換後,以下:

[0110 0001] [00110 0001] [100110 0011] [10] [10] [11]

 

Step 7 

7th是a,已有a,因此掛在已有的node a下面。

[0110 0001] [00110 0001] [100110 0011] [10] [10] [11] 0

此時,254的a的權重變爲4,根據the Rule,權重大的index較大。

因此,254:a 應該在255的後面。交換後以下:

[0110 0001] [00110 0001] [100110 0011] [10] [10] [11] [0]

相關文章
相關標籤/搜索