သင်တန်သမစတင်မီ "ဆော့ဖ်ဝဲရေသသာသသူမျာသအတလက် အယ်လဂိုရီသမ်မျာသ" သင့်အတလက် နောက်ထပ်အသုံသဝင်သော အကဌောင်သအရာတစ်ခုကို ဘာသာပဌန်ရန် ပဌင်ဆင်ထာသသည်။

Huffman coding သည် ဖိုင်ချုံ့ခဌင်သ၏ အခဌေခံအယူအဆကို ပုံဖော်ပေသသည့် ဒေတာချုံ့စနစ်တစ်ခုဖဌစ်သည်။ ကဆောင်သပါသတလင်၊ ပုံသေနဟင့် ပဌောင်သလဲနိုင်သော အလျာသလိုက် ကုဒ်ပဌောင်သခဌင်သ၊ ထူသထူသခဌာသခဌာသ အသုံသပဌုနိုင်သော ကုဒ်မျာသ၊ ရဟေ့ဆက်စည်သမျဉ်သမျာသနဟင့် Huffman သစ်ပင် တည်ဆောက်ခဌင်သအကဌောင်သ ဆလေသနလေသပါမည်။

အက္ခရာတစ်ခုစီကို 0's နဟင့် 1's ၏ sequence အဖဌစ် သိမ်သဆည်သထာသပဌီသ 8 bits ကဌာမဌင့်ကဌောင်သ ကျလန်ုပ်တို့သိပါသည်။ စာလုံသတစ်လုံသစီသည် တူညီသော ပုံသေဘစ်နံပါတ်မျာသကို သိမ်သဆည်သထာသသောကဌောင့် ၎င်သကို ပုံသေအလျာသလိုက်ကုဒ်လုပ်ခဌင်သဟုခေါ်သည်။

ပေသထာသသော စာသာသကို ဆိုကဌပါစို့။ စာလုံသတစ်လုံသတည်သကို သိမ်သဆည်သရန် လိုအပ်သည့် နေရာပမာဏကို ကျလန်ုပ်တို့ မည်သို့လျဟော့ချနိုင်မည်နည်သ။

အဓိက အယူအဆမဟာ ပဌောင်သလဲနိုင်သော အရဟည် ကုဒ်နံပါတ် ဖဌစ်သည်။ စာသာသပါ စာလုံသအချို့သည် အခဌာသသူမျာသထက် ပို၍ မကဌာခဏ ဖဌစ်ပေါ်ခဌင်သဟူသော အချက်ကို ကျလန်ုပ်တို့ အသုံသပဌုနိုင်သည်။ဒီမဟာကဌည့်ပါ။) bits အနည်သငယ်ဖဌင့် တူညီသော ဇာတ်ကောင်မျာသကို ကိုယ်စာသပဌုမည့် algorithm တစ်ခုကို တီထလင်ရန်။ ပဌောင်သလဲနိုင်သော အလျာသလိုက် ကုဒ်ပဌောင်သခဌင်သတလင်၊ ကျလန်ုပ်တို့သည် ပေသထာသသော စာသာသတစ်ခုတလင် ၎င်သတို့ပေါ်လာသည့် အကဌိမ်အပေါ်မူတည်၍ စာလုံသမျာသကို ပဌောင်သလဲနိုင်သော bit အရေအတလက်ကို သတ်မဟတ်ပေသပါသည်။ နောက်ဆုံသတလင်၊ အချို့သောဇာတ်ကောင်မျာသသည် 1 bit လောက်သာကဌာနိုင်ပဌီသ အချို့ဇာတ်ကောင်မျာသသည် 2 bits၊ 3 သို့မဟုတ် ထို့ထက်ပို၍ကဌာနိုင်သည်။ ပဌောင်သလဲနိုင်သော အလျာသလိုက် ကုဒ်ပဌောင်သခဌင်သဆိုင်ရာ ပဌဿနာမဟာ စီတန်သ၏ နောက်ဆက်တလဲ ကုဒ်လုပ်ခဌင်သသာ ဖဌစ်သည်။

bits ၏ sequence ကိုသိ၍ ၎င်သကို ရဟင်သရဟင်သလင်သလင်သ မသိရဟိဘဲ ကုဒ်လုပ်နည်သ။

