Skip to content

Commit

Permalink
doc: basic readme and documentation for FA classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakrok09 committed Mar 29, 2024
1 parent c5dde8a commit 16bab01
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# TS Automata

[![NPM Package](https://img.shields.io/npm/v/ts-automata.svg?style=flat)](https://npmjs.org/package/ts-automata "View this project on npm")

A TypeScript/JavaScript library for managing automata.

```shell
npm i ts-automata
```

## Overview

Currently supported are the Deterministic and Non-deterministic finite state automata.
Make use of the provided TSDoc and JSDoc for each method to see

## Usage

Automatons can be created by initialising an `Alphabet` to feed to the Automaton,
defining the states of the automaton and finally the transition function.
The code below is an example of creating and running input on a DFA.

```typescript
/* Create an alphabet for the Finite Automaton */
const alphabet = new Set<Symbol>();
alphabet.add(toChar("a"));
alphabet.add(toChar("b"));

/* Initialize the DFA */
const dfa = new DFA(alphabet, "q0", false)

/* Add states to the DFA */
dfa.addState("q1", false);
dfa.addState("q2", true)

/* Define the transition function for each state */
dfa.addEdge("q0", toChar('b'), "q0");
dfa.addEdge("q0", toChar('a'), "q1");
dfa.addEdge("q1", toChar('a'), "q2");
dfa.addEdge("q1", toChar('b'), "q0");
dfa.addEdge("q2", toChar('a'), "q2");
dfa.addEdge("q2", toChar('b'), "q2");

/* Check for validity */
console.log(dfa.isValid());

/* Run strings on the DFA */
dfa.runString("ababababaa") // true
dfa.runString("aa") // true
dfa.runString("abb") // false
```
_Figure 1: The code for the deterministic finite automaton D._

![There are three stares: q0, q1 and q2
q0 is the starting state
q2 is the only accepting state](https://i.imgur.com/pRuPlEv.jpeg "Image of the DFA described in the code abov")
_Figure 2: Visual representation of D._

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "jest",
"build": "tsup"
},
"keywords": ["automata", "typescript"],
"keywords": ["automata", "typescript", "math"],
"author": "Kiril Panayotov",
"license": "MIT",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/automata/regular/DFA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {IllegalArgument, IllegalAutomatonState} from "../../exceptions/exception
/**
* Represents a deterministic finite automaton.
*
* @class DFA
* @extends FiniteAutomaton with DFAState.
*/
export class DFA extends FiniteAutomaton<DFAState> {

Expand Down
5 changes: 5 additions & 0 deletions src/automata/regular/NFA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import {DFA, Symbol, toChar} from "../../index";
import {NFAState} from "../../states/RegularStates";
import {IllegalArgument} from "../../exceptions/exceptions";

/**
* Class representation of a non-deterministic finite automaton.
*
* @extends FiniteAutomaton with NFAState.
*/
export class NFA extends FiniteAutomaton<NFAState> {

/**
Expand Down

0 comments on commit 16bab01

Please sign in to comment.