Analysis of Algorithms, 91

advertisement
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┘
1
7
0
7
1
7
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┘
1
6
0
6
1
6
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┘
1
11
0
11
1
11
1
0
(d,x,y) = (1,1,0)
since (d|b) = 1|3, x0 = 1(4/1) mod 11 = 4 mod 11 = 4
d
1
1
1
x
1
0
1
y
0
1
0
d
1
1
1
x
1
0
1
y
0
1
0
d
1
1
1
x
1
0
1
y
0
1
0
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.
■
Page 1 of 4
Algorithms (91.503)
Homework # 8
Fall 2002
#3
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
then return “No Pattern Found”
5
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
6
then return s + m + 1
return -1
To analyze the running time of the algorithm above, lines (1) and (3) need to be analyzed in GAPMATCHER.
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.
Page 2 of 4
Algorithms (91.503)
Homework # 8
Fall 2002
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.
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-STRINGMATCHER.
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: -
Page 3 of 4
Algorithms (91.503)
Homework # 8
Fall 2002
Window Number
X1
X2
X3
X4
X5
X6
X7
X8
X9
X10
X11
Window String
412
126
267
671
719
190
902
021
215
158
586
Window Value
1
0
0
2
2
1
2
0
2
2
1
Spurious or Proper Hit
No Hit
No Hit
No Hit
Spurious Hit
Spurious Hit
No Hit
Spurious Hit
No Hit
Spurious Hit
Spurious Hit
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)
ε
a
ab
abc
abca
abcaa
abcaab
abcaabb
abcaabbc
abcaabbcc
abcaabbcca
Π [q] (q = 1 to 11)
0
0
0
1
1
2
0
0
0
1
2
P [q] (q = 1 to 11)
a
ab
abc
abca
abcaa
abcaab
abcaabb
abcaabbc
abcaabbcc
abcaabbcca
abcaabbccab
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
Download