5

There are 5 red balls, 5 green balls, and 5 blue balls. How many ways are there to arrange them in a straight line so that no two balls of the same color are adjacent?

Here is what I have tried so far: I ignored the colors of the blue and the green balls and just treated them as the same colored balls (say black). Then, for the red balls to be apart from each other, there are 11C5 ways.

After that, I am stuck on what I should do for the green and blue balls.

H.C
  • 51

2 Answers2

4

Update. See OEIS sequence A110706 Number of linear arrangements of n blue, n red and n green items such that there are no adjacent items of the same color.

Assuming the balls are to be arranged in a line, I get $7188$.

The number of arrangements with a red ball at each end is $$\sum_{k=0}^2\binom4{2k}\binom{6-k}3\binom{4-2k}{2-k}2^{2k}=664$$ which I will now explain.

We start with a supply of $5$ red balls and $10$ colorless balls; we will decide later which of the latter will be blue or green. Place the $5$ red balls and $4$ of the colorless balls in a line, alternating between red and colorless. We have $6$ colorless balls left to distribute among the $4$ gaps between the red balls.

First we decide how many of the $4$ gaps will get an odd number of additional balls (so they end up with an even number). This will be an even number $2k$ where $0\le k\le2$

Next we decide which of the $4$ gaps get an odd number of additional balls, in $\binom4{2k}$ ways. Placing one ball in each of the chosen gaps, we are left with $6-2k$ colorless balls.

Now, so as not to disturb the parities, we divide the remaining $6-2k$ colorless balls into $3-k$ pairs, and distribute the pairs among the $4$ gaps; this can be done in $\binom{6-k}3$ ways according to the "Stars and Bars" formula.

Finally we have to assign colors to the colorless balls. Of course the colors in each gap between red balls must alternate between blue and green, but we have to decide whether to start with blue or green. In the $2k$ even gaps we can choose the starting colors arbitrarily, so there are $2^{2k}$ ways. In the $4-2k$ odd gaps, however, half must start with blue and half with green, so there are $\binom{4-2k}{2-k}$ ways.

By a similar argument, the number of arrangements with a red ball at the left end and a non-red ball at the right end is $$\sum_{k=0}^2\binom5{2k+1}\binom{6-k}4\binom{4-2k}{2-k}2^{2k+1}=1732.$$

Thus the total number of arrangements with a red ball at the left end is $$664+1732=2396.$$

By symmetry, one third of the arrangements have a red ball at the left end, so the grand total number of arrangements is $$3\times2396=\boxed{7188}.$$

Alternatively, the number of arrangements with non-red balls at both ends is $$\sum_{k=0}^2\binom6{2k}\binom{7-k}5\binom{6-2k}{3-k}2^{2k}=3060$$ so the grand total is $$664+1732+1732+3060=\boxed{7188}.$$

P.S. Let $A$ be the number of arrangements with red balls at both ends, $B$ the number with a red ball at the left end and a non-red ball at the right end, $C$ the number with non-red balls at both ends, and $N$ the total number of arrangements. Since (by symmetry) one third of the arrangements have a red ball at the left end, we only need to compute two of the three sums: $$N=A+2B+C=3(A+B)=3(C-A)=\frac{3(B+C)}2$$ $$=3\sum_{k=0}^2\left[\binom4{2k}\binom{6-k}3+2\binom5{2k+1}\binom{6-k}4\right]\binom{4-2k}{2-k}4^k.$$ More generally, if we want to arrange $n$ red, $n$ green, and $n$ blue balls in a line so that adjacent balls have different colors, then for an odd number $n=2t+1$ the number of arrangements is $$3\sum_{k=0}^t\left[\binom{2t}{2k}\binom{3t-k}{2t-1}+2\binom{2t+1}{2k+1}\binom{3t-k}{2t}\right]\binom{2t-2k}{t-k}4^k;$$ for an even number $n=2t$ the number of arrangements is $$\frac32\sum_{k=0}^t\left[\binom{2t}{2k}\binom{3t-1-k}{2t-1}+2\binom{2t+1}{2k+1}\binom{3t-1-k}{2t}\right]\binom{2t-2k}{t-k}4^k.$$

