Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Strings CopyWrite D.Bockus. 2 Strings Def: A string is a sequence (possibly empty) of symbols from some alphabet. What do we use strings for? 1) Text.

Similar presentations


Presentation on theme: "1 Strings CopyWrite D.Bockus. 2 Strings Def: A string is a sequence (possibly empty) of symbols from some alphabet. What do we use strings for? 1) Text."— Presentation transcript:

1 1 Strings CopyWrite D.Bockus

2 2 Strings Def: A string is a sequence (possibly empty) of symbols from some alphabet. What do we use strings for? 1) Text processing. Word processing. 2) Grammatical Structure of Languages. 3) Searching, String Sequences

3 3 String Example 1 E.g. Java's "for" statement. (simplistic view) for (initialization ; condition; increment) u v w –Where a “for” statement breaks down into ‘for (u;v;w)’. –We can then define each part: u » identifier = constant v » identifier relational_operator value w » identifier++ In this context we can also define a while loop as: –while(v) Deterministic Context Free Languages (programming languages) are defined by breaking rules down into sub-rules, etc.

4 4 Strings Example 2 –Genetic Coding: –aab cd aab d  Searching for, and matching codes, leads into graph theory. a b b b a a a b b a a c c c d d d a b f g a b f g b b d s1 s2s3 s4

5 5 String Example 3 Compression Converting a large volume of symbols into a smaller format. –Huffman Coding –LZW compression.

6 6 Basics  Given a string v:  The length of v can be expressed: 1) |v| = magnitude of v 2) length (v). –Empty strings v = ' ' or v =  There are 5 common operations that may be performed on strings. –Insertion, Deletion, Substitution, Concatenation, Comparison.

7 7 Insertion & Deletion Insertion k = ac where a = (a 1, a 2.. a m ) c = (c 1, c 2.. c n ) insert b = (b 1, b 2... b p ) between ac  k = abc = a 1, a 2 … a m, b 1, b 2 … b p, c 1, c 2 … c n |k| = m + p + n Deletion k = abc delete c k = ab

8 8 Substitution k =  u  where  &  maybe null, i.e. |  | = 0 or |  | = 0 Search for u & replace with v.  k =  v   Notice this same operation can be accomplished with a deletion and insertion. k =  u  Delete u k =   Insert v k =  v  Note: |u| does not have to equal |v|;  |k| before does not have to equal |k| after.

9 9 Concatination  This is the joining of 2 strings a & b. c = a + b So if a = (a 1, a 2.. a m ) & b = (b 1, b 2... b n ) Then c = (a 1, a 2.. a m, b 1, b 2... b n ) –Note: concatenation may be performed with insertion, i.e. insert b at end of a, or substitution. a  where  is null. substitute  for b. |c| = m + n

10 10 Comparison –Compare a & b to see if one of the following is true. 1)a < b 2)a = b 3)a > b 1) a is less then b if a lexicographical comparison is performed on each element of a & b. Until the first a k < b k is true. ab a 1 b 1 a 1 = b 1 a 2 b 2 a 2 = b 2 a 3 b 3 a 3 < b 3 a 4 b 4 a 4 = b 4 b 5 (a 5 =  ) < b 5 a3 < b3 So, a < b

11 11 Comparison Cont... –Note: a 3 < b 3 is the first instance where an element in a differs from b. –  a < b. –If a 3 = b 3 then a is still less then b because |a| < |b|. Can think of  having a value of -  for comparison purposes. 2). For a = b the following must be true. |a| = |b| –and a k = b k  k 3). a > b, opposite of (1). –Or b < a

12 12 String Representations Consider the string "L1 CMPR BANANAS WATERMELLONS 12” There are 6 ways to represent strings in storage noting that 3 criteria must be kept in mind –Storage Efficiency (1:1 packing ratio) –Ease of Lookup (Searching) –Ease of Modification Insertion Deletion

13 13 Fixed Length Strings –Adv: Ease of Modification –Dis: Storage Efficiency due of wasted space at end of short strings.

14 14 Var Strings –Adv: Easier to look up strings, we already have the length. –Dis: Still wastes space.

15 15 Count Delimited –Adv: Very efficient in space usage, Lookup is not bad. –Dis: Modification is hard, Replacing a string must be same length or readjustment of array is needed.

16 16 Indexed List –Adv: Good Storage and Search capabilities –Dis: Modification is poor Strategies include: always adding new strings and never reclaiming space except during a repack.

17 17 Linked List –Adv: Modification is simple pointer manipulation. –Dis: Storage overhead. one address per character –Note: Lookup can be improved by adding additional length field to table or by imploring a hash function.

18 18 Blocked Linked List –Adv: Better storage then linked list. More characters per node –Note: A trade off between dealing with single characters and blocks of characters during modification. Note: If modification is not required then methods such as indexed lists are quite useful. Applications include symbol tables in compilers.

19 19 Implementation –In most cases a variable length string structure is desirable. i.e. the most versatile. –Consider a string type as: String { int size; char data[]; } –Java declares string objects with methods to determine length and other attributes. –Declaring Variables: String S1, S2

20 20 Basic Functions s.length(); -- Returns the length of S1 Other Usefull functions –String s.concat(String t); –String new String(s); –String s.substring(int i); –int indexOf(String t, int index); See Java api.

21 21 Variable Length Coding

