6

I'm having difficulty with a seemingly simple question. "Is it possible to have a finite state machine that detects if a bit string of arbitrary length has the same number of zeros as ones? If so what would it look like"? In trying to solve this problem I have come up with a variety of finite state machines. However, none have been able to solve the problem for a bit string of arbitrary length.

Any suggestions? Thank you!

Tommy
  • 89

2 Answers2

6

The number of $1$'s in the first $n$ bits of input may be any integer from $0$ to $n$, and if there are at least $n$ more bits to go, in order to determine the result the FSM would need to "remember" which of those integers it is. Thus at least $n+1$ states are needed for a FSM that can do this with $2n$-bit strings. Since the size of the input string is arbitrary, no such FSM is possible.

Robert Israel
  • 470,583
5

This is clearly possible with Turing machines, so let me assume that you are using finite state automata.

In this case, it is not possible. The reason is the pumping lemma. Consider the strings $0^n1^n$, where $n$ is larger than the number of states in the machine. This string must be accepted by the machine, and so it can be pumped. But by the pumping lemma, the effect of pumping will be to insert extra $0$s into the string, making it unbalanced with respect to the criterion, but still accepted by the machine. So the machine does not work as suggested. So there is no finite state automata accepting all and only the balanced strings.

JDH
  • 45,373