DFA design accepting binary strings divisible by n,

I need to learn how to design DFA so that for any number n it takes binary strings {0, 1} whose decimal equivalent number is divisible by n.

There will be different DFAs for different 'n', but can anyone give a basic approach that I have to follow in order to go to any number 0 <n <10.

+61
regex dfa automata
Feb 20 '14 at 3:35
source share
4 answers

Below I wrote an answer for n equal to 5, but you can use the same approach for drawing DFA for any value of n and "any positional number system", for example binary, triple ...

First leave the term β€œFull DFA”, the DFA defined in the full domain in the format Ξ΄: Q Γ— Ξ£ β†’ Q is called β€œFull DFA”. In other words, we can say; in the transition diagram of the full DFA there is no missing edge (for example, from each state in Q there is one outgoing edge present for each language symbol in Ξ£). Note: Once we define a partial DFA as Ξ΄ βŠ† Q Γ— Ξ£ β†’ Q (Read: How β€œΞ΄: Q Γ— Ξ£ β†’ Q” is read in the definition of DFA ).

A DFA construct that takes binary numbers divisible by the number "n":

Step 1 When you divide the number Ο‰ by n , then the reminder can be either 0, 1, ..., (n - 2) or (n - 1). If the remainder 0 means that Ο‰ is divisible by n otherwise. Thus, in my DFA there will be a state q r , which will correspond to the residual value of r , where 0 <= r <= (n - 1) , and the total number of states in the DFA is n .
After processing the number string Ο‰ over Ξ£, the final state q r means that Ο‰% n => r (% reminder operator).

In any state machine, state assignment is similar to a memory element. The state in the atom stores some information, such as a fan switch, that can determine if the fan is in the off state or in the 'on' state. For n = 5, five states in the DFA correspond to five reminders as follows:

  • The state q 0 is reached if the reminder is 0. The state q 0 is the final state (receiving state). This is also an initial condition.
  • State q 1 reaches, if the reminder is 1, an undefined state.
  • State q 2 , if the reminder is 2, the state is not final.
  • State q 3 , if the reminder is 3, the state is not final.
  • State q 4 , if the reminder is 4, the state is not final.

Using the above information, we can begin to draw a transition diagram of five states as follows:

fig-1
Rice-1

So, 5 states for 5 residual values. After processing the string Ο‰, if the final state becomes q 0 , this means that the decimal equivalent of the input string is divided by 5. In the above figure, q 0 marks the final state as two concentric circles.
In addition, I defined the transition rule Ξ΄: (q 0 , 0) β†’ q 0 as a self cycle for the character '0' in the state q 0 , this is because the decimal equivalent of any string consists only of '0' is 0, and 0 is divisible by n .

Step 2 : TD above incomplete; and can only process strings '0' s. Now add a few more edges so that it can handle subsequent lines of numbers. The table below shows the new transition rules that you can add as the next step:

 β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ Number β”‚ Binary β”‚ Remainder (% 5) β”‚ End-state β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚One β”‚1 β”‚1 β”‚q 1 β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚Two β”‚10 β”‚2 β”‚q 2 β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚Three β”‚11 β”‚3 β”‚q 3 β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚Four β”‚100 β”‚4 β”‚q 4 β”‚
 └──────┴──────────────────────────────
  • To process the binary string '1' , there must be a transition rule Ξ΄: (q 0 , 1) β†’ q 1
  • Two: - the binary representation is '10' , the final state should be q 2 , and for processing '10' we just need to add another transition rule Ξ΄: (q 1 , 0) β†’ q 2
    Way : β†’ (q 0 ) ─1 β†’ (q 1 ) ─0 β†’ (q 2 )
  • Three: - in binary format, this is '11' , the final state is q 3 , and we need to add the transition rule Ξ΄: (q 1 , 1) β†’ d <yug> 3sub>
    Way : β†’ (q 0 ) ─1 β†’ (q 1 ) ─1 β†’ (q 3 )
  • Four: - in binary '100' , the final state is q 4 . TD already processes the prefix line '10' , and we just need to add a new transition rule: (q 2 , 0) β†’ q 4
    Way : β†’ (q 0 ) ─1 β†’ (q 1 ) ─0 β†’ (q 2 ) ─ 0 β†’ (q 4 )

