Designing Deterministic Finite Automata (Set 4)
Last Updated :
28 Jan, 2024
Prerequisite:
Designing finite automata
,
Designing Deterministic Finite Automata (Set 3)
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, b} in which every ‘a’ is followed by a ‘b’.
Explanation:
The desired language will be like:
L1 = {ε, ab, abab, abbbb, ababababab, ..............}
Here as we can see that each string of the language containing ‘a’ just followed by ‘b’ but the below language is not accepted by this DFA because some of the string of the below language does not contain ‘a’ just followed by ‘b’.
L2 = {ba, baab, bbaba, ..............}
The state transition diagram of the language containing ‘a’ just followed by ‘b’ will be like:
In the above DFA, state ‘W’ is the initial and final state too which on getting ‘b’ as the input it remains in the state of itself and on getting ‘a’ as the input it transit to a normal state ‘X’ which on getting ‘b’ as the input it transit to the final state ‘W’. The state ‘X’ on getting ‘a’ as input it transit to the dead state ‘Z’. The state ‘Z’ is called dead state because on getting any input it can not transit to the final state ever.
C++
#include <iostream>
#include <string>
void stateW( const std::string& n);
void stateX( const std::string& n);
void stateZ( const std::string& n);
void stateW( const std::string& n) {
if (n.empty()) {
std::cout << "Accepted" << std::endl;
} else {
if (n[0] == 'a' ) {
stateX(n.substr(1));
}
else if (n[0] == 'b' ) {
stateW(n.substr(1));
}
}
}
void stateX( const std::string& n) {
if (n.empty()) {
std::cout << "Not Accepted" << std::endl;
} else {
if (n[0] == 'a' ) {
stateZ(n.substr(1));
}
else if (n[0] == 'b' ) {
stateW(n.substr(1));
}
}
}
void stateZ( const std::string& n) {
if (n.empty()) {
std::cout << "Not Accepted" << std::endl;
} else {
if (n[0] == 'a' || n[0] == 'b' ) {
stateZ(n.substr(1));
}
}
}
int main() {
std::string inputString= "abab" ;
stateW(inputString);
return 0;
}
|
Java
public class StateMachine {
public static void stateW(String n) {
if (n.isEmpty()) {
System.out.println( "Accepted" );
} else {
if (n.charAt( 0 ) == 'a' ) {
stateX(n.substring( 1 ));
}
else if (n.charAt( 0 ) == 'b' ) {
stateW(n.substring( 1 ));
}
}
}
public static void stateX(String n) {
if (n.isEmpty()) {
System.out.println( "Not Accepted" );
} else {
if (n.charAt( 0 ) == 'a' ) {
stateZ(n.substring( 1 ));
}
else if (n.charAt( 0 ) == 'b' ) {
stateW(n.substring( 1 ));
}
}
}
public static void stateZ(String n) {
if (n.isEmpty()) {
System.out.println( "Not Accepted" );
} else {
if (n.charAt( 0 ) == 'a' || n.charAt( 0 ) == 'b' ) {
stateZ(n.substring( 1 ));
}
}
}
public static void main(String[] args) {
String inputString = "abab" ;
stateW(inputString);
}
}
|
Python3
def stateW(n):
if ( len (n) = = 0 ):
print ("Accepted")
else :
if (n[ 0 ] = = 'a' ):
stateX(n[ 1 :])
elif (n[ 0 ] = = 'b' ):
stateW(n[ 1 :])
def stateX(n):
if ( len (n) = = 0 ):
print ("Not Accepted")
else :
if (n[ 0 ] = = 'a' ):
stateZ(n[ 1 :])
elif (n[ 0 ] = = 'b' ):
stateW(n[ 1 :])
def stateZ(n):
if ( len (n) = = 0 ):
print ("Not Accepted")
else :
if (n[ 0 ] = = 'a' or n[ 0 ] = = 'b' ):
stateZ(n[ 1 :])
n = input ()
stateA(n)
|
C#
using System;
class StateMachine
{
static void StateW( string n)
{
if ( string .IsNullOrEmpty(n))
{
Console.WriteLine( "Accepted" );
}
else
{
if (n[0] == 'a' )
{
StateX(n.Substring(1));
}
else if (n[0] == 'b' )
{
StateW(n.Substring(1));
}
}
}
static void StateX( string n)
{
if ( string .IsNullOrEmpty(n))
{
Console.WriteLine( "Not Accepted" );
}
else
{
if (n[0] == 'a' )
{
StateZ(n.Substring(1));
}
else if (n[0] == 'b' )
{
StateW(n.Substring(1));
}
}
}
static void StateZ( string n)
{
if ( string .IsNullOrEmpty(n))
{
Console.WriteLine( "Not Accepted" );
}
else
{
if (n[0] == 'a' || n[0] == 'b' )
{
StateZ(n.Substring(1));
}
}
}
static void Main()
{
string inputString = "abab" ;
StateW(inputString);
}
}
|
Output:
Accepted
Problem-2:
Construction of a minimal DFA accepting set of strings over {a, b} in which every ‘a’ is never be followed by ‘b’
Explanation:
The desired language will be like:
L1 = {ε, a, aa, aaaa, b, bba, bbbbba..............}
Here as we can see that each string of the language containing ‘a’ is never be followed by ‘b’ but the below language is not accepted by this DFA because some of the string of the below language containing ‘a’ is followed by ‘b’.
L2 = {ba, baab, bbaba, ..............}
The state transition diagram of the language containing ‘a’ never be followed by ‘b’ will be like:
In the above DFA, state ‘X’ is the initial and final state which on getting ‘b’ as the input it remains in the state of itself and on getting ‘a’ as input it transit to the final state ‘Y’ which on getting ‘a’ as the input it remains in the state of itself and on getting ‘b’ as input transit to the dead state ‘Z’. The state ‘Z’ is called dead state this is because it can not ever go to any of the final states.
Python3
def stateX(n):
if ( len (n) = = 0 ):
print ("Accepted")
else :
if (n[ 0 ] = = 'a' ):
stateY(n[ 1 :])
elif (n[ 0 ] = = 'b' ):
stateX(n[ 1 :])
def stateY(n):
if ( len (n) = = 0 ):
print ("Accepted")
else :
if (n[ 0 ] = = 'a' ):
stateZ(n[ 1 :])
elif (n[ 0 ] = = 'b' ):
stateY(n[ 1 :])
def stateZ(n):
if ( len (n) = = 0 ):
print ("Not Accepted")
else :
if (n[ 0 ] = = 'a' or n[ 0 ] = = 'b' ):
stateZ(n[ 1 :])
n = input ()
stateA(n)
|
Share your thoughts in the comments
Please Login to comment...