Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

ืื™ื™ื“ืขืจ ื“ื™ ืึธื ื”ื™ื™ื‘ ืคื•ืŸ ื“ืขื ืงื•ืจืก "ืึทืœื’ืขืจื™ื“ืึทืžื– ืคึฟืึทืจ ื“ืขื•ื•ืขืœืึธืคึผืขืจืก" ืžื™ืจ ื”ืึธื‘ืŸ ืฆื•ื’ืขื’ืจื™ื™ื˜ ืึท ืื™ื‘ืขืจื–ืขืฆื•ื ื’ ืคื•ืŸ ืืŸ ืื ื“ืขืจ ื ื•ืฆื™ืง ืžืึทื˜ืขืจื™ืึทืœ ืคึฟืึทืจ ืื™ืจ.

Huffman ืงืึธื“ื™ืจื•ื ื’ ืื™ื– ืึท ื“ืึทื˜ืŸ ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื ื•ื•ืึธืก ืคืึธืจืžื•ืœื™ืจื˜ ื“ื™ ื’ืจื•ื ื˜ ื’ืขื“ืึทื ืง ืคื•ืŸ ื˜ืขืงืข ืงืึทืžืคึผืจืขืฉืึทืŸ. ืื™ืŸ ื“ืขื ืึทืจื˜ื™ืงืœ ืžื™ืจ ื•ื•ืขืœืŸ ืจืขื“ืŸ ื•ื•ืขื’ืŸ ืคืึทืจืคืขืกื˜ื™ืงื˜ ืื•ืŸ ื•ื•ืขืจื™ืึทื‘ืึทืœ ืœืขื ื’ ืงืึธื“ื™ืจื•ื ื’, ื™ื•ื ื™ืงืœื™ ื“ื™ืงืึธื•ื“ืึทื‘ืึทืœ ืงืึธื•ื“ื–, ืคึผืจืขืคื™ืงืก ื›ึผืœืœื™ื ืื•ืŸ Huffman ื‘ื•ื™ื ืงืึทื ืกื˜ืจืึทืงืฉืึทืŸ.

ืžื™ืจ ื•ื•ื™ืกืŸ ืึทื– ื™ืขื“ืขืจ ื›ืึทืจืึทืงื˜ืขืจ ืื™ื– ืกื˜ืึธืจื“ ื•ื•ื™ ืึท ืกื™ืงื•ื•ืึทื ืก ืคื•ืŸ 0 ืก ืื•ืŸ 1 ืก ืื•ืŸ ืึทืงื™ืึทืคึผื™ื™ื– 8 ื‘ื™ื˜ืŸ. ื“ืึธืก ืื™ื– ื’ืขืจื•ืคืŸ ืคืึทืจืคืขืกื˜ื™ืงื˜-ืœืขื ื’ ืงืึธื“ื™ืจื•ื ื’ ื•ื•ื™ื™ึทืœ ื™ืขื“ืขืจ ื›ืึทืจืึทืงื˜ืขืจ ื ื™ืฆื˜ ื“ื™ ื–ืขืœื‘ืข ืคืึทืจืคืขืกื˜ื™ืงื˜ ื ื•ืžืขืจ ืคื•ืŸ ืกื˜ืึธืจื™ื“ื–ืฉ ื‘ื™ื˜ืŸ.

ื–ืืœ ืก ื–ืึธื’ืŸ ื“ื™ ื˜ืขืงืกื˜ ืื™ื– ื’ืขื’ืขื‘ืŸ. ื•ื•ื™ ืงืขื ืขืŸ ืžื™ืจ ืจืขื“ื•ืฆื™ืจืŸ ื“ื™ ืกื•ืžืข ืคื•ืŸ โ€‹โ€‹ืคึผืœืึทืฅ ืคืืจืœืื ื’ื˜ ืฆื• ืงืจืึธื ืื™ื™ืŸ ื›ืึทืจืึทืงื˜ืขืจ?

