# One Dimensional 𝑛ary Density Classification Using Two Cellular Automaton Rules

## Abstract

Suppose each site on a one-dimensional chain with periodic boundary condition may take on any one of the states $`0,1,\mathrm{},n1`$, can you find out the most frequently occurring state using cellular automaton? Here, we prove that while the above density classification task cannot be resolved by a single cellular automaton, this task can be performed efficiently by applying two cellular automaton rules in succession.

Cellular automaton (CA) is a simple local parallel interaction model for many natural systems . And from the computer science point of view, CA can be regarded as a special kind of Turing machine without internal memory. In fact, tailor-made CA can be used to simulate certain logical operations .

Since CA is essentially an internal-memoryless Turing machine, it is natural to ask if it can be used to perform certain tasks that require global counters. One such task, called (binary) density classification, was recently studied by Land and Belew . They consider a one-dimensional (finite but arbitrarily long) chain of sites with periodic boundary condition. Each site is in either state zero or state one. That is, each site contains a Boolean state. Our task is to change the state of every site to one if the number of ones is more than the number of zeros in the chain. (That is, the density of one, $`\rho _c`$, in the chain is greater than 1/2.) Otherwise, every site is set to the state zero. Clearly, the density classification problem is trivial if one uses a global counter. Alternatively, one can also solve this problem if the CA rule table scales with the number of sites $`N`$ so that the CA model becomes nonlocal in the limit of large $`N`$. Nonetheless, Land and Belew proved that (binary) density classification cannot be done perfectly using a single one-dimensional CA . Their proof can be extended to multiple dimensions, too.

Quite unexpectedly, Fukś found that the (binary) density classification problem can be solved if we apply two CA rules in succession . Later on, Chau et al. generalized his result by showing that classifying any rational density $`\rho _c`$ on a $`N`$ site chain can be performed efficiently in $`\text{O}(N)`$ time using two CA rules in succession .

At this point, it is natural to ask if CA can be used to classify $`n`$ary density, namely, when each site takes on a $`n`$ary state. In fact, Land and Belew conjectured that a significantly different way of argument is required to generalize their “no-go” result to the $`n`$ary case . In this Letter, we first prove that $`n`$ary density classification by CA is impossible. Although we only report our proof for the one-dimensional case, our arguments can be readily generalized to multiple dimensions. But most important of all, our proof provides a hint to solve the $`n`$ary density classification problem using two CA rules. Guided by this hint, we report a simple and efficient way to classify $`n`$ary density with two CA rules.

Statement of the problem — We begin by formally defining the $`n`$ary density classification problem. Suppose that each of the one-dimensional chain of $`N`$ sites in periodic boundary condition may take on a state in $`0,1,\mathrm{},n1`$. We define $`\rho _i(\alpha )`$ as the number of sites in state $`i`$ for the configuration $`\alpha `$ divided by $`N`$. That is, $`\rho _i(\alpha )`$ is the density of the state $`i`$ of the configuration $`\alpha `$. Our goal is to evolve the state of all sites to $`i`$ if and only if $`\rho _i(\alpha )>\rho _j(\alpha )`$ for all $`ji`$. While this task is trivial if one has a global counter, we now show that this task is not achievable using a (deterministic) CA. More precisely, we mean that given any deterministic CA with a finite rule table, we can find a configuration with a sufficiently large $`N`$ such that this configuration cannot be correctly classified by this CA.

Impossibility to classify density by one CA rule — We prove our impossibility result by contradiction. Suppose a CA rule with radius $`r`$ correctly classifies any $`n`$ary density for all $`N`$, then we denote the action of this CA on a configuration $`\alpha `$ by $`𝐓`$. Moreover, we denote the set of all configurations with density $`\rho _i>\rho _j`$ for all $`ji`$ by $`\mathrm{\Omega }_i`$. Then, Land and Belew showed that 

###### Lemma 1

$`𝐓\left[\mathrm{\Omega }_i\right]\mathrm{\Omega }_i`$ for all $`i`$. Besides, $`𝐓\left[j^N\right]=j^N`$ where $`j^N`$ denotes a configuration of $`N`$ consecutive $`j`$s. In fact, if the states of a site and its $`2r`$ neighbors are all $`j`$, then the state of that site under the action of $`𝐓`$ equals $`j`$. Similarly, if $`\alpha `$ is a period $`2r+1`$ sequence, then so is $`𝐓(\alpha )`$.

Now, we make the following claim:

###### Claim 1

The action of $`𝐓`$ on any configuration $`\alpha `$ preserves the densities $`\rho _i`$ of the configuration $`\alpha `$.

