The Wayback Machine - https://web.archive.org/web/20240607080331/https://www.geeksforgeeks.org/dfa-for-accepting-the-language-l-an-bm-nmeven/
Open In App

DFA for accepting the language L = { anbm | n+m=even }

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Design a deterministic finite automata(DFA) for accepting the language L = {a^n b^{m} | m+n=even}
For creating DFA for language, L = { a^n b^m ; n+m=even } use elementary mathematics, which says- 
even + even = even and odd + odd = even


Examples: 

Input:  a a b b     // n = 2, m = 2, 2 + 2 = 4 (even)
Output:  ACCEPTED

Input:  a a a b b b b      // n = 3, m = 4, 3 + 4 = 7 (odd) 
Output:  NOT ACCEPTED

Input:  a a a b b b     // n = 3, m = 3, 3 + 3 = 6 (even)
Output:  ACCEPTED


Approaches: 
There is 2 cases that results in acceptance of string:  

  1. If both n and m are even then their sum will be even
  2. If both n and m are odd then their sum will be even


Description: 
Given DFA has 2 parts. The first part consists of states 0, 1, 5, and 6 which is for both n and m is odd. The second part consists of states 2, 3, and 4 is for both n and m is even.
DFA State Transition Diagram:


Let’s see the code for the demonstration: 

C++

// C++ program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
#include <bits/stdc++.h>
using namespace std;
 
// dfa tells the number associated
// with the present state
int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
    if (c == 'a')
        dfa = 1;
    else if (c == 'b')
        dfa = 2;
 
    // -1 is used to check for any invalid symbol
    else
        dfa = -1;
}
 
