京都大学 情報学研究科 知能情報学専攻 2024年2月実施 基礎科目 F2-2
Author
itsuitsuki, 祭音Myyura (assisted by ChatGPT 5.4 Thinking)
Description (English)
We represent an array
where
(1) Compute the results of the prefix function aabaacaabaa.
(2) Algorithm 1 is a pseudo-code of an algorithm for computing the results of the prefix function
Algorithm 1 COMPUTE-PREFIX-FUNCTION(P):
m = P.length
let π be a new array for keeping the results of the prefix function
π[1] = 0
k = 0
for q = 2 to m do
while k > 0 and P[k + 1] ≠ P[q] do
(a)
end while
if P[k + 1] = P[q] then
(b)
end if
(c)
end for
return π
We represent an array
(3) Algorithm 2 is a pseudo-code of an algorithm for the string-matching problem utilizing the results of the prefix function computed with Algorithm 1. Fill the blanks (d), (e), and (f).
Algorithm 2 STRING-MATCHING(T, P):
n = T.length
m = P.length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0
for i = 1 to n do
while q > 0 and P[q + 1] ≠ T[i] do
(d)
end while
if P[q + 1] == T[i] then
(e)
end if
if q == m then
print "Pattern occurs with shift" i - m
(f)
end if
end for
(4) Show the time complexity of Algorithm 2 with reasons.
Kai
Q.1
(1) Prefix values for P = aabaacaabaa
Let
P = a a b a a c a a b a a
1 2 3 4 5 6 7 8 9 10 11
The prefix-function values are
π[1..11] = 0, 1, 0, 1, 2, 0, 1, 2, 3, 4, 5.
A compact table is:
| q | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| P[q] | a | a | b | a | a | c | a | a | b | a | a |
| π[q] | 0 | 1 | 0 | 1 | 2 | 0 | 1 | 2 | 3 | 4 | 5 |
(2) Fill the blanks of Algorithm 1
This is the standard prefix-function procedure.
- (a)
k = π[k]
- (b)
k = k + 1
- (c)
π[q] = k
Q2
(3) Fill the blanks of Algorithm 2
This is the standard KMP string-matching algorithm.
- (d)
q = π[q]
- (e)
q = q + 1
- (f)
q = π[q]
(4) Time complexity of Algorithm 2
Once the array π has already been computed, Algorithm 2 runs in
O(n)
time.
Reason:
- The outer
forloop runsntimes. - Inside the loop,
qcan increase by at most1in each iteration. - Every time the
whileloop executes,qis replaced byπ[q], which is strictly smaller thanq. - Therefore the total number of decreases of
qover the whole execution is at most the total number of increases ofq, which isO(n).
Hence the total running time of Algorithm 2 is linear in the text length.
If the preprocessing step COMPUTE-PREFIX-FUNCTION(P) is also included, then the total time becomes
O(m + n).