Lecture 37 String Matching
Lecture 37 String Matching
Branch - CSE
Design and Analysis of Algorithms
Lecture – 37
Analysis: This for loop from 3 to 5 executes for n-m + 1(we need at least m characters
at the end) times and in iteration we are doing m comparisons.
So the total complexity is O (n-m+1)m.
Example on Naïve String Matching Algorithm:
Let T = 1011101110 & P = 111 . Find valid Shift using Naïve String Matching
Algorithm.
Example on Naïve String Matching Algorithm:
Example on Naïve String Matching Algorithm:
Finite Automata String Matching Algorithm:
Finite Automata:
A finite automaton M 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 notable set of accepting states,
• ∑ is a finite input alphabet,
• δ is a function from Q x ∑ into Q called the transition function of M.
The finite automaton starts 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
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 allowed is rejected.
Finite Automata String Matching Algorithm:
FINITE- AUTOMATON-MATCHER (T,δ, m):
1. n ← length [T]
2. q ← 0
3. For i ← 1 to n
4. do q ← δ (q, T[i])
5. If q =m
6. then s ←i - m
7. Print "Pattern occurs with shift s" s