熵编码(entropy encoder)

来源:互联网 发布:南达科他级战列舰数据 编辑:程序博客网 时间:2024/06/07 00:36

Description

An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding. 

English text encoded in ASCII has a high degree of entropy because all characters are encoded using the same number of bits, eight. It is a known fact that the letters E, L, N, R, S and T occur at a considerably higher frequency than do most other letters in english text. If a way could be found to encode just these letters with four bits, then the new encoding would be smaller, would contain all the original information, and would have less entropy. ASCII uses a fixed number of bits for a reason, however: it’s easy, since one is always dealing with a fixed number of bits to represent each possible glyph or character. How would an encoding scheme that used four bits for the above letters be able to distinguish between the four-bit codes and eight-bit codes? This seemingly difficult problem is solved using what is known as a "prefix-free variable-length" encoding. 

In such an encoding, any number of bits can be used to represent any glyph, and glyphs not present in the message are simply not encoded. However, in order to be able to recover the information, no bit pattern that encodes a glyph is allowed to be the prefix of any other encoding bit pattern. This allows the encoded bitstream to be read bit by bit, and whenever a set of bits is encountered that represents a glyph, that glyph can be decoded. If the prefix-free constraint was not enforced, then such a decoding would be impossible. 

Consider the text "AAAAABCD". Using ASCII, encoding this would require 64 bits. If, instead, we encode "A" with the bit pattern "00", "B" with "01", "C" with "10", and "D" with "11" then we can encode this text in only 16 bits; the resulting bit pattern would be "0000000000011011". This is still a fixed-length encoding, however; we’re using two bits per glyph instead of eight. Since the glyph "A" occurs with greater frequency, could we do better by encoding it with fewer bits? In fact we can, but in order to maintain a prefix-free encoding, some of the other bit patterns will become longer than two bits. An optimal encoding is to encode "A" with "0", "B" with "10", "C" with "110", and "D" with "111". (This is clearly not the only optimal encoding, as it is obvious that the encodings for B, C and D could be interchanged freely for any given encoding without increasing the size of the final encoded message.) Using this encoding, the message encodes in only 13 bits to "0000010110111", a compression ratio of 4.9 to 1 (that is, each bit in the final encoded message represents as much information as did 4.9 bits in the original encoding). Read through this bit pattern from left to right and you’ll see that the prefix-free encoding makes it simple to decode this into the original text even though the codes have varying bit lengths. 

As a second example, consider the text "THE CAT IN THE HAT". In this text, the letter "T" and the space character both occur with the highest frequency, so they will clearly have the shortest encoding bit patterns in an optimal encoding. The letters "C", "I’ and "N" only occur once, however, so they will have the longest codes. 

There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with "00", "A" with "100", "C" with "1110", "E" with "1111", "H" with "110", "I" with "1010", "N" with "1011" and "T" with "01". The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1. 

Input

The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output

For each text string in the input, output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

Sample Input

AAAAABCDTHE_CAT_IN_THE_HATEND

Sample Output

64 13 4.9144 51 2.8
超时,可能树浪费了资源
#define _CRT_SECURE_NO_WARNINGS#include<stdlib.h>#include<stdio.h>typedef int ElemType;struct ch{char c;int  n;};typedef struct _BTreeNode{ElemType data;struct _BTreeNode* left;struct _BTreeNode* right;}BTreeNode;//根据数组 a 中 n 个权值建立一棵哈夫曼树,返回树根指针BTreeNode* CreateHuffman(ElemType a[], int n){int i, j;BTreeNode **b, *q=NULL;b = (BTreeNode **)malloc(n * sizeof(BTreeNode));for (i = 0; i < n; i++) //初始化b指针数组,使每个指针元素指向a数组中对应的元素结点{b[i] = (BTreeNode *)malloc(sizeof(BTreeNode));b[i]->data = a[i];b[i]->left = b[i]->right = NULL;}for (i = 1; i < n; i++)//进行 n-1 次循环建立哈夫曼树{//k1表示森林中具有最小权值的树根结点的下标,k2为次最小的下标int k1 = -1, k2;for (j = 0; j < n; j++)//让k1初始指向森林中第一棵树,k2指向第二棵{if (b[j] != NULL && k1 == -1){k1 = j;continue;}if (b[j] != NULL){k2 = j;break;}}for (j = k2; j < n; j++)//从当前森林中求出最小权值树和次最小{if (b[j] != NULL){if (b[j]->data < b[k1]->data){k2 = k1;k1 = j;}else if (b[j]->data < b[k2]->data)k2 = j;}}//由最小权值树和次最小权值树建立一棵新树,q指向树根结点q = (BTreeNode *)malloc(sizeof(BTreeNode));q->data = b[k1]->data + b[k2]->data;q->left = b[k1];q->right = b[k2];b[k1] = q;//将指向新树的指针赋给b指针数组中k1位置b[k2] = NULL;//k2位置为空}free(b); //删除动态建立的数组breturn q; //返回整个哈夫曼树的树根指针}ElemType WeightPathLength(BTreeNode* FBT, int len)//len初始为0{if (FBT == NULL) //空树返回0return 0;else{if (FBT->left == NULL && FBT->right == NULL)//访问到叶子结点return FBT->data * len;else //访问到非叶子结点,进行递归调用,返回左右子树的带权路径长度之和,len递增return WeightPathLength(FBT->left, len + 1) + WeightPathLength(FBT->right, len + 1);}}int main() {while(true){char c;ch k[37];int n = 0;int num = 0;int a = 0;if ((c = getchar()) == 'E')if ((c = getchar()) == 'N')if ((c = getchar()) == 'D')if((c=getchar())=='\n')break;else{num = 3;n = 3;k[0].c = 'E';k[0].n = 1;k[1].c = 'N';k[1].n = 1;k[2].c = 'D';k[2].n = 1;}else{num = 2;n = 2;k[0].c = 'E';k[0].n = 1;k[1].c = 'N';k[1].n = 1;}else{num = 1;n = 1;k[0].c = 'E';k[0].n = 1;}while (true) {if (c == '\n'){int* kk;kk= (int *)malloc(n * sizeof(int));for (int i = 0; i < n; i++) kk[i] = k[i].n;if (n == 1)a = num * 8;else{BTreeNode* fbt;fbt = CreateHuffman(kk, n);a = WeightPathLength(fbt, 0);}float s = (8 * num) / (float)a;printf("%d %d %.1f\n", 8 * num, a, s);break;}num++;int ii;for ( ii= 0; ii < n; ii++) {if (c == k[ii].c) {(k[ii].n)++;break;}}if (ii == n) {k[n].c = c;k[n].n = 1;n++;}c = getchar();}}}