bof
  • 78,265
1

I started this answer, typed a good part of it, but then a compact solution having in essence the same counting strategy already appeared. The only difference is that below we go explicitly through the partitions of ten, and count for each one the corresponding cases. Details are given at all places, two ways to check are also inserted.

The reader may benefit from the two points of view of the same idea, and the variations.

@OP: Please consider already accepting the other answer of bof!


I will use $R,G,B$ for the given colors.

Let the places in the line be numbered from $1$ to $15$. We want to count the functions $X:\{1,2,\dots,15\}\to\{R,G,B\}$ with $X(k)\ne X(k+1)$ when $k$ runs from $1$ to $14$. Below $X$ is such a function.

There are three possibilities for $X(1)$. By symmetry, we may and do assume $X(1)=R$, count such functions first, then finally multiply by $3$. There are two possibilities now for $X(2)$. We may and do assume furthermore $X(2)=G$, count such functions

Consider the other four places where $X=1$, and remove now all $R$ nodes from the connected graph

o---o---o---o---o---o---o---o---o---o---o---o---o---o---o
1   2   3   4   5   6   7   8   9  10  11  12  13  14  15

Then there remain

  • either four connected components, this is the case when $X(15)=R$,
  • or else five connected components, the case $X(15)\ne R$. We take the number of elements in those components, and obtain a partition of $10$, the remained $5G$ and $5B$ nodes. We list all possible partitions of $10$ in four or five pieces, after we order each such partition. For instance, $10$ can be partitioned as $\pi=(1,5,1,3)$. The ordered version is $\Pi=(5,3,1,1)$. There are more partitions having $\Pi$ as ordered version, and we denote by $n(\Pi)$ this number of unordered versions leading to $\Pi$. How many versions are there for a given $\Pi$? To have a formula from case to case we proceed as follows. Collect the elements in $\Pi$, seen as a set, In the sample $\Pi$, the elements are $5,3,1$. Then we count how often they appear. In the sample respectively $1,1,2$ times. Then the number of version is the corresponding multinomial coefficient, in the sample $\binom 4{112}$.

The length of a partition $\Pi$ is denoted below by $l(\Pi)$, so for our sample above $l(\Pi)=4$.

It remains to count how many possibilities there are to fill a partition $\Pi$ in the four or five intervals in their nodes with the $5G$ and $5B$ balls, let us denote this number by $f(\pi)$. Of course, $f$ is the same or $\pi$, and for the ordered version $\Pi$.

How to compute $f$ for a given partition $\Pi=(\Pi_1,\Pi_2,\Pi_3,\dots)$? First of all we note that if the color ($G$ or $B$) of the first ball in an interval of length $\Pi_k$ is fixed, then the interval is colored in a unique manner, we are constrained to alternate the two colors. So there are at a rough view $2\cdot2\cdot2\cdot\dots\cdot 2=2^{l(\Pi)}$ filling possibilities. However, there is the restriction that we use as many $G$ balls as $B$ balls. The intervals of even length are already balanced, they are not introducing any constraint. But the odd length intervals do contribute. Since we are partitioning ten, an even number, there are either two or four odd entries among $\Pi_1,\Pi_2,\Pi_3,\dots$ - and in case of two odd entries the two odd length interval must start (and end) with different colors. So there are $2^{l(P)-1}$ possibilities, taking the contraint in account. What about four odd entries, as in the case $5,3,1,1$. Exactly two of the four intervals start with $G$, exactly two with $B$. We consider the positions of the two $G$-starting blocks, two positions among four, so there are $\binom 42=6$ possibilities. The formula for the filling $f(\Pi)$ is in this case $2^{l(\Pi)-4}\cdot\binom 42$. With this structure in fact, the number of fillings in case of a $\Pi$ with $0$, respectively $2$ odd pieces is $2^{l(\Pi)-0}\cdot\binom00=2^{l(\Pi)}$, respectively $2^{l(\Pi)-2}\cdot\binom21=2^{l(\Pi)-1}$.

