20
Algorithm Programming 1 89-210 Some Topics in Compression Bar-Ilan University 2006-2007 זזז"זby Moshe Fresko

Algorithm Programming 1 89-210 Some Topics in Compression

  • Upload
    ivria

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

Algorithm Programming 1 89-210 Some Topics in Compression. Bar-Ilan University 2006-2007 תשס"ז by Moshe Fresko. Huffman Coding. Variable-length encoding Works on probabilities of symbols (characters, words, etc.) Build a tree Get two least frequent symbols/nodes - PowerPoint PPT Presentation

Citation preview

Page 1: Algorithm Programming 1 89-210 Some Topics in Compression

Algorithm Programming 189-210Some Topics in Compression

Bar-Ilan University

תשס"ז 2006-2007

by Moshe Fresko

Page 2: Algorithm Programming 1 89-210 Some Topics in Compression

Huffman Coding Variable-length encoding Works on probabilities of symbols (characters,

words, etc.) Build a tree

Get two least frequent symbols/nodes Join them into a parent node Parent node’s frequency is sum of child nodes’ Continue until the tree contains all nodes and symbols The path of a leaf indicates its code

Frequent symbols are near the root giving them short codes

Page 3: Algorithm Programming 1 89-210 Some Topics in Compression

LZ77 Introduced in 1977 by Abraham Lempel and Jacob

Ziv Dictionary based Works in a window size n Decoding is easy and fast (but not Encoding) Produces a list of tuples (Pos,Len,C)

Pos : Position backwards from the current position Len : Number of symbols to be taken C : Next character

Page 4: Algorithm Programming 1 89-210 Some Topics in Compression

LZ77 Based on strings that repeat themselves

An outcry in Spain is an outcry in vain

An outcry in Spa(6,3)is a(22,12)v(21,3)

aaaaaaaaaa

a(1,9)

Page 5: Algorithm Programming 1 89-210 Some Topics in Compression

LZ77 - Example Window size : 5 ABBABCABBBBC

NextSeq CodeA (0,0,A)

B (0,0,B)

BA (1,1,A)

BC (3,1,C)

ABB (3,2,B)

BBC (2,2,C)

Page 6: Algorithm Programming 1 89-210 Some Topics in Compression

LZ77 - Some Variations LZSS - A flag bit for distinguishing pointers

from the other items. LZR - No limit on the pointer size. LZH - Compress the pointers in Huffman

coding.

Page 7: Algorithm Programming 1 89-210 Some Topics in Compression

LZ78 Instead of a window to previously seen text, a

dictionary of phrases will be build Both encoding and decoding are simple

From the current position in the text, find the longest phrase that is found in the dictionary

Output the pair (Index,NextChar) Index : The dictionary phrase of that index NextChar : The next character after that phrase

Add to the dictionary the new phrase by appending the next character

Page 8: Algorithm Programming 1 89-210 Some Topics in Compression

LZ78 - Example ABBABCABBBBC

Input Output Add to dictionaryA (0,A) 1 = “A”B (0,B) 2 = “B”BA (2,A) 3 = “BA”BC (2,C) 4 = “BC”AB (1,B) 5 = “AB”BB (2,B) 6 = “BB”BC (4,EOLN)

Dictionary size

Page 9: Algorithm Programming 1 89-210 Some Topics in Compression

LZW Produces only a list of dictionary entry indexes Encoding

1. Starts with initial dictionary For example, possible ascii characters (0..255)

2. From the input, find the longest string that exists in the dictionary

3. Output this string’s index in the dictionary

4. Append the next character in the input to that string and add it into the dictionary

5. Continue from that character on from (2)

Page 10: Algorithm Programming 1 89-210 Some Topics in Compression

LZW - Example ABBABCABBBBC

Initial dictionary 0=“A”, 1=“B”, 2=“C”Input NextChar Output Add to dictionaryA B 0 3 = “AB”B B 1 4 = “BB”B A 1 5 = “BA”AB C 3 6 = “ABC”C A 2 7 = “CA”AB B 3 8 = “ABB”BB B 4 9 = “BBB”B C 1 10 = “BC”C - 2 -

Dictionary size : ?

Page 11: Algorithm Programming 1 89-210 Some Topics in Compression

LZW – Encoding Example T=ababcbababaaaaaaa

Initial Dictionary Entries :1=a 2=b 3=c

Input Output NextSymbol Add To Dictionarya 1 b 4 = abb 2 a 5 = baab 4 c 6 = abcc 3 b 7 = cbba 5 b 8 = babbab 8 a 9 = babaa 1 a 10 = aaaa 10 a 11 = aaaaaa 11 a 12= aaaaa 1 - -