2.0


还是超时

#include<stdlib.h>#include<stdio.h>struct ch{char c;int  n;};int WeightPathLength(int *kk, int num,int n) {int a = 0;int nn = n;int m[2][2] = { {num,-1},{num,-1} };int aa = 0;while (true){for (int j = 0; j < 2; j++) {for (int i = 0; i < n; i++) {if (m[j][0] > kk[i] && kk[i]>0) {if (m[j][1] > -1)kk[m[j][1]] = m[j][0];m[j][0] = kk[i];m[j][1] = i;kk[i] = 0;}}}if (nn == 1)break;aa = m[0][0] + m[1][0];a += aa;kk[m[0][1]] = aa;m[0][0] = num;m[1][0] = num;m[0][1] = -1;m[1][1] = -1;nn--;}return a;}int main() {while (true) {char c;ch k[37];int n = 0;int num = 0;int a = 0;if ((c = getchar()) == 'E')if ((c = getchar()) == 'N')if ((c = getchar()) == 'D')if ((c = getchar()) == '\n')break;else {num = 3;n = 3;k[0].c = 'E';k[0].n = 1;k[1].c = 'N';k[1].n = 1;k[2].c = 'D';k[2].n = 1;}else {num = 2;n = 2;k[0].c = 'E';k[0].n = 1;k[1].c = 'N';k[1].n = 1;}else {num = 1;n = 1;k[0].c = 'E';k[0].n = 1;}while (true) {if (c == '\n') {int* kk;kk = (int *)malloc(n * sizeof(int));for (int i = 0; i < n; i++)kk[i] = k[i].n;if (n == 1)a = num * 8;elsea = WeightPathLength(kk, num,n);float s = (8 * num) / (float)a;printf("%d %d %.1f\n", 8 * num, a, s);break;}num++;int ii;for (ii = 0; ii < n; ii++) {if (c == k[ii].c) {(k[ii].n)++;break;}}if (ii == n) {k[n].c = c;k[n].n = 1;n++;}c = getchar();}}}

3.0

还是C++方便一点直接实现top

#include<cstring>#include<cstdio>#include<queue>#include<functional>using namespace std;char s[201];int c[300];priority_queue<int, vector<int>, greater<int> >Q;int main(){int i, len, sum;while (gets(s)){if (!strcmp(s, "END"))break;len = strlen(s);for (i = 0; i<len; i++)c[s[i]]++;//c[]数组声请足够大的空间,保证所有字符的ASCII值不越界for (i = 0; i<200; i++){if (c[i])Q.push(c[i]);//将字符的个数存放在优先队列中c[i] = 0;//将该字符的个数清零;}sum = 0;//实现huffman编码//(控制Q.size()>1,优先队列中剩余一个节点时,huffman编码完成)while (Q.size()>1){int a = Q.top();//弹出两个权值最小的结点Q.pop();//抛弃该节点int b = Q.top();Q.pop();sum += a + b;Q.push(a + b);//新节点重新进入队列}if (sum == 0)//特殊情况,sum==0,只有一个字符时sum = len;while (!Q.empty())//记得把队列制空Q.pop();printf("%d %d %.1f\n", 8 * len, sum, double((double)8 * len / sum));}return 0;}