2

So this is more of a statement of an interesting approach I found (of solving a particular type of problem) than a question, but I would like to read up more on the subject, if there even is a subject here. So maybe that would be my question - where to investigate this topic further?

The question is about finding the number "string" representations given certain conditions. For example, consider the question: How many three digit numbers contain exactly one '3'? Now, I suppose you could write out the combinations manually, but what I found was a more general way of solving these kinds of problems.

The main part of the approach is to reword the problem in terms of a finite-state automaton, that is finding the possible states and the transitions between them, deriving a recurrence relation for the $n$-th case (where $n$ represents the string length), solving it and finding the desired value for a particular length. These states represent the mental model of when you try to solve such problems manually. For the example above, imagine trying to determine the possible values for the first digit, based on those the possible values of the second and lastly, based on those the ones for the third. You could argue that for the first digit you can have any digit other than '0' (that would make the number a two digit number). The possible digits in the second position depend on the digit in the first - if it was a '3', you can have a digit from '0'-'9' except '3', and if it wasn't you can have any. For the third digit, if you had a digit '3' in either the first or second place, you can have but '3', but if you haven't you can only have the digit '3'. So, as you can see, this gets complicated quickly.

In my approach, you recognize there are two states - one where the digit '3' was not yet included (let's call that state $N$), and one where it was (let's call that state $Y$). Now, you can see that you can stay in $N$ by choosing any digit but the digit '3', you can go from $N$ to $Y$ by choosing the digit '3' and you stay in $Y$ by choosing any digit but the digit '3'. Also, you can start in $N$ in 8 possible ways (any digit but '0' and '3') and you can start in $Y$ in one possible way (first digit is '3'). Here's a visual representation of that.

Now, let $N_n$ denote the number of possible combinations generated after $n$ steps and ending in state $N$, and $Y_n$ the same, but when ending in state $Y$. Now, looking at the diagram, we see that this must hold:

$N_n=9 N_{n-1}$ with $N_1=8$

$Y_n=N_{n-1} + 9 Y_{n-1}$ with $Y_1=1$

The reasoning behind this is that the number of combinations after $n$ steps that end in state $X$ is equal to the number of combinations after $n-1$ steps that end in any of the possible states times the number of ways we can get from a particular state to state $X$.

Now we just need to solve this, but this is relatively simple when written in matrix form like this:

$\begin{pmatrix}N_n \\ Y_n\end{pmatrix}=\begin{pmatrix}9 & 0 \\ 1 & 9\end{pmatrix}\begin{pmatrix}N_{n-1} \\ Y_{n-1}\end{pmatrix}=\begin{pmatrix}9 & 0 \\ 1 & 9\end{pmatrix}^{n-1}\begin{pmatrix}N_{1} \\ Y_{1}\end{pmatrix}=\begin{pmatrix}9 & 0 \\ 1 & 9\end{pmatrix}^{n-1}\begin{pmatrix}8 \\ 1\end{pmatrix}$

So, for the example above, when $n=3$ we have $\begin{pmatrix}N_3 \\ Y_3\end{pmatrix}=\begin{pmatrix}9 & 0 \\ 1 & 9\end{pmatrix}^{2}\begin{pmatrix}8 \\ 1\end{pmatrix}=\begin{pmatrix}648 \\ 225\end{pmatrix}$.

And since the valid end-state for this example is only the $Y$ state, we get the answer to be $Y_3=225$.

Now, for a simple example such as tone above, there doesn't seem to be much benefit in this over-the-top approach. However, I believe it does for examples that are a bit more complicated. An example to prove my point: What is the number of possible 4 digit numbers where the digits '0' and '1' are never next to each other? So $6013$ and $1210$ are note allowed, but $1304$ and $7007$ are. Doing this manually would be a chore (for me), but doing it like I've described is manageable. Here's the finite-state machine describing this problem. From this we have:

$\begin{pmatrix}N_n \\ Z_n \\ Y_n\end{pmatrix}=\begin{pmatrix}8 & 8 & 8 \\ 1 & 1 & 0 \\ 1 & 0 & 1 \end{pmatrix}^{n-1}\begin{pmatrix}8 \\ 0 \\ 1\end{pmatrix}$

Plugging in $n=4$ gives

$\begin{pmatrix}N_4 \\ Z_4 \\ Y_4\end{pmatrix}=\begin{pmatrix}6984 \\ 792 \\ 793\end{pmatrix}$

And since all of the states are valid final states, the total number of combinations is $N_4+Z_4+Y_4=8569$.

Note that it would probably be simpler to solve this example by calculating the number of valid strings as the number of all strings minus the number of invalid ones, but even that approach has it's downfalls.

So, that's it. Now - how could I expand my knowledge on this?

I hope this made a somewhat interesting read...

EDIT: Just to add one more example I find interesting: What's the number of possible binary strings (strings containing only digits '0' and '1') of length $n$ where the digit '1' is never in succession? Turns our the resulting matrix is the Fibonacci number matrix.

RobPratt
  • 50,938
milin
  • 233
  • You might be interested in https://en.wikipedia.org/wiki/Subshift_of_finite_type – kimchi lover Dec 17 '20 at 13:06
  • Thanks! I will definitely look into it. – milin Dec 17 '20 at 13:17
  • 2
    Counting of strings can also be linked to generating functions. Examples can be found in Feller, "An Introduction to Probability Theory and Its Applications, Volume 1, Third Edition", section XIII.7; Lando, "Lectures on Generating Functions"; and Sedgewick and Flajolet, "An Introduction to the Analysis of Algorithms, Second Edition". – awkward Dec 17 '20 at 14:06

1 Answers1

1

Much of what you discuss can be simplified.

Three digit numbers with exactly one $3$: if you allow the number to start with $0$, then you have to place the $3$ and then the others can be anything (other than $3$), hence $3\times 9^2=243$. If you don't allow the first digit to be $0$ then we split into cases. There are $9^2$ with first digit $3$ and then there are $2\times 8\times 9=144$ where the $3$ is not first. Thus, in that case there are $81+144=225$. I gather from your result that you are interested in the second case.

And your final problem can be addressed recursively, which makes the connection to Fibonacci more obvious. Indeed, a good string of length at least $2$ must end in either $0$ or $01$, so $A_n=A_{n-1}+A_{n-2}$ (where, of course, $A_n$ is the number of good strings of length $n$).

lulu
  • 76,951