# Cellular Automata/Counting Preimages

Preimages are configurations in the past that lead in one step to the present configuration.

## Reversing the direction of time

The local transition function and the global transition function define the evolution of cellular automata in the forward time direction. To calculate preimages from the present configuration, inverses of the forward mappings must be defined.
The preimages of a single cell $c_{x}^{t}$ are the locally valid neighborhoods $n_{x}^{t-1}$ defined by the inverse of the local transition function
    $f^{-1}(c_{x}^{t})=\{n_{x}^{t-1}\in S^{N}\;|\;f(n_{x}^{t-1})=c_{x}^{t}\}$
Preimages $C^{t-1}$ of a configuration $C^{t}$ are defined by the inverse of the global transition function
    $F^{-1}(C^{t})=\{C^{t-1}\in S^{Z}\;|\;f(C^{t-1})=C^{t}\}$
Locally valid neighborhoods of adjacent cells must overlap correctly to become globally valid. De Bruijn diagrams describe how sequences can overlap an thus provide a method to calculate global inverses knowing the local inverse.
The number $p$ of preimages $C^{t-1}$ can vary from none to many, depending on the rule and the present configuration $C^{t}$, arising questions about the injectivity, surjectivity and reversibility of the rule.

## De Bruijn diagrams

De Bruijn diagrams come from the theory describing shift registers. Its nodes are strings $w$ of symbols over some alphabet, usually all strings of a fixed length. Its directed links describe how this strings overlap if one of them is shifted. Here only shifts of a single cell are described.
There is a link from a source node $w_{s}=a\alpha$ to a drain node $w_{d}=\beta b$ if string $w_{s}$ overlap with string $o_{R}$ shifted right by one symbol. This means that the overlapping source substring $\alpha$ ($w_{s}$ without the start symbol $a$) is equal to the overlapping drain substring $\beta$ ($w_{d}$ without the end symbol $b$).
The topological matrix of the De Bruijn diagram is
    $d_{w_{s}w_{d}}={\begin{cases}1,&\alpha =\beta \\0,&\alpha \neq \beta \end{cases}}$
For the diagram to be easier to read and use on cellular automata this book uses a diagram representation, where all the nodes are drawn twice. Nodes on the left are source points of links and nodes on the right are drain points of links. The direction of the links is chosen to point in the increasing direction of cell position indexes.

### See also

* There is more about De Bruijn diagrams in references.

## Preimage diagrams and matrices
### Overlapping of adjacent neighborhoods

The overlap $o_{i}$ is the group cells from the overlapping neighborhoods of a pair of adjacent cells $c_{x}c_{x+1}$. The size of the overlapping $k-1$ is one cell less than the size of the neighborhood.
    $o_{x}=c_{x+1-k_{0}}c_{x+2-k_{0}}\dots c_{x+k-k_{0}-1}$
A compact representation of the overlap is a number with $k-1$ digits base $|S|$.
    $o_{x}=\sum _{i=0}^{k-2}{c_{x+1-k_{0}+i}|S|^{k-2+i}}=c_{x+1-k_{0}}|S|^{k-2}+c_{x+2-k_{0}}|S|^{k-2}+\dots +c_{x+k-k_{0}-1}|S|^{0}$
If a cell sequence $...c_{x-2}c_{x-1}c_{x}c_{x+1}c_{x+2}c_{x+3}...$ is cut (or joined) into two parts between $c_{x}$ and $c_{x+1}$, the neighborhood of the last cell in the left part $...c_{x-2}c_{x-1}c_{x}$ is overlapping with the neighborhood of the first cell on the right part $c_{x+1}c_{x+2}c_{x+3}...$. The overlap $o_{x}$ at the cut (or junction) is defined as above and is used to describe the boundaries of sequences.
Example:
Overlapping of neighborhoods in rule 110

### Preimage matrix

