SlideShare uma empresa Scribd logo
1 de 20
KKnnuutthh--MMoorrrriiss--PPrraatttt 
AAllggoorriitthhmm 
PPrreeppaarreedd bbyy:: MMaayyaannkk AAggaarrwwaall 
NNiitteesshh MMaaaann
TThhee pprroobblleemm ooff SSttrriinngg MMaattcchhiinngg 
GGiivveenn aa ssttrriinngg ‘‘SS’’,, tthhee pprroobblleemm ooff ssttrriinngg 
mmaattcchhiinngg ddeeaallss wwiitthh ffiinnddiinngg wwhheetthheerr aa 
ppaatttteerrnn ‘‘pp’’ ooccccuurrss iinn ‘‘SS’’ aanndd iiff ‘‘pp’’ ddooeess 
ooccccuurr tthheenn rreettuurrnniinngg ppoossiittiioonn iinn ‘‘SS’’ wwhheerree 
‘‘pp’’ ooccccuurrss..
…….. aa OO(mmnn) aapppprrooaacchh 
OOnnee ooff tthhee mmoosstt oobbvviioouuss aapppprrooaacchh ttoowwaarrddss tthhee 
ssttrriinngg mmaattcchhiinngg pprroobblleemm wwoouulldd bbee ttoo ccoommppaarree 
tthhee ffiirrsstt eelleemmeenntt ooff tthhee ppaatttteerrnn ttoo bbee sseeaarrcchheedd 
‘‘pp’’,, wwiitthh tthhee ffiirrsstt eelleemmeenntt ooff tthhee ssttrriinngg ‘‘SS’’ iinn wwhhiicchh 
ttoo llooccaattee ‘‘pp’’.. IIff tthhee ffiirrsstt eelleemmeenntt ooff ‘‘pp’’ mmaattcchheess 
tthhee ffiirrsstt eelleemmeenntt ooff ‘‘SS’’,, ccoommppaarree tthhee sseeccoonndd 
eelleemmeenntt ooff ‘‘pp’’ wwiitthh sseeccoonndd eelleemmeenntt ooff ‘‘SS’’.. IIff 
mmaattcchh ffoouunndd pprroocceeeedd lliikkeewwiissee uunnttiill eennttiirree ‘‘pp’’ iiss 
ffoouunndd.. IIff aa mmiissmmaattcchh iiss ffoouunndd aatt aannyy ppoossiittiioonn,, 
sshhiifftt ‘‘pp’’ oonnee ppoossiittiioonn ttoo tthhee rriigghhtt aanndd rreeppeeaatt 
ccoommppaarriissoonn bbeeggiinnnniinngg ffrroomm ffiirrsstt eelleemmeenntt ooff ‘‘pp’’..
How ddooeess tthhee OO(mmnn) aapppprrooaacchh 
wwoorrkk 
BBeellooww iiss aann iilllluussttrraattiioonn ooff hhooww tthhee pprreevviioouussllyy 
ddeessccrriibbeedd OO(mmnn) aapppprrooaacchh wwoorrkkss.. 
SSttrriinngg SS aa bb cc aa bb aa aa bb cc aa bb aa cc 
Pattern p aa bb aa aa
SStteepp 11::ccoommppaarree pp[[11]] wwiitthh SS[[11]] 
SS aa bb cc aa bb aa aa bb cc aa bb aa cc 
p aa bb aa aa 
Step 2: compare p[2] with S[2] 
S aa bb cc aa bb aa aa bb cc aa bb aa cc 
p aa bb aa aa
SStteepp 33:: ccoommppaarree pp[[33]] wwiitthh SS[[33]] 
SS 
aa bb cc aa bb aa aa bb cc aa bb aa cc 
p aa bb aa aa 
Mismatch occurs here.. 
Since mismatch is detected, shift ‘p’ one position to the left and 
perform steps analogous to those from step 1 to step 3. At position 
where mismatch is detected, shift ‘p’ one position to the right and 
repeat matching procedure.
SS aa bb cc aa bb aa aa bb cc aa bb aa cc 
p aa bb aa aa 
Finally, a match would be found after shifting ‘p’ three times to the right side. 
Drawbacks of this approach: if ‘m’ is the length of pattern ‘p’ and ‘n’ the length 
of string ‘S’, the matching time is of the order O(mn). This is a certainly a very 
slow running algorithm. 
What makes this approach so slow is the fact that elements of ‘S’ with which 
comparisons had been performed earlier are involved again and again in 
comparisons in some future iterations. For example: when mismatch is 
detected for the first time in comparison of p[3] with S[3], pattern ‘p’ would be 
moved one position to the right and matching procedure would resume from 
here. Here the first comparison that would take place would be between p[0]=‘a’ 
and S[1]=‘b’. It should be noted here that S[1]=‘b’ had been previously involved 
in a comparison in step 2. this is a repetitive use of S[1] in another comparison. 
It is these repetitive comparisons that lead to the runtime of O(mn).
The Knuth-MMoorrrriiss--PPrraatttt AAllggoorriitthhmm 
KKnnuutthh,, MMoorrrriiss aanndd PPrraatttt pprrooppoosseedd aa lliinneeaarr 
ttiimmee aallggoorriitthhmm ffoorr tthhee ssttrriinngg mmaattcchhiinngg 
pprroobblleemm.. 
AA mmaattcchhiinngg ttiimmee ooff OO((nn)) iiss aacchhiieevveedd bbyy 
aavvooiiddiinngg ccoommppaarriissoonnss wwiitthh eelleemmeennttss ooff ‘‘SS’’ 
tthhaatt hhaavvee pprreevviioouussllyy bbeeeenn iinnvvoollvveedd iinn 
ccoommppaarriissoonn wwiitthh ssoommee eelleemmeenntt ooff tthhee 
ppaatttteerrnn ‘‘pp’’ ttoo bbee mmaattcchheedd.. ii..ee..,, 
bbaacckkttrraacckkiinngg oonn tthhee ssttrriinngg ‘‘SS’’ nneevveerr ooccccuurrss
CCoommppoonneennttss ooff KKMMPP aallggoorriitthhmm 
 TThhee pprreeffiixx ffuunnccttiioonn,, ΠΠ 