Clearly, our conclusion that no such CA rule $`𝐓`$ exits follows directly from Claim 1 because the state $`00\mathrm{}001`$ cannot be brought to $`00\mathrm{}000`$ under the repeated action of $`𝐓`$.

Proof of the claim: Suppose the contrary, then we can find a configuration $`\beta `$ whose density $`\rho _i`$ is not preserved under the action of $`𝐓`$ for some $`i`$. Without lost of generality, we may assume that $`\rho _0(\beta )>\rho _0(𝐓(\beta ))`$ and $`\rho _1(\beta )<\rho _1(𝐓(\beta ))`$. Otherwise the proof is similar. If we denote the length of $`\beta `$ by $`s`$, then $`\rho _0(𝐓(\beta ))\rho _0(\beta )1/s`$. From Lemma 1, we know that $`\beta `$ must make up of more than one type of states. Now, we consider configurations in the form $`\gamma =\beta ^x0^y1^z`$; that is, $`\gamma `$ makes up of $`x`$ copies of $`\beta `$ followed by $`y`$ consecutive zeros and then $`z`$ consecutive ones. We choose the ratio $`x:y:z`$ in such a way that $`\gamma \mathrm{\Omega }_0`$ while $`\rho _1(\gamma )<\rho _0(\gamma )<\rho _1(\gamma )+\frac{x}{2(sx+y+z)}`$. In other words, $`\gamma `$ has a slight excess of zeros over ones to put it in $`\mathrm{\Omega }_0`$. Now, we consider the state $`𝐓(\gamma )`$. Since the radius of our CA equals $`r`$, so except for the $`6r`$ sites at the boundaries between $`\beta `$, $`0`$ and $`1`$, Lemma 1 tells us that states of all other sites containing $`0^y1^z`$ are unchanged by the action of $`𝐓`$. Consequently, $`\rho _0(𝐓(\gamma ))\rho _0(\gamma )\frac{x}{sx+y+z}+\frac{4r}{sx+y+z}`$. So, if we choose sufficiently large values of $`x`$, $`y`$ and $`z`$ while keeping the ratio $`x:y:z`$ fixed, we can make $`\rho _0(𝐓(\gamma ))\rho _0(\gamma )\frac{x}{2(sx+y+z)}`$ while $`\rho _1(𝐓(\gamma ))>\rho _1(\gamma )+\frac{x}{2(sx+y+z)}`$. Therefore, $`\rho _0(𝐓(\gamma ))<\rho _1(𝐓(\gamma ))`$. Hence $`𝐓(\gamma )\mathrm{\Omega }_0`$ although $`\gamma \mathrm{\Omega }_0`$. This contradicts Lemma 1. $`\mathrm{}`$

Clearly, we can modify the above proof to show that $`n`$ary density classification in multiple dimension by a single CA is also impossible. More importantly, Claim 1 tells us that in order to construct an $`n`$ary density classification rule using two CAs, the first rule must preserve densities. This is the reason why both the traffic rule and modified traffic rule used by Fukś  and Chau et al.  in binary density classification are density preserving.

Classification of $`n`$ary density using two CA rules — Let us introduce a few useful definitions before reporting the two CA rules that classifies $`n`$ary density.

###### Definition 1

Let $`\alpha =(a_i)_{i=1}^N`$ be a one-dimensional configuration in periodic boundary condition. An elementary block (EB) in $`\alpha `$ is defined to be a subsequence of consecutive states $`(b_j)_{j=1}^k`$ of $`\alpha `$ with $`b_1<b_2<\mathrm{}<b_k`$. Moreover, such a subsequence is maximal in the sense that inclusion of further element into the subsequence in either ends does not produce any EB.

Clearly, any configuration $`\alpha `$ can be uniquely decomposed into a collection of EBs. For instance, the configuration $`40223441`$ makes up of EBs $`02`$, $`234`$, $`4`$ and $`14`$. Furthermore, for an $`n`$ary state system, elementary combinatorics arguments shows that there are totally $`2^n1`$ different possible EBs.

###### Definition 2

We define the homogeneous lexicographic (hlex) ordering  to these $`2^n1`$ EBs as follows: An EB $`(a_j)`$ is greater than another EB $`(b_j)`$ if and only if (1) the length of $`(a_j)`$ is greater than that of $`(b_j)`$; or (2) the lengths of $`(a_j)`$ and $`(b_j)`$ agree and $`a_j>b_j`$ for the first index $`j`$ with $`a_jb_j`$.

