Computer Science, asked by sneha9140, 1 year ago

Write a program to color the circles in such a way that no two adjacent circles has the same color.

Answers

Answered by avirao
0
Let anan be the number of ways of coloring a ring of nn dots. Now consider a ring of n+1n+1 dots that have been properly colored. There are two possibilities: either dots 11 and nn have the same color, or they do not.

If they do not have the same color, then removing dot n+1n+1 leaves one of the anan properly colored rings of nndots. Conversely, if we insert a dot n+1n+1 into a properly colored ring of nn dots, we must give it the third color in order to have a properly colored ring of n+1n+1 dots. Thus, there are ananproperly colored rings of n+1n+1 dots in which dots 11 and nn have different colors.

If dots 11 and nn have the same color, imagine removing dot n+1n+1 and merging dots 11 and nn into a single dot; you now have a properly colored ring of n−1n−1 dots. Conversely, if you start with a properly colored ring of n−1n−1 dots, you can split dot 11 into two dots of the same color, one adjacent to dot 22 and one adjacent to dot n−1n−1; call the former dot 11 and the latter dot nn. Now insert dot n+1n+1between the new dots 11 and nn; this time you have two choices for its color, so there are 2an−12an−1 properly colored rings of n+1n+1 dots in which dots 11 and nn have the same color.

The recurrence is therefore an+1=an+2an−1an+1=an+2an−1, and I’ll leave it to you to work out the initial conditions

Similar questions