fa /Jcnm '::le) l ; '
rf
•
).
a a . a6 . /lJ b
a a a
3 I,
: f s S'
I~
a a,2_ a
3
hJr
I
•
j
~ e ,, k ~
~ / b d . :r! M n ~ tJ
-"t i ~ j thmwJ:-1
~
' 1-k d--,~()
c,n6i1' T·
-J ~{[Link]
J a,.,,,,} j •
a a _$b J
a a r
~
a a a1 , . ,
,
s-- I
.t.. !
f
dI ai_ a
j
b
J,
• ,_ _ J A..J .
.J
i -n-01- J',ff,_ ~
.Ht'J
-#re
rvu•orr,a/ch
.L 1
➔
· b ~ r« J&lT n
JIlj?e J!J a
~tJ;/;or,) tJ-nd J. Jo - f ¼
f
11 J lr n id{ F ,: n ;i
_.. p7~ Vd' j -fo -/1,e
'J-;tP'l •
~ f()i:µ
.
4 ~44
J.
q..44q
4 q_ q q
j
be/-weer,i i 1-- j . , . . ..
f,;iocttfl')
➔ f?efd +Ire ~
J - I¼ l i l f h
a/J,tJ 7 t" ;M ,, ,, . (
fVll/n:- at) tk t J ~ /.JJ-1!.
: . _r,,, -/h~ b om: t l / d ' .,;,
~
r il1,;/J;cf
f/ a l/YuJkf- ~ ju/J
J .f b li " 'f / cJ,tL/V.) m
4 (WJMaM.
~ a,,,,)_ { 8 ~
-I¼ /j; (}e 4
(!}(,-,z-,{)_
af > " t!
kJ- P l, 'YI
T i ~ J-a;,,,,, ff M b~C ~;/1,,,;z
{j)
t1b&', t 1 b
t' d
a. ab/
c, bC, e 1 b t /
dabc
I
I
/
zm~ h d - w ~ mt/41-
~tJ J o a !Jf/u '
df 4 fire aLPJu;btJ
7a/(e --/be ~
f~[z.' - b'fl'·u ,4- -IN ~
-
#e
/JJo,,,r /, /1 ,r r< J
:d · • •
~ a b t
~_z.' - a / 0 - h_ obe
-- . fJ,,J-, u l ) k u /
) UtWJJ-
1-1->e [Link] d -ff.-e ~
M/x.'- T a !{ d
'J c-7,
~/ia,,,JJ
~ -If,-(
a b c , Jah~
c, h e /
&,:-
fb}lvw, C J '1
J/ ,t-1 f/0,, cJ ~ ~
-+ :Irn/).'tk ff ,~
a/J -/l i a f ~ 5 o f/¥-! ,
/tJl-kwi
a
rvah'o-r-z :.0
~ fJ f l, e ; (M/7 ~ ;/In-,, ,m
➔ Thl!J ~b/Jo ~tYll'a~.
4
-1-o 4yJ;d ~
~ {I}
-? Ir t1 J{ITJ/? ~ ;- J I- , ,
k e J o ~ '71',, /a,ble.
/,J e
~ ~ t:Ja ifot /., /7-5J .
➔ ~ [Link]/& UJ (V J
wfu di
7T:
f, : )ibcda/Jb!e"iJJJl21()
t;J O c, tJ
I
I fl .5
at:J c,bCJct,d t!Je aI flbtJf aI hfl. c.5
fl~
a3b~e
a 1(b)/cJ- Ia d Ia ~
a
I
tJ
o
a . J l/2a b~ a a/2 c d t)0
tJ .J
I
I
•
.L
a, I; a b c a '2 c8 a b Cl b a b d,.s-
3 ,~ // Ill 13 ,,,
5/mi,n0/f '. "I. !I It .s I I
[Link]: a b a_ b d
-r f ,n-e a
R n ee, ·:rr ,. fob)e ~
t:J I 1!,
a b a
o o I ~ o
•
..L
.J b!', C/s1-
Cl. b o .b c a b c a
~
haba 112 l:J
Ji 5 / 1 $ lb II
I Jl j
•
J
f; I ~
Cl b a
;r'ni/-:afj,
7 #me i::: ~ ~ J ; I
-u J faffr/V(/ [J+[J. J I /»lh [Link] L1"""7c
➔ ~" 1f c. nt :' 5 / ~ O..J
) i ~ j. f?e~tff if ~~.
k J J (, p) rm f( - , Vi:'_
,,,,,,). %1!-,£/-1 -j]
➔ t4 , (/Y I L-s- ~ j.:: t- ,i
J, , an
f -6 5 /~ f iJ
fo ync}~.!Z ''cf2.''
b&}h doe/J 'IWf m a kh ~ ~ v i '' j ,,
T}
or-_ _,,. ../)
•
J J,
<J I fl
a b a. b J
-rh-wt-?
/t]~Y~ ''J. •r P J -- -t C) tJ I fl ~
JJ "C "a,.u1 fa/1,m,.[}+J J
-r f O
1:: .5 ~ J = -2 , & -, i(i m e 5 ~
rtU>Ve J '' -1-o ~ ''<J
''
~
11
" . :rJ btrfh tdo
~ "ba etJ J rt'la Hi/
'- '
t') (J J t2 ,tJ
~J:J] : 'a " ~
J ·~ ,2 , ~ i,,- I ~ ,(15,,,,f?=e
r<74~ rU>ve
~llum~Jl-iJ -- o
. :r.f b~fh o.n-e
vl~J. ,
.L
: q f c ; 1 ~ 1 ~ 1 k 4 s -
tr ~ q ,Z ;
/JI.J vaf,•~_1-
J,i;'-"an.d dJo.a:!,a>1 ~
(!) t/ l
: d '' ) '' ~ ; ,, -/171'!
-?' t,,e ha;,,,:: ,. w n .
al .
1/1 e ~ btJLif:foaLX •' ,n ''
/. J f/,'Je o j fo e , j / - ~ itJ
}a:ifJ!~
➔ J,'n-ze Cbrrip
4 #-e ftJ [Link] ,o '',-r1 ,,
5i'Je
... e { ; - + 1
h
~ l-t.:tbl~ $ e tU Z d
..
Knuth-Morris-Pratt (KMP) Algorithm
The KMP algorithm consists of two main steps:
1. Preprocessing the Pattern: This involves creating the longest prefix which is also a
suffix (LPS) array. This array helps to skip unnecessary comparisons when a mismatch
occurs.
2. Pattern Search: After preprocessing, KMP uses the LPS array to find matches
efficiently by skipping comparisons as soon as a mismatch occurs.
Steps:
1. Preprocessing the Pattern (LPS Array):
o Create an array lps[] for the pattern.
o Traverse the pattern and store lengths of the longest proper prefix which is
also a suffix for each index in the array.
2. Search for the Pattern:
o Start scanning the text from the beginning and use the LPS array to skip
unnecessary comparisons.
String: ABC ABCDAB ABCDABCDABDE
Pattern: ABCDABD
Step 1: Compute LPS array for the pattern
The pattern is: ABCDABD
0 1 2 3 4 5 6 7
P A B C D A B D
lps 0 0 0 0 1 2 0
The LPS array indicates that after a mismatch, how much the pattern should be shifted. For
example, if there’s a mismatch at index 6 of the pattern, instead of starting over, you can shift
by 2 characters.
Step 2: Search the Pattern in the String
Using the LPS array:
Start comparing from the beginning.
If a mismatch occurs after a few characters, use the LPS array to avoid rechecking the matched
portion.
For the string ABC ABCDAB ABCDABCDABDE, the pattern ABCDABD occurs at index
15.
Visualization:
Text: ABC ABCDAB ABCDABCDABDE
Pattern: ABCDABD
The KMP algorithm efficiently finds the pattern without backtracking and outputs the index
where the match begins (i.e., at index 15 in this case).