fig-2 Rice-2

Step 3 : Five = 101
Above the transition diagram in Figure-2, it is still incomplete and there are many missing edges; for example, the transition for Ξ΄ is not defined: (q 2 , 1) - ? . And a rule must be present to handle strings such as '101' .
Since '101' = 5 is divisible by 5 and takes '101' , I will add Ξ΄: (q 2 , 1) β†’ q 0 in the above figure-2.
Path: β†’ (q 0 ) ─1 β†’ (d <sub> 1sub>) ─0 β†’ (d <sub> 2sub>) ─1 β†’ (d <sub> 0sub>)
with this new rule, the transition diagram becomes the following:

fig-3 Rice 3

Below at each step, I select the next subsequent binary number to add the missing edge until I get the TD as "full DFA".

Step 4 : Six = 110.

We can process '11' in the current TD in Figure 3 as: β†’ (q 0 ) ─11 β†’ (q 3 ) ─0 β†’ ( ) Since 6% 5 = 1, this means adding one rule: (q 3 , 0) β†’ q 1 .

fig-4 Figure 4

Step 5 : Seven = 111

 β”Œ ─ ─ ─ ─ ┬ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ ─┬───────────┐
 β”‚ Number β”‚ Binary β”‚ Remainder (% 5) β”‚ End-state β”‚ Path β”‚ Add β”‚
 β”œ ─ ─ ─ ─ β”Ό β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ ─┼────────────
 β”‚Seven β”‚111 β”‚7% 5 = 2 β”‚q 2 β”‚ q 0 ─11 ​​→ q 3 β”‚ q 3 ─1 β†’ q 2 β”‚
 β”” ─ ─ ─ ─ β”΄ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

fig-5 Figure 5

Step 6 : Eight = 1000

 β”Œ ─ ─ ─ ─ ┬ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”Œ ─────────┐
 β”‚ Number β”‚ Binary β”‚ Remainder (% 5) β”‚ End-state β”‚ Path β”‚ Add β”‚
 β”œ ─ ─ ─ ─ β”Ό β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”œ ──────────
 β”‚Eight β”‚1000 β”‚8% 5 = 3 β”‚q 3 β”‚q 0 ─100 β†’ q 4 β”‚ q 4 ─0 β†’ q 3 β”‚
 β”” ─ ─ ─ ─ β”΄ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”” β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

fig-6 Figure 6

Step-7 : Nine = 1001

 β”Œ ─ ─ ─ ─ ┬ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ ─────────┐
 β”‚ Number β”‚ Binary β”‚ Remainder (% 5) β”‚ End-state β”‚ Path β”‚ Add β”‚
 β”œ ─ ─ ─ ─ β”Ό β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ ──────────
 β”‚Nine β”‚1001 β”‚9% 5 = 4 β”‚q 4 β”‚q 0 ─100 β†’ q 4 β”‚ q 4 ─1 β†’ q 4 β”‚
 β”” ─ ─ ─ ─ β”΄ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ’ β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

fig-7 Figure 7

In TD-7, the total number of edges is 10 == Q Γ— Ξ£ = 5 Γ— 2. And this is a complete DFA that can take all possible binary strings, then the decimal equivalent is divided by 5.

DFA construct accepting ternary numbers divisible by n:

Step-1 In the same way as for binary code, use the number-1.

Step-2 Add Zero, One, Two

 β”Œ ─ ─ ─ ┬ ┬ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────┐
 β”‚ Decimal β”‚ Ternary β”‚ Remainder (% 5) β”‚ End-state β”‚ Add β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚Zero β”‚0 β”‚0 β”‚q0 β”‚ Ξ΄: (q0,0) β†’ q0 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚ One β”‚1 β”‚1 β”‚q1 β”‚ Ξ΄: (q0,1) β†’ q1 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚Two β”‚2 β”‚2 β”‚q2 β”‚ Ξ΄: (q0,2) β†’ q3 β”‚
 β”” ─ ─ ─ β”΄ β”΄ ─ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”€β”€β”€β”€β”€β”˜

fig-8
Fig-8