Page 12: Algorithm Programming 1 89-210 Some Topics in Compression

LZW – Encoding Algorithmw = Emptywhile ( read next symbol k ) {

if wk exists in the dictionary w = wk

else add wk to the dictionary; output the code for w; w = k;

}

Page 13: Algorithm Programming 1 89-210 Some Topics in Compression

LZW – Decoding Algorithmread a code koutput dictionary entry for kw = kwhile ( read a code k ) {

entry = dictionary entry for koutput entryadd w + entry[0] to dictionaryw = entry

}

Page 14: Algorithm Programming 1 89-210 Some Topics in Compression

LZW – Decoding There is a special case problem with the previous

algorithm It can be confronted on every decoding process of a big file It is the case where the index number read is not in the

dictionary yet Example : ABABABA Initially : A=1,B=2 Output=1 2 3 5 In decoding above algorithm will not find the dictionary

entry ABA=5 An additional small check will solve the problem

Be careful to do it in the Exercise 3

Page 15: Algorithm Programming 1 89-210 Some Topics in Compression

LZW – Dictionary Length Dictionary length

Typically : 14 bits = 16384 entries (first 256 of them are single bytes)

What if we are out of dictionary length1. Don’t add to the dictionary any more

2. Delete the whole dictionary (This will be used in the exercise)

3. LRU : Throw those that are not used recently

4. Monitor performance, and flush dictionary when the performance is poor.

5. Double the dictionary size

Page 16: Algorithm Programming 1 89-210 Some Topics in Compression

Exercise 3 – Compression Algorithms

1. Define an interface “Compression” in “Compression.java” as:interface Compression {

void compress(InputStream is, OutputStream os) throws IOException ;void decompress(InputStream is, OutputStream os) throws

IOException ;void stop() ;

}// Note that these definitions are as general as possible to let flexibility in the input and output media chosen.

2. Define the LZW compression algorithm in file LZW.java

3. This class must implement the above Compress interface.

Page 17: Algorithm Programming 1 89-210 Some Topics in Compression

Exercise 3 – Compression Algorithms1. Write an application program “main()” in “LZW.java” that will run in

the following form java LZW compress/decompress InputFile OutputFileIt will run the given Algorithm for compressing/decompressing the given input file into the given output file.

2. Example:

java LZW compress MyTextFile.txt MySmallFile.lzw

will compress MyTextFile.txt into MySmallFile.lzw

java LZW decompress MySmallFile.lzw MyNewTextFile.txt

will uncompress the given file into a new file

Page 18: Algorithm Programming 1 89-210 Some Topics in Compression

Exercise 3 – Compression Algorithms You can use the following two classes to keep a dynamic list and an associative array

ArrayList (with interface List) HashMap (with interface Map)

Example:// For keeping a dynamic list

List myList = new ArrayList() ;// For keeping an associative array

Map myMap = new HashMap() ; List

boolean add(Object) int size() Object get(int)

Map Object put(Object key,Object value) boolean containsKey(Object key) Object get(Object key)

For more information, look at the Java API specification page.

Page 19: Algorithm Programming 1 89-210 Some Topics in Compression

Exercise 3 – Compression Algorithms Algorithm Parameters:

LZW: The maximal initial Dictionary size is 512 entries which requires 9 bits for writing. First 256 entries are the ascii characters.

After reaching the end of the dictionary it will double the dictionary size. Which means from that point on the representation of an entry will be 10 bits and will be a dictionary of max size 1024.

This will continue until 14 bits. After reaching 214 entries, it will clean the dictionary by starting from the beginning with 9-bit represenations.

Please take the two numbers 9 and 14 (start Bit Count, last Bit Count) as a parameter in the constructor. The default constructor will start them with 9 and 16. LZW() { this(9,14); } LZW(int startNumOfBits, int endNumOfBits) { /*Initialization*/ }

You have to use BitInputStream/BitOutputStream from the 1st Exercise to write the bits into an output or to read from input.

Page 20: Algorithm Programming 1 89-210 Some Topics in Compression

Exercise # 2Compression Algorithms Important:

You submit via submitex. Course number: 89-210 Exercise: ex2 Two files will be delivered. Compression.java and LZW.java

It must compile/work under Java 1.4/1.5. You can try it on the Unix environment (on Sunshine).

Do not write debugging information to the console or any other place.

Write enough comments. Write wherever you think there is a need for the understanding of the code.

Write your code according to the OOP principles. Everybody must do it alone.

Deadline: 14 Dec 2006