arXiv:2608.23357cs.DSAug 2026

Manuscript Offprint

arXiv:2608.23357

A Geometric Derivation of the Bitner--Ehrlich--Reingold Loopless Gray Code Algorithm

Andrew Au

Abstract

The Bitner–Ehrlich–Reingold algorithm generates the binary reflected Gray code with constant work per codeword, using a focus-pointer array. Its compact update is easy to state but gives little indication of why such pointers should exist. This note reconstructs a geometric route to the algorithm. The sequence of flipped bit positions is the ruler sequence, OEIS A007814. We realize its finite prefixes as in-order traversals of recursively expanding trees, decorate each activation with its nearest ancestor to the right, contract all stack operations between successive outputs into successor jumps, and prepare the bounded level-indexed stack by predicting its future slot values. This gives and proves a branch-based loopless generator. The construction grew from an earlier informal public exposition by the author. Viewing horizontal position in the tree as time then explains each stack-slot write as a prediction for the next activation at the same level. Finally, the two possible future roles—an inherited continuation for a right child and a default continuation for a left child—are scheduled together, leading to the two assignments of the published focus-pointer algorithm. The account is a reconstructed derivation, not a claim about the historical reasoning of Bitner, Ehrlich, or Reingold.

Keywords: Gray codes; combinatorial generation; loopless algorithms; ruler sequence; focus pointers.

1 Introduction

For n≥1n \geq 1, let the bits of an n-bit binary reflected Gray code (BRGC) be numbered 0,…,n−10, \ldots, n-1. The BRGC orders the vertices of the n-dimensional hypercube along a Hamiltonian path; for n≥2n \geq 2, the closing edge makes it a Hamiltonian cycle. This note studies the induced coordinate-flip schedule rather than the cube embedding itself. The positions flipped after the initial all-zero word, for n=4n = 4, are

0,  1,  0,  2,  0,  1,  0,  3,  0,  1,  0,  2,  0,  1,  0.0,\;1,\;0,\;2,\;0,\;1,\;0,\;3,\;0,\;1,\;0,\;2,\;0,\;1,\;0.

Bitner, Ehrlich, and Reingold gave a loopless algorithm for producing the BRGC [1]. Knuth presents its focus-pointer form as Algorithm L [2, §7.2.1.1]:

a[0..n - 1] = 0; f[j] = j for 0 <= j <= n visit(a) repeat: j = f[0]; f[0] = 0 if j == n: terminate flip(a[j]) f[j] = f[j + 1]; f[j + 1] = j + 1 visit(a)

For the remainder of the paper, we analyze only the sequence of flipped positions. We say that Algorithm L emits j when it executes flip(a[j]); the visit(a) operations output the corresponding Gray-code words but play no role in the derivation.

At the start of each iteration, f[0]f[0] is the next level, with n serving as the termination sentinel; the remaining entries encode deferred successor information. The purpose of the derivation is to identify that information geometrically.

Every iteration uses O(1)O(1) operations, after O(n)O(n) initialization. The difficulty is motivational: why should the two focus-pointer writes encode the recursive Gray-code order? For broader background on combinatorial generation and Gray codes, see Ruskey [3].

This note develops an answer from the geometry of the flip sequence. The route originated in an exploratory Stack Overflow answer posted by the author in 2021 [5]. That answer found the decorated tree and a branch-based loopless algorithm, but it moved too quickly from recursive execution to successor jumps and stopped short of deriving Algorithm L. Here we fill those gaps.

We make no claim about how the published algorithm was historically discovered. Our claim is mathematical: each transformation below preserves the generated flip sequence, and the endpoint is Algorithm L.