Step-3 Add Three, Four, Five

 β”Œ ─ ─ ─ ┬ ┬ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ────┐
 β”‚ Decimal β”‚ Ternary β”‚ Remainder (% 5) β”‚ End-state β”‚ Add β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────
 β”‚Three β”‚10 β”‚3 β”‚q3 β”‚ Ξ΄: (q1,0) β†’ q3 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─────
 β”‚Four β”‚11 β”‚4 β”‚q4 β”‚ Ξ΄: (q1,1) β†’ q4 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────
 β”‚Five β”‚12 β”‚0 β”‚q0 β”‚ Ξ΄: (q1,2) β†’ q0 β”‚
 β”” ─ ─ ─ β”΄ β”΄ ─ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other β”€β”€β”€β”€β”˜

fig-9
Rice-9

Step 4 Add Six, Seven, Eight

 β”Œ ─ ─ ─ ┬ ┬ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ────┐
 β”‚ Decimal β”‚ Ternary β”‚ Remainder (% 5) β”‚ End-state β”‚ Add β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─────
 β”‚Six β”‚20 β”‚1 β”‚q1 β”‚ Ξ΄: (q2,0) β†’ q1 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────
 β”‚ Seven β”‚21 β”‚2 β”‚q2 β”‚ Ξ΄: (q2,1) β†’ q2 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────
 β”‚Eight β”‚22 β”‚3 β”‚q3 β”‚ Ξ΄: (q2,2) β†’ q3 β”‚
 β”” ─ ─ ─ β”΄ β”΄ ─ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”€β”€β”€β”€β”˜

fig-10
Figure 10

Step 5 Add nine, ten, eleven

 β”Œ ─ ─ ─ ┬ ┬ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ────┐
 β”‚ Decimal β”‚ Ternary β”‚ Remainder (% 5) β”‚ End-state β”‚ Add β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─────
 β”‚Nine β”‚100 β”‚4 β”‚q4 β”‚ Ξ΄: (q3,0) β†’ q4 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ other ─────
 β”‚Ten β”‚101 β”‚0 β”‚q0 β”‚ Ξ΄: (q3,1) β†’ q0 β”‚
 β”œ ─ ─ ─ β”Ό β”Ό ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─────
 β”‚Eleven β”‚102 β”‚1 β”‚q1 β”‚ Ξ΄: (q3,2) β†’ q1 β”‚
 β”” ─ ─ ─ β”΄ β”΄ ─ β”΄ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”€β”€β”€β”€β”˜

fig-11
Figure 11

Step-6 Add Twelve, Thirteen, Fourteen

 β”Œβ”Œ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─────┐
 β”‚ Decimal β”‚ Ternary β”‚ Remainder (% 5) β”‚ End-state β”‚ Add β”‚
 β”œβ”œ ─ ─ ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚ Twelve β”‚110 β”‚2 β”‚q2 β”‚ Ξ΄: (q4,0) β†’ q2 β”‚
 β”œβ”œ ─ ─ ─ β”Ό ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚Thirteenβ”‚111 β”‚3 β”‚q3 β”‚ Ξ΄: (q4,1) β†’ q3 β”‚
 β”œ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ──────
 β”‚Fourteenβ”‚112 β”‚4 β”‚q4 β”‚ Ξ΄: (q4,2) β†’ q4 β”‚
 β”” ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β”€β”€β”€β”€β”€β”˜

<T411>
Rice-12

The total number of edges in the -12 transition diagram is 15 = Q Γ— Ξ£ = 5 * 3 (full DFA). And this DFA can accept all strings consisting of {0, 1, 2}, these decimal equivalents are divided by 5.
If you noticed at each step, there are three entries in the table, because at each step I add all possible outgoing edges from the state to make a full DFA (and add an edge to get the state q r for the remainder r )!

To add further, remember that combining two regular languages ​​is also regular. If you need to create a DFA that accepts binary strings, then the decimal equivalent is divisible by 3 or 5, then draw two separate DFAs divisible by 3 and 5, then combine both DFAs to build the target DFA (for 1 <= n <= 10 for you have a union of 10 DFAs).

If you are asked to draw a DFA that accepts binary strings in such a way that the decimal equivalent is divisible by 5 and 3, then you are looking for DFAs divisible by 15 (but what about 6 and 8?).

