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



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 which 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
  3. Any other combination result is the rejection of the input string.

Description:
Given DFA has 2 parts. First part consisting of states 0, 1, 5 and 6 which is for both n and m being odd. Second part consist of states 2, 3 and 4 is for both n and m is even.



DFA State Transition Diagram:

Let’s see C code for the demonstration:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to implement DFS that accepts
// all string 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;
}

chevron_right


Output:

ACCEPTED


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.