Chapter 9: String Matching and Compression

Chapter 9: String Matching and Compression · Study Questions
Brute forceKMP and the LPS tableBoyer-Moore and HorspoolRabin-KarpRLELZW and LZ78
Sercan KÜLCÜ · Giresun University · Computer Engineering

Output Question · Brute Force

Question 1: How Many Comparisons?

What value does the lecture's bruteForce method return for the call bruteForce("abracacadabra", "cad"), and how many character comparisons does it make in total?

int bruteForce(String t, String p) {
  int n = t.length(), m = p.length();
  for (int i = 0; i <= n - m; i++) {
    int j = 0;
    while (j < m &&
      t.charAt(i + j) == p.charAt(j))
      j++;
    if (j == m) return i;
  }
  return -1;
}
Show answer

Returns: 6 · comparisons: 11

  • s = 0, 1, 2, 3: the first character (a, b, r, a) ≠ c → 4 × 1 = 4
  • s = 4: c = c, a = a, c ≠ d → 3
  • s = 5: a ≠ c → 1
  • s = 6: c, a, d match → 3; since j == m, 6 is returned
abracacadabra0123456789101112s=4cads=5cads=6cad

4 + 3 + 1 + 3 = 11. On a mismatch the information learned is thrown away; the pattern shifts by just 1 each time.

Multiple Choice · Brute Force

Question 2: Loop Bound Bug

If the condition i <= n - m in bruteForce is mistakenly written as i < n - m, which of the following calls returns a wrong result (−1)?

C: For n = 5, m = 2 the correct shifts are 0..3; the faulty condition tries only 0, 1, 2. "em" occurs only at s = 3 = n − m, so it is not found. "le" is still found because it is at s = 2.

Step-by-Step Trace · Brute Force

Question 3: bruteForce Trace

What value does bruteForce("AABAABAAC", "AAC") return, and how many character comparisons are made?

AABAABAAC012345678AAC

n = 9, m = 3: the valid shifts are i = 0..6. The pattern will be placed under the text at alignment i = 0.

AABAABAAC012345678AAC

i = 0: 2 characters matched, t[2] = 'B' ≠ p[2] = 'C' → the pattern shifts by 1.

AABAABAAC012345678AAC

i = 1: 1 character matched, t[2] = 'B' ≠ p[1] = 'A' → the pattern shifts by 1.

AABAABAAC012345678AAC

i = 2: t[2] = 'B' ≠ p[0] = 'A' → mismatch on the first character, the pattern shifts by 1.

AABAABAAC012345678AAC

i = 3: 2 characters matched, t[5] = 'B' ≠ p[2] = 'C' → the pattern shifts by 1.

AABAABAAC012345678AAC

i = 4: 1 character matched, t[5] = 'B' ≠ p[1] = 'A' → the pattern shifts by 1.

AABAABAAC012345678AAC

i = 5: t[5] = 'B' ≠ p[0] = 'A' → mismatch on the first character, the pattern shifts by 1.

AABAABAAC012345678AAC

i = 6: A, A, C all three matched; when j = 3 = m the while loop ends.

AABAABAAC012345678AAC

j == m → 6 is returned. 15 comparisons in total; the same A's were read again and again at different alignments.

int bruteForce(String t, String p) {
  int n = t.length(), m = p.length();
  for (int i = 0; i <= n - m; i++) {
    int j = 0;
    while (j < m &&
      t.charAt(i + j) == p.charAt(j))
      j++;
    if (j == m) return i;
  }
  return -1;
}

Multiple Choice · KMP

Question 4: LPS Definition

What array does computeLPS("aaaa") return?

A: lps[q] is the length of the longest proper prefix of P[0..q] (one not equal to the string itself) that is also a suffix. For "aaaa" this is "aaa" → 3. B also counts the string itself; D tries only the one-character prefix.

Step-by-Step Trace · KMP

Question 5: computeLPS Trace

What array does computeLPS("ababaca") return?

Pababaca0123456lps0

lps[0] = 0 always (a single character has no proper prefix). k: the length of the prefix matched so far.

Pababaca0123456lps00

p[0] = 'a' ≠ p[1] = 'b' → k stays 0, lps[1] = 0.

Pababaca0123456lps001

p[0] = 'a' = p[2] → k = 1, lps[2] = 1.

Pababaca0123456lps0012

p[1] = 'b' = p[3] → k = 2, lps[3] = 2.

Pababaca0123456lps00123

