Designing Deterministic Finite Automata (Set 8)
Last Updated :
13 Feb, 2024
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, ...........}
Here as we can see that each string of the language containing ‘b’ as the second symbol from left-hand side but the below language is not accepted by this DFA because some of the string of the below language does not contain ‘b’ as the second symbol from the left-hand side.
L2 = {ba, ba, babaaa..............}
This language L2 is not accepted by this required DFA because it does not contain ‘b’ as the second symbol from the left-hand side. The state transition diagram of the desired language will be like below:
In the above DFA, The state ‘W’ is the initial state which on getting either ‘a’ or ‘b’ as the input it transit to a state ‘X’. The state ‘X’ on getting ‘b’ as the input it transits to the final state ‘Y’ and on getting ‘a’ as the input it transit to a dead state ‘Z’. The final state ‘Y’ on getting either ‘a’ or ‘b’ as the input it remains in the state of itself. The dead state ‘Z’ is called dead because it can not go to the final state on getting any of the input alphabets.
Note:
The number of states in the above DFA is (n+2), where ‘n’ is the number from the left-hand side of the string used in the above language.
Problem-2:
Construction of a minimal DFA accepting a set of strings over {a, b} in which the third symbol from left-hand side is always ‘b’.
Explanation:
The desired language will be like:
L1 = {aab, baba, aabaa, bbb, abb, bbbb, ...........}
Here as we can see that each string of the language containing ‘b’ as the third symbol from left-hand side but the below language is not accepted by this DFA because some of the string of the below language does not contain ‘b’ as the third symbol from the left-hand side.
L2 = {baa, aba, baabaaa..............}
This language L2 is not accepted by this required DFA because it does not contain ‘b’ as the third symbol from the left-hand side. The state transition diagram of the desired language will be like below:
In the above DFA, The state ‘V’ is the initial state which on getting either ‘a’ or ‘b’ as the input it transits to the state ‘W’. The state ‘W’ is a state which on getting either ‘a’ or ‘b’ as the input it transits to a state ‘X’. The state ‘X’ on getting ‘b’ as the input it transits to the final state ‘Y’ and on getting ‘a’ as the input it transit to a dead state ‘Z’. The final state ‘Y’ on getting either ‘a’ or ‘b’ as the input it remains in the state of itself. The dead state ‘Z’ is called dead because it can not go to the final state on getting any of the input alphabets.
Code Solution:
This Python code defines a minimal DFA with three states (q0, q1, and q2). The accepting state is q2, and the transition function is defined according to the condition that the third symbol from the left must be ‘b’. The process_string method checks if a given string is accepted by the DFA. The example then tests the DFA with a list of strings and prints whether each string is accepted or not.
Python3
class DFA:
def __init__( self ):
self .states = { 'q0' , 'q1' , 'q2' }
self .alphabet = { 'a' , 'b' }
self .current_state = 'q0'
self .accepting_state = 'q2'
def transition( self , state, symbol):
if state = = 'q0' :
if symbol = = 'a' :
return 'q1'
elif symbol = = 'b' :
return 'q0'
elif state = = 'q1' :
if symbol = = 'a' :
return 'q2'
elif symbol = = 'b' :
return 'q1'
elif state = = 'q2' :
return 'q2'
return 'error'
def process_string( self , input_string):
for symbol in input_string:
if symbol not in self .alphabet:
return False
self .current_state = self .transition( self .current_state, symbol)
return self .current_state = = self .accepting_state
def main():
dfa = DFA()
test_strings = [ "abab" , "bbba" , "aab" , "baaab" ]
for test_string in test_strings:
result = dfa.process_string(test_string)
print (f "String: {test_string} - Accepted: {'Yes' if result else 'No'}" )
if __name__ = = "__main__" :
main()
|
OUTPUT:
String: abab – Accepted: Yes
String: bbba – Accepted: Yes
String: aab – Accepted: Yes
String: baaab – Accepted: Yes
Note:
The number of states in the above DFA is (n+2), where ‘n’ is the number from the left-hand side of the string used in the above language.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...