2 views

Uploaded by Grama Silviu

- A Review of Prime Patterns
- Chapter Two: Finite Automata
- KTU_MTechCSEFinalSyllabi
- binomial theorem
- Study of LDPC Codes
- sop comp
- GCF
- Se Imo Function, maths
- Lecture on Advanced Mathematical Technques
- csitec
- New Resume
- Advanced Databases May2006 Nr420504
- Loria Presentation
- CCSIT - 2016
- Inequalities & Graphing
- Software Engineering
- Exploring the Use of the Human-Artifact Model for Studying Ubiquitous Interactions
- Some New Families of 4-Prime Cordial Graphs
- Data Cleansing Functions - InFORMATICA
- 1376_33 D1 June 2007

You are on page 1of 4

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/StringM

atch/kuthMP.htm

Knuth, Morris and Pratt discovered first linear time string-matching algorithm

by following a tight analysis of the nave algorithm. Knuth-Morris-Pratt

algorithm keeps the information that nave approach wasted gathered during the

scan of the text. By avoiding this waste of information, it achieves a running

time of O(n + m), which is optimal in the worst case sense. That is, in the

worst case Knuth-Morris-Pratt algorithm we have to examine all the characters

in the text and pattern at least once.

The KMP algorithm preprocess the pattern P by computing a failure function f

that indicates the largest possible shift s using previously performed

comparisons. Specifically, the failure function f(j) is defined as the length of

the longest prefix of P that is a suffix of P[i . . j].

Input: Pattern with m characters

Output: Failure function f for P[i . . j]

i1

j0

f(0) 0

while i < m do

if P[j] = P[i]

f(i) j +1

i i +1

j j + 1

else if

j f(j - 1)

else

f(i) 0

i i +1

Note that the failure function f for P, which maps j to the length of the longest

prefix of P that is a suffix of P[1 . . j], encodes repeated substrings inside the

pattern itself.

f(j), using above algorithm is

a 1 2

j

3 4 5

a b a

P[j]

c a b

0 0

f(j)

1 0 1 2

By observing the above mapping we can see that the longest prefix of pattern,

P, is "a b" which is also a suffix of pattern P.

Consider an attempt to match at position i, that is when the pattern P[0 ..m

-1] is aligned with text P[i . . i + m -1].

T: a b a c a a b a c c

P: a b a c a b

Assume that the first mismatch occurs between characters T[ i+ j] and P[j]

for 0 < j < m. In the above example, the first mismatch is T[5] = a and

P[5] = b.

Then, T[i . . i + j -1] = P[0 . . j -1] = u

That is, T[ 0 . . 4] = P[0 . . 4] = u, in the example [u = a b

a c a] and

T[i + j] P[j] i.e., T[5] P[5], In the example [T[5] = a

b = P[5]].

some suffix of the portion u of the text. In our example, u = a b a c a and v

= a b a c a, therefore, 'a' a prefix of v matches with 'a' a suffix of u. Let l(j)

be the length of the longest string P[0 . . j -1] of pattern that matches with

text followed by a character c different from P[j]. Then after a shift, the

comparisons can resume between characters T[i + j] and P[l(j)], i.e., T(5)

and P(1)

T: a b a c a a b a c c

P: a b a c a b

KNUTH-MORRIS-PRATT (T, P)

Input: Strings T[0 . . n] and P[0 . . m]

Output: Starting index of substring of T matching P

f compute failure function of Pattern P

i0

j0

while i < length[T] do

if j m-1 then

return i- m+1 // we have a match

i i +1

j j +1

else if j > 0

j f(j -1)

else

i i +1

Analysis

The running time of Knuth-Morris-Pratt algorithm is proportional to the time

needed to read the characters in text and pattern. In other words, the worst-case

running time of the algorithm is O(m + n) and it requires O(m) extra space.

It is important to note that these quantities are independent of the size of the

underlying alphabet.

