Papers
Topics
Authors
Recent
Search
2000 character limit reached

Gray Codes With Constant Delay and Constant Auxiliary Space

Published 12 Feb 2026 in cs.DS and cs.CC | (2602.11791v1)

Abstract: We give the first two algorithms to enumerate all binary words of 0,1<sup>{0,1}<sup>\ell (like Gray codes) while ensuring that the delay and the auxiliary space is independent from \ell, i.e., constant time for each word, and constant memory in addition to the \ell bits storing the current word. Our algorithms are given in two new computational models: tape machines and deque machines. We also study more restricted models, queue machines and stack machines, and show that they cannot enumerate all binary words with constant auxiliary space, even with unrestricted delay. A tape machine is a Turing machine that stores the current binary word on a single working tape of length \ell. The machine has a single head and must edit its tape to reach all possible words of 0,1<sup>{0,1}<sup>{\ell} , and output them (in unit time, by entering special output states), with no duplicates. We construct a tape machine that achieves this task with constant delay between consecutive outputs, which implies that the machine implements a so-called skew-tolerant quasi-Gray code. We then construct a more involved tape machine that implements a Gray code. A deque machine stores the current binary word on a double-ended queue of length \ell, and stores a constant-size internal state. It works as a tape machine, except that it modifies the content of the deque by performing push and pop operations on the endpoints. We construct deque machines that enumerate all words of 0,1<sup>{0,1}<sup>\ell with constant-delay. The main technical challenge in this model is to correctly detect when enumeration has finished. Our work on deque machine is also motivated by other contexts in which endpoint modifications occur naturally. In particular, our result is a first step towards enumerating walks in directed graphs with constant delay and constant auxiliary space, addressing a core task in modern graph database query processing.

Summary

  • The paper proves that tape machines can enumerate every binary word with constant delay and constant auxiliary space, including a Hamming-1 Gray code implemented through a tree traversal and parity-based state.
  • The paper constructs Hamiltonian deque machines using double traversal and lookahead techniques, while deriving constant-time Gray-code counters and explicit rank and unrank procedures.
  • The paper establishes sharp model limitations: queue and stack machines cannot enumerate all binary words under the stated finite-state restrictions, showing that two-ended editing is essential.

This paper studies whether all binary words of length \ell can be enumerated with both constant delay between consecutive outputs and constant auxiliary memory beyond the \ell bits holding the current word. The authors answer this question affirmatively in two computational models of their own design — tape machines and deque machines — and negatively for two restricted variants, queue machines and stack machines. The work sits at the intersection of combinatorial Gray code generation, enumeration algorithms, and automata theory.

Motivation and problem statement

Under the "do not count the output" principle from the enumeration literature, an algorithm maintains a single word in memory, outputs it in unit time via a dedicated instruction, and edits it in place before the next output. Two optimality criteria are considered: constant delay, meaning a bound BB independent of \ell on the number of steps between consecutive outputs (and before the first output and after the last), and constant auxiliary space, meaning that besides the current word only a finite state independent of \ell is used. Prior results optimize each criterion separately: the Reflected Binary Gray Code achieves loopless generation but requires linear auxiliary memory, while decision assignment tree (DAT) / bit-probe constructions achieve near-optimal memory but are non-uniform and allow arbitrary computation between bit accesses. Whether both requirements can be met simultaneously depends on the allowed edit operations, which is precisely what the paper investigates.

Tape machines

A tape machine is a deterministic Turing machine variant resembling a linear bounded automaton: it has a single binary working tape of length \ell with end markers, one head, a finite state set including distinguished output states, and no other storage. It is Hamiltonian if, started on 00^\ell, it produces every word of {0,1}\{0,1\}^\ell exactly once and halts; it is Hamming-1 if consecutive outputs differ in exactly one bit.

The first construction, T0T_0, is Hamiltonian and constant-delay but not Hamming-1: it changes up to 3 bits per step, making its output a quasi-Gray code. Its proof maps non-zero words to nodes of the complete binary tree B1B_{\ell-1} by distinguishing the rightmost 1, then performs a depth-first traversal using the even-odd trick of Feder — producing nodes in prefix order at even depths and postfix order at odd depths — so that consecutive visited nodes differ locally. The head stays on the rightmost 1, and the state records traversal direction and depth parity; all tests and moves are local reads/writes within a window of size 3.

The main result strengthens this:

Theorem: There exists a Hamiltonian constant-delay Hamming-1 tape machine.

The machine \ell0 realizes this. Each node of \ell1 is associated with two words (a bottom word \ell2 and top word \ell3, where \ell4 is the parity of the number of 0s on the root-to-node path), and a traversal visiting bottom words before descending and top words after ascending yields a Gray code; \ell5 and \ell6 are produced first and last. Notably, the resulting code turns out to coincide, up to mirroring and reversal, with Construction A of Sac Himelfarb and Schwartz's 3-skew-tolerant Gray codes, despite being derived independently. A companion machine \ell7 implements that code exactly. This establishes that a known skew-tolerant Gray code admits an implementation with constant delay and constant auxiliary memory — a property not shared by the standard RBGC, whose flipped-bit distances grow unboundedly.

