Prerequisite – Finite Automata Introduction
Problem: Design a deterministic finite automata (DFA) for accepting the language 
Regular expression for above language L is,
L = (aa)*.b+
Examples:
Input: a a b b b
Output: ACCEPTED
// n = 2 (even) m=3 (>=1)
Input: a a a b b b
Output: NOT ACCEPTED
// n = 3 (odd), m = 3
Input: a a a a
Output: NOT ACCEPTED
// n = 4, m = 0( must be >=1)
Approaches:
There are 3 steps involve which results in acceptance of string:
- Construct FA for
means having even number of a’s.
- Construct FA for
means having any number of b’s greater than one.
- Concatenate the two FA and make single DFA.
Any other combination result is the rejection of the input string.
Description:
Given DFA has following states. State 3 leads to the acceptance of the string, whereas states 0, 1, 2 and 4 leads to the rejection of the string.
DFA State Transition Diagram:

Let’s see code for the demonstration:
C/C++
#include <stdio.h>
#include <string.h>
int dfa = 0;
void start( char c)
{
if (c == 'a' ) {
dfa = 1;
}
else if (c == 'b' ) {
dfa = 3;
}
else {
dfa = -1;
}
}
void state1( char c)
{
if (c == 'a' ) {
dfa = 2;
}
else if (c == 'b' ) {
dfa = 4;
}
else {
dfa = -1;
}
}
void state2( char c)
{
if (c == 'b' ) {
dfa = 3;
}
else if (c == 'a' ) {
dfa = 1;
}
else {
dfa = -1;
}
}
void state3( char c)
{
if (c == 'b' ) {
dfa = 3;
}
else if (c == 'a' ) {
dfa = 4;
}
else {
dfa = -1;
}
}
void state4( char c)
{
dfa = -1;
}
int isAccepted( char str[])
{
int i, len = strlen (str);
for (i = 0; i < len; i++) {
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else
return 0;
}
if (dfa == 3)
return 1;
else
return 0;
}
int main()
{
char str[] = "aaaaaabbbb" ;
if (isAccepted(str))
printf ( "ACCEPTED" );
else
printf ( "NOT ACCEPTED" );
return 0;
}
|
Java
class GFG
{
static int dfa = 0 ;
static void start( char c)
{
if (c == 'a' )
{
dfa = 1 ;
}
else if (c == 'b' )
{
dfa = 3 ;
}
else
{
dfa = - 1 ;
}
}
static void state1( char c)
{
if (c == 'a' )
{
dfa = 2 ;
}
else if (c == 'b' )
{
dfa = 4 ;
}
else
{
dfa = - 1 ;
}
}
static void state2( char c)
{
if (c == 'b' )
{
dfa = 3 ;
}
else if (c == 'a' )
{
dfa = 1 ;
}
else
{
dfa = - 1 ;
}
}
static void state3( char c)
{
if (c == 'b' )
{
dfa = 3 ;
}
else if (c == 'a' )
{
dfa = 4 ;
}
else
{
dfa = - 1 ;
}
}
static void state4( char c)
{
dfa = - 1 ;
}
static int isAccepted( char str[])
{
int i, len = str.length;
for (i = 0 ; i < len; i++)
{
if (dfa == 0 )
start(str[i]);
else if (dfa == 1 )
state1(str[i]);
else if (dfa == 2 )
state2(str[i]);
else if (dfa == 3 )
state3(str[i]);
else if (dfa == 4 )
state4(str[i]);
else
return 0 ;
}
if (dfa == 3 )
return 1 ;
else
return 0 ;
}
public static void main(String []args)
{
char str[] = "aaaaaabbbb" .toCharArray();
if (isAccepted(str) == 1 )
System.out.printf( "ACCEPTED" );
else
System.out.printf( "NOT ACCEPTED" );
}
}
|
Python 3
def start(c):
if (c = = 'a' ):
dfa = 1
elif (c = = 'b' ):
dfa = 3
else :
dfa = - 1
return dfa
def state1(c):
if (c = = 'a' ):
dfa = 2
elif (c = = 'b' ):
dfa = 4
else :
dfa = - 1
return dfa
def state2(c):
if (c = = 'b' ):
dfa = 3
elif (c = = 'a' ):
dfa = 1
else :
dfa = - 1
return dfa
def state3(c):
if (c = = 'b' ):
dfa = 3
elif (c = = 'a' ):
dfa = 4
else :
dfa = - 1
return dfa
def state4(c):
dfa = - 1
return dfa
def isAccepted(String):
l = len (String)
dfa = 0
for i in range (l):
if (dfa = = 0 ):
dfa = start(String[i])
elif (dfa = = 1 ):
dfa = state1(String[i])
elif (dfa = = 2 ) :
dfa = state2(String[i])
elif (dfa = = 3 ) :
dfa = state3(String[i])
elif (dfa = = 4 ) :
dfa = state4(String[i])
else :
return 0
if (dfa = = 3 ) :
return 1
else :
return 0
if __name__ = = "__main__" :
String = "aaaaaabbbb"
if (isAccepted(String)) :
print ( "ACCEPTED" )
else :
print ( "NOT ACCEPTED" )
|
C#
using System;
class GFG
{
static int dfa = 0;
static void start( char c)
{
if (c == 'a' )
{
dfa = 1;
}
else if (c == 'b' )
{
dfa = 3;
}
else
{
dfa = -1;
}
}
static void state1( char c)
{
if (c == 'a' )
{
dfa = 2;
}
else if (c == 'b' )
{
dfa = 4;
}
else
{
dfa = -1;
}
}
static void state2( char c)
{
if (c == 'b' )
{
dfa = 3;
}
else if (c == 'a' )
{
dfa = 1;
}
else
{
dfa = -1;
}
}
static void state3( char c)
{
if (c == 'b' )
{
dfa = 3;
}
else if (c == 'a' )
{
dfa = 4;
}
else
{
dfa = -1;
}
}
static void state4( char c)
{
dfa = -1;
}
static int isAccepted( char []str)
{
int i, len = str.Length;
for (i = 0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else
return 0;
}
if (dfa == 3)
return 1;
else
return 0;
}
public static void Main(String []args)
{
char []str = "aaaaaabbbb" .ToCharArray();
if (isAccepted(str) == 1)
Console.Write( "ACCEPTED" );
else
Console.Write( "NOT ACCEPTED" );
}
}
|
PHP
<?php
function start( $c , & $dfa )
{
if ( $c == 'a' )
$dfa = 1;
elseif ( $c == 'b' )
$dfa = 3;
else $dfa = -1;
}
function state1( $c , & $dfa )
{
if ( $c == 'a' )
$dfa = 2;
elseif ( $c == 'b' )
$dfa = 4;
else
$dfa = -1;
}
function state2( $c , & $dfa )
{
if ( $c == 'b' )
$dfa = 3;
elseif ( $c == 'a' )
$dfa = 1;
else
$dfa = -1;
}
function state3( $c , & $dfa )
{
if ( $c == 'b' )
$dfa = 3;
elseif ( $c == 'a' )
$dfa = 4;
else
$dfa = -1;
}
function state4( $c , & $dfa )
{
$dfa = -1;
}
function isAccepted( $str ,& $dfa )
{
$i = 0; $len = sizeof( $str );
for ( $i = 0; $i < $len ; $i ++)
{
if ( $dfa == 0)
start( $str [ $i ], $dfa );
elseif ( $dfa == 1)
state1( $str [ $i ], $dfa );
elseif ( $dfa == 2)
state2( $str [ $i ], $dfa );
elseif ( $dfa == 3)
state3( $str [ $i ], $dfa );
elseif ( $dfa == 4)
state4( $str [ $i ], $dfa );
else
return 0;
}
if ( $dfa == 3)
return 1;
else
return 0;
}
$dfa = 0;
$str = array ( "a" , "a" , "a" , "a" , "a" ,
"a" , "b" , "b" , "b" , "b" );
if (isAccepted( $str , $dfa ) != 0)
echo "ACCEPTED" ;
else
echo "NOT ACCEPTED" ;
?>
|
Output:
ACCEPTED
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing
Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.