4-way finite automata
recognizing
families of rectangles
Jarkko Kari
Mathematics Department, University of Turku, Finland
In collaboration with Prof. Cristopher Moore
Two-dimensional words — or pictures — are rectangular arrays of symbols
over a finite alphabet. A set of pictures is called a picture language.
In this work we consider picture languages that are accepted by
4-way finite automata. Such devices were first defined by M.Blum and
C.Hewitt in 1967. The finite control of the automaton moves inside the
picture, scanning the symbols. Depending on the internal state and the
current input symbol the automaton may change the internal state and
move left, right, up or down.
The transitions of the automaton may be deterministic (4-DFA) or
non-deterministic (4-NFA).
The automaton is not allowed to move outside the input picture.
We guarantee this property by letting the automaton sense when it is
located on a border cell, and to use different transition rules on the borders
and corners. These rules are not permitted to include a move that would
take the automaton outside the picture.
In the beginning of the computation, the automaton is located in the lower
left corner of the picture, in an initial state. The picture is accepted if the
automaton can reach an accepting state. If needed, we may assume
(without loss of generality) that the accepting state is unique, and the
automaton enters the accepting state only at the lower left corner.
Example. Let Σ = { ,
}, and consider the language of squares of odd
size whose middle cell is black, e.g. picture
Example. Let Σ = { ,
}, and consider the language of squares of odd
size whose middle cell is black, e.g. picture
#
A 4-way NFA to accept this language does the following operations:
(1) Move diagonally (using right-step/up-step combinations) from the lower
left corner to the upper right corner to verify that the input rectangle is a
square. Also verify that the length of the diagonal is odd.
Example. Let Σ = { ,
}, and consider the language of squares of odd
size whose middle cell is black, e.g. picture
#
A 4-way NFA to accept this language does the following operations:
(2) Move back along the diagonal. On some black cell on the diagonal
make a non-deterministic guess that the cell is the center cell. Verify the
correctness of the guess by following the other diagonal to the lower right
corner.
Example. Let Σ = { ,
}, and consider the language of squares of odd
size whose middle cell is black, e.g. picture
A counting argument was made by Rosenfeld to show that the previous
language cannot be accepted by any deterministic 4-way automaton,
proving
Theorem 4-way DFA are strictly less powerful than 4-way NFA.
Consider the unary case when there is only one letter in the input
alphabet. Then pictures are unmarked rectangles. The following 4-way
NFA A shows that 4-way NFA are more powerful than 4-way DFA even in
the unary case.
1
0
1
0
#
The NFA A moves diagonally from left to right, ”bouncing” from the top
and the bottom walls of the rectangle, until it reaches the last column
where it stops. Whenever the automaton ”bounces” it may
non-deterministically make one additional step to the right. The rectangle
is accepted iff the automaton reaches a right corner of the rectangle.
1
0
0
1
#
If a rectangle of width W is accepted by a computation that consists of n
immediate bounces and m ”delayed” bounces, then
W = 1 + n(H − 1) + mH
where H is the height of the rectangle. For any fixed height H there are
then only a finite number of widths W that can not be accepted. The
longest such width is
(H − 1)(H − 2)
i.e. quadratic in H.
1
0
0
1
#
Next we show that the complement of the unary picture language above
is not accepted by any 4-way NFA. To prove this, we turn to 2-way finite
automata that accept classical one-dimensional words.
If we fix the height H then any s-state 4-way NFA can be simulated by a
2-way, one-dimensional NFA with s · H states, where each state is a pair
(q, i) recording the state of the original 4-way automaton and the vertical
position of the automaton in the picture:
q
2D:
1D:
1
0
0
1
0
1
0
1
1
0
(q,i)
row i
This correspondence gives the following simple lemma:
Lemma Let A be a 4-way NFA over unary alphabet, and let A have s
states. Then for any height H the one-dimensional language
{1W | A accepts the H × W rectangle }
of corresponding widths is accepted by an sH-state 2-way NFA.
This correspondence gives the following simple lemma:
Lemma Let A be a 4-way NFA over unary alphabet, and let A have s
states. Then for any height H the one-dimensional language
{1W | A accepts the H × W rectangle }
of corresponding widths is accepted by an sH-state 2-way NFA.
In one-dimensional case we know that the longest word of a finite language
cannot be (much) longer than the number of states in the automaton
accepting the language:
Lemma If L is a finite language recognized by a 2-way NFA with k states
then L does not contain any words longer than k + 2.
Combining the two lemmas we have:
Theorem Let A be an s-state, 4-way NFA over unary alphabet. For any
H, if A accepts only a finite number of rectangles of height H, then A
cannot accept a rectangle of height H and width greater than sH + 2.
Combining the two lemmas we have:
Theorem Let A be an s-state, 4-way NFA over unary alphabet. For any
H, if A accepts only a finite number of rectangles of height H, then A
cannot accept a rectangle of height H and width greater than sH + 2.
Let us return to the picture language accepted by the bouncing 4-way
NFA. The complement of that language has the property that for every
height H there are only a finite number of widths W such that the H × W
rectangle is in the complement, and the largest such width is
(H − 1)(H − 2).
Assume there is a 4-way NFA accepting the complement with s states. If
we choose H large enough we have
(H − 1)(H − 2) > sH + 2,
contradicting the previous theorem. We conclude that the complement is
not accepted by any 4-way NFA.
We have proved:
Theorem 4-way NFA are not closed under complementation. Not even in
the case of the unary alphabet.
We have proved:
Theorem 4-way NFA are not closed under complementation. Not even in
the case of the unary alphabet.
There is a very nice argument due to Sipser (1980) that shows that 4-way
DFA are closed under complementation. Because the complement of the
”bouncing” language is not accepted by a DFA, we also have the corollary
that our ”bouncing” language is not accepted by any 4-way DFA:
Theorem 4-way NFA are strictly more powerful than the 4-way DFA even
in the case of the unary alphabet.
Since we use Sipser’s argument later, let us recall it: The idea is that we
can replace any 4-way DFA A by an equivalent 4-way DFA B that has the
additional property that it halts with every input. Then the closure of DFA
under complementation becomes trivial since we can swap accepting and
non-accepting halting states.
Since we use Sipser’s argument later, let us recall it: The idea is that we
can replace any 4-way DFA A by an equivalent 4-way DFA B that has the
additional property that it halts with every input. Then the closure of DFA
under complementation becomes trivial since we can swap accepting and
non-accepting halting states.
F
The halting DFA B does a depth-first search (DFS) of predecessors of
configurations, starting from the accepting configuration of A. (We can
assume without loss of generality that A has a unique accepting
configuration where A is located at the lower left corner of the input.)
If the initial configuration of A is found, then automaton B accepts.
• Since A is deterministic, backtracking during the DFS can be done
without any additional memory. (The forward rule of A tells where to
backtrack.)
• Since A is deterministic, backtracking during the DFS can be done
without any additional memory. (The forward rule of A tells where to
backtrack.)
• When backtracking, we only need to memorize which configuration we
backtracked from. This is done so that we know which predecessor
configuration to try next. We fix a global ordering of possible
predecessors.
• Since A is deterministic, backtracking during the DFS can be done
without any additional memory. (The forward rule of A tells where to
backtrack.)
• When backtracking, we only need to memorize which configuration we
backtracked from. This is done so that we know which predecessor
configuration to try next. We fix a global ordering of possible
predecessors.
• The search tree contains no loops. Again, this follows from the
determinism of A. This means that the search always ends. Either it
ends when we find the initial configuration (and accept) or we exhaust
the whole search tree (and reject).
Theorem For every 4-way DFA there is an equivalent 4-way DFA that
halts with every input. Consequently 4-way DFA are closed under
complementation.
Theorem For every 4-way DFA there is an equivalent 4-way DFA that
halts with every input. Consequently 4-way DFA are closed under
complementation.
We have the following closures under boolean operations:
∩
∪
co
DFA yes
yes
yes
NFA
yes
no
yes
Obviously, both 4-way NFA and 4-way DFA language families are closed
under intersection (start the second automaton once the first automaton
accepts), and 4-way NFA languages are closed under union (start the
computation by making a non-deterministic choice of which automaton to
use). Finally, closure of 4-way DFA under union follows from their closure
under intersection and complementation
The same table is valid over the unary alphabet.
∩
∪
co
DFA yes
yes
yes
NFA
yes
no
yes
4-way alternating finite automata (4-AFA) are generalizations of
4-way NFA, where states are classified as universal and existential: For
acceptance
• if the automaton is in an existential state, at least one of the available
non-deterministic choices must lead to acceptance, and
• if the automaton is in a universal state, all available choices must lead
to acceptance.
∩
∪
co
DFA
yes
yes
yes
NFA
yes
yes
no
AFA
yes yes
no
The complement of the ”bouncing” language discussed above is accepted
by a 4-way AFA all of whose states are universal. Hence 4-way AFA are
strictly more powerful than 4-way NFA, even over unary languages.
Closure of 4-way AFA languages under union and intersection are trivial.
In general, they are not closed under complementation.
However, over unary alphabets the closure of 4-way AFA under
complementation is unknown:
∩
∪
co
DFA
yes
yes
yes
NFA
yes
yes
no
AFA
yes yes ???
Next, let’s see a few examples of unary picture languages recognized by
4-way DFA:
Example 1 The set of all squares can be easily accepted.
Next, let’s see a few examples of unary picture languages recognized by
4-way DFA:
Example 1 The set of all squares can be easily accepted.
Just move diagonally through the rectangle.
Example 2 What about squares whose size is a power of two ?
2
2
n
n
Example 2 What about squares whose size is a power of two ?
(1) First verify that the shape is a square.
Example 2 What about squares whose size is a power of two ?
(2) Using knights moves (two steps right, one step up) verify that the width
of the square is even, and move to the half way position.
Example 2 What about squares whose size is a power of two ?
(2) Using knights moves (two steps right, one step up) verify that the width
of the square is even, and move to the half way position.
Example 2 What about squares whose size is a power of two ?
(3) Repeat the process, and keep repeating as long as the position is even
and can be divided by two. Accept if the automaton reaches the lower right
corner.
Example 3 Let’s see next how we can recognize even sparser sets of
2n
squares. Consider squares whose sizes are of the form 2 for n = 1, 2, 3, . . ..
n
2
2
n
2
2
We say that the 4-way DFA is in position k in the square if it is on the
lowest row of the square and the distance to the lower right corner is k:
#
k
We use following two types of moves:
(A) Using a diagonal path and a counter modulo m we can check
whether the position k of the automaton is divisible by m. After
the test the automaton remains in the original position k.
k
(B) We can change position k = apj into aq j , provided that
• p and q are relative primes,
• p does not divide a, and
• The square is large enough to contain the new position aq j .
ap j
aq
j
(B) We can change position k = apj into aq j , provided that
• p and q are relative primes,
• p does not divide a, and
• The square is large enough to contain the new position aq j .
q
j−1
k p = ap q
k=apj
This is established using knight-like moves with speed q/p.
(B) We can change position k = apj into aq j , provided that
• p and q are relative primes,
• p does not divide a, and
• The square is large enough to contain the new position aq j .
q
j−1
k p = ap q
k=apj
k=aq
j
This is established using knight-like moves with speed q/p.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
n
Example 3 To recognize squares of sizes 22 we
(1) Verify that the input is a square whose size is some power of two.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
n
Example 3 To recognize squares of sizes 22 we
(1) Verify that the input is a square whose size is some power of two.
(2) Using (B) with p = 4 and q = 3 we change position a4j into position
a3j . Here a = 1 or 2.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
n
Example 3 To recognize squares of sizes 22 we
(1) Verify that the input is a square whose size is some power of two.
(2) Using (B) with p = 4 and q = 3 we change position a4j into position
a3j . Here a = 1 or 2.
(3) Using (A) and m = 2 check if the current position is even, that is,
whether a = 2. If it is then reject (the size is an odd power of 2).
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
n
Example 3 To recognize squares of sizes 22 we
(1) Verify that the input is a square whose size is some power of two.
(2) Using (B) with p = 4 and q = 3 we change position a4j into position
a3j . Here a = 1 or 2.
(3) Using (A) and m = 2 check if the current position is even, that is,
whether a = 2. If it is then reject (the size is an odd power of 2).
(4) Using (B) with p = 3 and q = 2 we change position 3j into position 2j .
Steps (2)-(3) changed the position 22j into 2j . We repeat these steps as
long as possible. If after step (4) the position is equal to 2 then we accept.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
Example 4 Squares of sizes 11p for prime numbers p.
(1) Verify that the input is a square whose size is some power of 11. We
also take care of small instances, so we can assume that the input is
”sufficiently large”.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
Example 4 Squares of sizes 11p for prime numbers p.
(1) Verify that the input is a square whose size is some power of 11. We
also take care of small instances, so we can assume that the input is
”sufficiently large”.
(2) Using (B) with p = 11 and q = 2 we change position 11p into position
2p . We use a signal of speed 25 to move to position 2p 5n with n = 2.
(A) Check if the position k is divisible by m.
(B) Change position k = apj into aq j , where p and q are relative
primes and a is not divisible by p 6∈ Z.
Example 4 Squares of sizes 11p for prime numbers p.
(1) Verify that the input is a square whose size is some power of 11. We
also take care of small instances, so we can assume that the input is
”sufficiently large”.
(2) Using (B) with p = 11 and q = 2 we change position 11p into position
2p . We use a signal of speed 25 to move to position 2p 5n with n = 2.
In the following loop, starting in position 2p 5n we check whether n divides
p (in which case we reject) and move to position 2p 5n+1 .
(3) Using signals 7/2 and 6/7 we copy p to the exponent of 3:
2p 5n −→ 7p 5n −→ 2p 3p 5n .
(3) Using signals 7/2 and 6/7 we copy p to the exponent of 3:
2p 5n −→ 7p 5n −→ 2p 3p 5n .
Using the following loop (4)-(5) we establish move
2p 3p 5n −→ 2p 3q 5n
where q ≡ p
(mod n) and 0 ≤ q < n.
(4) With signal of speed 7/(3 · 5) we move
2p 3 x 5 n
−→ 2p 3x−n 7n
if x ≥ n and
2 p 3x 5n
−→ 2p 5n−x 7x
if x < n
(5) Check if the result is divisible with 5, If not (i.e. x ≥ n), then we move
2p 3x−n 7n −→ 2p 3x−n 5n and repeat step (4). Otherwise (x = q is the
number we are looking for) we move back 2p 5n−x 7x −→ 2p 3x 5n .
So far we converted
2p 5n −→ 2p 3q 5n
where q ≡ p
(mod n) and 0 ≤ q < n.
(6) Next we test if the result is divisible by 3. If it is not then we reject the
input since q = 0 which means that p is divisible by n.
(7) Otherwise we use a signal of speed 1/3 to remove 3’s and then we
multiply the result by 5:
2p 3q 5n −→ 2p 5n −→ 2p 5n+1 .
Now we are ready for the next round, to test if n + 1 divides p.
So far we converted
2p 5n −→ 2p 3q 5n
where q ≡ p
(mod n) and 0 ≤ q < n.
(6) Next we test if the result is divisible by 3. If it is not then we reject the
input since q = 0 which means that p is divisible by n.
(7) Otherwise we use a signal of speed 1/3 to remove 3’s and then we
multiply the result by 5:
2p 3q 5n −→ 2p 5n −→ 2p 5n+1 .
Now we are ready for the next round, to test if n + 1 divides p.
The loop above is repeated for n = 2, 3, . . . until a number is found that
divides p (reject) or until the automaton tries to move to a position that
exceeds the size of the square. This happens surely before n reaches p since
one of the positions we use is 7p 5n which is greater than 11p when n = p.
On the other hand, surely all numbers n up to p/4 are tried.
We have seen examples that show that 4-way DFA can accept squares
2n
whose sizes form a very fast growing set (sizes 2 ), and squares of
complicated sizes (11p for prime p).
Question: How sparse can the set of squares recognized be ?
We have seen examples that show that 4-way DFA can accept squares
2n
whose sizes form a very fast growing set (sizes 2 ), and squares of
complicated sizes (11p for prime p).
Question: How sparse can the set of squares recognized be ?
Answer: Very sparse. For any recursively enumerable set
{a1 , a2 , . . .}
of positive integers there exists a 4-way DFA that recognizes squares of sizes
{b1 , b2 , . . .}
such that bi > ai for all i = 1, 2, . . ..
To prove this fact we use 4-way DFA to simulate 2-counter machines.
A 2-counter machine (without an input tape) consists of a deterministic
finite state machine and two counters that can store one non-negative
integer each. The machine can detect when either counter is zero.
The machine changes its state and increments/decrements the counters
according to a deterministic transition rule. The transition only depends on
the old state and the zero vs. non-zero status of the two counters.
The input to the machine is an integer. In the beginning of the
computation the input number is stored in the first counter, and the second
counter is equal to 0. The input is accepted if the machine eventually
enters its accepting state.
Minsky proved over 40 years ago that 2-counter machines can simulate
Turing machines: For any r.e. set X of integers there exists a 2-counter
machine that accepts the set
{2n | n ∈ X}.
Minsky proved over 40 years ago that 2-counter machines can simulate
Turing machines: For any r.e. set X of integers there exists a 2-counter
machine that accepts the set
{2n | n ∈ X}.
The crucial observation for our case is the fact that a 2-counter machine
can be interpreted as a 4-way DFA that operates on the (infinite) quadrant
of the plane. The x- and y-coordinates of the DFA position are the current
counter values. Incrementing or decrementing a counter is then identical to
single steps in the horizontal and vertical directions. Detecting the left or
bottom boundary is equivalent to having a counter value 0.
y=counter2
q
x=counter1
Using this observation we easily build for any given two-counter machine C
a 4-way non-deterministic automaton A that recognizes squares whose size
is the maximum counter value used during some accepting computation
by C:
(1) First we verify that the input is square. After that we move along the
bottom row and non-deterministically guess the input value to the
2-counter machine C.
(1) First we verify that the input is square. After that we move along the
bottom row and non-deterministically guess the input value to the
2-counter machine C.
(2) Next we simulate C.
(1) First we verify that the input is square. After that we move along the
bottom row and non-deterministically guess the input value to the
2-counter machine C.
(2) Next we simulate C.
(3) If the automaton tries to move outside the input square by crossing the
top or the right boundary, we reject since the largest counter value
exceeds the size of the input square.
(1) First we verify that the input is square. After that we move along the
bottom row and non-deterministically guess the input value to the
2-counter machine C.
(2) Next we simulate C.
(3) If the automaton tries to move outside the input square by crossing the
top or the right boundary, we reject since the largest counter value
exceeds the size of the input square.
(4) If the automaton visits the topmost row or the rightmost column, we
memorize this fact.
(1) First we verify that the input is square. After that we move along the
bottom row and non-deterministically guess the input value to the
2-counter machine C.
(2) Next we simulate C.
(3) If the automaton tries to move outside the input square by crossing the
top or the right boundary, we reject since the largest counter value
exceeds the size of the input square.
(4) If the automaton visits the topmost row or the rightmost column, we
memorize this fact.
(5) If the 2-counter machine enters accepting state then the 4-way NFA
accepts if it has visited the top row or the rightmost column.
Otherwise it rejects.
It is clear that the above 4-way NFA A accepts a square if and only if the
size of the square is identical to the maximum counter value during some
accepting computation by C.
According to Minsky’s result, for any r.e. set X = {a1 , a2 , . . .} of pos.
integers there is 2-counter machine C that accepts initial counter values
2ai .
The corresponding 4-way NFA accepts squares whose sizes are b1 , b2 , . . .
where bi is the maximum counter value during the accepting
computation of C on input 2ai . Since
bi ≥ 2ai > ai
we have a 4-way NFA A that accepts squares whose sizes are sparser than
set X.
The only remaining problem is that the 4-way automaton A is
non-deterministic. So our next step is to determinize A. Notice that the
only non-deterministic step is the selection of the starting counter value.
The simulation steps (2)-(5) are deterministic.
The only remaining problem is that the 4-way automaton A is
non-deterministic. So our next step is to determinize A. Notice that the
only non-deterministic step is the selection of the starting counter value.
The simulation steps (2)-(5) are deterministic.
So we can use Sipser’s idea: Starting from the accepting configuration of
A (which we can assume is unique and in the lower left corner) we search
backwards in the computation tree, using depth-first-search.
We accept if we reach a configuration where A is located on the lowest row,
in the initial configuration to start the simulation of C. Such a
configuration is reached if and only if there is a non-deterministic choice
that makes A to accept the input square.
Conclusions
Over unary alphabets we have strict inclusions
4-DFA ⊂ 4-NFA ⊂ 4-AFA.
Still, even 4-way DFA can recognize very complicated sets of rectangles,
e.g. squares of sizes 11p for prime numbers p.
Open questions:
• How to prove that some sets of squares can not be recognized by 4-way
DFA ? Can e.g. squares whose sizes are primes be recognized ?
• Are the inclusion above strict if restricted to squares ?
• Are 4-way AFA closed under complementation when the alphabet is
unary ?
© Copyright 2026 Paperzz