This repo includes two types of automaton:
- DFA (Deterministic Finite Automaton)
- Turing Machine
DFA is represented by 5-tuple:
- ∑ is the non-empty set of input alphabet/symbols:
String input
- S is the non-empty set of states:
HashSet<String> setOfStates
- s0 is the starting state:
String startState
- 𝛿 is the transition function: 𝛿 : S × ∑ -> S:
ArrayList<String[]> transition
, the String array must have three elements in order: fromNode, transition value, toNode - F is the set of final/accepting states:
String acceptState
When initialize the DFA class, four types of elements need to be presented: the non-empty set of states, the starting state, the set of final/accepting states and the transition function.
Sample code:
String startState = "a";
String acceptState = "c";
HashSet<String> setOfStates = new HashSet<String>(Arrays.asList("a", "b", "c"));
ArrayList<String[]> transition = new ArrayList<>();
transition.add(new String[] { "a", "0", "b" });
transition.add(new String[] { "b", "1", "c" });
transition.add(new String[] { "c", "0", "c" });
transition.add(new String[] { "c", "1", "c" });
DFA myDFA = new DFA(setOfStates, startState, acceptState, transition);
This DFA has a diagram that looks like this:
When testing a specific input string, the input need to be in String type, and .run()
function will output whether the current DFA accepts the given input string:
String input = "01.0111";
System.out.println(myDFA.run(input));
This example builds a DFA that will accepty any 0/1 string that starts with 01
sequence
- Note that the kleene star(*) can be used to represent any character.
Turing Machine is represented by 7-tuple:
- ∑ is the non-empty set of input alphabet/symbols:
String input
, which stands for an infinitely long input tape string. - Non-empty set of symbols, tape alphabets: not needed when initialize the TM.
- S is the non-empty set of states:
HashSet<String> setOfStates
- s0 is the starting state:
String startState
- 𝛿 is the transition function: 𝛿 : S × ∑ -> S:
ArrayList<String[]> transition
, the String array must have five elements in order: fromNode, read value, update value, direction, toNode - F is the set of final/accepting states:
String acceptState
- Reject states: default state, not needed when iniitialize the TM.
When initialize the TM class, four types of elements need to be presented: the non-empty set of states, the starting state, the set of final/accepting states and the transition function.
This Turing Machine reads a 0/1 tape string as input and delete the last two elements of this input. Sample code:
String startState = "a";
String acceptState = "d";
HashSet<String> setOfStates = new HashSet<String>(Arrays.asList("a", "b", "c", "d"));
ArrayList<String[]> transition = new ArrayList<>();
transition.add(new String[] { "a", "0", "0", "R", "a" });
transition.add(new String[] { "a", "1", "1", "R", "a" });
transition.add(new String[] { "a", "*", "*", "L", "b" });
transition.add(new String[] { "b", "1", "*", "L", "c" });
transition.add(new String[] { "b", "0", "*", "L", "c" });
transition.add(new String[] { "c", "1", "*", "L", "d" });
transition.add(new String[] { "c", "0", "*", "L", "d" });
TuringMachine tm = new TuringMachine(setOfStates, startState, acceptState, transition);
This Turing Machine has a diagram that looks like this:
When testing a specific input string, the input need to be in String type, and .run()
function will output the result. If the current Turing Machine does not accept the given ipnut string, the result will be "Input string not accepted"
:
String input = "*01100*";
System.out.println(tm.run(input));
Output: 011
- Note that the input string must start and end with
*
, which stands for the boundary of infinitely long input tape string. - If the Turing Machine does not halt due to a bad configuration, the Turing Machine will run forever due to Turing undecidability.