In the previous post, we discussed Finite Automata based pattern searching algorithm. The FA (Finite Automata) construction method discussed in previous post takes O((m^3)*NO_OF_CHARS) time. FA can be constructed in O(m*NO_OF_CHARS) time. In this post, we will discuss the O(m*NO_OF_CHARS) algorithm for FA construction. The idea is similar to lps (longest prefix suffix) array construction discussed in the KMP algorithm. We use previously filled rows to fill a new row.
1) Fill the first row. All entries in first row are always 0 except the entry for pat character. For pat character, we always need to go to state 1.
2) Initialize lps as 0. lps for the first index is always 0.
3) Do following for rows at index i = 1 to M. (M is the length of the pattern)
…..a) Copy the entries from the row at index equal to lps.
…..b) Update the entry for pat[i] character to i+1.
…..c) Update lps “lps = TF[lps][pat[i]]” where TF is the 2D array which is being constructed.
Following is C/C++ implementation for the above algorithm.
pattern found at index 0 pattern found at index 10
Time Complexity for FA construction is O(M*NO_OF_CHARS). The code for search is same as the previous post and time complexity for it is O(n).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Finite Automata algorithm for Pattern Searching
- Pattern Searching using C++ library
- KMP Algorithm for Pattern Searching
- Naive algorithm for Pattern Searching
- Pattern Searching using Suffix Tree
- Pattern Searching using a Trie of all Suffixes
- Aho-Corasick Algorithm for Pattern Searching
- Optimized Naive Algorithm for Pattern Searching
- Rabin-Karp Algorithm for Pattern Searching
- Boyer Moore Algorithm for Pattern Searching
- Real time optimized KMP Algorithm for Pattern Searching
- Z algorithm (Linear time pattern searching Algorithm)
- Ukkonen's Suffix Tree Construction - Part 3
- Ukkonen's Suffix Tree Construction - Part 2
- Ukkonen's Suffix Tree Construction - Part 1
- Ukkonen's Suffix Tree Construction - Part 6
- Ukkonen's Suffix Tree Construction - Part 5
- Ukkonen's Suffix Tree Construction - Part 4
- Suffix Tree Application 2 - Searching All Patterns
- kasai’s Algorithm for Construction of LCP array from Suffix Array