1 / 36

Strings

Strings. CopyWrite D.Bockus. 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. String Example 1.

july
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 processing. Word processing. 2) Grammatical Structure of Languages. 3) Searching, String Sequences

  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. 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 d s1 s4 s2 s3

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

  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. Insertion & Deletion • Insertion k = ac where a = (a1, a2 .. am) c = (c1, c2 .. cn) insert b = (b1, b2 ... bp) between ac  k = abc = a1, a2 … am, b1, b2 … bp, c1, c2 … cn |k| = m + p + n • Deletion k = abc delete c k = ab

  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. Concatination • This is the joining of 2 strings a & b. c = a + b So if a = (a1, a2 .. am) & b = (b1, b2 ... bn) Then c = (a1, a2 .. am, b1, b2 ... bn) • 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. 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 ak < bk is true. a b a1 b1 a1 = b1 a2 b2 a2 = b2 a3 b3 a3 < b3 a4 b4 a4 = b4 b5 (a5 = ) < b5 a3 < b3So, a < b

  11. Comparison Cont... • Note: a3 < b3 is the first instance where an element in a differs from b. •  a < b. • If a3 = b3 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 • ak = bkk • 3). a > b, opposite of (1). • Or b < a

  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. Fixed Length Strings • Adv: Ease of Modification • Dis: Storage Efficiency due of wasted space at end of short strings.

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

  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. 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. 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. 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. 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. 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. Variable Length Coding

  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. Variable Length Coding New Tree

  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. 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. Compressor Pseudo Codehttp://marknelson.us/1989/10/01/lzw-data-compression/ STRING = get input characterWHILE 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 IFEND of WHILE

  27. DeCompressor Pseudo Codehttp://marknelson.us/1989/10/01/lzw-data-compression/ Read OLD_CODEoutput OLD_CODECHARACTER = OLD_CODEWHILE 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_CODEEND of WHILE

  28. Code String 0 a 1 b 2 3 4 5 6 7 8 9 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

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

  30. Code String 0 a 1 b 2 3 4 5 6 7 8 9 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

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

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

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

More Related