Finite State Automata utilities
Project description
Finite State Automata
A small project demonstrating both deterministic and nondeterministic finite state machines.
Docs
Install
pip install python-fsa
Installation Notes
Python-fsa depends on graphviz and pygraphviz. There seem to be some global options that are needed to install pygraphviz. I have only tested this on macOS and have found the following commands to work:
brew install graphviz
pip install \
--global-option=build_ext \
--global-option="-I/opt/homebrew/Cellar/graphviz/8.0.5/include/" \
--global-option="-L/opt/homebrew/Cellar/graphviz/8.0.5/lib" \
pygraphviz
pip install python-fsa
Replace the 8.0.5
version number with the current version of graphviz.
See this comment
for more.
Examples
DFA Example
Consider the following DFA that recognises the language of words over the alphabet {0, 1} which contain an even number of 1s
A DFA instance can be constructed:
from python_fsa.dfa import DFA
a, b = "a", "b"
dfa = DFA(
alphabet=frozenset((0, 1)),
states=frozenset((a, b)),
initial=a,
transitions={
(a, 0): a,
(a, 1): b,
(b, 0): b,
(b, 1): a,
},
final=frozenset((a,)),
)
Words can then be accepted or rejected by calling accepts
:
dfa.accepts((0, 0, 0, 1)) # False
dfa.accepts((0, 1, 1, 0)) # True
Words can be given one at a time to a mutable transducer of the DFA:
dfa_transducer = dfa.transducer()
dfa_transducer.push(1) # False
dfa_transducer.push(0) # False
dfa_transducer.push(1) # True
dfa_transducer.push(0) # True
NFA Example
Consider the following NFA that recognises the language of words over the alphabet {0, 1} whose second to last symbol is 1.
An NFA instance can be constructed:
from python_fsa.nfa import NFA
a, b, c = "a", "b", "c"
nfa = NFA(
alphabet=frozenset((1, 0)),
states=frozenset((a, b, c)),
initial=a,
transitions={
(a, 0): frozenset((a,)),
(a, 1): frozenset((a, b)),
(b, 0): frozenset((c,)),
(b, 1): frozenset((c,)),
},
final=frozenset((c,)),
)
Words can then be accepted or rejected by calling accepts
:
nfa.accepts((0, 1, 1, 0)) # True
nfa.accepts((0, 0, 0, 1)) # False
This NFA can be converted to an equivalent DFA by calling to_dfa
:
dfa = nfa.to_dfa()
However, this will result in a DFA of type DFA[T, frozenset[S]]
– as the
states of the resulting DFA are from the powerset of NFA states. This can cause
errors in writing the resulting DFA to dot-format.
The frozenset[S]
states can be squashed to strings by calling dfa.squash()
,
which stringifies and joins states in each frozenset[S]
:
dfa = nfa.to_dfa().squash()
Which produces the following DFA:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-fsa-0.0.5.tar.gz
.
File metadata
- Download URL: python-fsa-0.0.5.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8ed7de73902c0566e0f13c95dc4b414463b0b035bccb0658f1f32e06d008d27 |
|
MD5 | 540e326aa2ac16f6b69239d9af1408eb |
|
BLAKE2b-256 | 97974608dd4ababde4d1185b67bf0de440a533da17514508527c06411de8b665 |
File details
Details for the file python_fsa-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: python_fsa-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05607b69979582d632dfb07a08e5c3e6b5ebbb89081438ddc2f168db9f1c825d |
|
MD5 | b1de62c4c8ef9abfbac826baf7e26bfc |
|
BLAKE2b-256 | d0fb309f5ce934a6cf636d0128740f7f034b2fa3efa00cdc7b1e17de67d7f6df |