I've recently been studying Deterministic Finite-Automata in class, and I've come a across a piece of code from princeton.edu that simply explains it on java. Problem is I'm not really well versed in java and I would like to ask what the line int[][] next = { { 0, 1 }, { 1, 2 }, { 2, 0 } }; in the code does.
I seem to understand how the over all code works. But I can't really understand what the line int[][] next = { { 0, 1 }, { 1, 2 }, { 2, 0 } }; specifically does. This seems to be basic java. can some one please enlighten me? thanks!
/*************************************************************************
* Compilation: javac DFA.java
* Execution: java DFA
*
* Simulate a DFA which recognizes the language of all strings over
* the binary alphabet { a, b } with a multiple of three b's.
*
* % java DFA babbabab
* false
*
* % java DFA babbaabbba
* true
*
*************************************************************************/
public class DFA {
public static void main(String[] args) {
String input = args[0];
boolean[] accept = { true, false, false };
int[][] next = { //<---
{ 0, 1 }, //<---
{ 1, 2 }, //<---
{ 2, 0 } }; //<---
int state = 0;
for (int i = 0; i < input.length(); i++) {
state = next[state][input.charAt(i) - 'a'];
}
System.out.println(accept[state]);
}
}
for more info here's a link to the source: http://introcs.cs.princeton.edu/java/73dfa/
The next
variable references a 2-dimensional array containing the state transitions of the DFA.
The array has three rows, one for each state in the DFA.
The line below shows that the array members in a row are the next state for 'a' and 'b' respectively.
state = next[state][input.charAt(i) - 'a']
The DFA is in state 0 only when the count of the number of b's is a multiple of 3. If it ends in state zero, this array says that the string is accepted.
boolean[] accept = { true, false, false };