p[2] = 'a' = p[4] → k = 3, lps[4] = 3.

Pababaca0123456lps00123

p[3] = 'b' ≠ p[5] = 'c' → fall back: k = lps[2] = 1.

Pababaca0123456lps00123

p[1] = 'b' ≠ p[5] = 'c' → fall back: k = lps[0] = 0.

Pababaca0123456lps001230

p[0] = 'a' ≠ p[5] = 'c' → k stays 0, lps[5] = 0.

Pababaca0123456lps0012301

p[0] = 'a' = p[6] → k = 1, lps[6] = 1.

Pababaca0123456lps0012301

Result: lps = [0, 0, 1, 2, 3, 0, 1]. The total fall-back cannot exceed the total increase → O(m).

int[] computeLPS(String p) {
  int m = p.length(), k = 0;
  int[] lps = new int[m];
  for (int q = 1; q < m; q++) {
    while (k > 0 &&
      p.charAt(k) != p.charAt(q))
      k = lps[k - 1];
    if (p.charAt(k) == p.charAt(q))
      k++;
    lps[q] = k;
  }
  return lps;
}

Multiple Choice · KMP

Question 6: Falling Back on a Mismatch

For the pattern "abcabd", lps = [0, 0, 0, 1, 2, 0]. In a KMP search, "abcab" has matched the text (q = 5) but the next text character is not d. What value does q take and how many positions has the pattern shifted?

D: q = lps[q − 1] = lps[4] = 2, because "ab" is both a prefix and a suffix of the matched "abcab"; the shift is 5 − 2 = 3. B is the mistake of using lps[q] (lps[5] = 0). The text pointer i does not go back; "ab" is not compared again.

Output Question · KMP

Question 7: Three Algorithms, One Text

Text "gel de geleyim", pattern "geleyim". How many character comparisons do brute force, KMP and Boyer-Moore (bad-character rule only) each make before finding the first match? At which position is the match?

Show answer

The match is at s = 7. Comparisons: brute force 17, KMP 15, Boyer-Moore 8.

  • Brute force: at s = 0 "gel" matches, '␣' ≠ 'e' (4); s = 1..6 end at the first character (6); at s = 7, 7 → 17.
  • KMP (lps is all 0): "gel" (3), '␣' first with p[3] then with p[0] (2), d, e, ␣ (3), geleyim (7) → 15. The text is never re-read.
  • Boyer-Moore: at s = 0 the first comparison from the right is t[6] = '␣' ≠ 'm'; ␣ is not in the pattern → shift 7; at s = 7, 7 matches → 8.
gel␣de␣geleyim012345678910111213BM 1geleyimBM 2geleyim

Multiple Choice · KMP

Question 8: Why Is KMP Linear?

In a KMP search, why can the total number of fall-backs in the while loop (q = lps[q - 1]) not exceed n?

B: Amortized argument: q increases by at most 1 per step (total ≤ n), every fall-back decreases it by at least 1, and q stays ≥ 0; so the total number of fall-backs is ≤ n and the search is O(n). C is wrong: in KMP, i never goes back; A is wrong too: lps values can be as large as m − 1.

Multiple Choice · Boyer-Moore

Question 9: Why Is There a max(1, …)?

In Boyer-Moore, what happens if the shift s += Math.max(1, j - last[c]) is written as s += j - last[c]?

A: For example, in "abacab", last = 4 for c = a; if the mismatch occurs at j = 3 we get 3 − 4 = −1. max(1, …) guarantees an advance of at least 1; otherwise there would be an infinite loop or a move backwards. B is wrong: a shift of j + 1 is safe, because no alignment that contains c can match.

Step-by-Step Trace · Boyer-Moore

Question 10: boyerMoore Trace

What value does boyerMoore("KARTAL KALKAR", "KALKAR") return?

KARTAL␣KALKAR0123456789101112KALKAR

n = 13, m = 6. Last occurrences: K = 3, A = 4, L = 2, R = 5, others −1. Comparison will go from right to left.

KARTAL␣KALKAR0123456789101112KALKAR

s = 0: p[5] = 'R' ≠ t[5] = 'L'. Bad character: 'L'.

KARTAL␣KALKAR0123456789101112KALKAR

last['L'] = 2 → max(1, 5 − 2) = 3; s = 3.

KARTAL␣KALKAR0123456789101112KALKAR

s = 3: p[5] = 'R' ≠ t[8] = 'A'. Bad character: 'A'.

