Algorithm for Identification of Infinite Clusters Based on Minimal Finite Automaton

We propose a finite automaton based algorithm for identification of infinite clusters in a 2D rectangular lattice with L = X × Y cells. The algorithm counts infinite clusters and finds one path per infinite cluster in a single pass of the finite automaton. The finite automaton is minimal according to the number of states among all the automata that perform such task.The correctness and efficiency of the algorithm are demonstrated on a planar percolation problem. The algorithm has a computational complexity of O(L) and could be appropriate for efficient data flow implementation.


Introduction
After the publication of Shannon's paper in 1951 [1], many scientists considered the behavior of systems of finite automata (FAs) in labyrinths, for example, in connected sets of cells of a grid or lattice.An overview of the obtained results is given in [2,3].Some problems of finite automaton (FA) traversing labyrinths can be found in [4,5].Many results indicate a limited power of FA.But, by introducing a collective of FAs, it was proved that there exists a collective of two FAs which can traverse all planar mosaic finite labyrinths [6].Also, in [7,8], the problem of FA recognition of some infinite classes of labyrinths (e.g., digits and letters) is studied.
Recognizing and labeling connected regions in binary images are important problems in image processing, machine vision, porous matter analysis, and many other fields of science.Studying site and bond percolation on any lattice is an important problem in computational physics [9][10][11][12].Some algorithms from these studies are presented in [13][14][15][16][17][18].In [19][20][21], polymerizing systems are presented, with branched or cross-linked polymers, which may go through distinct gelation transitions; a formed gel is seen as a twodimensional lattice-percolation cluster and the authors use a "hull-generating walk" (HGW) algorithm.
The similarity between the papers [19][20][21] and the present paper consists in the fact that here we use a similar algorithm (in fact, HGW algorithm and the algorithm from our paper are in the class of the so-called "maze solving algorithms" [22]).But in contrast to papers [19][20][21], where HGW algorithm is used for generating perimeters of clusters in a 2D random binary grid, we use our algorithm for identifying and counting infinite clusters in such grid.This is a new, purely mathematical approach, in terms of the theory of finite automata, to the problem considered in our paper.It consists in the construction of a minimal finite automaton (FA) which can traverse the border of any cluster on any 2D binary grid and always stops in the planar site percolation model.
The planar site percolation model is represented by a 2D grid of square cells and each cell can exist in two different states, white or black.Any cell is colored black with probability  and white with probability 1 − .These probabilities are independent for each cell.We use open boundary conditions with boundary cells on the top and on the bottom of the grid in different states, blue and red, respectively.Cells on the left and right boundaries are white.
We construct an initial FA A  0 which can traverse the border of any cluster in every planar 2D grid in accordance with the left-hand rule (which is also a kind of a HGW Mathematical Problems in Engineering algorithm).The automaton A  0 can be launched from any cell of a grid.In any moment, the input of A  0 is information about von Neumann neighborhood of the present cell given in the form of a 5-tuple representing the state of the central cell and the states of its four neighbors.In the next moment, depending on its present state and its input, the FA proceeds from the present cell in one of the directions, defined by the output symbols , , , or  (denoting the corresponding directions, east, north, west, or south, resp.), and moves into a corresponding neighbor cell or stays at the same place if output symbol is 0 (which means "no move") and passes into a new state.On the bases of this automaton, we give an algorithm which can be used for finding/identifying and counting infinite clusters in the given grid.Such algorithm is simple to implement and does not need extra memory or stacks.The time complexity of the algorithm is linear and not larger than the time complexity of already existing algorithms.The FA has only four states, including the initial state.We have proved that this is the minimal FA according to the number of states.The paper is organized as follows.In Section 2, general grid-related definitions and definitions of the FA and its properties are given.In Section 3, the FA-based algorithm for counting connected paths is described and its complexity is proved.The main conclusions and results are summarized in Section 4.

