The Wayback Machine - https://web.archive.org/web/20241117195310/https://www.geeksforgeeks.org/designing-deterministic-finite-automata-set-7/
Open In App

Designing Deterministic Finite Automata (Set 7)

Last Updated : 23 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

Designing Deterministic Finite Automata (Set 5)
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 every 'a' is followed by a 'bb'. Explanation: The desired language will be like: L1 = {ε, abb, abbabb, bbbbabb, abbabbabbabbabbbb,
3 min read
Designing Deterministic Finite Automata (Set 9)
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 a set of strings over {a, b} in which {abwa / wε{a, b}*}. Here 'w' is the substring containing alphabets over any number of 'a' and 'b' ranging from zero to infinity.
4 min read
Designing Deterministic Finite Automata (Set 6)
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 anbm, where n and m is greater than or equal to 1. Explanation: The desired language will be like: L1 = {ab, aab, abb, aabb, aaabbb, aaabbb
3 min read
Designing Deterministic Finite Automata (Set 2)
Prerequisite - Designing finite automata, previous article: Designing Deterministic Finite Automata (Set 1) In this article, we will see some designing of Deterministic Finite Automata (DFA). Problem-1: Construction of a DFA for the set of string over {a, b} such that length of the string |w| is divisible by 2 i.e, |w| mod 2 = 0. Explanation - The
7 min read
Designing Non-Deterministic Finite Automata (Set 4)
Prerequisite: Finite Automata Introduction In this article, we will see some designing of Non-Deterministic Finite Automata (NFA). Problem-1: Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language contain 'a' as the substring. Explanation: The desired language will be like: L1 = {ab, abba, abaa, ..
4 min read
Designing Deterministic Finite Automata (Set 4)
Prerequisite: Designing finite automata , Designing Deterministic Finite Automata (Set 3) 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 every 'a' is followed by a 'b'. Explanation: The desired language will be like: L1 = {ε,
6 min read
Designing Non-Deterministic Finite Automata (Set 1)
Prerequisite: Finite Automata Introduction In this article, we will see some designing of Non-Deterministic Finite Automata (NFA). Problem-1: Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language starts with 'a'. Explanation: The desired language will be like: L1 = {ab, abba, abaa, ...........} He
6 min read
Designing Deterministic Finite Automata (Set 3)
Prerequisite: Designing finite automata, Designing Deterministic Finite Automata (Set 2) In this article, we will see some designing of Deterministic Finite Automata (DFA). Problem-1: Construction of a minimal DFA accepting set of string over {a, b} where each string containing 'a' as the substring. Explanation: The desired language will be like:
4 min read
Designing Deterministic Finite Automata (Set 11)
Prerequisite: Designing finite automata In this article, we will see a designing of Deterministic Finite Automata (DFA). Problem: Construction of a minimal DFA accepting set of strings over {a, b} in which Number of a(w) mod 2 = 0 or Number of b(w) mod 2 = 0 i.e, number of 'a' should be divisible by 2 or number of 'b' should be divisible by 2 or bo
3 min read
Designing Deterministic Finite Automata (Set 10)
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} in which {an | n≥0, n≠2 i.e, 'n' should be greater than 0 and not equal to 2}. Explanation: The desired language will be like:   L1 = {ε, a, aaa, aaaa,
6 min read
Designing Non-Deterministic Finite Automata (Set 5)
Prerequisite: Finite Automata Introduction In this article, we will see some designing of Non-Deterministic Finite Automata (NFA). Problem-1: Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language contain 'ab' as the substring. Explanation: The desired language will be like: L1 = {ab, abba, abaa, .
3 min read
Designing Deterministic Finite Automata (Set 8)
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 a set of strings over {a, b} in which the second symbol from left-hand side is always 'b'. Explanation: The desired language will be like: L1 = {ab, aba, abaa, bb, bb, bbbb, .
5 min read
Designing Deterministic Finite Automata (Set 1)
C/C++ Code // dfa that accepts atleast 2 strings or more // alphabet = {a,b} #include <iostream> #include <string> using namespace std; // initialize dfa variable // A=0,B=1,C=2 int dfa = 0; // function for state A void stateA(char n) { /*here if char is a or b there is a transition from stateA to StateB*/ if ((n == 'a') || (n == 'A') |
12 min read
Designing Non-Deterministic Finite Automata (Set 3)
Prerequisite: Finite Automata Introduction In this article, we will see some designing of Non-Deterministic Finite Automata (NFA).Problem-1: Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language starts with 'ab'. Explanation: The desired language will be like:   L1 = {ab, abba, abaa, ...........}H
4 min read
Reversing Deterministic Finite Automata
Prerequisite – Designing finite automata Reversal: We define the reversed language [Tex]L^R \text{ of } L [/Tex] to be the language [Tex]L^R = \{ w^R \mid w \in L \} [/Tex], where [Tex]w^R := a_n a_{n-1} \dots a_1 a_0 \text{ for } w = a_0 a_1 \dots a_{n-1} a_n [/Tex] Steps to Reversal:  Draw the states as it is.Add a new single accepting stateMake
4 min read
Designing Finite Automata from Regular Expression (Set 6)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 5) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = a(a+b)* The language of the given RE is, {aaa, aba, baa, bba} Strings
3 min read
Designing Finite Automata from Regular Expression (Set 2)
Prerequisite: Finite automata, Regular expressions, grammar and language. Designing finite automata from Regular expression In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Φ (Phi). The language of the given RE is L1 = {} i.e, empty string. Its finite automata will be
3 min read
Designing Finite Automata from Regular Expression (Set 3)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 2) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression. Regular Expression 1: 'ab*' ('a' followed by any number of 'b'). The language of the given RE is, L1 = {a, ab, abb
3 min read
Designing Finite Automata from Regular Expression (Set 4)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 3) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = (a+b)(a+b) The language of the given RE is, {aa, ab, ba, bb} Length o
3 min read
Designing Finite Automata from Regular Expression (Set 5)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 4) In the below article, we shall see some Designing of Non-deterministic Finite Automata form the given Regular Expression- As NFA can be changed to corresponding DFA. Regular Expression 1: Regular language, L1 = ((a+b)(
3 min read
Designing Finite Automata from Regular Expression (Set 7)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 6) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = b*aa(a+b)*+b*ab*aa(a+b*) The language of the given RE is- {aaa, baa,
3 min read
Designing Finite Automata from Regular Expression (Set 8)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 7) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = {an | n≥ 1} The language of the given RE is- {a, aa, aaa, ........
2 min read
Designing Finite Automata from Regular Expression (Set 1)
In this article, we will see some popular regular expressions and how we can convert them to finite automata. Even number of a's : The regular expression for even number of a's is (b|ab*ab*)*. We can construct a finite automata as shown in Figure 1. The above automata will accept all strings which have even number of a’s. For zero a’s, it will be i
3 min read
Difference Between Pushdown Automata and Finite Automata
Two important computational models in automata theory that are used to identify various language kinds are Pushdown Automata (PDA) and Finite Automata (FA). Both automata process strings as input and switch between states according to pre-established rules. Still, their memory capacities account for the majority of the variance. Pushdown Automata h
5 min read
Designing finite automata for various operations like 1's complement, 2's complement
Transducers in Finite Automata(FA) means FA with Output.There are two types of Machines for FA with Output. 1. Mealy Machine : It is FA in which output is associated with each transition. (that means output depends on state and input).2. Moore Machine : It is FA in which output is associated with each state. (that means output depends on state only
4 min read
Practice problems on finite automata | Set 2
Que-1: Draw a deterministic and non-deterministic finite automate which either starts with 01 or end with 01 of a string containing 0, 1 in it, e.g., 01010100 but not 000111010. Explanation - Draw a DFA and NFA of same language whose strings only reach to the final state containing either 01 at start or at the end. If anything else is come then com
3 min read
Finite Automata with Output (Set 6)
Prerequisite: Mealy and Moore Machines, Difference between Mealy machine and Moore machine In this article, we will see some designing of Finite Automata with Output, i.e., Moore and Mealy machines. Problem: Construction of the machines that take set of all string over {0, 1} as input and produce 'A' as output if the input contains '101' as the sub
2 min read
Finite Automata with Output (Set 5)
Prerequisite: Mealy and Moore Machines, Difference between Mealy machine and Moore machine In this article, we will see some designing of Finite Automata with Output i.e, Moore and Mealy machines. Problem: Construction of the machines that take set of all string over {0, 1} as input and produce 'A' as output if the input contains '1' as the substri
2 min read
Finite Automata with Output (Set 3)
Prerequisite: Mealy and Moore Machines, Difference between Mealy machine and Moore machine In this article, we will see some designing of Finite Automata with Output i.e, Moore and Mealy machines. Problem: Construction of the machines that take set of all string over {a, b} as input and count number of substring 'a'. That is here we have, Ε
2 min read
Finite Automata with Output (Set 11)
Prerequisite: Mealy and Moore Machines, Difference between Mealy machine and Moore machine In this article, we will see some designing of Finite Automata with Output i.e, Moore and Mealy machines. Problem: Construction of the machines that take the binary number {0, 1} as input and produce 1's complement of that number as output. Here the assumptio
4 min read
three90RightbarBannerImg