{"id":25435750,"url":"https://github.com/quentin18/fsmdot","last_synced_at":"2025-11-01T02:30:35.112Z","repository":{"id":57432383,"uuid":"311064831","full_name":"Quentin18/fsmdot","owner":"Quentin18","description":"Implementation of finite-state machines and exportation to dot format","archived":false,"fork":false,"pushed_at":"2020-11-30T17:56:05.000Z","size":49,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-16T11:51:53.692Z","etag":null,"topics":["dfa","dfa-minimization","fsm","graphviz-dot","nfa","nfa-to-dfa-conversion"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/fsmdot/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Quentin18.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-08T13:07:01.000Z","updated_at":"2025-01-21T12:12:56.000Z","dependencies_parsed_at":"2022-08-29T17:13:31.151Z","dependency_job_id":null,"html_url":"https://github.com/Quentin18/fsmdot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Quentin18%2Ffsmdot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Quentin18%2Ffsmdot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Quentin18%2Ffsmdot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Quentin18%2Ffsmdot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Quentin18","download_url":"https://codeload.github.com/Quentin18/fsmdot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239252876,"owners_count":19607924,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dfa","dfa-minimization","fsm","graphviz-dot","nfa","nfa-to-dfa-conversion"],"created_at":"2025-02-17T07:31:51.890Z","updated_at":"2025-11-01T02:30:35.076Z","avatar_url":"https://github.com/Quentin18.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Finite-state machines with dot\n\n[![Build Status](https://travis-ci.org/Quentin18/fsmdot.svg?branch=master)](https://travis-ci.org/Quentin18/fsmdot)\n![PyPI](https://img.shields.io/pypi/v/fsmdot)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fsmdot)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**fsmdot** is a Python package to create finite-state machines which can be exported to dot format. It uses the [pygraphviz](https://pygraphviz.github.io/) library which is a Python interface to the [Graphviz](https://graphviz.org/) graph layout and visualization package.\n\n## Installing\n- First, you need to install Graphviz. See how to download it [here](https://graphviz.org/download/).\n- Then, *fsmdot* can be installed using [pip](https://pip.pypa.io/en/stable/):\n```\npip3 install fsmdot\n```\n\n## Usage\nWith the *fsmdot* library, you can create two different types of finite-state machine:\n- **Deterministic finite automaton** (DFA)\n- **Nondeterministic finite automaton** (NFA)\n\nA finite-state machine is represented by a quintuple (Q, S, T, q0, F) where:\n- **Q** is a set of states\n- **S** is a set of input symbols (alphabet)\n- **d** is a dictionnary containing the transitions\n- **q0** is the initial state\n- **F** is the set of accept states\n\n### Deterministic finite automaton\nThis is how to create a deterministic finite automaton.\n- First, we import the **Dfa** class:\n```python\nfrom fsmdot.dfa import Dfa\n```\n- Create the set of states:\n```python\nQ = {'S1', 'S2'}\n```\n- Create the set of symbols representing the input alphabet:\n```python\nS = {'0', '1'}\n```\n- Create the transitions as a dictionnary.\n```python\nd = {\n    'S1': {\n        '0': 'S2',\n        '1': 'S1'\n    },\n    'S2': {\n        '0': 'S1',\n        '1': 'S2'\n    }\n}\n```\n- Create the initial state (the state must be in Q):\n```python\nq0 = 'S1'\n```\n- Create the set of accept states (the states must be in Q):\n```python\nF = {'S1'}\n```\n- Then, you can create the DFA:\n```python\na = Dfa(Q, S, d, q0, F)\n```\n- To see the state-transition table, use the **print_table** method:\n```python\na.print_table()\n```\nThis is the result:\n```\n+---------+-----+-----+\n|         |   0 |   1 |\n+=========+=====+=====+\n| -\u003e * S1 |  S2 |  S1 |\n+---------+-----+-----+\n|      S2 |  S1 |  S2 |\n+---------+-----+-----+\n```\n- You can check if a string is accepted by the automata using the **accept** method:\n```python\nprint(a.accept('11110'))\nprint(a.accept('110110110101'))\n```\nThis is the result:\n```\nFalse\nTrue\n```\n- To create the dot graph representing the DFA, use the **dot_graph** method. It creates a graph object.\n```python\nG = a.dot_graph()\n```\nYou 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](https://pygraphviz.github.io/)):\n```python\nprint(G.to_string())\nG.write('graph1_dfa.dot')\n```\nResult:\n```\nstrict digraph FSM {\n\tgraph [rankdir=LR];\n\tnode [shape=circle];\n\tnull\t[shape=point];\n\tS1\t[shape=doublecircle];\n\tnull -\u003e S1;\n\tS1 -\u003e S1\t[label=1];\n\tS1 -\u003e S2\t[label=0];\n\tS2 -\u003e S1\t[label=0];\n\tS2 -\u003e S2\t[label=1];\n}\n```\nFile *graph1_dfa.dot*:\n\n![Graph 1](./img/graph1.svg)\n\n### Nondeterministic finite automaton\nYou can also create nondeterministic finite automatons using the **Nfa** class.\n- To add epsilon-moves, use the symbol **Nfa.EPSILON**.\n\nExample:\n```python\nfrom fsmdot.nfa import Nfa\n\nQ = {1, 2, 3, 4}\nS = {Nfa.EPSILON, '0', '1'}\nd = {\n    1: {\n        Nfa.EPSILON: {3},\n        '0': {2}\n    },\n    2: {\n        '1': {2, 4}\n    },\n    3: {\n        Nfa.EPSILON: {2},\n        '0': {4}\n    },\n    4: {\n        '0': {3}\n    }\n}\nq0 = 1\nF = {3, 4}\n\na = Nfa(Q, S, d, q0, F)\na.print_table()\n\nG = a.dot_graph()\nG.write('graph6_nfa.dot')\n```\nState-transition table:\n```\n+------+-----+--------+-----+\n|      |   0 |      1 |   ε |\n+======+=====+========+=====+\n| -\u003e 1 | {2} |     {} | {3} |\n+------+-----+--------+-----+\n|    2 |  {} | {2, 4} |  {} |\n+------+-----+--------+-----+\n|  * 3 | {4} |     {} | {2} |\n+------+-----+--------+-----+\n|  * 4 | {3} |     {} |  {} |\n+------+-----+--------+-----+\n```\nFile *graph6_nfa.dot*:\n\n![Graph 6 NFA](./img/graph6_nfa.svg)\n\n- To calculate the epsilon closure of a state, use the **epsilon_closure** method.\n```python\n# Calculations of epsilon closure\nfor state in Q:\n    print(state, a.epsilon_closure(state))\n```\nResult:\n```\n1 {1, 2, 3}\n2 {2}\n3 {2, 3}\n4 {4}\n```\n- To convert a NFA to a DFA, use the **to_dfa** method. It uses the powerset construction.\n```python\n# Conversion to DFA\ndfa = a.to_dfa()\ndfa.print_table()\nG2 = dfa.dot_graph()\nG2.write('graph6_dfa.dot')\n```\nState-transition table of dfa:\n```\n+----------------+--------+--------+\n|                |      0 |      1 |\n+================+========+========+\n| -\u003e * {1, 2, 3} | {2, 4} | {2, 4} |\n+----------------+--------+--------+\n|       * {2, 3} |    {4} | {2, 4} |\n+----------------+--------+--------+\n|       * {2, 4} | {2, 3} | {2, 4} |\n+----------------+--------+--------+\n|          * {4} | {2, 3} |     {} |\n+----------------+--------+--------+\n```\nFile *graph6_dfa.dot*:\n\n![Graph 6 NFA](./img/graph6_dfa.svg)\n\n## Examples\nTo see how the library works, look at the examples in the *examples* folder.\n\n## References\n- [Automata theory](https://en.wikipedia.org/wiki/Automata_theory)\n- [Finite-state machines](https://en.wikipedia.org/wiki/Finite-state_machine)\n- [Deterministic finite automaton](https://en.wikipedia.org/wiki/Deterministic_finite_automaton)\n- [Nondeterministic finite automaton](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton)\n- [Powerset construction](https://en.wikipedia.org/wiki/Powerset_construction)\n- [DFA minimization](https://en.wikipedia.org/wiki/DFA_minimization)\n\n## Author\n[Quentin Deschamps](mailto:quentindeschamps18@gmail.com)\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquentin18%2Ffsmdot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquentin18%2Ffsmdot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquentin18%2Ffsmdot/lists"}