KARTAL␣KALKAR0123456789101112KALKAR

last['A'] = 4 → max(1, 5 − 4) = 1; s = 4.

KARTAL␣KALKAR0123456789101112KALKAR

s = 4: p[5] = 'R' ≠ t[9] = 'L'. Bad character: 'L'.

KARTAL␣KALKAR0123456789101112KALKAR

last['L'] = 2 → max(1, 5 − 2) = 3; s = 7.

KARTAL␣KALKAR0123456789101112KALKAR

s = 7: R, A, K, L, A, K matched from right to left, j = −1.

KARTAL␣KALKAR0123456789101112KALKAR

j < 0 → 7 is returned. 4 alignments, 9 comparisons in total (brute force tries 8 alignments).

int boyerMoore(String t, String p) {
  int n = t.length(), m = p.length();
  int[] last = lastOccurrence(p);
  int s = 0;
  while (s <= n - m) {
    int j = m - 1;
    while (j >= 0 &&
      p.charAt(j) == t.charAt(s + j))
      j--;
    if (j < 0) return s;
    s += Math.max(1,
      j - last[t.charAt(s + j)]);
  }
  return -1;
}

Output Question · Boyer-Moore

Question 11: Horspool Shift Table

Build the Horspool shift table for the pattern "BARBER" with the rule shift[c] = m − 1 − (rightmost position of c in P[0..m−2]), or m if c does not occur there. If the text character under the last character of the alignment is B, R and X, how far is the pattern shifted?

PBARBER012345
Show answer

m = 6; only P[0..4] = B A R B E take part in the table:

cBAREother
shift[c]5 − 3 = 25 − 1 = 45 − 2 = 35 − 4 = 16

B → 2, R → 3, X → 6. The last character (R, index 5) is not part of the table; R still occurs at index 2, so it shifts by 3. The shift does not look at where the mismatch is; it is always based on the last character of the alignment.

Output Question · Rabin-Karp

Question 12: Rolling Hash

Base B = 10, Q = 11, the characters are digits; T = "26535", m = 3. h("265") = 265 mod 11 = 1 and Bm−1 mod Q = 100 mod 11 = 1. Using the sliding formula hs+1 = (B · (hs − t[s] · Bm−1) + t[s+m]) mod Q, find h("653") and h("535").

T2653501234
Show answer
  • h("653") = (10 · (1 − 2 · 1) + 3) mod 11 = (−7) mod 11 = 4 ✓ (653 = 59 · 11 + 4)
  • h("535") = (10 · (4 − 6 · 1) + 5) mod 11 = (−15) mod 11 = 7 ✓ (535 = 48 · 11 + 7)

Each new window takes O(1): the outgoing digit is subtracted, the rest shift one place left, and the incoming digit is added. The intermediate result can be negative; since % gives negative values in Java, if (th < 0) th += Q is needed.

Multiple Choice · Rabin-Karp

Question 13: Spurious Hit

Toy hash: the sum of the letter values (a = 1, b = 2, c = 3, d = 4). The pattern "bc" (hash 5) is searched for in the text "adcb". Which windows are spurious hits?

D: The windows are "ad" = 5, "dc" = 7, "cb" = 5. Two windows have the same hash as the pattern but different characters; verification rejects both and "bc" is not in the text. The sum hash is insensitive to order ("cb" also gives 5); that is why a position-weighted polynomial hash is used.

Multiple Choice · Rabin-Karp

Question 14: Many Patterns

500 different virus signatures of the same length are to be searched for in a file in a single pass. Which approach is most suitable?

B: The window hash is updated in O(1) and looked up in a HashSet in O(1) on average; if found, it is verified → expected O(n + k · m). A and D scan the file k times; in C, concatenating the signatures means no single signature is found on its own.

Multiple Choice · RLE

Question 15: When Does RLE Make Things Bigger?

For which of the following inputs does rleCompress make the output longer than the input?

D: There is no consecutive repeat; each character is written as "1" + character: 8 → 16 characters (twice as long). The others shrink: "5a5b" (10 → 4), "4a1b4a" (9 → 6), "11x1y" (12 → 5). RLE catches only consecutive repeats; LZW catches repeating strings like "abab…".

Step-by-Step Trace · LZW

Question 16: lzw Trace

With the initial dictionary A = 0, B = 1, what codes does lzw("BABAABAAA") produce?

BABAABAAA#codestringoutput: —