ื“ื™ ื’ืจื•ื ื˜ ื’ืขื“ืึทื ืง ืื™ื– ืงืึธื“ื™ืจื•ื ื’ ืคื•ืŸ ื•ื•ืขืจื™ืึทื‘ืึทืœ ืœืขื ื’. ืžื™ืจ ืงืขื ืขืŸ ื ื•ืฆืŸ ื“ืขื ืคืึทืงื˜ ืึทื– ืขื˜ืœืขื›ืข ืื•ืชื™ื•ืช ื“ืขืจืฉื™ื™ึทื ืขืŸ ืžืขืจ ืึธืคื˜ ืื™ืŸ ื˜ืขืงืกื˜ ื•ื•ื™ ืื ื“ืขืจืข (ื–ืขืŸ ื“ืึธ) ืฆื• ืึทื ื˜ื•ื•ื™ืงืœืขืŸ ืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื ื•ื•ืึธืก ื•ื•ืขื˜ ืคืึธืจืฉื˜ืขืœืŸ ื“ื™ ื–ืขืœื‘ืข ืกื™ืงื•ื•ืึทื ืก ืคื•ืŸ ืื•ืชื™ื•ืช ืื™ืŸ ื•ื•ื™ื™ื ื™ืงืขืจืข ื‘ื™ื˜ืŸ. ืื™ืŸ ื•ื•ืขืจื™ืึทื‘ืึทืœ ืœืขื ื’ ืงืึธื“ื™ืจื•ื ื’, ืžื™ืจ ื‘ืึทืฉื˜ื™ืžืขืŸ ืึท ื‘ื™ื™ึทื˜ืขื•ื•ื“ื™ืง ื ื•ืžืขืจ ืคื•ืŸ ื‘ื™ื˜ืŸ ืฆื• ืื•ืชื™ื•ืช ื“ื™ืคึผืขื ื“ื™ื ื’ ืื•ื™ืฃ ื“ื™ ืึธืคื˜ืงื™ื™ึทื˜ ืคื•ืŸ ื–ื™ื™ืขืจ ืคึผืึทืกื™ืจื•ื ื’ ืื™ืŸ ืึท ื’ืขื’ืขื‘ืŸ ื˜ืขืงืกื˜. ืœืขืกืึธืฃ, ืขื˜ืœืขื›ืข ืื•ืชื™ื•ืช ืงืขืŸ ื ืขืžืขืŸ ื‘ืœื•ื™ื– 1 ื‘ื™ืกืœ, ื‘ืฉืขืช ืื ื“ืขืจืข ืงืขืŸ ื ืขืžืขืŸ 2 ื‘ื™ื˜ืŸ, 3 ืึธื“ืขืจ ืžืขืจ. ื“ื™ ืคึผืจืึธื‘ืœืขื ืžื™ื˜ ื“ื™ ืงืึธื“ื™ืจื•ื ื’ ืคื•ืŸ ื•ื•ืขืจื™ืึทื‘ืึทืœ ืœืขื ื’ ืื™ื– ื‘ืœื•ื™ื– ื“ื™ ืกืึทื‘ืกืึทืงื•ื•ืึทื ื˜ ื“ื™ืงืึธื•ื“ื™ื ื’ ืคื•ืŸ ื“ื™ ืกื™ืงื•ื•ืึทื ืก.

ื•ื•ื™, ื•ื•ื™ืกืŸ ืึท ืกื™ืงื•ื•ืึทื ืก ืคื•ืŸ ื‘ื™ื˜ืŸ, ืงืขื ืขืŸ ืื™ืจ ื“ืขืงืึธื“ืข ืขืก ืึทื ืึทืžื‘ื™ื’ื™ื•ืึทืกืœื™?

ื‘ืึทื˜ืจืึทื›ื˜ืŸ ื“ื™ ืฉื•ืจื” "ืึทืึทื‘ืึทืงื“ืึทื‘". ืขืก ื”ืื˜ 8 ืื•ืชื™ื•ืช, ืื•ืŸ ืžื™ื˜ ืคืึทืจืคืขืกื˜ื™ืงื˜ ืœืขื ื’ ืงืึธื“ื™ืจื•ื ื’ ืขืก ื•ื•ืขื˜ ื“ืึทืจืคืŸ 64 ื‘ื™ืฅ ืฆื• ืงืจืึธื ืขืก. ื‘ืึทืžืขืจืงื•ื ื’ ืึทื– ื“ื™ ืกื™ืžื‘ืึธืœ ืึธืคื˜ืงื™ื™ึทื˜ "ืึท", "ื‘", "ืง" ะธ "ื“" ื™ืงื•ื•ืึทืœื– 4, 2, 1, 1 ืจื™ืกืคึผืขืงื˜ื™ื•ื•ืœื™. ื–ืืœ ืก ืคึผืจื•ื‘ื™ืจืŸ ืฆื• ื™ืžืึทื“ื–ืฉืึทืŸ "ืึทืึทื‘ืึทืงื“ืึทื‘" ืื™ืŸ ื•ื•ื™ื™ื ื™ืงืขืจืข ื‘ื™ื˜ืŸ, ื ื™ืฆืŸ ื“ื™ ืคืึทืงื˜ ืึทื– "ืฆื•" ืึทืงืขืจื– ืžืขืจ ืึธืคื˜ ื•ื•ื™ "ื‘"ืื•ืŸ "ื‘" ืึทืงืขืจื– ืžืขืจ ืึธืคื˜ ื•ื•ื™ "ื’" ะธ "ื“". ืžื™ืจ ื•ื•ืขืœืŸ ืึธื ื”ื™ื™ื‘ืŸ ืžื™ื˜ ืงืึธื“ื™ืจื•ื ื’ "ืฆื•" ื ื™ืฆืŸ ืื™ื™ืŸ ื‘ื™ืกืœ ื’ืœื™ื™ึทืš ืฆื• 0, "ื‘" ืžื™ืจ ื•ื•ืขืœืŸ ื‘ืึทืฉื˜ื™ืžืขืŸ ืึท ืฆื•ื•ื™ื™-ื‘ื™ืกืœ ืงืึธื“ ืฆื• 11, ืื•ืŸ ื ื™ืฆืŸ ื“ืจื™ื™ ื‘ื™ื˜ืŸ 100 ืื•ืŸ 011, ืžื™ืจ ืขื ืงืึธื•ื“ "ื’" ะธ "ื“".

