3

I am practicing my DFA and I came across this question that seems to be very complex. I tried splitting it up into two smaller questions and I have an answer but it doesn't look right to me.

Can someone help me or at least give me some tips.

They are all accepting states btw. enter image description here

Another possibility that I can think of is that you only have the accepting states every two O's or 1's since they need to be a pair. So for example 11 accepting 11 accepting 11 accepting.

Gana
  • 75
  • 1
  • 7

2 Answers2

3

You're describing your state machine as a huge picture, but you can make your problem much easier by naming your states with structured names rather than trying to draw a huge diagram.

Let your states be (n, m, s) where n is the number of 00s you've seen, m is the number of 11s you've seen and s is the previous character read (s='', '1', '0') (where '' means you haven't seen a previous character, or you've just found a '00' or '11').

Then your transitions are:

(n, m, '')  -0-> (n, m, '0')
(n, m, '')  -1-> (n, m, '1')
(n, m, '0') -0-> (n+1, m, '')
(n, m, '0') -1-> (n, m, '1')
(n, m, '1') -0-> (n, m, '0')
(n, m, '1') -1-> (n, m+1, '')

All states with n<=2 and m<=3 are accepting. The start state is (0, 0, '').

This isn't finite, but you can make it so by merging all non-accepting states into a single state. It'll have (3 * 4 * 3 + 1) = 37 states, which is minimal.

Counting overlapping 00 and 11.

The answer above assumes that '000' contains one '00' rather than 2 '00's (that is the number of '00' is the maximal number of non-overlapping '00' in the string, and the same for '11'). If you want to count '000' as 2, you need this machine:

States are S (start) or (n, m, s) where n, m are as before, and s is '0' or '1'. Then:

S -0-> (0, 0, '0')
S -1-> (0, 0, '1')
(n, m, '0') -0-> (n+1, m, '0')
(n, m, '0') -1-> (n, m, '1')
(n, m, '1') -0-> (n, m, '0')
(n, m, '1') -1-> (n, m+1, '1')

All states are accepting except those with n>2 or m>3. Again, we merge all non-accepting states into a single state.

This has (3 * 4 * 2 + 2) = 26 states, which again, is minimal.

Generating a FSM diagram.

Given the formal description, one can write a program to generate a DOT file that describes the machine. Here's the program to generate the diagram for the machine in the first part of the answer. (Note, it doesn't show which states are accepting, but they all are except FAIL).

def state(n, m, s):
    if n > 2 or m > 3: return 'FAIL'
    return "n%s_%s_%s" % (n, m, s)

def T(st, c):
    n, m, s = st
    if s == '':
        return (n, m, c)
    if s == '0':
        return (n+1, m, '') if c=='0' else (n, m, c)
    if s == '1':
        return (n, m+1, '') if c=='1' else (n, m, c)

print 'digraph {'
for n in xrange(3):
    for m in xrange(4):
        for s in ['', '0', '1']:
            for c in '01':
                print '    %s -> %s [label="%s"]' % (state(n, m, s), state(*T((n, m, s), c)), c)

print '}'

Here's the result from piping the output through dot -Tpng: FSM

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
  • Thanks a lot Paul! This is pretty much what I had as well( again assuming 00 0 is one 00). For some reason, I thought that (0,1,1) means that since 1 is the last read character, it was also the transition into 1. Rather than having a separate 0 and 1 for the transition. This makes is so much clearer for me. Thanks once again. – Gana Jan 11 '16 at 01:33
2

Well, instead of drawing the automaton you may describe it using words.

How would I do that for your problem: Let a vertex be a triple where

  • first element is 0, 1, 2, 3 and "at least 4" showing the number of 11s.
  • second element is 0, 1, 2 and "at least 3" depending on number of "00"s
  • third element is the last symbol (0, 1 or "string is empty")

It's pretty easy to define the transitions, start states and finish states.

So, there's 5 * 4 * 3 = 60 states

You may notice that there are some not achievable states and some states that may be united to "failed states" that can reduce the size of automaton significantly

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • Can you expand on this? What exactly do you mean by let a vertex be a triple? Do you mean like this : (x,x, 0) ? I'm not sure if I understand. – Gana Jan 10 '16 at 22:57
  • Well, yes like (0, "at least 3", 1) means that it was no 11 yet, there was at least 3 "00s" and last symbol is 1 – RiaD Jan 10 '16 at 23:02
  • OK so for in this case (0, at least 3, 1) corresponds to 0 000 1 ? So essentially it is two 00's and one 1. So in that case, why is your first condition "first element is 0 if there was no 11 yet, else 1" ? Let's say there was 11, why can't the first element be 1 as well ? I mean the question says that at most three 11's (i.e 11 11 11) – Gana Jan 10 '16 at 23:10
  • I think this is definitely going in the right step. For the second element, can I just say something along the lines of "at most 3 0's" ? – Gana Jan 10 '16 at 23:17
  • Could you give a few example of the triple vertices ? – Gana Jan 10 '16 at 23:25
  • yes, 00001 is example of string that correspond to this vertex as well as 01001001001001001, "I didn't get question about "At most 3 0's" and as for examples: `(0, 0, "string empty")`, `(1, 2, 0)` – RiaD Jan 10 '16 at 23:30
  • you may choose any element from first group for the first place, any of the second group for second place and same for third. Not all of them would be achiveable but that's not important – RiaD Jan 10 '16 at 23:31
  • oops I didn't read carefully the statement I'm revising states but the idea is the same: you are counting up to how many you need – RiaD Jan 10 '16 at 23:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100321/discussion-between-gana-and-riad). – Gana Jan 10 '16 at 23:34
  • I wrote my answer without reading yours, but we've solved the problem identically. I'll leave my answer because it contains a solution to a different interpretation of the question (on how to count 000 and 111), and generation of dot files, but I upvoted this. – Paul Hankin Jan 11 '16 at 03:19