The dictionary starts with single characters: A = 0, B = 1; new strings are numbered from 2. w = "".

BABAABAAA#codestringoutput: —

"B" in dict (1) → w = "B"; look for a longer match.

BABAABAAA#codestring2BAoutput: 1

"BA" not in dict → output code of w = "B": 1; add "BA" = 2; w = "A".

BABAABAAA#codestring2BA3ABoutput: 1 0

"AB" not in dict → output code of w = "A": 0; add "AB" = 3; w = "B".

BABAABAAA#codestring2BA3ABoutput: 1 0

"BA" in dict (2) → w = "BA"; look for a longer match.

BABAABAAA#codestring2BA3AB4BAAoutput: 1 0 2

"BAA" not in dict → output code of w = "BA": 2; add "BAA" = 4; w = "A".

BABAABAAA#codestring2BA3AB4BAAoutput: 1 0 2

"AB" in dict (3) → w = "AB"; look for a longer match.

BABAABAAA#codestring2BA3AB4BAA5ABAoutput: 1 0 2 3

"ABA" not in dict → output code of w = "AB": 3; add "ABA" = 5; w = "A".

BABAABAAA#codestring2BA3AB4BAA5ABA6AAoutput: 1 0 2 3 0

"AA" not in dict → output code of w = "A": 0; add "AA" = 6; w = "A".

BABAABAAA#codestring2BA3AB4BAA5ABA6AAoutput: 1 0 2 3 0

"AA" in dict (6) → w = "AA"; look for a longer match.

BABAABAAA#codestring2BA3AB4BAA5ABA6AAoutput: 1 0 2 3 0 6

Loop finished; the code 6 of the remaining w = "AA" is added. Output: 1 0 2 3 0 6 (9 symbols → 6 codes).

List<Integer> lzw(String s) {
  Map<String, Integer> dict = initDict(s);
  String w = "";
  List<Integer> codes = new ArrayList<>();
  for (char c : s.toCharArray()) {
    String wc = w + c;
    if (dict.containsKey(wc)) {
      w = wc;
    } else {
      codes.add(dict.get(w));
      dict.put(wc, dict.size());
      w = "" + c;
    }
  }
  if (!w.isEmpty()) codes.add(dict.get(w));
  return codes;
}

Output Question · LZW

Question 17: LZW Decoding

Initial dictionary A = 0, B = 1, C = 2. Decode the LZW-compressed code sequence 0 1 3 2 4 7. At which code does the decoder meet the "code not yet defined" case?

Show answer
codewrittenadded to dictionary
0A—
1B3 = AB
3AB4 = BA
2C5 = ABC
4BA6 = CB
7BAB7 = BAB (special case)

Result: ABABCBABAB. When 7 is read, the decoder's dictionary only goes up to 6: e = w + w[0] = "BA" + "B" = "BAB". Check: lzw("ABABCBABAB") again gives 0 1 3 2 4 7.

Multiple Choice · LZW

Question 18: LZ78 Output

Starting with an empty dictionary, which (index, character) pairs does LZ78 turn the string "abababa" into?

C: "a" → (0, a), dictionary 1 = a; "b" → (0, b), 2 = b; "ab": a exists (1), ab does not → (1, b), 3 = ab; "aba": ab exists (3), aba does not → (3, a), 4 = aba. The difference of LZW is that it starts the dictionary with all single characters; so there is no need to write a character in the output.

Classic Question · Comparison

Question 19: Choosing the Algorithm by Scenario

For each scenario choose one of the string matching algorithms from the lecture and give your reasoning:
(a) In data streaming in from the network that cannot be read back, a search with a bounded worst case is needed.
(b) A 12-letter word is searched for in a long English text in an editor.
(c) Thousands of sentences of the same length are searched for in a document (plagiarism check).

Show answer
  • (a) KMP: the text pointer never moves backwards (suits a stream); even in the worst case O(n + m).
  • (b) Boyer-Moore / Horspool: with a long pattern and a large alphabet the bad-character rule skips almost m characters; the fastest in practice, O(n/m) at best.
  • (c) Rabin-Karp: pattern hashes go into a set, each window is updated in O(1) and looked up in the set; expected O(n + k · m).

Brute force needs no preprocessing and is enough for short patterns, but its worst case is O(n · m).

The End

Chapter 9: String Matching and Compression: Study Questions
Lecture slides: Chapter 9: String Matching and Compression · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.