Note: DFAs drawn using this technique will minimize DFAs only when there is no common coefficient between the number n and the base, for example. in the first example, there are no 5 to 2 or 5 to 3 in the second example, so both DFAs built above are minimized DFAs. If you are interested in learning about the possible mini-states for the number n and base b , read the document: Divisibility and State Complexity .

below I added a Python script, I wrote it for fun while learning the Python library pygraphviz. I add it, I hope that it can be useful to someone in something.

DFA design for base numbers 'b' divisible by number 'n':

So we can apply the above trick to draw a DFA to recognize numeric strings in any base 'b' that are divisible by a given number 'n' . In this DFA, the total number of states will be n (for n residues), and the number of edges should be equal to 'b' * 'n' - that is, the full DFA: 'b' = the number of characters in the DFA language and 'n' = the number of states.

Using the trick above, below I wrote Python Script to draw a DFA to enter base and number . In the script, the divided_by_N function populates the DFA transition rules in the base * number steps. In each num step, I convert num to the num_s number string using the baseN() function. To avoid processing each numeric string, I used the lookup_table temporary data lookup_table . At each step, the final state for the num_s numeric string is evaluated and stored in lookup_table for use in the next step.

For the DFA transition graph, I wrote the draw_transition_graph function using the Pygraphviz library (very easy to use). To use this script, you need to install graphviz . To add colorful edges to the transition diagram, I randomly generate color codes for each function of the get_color_dict symbol.

 #!/usr/bin/env python import pygraphviz as pgv from pprint import pprint from random import choice as rchoice def baseN(n, b, syms="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"): """ converts a number `n` into base `b` string """ return ((n == 0) and syms[0]) or ( baseN(n//b, b, syms).lstrip(syms[0]) + syms[n % b]) def divided_by_N(number, base): """ constructs DFA that accepts given `base` number strings those are divisible by a given `number` """ ACCEPTING_STATE = START_STATE = '0' SYMBOL_0 = '0' dfa = { str(from_state): { str(symbol): 'to_state' for symbol in range(base) } for from_state in range(number) } dfa[START_STATE][SYMBOL_0] = ACCEPTING_STATE # `lookup_table` keeps track: 'number string' -->[dfa]--> 'end_state' lookup_table = { SYMBOL_0: ACCEPTING_STATE }.setdefault for num in range(number * base): end_state = str(num % number) num_s = baseN(num, base) before_end_state = lookup_table(num_s[:-1], START_STATE) dfa[before_end_state][num_s[-1]] = end_state lookup_table(num_s, end_state) return dfa def symcolrhexcodes(symbols): """ returns dict of color codes mapped with alphabets symbol in symbols """ return { symbol: '#'+''.join([ rchoice("8A6C2B590D1F4E37") for _ in "FFFFFF" ]) for symbol in symbols } def draw_transition_graph(dfa, filename="filename"): ACCEPTING_STATE = START_STATE = '0' colors = symcolrhexcodes(dfa[START_STATE].keys()) # draw transition graph tg = pgv.AGraph(strict=False, directed=True, decorate=True) for from_state in dfa: for symbol, to_state in dfa[from_state].iteritems(): tg.add_edge("Q%s"%from_state, "Q%s"%to_state, label=symbol, color=colors[symbol], fontcolor=colors[symbol]) # add intial edge from an invisible node! tg.add_node('null', shape='plaintext', label='start') tg.add_edge('null', "Q%s"%START_STATE,) # make end acception state as 'doublecircle' tg.get_node("Q%s"%ACCEPTING_STATE).attr['shape'] = 'doublecircle' tg.draw(filename, prog='circo') tg.close() def print_transition_table(dfa): print("DFA accepting number string in base '%(base)s' " "those are divisible by '%(number)s':" % { 'base': len(dfa['0']), 'number': len(dfa),}) pprint(dfa) if __name__ == "__main__": number = input ("Enter NUMBER: ") base = input ("Enter BASE of number system: ") dfa = divided_by_N(number, base) print_transition_table(dfa) draw_transition_graph(dfa) 

Run it:

 ~/study/divide-5/script$ python script.py Enter NUMBER: 5 Enter BASE of number system: 4 DFA accepting number string in base '4' those are divisible by '5': {'0': {'0': '0', '1': '1', '2': '2', '3': '3'}, '1': {'0': '4', '1': '0', '2': '1', '3': '2'}, '2': {'0': '3', '1': '4', '2': '0', '3': '1'}, '3': {'0': '2', '1': '3', '2': '4', '3': '0'}, '4': {'0': '1', '1': '2', '2': '3', '3': '4'}} ~/study/divide-5/script$ ls script.py filename.png ~/study/divide-5/script$ display filename 