ื•ื•ื™ ืึท ืจืขื–ื•ืœื˜ืึทื˜, ืžื™ืจ ื•ื•ืขืœืŸ ื‘ืึทืงื•ืžืขืŸ:

a
0

b
11

c
100

d
011

ืึทื–ื•ื™ ื“ื™ ืฉื•ืจื” "ืึทืึทื‘ืึทืงื“ืึทื‘" ืžื™ืจ ื•ื•ืขื˜ ืงืึธื“ ืขืก ื•ื•ื™ 00110100011011 (0|0|11|0|100|011|0|11)ื ื™ืฆืŸ ื“ื™ ืงืึธื•ื“ื– ืฆื•ื’ืขืฉื˜ืขืœื˜ ืื•ื™ื‘ืŸ. ืึธื‘ืขืจ, ื“ืขืจ ื”ื•ื™ืคึผื˜ ืคึผืจืึธื‘ืœืขื ื•ื•ืขื˜ ื–ื™ื™ืŸ ืื™ืŸ ื“ื™ืงืึธื•ื“ื™ื ื’. ื•ื•ืขืŸ ืžื™ืจ ืคึผืจื•ื‘ื™ืจืŸ ืฆื• ื“ืขืงืึธื“ืข ื“ื™ ืฉื˜ืจื™ืงืœ 00110100011011, ืžื™ืจ ื•ื•ืขืœืŸ ื‘ืึทืงื•ืžืขืŸ ืึทืŸ ืึทืžื‘ื™ื’ื™ื•ืึทืก ืจืขื–ื•ืœื˜ืึทื˜, ื•ื•ื™ื™ึทืœ ืขืก ืงืขื ืขืŸ ื–ื™ื™ืŸ ืจืขืคึผืจื™ื–ืขื ื˜ื™ื“ ื•ื•ื™:

0|011|0|100|011|0|11    adacdab
0|0|11|0|100|0|11|011   aabacabd
0|011|0|100|0|11|0|11   adacabab 

...
ืืื–"ื• ื•

ืฆื• ื•ื™ืกืžื™ื™ื“ืŸ ื“ืขื ืึทืžื‘ื™ื’ื™ื•ืึทื˜ื™, ืžื™ืจ ืžื•ื–ืŸ ืขื ืฉื•ืจ ืึทื– ืื•ื ื“ื–ืขืจ ืงืึธื“ื™ืจื•ื ื’ ืกืึทื˜ื™ืกืคื™ื™ื– ืึท ื‘ืึทื’ืจื™ืฃ ืึทื–ืึท ื•ื•ื™ ืคึผืจืขืคื™ืงืก ื”ืขืจืฉืŸ, ื•ื•ืึธืก ืื™ืŸ ืงืขืจ ื™ืžืคึผืœื™ื™ื– ืึทื– ื“ื™ ืงืึธื•ื“ื– ืงืขื ืขืŸ ื–ื™ื™ืŸ ื“ื™ืงืึธื•ื“ื™ื“ ืื™ืŸ ื‘ืœื•ื™ื– ืื™ื™ืŸ ื™ื™ื ืฆื™ืง ื•ื•ืขื’. ื“ื™ ืคึผืจืขืคื™ืงืก ื”ืขืจืฉืŸ ื™ื ืฉื•ืจื– ืึทื– ืงื™ื™ืŸ ืงืึธื“ ืื™ื– ืึท ืคึผืจืขืคื™ืงืก ืคื•ืŸ ืื ื“ืขืจืŸ. ืžื™ื˜ ืงืึธื“ ืžื™ืจ ืžื™ื™ื ืขืŸ ื“ื™ ื‘ื™ื˜ืŸ ื’ืขื ื™ืฆื˜ ืฆื• ืคืึธืจืฉื˜ืขืœืŸ ืึท ื‘ืึทื–ื•ื ื“ืขืจ ื›ืึทืจืึทืงื˜ืขืจ. ืื™ืŸ ื“ื™ ืื•ื™ื‘ืŸ ื‘ื™ื™ึทืฉืคึผื™ืœ 0 ืื™ื– ืึท ืคึผืจืขืคื™ืงืก 011, ื•ื•ืืก ืคืืจืœืขืฆื˜ ื“ื™ ืคืจืขืคื™ืงืก ื”ืขืจืฉืŸ. ืึทื–ื•ื™, ืื•ื™ื‘ ืื•ื ื“ื–ืขืจ ืงืึธื•ื“ื– ื‘ืึทืคืจื™ื“ื™ืงืŸ ื“ื™ ืคึผืจืขืคื™ืงืก ื”ืขืจืฉืŸ, ืžื™ืจ ืงืขื ืขืŸ ืึทื ืึทืžื‘ื™ื’ื™ื•ืึทืกืœื™ ื“ืขืงืึธื“ืข (ืื•ืŸ ื•ื•ื™ืฆืข ื•ื•ืขืจืกืึท).

