Program to construct a DFA which accept the language L = {anbm | n mod 2=0, m≥1}
Prerequisite – Finite Automata Introduction
Problem: Design a deterministic finite automata (DFA) for accepting the language ![]()
Regular expression for above language L is,
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.
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++
// C program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; (n)mod 2=0, m>=1 }#include <stdio.h>#include <string.h>// dfa tells the number associated// string end in which state.intdfa = 0;// This function is for// the starting state (Q0)of DFAvoidstart(charc){if(c =='a') {dfa = 1;}elseif(c =='b') {dfa = 3;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (Q1) of DFAvoidstate1(charc){if(c =='a') {dfa = 2;}elseif(c =='b') {dfa = 4;}else{dfa = -1;}}// This function is for the second state (Q2) of DFAvoidstate2(charc){if(c =='b') {dfa = 3;}elseif(c =='a') {dfa = 1;}else{dfa = -1;}}// This function is for the third state (Q3)of DFAvoidstate3(charc){if(c =='b') {dfa = 3;}elseif(c =='a') {dfa = 4;}else{dfa = -1;}}// This function is for the fourth state (Q4) of DFAvoidstate4(charc){dfa = -1;}intisAccepted(charstr[]){// store length of stringinti, len =strlen(str);for(i = 0; i < len; i++) {if(dfa == 0)start(str[i]);elseif(dfa == 1)state1(str[i]);elseif(dfa == 2)state2(str[i]);elseif(dfa == 3)state3(str[i]);elseif(dfa == 4)state4(str[i]);elsereturn0;}if(dfa == 3)return1;elsereturn0;}// driver codeintmain(){charstr[] ="aaaaaabbbb";if(isAccepted(str))printf("ACCEPTED");elseprintf("NOT ACCEPTED");return0;}// This code is contributed by SHUBHAMSINGH10.Java
// Java program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; (n)mod 2=0, m>=1 }classGFG{// dfa tells the number associated// string end in which state.staticintdfa =0;// This function is for// the starting state (Q0)of DFAstaticvoidstart(charc){if(c =='a'){dfa =1;}elseif(c =='b'){dfa =3;}// -1 is used to check for// any invalid symbolelse{dfa = -1;}}// This function is for the// first state (Q1) of DFAstaticvoidstate1(charc){if(c =='a'){dfa =2;}elseif(c =='b'){dfa =4;}else{dfa = -1;}}// This function is for the// second state (Q2) of DFAstaticvoidstate2(charc){if(c =='b'){dfa =3;}elseif(c =='a'){dfa =1;}else{dfa = -1;}}// This function is for the// third state (Q3)of DFAstaticvoidstate3(charc){if(c =='b'){dfa =3;}elseif(c =='a'){dfa =4;}else{dfa = -1;}}// This function is for the// fourth state (Q4) of DFAstaticvoidstate4(charc){dfa = -1;}staticintisAccepted(charstr[]){// store length of stringinti, len = str.length;for(i =0; i < len; i++){if(dfa ==0)start(str[i]);elseif(dfa ==1)state1(str[i]);elseif(dfa ==2)state2(str[i]);elseif(dfa ==3)state3(str[i]);elseif(dfa ==4)state4(str[i]);elsereturn0;}if(dfa ==3)return1;elsereturn0;}// Driver codepublicstaticvoidmain(String []args){charstr[] ="aaaaaabbbb".toCharArray();if(isAccepted(str) ==1)System.out.printf("ACCEPTED");elseSystem.out.printf("NOT ACCEPTED");}}// This code is contributed by 29AjayKumarPython 3
# Python3 program to implement DFS that accepts# all Stringing which follow the language# L = {a ^ n b ^ m (n)mod 2 = 0, m>= 1 }# This function is for the dfa = starting# dfa = state (zeroth) of DFAdefstart(c):if(c=='a'):dfa=1elif(c=='b'):dfa=3# -1 is used to check for any# invalid symbolelse:dfa=-1returndfa# This function is for the first# dfa = state of DFAdefstate1(c):if(c=='a'):dfa=2elif(c=='b'):dfa=4else:dfa=-1returndfa# This function is for the second# dfa = state of DFAdefstate2(c):if(c=='b'):dfa=3elif(c=='a'):dfa=1else:dfa=-1returndfa# This function is for the third# dfa = state of DFAdefstate3(c):if(c=='b'):dfa=3elif(c=='a'):dfa=4else:dfa=-1returndfa# This function is for the fourth# dfa = state of DFAdefstate4(c):dfa=-1returndfadefisAccepted(String):# store length of Stringingl=len(String)# dfa tells the number associated# with the present dfa = statedfa=0foriinrange(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:return0if(dfa==3) :return1else:return0# Driver codeif__name__=="__main__":String="aaaaaabbbb"if(isAccepted(String)) :print("ACCEPTED")else:print("NOT ACCEPTED")# This code is contributed by SHUBHAMSINGH10.C#
// C# program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; (n)mod 2=0, m>=1 }usingSystem;classGFG{// dfa tells the number associated// string end in which state.staticintdfa = 0;// This function is for// the starting state (Q0)of DFAstaticvoidstart(charc){if(c =='a'){dfa = 1;}elseif(c =='b'){dfa = 3;}// -1 is used to check for// any invalid symbolelse{dfa = -1;}}// This function is for the// first state (Q1) of DFAstaticvoidstate1(charc){if(c =='a'){dfa = 2;}elseif(c =='b'){dfa = 4;}else{dfa = -1;}}// This function is for the// second state (Q2) of DFAstaticvoidstate2(charc){if(c =='b'){dfa = 3;}elseif(c =='a'){dfa = 1;}else{dfa = -1;}}// This function is for the// third state (Q3)of DFAstaticvoidstate3(charc){if(c =='b'){dfa = 3;}elseif(c =='a'){dfa = 4;}else{dfa = -1;}}// This function is for the// fourth state (Q4) of DFAstaticvoidstate4(charc){dfa = -1;}staticintisAccepted(char[]str){// store length of stringinti, len = str.Length;for(i = 0; i < len; i++){if(dfa == 0)start(str[i]);elseif(dfa == 1)state1(str[i]);elseif(dfa == 2)state2(str[i]);elseif(dfa == 3)state3(str[i]);elseif(dfa == 4)state4(str[i]);elsereturn0;}if(dfa == 3)return1;elsereturn0;}// Driver codepublicstaticvoidMain(String []args){char[]str ="aaaaaabbbb".ToCharArray();if(isAccepted(str) == 1)Console.Write("ACCEPTED");elseConsole.Write("NOT ACCEPTED");}}// This code is contributed by 29AjayKumarPHP
<?php// PHP program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ;(n)mod 2=0, m>=1 }// This function is for the starting// state (zeroth) of DFAfunctionstart($c, &$dfa){if($c=='a')$dfa= 1;elseif($c=='b')$dfa= 3;// -1 is used to check for any// invalid symbolelse$dfa= -1;}// This function is for the first// state of DFAfunctionstate1($c, &$dfa){if($c=='a')$dfa= 2;elseif($c=='b')$dfa= 4;else$dfa= -1;}// This function is for the second// state of DFAfunctionstate2($c, &$dfa){if($c=='b')$dfa= 3;elseif($c=='a')$dfa= 1;else$dfa= -1;}// This function is for the third// state of DFAfunctionstate3($c, &$dfa){if($c=='b')$dfa= 3;elseif($c=='a')$dfa= 4;else$dfa= -1;}// This function is for the fourth// state of DFAfunctionstate4($c, &$dfa){$dfa= -1;}functionisAccepted($str,&$dfa){// store length of string$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);elsereturn0;}if($dfa== 3)return1;elsereturn0;}// Driver Code// dfa tells the number associated// with the present state$dfa= 0;$str=array("a","a","a","a","a","a","b","b","b","b");if(isAccepted($str,$dfa) != 0)echo"ACCEPTED";elseecho"NOT ACCEPTED";// This code is contributed// by Adesh Singh?>
Output:ACCEPTED