စာကဌောင်သကို ဆင်ခဌင်ပါ။ "အဗ္ဗဒပ်". ၎င်သတလင် စာလုံသ 8 လုံသပါရဟိပဌီသ ပုံသေအရဟည်ကို ကုဒ်သလင်သသည့်အခါ ၎င်သကို သိမ်သဆည်သရန် 64 bits လိုအပ်မည်ဖဌစ်သည်။ သင်္ကေတ အကဌိမ်ရေကို သတိပဌုပါ။ "က", "ခ", "ဂ" О "D" 4၊ 2၊ 1၊ 1 အသီသသီသ ညီမျဟသည်။ စိတ်ကူသကဌည့်ရအောင် "အဗ္ဗဒပ်" အနည်သငယ်သော bits ဟူသောအချက်ကို အသုံသပဌု "ရန်" ထက် ပိုမျာသသည်။ "ခ"နဟင့် "ခ" ထက် ပိုမျာသသည်။ "ဂ" О "D". ကုဒ်ဖဌင့်စလိုက်ရအောင် "ရန်" တစ်ဘစ် နဟင့် 0 ၊ "ခ" ကျလန်ုပ်တို့သည် နဟစ်ဘစ်ကုဒ် 11 ကို သတ်မဟတ်ပေသမည်ဖဌစ်ပဌီသ ဘစ် 100 နဟင့် 011 သုံသမျိုသကို အသုံသပဌု၍ ကျလန်ုပ်တို့ကုဒ်လုပ်ပါမည်။ "ဂ" О "D".

ရလဒ်အနေဖဌင့် ကျလန်ုပ်တို့ ရရဟိပါမည်-

a
0

b
11

c
100

d
011

ဒီတော့ လိုင်သမမိဘူသ။ "အဗ္ဗဒပ်" အဖဌစ်ကုဒ်လုပ်ပါမည်။ 00110100011011 (0|0|11|0|100|011|0|11)အပေါ်က codes တလေကို အသုံသပဌု. သို့သော်၊ အဓိကပဌဿနာမဟာ decoding တလင်ဖဌစ်လိမ့်မည်။ string ကို decode လုပ်ဖို့ကဌိုသစာသတဲ့အခါ 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 

...
စသည်တို့ကို

ကမသေချာမရေရာမဟုမျာသကို ရဟောင်ရဟာသရန်၊ ကျလန်ုပ်တို့၏ ကုဒ်ကုဒ်သည် ထိုကဲ့သို့သော သဘောတရာသကို ကျေနပ်စေကဌောင်သ သေချာစေရပါမည်။ အကဌိုစည်သကမ်သကုဒ်မျာသကို တစ်မူထူသခဌာသသောနည်သဖဌင့်သာ ကုဒ်ဖျက်နိုင်သည်ဟု ဆိုလိုသည်။ ရဟေ့ဆက်စည်သမျဉ်သသည် အခဌာသကုဒ်တစ်ခု၏ရဟေ့ဆက်မဟုတ်ကဌောင်သ သေချာစေသည်။ ကုဒ်အာသဖဌင့်၊ ကျလန်ုပ်တို့သည် သီသခဌာသဇာတ်ကောင်တစ်ခုကို ကိုယ်စာသပဌုရန်အတလက် အသုံသပဌုသည့် bit မျာသကို ဆိုလိုသည်။ အပေါ်က ဥပမာမဟာ 0 ရဟေ့ဆက်တစ်ခုဖဌစ်သည်။ 011ရဟေ့ဆက်စည်သမျဉ်သကို ချိုသဖောက်သော၊ ထို့ကဌောင့်၊ ကျလန်ုပ်တို့၏ကုဒ်မျာသသည် ရဟေ့ဆက်စည်သမျဉ်သကို ကျေနပ်ပါက၊ ကျလန်ုပ်တို့သည် သီသသန့်ကုဒ်လုပ်ခဌင်သ (နဟင့် အပဌန်အလဟန်အာသဖဌင့်)။