ื–ืืœ ืก ืจื™ื•ื•ื™ื–ืŸ ื“ื™ ื‘ื™ื™ึทืฉืคึผื™ืœ ืื•ื™ื‘ืŸ. ื“ืึธืก ืžืึธืœ ืžื™ืจ ื•ื•ืขืœืŸ ื‘ืึทืฉื˜ื™ืžืขืŸ ืคึฟืึทืจ ืกื™ืžื‘ืึธืœืก "ืึท", "ื‘", "ืง" ะธ "ื“" ืงืึธื•ื“ื– ื•ื•ืึธืก ื‘ืึทืคืจื™ื“ื™ืงืŸ ื“ื™ ืคึผืจืขืคื™ืงืก ื”ืขืจืฉืŸ.

a
0

b
10

c
110

d
111

ื ื™ืฆืŸ ื“ืขื ืงืึธื“ื™ืจื•ื ื’, ื“ื™ ืฉื˜ืจื™ืงืœ "ืึทืึทื‘ืึทืงื“ืึทื‘" ื•ื•ืขื˜ ื–ื™ื™ืŸ ืขื ืงืึธื•ื“ื™ื“ ื•ื•ื™ 00100100011010 (0|0|10|0|100|011|0|10). ืื•ืŸ ื“ืึธ 00100100011010 ืžื™ืจ ืงืขื ืขืŸ ืื™ืฆื˜ ืึทื ืึทืžื‘ื™ื’ื™ื•ืึทืกืœื™ ื“ืขืงืึธื“ืข ืื•ืŸ ืฆื•ืจื™ืงืงื•ืžืขืŸ ืฆื• ืื•ื ื“ื–ืขืจ ืึธืจื™ื’ื™ื ืขืœ ืฉื˜ืจื™ืงืœ "ืึทืึทื‘ืึทืงื“ืึทื‘".

ื”ืืคืžืึทืŸ ืงืึธื“ื™ืจื•ื ื’

ืื™ืฆื˜ ืึทื– ืžื™ืจ ืคึฟืึทืจืฉื˜ื™ื™ืŸ ื“ื™ ืงืึธื“ื™ืจื•ื ื’ ืคื•ืŸ ื•ื•ืขืจื™ืึทื‘ืึทืœ ืœืขื ื’ ืื•ืŸ ื“ื™ ืคึผืจืขืคื™ืงืก ื”ืขืจืฉืŸ, ืœืึธื–ืŸ ืื•ื ื“ื– ืจืขื“ืŸ ื•ื•ืขื’ืŸ Huffman ืงืึธื“ื™ืจื•ื ื’.