The first step to the preimage matrix is constructing a De Bruijn diagram representing preimages of a single cell in a cellular automaton. Its nodes are all $|S|^{k-1}$ different overlaps of neighborhoods. The source nodes $o_{L}$ are overlaps on the left side of the cell and the drain nodes $o_{R}$ are overlaps on the right side of the cell. The links represent neighborhoods $n$ and connect nodes $o_{L}$ and $o_{R}$ according to the next two decompositions:
* the neighborhood $n=c_{L}o_{R}$ is formed from the remaining left cell $c_{L}$ and the right overlap $o_{R}$ or
* the neighborhood $n=o_{L}c_{R}$ is formed from the left overlap $o_{L}$ and the remaining right cell $c_{R}$
The topological matrix of the diagram is a square of $|S|^{k-1}\times |S|^{k-1}$ elements.
    $D=\left[{\begin{matrix}d_{00}&d_{01}&\cdots \\d_{10}&d_{11}&\cdots \\\vdots &\vdots &\ddots \end{matrix}}\right]$
The value of an element is $1$ if there is a link between nodes $o_{L}o_{R}$ and $0$ else.
    $d_{o_{L}o_{R}}={\begin{cases}1,&o_{L}c_{R}=c_{L}o_{R}=n\\0,&{\mbox{else}}\end{cases}}$
The next step is to form a symbolic De Bruijn matrix, where elements are link labels. A link representing the neighborhood $n$ is labeled according to the output cell value $c=f(n)$ defined by the local transition function. Node pairs without a link can be labeled with a dot.
    $d_{o_{L}o_{R}}={\begin{cases}f(n),&o_{L}c_{R}=c_{L}o_{R}=n\\.,&{\mbox{else}}\end{cases}}$
The last step is to form preimage matrices $D(c)$ one for each of the $|S|$ available cell states $c$. There is a link between a pair of nodes $o_{L}o_{R}$ only if the relative neighborhood $n$ leads to the desired cell value $c$.
    $d_{o_{L}o_{R}}(c)={\begin{cases}1,&o_{L}c_{R}=c_{L}o_{R}=n\wedge f(n)=c\\0,&{\mbox{else}}\end{cases}}$
Example:
De Bruijn and preimage diagrams in rule 110

### The preimage matrix of a sequence

Before the definition of the preimage matrix can be extended to a sequence of cells $\alpha$ the meaning of the matrix elements must be defined, so the definition can be checked against it.
Entries $d_{o_{L}o_{R}}$ in the matrix $D(\alpha )$ represent the number of preimages of length $|\alpha |+k-1$ that begin with the left overlapping $o_{L}$ and end with the right overlapping $o_{R}$.
    $\alpha ^{t-1}=o_{L}\beta _{R}=\beta _{L}o_{R}\;\wedge \;F(\alpha ^{t-1})=\alpha ^{t}\quad \Rightarrow \quad |F^{-1}(\alpha ^{t})|=d_{o_{L}o_{R}}(\alpha ^{t})$
The preimage matrix of a string of cells $\alpha =c_{p}c_{p+1}\dots c_{q}$ is the multiplied chain of single cell matrices
    $D(\alpha )=D(c_{p}c_{p+1}\dots c_{q})=\prod _{x=p}^{q}D(c_{x})=D(c_{p})\cdot D(c_{p+1})\cdots D(c_{q})$
The preimage matrix of an empty string $\varepsilon$ is an identity matrix.
    $D(\varepsilon )=I$
Proof: Proof of the preimage matrix equation for sequences $\Box$

### Preimage boundary conditions

The preimage vector consists of $|S|^{k-1}$ nonnegative integer entries, one for each of the possible neighborhood overlaps or nodes of the preimage diagram.
Elements $p_{i}$ of the preimage vector $b_{x}$ count the preimages that contain an overlap $o_{x}=i$ of value $i$ at position $x$ in the present cell sequence.
    $b_{x}=[p_{0},p_{1},\dots ,p_{i-1},p_{i},p_{i+1},\dots ,p_{|S|^{k-1}-1}]$
