https://github.com/rohaquinlop/automathon
A Python library for simulating and visualizing finite automata
https://github.com/rohaquinlop/automathon
automata dfa epsilon-nfa nfa pypi python python-automaton python-library python3 sigma visualization
Last synced: about 1 year ago
JSON representation
A Python library for simulating and visualizing finite automata
- Host: GitHub
- URL: https://github.com/rohaquinlop/automathon
- Owner: rohaquinlop
- License: mit
- Created: 2021-03-01T15:46:49.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-21T15:37:21.000Z (over 1 year ago)
- Last Synced: 2025-04-05T14:58:34.289Z (about 1 year ago)
- Topics: automata, dfa, epsilon-nfa, nfa, pypi, python, python-automaton, python-library, python3, sigma, visualization
- Language: Python
- Homepage: https://rohaquinlop.github.io/automathon/
- Size: 783 KB
- Stars: 66
- Watchers: 1
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# automathon
A Python library for simulating and visualizing finite automata.
---
**Documentation**: https://rohaquinlop.github.io/automathon/
**Source Code**: https://github.com/rohaquinlop/automathon
**PyPI**: https://pypi.org/project/automathon/
---
## Requirements
- Python >= 3.10
- You also need to install Graphviz on your computer ([download page](https://www.graphviz.org/download/), [installation procedure for Windows](https://forum.graphviz.org/t/new-simplified-installation-procedure-on-windows/224), [archived versions](https://www2.graphviz.org/Archive/stable/)).Make sure that the directory containing the **dot** executable is on your systems’ path.
## Installation
```bash
pip install automathon
```
### Upgrade
```bash
pip install automathon --upgrade
```
## Example
Here is are some examples about what you can do with **automathon**, you can
check the documentation about [Deterministic Finite Automata](https://rohaquinlop.github.io/automathon/dfa/) and
[Non-Deterministic Finite Automata](https://rohaquinlop.github.io/automathon/nfa/) to know more about the functions and
methods that are available.
### DFA - Deterministic Finite Automata

This image was created using **automathon**.
Let's create the previous automata using the library:
```python
from automathon import DFA
q = {'q0', 'q1', 'q2'}
sigma = {'0', '1'}
delta = { 'q0' : {'0' : 'q0', '1' : 'q1'},
'q1' : {'0' : 'q2', '1' : 'q0'},
'q2' : {'0' : 'q1', '1' : 'q2'}
}
initial_state = 'q0'
f = {'q0'}
automata = DFA(q, sigma, delta, initial_state, f)
```
#### Verify if the automata is valid
```python
automata.is_valid() # True
```
In this case, the automata is valid but if it wasn't, the library would raise an
exception with the error message.
#### Errors
Errors that the library can raise are:
- **SigmaError**:
- The automata contain an initial state, or a final state that's not defined in Q.
- The automata contain a delta transition that's not defined in Q nor Sigma.
- **InputError**:
- The automata is trying to consume a letter that's not defined in sigma.
#### Verify if the automata accept a given string
```python
automata.accept("001001") # True
automata.accept("00100") # False
```
#### Get the automata's complement
```python
not_automata = automata.complement()
not_automata.accept("00100") #True
```
Note that this function returns a new automata, it doesn't modify the original
one.
#### Visualize the automata
For both, [DFA](https://rohaquinlop.github.io/automathon/dfa/) and [NFA](https://rohaquinlop.github.io/automathon/nfa/), the view method enables to visualize the automaton, receives as parameter a String as the file name for the png and svg files.
More information about the graphviz attributes [here](https://www.graphviz.org/doc/info/attrs.html).

```python
# Default styling
automata.view("DFA Visualization")
# You can decide between png and svg file formats
# If you want to add custom styling, you can use the following
automata.view(
file_name="DFA Custom Styling",
file_format="png" or "svg",
node_attr={'fontsize': '20'},
edge_attr={'fontsize': '20pt'}
)
```
If you want to explore more about the functions and methods of the DFA class,
you can check the [DFA documentation](https://rohaquinlop.github.io/automathon/dfa/). And if you want to know more about
the NFA class, you can check the [NFA documentation](https://rohaquinlop.github.io/automathon/nfa/).
### NFA - Non-Deterministic Finite Automata

Image taken from: [r9paul.org](http://www.r9paul.org/blog/2008/nondeterministic-finite-state-machine/)
#### Representing the previous automata
```python
from automathon import NFA
## Epsilon Transition is denoted by '' -> Empty string
q = {'q1', 'q2', 'q3', 'q4'}
sigma = {'0', '1'}
delta = {
'q1' : {
'0' : {'q1'},
'1' : {'q1', 'q2'}
},
'q2' : {
'0' : {'q3'},
'' : {'q3'}
},
'q3' : {
'1' : {'q4'},
},
'q4' : {
'0' : {'q4'},
'1' : {'q4'},
},
}
initial_state = 'q1'
f = {'q4'}
automata = NFA(q, sigma, delta, initial_state, f)
```
#### Verify if the automata is valid
```python
automata.is_valid() # True
```
#### Verify if the automata accept a string
```python
automata.accept("0000011") #True
automata.accept("000001") #False
```
#### Get the automata's complement
```python
not_automata = automata.complement()
not_automata.accept("000001") #True
```
#### Visualize the automata

```python
# Default styling
automata.view("NFA Visualization")
# You can decide between png and svg file formats
# If you want to add custom styling, you can use the following
automata.view(
file_name="NFA Custom Styling",
file_format="png" or "svg",
node_attr={'fontsize': '20'},
edge_attr={'fontsize': '20pt'}
)
```
## License
This project is licensed under the terms of the MIT license.