We denote the $`2^n1`$ EBs by $`_i`$s. Then, the hlex order goes as $`_00<_{\mathrm{hlex}}_11<_{\mathrm{hlex}}\mathrm{}<_{\mathrm{hlex}}_{n1}n1<_{\mathrm{hlex}}_n01<_{\mathrm{hlex}}_{n+1}02\mathrm{}<_{\mathrm{hlex}}_{2n2}0(n1)<_{\mathrm{hlex}}\mathrm{}<_{\mathrm{hlex}}_{2^n2}012\mathrm{}(n1)`$. We may interpret the hlex ordering as a measure of affinity between different states.

Our goal of the first CA rule is to make the EBs to interact with each other, hoping to make as many high affinity EBs as possible. We achieve this goal by first introducing a greedy interaction between two adjacent EBs. Since the union and intersection of elements of two EBs can form EBs. So, we have:

###### Definition 3

Let $`𝒞_1𝒞_2`$ be two adjacent EBs, and we abuse the notation a bit by writing $`𝒞_1`$ and $`𝒞_2`$ both as sequences and sets. Then, we define $`I(𝒞_1𝒞_2)=𝒟_1𝒟_2`$ where $`𝒟_1`$ is the EB formed by the set $`𝒞_1𝒞_2`$, and $`𝒟_2`$ is the (possibly empty) EB formed by the elements in $`𝒞_1𝒞_2`$. In case $`𝒟_1𝒟_2`$ equals $`𝒞_1𝒞_2`$ or $`𝒞_2𝒞_1`$, then we said that $`𝒞_1`$ interacts elastically with $`𝒞_2`$. Otherwise, we said that $`𝒞_1`$ interacts inelastically with $`𝒞_2`$.

From the above definition, readers can verify that $`I(2301)=0123`$, $`I(02123)=01232`$, $`I(01201)=01201`$ and $`I(0101)=0101`$. We also observe that

###### Lemma 2

The EB $`𝒟_1`$ defined above is the highest possible EB formed from $`𝒞_1𝒞_2`$ with respected to the hlex order. Furthermore, $`𝒟_2_{\mathrm{hlex}}𝒟_1`$.

Proof: Since all elements in an EB are distinct, so the highest possible affinity EB is formed by selecting all the elements in $`𝒞_1𝒞_2`$. In addition, $`𝒟_2_{\mathrm{hlex}}𝒟_1`$ follows directly from the fact that $`𝒞_1𝒞_2𝒞_1𝒞_2`$. $`\mathrm{}`$

Since $`𝒟_1`$ is the highest possible affinity EB constructed out of $`𝒞_1`$ and $`𝒞_2`$, the greedy interaction is an effective way to construct high affinity while keeping the density $`\rho _i`$ of a configuration. Now, we report two technical lemmas before going on.

###### Lemma 3

Let $`\alpha `$ be a one-dimensional configuration with $`N`$ sites. Suppose there exist two EBs in $`\alpha `$ which can interact inelastically with each other, then the total number of EBs in $`\alpha `$ is less than or equal to $`N2`$. Besides, this bound is tight.

Proof: Suppose $`\alpha `$ contains $`N`$ EBs, then from Definition 1, it is easy to see that states in all the $`N`$ sites must be identical. Hence, none of the EB interacts inelastically with each other. Similarly, if $`\alpha `$ contains $`N1`$ EBs, then exactly $`N2`$ of them contains one element and one of them contains two elements. Since periodic boundary conditions applies to the $`N`$ sites, again from Definition 1, we know that all the $`N2`$ singleton EBs must be in an identical state, say $`a`$. Suppose the remaining EB is in state $`bc`$, then $`bac`$. (Otherwise, we can extend the size of the EB $`bc`$ by one, contradicting the maximality of an EB.) Now, it is easy to check that $`a`$ and $`bc`$ interact elastically. Thus, an inelastically interacting configuration must contain less than $`N1`$ EBs. Finally, the tightness of this bound is revealed by the configuration $`020100000`$. $`\mathrm{}`$

###### Lemma 4

Let $`\alpha `$ be a configuration whose EB only interacts elastically with each other. Then $`\alpha \mathrm{\Omega }_i`$ if and only if $`_i`$ is an EB in $`\alpha `$.

Proof: From Definition 3, two EBs interacts elastically if and only if one is a subset of the other. Hence, if the EBs in $`\alpha `$ interact elastically, we can always arrange them sequentially so that one is a subset of the next. Clearly, the most frequently occurring state(s) are the ones in the first element of this sequence $`𝒞`$. Thus, $`\alpha \mathrm{\Omega }_i`$ if and only if $`i`$ is the only element in $`𝒞`$. Hence, our assertion is proved. $`\mathrm{}`$