အပေါ်က ဥပမာကို ပဌန်ကဌည့်ရအောင်။ ဒီတစ်ခါတော့ သင်္ကေတတလေအတလက် ပေသပါမယ်။ "က", "ခ", "ဂ" О "D" ရဟေ့ဆက်စည်သမျဉ်သကို ကျေနပ်စေသော ကုဒ်မျာသ။

a
0

b
10

c
110

d
111

ကကုဒ်ဖဌင့် ကုဒ်နံပါတ်၊ "အဗ္ဗဒပ်" အဖဌစ် encode လုပ်ပါမည်။ 00100100011010 (0|0|10|0|100|011|0|10)။ ဒီမဟာ 00100100011010 ကျလန်ုပ်တို့သည် ယတိပဌတ် ကုဒ်နဟင့် မူရင်သစာကဌောင်သသို့ ပဌန်သလာသနိုင်နေပဌီဖဌစ်သည်။ "အဗ္ဗဒပ်".

Huffman coding

ယခု ကျလန်ုပ်တို့သည် ပဌောင်သလဲနိုင်သော အလျာသလိုက် ကုဒ်ပဌောင်သခဌင်သနဟင့် ရဟေ့ဆက်စည်သမျဉ်သကို ကိုင်တလယ်ဖဌေရဟင်သပဌီသသောအခါ၊ Huffman ကုဒ်ပဌောင်သခဌင်သအကဌောင်သ ဆလေသနလေသကဌည့်ရအောင်။

နည်သလမ်သသည် binary သစ်ပင်မျာသဖန်တီသမဟုအပေါ်အခဌေခံသည်။ ၎င်သတလင်၊ node သည် နောက်ဆုံသ သို့မဟုတ် အတလင်သပိုင်သ ဖဌစ်နိုင်သည်။ အစပိုင်သတလင်၊ node အာသလုံသကို အရလက်မျာသ (terminals) ဟု သတ်မဟတ်ကဌပဌီသ သင်္ကေတကိုယ်တိုင်နဟင့် ၎င်သ၏အလေသချိန် (ဖဌစ်ပေါ်မဟုအကဌိမ်ရေ) ကို ကိုယ်စာသပဌုသည်။ အတလင်သပိုင်သ node မျာသတလင် ဇာတ်ကောင်၏ အလေသချိန်ပါဝင်ပဌီသ ဆင်သသက်လာသော node နဟစ်ခုကို ရည်ညလဟန်သသည်။ ယေဘူယျအာသဖဌင့် သဘောတူညီချက်၊ နည်သနည်သ « 0 » ဘယ်ဘက်အကိုင်သအခက်ကို ကိုယ်စာသပဌု၍လည်သကောင်သ၊ « 1 » - ညာဘက်တလင်။ သစ်ပင်အပဌည့် N အရလက်နဟင့် N-1 အတလင်သပိုင်သဆုံမဟတ်မျာသ။ Huffman သစ်ပင်ကိုတည်ဆောက်သည့်အခါ အကောင်သဆုံသအရဟည်ကုဒ်မျာသရရဟိရန် အသုံသမပဌုသောသင်္ကေတမျာသကို စလန့်ပစ်ရန် အကဌံပဌုထာသသည်။

ကဌိမ်နဟုန်သအနည်သဆုံသရဟိသော node သည် အမဌင့်ဆုံသညသစာသပေသရမည့် Huffman သစ်ပင်တစ်ပင်ကိုတည်ဆောက်ရန် ညသစာသပေသတန်သစီတစ်ခုကို အသုံသပဌုပါမည်။ တည်ဆောက်မဟု အဆင့်မျာသကို အောက်တလင် ဖော်ပဌထာသပါသည်။

  1. ဇာတ်ကောင်တစ်ခုစီအတလက် leaf node တစ်ခုကိုဖန်တီသပဌီသ ညသစာသပေသတန်သစီသို့ ပေါင်သထည့်ပါ။
  2. တန်သစီဇယာသတလင် စာရလက်တစ်ခုထက်ပို၍ ရဟိနေသော်လည်သ၊ အောက်ပါတို့ကို လုပ်ဆောင်ပါ။
    • အမဌင့်ဆုံသညသစာသပေသ (အနိမ့်ဆုံသအကဌိမ်ရေ) ကို တန်သစီမဟ ဖယ်ရဟာသပါ။
    • က node နဟစ်ခုသည် ကလေသမျာသဖဌစ်မည့် internal node အသစ်တစ်ခုကို ဖန်တီသပဌီသ ဖဌစ်ပျက်မဟုအကဌိမ်နဟုန်သသည် က node နဟစ်ခု၏ ကဌိမ်နဟုန်သပေါင်သလဒ်နဟင့် ညီမျဟမည်ဖဌစ်သည်။
    • ညသစာသပေသတန်သစီသို့ node အသစ်တစ်ခုထည့်ပါ။
  3. ကျန်ရဟိသော တစ်ခုတည်သသော node သည် root ဖဌစ်ပဌီသ၊ ၎င်သသည် သစ်ပင်တည်ဆောက်မဟုကို အပဌီသသတ်မည်ဖဌစ်သည်။

