# Minimum number of states in DFA accepting strings where the numbers of a and b are divisible by X and Y respectively?

While studying automata theory a typical problem that I face is of the following type:

Constructing a DFA with minimum number of states for all strings over $\{a,b\}$ which have number of $a$’s divisible by $X$ and number of $b$’s divisible by $Y$ (where $X$ and $Y$ are some positive integer values).

Is there some standard way of solving such problems?

I looked it up over the net and most people seem to have an idea that minimum number of states will be $X \cdot Y$. I don't think that is right. I constructed a DFA with 15 states for $X = 6$ and $Y = 8$.

Also along a similar line suppose the problem is changed slightly and we are given "number of $a$'s mod $X$ = $P$ and number of $b$'s mod $Y$ = $Q$".

I believe this type of problem will also have a similar solution as the above problem. Only difference will be in the final states of the two machines. Can someone please confirm whether I am right about this?

• Build any NFA for the language, determinise and minimise (both simple algorithms). This provably (well, you have to prove the first step) leads to a minimal DFA for the language, thus you know the number. – Raphael Feb 19 '14 at 7:59
• If you are from computer Science background then you may like derivation by analysis instead by formal technique. There is no technique(automation/formula) using that you can tell how many states are required but Can guess when you need to switch states while writing DFA then you can count on fingers(yes not for very complex problem but for problem you will most of time find in your book): Here is my way I described to draw minimized DFA – Grijesh Chauhan Feb 19 '14 at 8:07
• there are quite a few different algorithms/study for DFA minimzation see also eg brzozowski algorithm for dfa minimization – vzn Feb 19 '14 at 19:19
• Hey can you please share your DFA with 15 states. – codeomnitrix Feb 22 '14 at 5:54

I think the minimum number of states is $X\cdot Y$. In particular, for $X=6$ and $Y=8$, I think 48 states are required.

One can prove this using Myhill-Nerode. The equivalence class (in Myhill-Nerode) of string $s$ is uniquely determined by the pair $\langle n_a(s) \bmod X, n_b(s) \bmod Y \rangle$, where $n_a(s)$ counts the number of $a$'s in $s$, and $n_b(s)$ the number of $b$'s. Two different equivalence classes $\langle i,j \rangle$, $\langle i',j' \rangle$ can be separated by the suffix $a^{X-i} b^{Y-j}$, since that'll take the former to an accepting state and the latter to a non-accepting state (if $i \ne i'$ or $j \ne j'$). This gives us $X \cdot Y$ different equivalence classes, so any DFA accepting this language must have at least $X \cdot Y$ states.

Are you sure you can do it in 15 states, for $X=6$ and $Y=8$? Is it possible you might have made a mistake somewhere in your DFA?

• Is it $|M_1|\times|M_2|$ - the number of states in intersection automaton of $|M_1|$ and $|M_2|$? or its $LCM(|M_1|$,$|M_2|)$? I am new to the topic. So just want to know, here we are talking about intersection automaton only right? It is also called as "product automaton" right? – Maha Jan 20 '16 at 8:30
• @Mahesha999, the former, as explained in my answer. Try working through an example. (I don't see any reason why it'd be the LCM. Note carefully that we are talking about $X$ a's and $Y$ b's -- note the a's and b's.) – D.W. Jan 20 '16 at 8:33
• Yes I understand the points made in answers here and its due to how product automaton is constructed. But I am confused because of slide 32 of pdf linked by @RickDecker in his answer. It says: let $M_k$ be the “cyclic” automaton that recognizes multiple of $k$, such that $L(M_k)=\{a^n|k\text{ divides }n\}$, then $M_6×M_9=M_{18}$ – Maha Jan 20 '16 at 8:57
• @Mahesha999, that's referring to something different -- there are no b's in that example, whereas this question has both a's and b's. Different problem, different answer. At this point, if you don't understand what's going on, I recommend you try working through some examples and then post a new question. Comment threads are not intended for interactive, back-and-forth questions and dialogue. Thank you! – D.W. Jan 20 '16 at 9:15

You could use the Myhill–Nerode theorem to find the minimum number of states, that is equal to the number of the equivalence classes.

A technique that is particularly helpful in problems like the ones you've given is to construct the cartesian product of two finite automata. (The relevant part of the link starts at slide 26.) In your situation, you'll construct two FA, one, $M_1$, for the language $$L_1 = \{a^k \mid k\equiv 0\pmod X\}\quad\text{i.e, strings of as with length a multiple of X}$$ One such machine will have $X$ states, $p_n,\ (0\le n < X)$, each corresponding to the situation where the number of $a$s seen so far is congruent to $n\bmod X$. It's clear that the transitions in $M_1$ will be $\delta(p_n, a) = p_{n+1 \pmod X}$.

In a similar way, construct another FA, $M_2$ for the language $$L_2 = \{b^k \mid k\equiv 0\pmod Y\}\quad\text{i.e, strings of bs with length a multiple of Y}$$ As above, denote the states of $M_2$ by $q_n,\ (0\le n < Y)$ with transitions defined to be $\delta(q_n, a) = q_{n+1 \pmod Y}$.

Now here's where the product construction comes in. Make a new FA whose states are pairs of states $(p_i, q_j)$ each component of which acts like the ones in $M_1$ and $M_2$. In other words, we'll have $X\cdot Y$ such pairs, with transitions \begin{align} \delta((p_i, q_j), a) &= (p_{i+1\pmod X}, q_j) \\ \delta((p_i, q_j), b) &= (p_i, q_{j+1\pmod X}) \end{align} For example, with $X=2, Y=3$ the product machine will have 6 states, $(0,0),(0,1),(0,1),(1,0),(1,1),(1,2)$, and if we happen to be in state $(1,1)$ and see input $abb$ our transitions will be $$(0, 1)\stackrel{a}{\longrightarrow}(1, 1)\stackrel{b}{\longrightarrow}(1, 2)\stackrel{b}{\longrightarrow}(1, 0)$$ Now what should the final state of this machine be? In your first example, it's easy to see that it should be $(0,0)$, namely when we've seen an input where the number of $a$s is divisible by $X$ and the number of $b$s is divisible by $Y$. Similarly, in your second example, the final state should be $(P, Q)$.

[It's worth noting that the construction of this product machine, $M_1\times M_2$, works for any FA $M_1, M_2$ and, by suitably choosing the final states, can be used to construct machines for the union, intersection, and difference of any regular languages.]