压缩算法实现之LZ78
LZ78编码
LZ78算法,建立词典的算法。
LZ78的编码思想:
不断地从字符流中提取新的缀-符串(String),通俗地理解为新"词条",然后用"代号"也就是码字(Code word)表示这个"词条"。
对字符流的编码就变成了用码字(Code word)去替换字符流(Charstream),生成码字流(Codestream),从而达到压缩数据的目的。
几个约定:
- 字符流(Charstream):要被编码的数据序列。
- 字符(Character):字符流中的基本数据单元。
- 前缀(Prefix): 在一个字符之前的字符序列。
- 缀-符串(String):前缀+字符。
- 码字(Code word):编码以后在码字流中的基本数据单元,代表词典中的一串字符
- 码字流(Codestream): 码字和字符组成的序列,是编码器的输出
- 词典(Dictionary): 缀-符串表。按照词典中的索引号对每条缀-符串(String)指定一个码字(Code word)
- 当前前缀(Current prefix):在编码算法中使用,指当前正在处理的前缀,用符号P表示
- 当前字符(Current character):在编码算法中使用,指当前前缀之后的字符,用符号Char表示。
- 当前码字(Current code word): 在译码算法中使用,指当前处理的码字,用W表示当前码字,String.W表示当前码字的缀-符串。
编码算法步骤:
步骤1: 在开始时,词典和当前前缀P 都是空的。
步骤2: 当前字符Char :=字符流中的下一个字符。
步骤3: 判断P+Char是否在词典中:
(1) 如果"是":用Char扩展P,让P := P+Char ;
(2) 如果"否":① 输出与当前前缀P相对应的码字和当前字符Char;
② 把字符串P+Char 添加到词典中。③ 令P :=空值。
(3) 判断字符流中是否还有字符需要编码
① 如果"是":返回到步骤2。
② 如果"否":若当前前缀P不空,输出相应于当前前缀P的码字,结束编码。
解码算法步骤:
步骤1:在开始时词典为空;
步骤2:当前码字W:= 码字流中的下一个码字
步骤3:当前字符Char:=紧随码字之后的字符
步骤4:把当前码字的缀-符串(string.W)输出到字符流,然后输出字符Char
步骤5:把string.W + Char添加到词典中
步骤6:判断码字流中是否还有码字要译码,
(1)如果有,返回步骤2 (2)如果没有,则结束
代码实现(C#):
- /// <summary>
- /// LZ78编码所需词典
- /// </summary>
- public struct Dictionary
- {
- public int id;
- public string context;
- public Dictionary(int id, string str)
- {
- this.id = id;
- this.context = str;
- }
- }
- /// <summary>
- /// 编码器类
- /// </summary>
- public static class Encoder
- {
- /// <summary>
- /// 词典
- /// </summary>
- static List<Dictionary> D = new List<Dictionary>();
- /// <summary>
- /// 在词典中查找相应串
- /// </summary>
- /// <param name="item"></param>
- /// <param name="D"></param>
- /// <returns></returns>
- static bool Find(string item, List<Dictionary> D)
- {
- foreach (Dictionary d in D)
- if (d.context == item)
- return true;
- return false;
- }
- /// <summary>
- /// 根据词典条目内容查找相应编号
- /// </summary>
- /// <param name="item"></param>
- /// <param name="D"></param>
- /// <returns></returns>
- static int GetDicID(string item, List<Dictionary> D)
- {
- foreach (Dictionary d in D)
- if (d.context == item)
- return d.id;
- return 0;
- }
- /// <summary>
- /// 将一个条目加入词典
- /// </summary>
- /// <param name="item"></param>
- /// <param name="D"></param>
- static void AddToDic(string item, List<Dictionary> D)
- {
- int maxID;
- if (D.Count == 0)
- maxID = 0;
- else
- maxID = D.Last().id;
- D.Add(new Dictionary(maxID + 1, item));
- }
- /// <summary>
- /// 显示词典
- /// </summary>
- public static void ShowDictionary()
- {
- Console.WriteLine("Dictionary:");
- foreach (Dictionary d in D)
- {
- Console.WriteLine("<{0},{1}>", d.id, d.context);
- }
- }
- /// <summary>
- /// 执行LZ78编码算法
- /// </summary>
- /// <param name="str"></param>
- public static void Execute(string str)
- {
- StringBuilder P = new StringBuilder();
- char CHAR;
- P.Clear();
- foreach (char c in str)
- {
- CHAR = c;
- if (Find((P.ToString() + CHAR.ToString()), D))
- P.Append(CHAR);
- else
- {
- Console.Write("({0},{1})", GetDicID(P.ToString(), D), c);
- AddToDic(P.ToString() + c.ToString(), D);
- P.Clear();
- }
- }
- if (P.ToString() != "")
- Console.Write("({0},{1})", GetDicID(P.ToString(), D), "/");
- Console.WriteLine();
- }
- }
- /// <summary>
- /// 解码器类
- /// </summary>
- public static class Decoder
- {
- /// <summary>
- /// 内部类:将码字字符串转换为编码数组
- /// </summary>
- struct Codes
- {
- public int id;
- public char code;
- public Codes(int id, char code)
- {
- this.id = id;
- this.code = code;
- }
- }
- /// <summary>
- /// 词典
- /// </summary>
- static List<Dictionary> D = new List<Dictionary>();
- /// <summary>
- /// 码字流,从字符串中获取
- /// </summary>
- static List<Codes> CodeStream = new List<Codes>();
- /// <summary>
- /// 将码字串变为码字流
- /// </summary>
- /// <param name="str"></param>
- static void BuildCodes(string str)
- {
- /******************
- * stauts 定义:
- * 0: 开始/结束状态
- * 1: 逗号之前
- * 2: 逗号之后
- ******************/
- int status = 0;
- int id = 0;
- char code = (char)0;
- string number = "";
- foreach (char c in str)
- {
- if (c == '(')
- status = 1;
- else if (status == 1 && c != ',')
- number += c;
- else if (c == ',')
- {
- status = 2;
- id = Convert.ToInt32(number);
- number = "";
- }
- else if (status == 2)
- {
- code = c;
- status = 0;
- }
- else if (c == ')')
- CodeStream.Add(new Codes(id, code));
- }
- }
- /// <summary>
- /// 将一个条目加入词典
- /// </summary>
- /// <param name="item"></param>
- /// <param name="D"></param>
- static void AddToDic(string item, List<Dictionary> D)
- {
- int maxID;
- if (D.Count == 0)
- maxID = 0;
- else
- maxID = D.Last().id;
- D.Add(new Dictionary(maxID + 1, item));
- }
- /// <summary>
- /// 根据词典序号找出词典内容
- /// </summary>
- /// <param name="id"></param>
- /// <param name="D"></param>
- /// <returns></returns>
- static string GetContext(int id, List<Dictionary> D)
- {
- foreach (Dictionary d in D)
- {
- if (d.id == id)
- return d.context;
- }
- return string.Empty;
- }
- /// <summary>
- /// 执行LZ78译码算法
- /// </summary>
- /// <param name="str"></param>
- public static void Execute(string str)
- {
- int W;
- char CHAR;
- string original;
- BuildCodes(str);
- foreach (Codes c in CodeStream)
- {
- W = c.id;
- if (c.code != '/')
- CHAR = c.code;
- else CHAR = (char)0;
- if (W == 0)
- {
- Console.Write(CHAR);
- AddToDic(CHAR.ToString(), D);
- }
- else
- {
- original = GetContext(W, D);
- Console.Write(original + CHAR.ToString());
- AddToDic(original + CHAR.ToString(), D);
- }
- }
- Console.WriteLine();
- }
- }
执行效果(主界面程序代码省略):

可见算法执行的结果是完全正确的。
源码下载:http://files.cnblogs.com/ryuasuka/LZ78.rar
压缩算法实现之LZ78的更多相关文章
- ZIP压缩算法详细分析及解压实例解释
最近自己实现了一个ZIP压缩数据的解压程序,觉得有必要把ZIP压缩格式进行一下详细总结,数据压缩是一门通信原理和计算机科学都会涉及到的学科,在通信原理中,一般称为信源编码,在计算机科学里,一般称为数据 ...
- 【数据压缩】LZ78算法原理及实现
在提出基于滑动窗口的LZ77算法后,两位大神Jacob Ziv与Abraham Lempel [1]于1978年又提出了LZ78算法:与LZ77算法不同的是LZ78算法使用树状词典维护历史字符串. [ ...
- 速度之王 — LZ4压缩算法(一)
LZ4 (Extremely Fast Compression algorithm) 项目:http://code.google.com/p/lz4/ 作者:Yann Collet 本文作者:zhan ...
- 为什么倒排索引不采用zlib这样的字典压缩算法——因为没法直接使用啊
看了下压缩算法的发展历史,根据倒排索引的数据结构特点,个人认为zstd不适合做倒排索引压缩,举例说明下: 假设有一份文档倒排列表为:[300, 302, 303, 332],对于这组倒排数据,是没法* ...
- LZ77压缩算法编码原理详解(结合图片和简单代码)
前言 LZ77算法是无损压缩算法,由以色列人Abraham Lempel发表于1977年.LZ77是典型的基于字典的压缩算法,现在很多压缩技术都是基于LZ77.鉴于其在数据压缩领域的地位,本文将结合图 ...
- Java数据结构之对称矩阵的压缩算法---
特殊矩阵 特殊矩阵是指这样一类矩阵,其中有许多值相同的元素或有许多零元素,且值相同的元素或零元素的分布有一定规律.一般采用二维数组来存储矩阵元素.但是,对于特殊矩阵,可以通过找出矩阵中所有值相同元素的 ...
- HBase中的压缩算法比较 GZIP、LZO、Zippy、Snappy [转]
网址: http://www.cnblogs.com/panfeng412/archive/2012/12/24/applications-scenario-summary-of-compressio ...
- LZW压缩算法
转载自http://www.cnblogs.com/jillzhang/archive/2006/11/06/551298.html 记录此处仅自己供学习之用 lzw解压缩算法: 用单个字符初始化字符 ...
- atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php
atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php 1. 压缩算法的归类::: 纯算法,带归档算法 1 2. zlib(适合字符串压缩) 1 3. gz ...
随机推荐
- NOIP2014提高组 DAY1 -SilverN
T1 生活大爆炸版石头剪刀布 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的 ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- codeforces 713A A. Sonya and Queries(状态压缩)
题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...
- UVA 11573 Ocean Currents --BFS+优先队列
采用优先队列做BFS搜索,d[][]数组记录当前点到源点的距离,每次出队时选此时eng最小的出队,能保证最先到达的是eng最小的.而且后来用普通队列试了一下,超时..所以,能用优先队列的,就要用优先队 ...
- TestLink学习一:Windows搭建Apache+MySQL+PHP环境
PHP集成开发环境有很多,如XAMPP.AppServ......只要一键安装就把PHP环境给搭建好了.但这种安装方式不够灵活,软件的自由组合不方便,同时也不利于学习.所以我还是喜欢手工搭建PHP开发 ...
- Studio 从入门到精通 (一)
目标:Android Studio新手–>下载安装配置–>零基础入门–>基本使用–>调试技能–>构建项目基础–>使用AS应对常规应用开发 AS简介 经过2年时间的研 ...
- linux下正向代理/反向代理/透明代理使用说明
代理服务技术对于网站架构部署时非常重要的,一般实现代理技术的方式就是在服务器上安装代理服务软件,让其成为一个代理服务器,从而实现代理技术.常用的代理技术分为正向代理.反向代理和透明代理.以下就是针对这 ...
- 抓包排错-tcp.flags.reset
一 排查思路: 1,了解协议运作过程 2,抓包 最小化原则 对比法 二 案例 微信连wifi问题: 不同地区的微信服务器的地址可能不同. 当出现认证问题: 1,不能跳转,点了按钮没反应 2,打开后 ...
- C语言 详解多级指针与指针类型的关系
//V推论①:指针变量的步长只与‘指针变量的值’的类型有关(指针的值的类型 == 指针指向数据的类型) //指针类型跟指针的值有关,指针是占据4个字节大小的内存空间,但是指针的类型却是各不相同的 // ...
- python数字图像处理(11):图像自动阈值分割
图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素 ...