VOOZH about

URL: https://www.geeksforgeeks.org/theory-of-computation/designing-deterministic-finite-automata-set-2/

⇱ Program for Deterministic Finite Automata - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for Deterministic Finite Automata

Last Updated : 26 Aug, 2025

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 desired language will be like:

L = {, aa, ab, ba, bb, aaaa, bbbb, ............}

The state transition diagram of the language will be like:

👁 Image

Here, state A represent set of all string of length even (0, 2, 4, ...), and state B represent set of all string of length odd (1, 3, 5, ...).

Number of states: n
If |W| mod n = 0

Output:

Accepted

The above automata will accept all the strings having the length of the string divisible by 2. When the length of the string is 1, then it will go from state A to B. When the length of the string is 2, then it will go from state B to A and so on. State A is the final state i.e, it accept all the string having length divisible by 2.

Problem-2:

Construction of a DFA for the set of string over {a, b} such that length of the string |w| is not divisible by 2 i.e, |w| mod 2 = 1.

Explanation -

The desired language will be like:

L = {a, b, aaa, aab, aba, abb, aaaaa, bbbb, .......} 

The state transition diagram of the language will be like:

👁 Image

Here, state A represent set of all string of length even (0, 2, 4, ...), and state B represent set of all string of length odd (1, 3, 5, ...).

The above automata will accept all the strings having the length of the string not divisible by 2. When the length of the string is 1, then it will go from state A to B. When the length of the string is 2, then it will go from state B to A and so on. State B is the final state i.e, it accept all the string having length not divisible by 2.

Problem-3:

Construction of a DFA for the set of string over {a, b} such that length of the string |w| is divisible by 3 i.e, |w| mod 3 = 0.

Explanation -

The desired language will be like:

L = {?, aaa, aab, aba, abb, aaaaaa, bbbbbb, .......} 

The state transition diagram of the language will be like:

👁 Image

Here, state A represents set for which string's length divided by 3 then remainder is zero (0), state B represents set for which string's length divided by 3 then the remainder is one (1), and state C represents set for which string's length divided by 3 then the remainder is two (2).

Number of states: n
If |W| mod n = 0

The above automata will accept all the strings having the length of the string divisible by 3. When the length of the string is 1, then it will go from state A to B. When the length of the string is 2, then it will go from state B to C and When the length of the string is 3, then it will go from state C to A (final state). State A is the final state i.e, it accepts all the string having the length divisible by 3.

Problem-4:

Construction of a DFA for the set of string over {a, b} such that length of the string |w| is not divisible by 3 i.e, |w| mod 3 = 1.

Explanation -

The desired language will be like:

L = {a, b, aa, ab, ba, bb, aaaa, bbbb, ........} 

The state transition diagram of the language will be like:

👁 Image

Here, state A represents set for which string's length divided by 3 then remainder is zero (0), state B represents set for which string's length divided by 3 then the remainder is one (1), and state C represents set for which string's length divided by 3 then the remainder is two (2).

The above automata will accept all the strings having the length of the string not divisible by 3. When the length of the string is 1, then it will go from state A to B. When the length of the string is 2, then it will go from state B to C and When the length of the string is 3, then it will go from state C to A. State B and C are the final state i.e, it accepts all the string having the length not divisible by 3.

Comment
Article Tags:

Explore