Design DFA accepting decimal strings divisible by 7 - automata

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

Related

How to manipulate multiple nested arrays in Dyalog APL?

I have been given matrices filled with alphanumerical values excluding lower case letters like so:
XX11X1X
XX88X8X
Y000YYY
ZZZZ789
ABABABC
and have been tasked with counting the repetitions in each row and then tallying up a score depending on the ranking of the character being repeated. I used {⍺ (≢⍵)}⌸¨ ↓ m to help me. For the example above I would get something like this:
X 4 X 4 Y 4 Z 4 A 3
1 3 8 3 0 3 7 1 B 3
8 1 C 1
9 1
This is great but now I need to do a function that would be able to multiply the numbers with each letter. I can access the first matrix with ⊃ but then I am completely lost on how to access the other ones. I can simply write ⊃w[2] and ⊃w[3] and so forth but I need a way to change every matrix at the same time in one function. For this example, the array of the ranking is as follow: ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210 so for the first array XX11X1X
which corresponds to:
X 4
1 3
So the X is 3rd in the array so it corresponds to a 3 and 1 is 35th so it's a 35. The final scoring would be something like (3×104)+(35×103). My biggest problem is not necessarily the scoring part but being able to access each matrix individually in one function. So for this nested array:
X 4 X 4 Y 4 Z 4 A 3
1 3 8 3 0 3 7 1 B 3
8 1 C 1
9 1
if I do arr[1] it gives me the scalar
X 4
1 3
and ⍴ arr[1] gives me nothing confirming it so I can do ⊃arr[1] to get the matrix itself and have access to each column individually. This is where I'm stuck. I'm trying to write a function to be able to do the math for each matrix and then saving those results to an array. I can easily do the math for the first matrix but I can't do it for all of them. I might have made a mistake by making using {⍺ (≢⍵)}⌸¨ ↓ m to get those matrices. Thanks.
Using your example arrangement:
⎕ ← arranged ← ⌽ ⎕D , ⎕A
ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210
So now, we can get the index values:
1 ⌷ m
XX11X1X
∪ 1 ⌷ m
X1
arranged ⍳ ∪ 1 ⌷ m
3 35
While you could compute the intermediary step first, it is much simpler to include most of the final formula in in Key's operand:
{ ( arranged ⍳ ⍺ ) × 10 * ≢⍵ }⌸¨ ↓m
┌───────────┬───────────┬───────────┬─────────────────┬───────────────┐
│30000 35000│30000 28000│20000 36000│10000 290 280 270│26000 25000 240│
└───────────┴───────────┴───────────┴─────────────────┴───────────────┘
Now we just need to sum each:
+/¨ { ( arranged ⍳ ⍺ ) × 10 * ≢⍵ }⌸¨ ↓m
65000 58000 56000 10840 51240
In fact, we can combine the summation with the application of Key to avoid a double loop:
{ +/ { ( arranged ⍳ ⍺ ) × 10 * ≢⍵ }⌸ ⍵}¨ ↓m
65000 58000 56000 10840 51240
For completeness, here is a way to use the intermediary result. Let's start by working on just the first matrix (you can get the second one with 2⊃ instead of ⊃ ― for details, see Problems when trying to use arrays in APL. What have I missed?):
⊃{⍺ (≢⍵)}⌸¨ ↓m
X 4
1 3
We can insert a function between the left column elements and the right column elements with reduction:
{⍺ 'foo' ⍵}/ ⊃{⍺ (≢⍵)}⌸¨ ↓m
┌─────────┬─────────┐
│┌─┬───┬─┐│┌─┬───┬─┐│
││X│foo│4│││1│foo│3││
│└─┴───┴─┘│└─┴───┴─┘│
└─────────┴─────────┘
So now we simply have to modify the placeholder function with one that looks up the left argument in the arranged items, and multiplies by ten to the power of the right argument:
{ ( arranged ⍳ ⍺ ) × 10 * ⍵ }/ ⊃{⍺ (≢⍵)}⌸¨ ↓m
30000 35000
Instead of applying this to only the first matrix, we apply it to each matrix:
{ ( arranged ⍳ ⍺ ) × 10 * ⍵ }/¨ {⍺ (≢⍵)}⌸¨ ↓m
┌───────────┬───────────┬───────────┬─────────────────┬───────────────┐
│30000 35000│30000 28000│20000 36000│10000 290 280 270│26000 25000 240│
└───────────┴───────────┴───────────┴─────────────────┴───────────────┘
Now we just need to sum each:
+/¨ { ( arranged ⍳ ⍺ ) × 10 * ⍵ }/¨ {⍺ (≢⍵)}⌸¨ ↓m
65000 58000 56000 10840 51240
However, this is a much more circuitous approach, and is only provided here for reference.

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.

When re-inserting into queue - Huffman Code

Example
3 2 5 5
a b c d
Joining first two
5 | 5 5
3 2 | c d
a b |
I have to put the new tree of five into the queue
Am I obligated to put it in the end like this:
5 5 5
c d / \
3 2
a b
Or can I put it in the beginning:
5 5 5
3 2 c d
a b
Or even in the middle of 'c' and 'd'
Is it my choice or is there a rule?
It's not your choice, the Queue needs to be sorted at all times (by it's number of occurrences and in case of equal number of occurrences by the depth of the tree). So it needs to be inserted where it belongs into the order.
This is needed to pick the sub-trees with the least amount of occurrences and if there is choice the most shallow one of them by simply pop-ing them.
If you simply resort after every insertion (this is inefficient and should not be done) the position obviously doesn't matter.
Yes, it's your choice. Whichever way you will get an optimal Huffman code, even though two resulting codes can be manifestly different.
You can get:
a - 00
b - 01
c - 10
d - 11
or you can get:
a - 111
b - 110
c - 10
d - 0
Now if I multiply the number of bits in each symbol times the number of occurrences, I get for the first code: 2*3 + 2*2 + 2*5 + 2*5 = 30 bits. For the second code: 3*3 + 3*2 + 2*5 + 1*5 = 30 bits. So both codes will code the original message to exactly 30 bits.

DFA over language {0,1}

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.

DFA of Binary numbers decimal equivalent divisible by some.number n

Hello we may construct DFA for binary equivalent decimal divisible by 3 for eg if we read a string 001 then first we read 0 and for next 0 we will shift towards left so it's 00 next if we read 1 00 will be left shifted and place 1 in 2^0 position so it is 1*(2^0) + 0*(2^1) +0*(2^2).....we read this string as 001 but what i want is I want a DFA for which reading a binary string is divisible by some n for eg if we want 3 for 3 like that......show be binary string divisible by 3 buy reading 001 have no left shift but places every bit in increasing powers......I.e reading 001 should be read as 0 in first position and second 0in second position &1 in third position....I.e 001 is read as 0*(2^0) + 0*(2^1) + 1*(2^1)..so it is 100 if we read from left .....but if we read it from right it is 001 I want DFA for this machine reading string from right to left

Resources