0

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;
}
Kamran Akram
  • 95
  • 1
  • 10
  • This automaton doesn't look like deterministic. – mkrakhin Feb 16 '15 at 19:57
  • What state transition are you expecting from state 2 on "b"? If you are expecting a transition to 3 why do you have the rule 2->b->a? – sprinter Feb 16 '15 at 20:28
  • 3 is final state, if it comes "b" on 2, should move to 3. but 2->b->1 is a necessary transition so that automaton accepts regular expression a(ba)*b – Kamran Akram Feb 16 '15 at 20:44
  • For this expression two states is just enough. Also, it would be simpler for you, if you will translate it to equal expression: (ab)+ – mkrakhin Feb 16 '15 at 21:01
  • i've to implement this in application. where user will input all the details, so can't apply static (ab)+ expression code to user's given inputs. mentioned is just one example where my algorithm fails. – Kamran Akram Feb 16 '15 at 22:25
  • total states, start state, final state and transitions will be set from users input. if user inputs this type of data then how to handle these transitions? – Kamran Akram Feb 16 '15 at 22:29
  • problem will be more understandable after complete code added to question. thanks – Kamran Akram Feb 16 '15 at 23:03

0 Answers0