DFA over language {0,1} - automata

I'm trying to satisfy the following requirements (homework)
Construct both regular expression and deterministic automatons that
accept the following languages over {0,1}.
(a) Strings that do not contain 00.
(b) Strings that contain at least three symbols.
(c) Strings where each 0 is directly followed by 1.
(d) Strings that both start and end with 11.
(e) Strings having an odd number of 0:s and an odd number of 1:s.
I've done the following so far but it feels wrong, anyone have any suggestions on what I can do better? I'm very new to this and don't even know if what I've done fills the requirements.

I find making regular expressions is harder than DFAs, so I recommend making the DFAs first and then getting regular expressions after.
(a) We can make a DFA that detects the substring 00 and transitions to a dead state.
q s q'
-- -- --
q0 0 q1 q0 is the initial state
q0 1 q0 q0 and q1 are accepting
q1 0 q2 q2 is a dead state
q1 1 q0
q2 0 q2
q2 1 q2
To get the regular expression, we iteratively find for each state a regular expression for strings leading to that state. Then, we take the union of all regular expressions for accepting states.
iteration 1
q0: e + (q0)1 + (q1)1
q1: (q0)0
q2: (q1)0 + (q2)0 + (q2)1
iteration 2
q0: e + (q0)1 + (q0)01
q1: (q0)0
q2: (q0)00 + (q2)0 + (q2)1
iteration 3
q0: e+(1+01)*
q1: (e+(1+01)*)0
q2: (e+(1+01)*)00(0+1)*
Because q0 and q1 are accepting states, the regular expression is
e+(1+01)*+(e+(1+01)*)0
= (1+01)*+(1+01)*0 // e + r* = r*
= (1+01)*e+(1+01)*0 // r = re
= (1+01)(e+0) // distributive law of concatenation
(b) A DFA here is:
q s s'
-- -- --
q0 0 q1
q0 1 q1 q0 is the initial state
q1 0 q2 q3 is the accepting state
q1 1 q2
q2 0 q3
q2 1 q3
q3 0 q3
q3 1 q3
Same exercise:
iteration 1
q0: e
q1: (q0)0 + (q0)1
q2: (q1)0 + (q1)1
q3: (q2)0 + (q2)1
iterations 2-3 (combined)
q0: e
q1: e0 + e1
q2: (0+1)0 + (0+1)1
q3: (q2)0 + (q2)1 + (q3)0 + (q3)1
iteration 4
q0: e
q1: e0 + e1
q2: (0+1)0 + (0+1)1
q3: ((0+1)0 + (0+1)1)(0+1)(0+1)*
Since q4 is the only accepting state, the answer is just
((0+1)0 + (0+1)1)(0+1)(0+1)*
= (00+10+01+11)(0+1)(0+1)*
(c) This is very similar to (a) except that we throw out strings ending in 0. That is, q1 is not accepting. So, the DFA is the same as in (a), with q1 not accepting, and the regular expression is therefore just e+(1+01)* = (1+01)*.
(d) A DFA:
q s q'
-- -- --
q0 0 q1
q0 1 q2 q0 is the initial state
q1 0 q1 q3 is the accepting state
q1 1 q1 q1 is the dead state
q2 0 q1 q0,q1,q2 ensure the string starts with 11
q2 1 q3 q3,q4,q5 ensure the string stops with 11
q3 0 q4
q3 1 q3
q4 0 q4
q4 1 q5
q5 0 q4
q5 1 q3
We can certainly go through the same exercise as above but a regular expression is actually easy to make here: 11+111+11(0+1)*11.
(e) A DFA:
q s q'
-- -- --
q0 0 q1
q0 1 q2 q0 is the initial state
q1 0 q0 q3 is the accepting state
q1 1 q3 q0: #0 and #1 are even
q2 0 q3 q1: #0 is odd, #1 is even
q2 1 q0 q2: #0 is even, #1 is odd
q3 0 q2 q3: #0 is odd, #1 is odd
q3 1 q1
The regular expression here is difficult and left as an exercise. You can go through the iterations as in the earlier examples; just remember the rule is:
qx: r + (qx)s ---> qx: (r)(s*)
Then just eliminate one symbol at a time until you have a regular expression for (q3) with no state placeholders. Good luck.

