Program to build DFA that accepts the languages ending with “01” over the characters {0, 1}
Given a binary string str, the task is to build a DFA that accepts the languages ending with “01” over the characters {0, 1}.
Examples:
Input: str = “00011101”
Output: String Accepted
Explanation:
As the given string ends with “01”, therefore, it is accepted.Input: str = “00001111”
Output: String Rejected
Explanation:
As the given string ends with “11”, therefore, it is rejected.
Approach: To solve the above problem, below are the class state and member function needed:
- Pair 1: It deals with the first type of input i.e. 0 and links it with an object pointer to the next state.
- Pair 2: It deals with the second type of input i.e. 1 and links it with an object pointer to the next state.
- m_x: It defines whether a particular state is initial or final.
Class member functions are defined below:
- initialize(): This function takes the two pairs(used for two kinds of inputs) as parameters along with the state defining character(i or f).
- transition(): This acts as a transition table for the automata and with a particular input it goes from one state to next state.
- traverse(): This function takes the input string and passes it through the automata.
- check(): This function checks whether after the input has ended the end state is a final state or not. If it is final then the string is accepted otherwise rejected.
For this problem three states are needed. Hence, create three-class objects and initialize them with required values as:
- State 1: On input 0 it goes to State 2 and on input 1 it goes to itself.
- State 2: On input 0 it goes to itself and on input 1 it goes to State 3.
- State 3: On input 0 it goes to State 2 and on input 1 goes to State 1. Also, this is our final state.
Below is the implementation of the above approach:
C++14
// C++ program of a DFA that accepts // all string ending with "01" #include <iostream> #include <string> using namespace std; // Class for automata class State { private : // Data members to store the input pair< int , State*> Pair1; pair< int , State*> Pair2; char m_x; public : // Pointer to the state of automata static State* m_ptr; // Constructor to initialize state State() { Pair1.first = 0; Pair1.second = nullptr; Pair2.first = 0; Pair2.second = nullptr; m_x = ' ' ; } // Initialise pair1 and pair2 // with state x void initialize( pair< int , State*> pair1, pair< int , State*> pair2, char x) { Pair1 = pair1; Pair2 = pair2; m_x = x; } // Passes a string through automata static void transition( int input); static void traverse(string& str, int n); // Checks if the last state // is final or not static void check(); }; // Pointer to the current // state of automata State* State::m_ptr{ nullptr }; // Function to provide state // transition of automata void State::transition( int input) { if ((*m_ptr).Pair1.first == input) m_ptr = (*m_ptr).Pair1.second; else m_ptr = (*m_ptr).Pair2.second; } // Checks if the last state // is final or not void State::check() { if ((*m_ptr).m_x == 'f' ) cout << "String Accepted\n" << endl; else cout << "String Rejected\n" << endl; } // Passes a string through automata void State::traverse(string& str, int n) { for ( int i = 0; i < n; i++) { int x{ ( int )str[i] - ( int ) '0' }; transition(x); } } // Function to check if the given // is accepted in DFA or not void isAccepted(string str) { // States of the automata State one, two, three; // Transition table for required // automata one.initialize({ 0, &two }, { 1, &one }, 'i' ); two.initialize({ 0, &two }, { 1, &three }, 'i' ); three.initialize({ 0, &two }, { 1, &one }, 'f' ); int length{ static_cast < int >(str.length()) }; State::m_ptr = &one; // Function call State::traverse(str, length); State::check(); } // Driver Code int main() { string str{ "00111101" }; isAccepted(str); return 0; } |
String Accepted
Time Complexity: O(N)
Auxiliary Space: O(1)