ื“ืขืจ ืื•ืคึฟืŸ ืื™ื– ื‘ืื–ื™ืจื˜ ืื•ื™ืฃ ืงืจื™ื™ื™ื˜ื™ื ื’ ื‘ื™ื™ื ืขืจื™ ื‘ื™ื™ืžืขืจ. ืื™ืŸ ืขืก, ืึท ื ืึธื“ืข ืงืขื ืขืŸ ื–ื™ื™ืŸ ืึธื“ืขืจ ืœืขืฆื˜ ืึธื“ืขืจ ื™ื ืขืจืœืขืš. ื˜ื›ื™ืœืขืก, ืึทืœืข ื ืึธื•ื“ื– ื–ืขื ืขืŸ ื’ืขื”ืืœื˜ืŸ ื‘ืœืขื˜ืขืจ (ืขื ื“ื–), ื•ื•ืึธืก ืคืึธืจืฉื˜ืขืœืŸ ื“ื™ ืกื™ืžื‘ืึธืœ ื–ื™ืš ืื•ืŸ ื–ื™ื™ึทืŸ ื•ื•ืึธื’ (ื“ืึธืก ืื™ื– ื“ื™ ืึธืคื˜ืงื™ื™ึทื˜ ืคื•ืŸ ืคึผืึทืกื™ืจื•ื ื’). ื“ื™ ื™ื ืขืจืœืขืš ื ืึธื•ื“ื– ืึทื ื˜ื”ืึทืœื˜ืŸ ื“ื™ ื•ื•ืึธื’ ืคื•ืŸ ื“ื™ ื›ืึทืจืึทืงื˜ืขืจ ืื•ืŸ ืึธืคึผืฉื™ืงืŸ ืฆื• ืฆื•ื•ื™ื™ ืกืึทืงืกืขืกืขืจ ื ืึธื•ื“ื–. ืœื•ื™ื˜ ืึทืœื’ืขืžื™ื™ืŸ ื”ืขืกืงืขื, ื‘ื™ืกืœ ยซ0ยป ืจืขืคึผืจืึทื–ืขื ืฅ ื•ื•ื™ื™ึทื˜ืขืจื“ื™ืง ื“ื™ ืœื™ื ืงืก ืฆื•ื•ื™ื™ึทื’, ืื•ืŸ ยซ1ยป - ืื•ื™ืฃ ื“ื™ ืจืขื›ื˜ืข ื–ื™ื™ื˜. ืื™ืŸ ืคื•ืœ ื‘ื•ื™ื N ื‘ืœืขื˜ืขืจ ืื•ืŸ N-ืงืกื ื•ืžืงืก ื™ื ืขืจืœืขืš ื ืึธื•ื“ื–. ืขืก ืื™ื– ืจืขืงืึทืžืขื ื“ื™ื“ ืึทื– ื•ื•ืขืŸ ืงืึทื ืกื˜ืจืึทืงื˜ื™ื ื’ ืึท Huffman ื‘ื•ื™ื, ืึทื ื™ื•ื–ื“ ืกื™ืžื‘ืึธืœืก ื–ืขื ืขืŸ ื“ื™ืกืงืึทืจื“ื™ื“ ืฆื• ื‘ืึทืงื•ืžืขืŸ ืงืึธื•ื“ื– ืคื•ืŸ ืึธืคึผื˜ื™ืžืึทืœ ืœืขื ื’.

ืžื™ืจ ื•ื•ืขืœืŸ ื ื•ืฆืŸ ืึท ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜ ืจื™ื™ ืฆื• ื‘ื•ื™ืขืŸ ืึท ื”ื•ืคืžืึทืŸ ื‘ื•ื™ื, ื•ื•ื• ื“ื™ ื ืึธื“ืข ืžื™ื˜ ื“ื™ ืœืึธื•ืึทืกื˜ ืึธืคื˜ืงื™ื™ึทื˜ ื•ื•ืขื˜ ื‘ืึทืงื•ืžืขืŸ ื“ื™ ื”ืขื›ืกื˜ืŸ ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜. ื“ื™ ืงืึทื ืกื˜ืจืึทืงืฉืึทืŸ ืกื˜ืขืคึผืก ื–ืขื ืขืŸ ื“ื™ืกืงืจื™ื™ื‘ื“ ืื•ื ื˜ืŸ:

  1. ืฉืึทืคึฟืŸ ืึท ื‘ืœืึทื˜ ื ืึธื“ืข ืคึฟืึทืจ ื™ืขื“ืขืจ ืกื™ืžื‘ืึธืœ ืื•ืŸ ืœื™ื™ื’ืŸ ื–ื™ื™ ืฆื• ื“ื™ ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜ ืจื™ื™.
  2. ื‘ืฉืขืช ืขืก ืื™ื– ืžืขืจ ื•ื•ื™ ืื™ื™ืŸ ื‘ืœืึทื˜ ืื™ืŸ ื“ืขืจ ืจื™ื™, ื˜ืึธืŸ ื“ื™ ืคืืœื’ืขื ื“ืข:
    • ืึทืจืึธืคึผื ืขืžืขืŸ ื“ื™ ืฆื•ื•ื™ื™ ื”ืขื›ืกื˜ืŸ ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜ (ืœืึธื•ืึทืกื˜ ืึธืคื˜ืงื™ื™ึทื˜) ื ืึธื•ื“ื– ืคื•ืŸ ื“ื™ ืจื™ื™;
    • ืฉืึทืคึฟืŸ ืึท ื ื™ื™ึทืข ื™ื ืขืจืœืขืš ื ืึธื“ืข, ื•ื•ื• ื“ื™ ืฆื•ื•ื™ื™ ื ืึธื•ื“ื– ื•ื•ืขื˜ ื–ื™ื™ืŸ ื“ื™ ืกืึทืงืกืขืกืขืจื–, ืื•ืŸ ื“ื™ ืึธืคื˜ืงื™ื™ึทื˜ ืคื•ืŸ ืคึผืึทืกื™ืจื•ื ื’ ื•ื•ืขื˜ ื–ื™ื™ืŸ ื’ืœื™ื™ึทืš ืฆื• ื“ื™ ืกืึทื›ืึทืงืœ ืคื•ืŸ ื“ื™ ืคืจื™ืงื•ื•ืึทื ืกื™ื– ืคื•ืŸ ื“ื™ ืฆื•ื•ื™ื™ ื ืึธื•ื“ื–.
    • ืœื™ื™ื’ ืึท ื ื™ื™ึทืข ื ืึธื“ืข ืฆื• ื“ื™ ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜ ืจื™ื™.
  3. ื“ืขืจ ื‘ืœื•ื™ื– ืจื•ืขืŸ ื ืึธื“ืข ื•ื•ืขื˜ ื–ื™ื™ืŸ ื“ืขืจ ื•ื•ืึธืจืฆืœ ื ืึธื“ืข, ืื•ืŸ ื“ืึธืก ื•ื•ืขื˜ ืคืึทืจืขื ื“ื™ืงืŸ ื“ื™ ืงืึทื ืกื˜ืจืึทืงืฉืึทืŸ ืคื•ืŸ ื“ืขื ื‘ื•ื™ื.