Related

Writing the production rules of this finite state machine

Consider the following state diagram which accepts the alphabet {0,1} and accepts if the input string has two consecutive 0's or 1's:
01001 --> Accept
101 --> Reject
How would I write the production rules to show this? Is it just:
D -> C0 | B1 | D0 | D1
C -> A0 | B0
B -> A1 | C1
And if so, how would the terminals (0,1) be differentiated from the states (A,B,C) ? And should the state go before or after the input? That is, should it be A1 or 1A for example?
The grammar you suggest has no A: it's not a non-terminal because it has no production rules, and it's not a terminal because it's not present in the input. You could make that work by writing, for example, C → 0 | B 0, but a more general solution is to make A into a non-terminal using an ε-rule: A → ε and then
C → A 0 | B 0.
B0 is misleading, because it looks like a single thing. But it's two grammatical symbols, a non-terminal (B) and a terminal 0.
With those modifications, your grammar is fine. It's a left linear grammar; a right linear grammar can also be constructed from the FSA by considering in-transitions rather than out-transitions. In this version, the epsilon production corresponds to final states rather than initial states.
A → 1 B | 0 C
B → 0 C | 1 D
C → 1 B | 0 D
D → 0 D | 1 D | ε
If it's not obvious why the FSM corresponds to these two grammars, it's probably worth grabbing a pad of paper and constructing a derivation with each grammar for a few sample sentences. Compare the derivations you produce with the progress through the FSM for the same input.

wxMaxima ezunits funny business