The preimage vector is used to describe the boundary or more precisely it can describe the preimage count at one side of a junction of two strings.
* to describe the right boundary of the string $\alpha =\dots c_{x-1}c_{x}$ the right boundary vector $b_{R}$ is used, it counts preimages to the right from the junction
* to describe the left boundary of the string $\alpha =c_{x+1}c_{x+2}\dots$ the left boundary vector $b_{L}$ is used, it counts preimages to the left from the junction
The unrestricted boundary is usually used to avoid any specific boundary. It is assumed that there is exactly one preimage for each overlap. All the entries in the vector are $1$.
    $b_{u}=[1,1,\dots ,1]\qquad |b_{u}|=|S|^{k-1}$
Since there can be an infinite number of preimage for a semi-infinite sequence, usually only periodic sequences are used, counting only preimages of the period $\alpha$
    $b_{L}=b_{u}D(\alpha )\,$
    $b_{R}=D(\alpha )b_{u}^{T}$
Quiescent and ether backgrounds can be represented by periodic sequences.

#### See also

* Cellular Automata/Boundary Conditions for more details on boundary conditions
Example:
Some common boundary conditions in rule 110

### Counting preimages

The number of preimages $p$ (paths through the network) of a sequence $\alpha$ with the left boundary condition $b_{L}$ and the right boundary condition $b_{R}$ is defined as
    $p=b_{L}D(\alpha )b_{R}^{T}$
The unrestricted boundary is commonly used and it counts all the preimages of a sequence $D(\alpha )$ exactly once. The number of preimages is simply the sum of all elements in the preimage matrix $D(\alpha )$.
    $p=b_{u}D(\alpha )b_{u}^{T}\,$

#### Preimages of a cyclic string of cells

Since a string in a cyclic lattice must have the same overlapping at its left and right side, only elements on the diagonal of the De Bruijn matrix form valid preimages. The number of all preimages of a string $\alpha$ is the sum of elements on the diagonal.
    $p=\sum _{i=0}^{|S|^{k-1}}d_{ii}(\alpha )$

#### Garden of eden

A garden of eden sequence does not have preimages. For finite strings on an infinite lattice this is true exactly when the De Bruijn matrix of the string is a zero matrix $M_{0}$ (all elements are zero).
    $D(\alpha )=M_{0}\Rightarrow \alpha \in G$
Any string which substring is a garden of eden is a garden of eden.
    $\forall \beta _{L},\beta _{R}\;[\alpha \in G\Rightarrow \beta _{L}\alpha \beta _{R}\in G]$
A garden of eden can be the consequence of the boundary conditions, both restricted of cyclic boundaries.
Example:
Garden of eden sequences in rule 110

## Proofs
### Proof of the preimage matrix equation for sequences

The proof of the formula on sequences is an induction on the length of the string.

#### Base ($l=0,1$)

    If the length of the string is $0$ than there is no shift and nodes are linked only to themselves.
    For the string of length $1$ the meaning of the single cell preimage matrix elements is evident from the definition.

#### Induction

    $D(\alpha a)=D(\alpha )D(a)\,$

## References

* Harold V. McIntosh, Linear Cellular Automata Via de Bruijn Diagrams, August 10, 1991 (HTML, PDF)
* Harold V. McIntosh, Ancestors: Commentaries on The Global Dynamics of Cellular Automata by Andrew Wuensche and Mike Lesser (Addison-Wesley, 1992), July 20, 1993 (HTML, PDF)
* Erica Jen, Enumeration of Preimages in Cellular Automata, Complex Systems 3 (5) (1989) 421-456
* Burton Voorhees, Predecessors of cellular automata states II. Pre-images of finite sequences, Phisica D 73 (1-2) (1994) 136-151
* Iztok Jeras, Andrej Dobnikar Algorithms for computing preimages of cellular automata configurations PDF

## Software

* DDLab Tools for researching Cellular Automata, Random Boolean Networks, multi-value Discrete Dynamical Networks, and beyond; by Andy Wuensche.
* Iztok Jeras, Algorithms for computing preimages of cellular automata configurations TAR.BZ2
* Cellular Automata Pre-Image Generator CAPIG is a user-friendly free software made to find pre-images according to the user chosen configuration.