TThhee pprreeffiixx ffuunnccttiioonn,,ΠΠ ffoorr aa ppaatttteerrnn eennccaappssuullaatteess 
kknnoowwlleeddggee aabboouutt hhooww tthhee ppaatttteerrnn mmaattcchheess 
aaggaaiinnsstt sshhiiffttss ooff iittsseellff.. TThhiiss iinnffoorrmmaattiioonn ccaann bbee 
uusseedd ttoo aavvooiidd uusseelleessss sshhiiffttss ooff tthhee ppaatttteerrnn ‘‘pp’’.. IInn 
ootthheerr wwoorrddss,, tthhiiss eennaabblleess aavvooiiddiinngg bbaacckkttrraacckkiinngg 
oonn tthhee ssttrriinngg ‘‘SS’’.. 
 TThhee KKMMPP MMaattcchheerr 
WWiitthh ssttrriinngg ‘‘SS’’,, ppaatttteerrnn ‘‘pp’’ aanndd pprreeffiixx ffuunnccttiioonn ‘‘ΠΠ’’ aass 
iinnppuuttss,, ffiinnddss tthhee ooccccuurrrreennccee ooff ‘‘pp’’ iinn ‘‘SS’’ aanndd 
rreettuurrnnss tthhee nnuummbbeerr ooff sshhiiffttss ooff ‘‘pp’’ aafftteerr wwhhiicchh 
ooccccuurrrreennccee iiss ffoouunndd..
TThhee pprreeffiixx ffuunnccttiioonn,, ΠΠ 
FFoolllloowwiinngg ppsseeuuddooccooddee ccoommppuutteess tthhee pprreeffiixx ffuuccnnccttiioonn,, ΠΠ:: 
CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn ((pp)) 
11 mm  lleennggtthh[[pp]] ////’’pp’’ ppaatttteerrnn ttoo bbee mmaattcchheedd 
22 ΠΠ[[11]]  00 
33 kk  00 
44 ffoorr qq  22 ttoo mm 
55 ddoo wwhhiillee kk >> 00 aanndd pp[[kk++11]] !!== pp[[qq]] 
66 ddoo kk  ΠΠ[[kk]] 
77 IIff pp[[kk++11]] == pp[[qq]] 
88 tthheenn kk  kk ++11 
99 ΠΠ[[qq]]  kk 
1100 rreettuurrnn ΠΠ
EExxaammppllee:: ccoommppuuttee ΠΠ ffoorr tthhee ppaatttteerrnn ‘‘pp’’ bbeellooww:: 
pp aa bb aa bb aa cc aa 
Initially: m = length[p] = 7 
Π[1] = 0 
k = 0 
Step 1: q = 2, k=0 
Π[2] = 0 
Step 2: q = 3, k = 0, 
Π[3] = 1 
Step 3: q = 4, k = 1 
Π[4] = 2 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 11 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc AA 
ΠΠ 00 00 11 22
SStteepp 44:: qq == 55,, kk ==22 
ΠΠ[[55]] == 33 
SStteepp 55:: qq == 66,, kk == 33 
ΠΠ[[66]] == 11 
SStteepp 66:: qq == 77,, kk == 11 
ΠΠ[[77]] == 11 
AAfftteerr iitteerraattiinngg 66 ttiimmeess,, tthhee pprreeffiixx 
ffuunnccttiioonn ccoommppuuttaattiioonn iiss 
ccoommpplleettee::  
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 11 22 33 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 11 22 33 00 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 11 22 33 00 11 
qq 11 22 33 44 55 66 77 
pp aa bb AA bb aa cc aa 
ΠΠ 00 00 11 22 33 00 11
TThhee KKMMPP MMaattcchheerr 
TThhee KKMMPP MMaattcchheerr,, wwiitthh ppaatttteerrnn ‘‘pp’’,, ssttrriinngg ‘‘SS’’ aanndd pprreeffiixx ffuunnccttiioonn ‘‘ΠΠ’’ aass iinnppuutt,, ffiinnddss aa mmaattcchh ooff pp iinn SS.. 
FFoolllloowwiinngg ppsseeuuddooccooddee ccoommppuutteess tthhee mmaattcchhiinngg ccoommppoonneenntt ooff KKMMPP aallggoorriitthhmm:: 
KKMMPP--MMaattcchheerr((SS,,pp)) 
11 nn  lleennggtthh[[SS]] 
22 mm  lleennggtthh[[pp]] 
33 ΠΠ  CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn((pp)) 
44 qq  00 ////nnuummbbeerr ooff cchhaarraacctteerrss mmaattcchheedd 
55 ffoorr ii  11 ttoo nn ////ssccaann SS ffrroomm lleefftt ttoo rriigghhtt 
66 ddoo wwhhiillee qq >> 00 aanndd pp[[qq++11]] !!== SS[[ii]] 
77 ddoo qq  ΠΠ[[qq]] ////nneexxtt cchhaarraacctteerr ddooeess nnoott mmaattcchh 
88 iiff pp[[qq++11]] == SS[[ii]] 
99 tthheenn qq  qq ++ 11 ////nneexxtt cchhaarraacctteerr mmaattcchheess 
1100 iiff qq == mm ////iiss aallll ooff pp mmaattcchheedd?? 
1111 tthheenn pprriinntt ““PPaatttteerrnn ooccccuurrss wwiitthh sshhiifftt”” ii –– mm 
1122 qq  ΠΠ[[ qq]] //// llooookk ffoorr tthhee nneexxtt mmaattcchh 
NNoottee:: KKMMPP ffiinnddss eevveerryy ooccccuurrrreennccee ooff aa ‘‘pp’’ iinn ‘‘SS’’.. TThhaatt iiss wwhhyy KKMMPP ddooeess nnoott tteerrmmiinnaattee iinn sstteepp 1122,, 
rraatthheerr iitt sseeaarrcchheess rreemmaaiinnddeerr ooff ‘‘SS’’ ffoorr aannyy mmoorree ooccccuurrrreenncceess ooff ‘‘pp’’..
IIlllluussttrraattiioonn:: ggiivveenn aa SSttrriinngg ‘‘SS’’ aanndd ppaatttteerrnn ‘‘pp’’ aass 
ffoolllloowwss:: 
SS bb a cc bb aa bb aa bb aa bb aa cc aa cc aa 
p aa bb aa bb aa cc aa 
Let us execute the KMP algorithm to find 
whether ‘p’ occurs in ‘S’. 
For ‘p’ the prefix function, Π was computed previously and is as follows: 
qq 11 22 33 44 55 66 77 
pp aa bb aa bb aa cc aa 
ΠΠ 00 00 11 22 33 00 11
Initially: n = size of S = 15; 
m = size of p = 7 
Step 1: i = 1, q = 0 
comparing p[1] with S[1] 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
Step 2: i = 2, q = 0 
comparing p[1] with S[2] 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
S 
p 
P[1] does not match with S[1]. ‘p’ will be shifted one position to the right. 
S 
p aa bb aa bb aa cc aa 
P[1] matches S[2]. Since there is a match, p is not shifted.
SStteepp 33:: ii == 33,, qq == 11 
Comparing p[2] with S[3] 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
p aa bb aa bb aa cc aa 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
S 
aa bb aa bb aa cc aa 
S 
p 
S 
p 
p[2] does not match with S[3] 
Backtracking on p, comparing p[1] and S[3] 
Step 4: i = 4, q =co 0m paring p[1] with S[4] p[1] does not match with S[4] 
Step 5: i = 5, q c=o 0m paring p[1] with S[5] p[1] matches with S[5]
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
SStteepp 77:: ii == 77,, qq == 22 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
SStteepp 66:: ii == 66,, qq == 11 
S 
p 
Comparing p[2] with S[6] p[2] matches with S[6] 
S 
p 
Comparing p[3] with S[7] p[3] matches with S[7] 
SStteepp 88:: ii == 88,, qq == 33 
Comparing p[4] with S[8] p[4] matches with S[8] 
S 
p
SStteepp 99:: ii == 99,, qq == 44 
Comparing p[5] with S[9] 
p 
SStteepp 1100:: ii == 1100,, qq == 55 
p[5] matches with S[9] 
aa bb aa bb aa cc aa 
Comparing p[6] with S[10] 
p 
Backtracking on p, comparing p[4] with S[10] because after mismatch q = Π[5] = 3 
SStteepp 1111:: ii == 1111,, qq == 44 
p[6] doesn’t match with S[10] 
aa bb aa bb aa cc aa 
Comparing p[5] with S[11] 
S 
S 
S 
p 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
p[5] matches with S[11] 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb 
aa bb aa bb aa cc aa 
SStteepp 1122:: ii == 1122,, qq == 55 
Comparing p[6] with S[12] 
Comparing p[7] with S[13] 
S 
p 
S 
p 
SStteepp 1133:: ii == 1133,, qq == 66 
p[6] matches with S[12] 
p[7] matches with S[13] 
Pattern ‘p’ has been found to completely occur in string ‘S’. The total number of shifts 
that took place for the match to be found are: i – m = 13 – 7 = 6 shifts.
RRuunnnniinngg -- ttiimmee aannaallyyssiiss 
 CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn ((ΠΠ)) 