Is the handling of the units broken or what am I missing?
load(ezunits);
σ_N: 10000`N/(50`mm*10`mm);
newts: 123`kg*m/s^3; newts `` N; newts + 321 `kg*m/s^2;
produces not what one would have hoped for:
(%i1) load(ezunits);
(%o1) "C:/maxima-5.43.2/share/maxima/5.43.2/share/ezunits/ezunits.mac"
(%i2) σ_N: 10000`N/(50`mm*10`mm);
(σ_N) 10000 ` (N/500 ` 1/mm^2)
(%i5) newts: 123`kg*m/s^3; newts `` N; newts + 321 `kg*m/s^2;
(newts) 123 ` (kg*m)/s^3
(%o4) 123/s ` N
(%o5) 321 ` (kg*m)/s^2+123 ` (kg*m)/s^3
Should be:
σ_N= 20 N/mm^2
newts= 123 N/s
For the first part, you have to use parentheses to indicate the grouping you want. When you write a ` b/c, it is interpreted as a ` (b/c), but in this case you want (a ` b)/c. (Grouping works that way because it's assumed that stuff like x ` m/s is more common than (x ` m)/s.)
(%i2) σ_N: (10000`N)/(50`mm*10`mm);
N
(%o2) 20 ` ---
2
mm
Just for fun, let's check the dimensions of this quantity. I guess it should be force/area.
(%i3) dimensions (%);
mass
(%o3) ------------
2
length time
(%i4) dimensions (N);
length mass
(%o4) -----------
2
time
(%i5) dimensions (mm);
(%o5) length
Looks right to me.
For the second part, I don't understand what you're trying to so. The variable newts has units equivalent to N/s, so I don't understand why you're trying to convert it to N, and I don't understand why you're trying to add N/s to N. Anyway here's what I can make of it.
(%i6) newts: 123`kg*m/s^3;
kg m
(%o6) 123 ` ----
3
s
(%i7) newts `` N/s;
N
(%o7) 123 ` -
s
When quantities with different dimensions are added, ezunits just lets it stand; it doesn't produce an error or anything.
(%i8) newts + 321 ` kg*m/s^2;
kg m kg m
(%o8) 321 ` ---- + 123 ` ----
2 3
s s
The motivation for that is that it allows for stuff like 3`sheep + 2`horse or x`hour + y`dollar-- the conversion rate can be determined after the fact. In general, allowing for expressions to be reinterpreted after the fact is, I believe, the mathematical attitude.

Minimum number of states in DFA

Minimum number states in the DFA accepting strings (base 3 i.e,, ternary form) congruent to 5 modulo 6?
I have tried but couldn't do it.
At first sight, It seems to have 6 states but then it can be minimised further.
Let's first see the state transition table:
Here, the states q0, q1, q2,...., q5 corresponds to the states with modulo 0,1,2,..., 5 respectively when divided by 6. q0 is our initial state and since we need modulo 5 therefore our final state will be q5
Few observations drawn from above state transition table:
states q0, q2 and q4 are exactly same
states q1, q3 and q5 are exactly same
The states which make transitions to the same states on the same inputs can be merged into a single state.
Note: Final and Non-final states can never be merged.
Therefore, we can merge q0, q2, q4 together and q1, q3 together leaving the state q5 aloof from collation.
The final Minimal DFA has 3 states as shown below:
Let's look at a few strings in the language:
12 = 1*3 + 2 = 5 ~ 5 (mod 6)
102 = 1*9 + 0*3 + 2 = 11 ~ 5 (mod 6)
122 = 1*9 + 2*3 + 2 = 17 ~ 5 (mod 6)
212 = 2*9 + 1*3 + 2 = 23 ~ 5 (mod 6)
1002 = 1*18 + 0*9 + 0*9 + 2 = 29 ~ 5 (mod 6)
We notice that all the strings end in 2. This makes sense since 6 is a multiple of 3 and the only way to get 5 from a multiple of 3 is to add 2. Based on this, we can try to solve the problem of strings congruent to 3 modulo 6:
10 = 3
100 = 9
120 = 15
210 = 21
1000 = 27
There's not a real pattern emerging, but consider this: every base-3 number ending in 0 is definitely divisible by 3. The ones that are even are also divisible by 6; so the odd numbers whose base-3 representation ends in 0 must be congruent to 3 mod 6. Because all the powers of 3 are odd, we know we have an odd number if the number of 1s in the string is odd.
So, our conditions are:
the string begins with a 1;
the string has an odd number of 1s;
the string ends with 2;
the string can contain any number of 2s and 0s.
To get the minimum number of states in such a DFA, we can use the Myhill-Nerode theorem beginning with the empty string:
the empty string can be followed by any string in the language. Call its equivalence class [e]
the string 0 cannot be followed by anything since valid base-3 representations don't have leading 0s. Call its equivalence class [0].
the string 1 must be followed with stuff that has an even number of 1s in it ending with a 2. Call its equivalence class [1].
the string 2 can be followed by anything in the language. Indeed, you can verify that putting a 2 at the front of any string in the language gives another string in the language. However, it can also be followed by strings beginning with 0. Therefore, its class is new: [2].
the string 00 can't be followed by anything to fix it; its class is the same as its prefix 0, [0]. same for the string 01.
the string 10 can be followed by any string with an even number of 1s that ends in a 2; it is therefore equivalent to the class [1].
the string 11 can be followed by any string in the language whatever; indeed, you can verify prepending 11 in front of any string in the language gives another solution. However, it can also be followed by strings beginning with 0. Therefore, its class is the same as [2].
12 can be followed by a string with an even number of 1s ending in 2, as well as by the empty string (since 12 is in fact in the language). This is a new class, [12].
21 is equivalent to 1; class [1]
22 is equivalent to 2; class [2]
20 is equivalent to 2; class [2]
120 is indistinguishable from 1; its class is [1].
121 is indistinguishable from [2].
122 is indistinguishable from [12].
We have seen no new equivalence classes on new strings of length 3; so, we know we have seen all the equivalence classes. They are the following:
[e]: any string in the language can follow this
[0]: no string can follow this
[1]: a string with an even number of 1s ending in 2 can follow this
[2]: same as [e] but also strings beginning with 0
[12]: same as [1] but also the empty string
This means that a minimal DFA for our language has five states. Here is the DFA:
[0]
^
|
0
|
----->[e]--2-->[2]<-\
| ^ |
| | |
1 __1__/ /
| / /
| | 1
V V |
[1]--2-->[12]
^ |
| |
\___0___/
(transitions not pictured are self-loops on the respective states).
Note: I expected this DFA to have 6 states, as Welbog pointed out in the other answer, so I might have missed an equivalence class. However, the DFA seems right after checking a few examples and thinking about what it's doing: you can only get to accepting state [12] by seeing a 2 as the last symbol (definitely necessary) and you can only get to state [12] from state [1] and you must have seen an odd number of 1s to get to [1]…
The minimum number of states for almost all modulus problems is the base of the modulus. The general strategy is one state for every modulus, as transitions between moduli are independent of what the previous numbers were. For example, if you're in state r4 (representing x = 4 (mod 6)), and you encounter a 1 as your next input, your new modulus is 4x6+1 = 25 = 1 (mod 6), so the transition from r4 on input 1 is to r1. You'll find that the start state and r0 can be merged, for a total of 6 states.

Design DFA accepting decimal strings divisible by 7

I'm a student studying DFAs looking for a DFA that could find if a decimal number is divisible by 7.
today I've solved divisibility problem for numbers 2,3,4,5,6,8,9 but I can't solve this problem for number 7. I've searched the web but I couldn't find any answer helping me or being understandable for me.
so now I'm here looking for help. thanks in advance.
The basic idea is that we will keep track of the current value, modulo seven, of the number we've seen so far. Each new digit takes the old number, multiplies by ten, and adds the new digit. Therefore, from the state corresponding to x (mod 7), adding digit d to the right means we go to the state corresponding to 10x + d (mod 7). This DFA has 70 states (the number of digits 0-9 times the number of remainders after division by seven 0-6).
q s q'
------------
q0 0 q0
q0 1 q1
q0 … …
q0 6 q6
q1 0 q3
q1 1 q4
q1 … …
q1 6 q2
…
q6 0 q4
q6 1 q5
q6 … …
q6 6 q3
Consider the processing of the number 36736:
(q0) --3--> (q3) --6--> (q1) --7--> (q3) --3--> (q5) --6--> (q0)
0 0*10+3 3*10+6 1*10+7 3*10+3 5*10+6
0+3 30+6 10+7 30+3 50+6
3 36 17 33 56
3 1 3 5 0
This number is divisible by seven because we end up in state q0, the state corresponding to zero modulo seven - meaning an even multiple of seven.
I think this will be helpful you can check this DFA by reminder(for ex. Binary 1001 = Decimal 9 and 9 mod 7 = 2 so our string 1001 should be end at q2.Check Image for DFA Design

Reducing a DFA using the Pair Table method

I'm learning about reducing DFA's using the Pair Table Method (Systematic Reduction Method).Here is the DFA we are looking to reduce.
The first step is to lay out the DFA in a table:
0 1
q0 {q0, q3} {q1}
q1 {q2} {q2}
q2 EmptySet {q2}
{q0, q1} {q0, q1, q3} {q1, q2}
{q1, q2} {q2} {q2}
{q0, q1, q2} {q0, q1, q2} {q1, q2}
We don't need to include the empty set state, I think.
Now here is where I'm confused, I need to go through the list of states and mark them based on something. I'm not sure how to proceed.
First of all, the table you have is not a match with the DFA.
The pair table method uses the classes of equivalency. First we partition the states in two: final and non-final states. Initially every final state is distinguishable from any non-final state. So you should create a table like below and mark (q0,q1) and (q1,q2) as q1 is final but q0 and q2 are non-final states.
+----+
| q0 |
+----+----+
| q1 | x |
+----+----+----+
| q2 | | x |
+----+----+----+----+
| q0 | q1 | q2 |
+----+----+----+
Then iteratively, you continue marking using the following rule and stop when in an iteration nothing is changed:
Mark (q_i,q_j) if for some alphabet symbol a, is marked. Int above table, and . As (q1,q2) is marked, we should mark (q0,q2) as well. Note that we only have half of the table. Hence, (q0,q2)=(q2,q0).
+----+
| q0 |
+----+----+
| q1 | x |
+----+----+----+
| q2 | x | x |
+----+----+----+----+
| q0 | q1 | q2 |
+----+----+----+

Resources