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.decider,Cslib.Algorithms.WordRAM.ElementDistinctness.deciderAdditive: the structured decision procedure 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.
Instances For
Register holding 1.
Instances For
Register holding the length n of the input.
Instances For
The allocation frontier.
Instances For
The number of cells to allocate.
Instances For
The address n + 1 of the table of flags, just past the input.
Instances For
Scratch register of the allocator.
Instances For
The address i + 1 of the next input entry l[i].
Instances For
The current input entry.
Instances For
Scratch register for table accesses.
Instances For
The flag of the current entry.
Instances For
The answer.
Instances For
Read the length, allocate a zeroed table of n flags just past the input, and start at the
first entry with answer 1.
Instances For
Process one entry: if its flag is set, answer 0 and stop; otherwise set its flag and
advance.
Instances For
The decision procedure for element distinctness: the answer is left in register ans.
Instances For
Additive-basis version of setup.
Instances For
Additive-basis version of body.
Instances For
Additive-basis decision procedure for element distinctness.
Instances For
The instances: lists of naturals whose entries are below their length.
Instances For
Correctness #
The loop invariant, after the first t = idx - 1 entries: the input is intact; either no
duplicate has been found, the first t entries are distinct and the table flags exactly them, or
a duplicate has been found, the answer is 0 and the loop is about to stop.
Instances For
Words of twice the number of binary digits of n hold 2 n + 1.
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.
One iteration of the loop takes at most 8 instructions, preserves the invariant, and
either processes the next entry or stops.
When the loop stops, the answer is 1 exactly if the input has no duplicates.
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.