Analysis of Algorithms, 91 s2

Total Page:16

File Type:pdf, Size:1020Kb

Analysis of Algorithms, 91 s2

Algorithms (91.503) Homework # 8 Fall 2002

Solution for Part I of HW#8 91.503, 2002

* #1. |Z 18| = φ(18) = 18(1-1/2)(1-1/3) = 6 * Z 18= {1, 5, 7, 11, 13, 17} ■

#2. MODULAR-EQUATION-SOLVER x≡3 mod 7 a b └a/b┘ d x y 1 7 0 1 1 0 7 1 7 1 0 1 1 0 - 1 1 0 (d,x,y) = (1,1,0) since (d|b) = 1|3, x0 = 1(3/1) mod 7 = 3 mod 7 = 3 x≡5 mod 6 a b └a/b┘ d x y 1 6 0 1 1 0 6 1 6 1 0 1 1 0 - 1 1 0 (d,x,y) = (1,1,0) since (d|b) = 1|5, x0 = 1(5/1) mod 6 = 5 mod 6 = 5 x≡4 mod 11 a b └a/b┘ d x y 1 11 0 1 1 0 11 1 11 1 0 1 1 0 - 1 1 0 (d,x,y) = (1,1,0) since (d|b) = 1|3, x0 = 1(4/1) mod 11 = 4 mod 11 = 4

Chinese Remainder Theorem 7,6,and 11 are relatively prime, so there is solution [x0]m, where m = 7*6*11 = 462 x0 = m1*b1*a1+ m2*b2*a3+ m3*b3*a3 m1 = 6*11 = 66, a1 = 3 m2 = 7*11 = 77, a2 = 5 m3 = 7*6 = 42, a3 = 4

66*b1≡1 mod 7, b1 = 5 77*b1≡1 mod 6, b2 = 5 42*b1≡1 mod 11, b3 = 5 x0 = 66*5*3 + 77*5*5 + 42*5*4 = 990 + 1925 + 840 = 3755 x ≡3755 mod 462 ≡59 mod 462 All solutions are of the form 59+462k for arbitrary integers k. ■

#3 Page 1 of 4 Algorithms (91.503) Homework # 8 Fall 2002

P and S cannot be a pair of public, secret keys for RSA. Since 221 = 13*17, hence φ(221) = (p-1)(q-1) = (13-1)(17-1) = 12*16 = 192. e = 11, which is relatively prime with 192. Now we have to find the multiplicative inverse of 11, modulo 192. 11*d = 1(mod 192). d= 17 in S. e*d mod 192= 11*17 mod 192= 187 mod 192 ≠ 1 mod 192 That means, d is not the multiplicative inverse of e. The pair should be P=(11,221) S=(35,221) (The congruents to 1 mod 192 are {1,193,385,577,…}. We notice that 11|385, so we find d=35) ■

4. (40 points) Chapter 32: Textbook, p. 910, Exercise 32.1-4. (Courtesy of Harish Rathi) GAP-MATCHER (T, P) 1 List A  MAKE-SUBLIST (P) 2 for i  1 to length[A] 3 do start  NAÏVE-STRING-MATCHER (T, A[i], start) 4 if start equals -1 5 then return “No Pattern Found” 6 return “Pattern Found”

A modified version of NAÏVE-STRING-MATCHER NAÏVE-STRING-MATCHER (T, P, start) 1 n  length[T] 2 m  length[P] 3 for s  start to n – m 4 do if P[1…m] = T[s + 1… s + m] 5 then return s + m + 1 6 return -1

To analyze the running time of the algorithm above, lines (1) and (3) need to be analyzed in GAP- MATCHER. Line (1) makes a sublist of pattern P where each element of list is the part between the gap characters, start and gap or gap and end of the text. e.g. for pattern abbac, A will have A[1] = ab, A[2] = ba, A[3] = c This is in O(n). This can be counted as preprocessing time. Now, line (3) is a call to NAÏVE-STRING-MATCHER whose running time is O ((n – m + 1)m) where m is the length of pattern and n is text length. Page 2 of 4 Algorithms (91.503) Homework # 8 Fall 2002

Line (3) is executed ‘i’ times, that is the number of gap characters in pattern P. Thus, O(i (n – m + 1)m) In worst case P can have length[P] = m’ number of gap characters. When m’ is maximum, m will be minimum (= 1). Giving the running time as O(m’n) which is O(n2). When m’ is minimum (= 1: no gap characters) then running time is O(1(n – m + 1)m) and when m = n/2 then running time is O(n2). Thus worst case running time is thus (n2).

Comments from Prof. Daniels: We need to add initialization of start to 0 in between lines 1 and 2 of GAP-MATCHER. In line 5 of modified NAÏVE-STRING-MATCHER, s+m+1 should be s+m. In the run-time analysis, we note that start increases monotonically, so the total number of iterations of NAÏVE-STRING-MATCHER‘s for loop is in O(n-m+1). Furthermore, the size of each element of A is in O(m). The total time is therefore in O((n-m+1)m), just like the unmodified NAÏVE-STRING- MATCHER.

5. (15 points) Chapter 32: Working modulo q = 3, how many spurious hits does the Rabin-Karp matcher encounter in the text T = 4126719021586 when looking for the pattern P = 125? Answer: - (Courtesy of Samip Banker)

Given, T = 4126719021586 P = 125 q = 3 d = 10 So, n = 13 m = 3 Window size is 3

Proper Match or spurious hits will be when x = 125 mod 3 = 2 Different windows that we will get are: -

Window Number Window String Window Value Spurious or Proper Hit

X1 412 1 No Hit

X2 126 0 No Hit Page 3 of 4 Algorithms (91.503) Homework # 8 Fall 2002

X3 267 0 No Hit

X4 671 2 Spurious Hit

X5 719 2 Spurious Hit

X6 190 1 No Hit

X7 902 2 Spurious Hit

X8 021 0 No Hit

X9 215 2 Spurious Hit

X10 158 2 Spurious Hit

X11 586 1 No Hit

We can see that X4, X5, X7, X9, and X10 have value as 2, which mean that either there is a proper match or spurious hit. But as we know that the string value doesn’t match with the pattern we are trying to match, so we have total of 5 spurious hits.

6. (20 points) Chapter 32: Compute the prefix function π for the pattern abcaabbccab when the alphabet is Σ = {a, b, c}. Answer: -(Courtesy of Samip Banker)

Given, P = abcaabbccab q = |P| = 11

For Every k < q

P [k] (k = 0 to 10) P [q] (q = 1 to 11) Π [q] (q = 1 to 11) ε a 0 a ab 0 ab abc 0 abc abca 1 abca abcaa 1 abcaa abcaab 2 abcaab abcaabb 0 abcaabb abcaabbc 0 abcaabbc abcaabbcc 0 abcaabbcc abcaabbcca 1 abcaabbcca abcaabbccab 2

q 1 2 3 4 5 6 7 8 9 10 11 P[q] a b c a a b b c c a b Π[q] 0 0 0 1 1 2 0 0 0 1 2

Page 4 of 4

Recommended publications