Write Required Functions Script Solve Non Deterministic Finite Automaton Problem Solved De Q43902860
Write the required functions and script that solve, for aNon-Deterministic Finite Automaton, the same problem that wassolved for a Deterministic Finite Automaton in Problem #3 (above).Read about the differences between these two automata (below).Hint: Adapt your code for the FA problem to solve the more generalNDFA problem.
A non-deterministic finite automaton (NDFA) is machine describedby its states and itstransitions: each transition fora statespecifies an input and aset of states (more than one isallowed) that input can lead to: sets withmore than one states is what makes itnon-deterministic. We can illustrate a NDFA as a graph with statelabels in circles and edge labels for transitions (see below). Thecritical difference between an FA and an NDFA is that an NDFA canhave multiple edges with the same label going to different states(we’ll see how to represent and simulate such transitionsbelow).
Input and Output:
Read a file that describes a NDFA: each line contains a state andan arbitrary number of input->statetransitions. Build a dictionary such that each keyis a str state and whose associated value isanother dictionary specifying all the transitions from that state:this second dictionary has keys that are strinputs and associated values that are sets ofstr states: all the states a particular input canlead to. The first token on each line is the strstate and the remaining tokens (always coming in pairs) arestrinputs and states. All tokens (which cancomprise any number of characters) are separated by one semicoloncharacter. We annotate this dictionary as{str:{str:{str}}}.
For example, the input file ndfaendin01.txtcontains the following lines (which could appear in this order, orany other and still specify the same NDFA):
start;0;start;1;start;0;near near;1;end endHere is a picture of the endin01 NDFA. Itgraphically illustrates the three states(start, near, andend) and their transitions, usinginputs (0 and1).
Here, the state start is a key in the maindictionary. It’s value is a dictionary with two key/value pairs:0 mapping to the setcontainingstart and near, and1 mapping to the set containingjust start. It means that in thestart state, if the input is a 0the NDFA can stay in the start state or it can goto the near state; if the input is a1 the NDFA must stay in the startstate. And similarly the next line means that in thenear state, if the input is a 1the NDFA must go into the end state. The last linemeans that the end state has no transitions out ofit.
Print the NDFA, one state (and its transitions) per line; thestates are printed alphabetically and the transition dictionary foreach state is printed as a list of input/set ofstate items (2-tuples) such that these are printed alphabeticallyby the inputs, and the set of states for each input is printed asan alphabetically sorted list (e.g., near comesbefore start). Note that the stateend is a key in the main dictionary, whoseassociated transitions are an empty dictionary.
For example, the file above would produce:
The Contents of the file picked for this Non-Deterministic Finite Automaton end transitions: [] near transitions: [(‘1’, [‘end’])] start transitions: [(‘0’, [‘near’, ‘start’]), (‘1’, [‘start’])]
Note that there are multiple data files for this program:ndfaendin01.txt and ndfatrain.txtand ndfare.txt;; test/debug your program on thefirst file; when you are done, test it on the last file. Draw theFA represented by each for to ensure that your code correctlyprints and computes with it.
Next, repeatedly read and process lines from a second inputfile, computing the results of the non-determinisitc finiteautomaton on the specified start-state with its inputs ; then printout the results in a special form. Each line in the file contains astart-state followed by a sequence of inputs (all separated bysemicolons). The start-state will be a state in the FA (it is a keyin the outer dictionary) the inputs may specify legal or illegaltransitions (may or may not be keys in some inner dictionary).
For example, the input filendfainputendin01.txt contains the following twolines:
start;1;0;1;1;0;1 start;1;0;1;1;0;0For example, the first line means, the start-state isstart and the inputs 1,0, 1, 1,0, and 1.
The result of processing each line is to print the start-state,and then each input and the new states (plural) it could transitionto (the could is what makes it non-deterministic),and finally print the stop-states. For thendfaendin01 NDFA and the first line in this file,it should print
Start state = start Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 1; new possible states = [‘end’, ‘start’] Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 1; new possible states = [‘end’, ‘start’] Stop state(s) = [‘end’, ‘start’]
Note that the set of states it might be in areprinted as an alphabetized list. Also noteespecially that in the start state, if the inputis a 0, then the NDFA can either remain in thestart state or go into the nearstate. For this program, we keep track of all states that the NDFAcan be in, using a set ofnew possible states. For the next input,1, we can be either in the startstate (from the start state; an input of1 allows us to stay in the startstate) or the end state (from thenear state; an input of 1 allowsus to transition to the end state). Thus, we keeptrack of the set of states the NDFA can be in, andthe new set of states the NDFA can be in afterprocessing the next input. In this example, because‘end’ is included in the stop-states, this inputdoes end in 01.
For any state that does not have a transition specifying thecurrent input, ignore that input for that state. For example, ifnear is one of the possible states and0 is the input, ignore the 0 forthe near state.
Functions and Script:
Write the following functions and script. I am providing linecounts for these function bodies not as requirements, but toindicate the lengths of well-written Pythonic code.
- read_ndfa has an open (file) parameter; itreturns the dictionary representing the non-deterministic finiteautomaton; hint: I used splicing and thezip function to build the inner dinctionaries, andI called the setdefault function for the innerdict: alternatively I could have built it asdefaultdicts from thestandard collections module (body is 9lines).
- ndfa_as_str has a dictionary parameter(representing the FA); it returns a multi-line string (each line isended by ‘n’), which when printed shows thecontents of the NDFA in the appropriate textual form: sortedalphabetically by state, with a state’s transitions sorted by theirinput values, and sorted by states if an input results in multiplestates (body is 4 lines; can you do it in 1?).
- process has a dictionary parameter(representing the NDFA), a str parameter(representing the start-state), and alistparameter (representing alist of str inputs); it returns alist that contains the start-state followed bytuples that show the input and resultingset of states after each transition. For theexample shown above, process returns the followinglist. [‘start’, (‘1’, {‘start’}), (‘0’, {‘near’, ‘start’}), (‘1’, {‘end’, ‘start’}), (‘1’, {‘start’}), (‘0’, {‘near’, ‘start’}), (‘1’, {‘end’, ‘start’})]Finally, remember that if an input is illegal for the current state(is not the key in some transition for the current state), justignore it. But if the input leads to no possible states (the emptyset of states) terminate processing there (body is 12 lines).
- interpret has a listparameter (the list result produced byprocess); it returns a multi-line string (eachline is ended by ‘n’), which when printedillustrates the results of processing an NDFA on an input in theappropriate textual form. Note that in this output thesets computed in process appearas lists sorted alphabetically by state. See howit prints the example list argument shown above inthe Sample Interaction below (body is 5lines).
- Write a script at the bottom of this module (in if__name__ == ‘__main__’:) that prompts the user to enterthe file describing the NDFA, prints it, prompts the user to enterthe file containing lines of start-states and input, and simulatesthe NDFA on each line, printing the results in the appropriatetextual form (body is 7 lines).
Sample Interaction:
The program, as specified, will have the following interaction:user-typed information appears in italics. Your outputshould “match” this one. Pick the file name containing this Non-Deterministic Finite Automaton: ndfaendin01.txt The Contents of the file picked for this Non-Deterministic Finite Automaton end transitions: [] near transitions: [(‘1’, [‘end’])] start transitions: [(‘0’, [‘near’, ‘start’]), (‘1’, [‘start’])] Pick the file name containing a sequence of start-states and subsequent inputs: ndfainputendin01.txt Commence tracing this NDFA from its start-state Start state = start Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 1; new possible states = [‘end’, ‘start’] Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 1; new possible states = [‘end’, ‘start’] Stop state(s) = [‘end’, ‘start’] Commence tracing this NDFA from its start-state Start state = start Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 1; new possible states = [‘end’, ‘start’] Input = 1; new possible states = [‘start’] Input = 0; new possible states = [‘near’, ‘start’] Input = 0; new possible states = [‘near’, ‘start’] Stop state(s) = [‘near’, ‘start’]
In Week #2 of this course we will cover EBNF and regularexpressions, which relate to the files below. You can run thesefiles on your code to ensure they produce the correct results.
The ndfatrain.txt file is a non-deterministicfinite automaton that determines whether or not a train (a sequenceof characters representing different kinds of cars) is a legaltrain according to Chapter Exercise #7 in the ENBF lecture. Itsinput file is ndfainputtrain.txt, which startswith a legal train (one that ends with the statedone as one possible state) followed by an illegaltrain (one that does not end with the state doneas one possible state).
The ndfare.txt file is a non-deterministicfinite automaton translation of the regular expression((a*|b)cd)+. Its input file isndfainputre.txt, which starts with a match (onethat ends with the state last as one possiblestate) followed by a non-match (one that does not end with thestate last as one possible state).
0,1 start near end Show transcribed image text 0,1 start near end
Expert Answer
Answer to Write the required functions and script that solve, for a Non-Deterministic Finite Automaton, the same problem that was …
OR