Element distinctness in linear time on the word RAM #
In the comparison model, deciding whether n numbers are pairwise distinct needs Ω(n log n)
comparisons, but on the word RAM numbers are also addresses. When the numbers are below n, a
table of n flags decides distinctness in linear time by direct addressing
Cormen et al., Section 11.1: allocate the table zeroed, then for each number check
its flag, answering no if it is set and setting it otherwise.
Cslib.Algorithms.WordRAM.ElementDistinctness.decidesInTime_nodup formalizes this: on lists of
naturals whose entries are below their length n, the compiled decider decides List.Nodup
within 16 (n + 1) instructions, the final halt included, at every word size of at least
2 (⌊log₂ n⌋ + 1), with one binary digit at zero. Every instance fits at this threshold
(Cslib.Algorithms.WordRAM.ElementDistinctness.inputFits). The decider allocates its table with
Cslib.WordRAM.alloc and reads and
writes it with Cslib.WordRAM.readAt and Cslib.WordRAM.writeAt; its correctness rests on the
loop rule Cslib.WordRAM.ExecWithin.loop_invariant.
Main definitions #
Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.zero,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.one,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.len,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.front,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.cnt,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.base,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.cur,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.idx,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.val,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.tmp,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.seen,Cslib.Algorithms.WordRAM.ElementDistinctness.Reg.ans: the twelve register names.Cslib.Algorithms.WordRAM.ElementDistinctness.setup,Cslib.Algorithms.WordRAM.ElementDistinctness.body,Cslib.Algorithms.WordRAM.ElementDistinctness.decider,Cslib.Algorithms.WordRAM.ElementDistinctness.setupAdditive,Cslib.Algorithms.WordRAM.ElementDistinctness.bodyAdditive,Cslib.Algorithms.WordRAM.ElementDistinctness.deciderAdditive: the structured decision procedure and its setup and loop body overBasis.standardandBasis.additive.Cslib.Algorithms.WordRAM.ElementDistinctness.Input: lists whose entries are below their length.
Main results #
Cslib.Algorithms.WordRAM.ElementDistinctness.answers: the decider answers whether its input list has no duplicates within14 n + 15instructions.Cslib.Algorithms.WordRAM.ElementDistinctness.inputFits: every instance fits at the advertised logarithmic word-size threshold.Cslib.Algorithms.WordRAM.ElementDistinctness.decidesInTime_nodup,Cslib.Algorithms.WordRAM.ElementDistinctness.decidesInTime_nodup_additive: the compiled decider decides element distinctness within16 (n + 1)instructions at every word size of at least2 (⌊log₂ n⌋ + 1).Cslib.Algorithms.WordRAM.ElementDistinctness.uniformLinearTime_nodup,Cslib.Algorithms.WordRAM.ElementDistinctness.uniformLinearTime_nodup_additive: element distinctness for entries below the length is uniformly in linear time overBasis.standardandBasis.additive.
References #
The program #
Register names #
Register holding 0, as all registers do initially.
Register holding 1.
Register holding the length n of the input.
The allocation frontier.
The number of cells to allocate.
The address n + 1 of the table of flags, just past the input.
Scratch register of the allocator.
The address i + 1 of the next input entry l[i].
The current input entry.
Scratch register for table accesses.
The flag of the current entry.
The answer.
Read the length, allocate a zeroed table of n flags just past the input, and start at the
first entry with answer 1.
Process one entry: if its flag is set, answer 0 and stop; otherwise set its flag and
advance.
The decision procedure for element distinctness: the answer is left in register ans.
Additive-basis version of setup.
Additive-basis version of body.
Additive-basis decision procedure for element distinctness.
Mapping additive-basis operations to the standard basis recovers decider.
The instances: lists of naturals whose entries are below their length.
Correctness #
Every instance fits at the logarithmic threshold used by the decider, and at every larger word size. Thus the input-fit premise in its uniform decision theorem is always satisfied.
Correctness and running time. On a list whose entries are below its length, at a word
size that holds 2 n + 1, the decider leaves in register ans the answer to whether the list has
no duplicates, within 14 n + 15 instructions.
Element distinctness in linear time. On lists of naturals whose entries are below their
length n, the compiled decider decides whether there are no duplicates within 16 (n + 1)
instructions, the final halt included, at every word size of at least 2 (⌊log₂ n⌋ + 1).
Element distinctness is uniformly in linear time on the word RAM, for lists whose entries are below their length.
Element distinctness also has a bounded nonuniform linear-time family.
Element distinctness in linear time over Basis.additive.
Element distinctness is uniformly in linear time over Basis.additive, for lists whose
entries are below their length.
Element distinctness also has a bounded nonuniform linear-time family over Basis.additive.