Open In App

Designing Deterministic Finite Automata (Set 7)

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite:

Designing finite automata

In this article, we will see some designing of Deterministic Finite Automata (DFA).

Problem-1:

Construction of a minimal DFA accepting set of strings over {a, b} in which a

n

b

m

c

l

, where n, m and l is greater than or equal to 0.

Explanation:

The desired language will be like:

L1 = {ε, a, aa, aaa, b, bb, bbb, c, cc, ccc, abc, ab, ac, ...........}

Note:

In the above string there should must be order like abc i.e, there should not be any ‘a’ after ‘b’ or any ‘a’ after ‘c’ etc. Here as we can see that each string of the language containing a, b and c whose power is greater or equal to 0 but the below language is not accepted by this DFA because some of the string of the below language does not contain a, b and c whose power is greater or equal to 0 or they might not follow the format of a, b and c i.e, there should not be any ‘a’ after ‘b’ or any ‘a’ after ‘c’ etc.

L2 = {ba, bac, bbacaa..............}

This language L2 is not accepted by this required DFA because it’s string contain ‘a’ after ‘b’ and ‘a’ after ‘c’ etc. The state transition diagram of the desired language will be like below:

Code implementation:

C++




#include <iostream>
#include <string>
 
void stateA(const std::string& n);
void stateB(const std::string& n);
void stateC(const std::string& n);
 
void stateA(const std::string& n) {
    if (n.empty()) {
        std::cout << "Accepted" << std::endl;
    } else {
        // on input 'a', call function stateA
        if (n[0] == 'a') {
            stateA(n.substr(1));
        }
        // on input 'b', call function stateB
        else if (n[0] == 'b') {
            stateB(n.substr(1));
        }
        // on input 'c', call function stateC
        else if (n[0] == 'c') {
            stateC(n.substr(1));
        } else {
            // reject for any other character
            std::cout << "Not Accepted" << std::endl;
        }
    }
}
 
void stateB(const std::string& n) {
    if (n.empty()) {
        std::cout << "Accepted" << std::endl;
    } else {
        // on input 'b', call function stateB
        if (n[0] == 'b') {
            stateB(n.substr(1));
        }
        // on input 'c', call function stateC
        else if (n[0] == 'c') {
            stateC(n.substr(1));
        } else {
            // reject for any other character
            std::cout << "Not Accepted" << std::endl;
        }
    }
}
 
void stateC(const std::string& n) {
    if (n.empty()) {
        std::cout << "Accepted" << std::endl;
    } else {
        // on input 'c', call function stateC
        if (n[0] == 'c') {
            stateC(n.substr(1));
        } else {
            // reject for any other character
            std::cout << "Not Accepted" << std::endl;
        }
    }
}
 
int main() {
    // take input
    std::string inputString = "abc";
 
    // call stateA to check the input string
    stateA(inputString);
 
    return 0;
}
// This code is contributed by utkarsh


Python3




def stateA(n):
    if not n:
        print("Accepted")
    else:
        if n[0] == 'a':
            stateA(n[1:])
        elif n[0] == 'b':
            stateB(n[1:])
        elif n[0] == 'c':
            stateC(n[1:])
        else:
            print("Not Accepted")
 
def stateB(n):
    if not n:
        print("Accepted")
    else:
        if n[0] == 'b':
            stateB(n[1:])
        elif n[0] == 'c':
            stateC(n[1:])
        else:
            print("Not Accepted")
 
def stateC(n):
    if not n:
        print("Accepted")
    else:
        if n[0] == 'c':
            stateC(n[1:])
        else:
            print("Not Accepted")
 
if __name__ == "__main__":
    # take input
    input_string = "abc"
 
    # call stateA to check the input string
    stateA(input_string)
    # This code is contributed by Aman


In the above DFA, the state ‘W’ is the initial and final state which on getting ‘a’ as the input it remains in the state of itself, on getting ‘b’ as the input it transit to the final state ‘X’ and on getting ‘c’ as the input it transits to another final state ‘Y’. The state ‘X’ is the final state which on getting ‘b’ as the input it remains in the state of itself, on getting ‘c’ as the input it transits to another final state ‘Y’ and on getting ‘a’ as the input it transits to a dead state ‘Z’. Another final state ‘Y’ on getting ‘c’ as the input it remains in the state of itself and on getting input either as ‘a’ or ‘b’ it transits to the same dead state ‘Z’. The state ‘Z’ is called a dead state because it can not go to any of the final states on getting any of the input alphabets.

Problem-2:

Construction of a minimal DFA accepting set of strings over {a, b} in which a

n

b

m

c

l

, where n, m and l is greater than or equal to 1.

Explanation:

The desired language will be like:

L1 = {abc, aabc, aabbc, aabbcc, abbc, ...........}

Note:

In the above string there should must be order like abc i.e, there should not be any ‘a’ after ‘b’ or any ‘c’ after ‘a’ etc. Here as we can see that each string of the language containing a, b and c whose power is greater or equal to 1 but the below language is not accepted by this DFA because some of the string of the below language does not contain a, b and c whose power is greater or equal to 1 or they might not follow the format of a, b and c i.e, there should not be any ‘a’ after ‘b’ or any ‘c’ after ‘a’ etc.

L2 = {ba, bac, bbacaa..............}

This language L2 is not accepted by this required DFA because it’s string contain ‘a’ after ‘b’ and ‘c’ after ‘a’ etc. The state transition diagram of the desired language will be like below:

In the above DFA, the initial state ‘V’ on getting ‘a’ as the input it transit to a state ‘W’ and on getting ‘b’ or ‘c’ as input it transit to a dead state ‘Z’. The state ‘W’ on getting ‘b’ as the input it either remains in the state itself or transit to a state ‘X’ and on getting ‘a’ or ‘c’ it transit to the same dead state ‘Z’. The state ‘X’ on getting ‘b’ as the input it remains in the state of itself and on getting ‘c’ as the input it transits to the final state ‘Y’ and on getting ‘a’ as the input it transit to the same dead state ‘Z’. The final state ‘Y’ on getting ‘c’ as the input it remains in the state of itself and on getting ‘a’ or ‘b’ as the input it transits to the same dead state ‘Z’. The state ‘Z’ is called a dead state this is because it can not go to final state ever on getting any of the input alphabets.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads