Designing Deterministic Finite Automata (Set 3)
Prerequisite: Designing finite automata, Designing Deterministic Finite Automata (Set 2)Â
In this article, we will see some designing of Deterministic Finite Automata (DFA).Â
Problem-1: Construction of a minimal DFA accepting set of string over {a, b} where each string containing ‘a’ as the substring.Â
Explanation:Â
The desired language will be like:Â
Â
L1 = {a, aa, ab, ba, ..............}
Here as we can see that each string of the language containing ‘a’ as the substring but the below language is not accepted by this DFA because some of the string of the below language does not contain ‘a’ as the substring.Â
Â
L2 = {b, bb, bbb, ..............}
The state transition diagram of the language containing ‘a’ as the substring will be like:Â
Â
Code Implementation:
C++
#include <iostream>
#include <string>
class DFA {
public :
void processInput( const std::string& input) {
bool containsA = false ;
for ( char c : input) {
if (c == 'a' ) {
containsA = true ;
break ;
}
else if (c == 'b' ) {
}
else {
std::cout << "Not Accepted" << std::endl;
return ;
}
}
if (containsA) {
std::cout << "Accepted" << std::endl;
} else {
std::cout << "Not Accepted" << std::endl;
}
}
};
int main() {
DFA dfa;
std::string input4 = "aba" ;
dfa.processInput(input4);
return 0;
}
|
Java
public class DFA {
public void processInput(String input) {
boolean containsA = false ;
for ( char c : input.toCharArray()) {
if (c == 'a' ) {
containsA = true ;
break ;
}
else if (c == 'b' ) {
}
else {
System.out.println( "Not Accepted" );
return ;
}
}
if (containsA) {
System.out.println( "Accepted" );
} else {
System.out.println( "Not Accepted" );
}
}
public static void main(String[] args) {
DFA dfa = new DFA();
String input = "aba" ;
dfa.processInput(input);
}
}
|
Output:
Accepted
In the above DFA, states ‘X’ and ‘Y’ are the initial and final state respectively, it accepts all the strings containing ‘a’ as the substring. Here as we see that on getting input as ‘b’ it remains in the state of ‘X’ itself but on getting ‘a’ as the input it transit to final state ‘Y’ and hence such string is accepted by above DFA.Â
Problem-2: Construction of a minimal DFA accepting set of string over {a, b} where each string containing ‘ab’ as the substring.Â
Explanation: The desired language will be like:Â
Â
L1 = {ab, aab, abb, bab, ..............}
Here as we can see that each string of the language containing ‘ab’ as the substring but the below language is not accepted by this DFA because some of the string of the below language does not contain ‘ab’ as the substring-Â
Â
L2 = {aba, bba, bbbaaa, ..............}
The state transition diagram of the language containing ‘ab’ as the substring will be like:Â
Â
In the above DFA, states ‘X’ and ‘Z’ are the initial and final state respectively, it accepts all the strings containing ‘ab’ as the substring. Here as we see that on getting ‘b’ as the input it remains in the state of initial state itself, on getting ‘a’ as the input it transit to state ‘Y’ and then on getting ‘b’ it finally transit to the final state ‘Z’ and hence this DFA is accepting all the language containing ‘ab’ as the substring.
Â
Last Updated :
29 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...