பாடநெறி தொடங்கும் முன்
ஹஃப்மேன் கோடிங் என்பது ஒரு தரவு சுருக்க வழிமுறையாகும், இது கோப்பு சுருக்கத்தின் அடிப்படை யோசனையை உருவாக்குகிறது. இந்தக் கட்டுரையில், நிலையான மற்றும் மாறி நீளக் குறியாக்கம், தனித்துவமாக நீக்கக்கூடிய குறியீடுகள், முன்னொட்டு விதிகள் மற்றும் ஹஃப்மேன் மரத்தை உருவாக்குதல் பற்றிப் பேசுவோம்.
ஒவ்வொரு எழுத்தும் 0 மற்றும் 1 இன் வரிசையாக சேமிக்கப்பட்டு 8 பிட்களை எடுக்கும் என்பதை நாம் அறிவோம். இது நிலையான நீள குறியாக்கம் என்று அழைக்கப்படுகிறது, ஏனெனில் ஒவ்வொரு எழுத்தும் ஒரே நிலையான எண்ணிக்கையிலான பிட்களை சேமிக்க பயன்படுத்துகிறது.
எங்களுக்கு உரை வழங்கப்பட்டுள்ளது என்று வைத்துக்கொள்வோம். ஒரு எழுத்தை சேமிக்க தேவையான இடத்தை எப்படி குறைக்கலாம்?
முக்கிய யோசனை மாறி நீளம் குறியாக்கம் ஆகும். உரையில் உள்ள சில எழுத்துக்கள் மற்றவர்களை விட அடிக்கடி நிகழ்கின்றன என்ற உண்மையைப் பயன்படுத்தலாம் (
எப்படி, பிட்களின் வரிசையை அறிந்து, அதை சந்தேகத்திற்கு இடமின்றி டிகோட் செய்வது?
வரியைக் கவனியுங்கள் "அபாக்டாப்". இது 8 எழுத்துக்களைக் கொண்டுள்ளது, மேலும் ஒரு நிலையான நீளத்தை குறியாக்கம் செய்யும் போது, அதைச் சேமிக்க 64 பிட்கள் தேவைப்படும். குறியீட்டின் அதிர்வெண் என்பதை நினைவில் கொள்க "a", "b", "c" и "டி" முறையே 4, 2, 1, 1 க்கு சமம். கற்பனை செய்ய முயற்சிப்போம் "அபாக்டாப்" குறைவான பிட்கள், என்ற உண்மையைப் பயன்படுத்தி "to" விட அடிக்கடி நிகழ்கிறது "பி"மற்றும் "பி" விட அடிக்கடி நிகழ்கிறது "சி" и "டி". குறியீட்டு முறை மூலம் தொடங்குவோம் "to" 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", "b", "c" и "டி" முன்னொட்டு விதியை பூர்த்தி செய்யும் குறியீடுகள்.
a
0
b
10
c
110
d
111
இந்த குறியாக்கத்துடன், சரம் "அபாக்டாப்" என குறியிடப்படும் 00100100011010 (0|0|10|0|100|011|0|10). இங்கு 00100100011010 எங்களால் ஏற்கனவே சந்தேகத்திற்கு இடமின்றி டிகோட் செய்து எங்களின் அசல் சரத்திற்கு திரும்ப முடியும் "அபாக்டாப்".
ஹஃப்மேன் குறியீட்டு முறை
இப்போது நாம் மாறி நீள குறியாக்கம் மற்றும் முன்னொட்டு விதியைக் கையாண்டுள்ளோம், ஹஃப்மேன் குறியாக்கத்தைப் பற்றி பேசலாம்.
இந்த முறை பைனரி மரங்களை உருவாக்குவதை அடிப்படையாகக் கொண்டது. அதில், முனை இறுதி அல்லது உட்புறமாக இருக்கலாம். ஆரம்பத்தில், அனைத்து முனைகளும் இலைகளாக (டெர்மினல்கள்) கருதப்படுகின்றன, அவை சின்னத்தையும் அதன் எடையையும் குறிக்கின்றன (அதாவது நிகழ்வின் அதிர்வெண்). உள் முனைகள் பாத்திரத்தின் எடையைக் கொண்டிருக்கின்றன மற்றும் இரண்டு சந்ததி முனைகளைக் குறிக்கின்றன. பொது உடன்படிக்கை மூலம், பிட் "0" இடது கிளையைப் பின்பற்றுவதைக் குறிக்கிறது, மற்றும் "1" - வலப்பக்கம். முழு மரத்தில் N இலைகள் மற்றும் என்-1 உள் முனைகள். ஹஃப்மேன் மரத்தை உருவாக்கும்போது, உகந்த நீளக் குறியீடுகளைப் பெற பயன்படுத்தப்படாத சின்னங்களை நிராகரிக்க பரிந்துரைக்கப்படுகிறது.
ஹஃப்மேன் மரத்தை உருவாக்க முன்னுரிமை வரிசையைப் பயன்படுத்துவோம், அங்கு குறைந்த அதிர்வெண் கொண்ட முனைக்கு அதிக முன்னுரிமை வழங்கப்படும். கட்டுமானப் படிகள் கீழே விவரிக்கப்பட்டுள்ளன:
- ஒவ்வொரு எழுத்துக்கும் ஒரு இலை முனையை உருவாக்கி அவற்றை முன்னுரிமை வரிசையில் சேர்க்கவும்.
- வரிசையில் ஒன்றுக்கும் மேற்பட்ட தாள்கள் இருக்கும்போது, பின்வருவனவற்றைச் செய்யுங்கள்:
- வரிசையில் இருந்து அதிக முன்னுரிமை (குறைந்த அதிர்வெண்) கொண்ட இரண்டு முனைகளை அகற்றவும்;
- ஒரு புதிய உள் முனையை உருவாக்கவும், அங்கு இந்த இரண்டு முனைகளும் குழந்தைகளாக இருக்கும், மேலும் நிகழ்வின் அதிர்வெண் இந்த இரண்டு முனைகளின் அதிர்வெண்களின் கூட்டுத்தொகைக்கு சமமாக இருக்கும்.
- முன்னுரிமை வரிசையில் புதிய முனையைச் சேர்க்கவும்.
- மீதமுள்ள ஒரே முனை வேராக இருக்கும், மேலும் இது மரத்தின் கட்டுமானத்தை நிறைவு செய்யும்.
எங்களிடம் சில எழுத்துக்கள் மட்டுமே உள்ளன என்று கற்பனை செய்து பாருங்கள் "ஏ பி சி டி" и "மற்றும்", மற்றும் அவற்றின் நிகழ்வு அதிர்வெண்கள் முறையே 15, 7, 6, 6 மற்றும் 5 ஆகும். அல்காரிதத்தின் படிகளை பிரதிபலிக்கும் விளக்கப்படங்கள் கீழே உள்ளன.
ரூட்டிலிருந்து எந்த முடிவு முனைக்கும் செல்லும் பாதையானது, அந்த இறுதி முனையுடன் தொடர்புடைய எழுத்துடன் தொடர்புடைய உகந்த முன்னொட்டுக் குறியீட்டை (ஹஃப்மேன் குறியீடு என்றும் அழைக்கப்படுகிறது) சேமிக்கும்.
ஹஃப்மேன் மரம்
சி++ மற்றும் ஜாவாவில் ஹஃப்மேன் சுருக்க அல்காரிதம் செயல்படுத்துவதை கீழே காணலாம்:
#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(log(N)) நேரம், ஆனால் ஒரு முழுமையான பைனரி மரத்தில் N தற்போது இலைகள் 2N-1 முனைகள், மற்றும் ஹஃப்மேன் மரம் ஒரு முழுமையான பைனரி மரம், பின்னர் வழிமுறை இயங்குகிறது O(Nlog(N)) நேரம், எங்கே N - பாத்திரங்கள்.
ஆதாரங்கள்:
ஆதாரம்: www.habr.com