0% found this document useful (0 votes)
473 views4 pages

To Convert NFA To DFA: Date: 04/02/2022

The document describes an algorithm to convert a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). The algorithm starts with an initial state q0. It finds the possible states reached from each current state through each input symbol using the NFA transition function. Newly reached states are added to the set of DFA states. The final states of the DFA are those containing any of the NFA final states. The algorithm is implemented in a Python program to convert a given NFA to a DFA.

Uploaded by

Tongpa Morrow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
473 views4 pages

To Convert NFA To DFA: Date: 04/02/2022

The document describes an algorithm to convert a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). The algorithm starts with an initial state q0. It finds the possible states reached from each current state through each input symbol using the NFA transition function. Newly reached states are added to the set of DFA states. The final states of the DFA are those containing any of the NFA final states. The algorithm is implemented in a Python program to convert a given NFA to a DFA.

Uploaded by

Tongpa Morrow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Shashwat Rai RA1911003010435

Exp No. 03 To convert NFA to DFA


Date: 04/02/2022

Aim: To convert the given NFA into DFA.

Algorithm:
1. Step 1: Initially Q’ = ɸ.
2. Add q0 to Q’.
3. For each state in Q’, find the possible set of states for each input symbol
using the transition function of NFA. If this set of states is not in Q’, add it
to Q’.
4. Final state of DFA will be all states with contain F (final states of NFA)

PROGRAM:

Language Used: Python

import pandas as pd
nfa = {}
n = int(input("No. of states : "))
#Enter total no. of transitions/paths eg: a,b so input 2 for
a,b,c t = int(input("No. of transitions : "))
for i in range(n):
state = input("state name : ")
nfa[state] = {}
for j in range(t):
path = input("path : ")
print("Enter end state from state {} traveling through path
{} : ".format(state,path))
reaching_state = [x for x in input().split()]
nfa[state][path] = reaching_state
print("\nNFA :- \n")

print(nfa) #Printing NFA


print("\nPrinting NFA table :- ")
nfa_table = pd.DataFrame(nfa)
print(nfa_table.transpose())
print("Enter final state of NFA : ")
nfa_final_state = [x for x in input().split()] # Enter final
state/states of NFA

new_states_list = []

dfa = {} keys_list = list(list(nfa.keys())[0])

further path_list = list(nfa[keys_list[0]].keys())

dfa[keys_list[0]] = {} #creating a nested dictionary in


dfa for y in range(t):
var = "".join(nfa[keys_list[0]][path_list[y]]) #creating a
single string from all the elements of the list which is a new
state dfa[keys_list[0]][path_list[y]] = var #assigning the state
in DFA table
if var not in keys_list: #if the state is newly created
new_states_list.append(var) #then append it to the
new_states_list
keys_list.append(var) #as well as to the keys_list which
contains all the states

# Computing the other rows of DFA transition table


while len(new_states_list) != 0: #condition is true only if
the new_states_list is not empty
dfa[new_states_list[0]] = {} #taking the first element of
the new_states_list and examining it
for _ in range(len(new_states_list[0])):
for i in range(len(path_list)):
temp = [] #creating a temporary list
for j in range(len(new_states_list[0])):
temp += nfa[new_states_list[0][j]][path_list[i]]
#taking the union of the states
s = ""
s = s.join(temp)
if s not in keys_list:
new_states_list.append(s)
keys_list.append(s)
dfa[new_states_list[0]][path_list[i]] = s
new_states_list.remove(new_states_list[0])
print("\nDFA :- \n")
print(dfa) #Printing the DFA created
print("\nPrinting DFA table :- ")
dfa_table = pd.DataFrame(dfa)
print(dfa_table.transpose())
dfa_states_list = list(dfa.keys())
dfa_final_states = []

for x in dfa_states_list:
for i in x:
if i in nfa_final_state:
dfa_final_states.append(x)
break

print("\nFinal states of the DFA are : ",dfa_final_states)

Output:
Result:
Program to convert RE to NFA was written and executed successfully using the
given example.

You might also like