- A Review of Prime PatternsUploaded byJaime
- Chapter Two: Finite AutomataUploaded bysonaliraiagarwal1
- KTU_MTechCSEFinalSyllabiUploaded bygpaswathy
- binomial theoremUploaded byAbhishek Jain
- Study of LDPC CodesUploaded byijsret
- GCFUploaded byMarietta Fragata Ramiterre
- Se Imo Function, mathsUploaded byAshish Manatosh Barik
- New ResumeUploaded bySathiya Murugesan
- sop compUploaded byNaveen Kashyap
- csitecUploaded byCS & IT
- Advanced Databases May2006 Nr420504Uploaded byNizam Institute of Engineering and Technology Library
- Lecture on Advanced Mathematical TechnquesUploaded byDebayan Gupta
- Loria PresentationUploaded bychkornaros
- CCSIT - 2016Uploaded byCS & IT
- Inequalities & GraphingUploaded byDaisyListening
- Software EngineeringUploaded bySemi Abbasi
- Exploring the Use of the Human-Artifact Model for Studying Ubiquitous InteractionsUploaded byIlya Shmorgun
- Some New Families of 4-Prime Cordial GraphsUploaded byIoan Degau
- Data Cleansing Functions - InFORMATICAUploaded byShiva CH
- 1376_33 D1 June 2007Uploaded byAlice_Caroline_8070
- C++ProgUploaded bythiru12741550
- Precalculus Note Sequence SeriesUploaded by01bourgeoisie 040104
- Problem Set 7Uploaded byRajnish Kumar
- HOMEWORK.docxUploaded bynom
- PG-May 2011Uploaded by9941461666
- AULAS ICOUploaded byIván Martínez Aguirre
- Cat Alan 4Uploaded byHoang Quan
- ACSSC.1990.523455Uploaded byAnonymous zN7CjfgnE
- Zeta SimpleUploaded byrichard_dalaud
- Online Learning - IT, CSUploaded byBulgantamir TJ

- (E-book IELTS) - LONGMAN - Focus on Advanced English Grammar Practice (1999)Uploaded byBiliga Petrut
- Succeed in Cambridge English Advanced - 10 PracUploaded byTiago Pedro
- PNAUploaded byGrama Silviu
- Mircea Ganga - Analiza Matematica IUploaded byCristi Nica
- Sports in the UKUploaded byGrama Silviu
- Knuth Morris Pratt String MatchingUploaded byGrama Silviu
- Knuth-Morris-Pratt Algorithm KENT.docUploaded byGrama Silviu
- OEGCA_Test2.pdfUploaded byila03
- SAT II PhysicsUploaded byvmsgr
- 10 Secrets for High School TestUploaded byapi-3714483
- Taking the SAT I Reasoning TestUploaded byvmsgr
- Ultimate New SAT Toolkit.pdfUploaded byGrama Silviu
- New SAT Writing WorkbookUploaded byvmsgr
- Business Vocabulary Use Intermediate MuyaUploaded bymd abdul khalek

- lecture-notes-ma12003.pdfUploaded byWhy
- Science 2009 Zimmer 198 9Uploaded byTalita De Silva
- Matrizes, Eliminação Gaussiana, Equações Lineares e Espaços VetoriaisUploaded byPedro Zimmermann
- csharp_variables(8).pdfUploaded byRakesh Rocky
- Heims Steve Joshua the Cybernetics Group 1991Uploaded bygwern
- Rig Evaluation MiSWACO New-LogoDay 2.pptUploaded byCerón Niño Santiago
- Tabernacle 0605Uploaded byelauwit
- Deac13uk10mm Uk Thermal Ac-compressor Leaflet WebUploaded bycatur yuliana
- r8200Uploaded byShameen McJester Dahalan
- 3 Muzharul Islam Archive_ Project_ Chittagong University, Chittagong, BangladeshUploaded byAditya Niloy
- P8A-AG3_UUploaded byManzar Khan
- MB_EN_CX_03Uploaded byjleh_ipasa
- 05 - Steam DistillationUploaded byHuỳnh TẤn Phát
- Instrucciones Air MouseUploaded bysoy1mas
- Comparison of R22 and R134aUploaded byMuhammad Bilal Farooq
- Skala Intimacy Attitude Scale-Revisid AsliUploaded byiravola
- art-3A10.1186-2Fs40327-015-0024-4Uploaded byaisyah sri lestari
- Anthropology ArticleUploaded byPrabhash Chand
- Embankment Failures Durign the Historic October 2015 Flood in South Carolina Paper 9Uploaded byArturo Castillo
- Macropinna MicrostomaUploaded byTina Chen
- Galaxy_v24n01_1965-10Uploaded bymoenobody
- Rendahl.html Pilots of the 40sUploaded byEric Doyle
- SmartLink_ModbusUploaded bycsongasz
- ProbabilityUploaded byIdris Ali
- 11_ & 12_ Language Related Assignment (New)Uploaded byMumtaz Ahmad
- MUJAHID PPT 1.pptxUploaded bySyed Khalid
- Education Web 2Uploaded bymercyisabella
- Quest-Password-Manager-Passcode-Instructions.pdfUploaded byAnonymous 6WF0cl5z
- vacancy_steno17_12092018Uploaded byTopRankers
- MICRO FOCUS - Object Oriented Programming With CobolUploaded byraneves