11 mm  lleennggtthh[[pp]] ////’’pp’’ ppaatttteerrnn ttoo bbee 
mmaattcchheedd 
22 ΠΠ[[11]]  00 
33 kk  00 
44 ffoorr qq  22 ttoo mm 
55 ddoo wwhhiillee kk >> 00 aanndd pp[[kk++11]] !!== pp[[qq]] 
66 ddoo kk  ΠΠ[[kk]] 
77 IIff pp[[kk++11]] == pp[[qq]] 
88 tthheenn kk  kk ++11 
99 ΠΠ[[qq]]  kk 
1100 rreettuurrnn ΠΠ 
IInn tthhee aabboovvee ppsseeuuddooccooddee ffoorr ccoommppuuttiinngg tthhee 
pprreeffiixx ffuunnccttiioonn,, tthhee ffoorr lloooopp ffrroomm sstteepp 44 
ttoo sstteepp 1100 rruunnss ‘‘mm’’ ttiimmeess. SStteepp 11 ttoo sstteepp 
33 ttaakkee ccoonnssttaanntt ttiimmee. HHeennccee tthhee rruunnnniinngg 
ttiimmee ooff ccoommppuuttee pprreeffiixx ffuunnccttiioonn iiss ΘΘ((mm)). 
 KKMMPP MMaattcchheerr 