General Definitions.
The planar site percolation model (see Figure 1) is represented by a two-dimensional lattice network (grid) of unit squares (cells) whose centers are in the integer lattice with  = × cells, ,  ≥ 4. The cell position is determined by its center.Each cell can exist in two different states, 0 or 1, where state 0 is usually called "white" and state 1 is usually called "black."The boundary cells of the simulated grid are in a fixed state, which remains constant throughout the simulation.The boundary cells on the bottom edge of the grid are in state 2, which is called "red," and the boundary cells on the top edge of the grid are in state 3, which is called "blue."The boundary cells on the left and right edge of the grid are white.State 4, which is called "orange," is used for the notation of labeled cells in the grid.
Two different cells ( 1 ,  1 ) and ( 2 ,  2 ) from a grid are adjacent (weakly adjacent . Two black (white) cells ( 1 ,  1 ) and (  ,   ) are connected if there exists a sequence of black (white) cells (  ,   ), 2 ≤  ≤ , such that each pair ( −1 ,  −1 ) and (  ,   ) are adjacent; such sequence is called a path.Blue-red path is a path whose one end is adjacent to a blue and the other to a red cell.A set of cells is connected if any two cells from the set are connected.A maximum connected set of black cells is called a cluster.The infinite cluster is a cluster that contains a blue-red path.
Let  = (, , , ) be a cyclic permutation.For  ⊆ ,  ̸ = 0, and  ∈ , denote by   () the element   0 (), where  0 = min{ ∈ N |   () ∈ }, where N is the set of natural numbers.By a finite automaton (FA) A, we mean a quintuple (, , , , ), where the finite nonempty sets , , and  are the input alphabet, the set of states, and the output alphabet of the automaton, respectively;  :  ×  →  is its output function; and  : × →  is its state-transition function.If we mark a state  0 ∈  as an initial state of A, we get an initial automaton A  0 = (, , , , ,  0 ) (in other words, A  0 is a Mealy machine).
Further, we consider the initial FA A   = (, , , , ,   ) supposing that  = {0, 1, 2, 3, 4} 5 ,  = {  ,   ,   ,   }, and  = {0, , , , }.The starting cell of this automaton in a grid is always the lowest right corner cell of the grid.If the output symbol of A   is  ∈ {, , , }, we say that FA proceeds in the direction .If the output symbol is 0, the FA stops.This way, FA simulates movements (goes around, traverses) through a grid.
The FA starts in state   and uses states from set  until it finds a boundary cell on the top of the grid (blue one).In this moment, the algorithm increments the counter of infinite clusters and starts labeling cells with an orange color.The algorithm labels a path, until the FA finds a boundary cell on the bottom edge of the grid (red one).After finding the lowest left end of an infinite cluster, the FA proceeds in the  (west) direction and it is on a white cell and in state   .After that, the algorithm breaks labeling and starts with new counting of infinite clusters and the FA repeats the process of traversing.The algorithm stops when the output of the FA is 0.
Notice two properties of the FA A   : (1) In the initial state, the FA is on the cell that has red cell on the south ( (,−1) = 2).The FA goes in the west direction until it finds the black cell with black cell on the north.If it cannot find it, the FA stops.
(2) The meaning of the fact that the FA is on the cell  and is in the state   is that the FA has arrived on the cell  from the direction ,  ∈  (see Figure 3).
Choosing the cyclic permutation  in the definition of the FA A   , we define an orientation.Namely, if the FA comes on the cell  from direction  ∈ , then the right, ahead, left, and behind cell (from the cell ) are  + (),  +  2 (),  +  3 (), and  +  4 () = , respectively.
Let  be a blue-red path in the grid  0 and   be the infinite cluster that encloses path .Let   ∈   be the lowest right cell in the cluster   and   be the subset of the infinite hole which is on the right side of the cluster.Right boundary of the cluster   ,    = { ∈   |  is weakly adjacent to some cell from   }, is connected and encloses a blue-red path.Notice that   ∈    .
It is not hard to see that the FA A   implements "left-hand-on-wall" algorithm ( [23]).Starting from the cell q s q n q w   , the FA traverses    , and the subsequence (  (0) =   ,   (1), . . .,   ()), for some  ∈ N, of the sequence (A   , ( 0 , (0))) creates a blue-red path.If there is not a connected path between red and blue cells in the grid  0 , then, for all cells  ∈   , the sequence ( =  0 ,  1 , . . .,   , . ..),  ≥ 0, will not contain a cell with blue north neighbor.The grid is finite and the FA will stop on the lowest left cell of the grid.Theorem 2. According to the number of states, the FA A   is the minimal initial finite automaton.
In the example from Figure 1(b), notice cell .It is in the infinite cluster and it is weakly adjacent by the right infinite hole.Traversing the cluster by the left-hand rule, the FA A   is four times in the cell  (from south, east, north, and west sides).All the visits must be done in different automaton states (input is the same).Otherwise, the FA makes a cycle.Hence, for traversing a border of a cluster, it is necessary to have 4 states.

The FA Traversing Algorithm and Its Time Complexity
The algorithm for identification of infinite clusters is based on the FA A   .To ensure appropriate counting of infinite clusters, the algorithm uses three global variables: count, labeling, and countingCluster.The count counts the number of infinite clusters, labeling is an indicator for labeling cells, and countingCluster is an indicator for the counting of the infinite cluster.
Let ( 0 , (0)) be an initial grid, where  0 is a grid and (0) is its lowest right corner.Let   be the initial state of the FA A   .The algorithm is presented as Algorithm 1.

Figure 1 :
Figure 1: Examples of percolation site models on 13 × 13 grid, generated with probability  = 0.55 without infinite cluster (a) and generated with probability  = 0.60 with infinite cluster (b).

Figure 4 :
Figure 4: The FA traversing algorithm for the two examples from Figure 1.

Figure 5 :
Figure 5: The FA traversing algorithm finds two infinite clusters in a grid of dimensions 201 × 201.