The combinatorial data can be collected so far in a table: $$ \begin{array}{|l|c|l|r|} \hline \Pi & l(\Pi) & n(\Pi) & f(\Pi) & n(\Pi)\cdot f(\Pi)\\\hline (7, 1, 1, 1) & 4 & \binom4{ 1,3 } = 4 & 2^{4-4}\cdot \binom 42 = 6 & 24\\\hline (6, 2, 1, 1) & 4 & \binom4{ 1,1,2 } = 12 & 2^{4-2}\cdot \binom 21 = 8 & 96\\\hline (5, 3, 1, 1) & 4 & \binom4{ 1,1,2 } = 12 & 2^{4-4}\cdot \binom 42 = 6 & 72\\\hline (5, 2, 2, 1) & 4 & \binom4{ 1,2,1 } = 12 & 2^{4-2}\cdot \binom 21 = 8 & 96\\\hline (4, 4, 1, 1) & 4 & \binom4{ 2,2 } = 6 & 2^{4-2}\cdot \binom 21 = 8 & 48 \\\hline (4, 3, 2, 1) & 4 & \binom4{ 1,1,1,1 } = 24 & 2^{4-2}\cdot \binom 21 = 8 & 192\\\hline (4, 2, 2, 2) & 4 & \binom4{ 1,3 } = 4 & 2^{4-0}\cdot \binom 00 = 16 & 64\\\hline (3, 3, 3, 1) & 4 & \binom4{ 3,1 } = 4 & 2^{4-4}\cdot \binom 42 = 6 & 24\\\hline (3, 3, 2, 2) & 4 & \binom4{ 2,2 } = 6 & 2^{4-2}\cdot \binom 21 = 8 & 48 \\\hline % (6, 1, 1, 1, 1) & 5 & \binom5{ 1,4 } = 5 & 2^{5-4}\cdot \binom 42 = 12 & 60\\\hline (5, 2, 1, 1, 1) & 5 & \binom5{ 1,1,3 } = 20 & 2^{5-4}\cdot \binom 42 = 12 & 240\\\hline (4, 3, 1, 1, 1) & 5 & \binom5{ 1,1,3 } = 20 & 2^{5-4}\cdot \binom 42 = 12 & 240\\\hline (4, 2, 2, 1, 1) & 5 & \binom5{ 1,2,2 } = 30 & 2^{5-2}\cdot \binom 21 = 16 & 480\\\hline (3, 3, 2, 1, 1) & 5 & \binom5{ 2,1,2 } = 30 & 2^{5-4}\cdot \binom 42 = 12 & 360\\\hline (3, 2, 2, 2, 1) & 5 & \binom5{ 1,3,1 } = 20 & 2^{5-2}\cdot \binom 21 = 16 & 320\\\hline (2, 2, 2, 2, 2) & 5 & \binom5{ 5 } = 1 & 2^{5-0}\cdot \binom 00 = 32 & 32\\\hline \end{array} $$ The wanted number is thus: $$ \begin{aligned} \sum_{\pi}f(\pi)&= \sum_{\Pi\text{ ordered}}n(\Pi)f(\Pi) \\ &= 24 + 96 + 72 + 96 + 48 + 192 + 64 + 24 + 48 \\ &\qquad\qquad + 60 + 240 + 240 + 480 + 360 + 320 + 32 \\ &=664 + 1732 = \bbox[yellow]{\ 2396\ }\ . \end{aligned} $$ Recall that this is the count for the case $X(1)=R$. The final count is $$ 3\cdot 2396=\bbox[yellow]{\ 7188\ }\ . $$


Computer check:

Here is a short piece of code that covers "all cases" and checks the $5R+5G+5B$ condition. Well, all cases are $3^{15}=14348907$, but we may use the information from the condition, and have thus after each known ball step exactly two chances to generate, the two other colors. Instead of using $R,G,B$ is useful to count using the (addition group of the) field with three elements $\Bbb F_3=\Bbb Z/3$. Then if one ball is $b$, the next ball is either $b+1$ or $b+2$, thus obtained from $b$ by a non-zero increment, the code uses inc for such an increment at each position. And here is the code, in fact we have a straightforward programming problem, but a rather complicated combinatorial one:

F = GF(3)
R = [F(1), F(2)]
increments_range = 14*[R]

good_cases = 0 # so far

