香农-范诺算法(Shannon-Fano coding)原理

       和Huffman-Tree一样,Shannon-Fano coding也是用一棵二叉树对字符进行编码。但在实际操作中呢,Shannon-Fano却没有大用处,这是由于它与Huffman coding相比,编码效率较低的结果(或者说香农-范诺算法的编码平均码字较大)。但是它的基本思路我们还是可以参考下的。


根据Wikipedia上面的解释,我们来看下香农范诺算法的原理:

Shannon-Fano的树是根据旨在定义一个有效的代码表的规范而建立的。实际的算法很简单:

  1. 对于一个给定的符号列表,制定了概率相应的列表或频率计数,使每个符号的相对发生频率是已知。
  2. 排序根据频率的符号列表,最常出现的符号在左边,最少出现的符号在右边。
  3. 清单分为两部分,使左边部分的总频率和尽可能接近右边部分的总频率和。
  4. 该列表的左半边分配二进制数字0,右半边是分配的数字1。这意味着,在第一半符号代都是将所有从0开始,第二半的代码都从1开始。
  5. 对左、右半部分递归应用步骤3和4,细分群体,并添加位的代码,直到每个符号已成为一个相应的代码树的叶。


示例


香农-范诺编码算法

这个例子展示了一组字母的香浓编码结构(如图a所示)这五个可被编码的字母有如下出现次数:

Symbol A B C D E
Count157665
Probabilities0.384615380.179487180.153846150.153846150.12820513

从左到右,所有的符号以它们出现的次数划分。在字母B与C之间划定分割线,得到了左右两组,总次数分别为22,17。 这样就把两组的差别降到最小。通过这样的分割, A与B同时拥有了一个以0为开头的码字, C,D,E的码子则为1,如图b所示。 随后, 在树的左半边,于A,B间建立新的分割线,这样A就成为了码字为00的叶子节点,B的码子01。经过四次分割, 得到了一个树形编码。 如下表所示,在最终得到的树中, 拥有最大频率的符号被两位编码, 其他两个频率较低的符号被三位编码。

符号 A B C D E
编码000110110111


Entropy(熵,平均码字长度): 


Pseudo-code


 1:  begin
 2:     count source units
 3:     sort source units to non-decreasing order
 4:     SF-SplitS
 5:     output(count of symbols, encoded tree, symbols)
 6:     write output
 7:   end
 8:  
 9:  procedure SF-Split(S)
10:  begin
11:     if (|S|>1) then
12:      begin
13:        divide S to S1 and S2 with about same count of units
14:        add 1 to codes in S1
15:        add 0 to codes in S2
16:        SF-Split(S1)
17:        SF-Split(S2)
18:      end
19:  end
想不清楚的朋友可以看下这个网站的模拟程序,很形象,perfect~






香农-范诺算法实现(Shannon-Fano coding implementation in C++)



我们由上面的算法可知,需要迭代地寻找一个最优点,使得树中每个节点的左右子树频率总和尽可能相近。
这里我寻找最优化点用的是顺次查找法,其实呢,我们还可以用二分法(dichotomy)达到更高的效率~


