Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lfa:lab05-nfa-python [2020/11/02 09:22] lfa | lfa:lab05-nfa-python [2020/11/08 13:11] (current) dmihai | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== NFA Python Implementation ====== | ====== NFA Python Implementation ====== | ||
| + | |||
| + | Consider the following encoding of an NFA: | ||
| + | <code> | ||
| + | <number_of_states> | ||
| + | <list_of_chars_in_alphabet> | ||
| + | <list_of_final_states> | ||
| + | <state> <symbol> <list_of_states> | ||
| + | </code> | ||
| + | |||
| + | Example: | ||
| + | <code> | ||
| + | 9 | ||
| + | a b | ||
| + | 4 | ||
| + | 0 EPS 1 | ||
| + | 0 a 5 | ||
| + | 0 b 0 | ||
| + | 1 EPS 2 | ||
| + | 1 b 3 5 | ||
| + | 2 EPS 4 | ||
| + | 2 a 2 3 | ||
| + | 3 a 3 | ||
| + | 3 b 3 | ||
| + | 4 EPS 4 7 8 | ||
| + | 5 a 6 | ||
| + | 5 b 5 | ||
| + | 6 EPS 7 | ||
| + | 6 a 6 | ||
| + | 7 EPS 6 | ||
| + | 7 b 4 | ||
| + | </code> | ||
| + | |||
| + | You'll find in //nfa_skel.rar// a class that already reads the input and forms the NFA. | ||
| + | |||
| + | Also, you can use the **graphvizNFA** method to get a graphical representation of the NFA, but you will have to install the //graphviz// library. | ||
| + | <code> | ||
| + | pip install graphviz | ||
| + | </code> | ||
| Implement (in Python) an NFA class that supports the following methods: | Implement (in Python) an NFA class that supports the following methods: | ||
| - | * **step(configuration)** = returns a list including all reachable configurations in //one step//. An epsilon transition is also considered a step. | + | |
| - | * **kstep(configuration, k)** = returns a list including all reachable configurations in //k steps//. An epsilon transition is also considered a step. | + | 1. **step(configuration)** | 
| - | * **accept(self, word: Word)** = True or False if the NFA accepts the given word. To simplify this task, you can use the kstep function. | + | * returns a list with all reachable configurations in //one step//. | 
| - | * **epsilonClosure(self, state: State)** = the set of all states where we can go from the current state by going through epsilon transitions | + | * an epsilon transition is also considered a step. | 
| - | * **emptyLanguage(self)** = True or False if the NFA's accepted language is the Empty Language | + | |
| + | 2. **kstep(configuration, k)** | ||
| + | * returns a list including all reachable configurations in //k steps// | ||
| + | * an epsilon transition is also considered a step. | ||
| + | * the final list contains only the states where we stopped, not the entire path to them. | ||
| + | * if no transitions are available and $ k > 0 $, return an empty list | ||
| + | |||
| + | 3. **accept(self, word: Word)** | ||
| + | * True or False if the NFA accepts the given word. | ||
| + | |||
| + | 4. **epsilonClosure(self, state: State)** | ||
| + | * the set of all states where we can go from the current state by going through epsilon transitions | ||
| + | |||
| + | 5. **emptyLanguage(self)** | ||
| + | * True or False if the NFA's accepted language is the Empty Language | ||
| {{:lfa:nfa_skel.rar|}} | {{:lfa:nfa_skel.rar|}} | ||
| + | |||