ืœืืžื™ืจ ื–ื™ืš ืคืืจืฉื˜ืขืœืŸ ืื– ืžื™ืจ ื”ืื‘ืŸ ืขืคืขืก ื ื˜ืขืงืกื˜ ื•ื•ืืก ื‘ืืฉื˜ื™ื™ื˜ ื‘ืœื•ื™ื– ืคื•ืŸ ืื•ืชื™ื•ืช "ื ื‘ ื’ ื“" ะธ "ืื•ืŸ", ืื•ืŸ ื–ื™ื™ืขืจ ืึธืคื˜ืงื™ื™ึทื˜ ืคื•ืŸ ืคึผืึทืกื™ืจื•ื ื’ ื–ืขื ืขืŸ 15, 7, 6, 6 ืื•ืŸ 5, ืจื™ืกืคึผืขืงื˜ื™ื•ื•ืœื™. ื•ื ื˜ืขืจ ื–ืขื ืขืŸ ืื™ืœื•ืกื˜ืจืืฆื™ืขืก ื•ื•ืึธืก ืคืึทืจื˜ืจืึทื›ื˜ื  ื–ื™ืš ื“ื™ ืกื˜ืขืคึผืก ืคื•ืŸ ื“ื™ ืึทืœื’ืขืจื™ื“ืึทื.

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื

ื“ืขืจ ื“ืจืš ืคื•ืŸ ื“ืขืจ ื•ื•ืึธืจืฆืœ ืฆื• ืงื™ื™ืŸ ื‘ืœืึทื˜ ื ืึธื“ืข ื•ื•ืขื˜ ืงืจืึธื ื“ื™ ืึธืคึผื˜ื™ืžืึทืœ ืคึผืจืขืคื™ืงืก ืงืึธื“ (ืื•ื™ืš ื‘ืืงืื ื˜ ื•ื•ื™ ืึท Huffman ืงืึธื“) ืงืึธืจืึทืกืคึผืึทื ื“ื™ื ื’ ืฆื• ื“ื™ ื›ืึทืจืึทืงื˜ืขืจ ืคืืจื‘ื•ื ื“ืŸ ืžื™ื˜ ื“ืขื ื‘ืœืึทื˜ ื ืึธื“ืข.

Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื
ื”ืืคืžืืŸ ื‘ื•ื™ื

ื•ื ื˜ืขืจ ืื™ืจ ื•ื•ืขื˜ ื’ืขืคึฟื™ื ืขืŸ ืึทืŸ ื™ืžืคึผืœืึทืžืขื ื˜ื™ื™ืฉืึทืŸ ืคื•ืŸ ื“ื™ Huffman ืงืึทืžืคึผืจืขืฉืึทืŸ ืึทืœื’ืขืจื™ื“ืึทื ืื™ืŸ C ++ ืื•ืŸ Java:

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;

// A Tree node
struct Node
{
	char ch;
	int freq;
	Node *left, *right;
};

// Function to allocate a new tree node
Node* getNode(char ch, int freq, Node* left, Node* right)
{
	Node* node = new Node();

	node->ch = ch;
	node->freq = freq;
	node->left = left;
	node->right = right;

	return node;
}

// Comparison object to be used to order the heap
struct comp
{
	bool operator()(Node* l, Node* r)
	{
		// highest priority item has lowest frequency
		return l->freq > r->freq;
	}
};

// traverse the Huffman Tree and store Huffman Codes
// in a map.
void encode(Node* root, string str,
			unordered_map<char, string> &huffmanCode)
{
	if (root == nullptr)
		return;

	// found a leaf node
	if (!root->left && !root->right) {
		huffmanCode[root->ch] = str;
	}

	encode(root->left, str + "0", huffmanCode);
	encode(root->right, str + "1", huffmanCode);
}

