ก่อนเริ่มหลักสูตร
การเข้ารหัส Huffman เป็นอัลกอริทึมการบีบอัดข้อมูลที่กำหนดแนวคิดพื้นฐานของการบีบอัดไฟล์ ในบทความนี้ เราจะพูดถึงการเข้ารหัสความยาวคงที่และแปรผัน โค้ดที่ถอดรหัสได้เฉพาะ กฎคำนำหน้า และการสร้างแผนผัง Huffman
เรารู้ว่าอักขระแต่ละตัวถูกจัดเก็บเป็นลำดับของ 0 และ 1 และใช้เวลาถึง 8 บิต สิ่งนี้เรียกว่าการเข้ารหัสความยาวคงที่เนื่องจากอักขระแต่ละตัวใช้จำนวนบิตคงที่เท่ากันในการจัดเก็บ
สมมติว่าเราได้รับข้อความ เราจะลดจำนวนพื้นที่ที่ต้องใช้ในการจัดเก็บอักขระตัวเดียวได้อย่างไร
แนวคิดหลักคือการเข้ารหัสความยาวผันแปร เราสามารถใช้ข้อเท็จจริงที่ว่าอักขระบางตัวในข้อความเกิดขึ้นบ่อยกว่าอักขระอื่น (
รู้ลำดับของบิตแล้วถอดรหัสได้อย่างไร?
พิจารณาบรรทัด "เอแบคดับ". มีอักขระ 8 ตัว และเมื่อเข้ารหัสความยาวคงที่ จะต้องใช้ 64 บิตในการจัดเก็บ โปรดทราบว่าสัญลักษณ์ความถี่ "ก", "ข", "ค" и "NS" เท่ากับ 4, 2, 1, 1 ตามลำดับ มาลองจินตนาการกัน "เอแบคดับ" บิตน้อยลงโดยใช้ความจริงที่ว่า "ถึง" เกิดขึ้นบ่อยกว่า "NS"และ "NS" เกิดขึ้นบ่อยกว่า "ค" и "NS". เริ่มต้นด้วยการเข้ารหัส "ถึง" โดยหนึ่งบิตเท่ากับ 0 "NS" เราจะกำหนดรหัสสองบิต 11 และใช้สามบิต 100 และ 011 เราจะเข้ารหัส "ค" и "NS".
เป็นผลให้เราได้รับ:
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ซึ่งละเมิดกฎคำนำหน้า ดังนั้น หากรหัสของเราเป็นไปตามกฎคำนำหน้า เราก็สามารถถอดรหัสได้โดยไม่ซ้ำกัน (และในทางกลับกัน)
มาดูตัวอย่างด้านบนกันอีกครั้ง ครั้งนี้เราจะกำหนดให้เป็นสัญลักษณ์ "ก", "ข", "ค" и "NS" รหัสที่เป็นไปตามกฎคำนำหน้า
a
0
b
10
c
110
d
111
ด้วยการเข้ารหัสนี้ สตริง "เอแบคดับ" จะถูกเข้ารหัสเป็น 00100100011010 (0|0|10|0|100|011|0|10). และที่นี่ 00100100011010 เราจะสามารถถอดรหัสและกลับไปใช้สตริงเดิมของเราได้อย่างแจ่มแจ้งแล้ว "เอแบคดับ".
ฮัฟฟ์แมนการเข้ารหัส
ตอนนี้เราได้จัดการกับการเข้ารหัสความยาวผันแปรและกฎคำนำหน้าแล้ว เรามาพูดถึงการเข้ารหัส Huffman กัน
วิธีการนี้ขึ้นอยู่กับการสร้างไบนารีทรี ในนั้นโหนดสามารถเป็นได้ทั้งแบบสุดท้ายหรือแบบภายใน ในขั้นต้นโหนดทั้งหมดถือเป็นใบไม้ (เทอร์มินัล) ซึ่งแสดงถึงสัญลักษณ์และน้ำหนักของมัน (นั่นคือความถี่ของการเกิดขึ้น) โหนดภายในประกอบด้วยน้ำหนักของอักขระและอ้างถึงโหนดที่สืบทอดมาสองโหนด ตามข้อตกลงทั่วไปบิต "0" แสดงถึงตามสาขาด้านซ้ายและ "1" - ทางขวา. อยู่เต็มต้นไม้ N ใบและ N-1 โหนดภายใน ขอแนะนำว่าเมื่อสร้างต้นไม้ Huffman ให้ทิ้งสัญลักษณ์ที่ไม่ได้ใช้เพื่อให้ได้รหัสความยาวที่เหมาะสม
เราจะใช้คิวลำดับความสำคัญเพื่อสร้างทรี Huffman โดยที่โหนดที่มีความถี่ต่ำสุดจะได้รับลำดับความสำคัญสูงสุด ขั้นตอนการก่อสร้างอธิบายไว้ด้านล่าง:
- สร้างโหนดลีฟสำหรับอักขระแต่ละตัวและเพิ่มลงในคิวลำดับความสำคัญ
- ในขณะที่มีมากกว่าหนึ่งแผ่นในคิว ให้ทำดังต่อไปนี้:
- ลบสองโหนดที่มีลำดับความสำคัญสูงสุด (ความถี่ต่ำสุด) ออกจากคิว
- สร้างโหนดภายในใหม่ โดยโหนดทั้งสองนี้จะเป็นโหนดย่อย และความถี่ของการเกิดขึ้นจะเท่ากับผลรวมของความถี่ของโหนดทั้งสองนี้
- เพิ่มโหนดใหม่ในคิวลำดับความสำคัญ
- โหนดเดียวที่เหลืออยู่จะเป็นรูทและสิ่งนี้จะทำให้การสร้างทรีเสร็จสมบูรณ์
ลองนึกภาพว่าเรามีข้อความบางส่วนที่ประกอบด้วยอักขระเท่านั้น "เอบีซีดี" и "และ"และความถี่ที่เกิดขึ้นคือ 15, 7, 6, 6 และ 5 ตามลำดับ ด้านล่างนี้คือภาพประกอบที่แสดงถึงขั้นตอนของอัลกอริทึม
เส้นทางจากรูทไปยังโหนดปลายทางใด ๆ จะจัดเก็บรหัสนำหน้าที่เหมาะสมที่สุด (หรือที่เรียกว่ารหัส 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++ ด้านบน เราใช้คลาสสตริงเพื่อจัดเก็บสตริงที่เข้ารหัสเพื่อทำให้โปรแกรมสามารถอ่านได้
เนื่องจากโครงสร้างข้อมูลคิวลำดับความสำคัญที่มีประสิทธิภาพต้องการการแทรกแต่ละครั้ง O(ล็อก(N)) เวลา แต่ในไบนารีทรีที่สมบูรณ์ด้วย N ใบไม้ปัจจุบัน 2N-1 โหนดและต้นไม้ Huffman เป็นต้นไม้ไบนารีที่สมบูรณ์ จากนั้นอัลกอริทึมจะทำงาน โอ(นล็อก(N)) เวลาที่ไหน N - ตัวละคร
แหล่งที่มา:
ที่มา: will.com