In how many ways can we colour $n$ baskets with $r$ colours?

In how many ways can we colour $n$ baskets using up to $r$ colours such that no two consecutive baskets have the same colour and the first and the last baskets also have different colours?

For example, if we take $N=5$ and $r = 4$, and represent the colours by $R,B,Y$ and $G$, then $\langle R,Y,B,G,Y\rangle$ is a valid arrangement whereas $\langle R,R,B,G,Y \rangle$ and $\langle G,B,R,Y,G\rangle$ aren't.

It's is not difficult to solve this one by brute force; however, I would like to see a combinatorial approach. Any thoughts?


Let $a_n$ be the number of arrangements in which the first and last basket have different colours, and $b_n$ the number of arrangements in which they have the same colour, where in either case adjacent baskets can't have the same colour. Then by adding an admissible basket at the end of such an arrangement we obtain the recurrence

$$ \begin{align} a_{n+1}&=(r-2)a_n+(r-1)b_n\;,\\ b_{n+1}&=a_n\;, \end{align} $$

and substituting the second equation into the first yields

$$ a_{n+1}=(r-2)a_n+(r-1)a_{n-1}\;. $$

The characteristic equation is

$$ \lambda^2-(r-2)\lambda-(r-1)=0\;, $$

with solutions $\lambda=-1$ and $\lambda=r-1$. Thus we have

$$ a_n=c_1(-1)^n+c_2(r-1)^n\;, $$

and the initial conditions $a_1=0$ and $a_2=r(r-1)$ yield

$$ -c_1+c_2(r-1)=0\;,\\ c_1+c_2(r-1)^2=r(r-1) $$

with solution $c_1=r-1$, $c_2=1$. The desired number of arrangements is therefore

$$ a_n=(-1)^n(r-1)+(r-1)^n\;. $$

To get the number of arrangements that use all $r$ colours, you can use inclusion/exclusion:

$$ \sum_{k=0}^r(-1)^{r-k}\binom rk\left((-1)^n(k-1)+(k-1)^n\right)\;, $$

and the sum over the first term vanishes, leaving

$$ \sum_{k=0}^r(-1)^{r-k}\binom rk(k-1)^n\;. $$