
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Perform Finite State Automaton based Search
An automaton with a finite number of states is called a Finite Automaton. We can use a Finite State Automaton (FSA) to perform optimized pattern search operation on a string. This article will discuss how to implement a Finite State Automaton based search in C++.
Problem Statement: Given two strings text[] and pattern[], we want to find all occurrences of the pattern in the text using a Finite State Automaton.
// Input Strings text[] = "tutorialsPoint provides premium tutorials on various topics" pattern[] = "tutorials" // Output Pattern found at index 0 and 32
Finite State Automaton Based Search
Finite State Automaton (FSA) is a computational model that can be used to recognize patterns in strings. It consists of a finite number of states, transitions between those states, and an initial state.
To implement a Finite State Automaton based search, we need to define the following components:
- States: This specifies different conditions your system can be in.
- Alphabet: The set of possible characters (like a-z or ASCII).
- Transition Function: This will tells you where to go next from a state when you read a character.
- Start State: The state where the automaton begins its operation.
- Accept State(s): The end state. If you reach this state after reading the input, it means the input is accepted by the automaton.
For example, suppose that you want to find all places where the pattern "AABA" occurs in a big text like "AABAACAADAABAABA". Follow this steps,
- Build a DFA (finite automaton) for the pattern "AABA".
- Simulate reading the big text through this DFA.
- Whenever you reach the final state, you found a match.
This is how DFA works:
State 0: Nothing matched yet State 1: 'A' matched State 2: 'AA' matched State 3: 'AAB' matched State 4: 'AABA' matched ? Match found
Algorithm for Finite State Automaton Based Search
To implement a Finite State Automaton based search, we can follow these steps:
- Define a 2D array TF[state][char] which tell the next state for each character.
- Set the start state startState to 0. and the accept state acceptState to the length of the pattern.
- Initialize the transition function TF based on the pattern.
- For each character in the text, update the current state using the transition function. ie, Update: state = TF[state][char]
- If the current state is equal to the accept state, it means we found a match. Print the index of the match.
C++ Code for Finite State Automaton Based Search
The code below implements the Finite State Automaton based search in C++:
#include <iostream> #include <vector> #include <string> using namespace std; const int NO_OF_CHARS = 256; void buildTransitionTable(const string& pattern, vector<vector<int>>& TF) { int m = pattern.length(); TF.assign(m + 1, vector<int>(NO_OF_CHARS, 0)); for (int state = 0; state <= m; ++state) { for (int x = 0; x < NO_OF_CHARS; ++x) { int nextState = 0; if (state < m && x == pattern[state]) nextState = state + 1; else { for (int k = state; k > 0; --k) { if (pattern[k - 1] == x) { int i = 0; for (; i < k - 1; ++i) if (pattern[i] != pattern[state - k + 1 + i]) break; if (i == k - 1) nextState = k; } } } TF[state][x] = nextState; } } } void searchPattern(const string& pattern, const string& text) { vector<vector<int>> TF; buildTransitionTable(pattern, TF); int state = 0; int m = pattern.length(); int n = text.length(); for (int i = 0; i < n; ++i) { state = TF[state][text[i]]; if (state == m) cout << "Pattern found at index " << (i - m + 1) << endl; } } int main() { string text = "AABAACAADAABAABA"; string pattern = "AABA"; searchPattern(pattern, text); return 0; }
The output of this code will be:
Pattern found at index 0 Pattern found at index 9
Time and Space Complexity
Time Complexity: The time complexity is O(m^2) for building the transition table, where m is the length of the pattern. The search operation takes O(n) time, where n is the length of the text. Therefore, the overall time complexity is O(m^2 + n).
Space Complexity: The space complexity is O(m) for storing the transition table, where m is the length of the pattern.