Two consequences follow. First, since the transition tables of \ell8 and \ell9 are injective, they yield RAM counters supporting increment and decrement in BB0 time with a single auxiliary BB1-bit word (versus two words in prior work answering Demaine's open problem), while additionally ensuring counter values form a Gray code, and supporting growth from BB2 to BB3 bits in constant time. Second, these results do not contradict the BB4 read lower bounds in the DAT model, because a tape machine implicitly stores its head position, amounting to BB5 freely accessible bits when translated to that model. Rank and unrank functions for BB6's ordering are also given explicitly.

Deque machines

Tape machines arguably hide logarithmic memory in the head position. Deque machines remove the head entirely: the word lives on a double-ended queue, the machine reads both endpoint symbols, and each step pops one endpoint and pushes (possibly the same) symbol to an endpoint. The configuration is thus exactly the current word plus a finite state. Push-pop quasi-Gray codes — sequences where each word follows from the previous by one pop and one push — are known to exist via universal cycles or Hamiltonian paths in Shuffle Exchange networks, so the question is whether such codes can be driven by a machine with constant auxiliary memory.

The base machine BB7 mirrors BB8: non-zero words encode nodes of BB9 via their leftmost 1, and the even-odd trick drives a depth-first traversal using left-pop/right-push to descend and right-pop/left-push-0 to ascend. It is constant-delay and prefix-Hamiltonian (the first \ell0 outputs are all distinct words), but it cannot halt: unlike tape machines, it cannot detect the root \ell1, since counting leading zeros or ones would require non-constant state. When the run reaches the root a second time, the parity stored in the state has been swapped, and the machine loops forever through repeated traversals.

The paper gives two techniques to restore halting, both exploiting that the leaves \ell2 and \ell3 are detectable, as the first visits of words starting with \ell4 and \ell5 respectively:

  • Double traversal (\ell6): perform four half-traversals (two full traversals) starting from \ell7, tracking the half-traversal index in the state, and write the parity bit as the rightmost deque symbol so that the two traversals partition the outputs despite the parity swap at the root.
  • Lookahead (\ell8): perform one full traversal from \ell9, but whenever descending from a right child, first descend to the subtree's leftmost leaf and return, tracking height modulo 4 so each node is output on exactly one of its four visits. Detecting the second visit of \ell0 lets the machine correct the height offset before the final descent along the leftmost path, and a special state handles the word \ell1.

Theorem: There exists a Hamiltonian constant-delay deque machine.

Both \ell2 and \ell3 satisfy this theorem. A limitation is noted for counters: because the transitions of \ell4 and \ell5 are not injective, decrement can be supported in constant time only under the assumption that the counter stays positive; whether this can be lifted is left open.

Lower bounds for queue and stack machines

The paper shows the deque model is minimal in a precise sense. A queue machine may only push left and pop right (reading the \ell6 rightmost symbols for constant \ell7):

Theorem: There exists no Hamiltonian queue machine.

The proof is a pumping-style argument: starting from the last configuration before halting, one builds ever longer words \ell8 such that each must be visited in a fresh state, contradicting finiteness of the state set once the number of constructed words exceeds \ell9. Crucially, this holds even without any delay requirement, whereas with unbounded auxiliary memory left-push/right-pop enumeration of all words is possible via universal cycles. The argument also rules out models additionally allowed to edit the \ell0 leftmost and rightmost characters in place.

For stack machines (push/pop on the right only), constant-delay enumeration of \ell1 is trivially impossible for large \ell2, and moreover:

Proposition: Any stack machine with \ell3 states fails to visit some words of length \ell4.

The proof uses a pumping lemma showing that if the machine revisits a longer extension of a prefix in the same state without popping below it, it loops forever with that prefix fixed. Consequently no stack machine enumerates \ell5, \ell6, or \ell7, even with duplicates. By contrast, \ell8 is enumerable with constant delay by length-dependent push-right/pop-right Gray codes built from the same tree-traversal technique, and enumerating \ell9 in constant delay is impossible by a prior result on regular language enumeration.

Limitations and open questions

Several restrictions are acknowledged. The equivalence between 00^\ell0's code and the 3-skew-tolerant construction does not extend to the known 2-skew-tolerant code, whose implementability by a tape machine is unclear. Deque-machine decrements remain unsupported in general, hinging on detecting return to the initial configuration. Generalizations are uncertain: extending to 00^\ell1-ary alphabets or arbitrary regular languages is not immediate, because the halting techniques (lookahead, double traversal, storing state on the tape) rely on structural properties specific to 00^\ell2. The stated long-term motivation — enumerating walks in directed graphs with constant delay and constant auxiliary space, relevant to graph database query processing, of which binary-word enumeration is the single-vertex two-self-loop special case — remains open, as does implementing the constant-delay regular-language enumeration algorithms of Amarilli and Monet with constant additional memory and machine size comparable to an automaton for the language.

Conclusion

The paper settles the simultaneous constant-delay/constant-space enumerability of 00^\ell3 across a hierarchy of edit-operation models: achievable by tape machines (even with the Hamming-1 property, yielding a skew-tolerant Gray code and improved RAM counters) and by deque machines (via two distinct halting techniques), but impossible for queue machines and stack machines even with unrestricted delay. The separation delineates exactly how much endpoint-editing power is necessary, and provides concrete machinery — tree encodings, the even-odd trick, lookahead, and double traversal — whose portability beyond binary words is the central question the work leaves open.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.