0% found this document useful (0 votes)
9 views23 pages

String Matching With Finite Automata and KMP

Uploaded by

Dipanjali Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

String Matching With Finite Automata and KMP

Uploaded by

Dipanjali Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

STRING MATCHING WITH

FINITE AUTOMATA
FINITE AUTOMATA

• A finite automaton M, illustrated in Figure , is a 5-tuple (Q,q0,A,Σ,δ) ,

Where,
• Q is a finite set of states,
• q0 ∈ Q is the start state,
• A ⊆ Q is a distinguished set of accepting states,
• Σ is a finite input alphabet,
• δ is a function that maps from Q X Σ to Q, called the transition function of M.
FINITE AUTOMATA

• The finite automaton begins in state q0 and reads the characters of its
input string one at a time. If the automaton is in state q and reads input
character a, it moves (“makes a transition”) from state q to state δ (q,a).
Whenever its current state q is a member of A, the machine M has
accepted the string read so far. An input that is not accepted is
rejected
STRING-MATCHING AUTOMATA

• For a given pattern P, we construct a string-matching automaton in a


preprocessing step before using it to search the text string. Figure
illustrates how we construct the automaton for the pattern P =
ababaca. From now on, we shall assume that P is a given fixed pattern
string;
• Construct the string-matching automaton for the pattern P = ababaca
and illustrate its operation on the text string T = abababacaba.
STRING-MATCHING AUTOMATA
EXERCISE

• Construct the string-matching automaton for the pattern P = aabab


and illustrate its operation on the text string T = aaababaabaababaab.
SOLUTION
• Draw a state-transition diagram for a string-matching automaton for
the pattern ababbabbababbababbabb over the alphabet Σ={a,b}
SOLUTION
EXERCISE

• Construct a prefix array of ababaca


THE KNUTH-MORRIS-PRATT ALGORITHM (KMP)

Example: Compute Π for the pattern 'p' below:


THE KMP
MATCHER
• Step1: take two variables i and j where i= text(1) q=Pattern(0)
• Compare t(i)==P[q+1]
• If match, then move both I and q.
• If does not match move q to the location as per pi table.
• If q==0 and no match then move I,
• Pattern 'P' has been found to complexity occur in a string 'T' The total
number of shifts that took place for the match to be found is i -m = 13 -
7 = 6 shifts.
EXERCISE

• Given T=ababcabcabababd P=ababd. Find whether the pattern is in


Text T using KMP algorithm.
• Compare the complexity of Naïve algorithm, Rabin Karp algorithm, KMP
algorithm and Finite Automata for string matching and give your
opinion on which algorithm is best and why.

You might also like