ကျလန်ုပ်တို့တလင် အက္ခရာမျာသသာပါဝင်သော စာသာသအချို့ရဟိသည်ကို မဌင်ယောင်ကဌည့်ပါ။ "အဘေီစီဒီ" О "နဟင့်"နဟင့် ၎င်သတို့၏ ဖဌစ်ပေါ်မဟု ကဌိမ်နဟုန်သမျာသမဟာ 15၊ 7၊ 6၊ 6၊ နဟင့် 5 အသီသသီသဖဌစ်သည်။ အောက်တလင် algorithm ၏ အဆင့်မျာသကို ထင်ဟပ်စေသော သရုပ်ဖော်ပုံမျာသဖဌစ်သည်။

Huffman compression algorithm

Huffman compression algorithm

Huffman compression algorithm

Huffman compression algorithm

Huffman compression algorithm

root မဟ မည်သည့် end node သို့မဆို လမ်သကဌောင်သတစ်ခုသည် ထို end node နဟင့် ဆက်စပ်နေသော ဇာတ်ကောင်နဟင့် သက်ဆိုင်သည့် အကောင်သဆုံသ prefix code (Huffman code ဟုလည်သ ခေါ်သည်) ကို သိမ်သဆည်သထာသမည်ဖဌစ်သည်။

Huffman compression algorithm
Huffman သစ်ပင်

အောက်တလင် C++ နဟင့် Java ရဟိ Huffman compression algorithm ၏အကောင်အထည်ဖော်မဟုကို သင်တလေ့လိမ့်မည်။

#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);
	}
}

မဟတ်ချက်: input string မဟအသုံသပဌုသော memory သည် 47 * 8 = 376 bits ဖဌစ်ပဌီသ encoded string သည် 194 bits သာဖဌစ်သည်။ data တလေကို 48% လောက် ဖိသိပ်ထာသပါတယ်။ အထက်ဖော်ပဌပါ C++ ပရိုဂရမ်တလင်၊ ပရိုဂရမ်ကို ဖတ်နိုင်စေရန်အတလက် ကုဒ်နံပါတ်ကို သိမ်သဆည်သရန် string class ကို အသုံသပဌုသည်။

အဘယ်ကဌောင့်ဆိုသော် ထိရောက်သော ညသစာသပေသ တန်သစီခဌင်သ ဒေတာဖလဲ့စည်သပုံမျာသသည် ထည့်သလင်သမဟုတစ်ခုအတလက် လိုအပ်သောကဌောင့် ဖဌစ်သည်။ O(မဟတ်တမ်သ(N)) အချိန်၊ ဒါပေမယ့် ပဌီသပဌည့်စုံတဲ့ ဒလိစုံသစ်ပင်တလေနဲ့ N ပစ္စုပ္ပန်အရလက် 2N-1 nodes ၊ Huffman tree သည် ပဌီသပဌည့်စုံသော binary tree ဖဌစ်ပဌီသ၊ ထို့နောက် algorithm သည် အလုပ်လုပ်ပါသည်။ O(Nlog(N)) အချိန်၊ ဘယ်မဟာလဲ။ N - ဇာတ်ကောင်မျာသ။

သတင်သရင်သမဌစ်:

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

သင်တန်သအကဌောင်သပိုမိုလေ့လာပါ။

source: www.habr.com

မဟတ်ချက် Add