of 14/14
String Matching Algorithms Kamalesh Karmakar Assistant Professor, Dept. of CSE & IT, Meghnad Saha Institute of Technology.

06. string matching

  • View
    309

  • Download
    5

Embed Size (px)

DESCRIPTION

String Matching

Text of 06. string matching

  • 1. String Matching AlgorithmsKamalesh KarmakarAssistant Professor,Dept. of CSE & IT,Meghnad Saha Institute of Technology.

2. String Matching Algorithms & ComplexityWeformalizethestring-matchingproblemasfollows.WeassumethatthetextisanarrayT[1..n]oflengthnandthatthepatternisanarrayP[1..m]oflengthmn.WefurtherassumethattheelementsofPandTarecharactersdrawnfromafinitealphabet. Forexample,wemayhave={0,1}or={a,b,...,z}.ThecharacterarraysPandTareoftencalledstringsofcharacters. 3. String Matching AlgorithmsWesaythatpatternPoccurswithshiftsintextT(or,equivalently,thatpatternPoccursbeginningatpositions+1intextT)if0snmandT[s+1..s+m]=P[1..m](thatis,ifT[s+j]=P[j],for1jm).IfPoccurswithshiftsinT,thenwecallsavalidshift;otherwise,wecallsaninvalidshift.Thestring-matchingproblemistheproblemoffindingallvalidshiftswithwhichagivenpatternPoccursinagiventextT.Figure32.1illustratesthesedefinitions. 4. Complexity comparison of Different Algorithms 5. The Naive string-matching algorithmThenaivealgorithmfindsallvalidshiftsusingaloopthatcheckstheconditionP[1..m]=T[s+1..s+m]foreachofthenm+1possiblevaluesofs.Procedure NAIVE-STRING-MATCHER takes time O((n m + 1)m), and this bound is tight in the worst case. 6. String matching with finite automata 7. String matching with finite automata 8. String matching with finite automata 9. String matching with finite automata 10. The Knuth-Morris-Pratt matching algorithm 11. The Knuth-Morris-Pratt matching algorithm 12. Example 13. Q & A