[cpp]  view plain  copy
  1. /************************************************************************/  
  2. /*  File Name: Shanno-Fano.cpp 
  3. *       @Function: Lossless Compression 
  4. @Author: Sophia Zhang 
  5. @Create Time: 2012-9-26 20:20 
  6. @Last Modify: 2012-9-26 20:57 
  7. */  
  8. /************************************************************************/  
  9.   
  10. #include"iostream"  
  11. #include "queue"  
  12. #include "map"  
  13. #include "string"  
  14. #include "iterator"  
  15. #include "vector"  
  16. #include "algorithm"  
  17. #include "math.h"  
  18. using namespace std;  
  19.   
  20. #define NChar 8 //suppose use 8 bits to describe all symbols  
  21. #define Nsymbols 1<<NChar //can describe 256 symbols totally (include a-z, A-Z)  
  22. #define INF 1<<31-1  
  23.   
  24. typedef vector<bool> SF_Code;//8 bit code of one char  
  25. map<char,SF_Code> SF_Dic; //huffman coding dictionary  
  26. int Sumvec[Nsymbols];   //record the sum of symbol count after sorting  
  27.   
  28. class HTree  
  29. {  
  30. public :  
  31.     HTree* left;  
  32.     HTree* right;  
  33.     char ch;  
  34.     int weight;  
  35.   
  36.     HTree(){left = right = NULL; weight=0;ch ='\0';}  
  37.     HTree(HTree* l,HTree* r,int w,char c){left = l; right = r;  weight=w;   ch=c;}  
  38.     ~HTree(){delete left; delete right;}  
  39.     bool Isleaf(){return !left && !right; }  
  40. };  
  41.   
  42. bool comp(const HTree* t1, const HTree* t2)//function for sorting  
  43. {   return (*t1).weight>(*t2).weight;    }  
  44.   
  45. typedef vector<HTree*> TreeVector;  
  46. TreeVector TreeArr;//record the symbol count array after sorting  
  47.   
  48. void Optimize_Tree(int a,int b,HTree& root)//find optimal separate point and optimize tree recursively  
  49. {  
  50.     if(a==b)//build one leaf node  
  51.     {  
  52.         root = *TreeArr[a-1];  
  53.         return;  
  54.     }  
  55.     else if(b-a==1)//build 2 leaf node  
  56.     {  
  57.         root.left = TreeArr[a-1];  
  58.         root.right=TreeArr[b-1];  
  59.         return;  
  60.     }  
  61.     //find optimizing point x  
  62.     int x,minn=INF,curdiff;  
  63.     for(int i=a;i<b;i++)//find the point that minimize the difference between left and right; this can also be implemented by dichotomy  
  64.     {  
  65.         curdiff = Sumvec[i]*2-Sumvec[a-1]-Sumvec[b];  
  66.         if(abs(curdiff)<minn){  
  67.             x=i;  
  68.             minn = abs(curdiff);  
  69.         }  
  70.         else break;//because this algorithm has monotonicity  
  71.     }  
  72.     HTree*lc = new HTree;   HTree *rc = new HTree;  
  73.     root.left = lc;     root.right = rc;  
  74.     Optimize_Tree(a,x,*lc);  
  75.     Optimize_Tree(x+1,b,*rc);  
  76. }  
  77.   
  78. HTree* BuildTree(int* freqency)//create the tree use Optimize_Tree  
  79. {  
  80.     int i;  
  81.     for(i=0;i<Nsymbols;i++)//statistic  
  82.     {  
  83.         if(freqency[i])  
  84.             TreeArr.push_back(new HTree (NULL,NULL,freqency[i], (char)i));  
  85.     }  
  86.     sort(TreeArr.begin(), TreeArr.end(), comp);  
  87.     memset(Sumvec,0,sizeof(Sumvec));  
  88.     for(i=1;i<=TreeArr.size();i++)  
  89.         Sumvec[i] = Sumvec[i-1]+TreeArr[i-1]->weight;  
  90.     HTree* root = new HTree;  
  91.     Optimize_Tree(1,TreeArr.size(),*root);  
  92.     return root;  
  93. }  
  94.   
  95. /************************************************************************/  
  96. /* Give Shanno Coding to the Shanno Tree 
  97. /*PS: actually, this generative process is same as Huffman coding 
  98. /************************************************************************/  
  99. void Generate_Coding(HTree* root, SF_Code& curcode)  
  100. {  
  101.     if(root->Isleaf())  
  102.     {  
  103.         SF_Dic[root->ch] = curcode;  
  104.         return;  
  105.     }  
  106.     SF_Code lcode = curcode;  
  107.     SF_Code rcode = curcode;  
  108.     lcode.push_back(false);  
  109.     rcode.push_back(true);  
  110.     Generate_Coding(root->left,lcode);  
  111.     Generate_Coding(root->right,rcode);  
  112. }  
  113.   
  114. int main()  
  115. {  
  116.     int freq[Nsymbols] = {0};  
  117.     char *str = "bbbbbbbccccccaaaaaaaaaaaaaaaeeeeedddddd";//15a,7b,6c,6d,5e  
  118.   
  119.     //statistic character frequency  
  120.     while (*str!='\0')      freq[*str++]++;  
  121.   
  122.     //build tree  
  123.     HTree* r = BuildTree(freq);  
  124.     SF_Code nullcode;  
  125.     Generate_Coding(r,nullcode);  
  126.   
  127.     for(map<char,SF_Code>::iterator it = SF_Dic.begin(); it != SF_Dic.end(); it++) {    
  128.         cout<<(*it).first<<'\t';    
  129.         std::copy(it->second.begin(),it->second.end(),std::ostream_iterator<bool>(cout));    
  130.         cout<<endl;    
  131.     }    
  132. }  


Result:

以上面图中的统计数据为例,进行编码。

符号 A B C D E
计数157665




Reference:

  1. Shannon-Fano coding. Wikipedia, the free encyclopedia
  2. Claude Elwood Shannon. Wikipedia, the free encyclopedia.
  3. C. E. Shannon: A Mathematical Theory of Communication. The Bell System Technical Journal, Vol. 27, July, October, 1948.
  4. C. E. Shannon: Prediction and Entropy of Printed English. The Bell System Technical Journal, Vol. 30, 1951.
  5. C. E. Shannon: Communication Theory of Secrecy Systems. The Bell System Technical Journal, Vol. 28, 1949.
  6. http://www.stringology.org/DataCompression/sf/index_en.html
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值