for b in F: # b is the color of the first ball for increments in cartesian_product(increments_range): balls = [b] for inc in increments: balls.append(balls[-1] + inc) if balls.count(F(0)) == 5 and balls.count(F(1)) == 5: good_cases += 1

print(good_cases)

That's all, we get our number, 7188.


There is an alternative approach using generating series. For instance as in

Counting words with Laguerre series, Jair Taylor, §2, Laguerre polynomials and Laguerre series

where polynomials $l_k(t)$ are introduced. They are defined as the part in $x^k$ in the (formal) Taylor expansion of the generating series $$ \exp\frac {tx}{1+x}\ . $$ Then we want to compute, in the notations from loc. cit. the polynomial $l_5(t)$, then consider $l_5(t)^3$ as a polynomial, and apply on it the linear functional $\Phi$ defined by $\Phi(t^n):=n!$. The polynomial $l_5(t)$ is easily computed with code

sage: var('t,x');
sage: taylor( exp(t*x/(1+x)), x, 0, 5).coefficient(x^5)
1/120*t^5 - 1/6*t^4 + t^3 - 2*t^2 + t

but also humanly we have a chance. Below, we denote by $[x^5]g$ the part in $x^5$ in a series $g$. $$ \begin{aligned} [x^5]\exp\frac {tx}{1+x} &= [x^5]\frac 1{0!} + [x^5]\frac 1{1!} \frac {tx}{1+x} + [x^5]\frac 1{2!} \frac {(tx)^2}{(1+x)^2} + [x^5]\frac 1{3!} \frac {(tx)^3}{(1+x)^3} + \\ &\qquad\qquad\qquad [x^5]\frac 1{4!} \frac {(tx)^4}{(1+x)^4} + [x^5]\frac 1{5!} \frac {(tx)^5}{(1+x)^5} \\ &= \frac t{1!}[x^5](x-x^2+x^3-x^4+x^5-\dots) \\ &\qquad + \frac {t^2}{2!}[x^5]\frac 11(1\cdot x^2-2\cdot x^3+3\cdot x^4-4\cdot x^5+5\cdot x^6-\dots) \\ &\qquad + \frac {t^3}{3!}[x^5]\frac1{1\cdot 2}(1\cdot 2x^3-2\cdot 3x^4 +3\cdot 4x^5-4\cdot 5x^6+\dots) \\ &\qquad + \frac {t^4}{4!}[x^5]\frac1{1\cdot 2\cdot 3}(1\cdot 2\cdot 3x^4-2\cdot 3\cdot 4x^5+3\cdot 4\cdot 5x^6-\dots) \\ &\qquad + \frac {t^5}{4!}[x^5]\frac1{1\cdot 2\cdot 3\cdot 4}(1\cdot 2\cdot 3\cdot 4x^5-2\cdot 3\cdot 4\cdot 5x^6+\dots) \\ &=t-2t^2+t^3-\frac 16t^4+\frac 1{120}t^5\ . \end{aligned} $$ But now, we have humanly a computational problem of $l_5(t)^3$, which is annoying, it hides more work than the combinatorial solution above. For this reason, i consider this solution as only a check, assisted by sage.

Yes, the whole checking code can be written in a simpler manner, since $\Phi$ can be implemented by $\Phi(p) = \int_{(0,\infty)}\exp(-t)\;p(t)\; dt$. Code, also including the above computation of $l_5$:

var('t,x');
L5 = taylor( exp(t*x/(1+x)), x, 0, 5).coefficient(x^5)
N = integral(exp(-t) * L5^3, t, 0, oo)
print(f"\\Phi(l_5(t)^3) = {N}\\ .")

And we get: $$ \Phi(l_5(t)^3) = 7188\ . $$ (Yes, a brilliant approach, when computers are allowed, but on the other side, brute force plain and lazy counting works too.)

dan_fulea
  • 32,856
  • At first I tried to do it your way, listing all the partitions. It was very tedious work; worse, I was unable to do the bookkeeping accurately (this was all by hand), and kept getting different answers. Thanks for confirming that $7188$ is correct! – bof Oct 18 '23 at 21:07
  • https://oeis.org/A110706 – bof Oct 18 '23 at 23:39