Non-deterministic finite automaton
w = F =Deterministic finite automaton
w = F =0 | 1 | |
---|---|---|
> a | ab | a |
b | c | ϕ |
* c | ϕ | ϕ |
0 | 1 | |
---|---|---|
> D | E | D |
E | F | D |
* F | F | D |
Non-deterministic Methods
function delta(q, c) { // (1|0)*00
if (q == 'a' && c == '0') return 'ab'
if (q == 'a' && c == '1') return 'a'
if (q == 'b' && c == '0') return 'c'
return ''; //default -- no transition
}
function accept(w, F = 'c', Q = 'a') {
//w: input String
//F: final state(s)
//Q: current state(s)
let i = 0, txt = Q
while (i < w.length) {
let c = w[i], T = ''
for (let q of Q)
T = union(T, delta(q, c))
Q = T
if (Q == '') break
i++; txt += ", " + c + " -> " + Q + '\n' + Q
}
input.selectionStart = i
input.selectionEnd = i + 1
return intersect(Q, F).length > 0
}
Deterministic Methods
function delta2(q, c) { // (1|0)*10
if (q == 'D' && c == '0') return 'E'
if (q == 'E' && c == '0') return 'F'
if (q == 'F' && c == '0') return 'F'
if (c == '1') return 'D'
return ''; //default -- no transition
}
function accept2(w, F = 'F', q = 'D') {
//w: input String
//F: final state(s)
//q: current state
let i = 0, txt = q
while (i < w.length) {
q = delta2(q, w[i])
if (q == '') break
i++; txt += " -> " + q
}
input.selectionStart = i
input.selectionEnd = i + 1
return (q != '' && F.includes(q))
}