Search code examples
fsm

about states of finite state machine


the question is let sigma =(1,2,3,$). I need to draw its diagram which outputs 1 when sum is >_5. if it is over 5, the amount will be carried over.

Im wondering what would be the state in this case. Can I let A = ive seen 1 B=ive seen 2, C=ive see 3 and D = accepted state?


Solution

  • No, you cannot use just three states. Your FSM must, somehow, keep counting the current sum of the input values so far. The problem is that a FSM has no memory (that would be another kind of automata) so it has to be one state for every possible combination of numbers that evaluate to all possible sums.

    That is: you need a state that means ("the current sum is 0").Let that state be state 0. That would be your initial state. Other states would be "the current sum is 1" (state 1), "the current sum is 2" (state 2), .... , "the current sum is 5" (state 5), "the current sum is 6" (state 6), etc (no, you won't need an inifinite number of states, as for example, from state 5, a transition with value 1 leads you again to the state 1.

    From state 0, a transition with 1 leads you to state 1. Transition with 2, to state 2 and transition with 3 to state 3. Easy, isn't it?

    From state 3, for example, a transition with 1 leads you to transition 4, Transition with 2 to state 5, which is an accept state, and transition with 3 to state 6, which is also an accepted state.

    To put another example: from state 6, a transition with 1 leads you to... state 2. That's right: state 6 means that the current sum is 6, which exceeds 5 in 1. So this is like transition 1, but it is an accepted state, so transitions from state 6 go to the same places as transitions from state 1 would go. This will help you building your FSM.

    In fact, the larger state number is determined by the largest value you can sum with 1,2 and 3 that overflows 5 for the first time. That would be 1+1+1+1+3 = 7. So you need to define from state 0 to state 7, and of course, a final state when $ is detected.