At this point, we have introduced enough material to present our first CA rule for the $`n`$ary density classification problem. We express a configuration $`\alpha `$ in terms of its EBs $`𝒞_1𝒞_2\mathrm{}𝒞_j`$. Inspired by the traffic rule used in Ref. , we define the basic rules $`𝐑_i`$ for $`i=0,1,\mathrm{},2^n2`$ as follows: Each EB $`𝒞_k\alpha `$ with $`𝒞_k=_i`$ will interact with its forward neighboring EB $`𝒞_{k+1}`$ if and only if $`𝒞_{k+1}_i`$. Besides, all interactions are taken in parallel. For instance, $`𝐑_1(1021101)=0121011`$. Clearly, $`𝐑_i`$ conserves $`\rho _j`$. More importantly, it is straight-forward to check that $`𝐑_i`$ is equivalent to the following rules: (1) If $`𝒞_{k1}`$, $`𝒞_k_i`$ or $`𝒞_k`$, $`𝒞_{k+1}=_i`$, then the states of the sites occupied by $`𝒞_k`$ remain unchanged; (2) Otherwise, if $`𝒞_{k1}=_i`$, then the states of the sites occupied by $`𝒞_k`$ become the last $`\mathrm{}(𝒞_k)`$ states of $`I(𝒞_{k1}𝒞_k)`$ where $`\mathrm{}(𝒞_k)`$ denotes the length of the sequence $`𝒞_k`$; (3) For the remaining case that $`𝒞_{k+1}_i`$, then the states of the sites occupied by $`𝒞_k`$ become the first $`\mathrm{}(𝒞_k)`$ states of $`I(𝒞_k𝒞_{k+1})`$. Hence, $`𝐑_i`$ is a CA rule with radius $`n+\mathrm{}(_i)1`$.

Since finite composition of CA rules is also a CA rule, we have:

###### Definition 4

We write the affinity rule $`𝐀=𝐑_0\left(𝐑_0𝐑_1\right)`$ $`\left(𝐑_0𝐑_1𝐑_2\right)`$ $`\mathrm{}\left(𝐑_0𝐑_1\mathrm{}𝐑_{2^n2}\right)`$. (That is, a total of $`(2^{n1}1)(2^n3)`$ terms in the above composition.) Then, the CA rule $`𝐀`$ preserves $`\rho _i`$ and has a radius between $`n(2^{n1}1)(2^n3)`$ and $`(2n1)(2^{n1}3)(2^n3)`$.

Note that the mobility of an EB increases with decreasing affinity, so the spirit of our affinity rule is to aggressively produce a much high affinity EBs as we can. In fact, we find that

###### Lemma 5

The total number of EBs of a configuration $`\alpha `$ is greater than or equal to that of $`𝐀(\alpha )`$. Besides, under the repeated action of $`𝐀`$, the number of EBs in a configuration will eventually stay constant.

Proof: The first part of the lemma follows directly from Definitions 3 and 4. Since $`N`$ is finite, the total number of EBs for any configuration must lie between 1 and $`N`$. Since the number of EBs is a decreasing function of $`𝐀`$ and $`n`$ is finite, so under the repeated action of $`𝐀`$, the number of EBs in a configuration will eventually stay constant. $`\mathrm{}`$

With all the preparation works above, we have confident to use $`𝐀`$ as our first CA rule in the $`n`$ary density classification problem. The power of this CA rule is apparent from the Theorem below.

###### Theorem 1

Let $`\alpha `$ be a configuration in a one-dimensional chain of $`N`$ sites. Then $`\alpha \mathrm{\Omega }_i`$ if and only if $`𝐀^{N2}(\alpha )`$ contains an EB $`_i`$.

Proof: By Lemma 4, we only need to prove that for any configuration $`\alpha `$, $`𝐀^{N3}(\alpha )`$ does not contain inelastically interacting EBs. In addition, we may assume that $`\alpha `$ contains at least a pair of inelastically interacting EBs. Otherwise, our assertion is trivially true.

We follow the motion of an arbitrarily chosen EB under the action repeated of $`𝐀`$. After the EB interacts with another one, then we turn to follow the motion of the left resultant EB. Besides, we turn to follow the motion of the left neighboring EB in case the left neighboring EB equals to that of our current EB. So, Lemma 2 tells us that the affinity of the EB we are following increases with time. In addition, Definition 4 tells us that EBs with different affinity interact at a different rate. More importantly, by direct checking, one sees that our monitoring EB must interact with the left neighboring EB under the action of $`𝐀`$. Besides, under the action of $`𝐀`$, two distance EBs can interact only when one of them first hops through all the EBs separating them by elastic interactions. Now, we denote the highest affinity EB under our monitoring scheme in $`𝐀^{N3}(\alpha )`$ by $`𝒞_1`$, then from Lemmas 2, 3 and 5, all EBs in $`𝐀^{N3}(\alpha )`$ are subsets of $`𝒞_2`$.