11 nn  lleennggtthh[[SS]] 
22 mm  lleennggtthh[[pp]] 
33 ΠΠ  CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn((pp)) 
44 qq  00 
55 ffoorr ii  11 ttoo nn 
66 ddoo wwhhiillee qq >> 00 aanndd pp[[qq++11]] !!== SS[[ii]] 
77 ddoo qq  ΠΠ[[qq]] 
88 iiff pp[[qq++11]] == SS[[ii]] 
99 tthheenn qq  qq ++ 11 
1100 iiff qq == mm 
1111 tthheenn pprriinntt ““PPaatttteerrnn ooccccuurrss wwiitthh sshhiifftt”” ii –– 
mm 
1122 qq  ΠΠ[[ qq]] 
TThhee ffoorr lloooopp bbeeggiinnnniinngg iinn sstteepp 55 rruunnss ‘‘nn’’ ttiimmeess,, 
ii.ee.,, aass lloonngg aass tthhee lleennggtthh ooff tthhee ssttrriinngg ‘‘SS’’. 
SSiinnccee sstteepp 11 ttoo sstteepp 44 ttaakkee ccoonnssttaanntt ttiimmee,, 
tthhee rruunnnniinngg ttiimmee iiss ddoommiinnaatteedd bbyy tthhiiss ffoorr 
lloooopp. TThhuuss rruunnnniinngg ttiimmee ooff mmaattcchhiinngg 
ffuunnccttiioonn iiss ΘΘ((nn)).

Mais conteúdo relacionado

Mais de thinkphp

Placemaker
PlacemakerPlacemaker
Placemaker
thinkphp
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Ahead
thinkphp
 
Atom Web Services
Atom Web ServicesAtom Web Services
Atom Web Services
thinkphp
 
Json Rpc Proxy Generation With Php
Json Rpc Proxy Generation With PhpJson Rpc Proxy Generation With Php
Json Rpc Proxy Generation With Php
thinkphp
 
Php And Web Services
Php And Web ServicesPhp And Web Services
Php And Web Services
thinkphp
 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
thinkphp
 
Principii Poo
Principii PooPrincipii Poo
Principii Poo
thinkphp
 

Mais de thinkphp (9)

Javascript patterns
Javascript patternsJavascript patterns
Javascript patterns
 
Placemaker
PlacemakerPlacemaker
Placemaker
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Ahead
 
Atom Web Services
Atom Web ServicesAtom Web Services
Atom Web Services
 
Json Rpc Proxy Generation With Php
Json Rpc Proxy Generation With PhpJson Rpc Proxy Generation With Php
Json Rpc Proxy Generation With Php
 
Php And Web Services
Php And Web ServicesPhp And Web Services
Php And Web Services
 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
 
OOP
OOPOOP
OOP
 
Principii Poo
Principii PooPrincipii Poo
Principii Poo
 

Último

Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergiaImmunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
Pasi Vilpas
 

Último (9)

Jedhi Malee (just do it).pdf
Jedhi Malee             (just do it).pdfJedhi Malee             (just do it).pdf
Jedhi Malee (just do it).pdf
 
