0

I have an unknown number with n digits without repetition. I have n sets with the allowed digits for each position. I need to know how many possibilities exist for the unknown number.

When the sets are identical I can just calculate the number of n permutations of the set size. For instance if n=4 and the sets are {1, 2, 3, 4, 5, 6}, I know there are 6! / (6 - 4) = 360 possibilities.

If the sets don't intersect at all I can just calculate the product of their sizes, so for n=4 and the sets {1, 2, 3, 4, 5}, {6, 7}, {8, 9}, {0} I know there are 5 * 2 * 2 * 1 = 20 possibilities.

My difficulty is in handling repeated digits with different sets, like for n=4 and the sets {1, 2, 3}, {3, 4, 5}, {4, 5, 6, 7}, {1, 2, 8, 9}. By generating all possibilities where no digit is repeated I know there are 84, but is there any way to calculate it?

  • 1
    I would say that it is non-trivial even to decide whether there is a single number that can be made this way...see this question. Of course, in this particular case, the sets are all very small so brute force shouldn't be too bad. – lulu Jan 03 '24 at 22:13
  • 1
    If I understand you correctly, rook theory is precisely the study of this question. Writing down an n-digit number in base $d$ with no repeated digits and restrictions on which digits can appear in each column is equivalent to placing rooks on an $n \times d$ chess board, with certain squares removed corresponding to the forbidden digits, so that no two rooks occupy the same row or column. – Jair Taylor Jan 03 '24 at 23:41
  • 1
    There are formulae for this problem for some boards (e.g. see my answer here) but there is no fast algorithm in general for large boards with random squares removed. – Jair Taylor Jan 03 '24 at 23:43
  • @JairTaylor Indeed, it's the same problem as placing rooks on a board with squares removed. I'd never think of that. I'm seeing some stuff with the rook polynomial with forbidden positions and square or rectangular subsets of the board that might be a good fit for my case. Thanks! – Pedro Werneck Jan 04 '24 at 15:19

1 Answers1

2

I don't think there's a "simple" answer to this question. In particular, the problem is #P-Complete when the number of digits is not just $10$ (say, you are in base $b$ for some variable $b$ instead of base $10$), by reduction to Maximum Bipartite Matching: let the left side of the bipartite graph be the positions $1,2,\ldots,n$ and the right side be digits $0,1,2,\ldots,b-1$, adding an edge between position $i$ and digit $j$ if $j$ lies in the set $S_i$.

However, there are algorithms which run in time $O^\ast(2^n)$ for solving this problem (see for example this short description of Ryser's algorithm). For your problem, this should run fairly quickly.