// traverse the Huffman Tree and decode the encoded string
void decode(Node* root, int &index, string str)
{
	if (root == nullptr) {
		return;
	}

	// found a leaf node
	if (!root->left && !root->right)
	{
		cout << root->ch;
		return;
	}

	index++;

	if (str[index] =='0')
		decode(root->left, index, str);
	else
		decode(root->right, index, str);
}

// Builds Huffman Tree and decode given input text
void buildHuffmanTree(string text)
{
	// count frequency of appearance of each character
	// and store it in a map
	unordered_map<char, int> freq;
	for (char ch: text) {
		freq[ch]++;
	}

	// Create a priority queue to store live nodes of
	// Huffman tree;
	priority_queue<Node*, vector<Node*>, comp> pq;

	// Create a leaf node for each character and add it
	// to the priority queue.
	for (auto pair: freq) {
		pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
	}

	// do till there is more than one node in the queue
	while (pq.size() != 1)
	{
		// Remove the two nodes of highest priority
		// (lowest frequency) from the queue
		Node *left = pq.top(); pq.pop();
		Node *right = pq.top();	pq.pop();

		// Create a new internal node with these two nodes
		// as children and with frequency equal to the sum
		// of the two nodes' frequencies. Add the new node
		// to the priority queue.
		int sum = left->freq + right->freq;
		pq.push(getNode('', sum, left, right));
	}

	// root stores pointer to root of Huffman Tree
	Node* root = pq.top();

	// traverse the Huffman Tree and store Huffman Codes
	// in a map. Also prints them
	unordered_map<char, string> huffmanCode;
	encode(root, "", huffmanCode);

	cout << "Huffman Codes are :n" << 'n';
	for (auto pair: huffmanCode) {
		cout << pair.first << " " << pair.second << 'n';
	}

	cout << "nOriginal string was :n" << text << 'n';

	// print encoded string
	string str = "";
	for (char ch: text) {
		str += huffmanCode[ch];
	}

	cout << "nEncoded string is :n" << str << 'n';

	// traverse the Huffman Tree again and this time
	// decode the encoded string
	int index = -1;
	cout << "nDecoded string is: n";
	while (index < (int)str.size() - 2) {
		decode(root, index, str);
	}
}

// Huffman coding algorithm
int main()
{
	string text = "Huffman coding is a data compression algorithm.";

	buildHuffmanTree(text);

	return 0;
}

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

// A Tree node
class Node
{
	char ch;
	int freq;
	Node left = null, right = null;

	Node(char ch, int freq)
	{
		this.ch = ch;
		this.freq = freq;
	}

	public Node(char ch, int freq, Node left, Node right) {
		this.ch = ch;
		this.freq = freq;
		this.left = left;
		this.right = right;
	}
};

class Huffman
{
	// traverse the Huffman Tree and store Huffman Codes
	// in a map.
	public static void encode(Node root, String str,
							  Map<Character, String> huffmanCode)
	{
		if (root == null)
			return;

		// found a leaf node
		if (root.left == null && root.right == null) {
			huffmanCode.put(root.ch, str);
		}


		encode(root.left, str + "0", huffmanCode);
		encode(root.right, str + "1", huffmanCode);
	}

	// traverse the Huffman Tree and decode the encoded string
	public static int decode(Node root, int index, StringBuilder sb)
	{
		if (root == null)
			return index;

		// found a leaf node
		if (root.left == null && root.right == null)
		{
			System.out.print(root.ch);
			return index;
		}

		index++;

		if (sb.charAt(index) == '0')
			index = decode(root.left, index, sb);
		else
			index = decode(root.right, index, sb);

		return index;
	}

	// Builds Huffman Tree and huffmanCode and decode given input text
	public static void buildHuffmanTree(String text)
	{
		// count frequency of appearance of each character
		// and store it in a map
		Map<Character, Integer> freq = new HashMap<>();
		for (int i = 0 ; i < text.length(); i++) {
			if (!freq.containsKey(text.charAt(i))) {
				freq.put(text.charAt(i), 0);
			}
			freq.put(text.charAt(i), freq.get(text.charAt(i)) + 1);
		}

		// Create a priority queue to store live nodes of Huffman tree
		// Notice that highest priority item has lowest frequency
		PriorityQueue<Node> pq = new PriorityQueue<>(
										(l, r) -> l.freq - r.freq);

		// Create a leaf node for each character and add it
		// to the priority queue.
		for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
			pq.add(new Node(entry.getKey(), entry.getValue()));
		}

