Skip to main content

Implementation of finite-state machines and exportation to dot format

Project description

Finite-state machines with dot

Build Status PyPI PyPI - Python Version License: MIT

fsmdot is a Python package to create finite-state machines which can be exported to dot format. It uses the pygraphviz library which is a Python interface to the Graphviz graph layout and visualization package.

Installing

  • First, you need to install Graphviz. See how to download it here.
  • Then, fsmdot can be installed using pip:
pip3 install fsmdot

Usage

With the fsmdot library, you can create two different types of finite-state machine:

  • Deterministic finite automaton (DFA)
  • Nondeterministic finite automaton (NFA)

A finite-state machine is represented by a quintuple (Q, S, T, q0, F) where:

  • Q is a set of states
  • S is a set of input symbols (alphabet)
  • d is a dictionnary containing the transitions
  • q0 is the initial state
  • F is the set of accept states

Deterministic finite automaton

This is how to create a deterministic finite automaton.

  • First, we import the Dfa class:
from fsmdot.dfa import Dfa
  • Create the set of states:
Q = {'S1', 'S2'}
  • Create the set of symbols representing the input alphabet:
S = {'0', '1'}
  • Create the transitions as a dictionnary.
d = {
    'S1': {
        '0': 'S2',
        '1': 'S1'
    },
    'S2': {
        '0': 'S1',
        '1': 'S2'
    }
}
  • Create the initial state (the state must be in Q):
q0 = 'S1'
  • Create the set of accept states (the states must be in Q):
F = {'S1'}
  • Then, you can create the DFA:
a = Dfa(Q, S, d, q0, F)
  • To see the state-transition table, use the print_table method:
a.print_table()

This is the result:

+---------+-----+-----+
|         |   0 |   1 |
+=========+=====+=====+
| -> * S1 |  S2 |  S1 |
+---------+-----+-----+
|      S2 |  S1 |  S2 |
+---------+-----+-----+
  • You can check if a string is accepted by the automata using the accept method:
print(a.accept('11110'))
print(a.accept('110110110101'))

This is the result:

False
True
  • To create the dot graph representing the DFA, use the dot_graph method. It creates a graph object.
G = a.dot_graph()

You can print the string representation of the graph using the to_string method or write this content in a file using the write method (see pygraphviz):

print(G.to_string())
G.write('graph1_dfa.dot')

Result:

strict digraph FSM {
	graph [rankdir=LR];
	node [shape=circle];
	null	[shape=point];
	S1	[shape=doublecircle];
	null -> S1;
	S1 -> S1	[label=1];
	S1 -> S2	[label=0];
	S2 -> S1	[label=0];
	S2 -> S2	[label=1];
}

File graph1_dfa.dot:

Graph 1

Nondeterministic finite automaton

You can also create nondeterministic finite automatons using the Nfa class.

  • To add epsilon-moves, use the symbol Nfa.EPSILON.

Example:

from fsmdot.nfa import Nfa

Q = {1, 2, 3, 4}
S = {Nfa.EPSILON, '0', '1'}
d = {
    1: {
        Nfa.EPSILON: {3},
        '0': {2}
    },
    2: {
        '1': {2, 4}
    },
    3: {
        Nfa.EPSILON: {2},
        '0': {4}
    },
    4: {
        '0': {3}
    }
}
q0 = 1
F = {3, 4}

a = Nfa(Q, S, d, q0, F)
a.print_table()

G = a.dot_graph()
G.write('graph6_nfa.dot')

State-transition table:

+------+-----+--------+-----+
|      |   0 |      1 |   ε |
+======+=====+========+=====+
| -> 1 | {2} |     {} | {3} |
+------+-----+--------+-----+
|    2 |  {} | {2, 4} |  {} |
+------+-----+--------+-----+
|  * 3 | {4} |     {} | {2} |
+------+-----+--------+-----+
|  * 4 | {3} |     {} |  {} |
+------+-----+--------+-----+

File graph6_nfa.dot:

Graph 6 NFA

  • To calculate the epsilon closure of a state, use the epsilon_closure method.
# Calculations of epsilon closure
for state in Q:
    print(state, a.epsilon_closure(state))

Result:

1 {1, 2, 3}
2 {2}
3 {2, 3}
4 {4}
  • To convert a NFA to a DFA, use the to_dfa method. It uses the powerset construction.
# Conversion to DFA
dfa = a.to_dfa()
dfa.print_table()
G2 = dfa.dot_graph()
G2.write('graph6_dfa.dot')

State-transition table of dfa:

+----------------+--------+--------+
|                |      0 |      1 |
+================+========+========+
| -> * {1, 2, 3} | {2, 4} | {2, 4} |
+----------------+--------+--------+
|       * {2, 3} |    {4} | {2, 4} |
+----------------+--------+--------+
|       * {2, 4} | {2, 3} | {2, 4} |
+----------------+--------+--------+
|          * {4} | {2, 3} |     {} |
+----------------+--------+--------+

File graph6_dfa.dot:

Graph 6 NFA

Examples

To see how the library works, look at the examples in the examples folder.

References

Author

Quentin Deschamps

License

MIT

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

fsmdot-0.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

fsmdot-0.0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file fsmdot-0.0.1.tar.gz.

File metadata

  • Download URL: fsmdot-0.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for fsmdot-0.0.1.tar.gz
Algorithm Hash digest
SHA256 55ced4f562466cef1236628eb66022a0dab8f75aae64385046d05879a68feb4c
MD5 568bbb58c7e515deaea978ac40320812
BLAKE2b-256 be842278e3673ff9dde3e042a33b36ae36ffef4ce82866e4b239b5bb2e272261

See more details on using hashes here.

File details

Details for the file fsmdot-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fsmdot-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for fsmdot-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 202c4f2c654ba4569664790e60268308e6927d7e30d9c395853838a659d9c12e
MD5 a7f16c633d87aaf7dfcd8a1427a7e029
BLAKE2b-256 0cc646646efb472f6f7277ce8a4ffffba992e4d61cf995a0ce9b66f1914da85a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page