// This function is for the first state of DFA
void state1(char c)
{
    if (c == 'a')
        dfa = 0;
    else if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
// This function is for the second state of DFA
void state2(char c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the third state of DFA
void state3(char c)
{
    if (c == 'b')
        dfa = 4;
    else
        dfa = -1;
}
 
// This function is for the fourth state of DFA
void state4(char c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the fifth state of DFA
void state5(char c)
{
    if (c == 'b')
        dfa = 6;
    else
        dfa = -1;
}
 
// This function is for the sixth state of DFA
void state6(char c)
{
    if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
int isAccepted(char str[])
{
    // Store length of string
    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 if (dfa == 5)
            state5(str[i]);
 
        else if (dfa == 6)
            state6(str[i]);
 
        else
            return 0;
    }
    if (dfa == 3 || dfa == 5)
        return 1;
    else
        return 0;
}
 
// Driver code
int main()
{
    char str[] = "aaabbb";
    if (isAccepted(str))
        cout << "\nACCEPTED\n";
    else
        cout << "NOT ACCEPTED\n";
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

                    

C

// C program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
#include <stdio.h>
#include <string.h>
 
// dfa tells the number associated
// with the present state
int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
    if (c == 'a')
        dfa = 1;
    else if (c == 'b')
        dfa = 2;
 
    // -1 is used to check for any invalid symbol
    else
        dfa = -1;
}
 
// This function is for the first state of DFA
void state1(char c)
{
    if (c == 'a')
        dfa = 0;
    else if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
// This function is for the second state of DFA
void state2(char c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the third state of DFA
void state3(char c)
{
    if (c == 'b')
        dfa = 4;
    else
        dfa = -1;
}
 
// This function is for the fourth state of DFA
void state4(char c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the fifth state of DFA
void state5(char c)
{
    if (c == 'b')
        dfa = 6;
    else
        dfa = -1;
}
 
// This function is for the sixth state of DFA
void state6(char c)
{
    if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
int isAccepted(char str[])
{
    // store length of string
    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 if (dfa == 5)
            state5(str[i]);
 
        else if (dfa == 6)
            state6(str[i]);
 
        else
            return 0;
    }
    if (dfa == 3 || dfa == 5)
        return 1;
    else
        return 0;
}
 
// driver code
int main()
{
    char str[] = "aaabbb";
    if (isAccepted(str))
        printf("\nACCEPTED\n");
    else
        printf("NOT ACCEPTED\n");
    return 0;
}

                    

Java

// Java program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
class GFG {
 
    // dfa tells the number associated
    // with the present state.
    static int dfa = 0;
 
    // This function is for
    // the starting state (Q0)of DFA
    static void start(char c)
    {
        if (c == 'a') {
            dfa = 1;
        }
        else if (c == 'b') {
            dfa = 2;
        }
 
        // -1 is used to check for any invalid symbol
        else {
            dfa = -1;
        }
    }
 
    // This function is for
    // the first state (Q1) of DFA
    static void state1(char c)
    {
        if (c == 'a') {
            dfa = 0;
        }
        else if (c == 'b') {
            dfa = 5;
        }
        else {
            dfa = -1;
        }
    }
 
    // This function is for the
    // second state (Q2) of DFA
    static void state2(char c)
    {
        if (c == 'b') {
            dfa = 3;
        }
        else {
            dfa = -1;
        }
    }
 
    // This function is for the
    // third state (Q3)of DFA
    static void state3(char c)
    {
        if (c == 'b') {
            dfa = 4;
        }
        else {
            dfa = -1;
        }
    }
 
    // This function is for the
    // fourth state (Q4) of DFA
    static void state4(char c)
    {
        if (c == 'b') {
            dfa = 3;
        }
        else {
            dfa = -1;
        }
    }
 
    // This function is for the
    // fifth state (Q5) of DFA
    static void state5(char c)
    {
        if (c == 'b') {
            dfa = 6;
        }
        else {
            dfa = -1;
        }
    }
 
    // This function is for the
    // sixth state (Q6) of DFA
    static void state6(char c)
    {
        if (c == 'b') {
            dfa = 5;
        }
        else {
            dfa = -1;
        }
    }
 
    static int isAccepted(char str[])
    {
        // store length of string
        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 if (dfa == 5)
                state5(str[i]);
 
            else if (dfa == 6)
                state6(str[i]);
 
            else
                return 0;
        }
        if (dfa == 3 || dfa == 5)
            return 1;
        else
            return 0;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        char str[] = "aaabbb".toCharArray();
        if (isAccepted(str) == 1)
            System.out.println("ACCEPTED");
        else
            System.out.println("NOT ACCEPTED");
    }
}

                    

Python3

# Python3 program to implement DFA that accepts
# all strings which follow the language
# L = a ^ n b ^ m n + m = even
 
# This function is for the dfa = starting
# dfa = state (zeroth) of DFA
def start(c):
    if (c == 'a'):
        dfa = 1
    elif (c == 'b'):
        dfa = 2
     
    # -1 is used to check for any
    # invalid symbol
    else:
        dfa = -1
    return dfa
 
# This function is for the first
# dfa = state of DFA
def state1(c):
    if (c == 'a'):
        dfa = 0
    elif (c == 'b'):
        dfa = 5
    else:
        dfa = -1
    return dfa
 
# This function is for the second
# dfa = state of DFA
def state2(c):
    if (c == 'b'):
        dfa = 3
    else:
        dfa = -1
    return dfa
 
# This function is for the third
# dfa = state of DFA
def state3(c):
    if (c == 'b'):
        dfa = 4
    else:
        dfa = -1
    return dfa
 
# This function is for the fourth
# dfa = state of DFA
def state4(c):
    if (c == 'b'):
        dfa = 3
    else:
        dfa = -1
    return dfa
 
# This function is for the fifth
# dfa = state of DFA
def state5(c):
    if (c == 'b'):
        dfa = 6
    else:
        dfa = -1
    return dfa
 
 
# This function is for the sixth
# dfa = state of DFA
def state6(c):
    if (c == 'b'):
        dfa = 5
    else:
        dfa = -1
    return dfa
 
def isAccepted(String):
     
    # store length of String
    l = len(String)
     
    # dfa tells the number associated
    # with the present dfa = state
    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])
  
        elif (dfa == 5) :
            dfa = state5(String[i])
 
        elif (dfa == 6):
            dfa = state6(String[i])
 
        else:
            return 0
    if(dfa == 3 or dfa == 5) :
        return 1
    else:
        return 0
 
# Driver code
if __name__ == "__main__" :
    String = "aaabbb"
    if (isAccepted(String)) :
        print("ACCEPTED")
    else:
        print("NOT ACCEPTED")
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

                    

C#

// C# program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
using System;
     
class GFG
{
 
    // dfa tells the number associated
    // with the present state.
    static int dfa = 0;
 
    // This function is for
    // the starting state (Q0)of DFA
    static void start(char c)
    {
        if (c == 'a')
        {
            dfa = 1;
        }
        else if (c == 'b')
        {
            dfa = 2;
        }
 
        // -1 is used to check for any invalid symbol
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for
    // the first state (Q1) of DFA
    static void state1(char c)
    {
        if (c == 'a')
        {
            dfa = 0;
        }
        else if (c == 'b')
        {
            dfa = 5;
        }
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for the
    // second state (Q2) of DFA
    static void state2(char c)
    {
        if (c == 'b')
        {
            dfa = 3;
        }
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for the
    // third state (Q3)of DFA
    static void state3(char c)
    {
        if (c == 'b')
        {
            dfa = 4;
        }
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for the
    // fourth state (Q4) of DFA
    static void state4(char c)
    {
        if (c == 'b')
        {
            dfa = 3;
        }
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for the
    // fifth state (Q5) of DFA
    static void state5(char c)
    {
        if (c == 'b')
        {
            dfa = 6;
        }
        else
        {
            dfa = -1;
        }
    }
 
    // This function is for the
    // sixth state (Q6) of DFA
    static void state6(char c)
    {
        if (c == 'b')
        {
            dfa = 5;
        }
        else
        {
            dfa = -1;
        }
    }
 
    static int isAccepted(char []str)
    {
        // store length of string
        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 if (dfa == 5)
                state5(str[i]);
 
            else if (dfa == 6)
                state6(str[i]);
 
            else
                return 0;
        }
        if (dfa == 3 || dfa == 5)
            return 1;
        else
            return 0;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        char []str = "aaabbb".ToCharArray();
        if (isAccepted(str) == 1)
            Console.WriteLine("ACCEPTED");
        else
            Console.WriteLine("NOT ACCEPTED");
    }
}
 
/* This code contributed by PrinciRaj1992 */

                    

PHP

<?php
// PHP program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
 
// This function is for the
// starting state (zeroth) of DFA
function start($c, &$dfa)
{
    if ($c == 'a') $dfa = 1;
    elseif ($c == 'b') $dfa = 2;
     
    // -1 is used to check for any
    // invalid symbol
    else $dfa = -1;
}
 
// This function is for the first
// state of DFA
function state1($c, &$dfa)
{
    if ($c == 'a') $dfa = 0;
    elseif ($c == 'b') $dfa = 5;
    else $dfa = -1;
}
 
// This function is for the second
// state of DFA
function state2($c, &$dfa)
{
    if ($c == 'b') $dfa = 3;
    else $dfa = -1;
}
 
// This function is for the third
// state of DFA
function state3($c, &$dfa)
{
    if ($c == 'b') $dfa = 4;
    else $dfa = -1;
}
 
// This function is for the fourth
// state of DFA
function state4($c, &$dfa)
{
    if ($c == 'b') $dfa = 3;
    else $dfa = -1;
}
 
// This function is for the fifth
// state of DFA
function state5($c, &$dfa)
{
    if ($c == 'b') $dfa = 6;
    else $dfa = -1;
}
 
// This function is for the sixth
// state of DFA
function state6($c, &$dfa)
{
    if ($c == 'b') $dfa = 5;
    else $dfa = -1;
}
 
function isAccepted($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);
 
        elseif ($dfa == 5)
            state5($str[$i], $dfa);
 
        elseif ($dfa == 6)
            state6($str[$i], $dfa);
 
        else
            return 0;
    }
     
    if($dfa == 3 || $dfa == 5)
        return 1;
    else
        return 0;
}
 
// Driver code
 
// dfa tells the number associated
// with the present state
$dfa = 0;
$str = array("a", "a", "a", "b", "b", "b");
 
if (isAccepted($str, $dfa) != 0)
    echo "ACCEPTED";
else
    echo "NOT ACCEPTED";
 
// This code is contributed by
// Adesh kumar Singh(adeshsingh1)
?>

                    

Javascript

<script>
 
// Javascript program to implement DFA that accepts
// all strings which follow the language
// L = { a^n b^m ; n+m=even }
 
// dfa tells the number associated
// with the present state
var dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
function start(c)
{
    if (c == 'a')
        dfa = 1;
    else if (c == 'b')
        dfa = 2;
 
    // -1 is used to check for any invalid symbol
    else
        dfa = -1;
}
 
// This function is for the first state of DFA
function state1(c)
{
    if (c == 'a')
        dfa = 0;
    else if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
// This function is for the second state of DFA
function state2(c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the third state of DFA
function state3(c)
{
    if (c == 'b')
        dfa = 4;
    else
        dfa = -1;
}
 
// This function is for the fourth state of DFA
function state4(c)
{
    if (c == 'b')
        dfa = 3;
    else
        dfa = -1;
}
 
// This function is for the fifth state of DFA
function state5(c)
{
    if (c == 'b')
        dfa = 6;
    else
        dfa = -1;
}
 
// This function is for the sixth state of DFA
function state6(c)
{
    if (c == 'b')
        dfa = 5;
    else
        dfa = -1;
}
 
function isAccepted(str)
{
    // Store length of string
    var 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 if (dfa == 5)
            state5(str[i]);
 
        else if (dfa == 6)
            state6(str[i]);
 
        else
            return 0;
    }
    if (dfa == 3 || dfa == 5)
        return 1;
    else
        return 0;
}
 
// Driver code
var str = "aaabbb";
if (isAccepted(str))
    document.write( "ACCEPTED");
else
    document.write( "NOT ACCEPTED");
 
 
</script>

                    

Output: 

ACCEPTED

Time Complexity: O(n) where n is the length of the given string array

Auxiliary Space: O(1)


 There is a minimal DFA for the same problem. 



Previous Article
Next Article

Similar Reads

DFA for accepting the language L = {an bm | n+m=odd}
Design a deterministic finite automata (DFA) for accepting the language [Tex]L = \{a^nb^m | \text{m+n=odd}\} [/Tex]For creating DFA for language L = {an bm | n+m=odd} use elementary mathematics which says, odd + even = odd, and even+odd = odd Examples: Input: a a b b b Output: ACCEPTED // n = 2, m = 3, 2 + 3 = 5 (odd) Input: a a a b b b Output: NOT
14 min read
DFA machines accepting odd number of 0’s or/and even number of 1’s
Prerequisite - Designing finite automata Problem - Construct a DFA machine over input alphabet [Tex]\sum_[/Tex]= {0, 1}, that accepts: Odd number of 0’s or even number of 1’s Odd number of 0’s and even number of 1’s Either odd number of 0’s or even number of 1’s but not the both together Solution - Let first design two separate machines for the two
3 min read
Program to construct DFA accepting odd number of 0s and odd number of 1s
Given a binary string S, the task is to write a program for DFA Machine that accepts a string with odd numbers of 0s and 1s. Examples: Input: S = "010011"Output: AcceptedExplanation:The given string S contains odd number of zeros and ones. Input: S = "00000"Output: Not AcceptedExplanation:The given string S doesn't contains odd number of zeros and
7 min read
DFA accepting all strings over w ∈(a,b)* which contains “aba” as a substring
Given a binary string S, the task is to write a program for DFA Machine that accepts a set of all strings over w ∈ (a, b)* which contains “aba” as a substring.Examples : Input-1 : ababa Output : Accepted Explanation : "ababa" consists "aba" Input-2 : abbbb Output : Not accepted Explanation : "abbbb" does not consist "aba" Approach : Below is the de
8 min read
DFA in LEX code which accepts even number of zeros and even number of ones
Lex is a computer program that generates lexical analyzers, which is commonly used with the YACC parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975, is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard. Lex reads an input stream
6 min read
NPDA for accepting the language L = {an bm cn | m,n&gt;=1}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem - – Design a non deterministic PDA for accepting the language L = {[Tex]a^n [/Tex][Tex]b^m [/Tex][Tex]c^n [/Tex]| m, n&gt;=1}, i.e., L = { abc, abbc, abbbc, aabbcc, aaabccc, aaaabbbcccc, ...... } In each of the string, the number of a's is equal to number of c's.
2 min read
NPDA for accepting the language L = {a<sup>2m</sup>b<sup>3m</sup> | m &ge; 1}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem – Design a non deterministic PDA for accepting the language L = {a2mb3m | m &ge; 1}, i.e., L = {aabbb, aaaabbbbbb, aaaaaabbbbbbbbb, aaaaaaaabbbbbbbbbbbb, ......} In each of the string, for every 2 'a's there is 3 'b'. Explanation – Here, we need to maintain the or
2 min read
NPDA for accepting the language L = {ambnc(m+n) | m,n ≥ 1}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem - Design a non deterministic PDA for accepting the language L = {[Tex]a^m b^n c^{(m+n)} [/Tex]| m,n ≥ 1}, i.e., L = {abcc, aabccc, abbbcccc, aaabbccccc, ......} In each of the string, the total sum of the number of 'a’ and 'b' is equal to the number of c’s. And al
2 min read
NPDA for accepting the language L = {a<sup>n</sup> b<sup>n</sup> c<sup>m</sup> | m,n&gt;=1}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem – Design a non deterministic PDA for accepting the language L = {[Tex]a^n[/Tex] [Tex]b^n[/Tex] [Tex]c^m[/Tex] | m, n&gt;=1}, i.e., L = { abc, abcc, abccc, aabbc, aaabbbcc, aaaabbbbccccc, ...... } In each of the string, the number of a's is equal to number of b's.
2 min read
NPDA for accepting the language L = {an bn | n&gt;=1}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem - Design a non deterministic PDA for accepting the language L = {[Tex]a^n [/Tex][Tex]b^n [/Tex]| n&gt;=1}, i.e., L = {ab, aabb, aaabbb, aaaabbbb, ......} In each of the string, the number of a's are followed by equal number of b's. Explanation - Here, we need to m
2 min read