i'm working on an application on deterministic finite automata but there's a problem in algorithm, all the details will be set from user's input. it becomes problem if user inputs details like following
totalStates = 3 //user input
initialState = 1 //user input
finalState = 3 //user input
//transitions created in linked-list
//contains three data members(prevState(int), transition symbol(String), nextState(int))
2->b->1 //user input
1->a->2 //user input
2->b->3 //user input
string = ab
this is a valid string for this automata but not according to my algorithm, there's problem in algo that it changes the states according to transitions indexes. like
for 'a' it works good and change the state from 1 to 2
for 'b' it choose the transition from 2 to 1 not 2 to 3
because 2->b->1 is created before 2->b->3
how to solve this using linked-list( for transitions) and String(for string input) in java. Thanks
complete code is
public boolean match() {
s = str;
current = FiniteAutomata.startState;
for (int t = 0; t < str.length(); t++) {
curFlag = false;
list2 = originalList.head;
while (list2 != null) {
if (current == list2.initialState) {
for (int i = 0; i < s.length(); i++) {
String s2 = delL(s.toCharArray(), i);
if (s2.equals(list2.transitionString)) {
current = list2.finalState;
curFlag = true;
s = delF(s.toCharArray(), i);
}
}
}
if (curFlag) {
break;
}
list2 = list2.next;
}
}
boolean flag = false;
if (s.equals("")) {
for (int i = 0; i < FiniteAutomata.finalStates.length; i++) {
if (current == FiniteAutomata.finalStates[i]) {
flag = true;
}
}
}
if (flag) {
return true;
} else {
return false;
}
}
String delF(char[] temp, int c) {
String s = "";
for (int i = c + 1; i < temp.length; i++) {
s = s + temp[i];
}
return s;
}
String delL(char[] temp, int c) {
String s = "";
for (int i = 0; i <= c; i++) {
s = s + temp[i];
}
return s;
}