Koululaiset, opiskelija, oppijat ja lapset sekä tutkinnot
Koululaiset, opiskelija, oppijat ja lapset sekä tutkinnotKoululaiset, opiskelija, oppijat ja lapset sekä tutkinnot
Koululaiset, opiskelija, oppijat ja lapset sekä tutkinnot
 
Koulutuksen rahoitus, tulot, menot ja talous
Koulutuksen rahoitus, tulot, menot ja talousKoulutuksen rahoitus, tulot, menot ja talous
Koulutuksen rahoitus, tulot, menot ja talous
 
Koulutuksen palkat ja kustannukset sekä koulutuksen ansiot
Koulutuksen palkat ja kustannukset sekä koulutuksen ansiotKoulutuksen palkat ja kustannukset sekä koulutuksen ansiot
Koulutuksen palkat ja kustannukset sekä koulutuksen ansiot
 
Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergiaImmunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
Immunologian perusteet: valkosolutyyppien yhteistyö, elinsiirrot, allergia
 
Kasvatus, koulutus, opetus ja osaaminen Suomessa
Kasvatus, koulutus, opetus ja osaaminen SuomessaKasvatus, koulutus, opetus ja osaaminen Suomessa
Kasvatus, koulutus, opetus ja osaaminen Suomessa
 
Aikuiskoulutus, jatkuva oppiminen, elinikäinen oppiminen ja henkilöstökoulutus
Aikuiskoulutus, jatkuva oppiminen, elinikäinen oppiminen ja henkilöstökoulutusAikuiskoulutus, jatkuva oppiminen, elinikäinen oppiminen ja henkilöstökoulutus
Aikuiskoulutus, jatkuva oppiminen, elinikäinen oppiminen ja henkilöstökoulutus
 
Oppimateriaaleihin, menetelmiin ja sovelluksiin tutustuminen 2.5.24
Oppimateriaaleihin, menetelmiin ja sovelluksiin tutustuminen 2.5.24Oppimateriaaleihin, menetelmiin ja sovelluksiin tutustuminen 2.5.24
Oppimateriaaleihin, menetelmiin ja sovelluksiin tutustuminen 2.5.24
 
Tutkimus-, kehittämis- ja innovaatiotoiminnan rahoitus
Tutkimus-, kehittämis- ja innovaatiotoiminnan rahoitusTutkimus-, kehittämis- ja innovaatiotoiminnan rahoitus
Tutkimus-, kehittämis- ja innovaatiotoiminnan rahoitus
 