		// do till there is more than one node in the queue
		while (pq.size() != 1)
		{
			// Remove the two nodes of highest priority
			// (lowest frequency) from the queue
			Node left = pq.poll();
			Node right = pq.poll();

			// Create a new internal node with these two nodes as children 
			// and with frequency equal to the sum of the two nodes
			// frequencies. Add the new node to the priority queue.
			int sum = left.freq + right.freq;
			pq.add(new Node('', sum, left, right));
		}

		// root stores pointer to root of Huffman Tree
		Node root = pq.peek();

		// traverse the Huffman tree and store the Huffman codes in a map
		Map<Character, String> huffmanCode = new HashMap<>();
		encode(root, "", huffmanCode);

		// print the Huffman codes
		System.out.println("Huffman Codes are :n");
		for (Map.Entry<Character, String> entry : huffmanCode.entrySet()) {
			System.out.println(entry.getKey() + " " + entry.getValue());
		}

		System.out.println("nOriginal string was :n" + text);

		// print encoded string
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < text.length(); i++) {
			sb.append(huffmanCode.get(text.charAt(i)));
		}

		System.out.println("nEncoded string is :n" + sb);

		// traverse the Huffman Tree again and this time
		// decode the encoded string
		int index = -1;
		System.out.println("nDecoded string is: n");
		while (index < sb.length() - 2) {
			index = decode(root, index, sb);
		}
	}

	public static void main(String[] args)
	{
		String text = "Huffman coding is a data compression algorithm.";

		buildHuffmanTree(text);
	}
}

ื‘ืึทืžืขืจืงื•ื ื’: ื“ืขืจ ื–ื›ึผืจื•ืŸ ื’ืขื ื™ืฆื˜ ื“ื•ืจืš ื“ื™ ืึทืจื™ื™ึทื ืฉืจื™ื™ึทื‘ ืฉื˜ืจื™ืงืœ ืื™ื– 47 * 8 = 376 ื‘ื™ืฅ ืื•ืŸ ื“ื™ ืขื ืงืึธื•ื“ื™ื“ ืฉื˜ืจื™ืงืœ ื ืขืžื˜ ื‘ืœื•ื™ื– 194 ื‘ื™ืฅ ื“.ื”. ื“ื™ ื“ืึทื˜ืŸ ื–ืขื ืขืŸ ืงืึทืžืคึผืจืขืกื˜ ืžื™ื˜ ื‘ืขืขืจืขืš 48%. ืื™ืŸ ื“ื™ C ++ ืคึผืจืึธื’ืจืึทื ืื•ื™ื‘ืŸ, ืžื™ืจ ื ื•ืฆืŸ ื“ื™ ืฉื˜ืจื™ืงืœ ืงืœืึทืก ืฆื• ืงืจืึธื ืึท ืขื ืงืึธื•ื“ื™ื“ ืฉื˜ืจื™ืงืœ ืฆื• ืžืึทื›ืŸ ื“ื™ ืคึผืจืึธื’ืจืึทื ืœื™ื™ื ืขื•ื•ื“ื™ืง.

ื•ื•ื™ื™ึทืœ ืขืคืขืงื˜ื™ื•ื• ื‘ื™ืœื›ืขืจืงื™ื™ึทื˜ ืจื™ื™ ื“ืึทื˜ืŸ ืกื˜ืจืึทืงื˜ืฉืขืจื– ื“ืึทืจืคืŸ ืคึผืขืจ ื™ื ืกืขืจืฉืึทืŸ ืึธ(ืœืึธื’(ืขืŸ)) ืฆื™ื™ึทื˜, ืื•ืŸ ืื™ืŸ ืึท ื’ืึทื ืฅ ื‘ื™ื™ื ืขืจื™ ื‘ื•ื™ื ืžื™ื˜ N ื‘ืœืขื˜ืขืจ ืคืึธืจืฉื˜ืขืœืŸ ืงืกื ื•ืžืงืกืŸ-ืงืกื ื•ืžืงืก ื ืึธื•ื“ื–, ืื•ืŸ ื“ื™ Huffman ื‘ื•ื™ื ืื™ื– ืึท ื’ืึทื ืฅ ื‘ื™ื™ื ืขืจื™ ื‘ื•ื™ื, ื“ืขืจ ืึทืœื’ืขืจื™ื“ืึทื ืึทืจื‘ืขื˜ ืื™ืŸ O(Nlog(N)) ืฆื™ื™ื˜, ื•ื•ื• N - ืื•ืชื™ื•ืช.

ืงื•ื•ืืœืŸ:

en.wikipedia.org/wiki/Huffman_coding
en.wikipedia.org/wiki/Variable-length_code
www.youtube.com/watch?v=5wRPin4oxCo

ืœืขืจื ืขืŸ ืžืขืจ ื•ื•ืขื’ืŸ ื“ืขื ืงื•ืจืก.

ืžืงื•ืจ: www.habr.com

ืœื™ื™ื’ืŸ ืึท ื‘ืึทืžืขืจืงื•ื ื’