Write a python script to convert a Nondeterministic finite automaton to a deterministic finite automaton.
This function generates the powerset of a given_set (of type list) of size no_of_elements(of type integer). This function is used to generate all the possible states of the DFA and returns the powerset(of type list) of the set of states of the NFA.
This function generates the set of all final(accepted) states of the DFA. It takes in two parameters: the set of all the possible states of the DFA i.e. the power set of set of all the states of the NFA (of type list) and the set of final states of the NFA(of type list) and returns the set of final states of the DFA (of type list) in accordance to the formula: F_d = {q in Q_d : F_n intersection q != phi}.
This function generates the next state in the DFA for the given initial state(of type list) and input (of type character) in accordance to the t_func of the NFA (of type list). It returns the union of the output of each element in initial_state for given input as the next state(of type list) in accordance to the formula: del_d(R) = {Union of all r' : r'=del_n(r)}
This function generates the t_func of the dfa, given the input alphabet (of type list of characters), the t_func of the nfa (of type list) and the set of all the possible states of the DFA (of type list). It generates the t_func in the following manner: It first considers each state in all_states_dfa. For each state and input character in the alphabet, it finds the next state using the function op_for (as defined above) and appends the transition (which is a list consisting of the state, input character and the next state) to the t_func of the dfa. This function returns a list.
nfa = dict()
with open('input.json', 'r') as f: # load the NFA from the file
nfa = json.load(f)
dfa = dict()
dfa["states"] = pow(2, nfa["states"])
dfa["letters"] = nfa["letters"]
all_states_dfa = generatePowerSet(list(range(0, nfa["states"])), nfa['states'])
dfa["t_func"] = generate_t_func_dfa(nfa["t_func"], nfa["letters"], all_states_dfa)
dfa["start"] = [nfa["start"]]
dfa["final"] = final_set_generator(all_states_dfa, nfa["final"])
Initialize DFA as an empty dictionary. And then assign its parameters:
- Number of states in a DFA is 2^number of states of an NFA
- Both NFA and DFA follow the same alphabet and hence have the same set of letters.
- The states of the DFA is given by the power set of all the states of the NFA.
- The t_func of the DFA is generated using the generate_t_func_dfa function as described above.
- Both DFA and NFA have the same start state.
- The set of all final/accept states of the DFA is generated using the final_set_generator function as described above.
with open('output.json', 'w') as outfile:
json.dump(dfa, outfile, indent=2)
{
"states": 2,
"letters": ["a", "b"],
"t_func": [
[0, "a", [0, 1]],
[0, "b", [1]],
[1, "b", [0, 1]]
],
"start": 0,
"final": [1]
}
{
"states": 4,
"letters": ["a", "b"],
"t_func": [
[[], "a", []],
[[], "b", []],
[[0], "a", [0, 1]],
[[0], "b", [1]],
[[1], "a", []],
[[1], "b", [0, 1]],
[[0, 1], "a", [0, 1]],
[[0, 1], "b", [0, 1]]
],
"start": [0],
"final": [[1], [0, 1]]
}
- There are no NULL transitions
- All the transitions, including those that have no next state, is included in the t_func of the DFA