Knut Morris Pratt Algorithm

  • 1. KKnnuutthh--MMoorrrriiss--PPrraatttt AAllggoorriitthhmm PPrreeppaarreedd bbyy:: MMaayyaannkk AAggaarrwwaall NNiitteesshh MMaaaann
  • 2. TThhee pprroobblleemm ooff SSttrriinngg MMaattcchhiinngg GGiivveenn aa ssttrriinngg ‘‘SS’’,, tthhee pprroobblleemm ooff ssttrriinngg mmaattcchhiinngg ddeeaallss wwiitthh ffiinnddiinngg wwhheetthheerr aa ppaatttteerrnn ‘‘pp’’ ooccccuurrss iinn ‘‘SS’’ aanndd iiff ‘‘pp’’ ddooeess ooccccuurr tthheenn rreettuurrnniinngg ppoossiittiioonn iinn ‘‘SS’’ wwhheerree ‘‘pp’’ ooccccuurrss..
  • 3. …….. aa OO(mmnn) aapppprrooaacchh OOnnee ooff tthhee mmoosstt oobbvviioouuss aapppprrooaacchh ttoowwaarrddss tthhee ssttrriinngg mmaattcchhiinngg pprroobblleemm wwoouulldd bbee ttoo ccoommppaarree tthhee ffiirrsstt eelleemmeenntt ooff tthhee ppaatttteerrnn ttoo bbee sseeaarrcchheedd ‘‘pp’’,, wwiitthh tthhee ffiirrsstt eelleemmeenntt ooff tthhee ssttrriinngg ‘‘SS’’ iinn wwhhiicchh ttoo llooccaattee ‘‘pp’’.. IIff tthhee ffiirrsstt eelleemmeenntt ooff ‘‘pp’’ mmaattcchheess tthhee ffiirrsstt eelleemmeenntt ooff ‘‘SS’’,, ccoommppaarree tthhee sseeccoonndd eelleemmeenntt ooff ‘‘pp’’ wwiitthh sseeccoonndd eelleemmeenntt ooff ‘‘SS’’.. IIff mmaattcchh ffoouunndd pprroocceeeedd lliikkeewwiissee uunnttiill eennttiirree ‘‘pp’’ iiss ffoouunndd.. IIff aa mmiissmmaattcchh iiss ffoouunndd aatt aannyy ppoossiittiioonn,, sshhiifftt ‘‘pp’’ oonnee ppoossiittiioonn ttoo tthhee rriigghhtt aanndd rreeppeeaatt ccoommppaarriissoonn bbeeggiinnnniinngg ffrroomm ffiirrsstt eelleemmeenntt ooff ‘‘pp’’..
  • 4. How ddooeess tthhee OO(mmnn) aapppprrooaacchh wwoorrkk BBeellooww iiss aann iilllluussttrraattiioonn ooff hhooww tthhee pprreevviioouussllyy ddeessccrriibbeedd OO(mmnn) aapppprrooaacchh wwoorrkkss.. SSttrriinngg SS aa bb cc aa bb aa aa bb cc aa bb aa cc Pattern p aa bb aa aa
  • 5. SStteepp 11::ccoommppaarree pp[[11]] wwiitthh SS[[11]] SS aa bb cc aa bb aa aa bb cc aa bb aa cc p aa bb aa aa Step 2: compare p[2] with S[2] S aa bb cc aa bb aa aa bb cc aa bb aa cc p aa bb aa aa
  • 6. SStteepp 33:: ccoommppaarree pp[[33]] wwiitthh SS[[33]] SS aa bb cc aa bb aa aa bb cc aa bb aa cc p aa bb aa aa Mismatch occurs here.. Since mismatch is detected, shift ‘p’ one position to the left and perform steps analogous to those from step 1 to step 3. At position where mismatch is detected, shift ‘p’ one position to the right and repeat matching procedure.
  • 7. SS aa bb cc aa bb aa aa bb cc aa bb aa cc p aa bb aa aa Finally, a match would be found after shifting ‘p’ three times to the right side. Drawbacks of this approach: if ‘m’ is the length of pattern ‘p’ and ‘n’ the length of string ‘S’, the matching time is of the order O(mn). This is a certainly a very slow running algorithm. What makes this approach so slow is the fact that elements of ‘S’ with which comparisons had been performed earlier are involved again and again in comparisons in some future iterations. For example: when mismatch is detected for the first time in comparison of p[3] with S[3], pattern ‘p’ would be moved one position to the right and matching procedure would resume from here. Here the first comparison that would take place would be between p[0]=‘a’ and S[1]=‘b’. It should be noted here that S[1]=‘b’ had been previously involved in a comparison in step 2. this is a repetitive use of S[1] in another comparison. It is these repetitive comparisons that lead to the runtime of O(mn).
  • 8. The Knuth-MMoorrrriiss--PPrraatttt AAllggoorriitthhmm KKnnuutthh,, MMoorrrriiss aanndd PPrraatttt pprrooppoosseedd aa lliinneeaarr ttiimmee aallggoorriitthhmm ffoorr tthhee ssttrriinngg mmaattcchhiinngg pprroobblleemm.. AA mmaattcchhiinngg ttiimmee ooff OO((nn)) iiss aacchhiieevveedd bbyy aavvooiiddiinngg ccoommppaarriissoonnss wwiitthh eelleemmeennttss ooff ‘‘SS’’ tthhaatt hhaavvee pprreevviioouussllyy bbeeeenn iinnvvoollvveedd iinn ccoommppaarriissoonn wwiitthh ssoommee eelleemmeenntt ooff tthhee ppaatttteerrnn ‘‘pp’’ ttoo bbee mmaattcchheedd.. ii..ee..,, bbaacckkttrraacckkiinngg oonn tthhee ssttrriinngg ‘‘SS’’ nneevveerr ooccccuurrss
  • 9. CCoommppoonneennttss ooff KKMMPP aallggoorriitthhmm  TThhee pprreeffiixx ffuunnccttiioonn,, ΠΠ TThhee pprreeffiixx ffuunnccttiioonn,,ΠΠ ffoorr aa ppaatttteerrnn eennccaappssuullaatteess kknnoowwlleeddggee aabboouutt hhooww tthhee ppaatttteerrnn mmaattcchheess aaggaaiinnsstt sshhiiffttss ooff iittsseellff.. TThhiiss iinnffoorrmmaattiioonn ccaann bbee uusseedd ttoo aavvooiidd uusseelleessss sshhiiffttss ooff tthhee ppaatttteerrnn ‘‘pp’’.. IInn ootthheerr wwoorrddss,, tthhiiss eennaabblleess aavvooiiddiinngg bbaacckkttrraacckkiinngg oonn tthhee ssttrriinngg ‘‘SS’’..  TThhee KKMMPP MMaattcchheerr WWiitthh ssttrriinngg ‘‘SS’’,, ppaatttteerrnn ‘‘pp’’ aanndd pprreeffiixx ffuunnccttiioonn ‘‘ΠΠ’’ aass iinnppuuttss,, ffiinnddss tthhee ooccccuurrrreennccee ooff ‘‘pp’’ iinn ‘‘SS’’ aanndd rreettuurrnnss tthhee nnuummbbeerr ooff sshhiiffttss ooff ‘‘pp’’ aafftteerr wwhhiicchh ooccccuurrrreennccee iiss ffoouunndd..
  • 10. TThhee pprreeffiixx ffuunnccttiioonn,, ΠΠ FFoolllloowwiinngg ppsseeuuddooccooddee ccoommppuutteess tthhee pprreeffiixx ffuuccnnccttiioonn,, ΠΠ:: CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn ((pp)) 11 mm  lleennggtthh[[pp]] ////’’pp’’ ppaatttteerrnn ttoo bbee mmaattcchheedd 22 ΠΠ[[11]]  00 33 kk  00 44 ffoorr qq  22 ttoo mm 55 ddoo wwhhiillee kk >> 00 aanndd pp[[kk++11]] !!== pp[[qq]] 66 ddoo kk  ΠΠ[[kk]] 77 IIff pp[[kk++11]] == pp[[qq]] 88 tthheenn kk  kk ++11 99 ΠΠ[[qq]]  kk 1100 rreettuurrnn ΠΠ
  • 11. EExxaammppllee:: ccoommppuuttee ΠΠ ffoorr tthhee ppaatttteerrnn ‘‘pp’’ bbeellooww:: pp aa bb aa bb aa cc aa Initially: m = length[p] = 7 Π[1] = 0 k = 0 Step 1: q = 2, k=0 Π[2] = 0 Step 2: q = 3, k = 0, Π[3] = 1 Step 3: q = 4, k = 1 Π[4] = 2 qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 11 qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc AA ΠΠ 00 00 11 22
  • 12. SStteepp 44:: qq == 55,, kk ==22 ΠΠ[[55]] == 33 SStteepp 55:: qq == 66,, kk == 33 ΠΠ[[66]] == 11 SStteepp 66:: qq == 77,, kk == 11 ΠΠ[[77]] == 11 AAfftteerr iitteerraattiinngg 66 ttiimmeess,, tthhee pprreeffiixx ffuunnccttiioonn ccoommppuuttaattiioonn iiss ccoommpplleettee::  qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 11 22 33 qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 11 22 33 00 qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 11 22 33 00 11 qq 11 22 33 44 55 66 77 pp aa bb AA bb aa cc aa ΠΠ 00 00 11 22 33 00 11
  • 13. TThhee KKMMPP MMaattcchheerr TThhee KKMMPP MMaattcchheerr,, wwiitthh ppaatttteerrnn ‘‘pp’’,, ssttrriinngg ‘‘SS’’ aanndd pprreeffiixx ffuunnccttiioonn ‘‘ΠΠ’’ aass iinnppuutt,, ffiinnddss aa mmaattcchh ooff pp iinn SS.. FFoolllloowwiinngg ppsseeuuddooccooddee ccoommppuutteess tthhee mmaattcchhiinngg ccoommppoonneenntt ooff KKMMPP aallggoorriitthhmm:: KKMMPP--MMaattcchheerr((SS,,pp)) 11 nn  lleennggtthh[[SS]] 22 mm  lleennggtthh[[pp]] 33 ΠΠ  CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn((pp)) 44 qq  00 ////nnuummbbeerr ooff cchhaarraacctteerrss mmaattcchheedd 55 ffoorr ii  11 ttoo nn ////ssccaann SS ffrroomm lleefftt ttoo rriigghhtt 66 ddoo wwhhiillee qq >> 00 aanndd pp[[qq++11]] !!== SS[[ii]] 77 ddoo qq  ΠΠ[[qq]] ////nneexxtt cchhaarraacctteerr ddooeess nnoott mmaattcchh 88 iiff pp[[qq++11]] == SS[[ii]] 99 tthheenn qq  qq ++ 11 ////nneexxtt cchhaarraacctteerr mmaattcchheess 1100 iiff qq == mm ////iiss aallll ooff pp mmaattcchheedd?? 1111 tthheenn pprriinntt ““PPaatttteerrnn ooccccuurrss wwiitthh sshhiifftt”” ii –– mm 1122 qq  ΠΠ[[ qq]] //// llooookk ffoorr tthhee nneexxtt mmaattcchh NNoottee:: KKMMPP ffiinnddss eevveerryy ooccccuurrrreennccee ooff aa ‘‘pp’’ iinn ‘‘SS’’.. TThhaatt iiss wwhhyy KKMMPP ddooeess nnoott tteerrmmiinnaattee iinn sstteepp 1122,, rraatthheerr iitt sseeaarrcchheess rreemmaaiinnddeerr ooff ‘‘SS’’ ffoorr aannyy mmoorree ooccccuurrrreenncceess ooff ‘‘pp’’..
  • 14. IIlllluussttrraattiioonn:: ggiivveenn aa SSttrriinngg ‘‘SS’’ aanndd ppaatttteerrnn ‘‘pp’’ aass ffoolllloowwss:: SS bb a cc bb aa bb aa bb aa bb aa cc aa cc aa p aa bb aa bb aa cc aa Let us execute the KMP algorithm to find whether ‘p’ occurs in ‘S’. For ‘p’ the prefix function, Π was computed previously and is as follows: qq 11 22 33 44 55 66 77 pp aa bb aa bb aa cc aa ΠΠ 00 00 11 22 33 00 11
  • 15. Initially: n = size of S = 15; m = size of p = 7 Step 1: i = 1, q = 0 comparing p[1] with S[1] bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa Step 2: i = 2, q = 0 comparing p[1] with S[2] bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb S p P[1] does not match with S[1]. ‘p’ will be shifted one position to the right. S p aa bb aa bb aa cc aa P[1] matches S[2]. Since there is a match, p is not shifted.
  • 16. SStteepp 33:: ii == 33,, qq == 11 Comparing p[2] with S[3] bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb p aa bb aa bb aa cc aa bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb S aa bb aa bb aa cc aa S p S p p[2] does not match with S[3] Backtracking on p, comparing p[1] and S[3] Step 4: i = 4, q =co 0m paring p[1] with S[4] p[1] does not match with S[4] Step 5: i = 5, q c=o 0m paring p[1] with S[5] p[1] matches with S[5]
  • 17. bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa SStteepp 77:: ii == 77,, qq == 22 bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa SStteepp 66:: ii == 66,, qq == 11 S p Comparing p[2] with S[6] p[2] matches with S[6] S p Comparing p[3] with S[7] p[3] matches with S[7] SStteepp 88:: ii == 88,, qq == 33 Comparing p[4] with S[8] p[4] matches with S[8] S p
  • 18. SStteepp 99:: ii == 99,, qq == 44 Comparing p[5] with S[9] p SStteepp 1100:: ii == 1100,, qq == 55 p[5] matches with S[9] aa bb aa bb aa cc aa Comparing p[6] with S[10] p Backtracking on p, comparing p[4] with S[10] because after mismatch q = Π[5] = 3 SStteepp 1111:: ii == 1111,, qq == 44 p[6] doesn’t match with S[10] aa bb aa bb aa cc aa Comparing p[5] with S[11] S S S p bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb p[5] matches with S[11] bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa
  • 19. bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa bb aa cc bb aa bb aa bb aa bb aa cc aa aa bb aa bb aa bb aa cc aa SStteepp 1122:: ii == 1122,, qq == 55 Comparing p[6] with S[12] Comparing p[7] with S[13] S p S p SStteepp 1133:: ii == 1133,, qq == 66 p[6] matches with S[12] p[7] matches with S[13] Pattern ‘p’ has been found to completely occur in string ‘S’. The total number of shifts that took place for the match to be found are: i – m = 13 – 7 = 6 shifts.
  • 20. RRuunnnniinngg -- ttiimmee aannaallyyssiiss  CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn ((ΠΠ)) 11 mm  lleennggtthh[[pp]] ////’’pp’’ ppaatttteerrnn ttoo bbee mmaattcchheedd 22 ΠΠ[[11]]  00 33 kk  00 44 ffoorr qq  22 ttoo mm 55 ddoo wwhhiillee kk >> 00 aanndd pp[[kk++11]] !!== pp[[qq]] 66 ddoo kk  ΠΠ[[kk]] 77 IIff pp[[kk++11]] == pp[[qq]] 88 tthheenn kk  kk ++11 99 ΠΠ[[qq]]  kk 1100 rreettuurrnn ΠΠ IInn tthhee aabboovvee ppsseeuuddooccooddee ffoorr ccoommppuuttiinngg tthhee pprreeffiixx ffuunnccttiioonn,, tthhee ffoorr lloooopp ffrroomm sstteepp 44 ttoo sstteepp 1100 rruunnss ‘‘mm’’ ttiimmeess. SStteepp 11 ttoo sstteepp 33 ttaakkee ccoonnssttaanntt ttiimmee. HHeennccee tthhee rruunnnniinngg ttiimmee ooff ccoommppuuttee pprreeffiixx ffuunnccttiioonn iiss ΘΘ((mm)).  KKMMPP MMaattcchheerr 11 nn  lleennggtthh[[SS]] 22 mm  lleennggtthh[[pp]] 33 ΠΠ  CCoommppuuttee--PPrreeffiixx--FFuunnccttiioonn((pp)) 44 qq  00 55 ffoorr ii  11 ttoo nn 66 ddoo wwhhiillee qq >> 00 aanndd pp[[qq++11]] !!== SS[[ii]] 77 ddoo qq  ΠΠ[[qq]] 88 iiff pp[[qq++11]] == SS[[ii]] 99 tthheenn qq  qq ++ 11 1100 iiff qq == mm 1111 tthheenn pprriinntt ““PPaatttteerrnn ooccccuurrss wwiitthh sshhiifftt”” ii –– mm 1122 qq  ΠΠ[[ qq]] TThhee ffoorr lloooopp bbeeggiinnnniinngg iinn sstteepp 55 rruunnss ‘‘nn’’ ttiimmeess,, ii.ee.,, aass lloonngg aass tthhee lleennggtthh ooff tthhee ssttrriinngg ‘‘SS’’. SSiinnccee sstteepp 11 ttoo sstteepp 44 ttaakkee ccoonnssttaanntt ttiimmee,, tthhee rruunnnniinngg ttiimmee iiss ddoommiinnaatteedd bbyy tthhiiss ffoorr lloooopp. TThhuuss rruunnnniinngg ttiimmee ooff mmaattcchhiinngg ffuunnccttiioonn iiss ΘΘ((nn)).