Now, we trace the motion of an arbitrarily chosen EBs in exactly the same way as before except that when the tracing EB interacts with the EB that leads to $`𝒞_1`$ on its way, then we turn to trace the motion of resultant right EB. Now, we write $`𝒞_2`$ as the highest affinity EB under this new monitoring scheme in $`𝐀^{N3}(\alpha )`$. Then, using the same argument as before, it is clear that all EBs in $`𝐀^{N3}(\alpha )`$ but $`𝒞_1`$ are subsets of $`𝒞_2`$. Besides, $`𝒞_2𝒞_1`$. Inductively, by tracing the motion of EBs in a similar way that leads to $`𝒞_1`$ and $`𝒞_2`$, we conclude that the EBs in $`𝐀^{N3}(\alpha )`$ interact elastically. Hence, by Lemma 4, this theorem is proved. $`\mathrm{}`$

With the help of Theorem 1, the $`n`$ary density classification problem using two CAs can be easily executed.

###### Definition 5

We express a configuration as a collection of EBs. For any three consecutive EBs $`𝒞_1𝒞_2𝒞_3`$, we define the propagation rule $`𝐏`$ as follows: The states of sites in $`𝒞_2`$ remain unchanged if none of the three blocks $`𝒞_1`$, $`𝒞_2`$ and $`𝒞_3`$ equals $`_i`$ for all $`0i<n`$. Otherwise, we set the state of sites in $`𝒞_2`$ to $`i`$ where $`i`$ is the state of the minimum affinity EB among $`𝒞_1`$, $`𝒞_2`$ and $`𝒞_3`$.

Since we can only find at most one type of $`_i`$ ($`0i<n`$) in $`𝐀^{N3}(\alpha )`$, repeatedly applying the propagation rule $`𝐏`$ to $`𝐀^{N3}(\alpha )`$ results in propagating that particular EB $`_i`$. In addition, similar to the affinity rule, it is straight-forward to show that $`𝐏`$ is a CA rule with a radius $`2n1`$. Besides, we have

###### Theorem 2

If $`\alpha `$ is a configuration whose EB only interacts elastically, then applying $`𝐏`$ $`\frac{N1}{2}/2`$ times to $`\alpha `$ will result in having all $`i`$s in the configuration if and only if $`\alpha \mathrm{\Omega }_i`$.

Proof: Lemma 4 tells us that $`\alpha `$ contains the EB $`_i`$ if and only if $`\alpha \mathrm{\Omega }_i`$ for $`0i<n`$. That is, we can find at most one type of EB $`_i`$ for $`0i<n`$ in $`\alpha `$. Clearly, the worst case occurs when $`\alpha `$ contains exact one such $`_i`$. Besides, all other EBs in $`\alpha `$ are of length two. In this case, it is clear that applying $`𝐏`$ $`\frac{N1}{2}/2`$ times result in converting all sites to state $`i`$. Hence it is proved. $`\mathrm{}`$

In summary, we show that it is impossible to solve the $`n`$ary density classification problem in any dimension using one CA. Nevertheless, we can solve this problem by first applying the affinity rule $`𝐀`$ (with a radius between $`n(2^{n1}1)(2^n3)`$ and $`(2n1)(2^{n1}1)(2^n3)`$ ) $`N3`$ times and then followed by the propagation rule $`𝐏`$ (with a radius of $`2n1`$) $`\frac{N1}{2}/2`$ times. Since the run time of the two CA rules scales as $`\text{O}(N)`$, so apart from a constant speed up, our two CA $`n`$ary density classification rules are optimal. In other words, the dynamics of our two CA rules gives rise to $`n`$ stable fixed points each with configuration $`i^N`$. Moreover, we find a large number of unstable fixed points whenever there is more than one most frequently occurring state in a configuration. Finally, we remark that these CA rules are not unique and other equally good method exists .

It is instructive to extend the $`n`$ary density classification problem to rational function classification similar to that reported in Ref.  as well as to higher dimensional lattices. We plan to report these results in future.

###### Acknowledgements.

We would like to thank Jessica H. Y. Chan for her valuable discussions and P. M. Hui for pushing us to investigate this problem.