22 22 Huffman Coding Algorithm 1) Collect a history of the frequencies of the characters i.e. determine the probabilities. 2) Arrange the characters in an ordered list (priority queue) based on increasing probabilities (frequency) 3) While (More then 1 node in List) Do i) Remove first 2 Nodes ii) Combine into a tree and have the tree root represent the sum of the frequencies of the children iii) Insert into List maintaining proper List order

23 23 Variable Length Coding New Tree

24 24 LZW Compression Lempel-Ziv Welch (LZW) Uses a method of finding the largest known prefix in a character string. Typical uses. –LossLess –Compressed file can be reconstructed without data loss GIF, TIFF zip & unzip

25 25 LZW Compression Idea is to build a code table, where codes are added as they are discovered. Look at the prefix for a given character.

26 26 Compressor Pseudo Code http://marknelson.us/1989/10/01/lzw-data-compression/ STRING = get input character WHILE there are still input characters DO CHARACTER = get input character IF STRING+CHARACTER is in the string table then STRING = STRING+character ELSE output the code for STRING add STRING+CHARACTER to the string table STRING = CHARACTER END of IF END of WHILE

27 27 DeCompressor Pseudo Code http://marknelson.us/1989/10/01/lzw-data-compression/ Read OLD_CODE output OLD_CODE CHARACTER = OLD_CODE WHILE there are still input characters DO Read NEW_CODE IF NEW_CODE is not in the translation table THEN STRING = get translation of OLD_CODE STRING = STRING+CHARACTER ELSE STRING = get translation of NEW_CODE END of IF output STRING CHARACTER = first character in STRING add OLD_CODE + CHARACTER to the translation table OLD_CODE = NEW_CODE END of WHILE

28 28 Compressor Example Assume we have an alphabet of a and b. We start by building a code book initialized to all characters in the alphabet, in this case a and b. We can now compress the string: a a a b b b b b b a a b a a b a CodeString 2 3 4 5 6 7 8 9 0 1 a b

29 29 Compressor Example … a a a b b b b b b a a b a a b a CodeString 2 3 4 5 6 7 8 9 0 1 a b Output Code 0 Find largest prefix in code book a Add code + next char to code book Find largest prefix in code book 2 Add code + next char to code book a a b Find largest prefix in code book 1 Add code + next char to code book b Find largest prefix in code book 4 Add code + next char to code book b b b Find largest prefix in code book 5 Add code + next char to code book b b b a Find largest prefix in code book 3 a a b a Add code + next char to code bookFind largest prefix in code book 7 No more input to compress so stop

30 30 De-compressor Example We have an encoded string. 0 2 1 4 5 3 7 To decode we need two things, –knowledge of the alphabet. –An initialized code book based on the alphabet. Headers on say GIF files contain the alphabet information. The code book is re-build during de-compression CodeString 2 3 4 5 6 7 8 9 0 1 a b

31 31 De-compressor Example.. During De-compression a code is read and an attempt is made to find this code in the code book. There are two cases: –The code is found in the code book. –The code is not found in the code book. Code found: –output the string from found code. –make an entry based on: previous string + firstChar of current string. Not found: –make an entry into the code book based on: previous string + firstChar of previous string. –output the string of new entry.

32 32 De-compressor Example... Notice that a code which is not found is a special case: E.g. during compression of a a a b b b …. –a is coded to 0, but the compressor now enters aa into the code book. –aa is the next code to be used. –During de-compression, we can guess at this code. –Text(previous) + FC(previous).

33 33 De-compressor Example…. More formally: –We encounter a string P[…]P[…]PQ. –If P[…] is in the code book and P[…]P is not, then the compressor outputs P[…] and adds P[…]P to the code book. –When the de-compressor sees P[…]P it will not of added this code yet. –We know from the pattern that P[…] is already in the code book and it was the last code encountered, and that P[…]P would normally be added next (during compression). –So…. We can accurately guess and enter P[…]P into the code book. Taken from: http://www.danbbs.dk/~dino/whirlgif/lzw.html

34 34 De-compressor Example…. CodeString 2 3 4 5 6 7 8 9 0 1 a b 0 2 1 4 5 3 7 Output Text a Found - No code book entry is made for first code a Not Found - Enter Text(previous) + FC(Previous). Output last code entered into code book a Found - Enter Text(previous) + FC(current). b a a b Not Found - Enter Text(previous) + FC(Previous). b Output last code entered into code book b Not Found - Enter Text(previous) + FC(Previous). b b b Output last code entered into code book b b b Found - Enter Text(previous) + FC(current). a a b b b b a Not Found - Enter Text(previous) + FC(Previous). a a b a Output last code entered into code book a a b a No more code to de-compress - STOP

35 35 Links http://www.cs.sfu.ca/cs/CC/365/li/squeeze/ Squeeze Page - Applets dealing with compression Algorithms http://www.geocities.com/yccheok/lzw/lzw.html

36 36 Finite State Machine for KMP Pattern 1010110 0 1 1 0 1 2 3 4 5 6 7 0 0 0 0 1 1 1 0 1 1 1 0 0


Download ppt "1 Strings CopyWrite D.Bockus. 2 Strings Def: A string is a sequence (possibly empty) of symbols from some alphabet. What do we use strings for? 1) Text."

Similar presentations


Ads by Google