Designing Deterministic Finite Automata (Set 10)
Last Updated :
29 Jan, 2024
Prerequisite: Designing finite automataÂ
In this article, we will see some designing of Deterministic Finite Automata (DFA).
Problem-1: Construction of a minimal DFA accepting set of strings over {a} in which {an | n≥0, n≠2 i.e, ‘n’ should be greater than 0 and not equal to 2}.Â
Explanation: The desired language will be like:Â
Â
L1 = {ε, a, aaa, aaaa, aaaaa, ..................}
Here ε is taken as string because value of ‘n’ is greater than or equal to zero and rest of the strings are having ‘a’ to the power of any positive natural number but not 2.Â
Below language is not accepted by this DFA because some of the string containing ‘a’ to the power 2.Â
Â
L2 = {aa, aaaaa, ..........}
This language L2 is not accepted by this required DFA because of its string containing ‘a’ to the power of 2.Â
The state transition diagram of the desired language will be like below:Â
Â
In the above DFA, the initial and final state’W’ on getting ‘a’ as the input it transits to a final state ‘X’. The final state ‘X’ on getting ‘a’ as the input it transits to a state ‘Y’. The state ‘Y’ on getting ‘a’ as the input it transits to a final state ‘Z’ which on getting any number of ‘a’ it remains in the state of itself.Â
Â
Python Implementation:
Â
C++
#include <iostream>
#include <string>
void stateW( const std::string& n);
void stateX( const std::string& n);
void stateY( const std::string& n);
void stateZ( const std::string& n);
void stateW( const std::string& n) {
if (n.empty()) {
std::cout << "string accepted" << std::endl;
} else if (n[0] == 'a' ) {
stateX(n.substr(1));
}
}
void stateX( const std::string& n) {
if (n.empty()) {
std::cout << "string accepted" << std::endl;
} else if (n[0] == 'a' ) {
stateY(n.substr(1));
}
}
void stateY( const std::string& n) {
if (n.empty()) {
std::cout << "string not accepted" << std::endl;
} else if (n[0] == 'a' ) {
stateZ(n.substr(1));
}
}
void stateZ( const std::string& n) {
if (n.empty()) {
std::cout << "string accepted" << std::endl;
} else if (n[0] == 'a' ) {
stateZ(n.substr(1));
}
}
int main() {
std::string assumedString = "aaa" ;
stateW(assumedString);
return 0;
}
|
Java
public class StateMachine {
public static void main(String[] args) {
String assumedString = "aaa" ;
stateW(assumedString);
}
static void stateW(String n) {
if (n.isEmpty()) {
System.out.println( "string accepted" );
} else if (n.charAt( 0 ) == 'a' ) {
stateX(n.substring( 1 ));
}
}
static void stateX(String n) {
if (n.isEmpty()) {
System.out.println( "string accepted" );
} else if (n.charAt( 0 ) == 'a' ) {
stateY(n.substring( 1 ));
}
}
static void stateY(String n) {
if (n.isEmpty()) {
System.out.println( "string not accepted" );
} else if (n.charAt( 0 ) == 'a' ) {
stateZ(n.substring( 1 ));
}
}
static void stateZ(String n) {
if (n.isEmpty()) {
System.out.println( "string accepted" );
} else if (n.charAt( 0 ) == 'a' ) {
stateZ(n.substring( 1 ));
}
}
}
|
Python3
def stateW(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateX(n[ 1 :])
def stateX(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateY(n[ 1 :])
def stateY(n):
if ( len (n) = = 0 ):
print ( "string not accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateZ(n[ 1 :])
def stateZ(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateZ(n[ 1 :])
n = input ()
stateW(n)
|
Problem-2: Construction of a minimal DFA accepting set of strings over {a} in which {an | n≥0, n≠2, n≠4 i.e, ‘n’ should be greater than 0 and not equal to 2 and 4}.Â
Explanation: The desired language will be like:Â
Â
L1 = {ε, a, aa, aaaaa, aaaaaa, .................. }
Here ε is taken as string because value of ‘n’ is greater than or equal to zero and rest of the strings are having ‘a’ to the power of any positive natural number but not 2 and 4.Â
Below language is not accepted by this DFA because some of the string containing ‘a’ to the power of 2 and 4.Â
Â
L2 = {aa, aaaaa, aaaaaaaaaa, ............. }
The state transition diagram of the desired language will be like below:Â
Â
In the above DFA, the initial and final state ‘A’ on getting ‘a’ as the input it transits to a final state ‘B’. The final state ‘B’ on getting ‘a’ as the input it transits to a state ‘C’. The state ‘C’ on getting ‘a’ as the input it transits to a final state ‘D’. The final state ‘D’ on getting ‘a’ as the input it transits to a state ‘E’. The state ‘E’ on getting ‘a’ as the input it transits to a final state ‘F’. The final state ‘F’ on getting ‘a’ as the input it remains in the state of itself.Â
Â
Python Implementation:
Â
Python3
def stateA(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateB(n[ 1 :])
def stateB(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateC(n[ 1 :])
def stateC(n):
if ( len (n) = = 0 ):
print ( "string not accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateD(n[ 1 :])
def stateD(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateE(n[ 1 :])
def stateE(n):
if ( len (n) = = 0 ):
print ( "string not accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateF(n[ 1 :])
def stateF(n):
if ( len (n) = = 0 ):
print ( "string accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateF(n[ 1 :])
n = input ()
stateA(n)
|
Share your thoughts in the comments
Please Login to comment...