arXiv:2610.38616cs.LOOct 2026

Manuscript Offprint

arXiv:2610.38616

Order Maintenance in Worst-Case Constant Time, Verified

Denis Firsov

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Order maintenance asks for a data structure that keeps a sequence of elements under insertions and deletions and tells which of two elements comes first. We give an algorithm that performs each operation in worst-case constant time, and verify it in Lean 4. The difficulty is to change labels gradually while queries and further insertions continue. The algorithm, assembled from published components, never relabels the members of a full unit in place: they move into fresh neighbouring units. Small groups keep element labels short; groups are collected into larger chunks, which keep accepting insertions while they wait their turn for a shared top tree; and the top tree never changes a label it has assigned. We implement the algorithm as a program for a random-access machine (RAM) deeply embedded in Lean, whose interpreter counts every instruction and allocates a zero-filled array in one step, and prove that the program answers every query as a simple reference specification does and that each operation takes at most a fixed number of steps, independent of the number of elements and of the history. The machine treats arithmetic on unbounded natural numbers as one step, and on such a machine a step bound alone is weak, so we also prove that after tt operations no value the machine has computed has more than O(log⁡t)O(\log t) bits, and that the program runs unchanged, with the same answers and step counts, on a machine whose words have that many bits. A verified translation through an intermediate language connects the high-level proofs to the machine program and preserves step counts exactly, so the interpreter is the only cost model to trust; the development has about 78,000 lines of Lean. Rust code transpiled from the program agrees with the machine semantics on all tested traces, and an unverified native implementation shows the algorithm's practical cost.

Keywords: order maintenance, worst-case complexity, word RAM, cost semantics, Lean 4.

Supplementary material. Lean development and transpiled Rust code:

https://github.com/dfirsov/order_maintenance

Interactive visualizer: https://dfirsov.github.io/order_maintenance/viz/

1 Introduction

The order-maintenance problem asks to maintain a sequence under three operations: insert a new element immediately after a given one, delete an element, and decide whether one element precedes another. It is a building block of many dynamic algorithms: fully persistent data structures [24], self-adjusting computation [1], race detection for fork-join programs [11], and incremental topological ordering [32]. The usual approach is labeling: give each element an integer label so that labels increase along the sequence, and answer a query by comparing two labels. The difficulty is the insertion: repeated insertions between the same two neighbours eventually use up the labels between them, and then some labels must change.

The problem goes back to Dietz [20], and constant amortized time per operation is classical [55, 22, 8]. Constant worst-case time has been known since 1987 [22, 23], but the known algorithms are intricate. Kopelowitz [42, Section 1] summarized the state of the art: “to date there is no published version of a worst-case solution for the OMP which is not considered highly complex”. Bender et al. have since simplified worst-case file maintenance [10] (keeping items in sorted order in an array with gaps), the component at the top of Dietz and Sleator’s construction, but as far as we know, no worst-case O(1)O(1) order-maintenance algorithm has been implemented, and no order-maintenance algorithm has been verified in a proof assistant.

The obstacle to worst-case constant time is unfinished work: to bound every operation, a relabeling must be spread over later insertions. Queries then see a partly changed structure, and further insertions can arrive at the very place being changed, or at other places that also need attention. A worst-case algorithm must explain both why every intermediate state answers queries correctly and why the unfinished work never falls behind.

Our construction answers the two questions separately. For correctness, a unit that fills up is never relabeled in place: it gets two empty neighbours, and its members move into them from its ends a few at a time, so keys stay ordered at every instant; in addition, every write that could break the order is checked in constant time. For timing, each insertion pays a fixed number of steps towards the repairs of the units it makes larger, and a largest-first queue arbitrates the one place where unrelated units compete. The same scheme serves groups of elements, chunks of groups, and the nodes of the top tree. Section 3 develops these ideas.

We assemble the algorithm from published components and implement it in a small imperative language that we deeply embed in Lean 4. The language’s interpreter counts every instruction it executes, so running time is part of the semantics and no part of the computation can escape the count. We prove two theorems about the program: it answers queries correctly (Theorem A), and every operation takes at most a fixed number of steps, independent of the length of the sequence and of the history (Theorem B).

A step bound alone would say little. The machine treats arithmetic on unbounded natural numbers, and allocation, as single steps, and on such a machine order maintenance is easy: labels of unbounded length never run out (Section 2). What makes the problem hard is that labels must stay short. We therefore also prove Theorem W: no value the machine computes, whether stored or intermediate, has more than O(log⁡N)O(\log N) bits, where NN is the capacity of the structure, except handles and a few wrapper counters, which are bounded by the number of operations. The capacity is at most linear in the number of operations, so the ii-th operation computes only values of O(log⁡i)O(\log i) bits, and a machine with words of that size, but otherwise the same, runs the program with the same answers and step counts. Word size and space thus grow with the number of operations rather than with the length of the sequence. These, and the machine’s one-step allocation of zero-filled arrays, are the gaps to a textbook word RAM (Section 6).

Checking the literature against our own derivations and simulations, we found errors in two papers we build on or compare with: the appendix pseudocode of Dietz and Sleator [23] has easily repaired slips (two swapped boundary tests, and a move after the last child of a node that has none), and the proof of the amortized bound of Bender et al. [8, Lemma 3] miscounts insertions by a constant factor (the O(log⁡n)O(\log n) bound survives, but their space/time trade-off does not). A pen-and-paper proof, written before the formalization, found a cost bug and two gaps in our own design, all of which extensive testing had missed. The formalization found no further errors, but three parameters had to change: two because the mechanized arguments are coarser, and one so that an inequality could be proved for all sizes at once (Section D).

References (47)