Output:

base_4_divided_5_best
DFA accepting numeric strings in base 4 is divided by 5

In the same way, enter base = 4 and number = 7 to generate - dfa, taking a number string in the base "4", which are divided by "7"
Btw, try changing filename to .png or .jpeg .

<sub> The links that I use to write this script are:
➊ baseN function from "convert integer to string in given numeric base in python"
βž‹ To install "pygraphviz": "Python does not see pygraphviz"
➌ To learn about using Pygraphviz: "Python-FSM"
➍ To generate random hexadecimal color codes for each character in the language: "How to create a random hexdigit code generator using .join and for loops?" Sub>

+189
Mar 09 '14 at 13:27
source share
β€” -

I know that I'm pretty late, but I just wanted to add a few things to the already correct answer provided by @Grijesh. I would just like to point out that the answer provided by @Grijesh does not give a minimum DFA. Although the answer is certainly the right way to get DFA, if you need a minimal DFA, you have to look into your divisor.

As, for example, in binary numbers, if the divisor is a power of 2 (i.e. 2 ^ n), then the minimum number of required states will be n + 1. How would you create such an automaton? Just look at the properties of binary numbers. For a number, say, 8 (which equals 2 ^ 3), all its multiples will have the last 3 bits as 0. For example, 40 in binary format is 101000. Therefore, in order for the language to accept any number divisible by 8, we just we need an automaton that sees if the last 3 bits are 0, which we can only do in 4 states instead of 8 states. This is half the complexity of the car.

In fact, this can be extended to any base. For a triple base number system, if, for example, we need to create an automaton for divisibility with 9, we just need to see if the last 2 input numbers are 0. This can only be done in three states again.

Although, if the divisor is not so special, we only need to go through the @Grijesh answer. For example, in the binary system, if we take the divisors 3 or 7, or maybe 21, we only need to have so many states. Therefore, for any odd number n in the binary system, we need n states to define a language that takes all multiples of n. On the other hand, if the number is even, but not cardinality 2 (only in the case of binary numbers), then we need to divide the number by 2 until we get an odd number, and then we can find the minimum number of states through adding an odd number and the number of times we divided by 2.

For example, if we need to find the minimum number of DFA states that takes all binary numbers divisible by 20, we do:

 20/2 = 10 10/2 = 5 

Therefore, our answer is 5 + 1 + 1 = 7 . (1 + 1 because we doubled the number 20).

+4
Oct 10 '17 at 10:14
source share

DFA . w k- ,

 V[0] = 0 V[i] = (S[i-1] * k) + to_number(str[i]) 

V[|w|] - , w . , w mod N , .

 V[0] = 0 V[i] = ((S[i-1] * k) + to_number(str[i])) mod N 

V[i] 0 N-1, DFA. .

. .

k = 2, N = 5

 | V | (V*2 + 0) mod 5 | (V*2 + 1) mod 5 | +---+---------------------+---------------------+ | 0 | (0*2 + 0) mod 5 = 0 | (0*2 + 1) mod 5 = 1 | | 1 | (1*2 + 0) mod 5 = 2 | (1*2 + 1) mod 5 = 3 | | 2 | (2*2 + 0) mod 5 = 4 | (2*2 + 1) mod 5 = 0 | | 3 | (3*2 + 0) mod 5 = 1 | (3*2 + 1) mod 5 = 2 | | 4 | (4*2 + 0) mod 5 = 3 | (4*2 + 1) mod 5 = 4 | 

k = 3, N = 5

 | V | 0 | 1 | 2 | +---+---+---+---+ | 0 | 0 | 1 | 2 | | 1 | 3 | 4 | 0 | | 2 | 1 | 2 | 3 | | 3 | 4 | 0 | 1 | | 4 | 2 | 3 | 4 | 

. DFA, , , 0 N-1.

0
21 . '18 17:17
source share

dfa, {a, b}, 3

0
03 . '18 9:37
source share



All Articles