Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1} - GeeksforGeeks

11 Jul, 2019
Problem: Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1}.
Explanation:For constructing the DFA, the following things to be remember:
 which means any no of elements, and
 =  which means any no of elements greater than 1.
Examples:
Input: a a b b b           
Output: NOT ACCEPTED
// n = 2 (>=1), m=3 ((3) mod3 != 1)

Input: a a a b        
Output: ACCEPTED
 // n = 3 (>=1), m = 1 ((1) mod 3= 1)

Input: b b b b        
Output: NOT ACCEPTED
// n = 0(must be >=1), m = 4 ((4) mod 3 = 1) 
Approaches:It’s construction should contain the following steps:
Step-1: Construct FA for  means having any number of a’s greater than one.
Step-2: Construct FA for  means having exactly
Step-3: Construct FA for  means having b’s equal to multiple of 3 .
Step-4: Concatenate the three FA and make single DFA. Always maintain the order of a, b and c.Given DFA has following states. State 2 leads to the acceptance of the string.Whereas states 0, 1, 3, 4 and 5 leads to the rejection of the string.DFA State Transition Diagram:Let’s see code for the demonstration:C/C++JavaPython 3C#C/C++// C program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}#include <stdio.h>#include <string.h>  // dfa tells the number associated// string end in which state.int dfa = 0;  // This function is for// the starting state (Q0)of DFAvoid start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }      // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}  // This function is for the first state (Q1) of DFAvoid state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}  // This function is for the second state (Q2) of DFAvoid state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the third state (Q3)of DFAvoid state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the forth state (Q4)of DFAvoid state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the fifth state (Q5) of DFAvoid state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}  // driver codeint main(){    char str[] = "aaabbbb";    if (isAccepted(str))        printf("ACCEPTED");    else        printf("NOT ACCEPTED");    return 0;}  // 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 >=1, (m)mod 3=1}import java.util.*;class GFG {   // dfa tells the number associated// string end in which state.static int dfa = 0;   // This function is for// the starting state (Q0)of DFAstatic void start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }       // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}   // This function is for the first state (Q1) of DFAstatic void state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}   // This function is for the second state (Q2) of DFAstatic void state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the third state (Q3)of DFAstatic void state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the forth state (Q4)of DFAstatic void state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the fifth state (Q5) of DFAstatic void state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}   // Driver codepublic static void main(String[] args) {   char str[] = "aaabbbb".toCharArray();    if (isAccepted(str)==1)        System.out.println("ACCEPTED");    else        System.out.println("NOT ACCEPTED");    }}  // This code is contributed by Rajput-JiPython 3# Python3 program to implement DFS that accepts   # all Stringing which follow the language   # L = {a ^ n b ^ m | n >= 1, (m)mod 3 = 1 }      # This function is for the dfa = starting  # dfa = state (zeroth) of DFA   def start(c):      if (c == 'a'):          dfa = 1    elif (c == 'b'):          dfa = 5              # -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 = 1    elif (c == 'b'):          dfa = 2    else:          dfa = -1    return dfa        # This function is for the second   # dfa = state of DFA   def state2(c):      if (c == 'b'):          dfa = 3    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa        # This function is for the third   # dfa = state of DFA   def state3(c):      if (c == 'b'):          dfa = 4    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa        # This function is for the fouth   # dfa = state of DFA   def state4(c):      if (c == 'b'):          dfa = 2    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa             # This function is for the fifth  # dfa = state of DFA   def state5(c):      dfa = -1    return dfa        def isAccepted(String):                # store length of Stringing       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])        else:              return 0    if(dfa == 2) :          return 1    else:          return 0      # Driver code   if __name__ == "__main__" :      String = "aaabbbb"    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 >=1, (m)mod 3=1}using System;public class GFG {    // dfa tells the number associated// string end in which state.static int dfa = 0;    // This function is for// the starting state (Q0)of DFAstatic void start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }        // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}    // This function is for the first state (Q1) of DFAstatic void state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}    // This function is for the second state (Q2) of DFAstatic void state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the third state (Q3)of DFAstatic void state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the forth state (Q4)of DFAstatic void state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the fifth state (Q5) of DFAstatic void state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}    // Driver codepublic static void Main(String[] args) {   char []str = "aaabbbb".ToCharArray();    if (isAccepted(str)==1)        Console.WriteLine("ACCEPTED");    else        Console.WriteLine("NOT ACCEPTED");    }}// This code has been contributed by 29AjayKumarOutput:ACCEPTEDMy Personal Notes
arrow_drop_upSave
Given DFA has following states. State 2 leads to the acceptance of the string.Whereas states 0, 1, 3, 4 and 5 leads to the rejection of the string.
DFA State Transition Diagram:
Let’s see code for the demonstration:
C/C++
Java
Python 3
C#
// C program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}#include <stdio.h>#include <string.h>  // dfa tells the number associated// string end in which state.int dfa = 0;  // This function is for// the starting state (Q0)of DFAvoid start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }      // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}  // This function is for the first state (Q1) of DFAvoid state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}  // This function is for the second state (Q2) of DFAvoid state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the third state (Q3)of DFAvoid state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the forth state (Q4)of DFAvoid state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}  // This function is for the fifth state (Q5) of DFAvoid state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}  // driver codeint main(){    char str[] = "aaabbbb";    if (isAccepted(str))        printf("ACCEPTED");    else        printf("NOT ACCEPTED");    return 0;}  // This code is contributed by SHUBHAMSINGH10.
// Java program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}import java.util.*;class GFG {   // dfa tells the number associated// string end in which state.static int dfa = 0;   // This function is for// the starting state (Q0)of DFAstatic void start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }       // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}   // This function is for the first state (Q1) of DFAstatic void state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}   // This function is for the second state (Q2) of DFAstatic void state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the third state (Q3)of DFAstatic void state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the forth state (Q4)of DFAstatic void state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}   // This function is for the fifth state (Q5) of DFAstatic void state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}   // Driver codepublic static void main(String[] args) {   char str[] = "aaabbbb".toCharArray();    if (isAccepted(str)==1)        System.out.println("ACCEPTED");    else        System.out.println("NOT ACCEPTED");    }}  // This code is contributed by Rajput-Ji
# Python3 program to implement DFS that accepts   # all Stringing which follow the language   # L = {a ^ n b ^ m | n >= 1, (m)mod 3 = 1 }      # This function is for the dfa = starting  # dfa = state (zeroth) of DFA   def start(c):      if (c == 'a'):          dfa = 1    elif (c == 'b'):          dfa = 5              # -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 = 1    elif (c == 'b'):          dfa = 2    else:          dfa = -1    return dfa        # This function is for the second   # dfa = state of DFA   def state2(c):      if (c == 'b'):          dfa = 3    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa        # This function is for the third   # dfa = state of DFA   def state3(c):      if (c == 'b'):          dfa = 4    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa        # This function is for the fouth   # dfa = state of DFA   def state4(c):      if (c == 'b'):          dfa = 2    elif (c == 'a'):          dfa = 5    else:          dfa = -1    return dfa             # This function is for the fifth  # dfa = state of DFA   def state5(c):      dfa = -1    return dfa        def isAccepted(String):                # store length of Stringing       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])        else:              return 0    if(dfa == 2) :          return 1    else:          return 0      # Driver code   if __name__ == "__main__" :      String = "aaabbbb"    if (isAccepted(String)) :          print("ACCEPTED")       else:          print("NOT ACCEPTED")     # This code is contributed by SHUBHAMSINGH10.
// C# program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}using System;public class GFG {    // dfa tells the number associated// string end in which state.static int dfa = 0;    // This function is for// the starting state (Q0)of DFAstatic void start(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 5;    }        // -1 is used to check for any invalid symbol    else {        dfa = -1;    }}    // This function is for the first state (Q1) of DFAstatic void state1(char c){    if (c == 'a') {        dfa = 1;    }    else if (c == 'b') {        dfa = 2;    }    else {        dfa = -1;    }}    // This function is for the second state (Q2) of DFAstatic void state2(char c){    if (c == 'b') {        dfa = 3;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the third state (Q3)of DFAstatic void state3(char c){    if (c == 'b') {        dfa = 4;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the forth state (Q4)of DFAstatic void state4(char c){    if (c == 'b') {        dfa = 2;    }    else if (c == 'a') {        dfa = 5;    }    else {        dfa = -1;    }}    // This function is for the fifth state (Q5) of DFAstatic void state5(char c){    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            return 0;    }    if (dfa == 2)        return 1;    else        return 0;}    // Driver codepublic static void Main(String[] args) {   char []str = "aaabbbb".ToCharArray();    if (isAccepted(str)==1)        Console.WriteLine("ACCEPTED");    else        Console.WriteLine("NOT ACCEPTED");    }}// This code has been contributed by 29AjayKumar
Output:
ACCEPTED
Rajput-Ji
29AjayKumar
GATE CS
Theory of Computation & Automata
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Differences between IPv4 and IPv6
Preemptive and Non-Preemptive Scheduling
Difference between Clustered and Non-clustered index
Phases of a Compiler
Introduction of Process Synchronization
Turing Machine in TOC
Introduction of Finite Automata
Difference between DFA and NFA
Introduction of Pushdown Automata
Construct Pushdown Automata for given languages