Algorithms and their running times

Lax185574.Algorithms · concepts/Lax185574/Algorithms.lean · lax-185574

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Definition

    An algorithm is a program of the word RAM of Lax808846.RamLax808846.Ram. It reads the encoding of a value and writes the encoding of a value, in the encodings of Lax185574.EncodingLax185574.Encoding.

    The word RAM computes with words of w bits. As usual, the word length must grow logarithmically with the input, so that a word can hold an address of the input and each number of the input. Each algorithm states how much it needs: an exponent e such that it works with words of e · log₂ (N + Σ) + O(1) bits on inputs of length N whose numbers sum to Σ.

    The algorithm A computes b from a in time O(T), with the constant c, if the following holds for the encoding x of a, of length N, whose numbers sum to Σ. At every word length w with c · (N + Σ + 1)^e ≤ 2^w,

    1. every run of A on x that halts outputs the encoding of b, and
    2. if c · T + c ≤ 2^w, then A halts on x within c · T + c steps.

    A word length w with c · (N + Σ + 1)^e ≤ 2^w is called admissible for c on the input x.

    The algorithm A decides a property P of a in time O(T) if it computes the truth value of P in time O(T).

    Concept map
    7 concepts
    100%
    DefinitionThis conceptRelated conceptA → B: B builds on ADescendants are omitted for concepts with more than 10 descendants.

    Lean source view on GitHub

    1import Lax185574.Encodings
    2import Lax808846.Ram
    3import Mathlib.Data.Real.Basic
    4
    5/-!
    6---
    7title: Algorithms and their running times
    8type: definition
    9---
    10An *algorithm* is a program of the word RAM of `Lax808846.Ram`. It reads
    11the encoding of a value and writes the encoding of a value, in the
    12encodings of `Lax185574.Encoding`.
    13
    14The word RAM computes with words of w bits. As usual, the word length
    15must grow logarithmically with the input, so that a word can hold an
    16address of the input and each number of the input. Each algorithm states
    17how much it needs: an exponent e such that it works with words of
    18e · log₂ (N + Σ) + O(1) bits on inputs of length N whose numbers sum to
    19Σ.
    20
    21The algorithm A *computes* b from a *in time O(T)*, with the constant c,
    22if the following holds for the encoding x of a, of length N, whose
    23numbers sum to Σ. At every word length w with c · (N + Σ + 1)^e ≤ 2^w,
    24
    251. every run of A on x that halts outputs the encoding of b, and
    262. if c · T + c ≤ 2^w, then A halts on x within c · T + c steps.
    27
    28A word length w with c · (N + Σ + 1)^e ≤ 2^w is called *admissible* for
    29c on the input x.
    30
    31The algorithm A *decides* a property P of a in time O(T) if it computes
    32the truth value of P in time O(T).
    33
    34# Formalization notes
    35
    36A statement about an algorithm has the form "there is an algorithm A
    37such that for all parameters there is a constant c such that for all
    38inputs a, A computes b from a in time O(T)". The program and the
    39exponent e come first, so neither depends on the parameters or on the
    40word length. The constant c is O-notation with explicit constants. It
    41bounds both the running time and the additive term of the word length.
    42
    43The word length bound includes the numbers of the input, since the
    44machine reduces every number modulo 2^w, so a number that does not fit
    45into a word cannot be read. The sum of the numbers dominates each of
    46them and also covers the empty input.
    47
    48The first clause is soundness at every word length of the hypothesis.
    49An algorithm that calls another one runs it at word lengths fixed by its
    50own memory, where the running time of the callee need not fit into a
    51word. It can trust any output it receives. The second clause claims
    52halting only once the time bound fits into a word, the usual word RAM
    53convention that a word holds the running time. This matters when the
    54output is large. Its size can be polynomial of a degree that grows with
    55parameters fixed after e, and then at some word lengths of the
    56hypothesis the output cannot be written. Since the machine is
    57deterministic, the two clauses together say that the run is correct
    58whenever it halts, and that it halts in time whenever the time fits.
    59
    60The time bound is a real number, so that bounds like n^(3+ε) can be
    61written directly. The additive c lets T vanish on small inputs, as
    62O-notation does. Several outputs are computed as one pair.
    63-/
    64
    65namespace Lax185574.Algorithms
    66
    67open Lax808846.Ram Lax185574.Encoding
    68
    69/-- An algorithm: a word RAM program, together with the exponent `e`
    70such that it needs words of `e · log₂ (N + Σ) + O(1)` bits on inputs of
    71length `N` whose numbers sum to `Σ`. -/
    72structure Algorithm where
    73 /-- The program. -/
    74 program : Program
    75 /-- The exponent of the word length the algorithm needs. -/
    76 wordExponent : ℕ
    77
    78/-- `A.Computes c a b T`: the algorithm `A` computes `b` from `a` in time
    79`O(T)`, with the constant `c`. Let `x` be the encoding of `a`. At every
    80word length `w` with `c · (|x| + Σx + 1)^e ≤ 2^w`, where `Σx` is the sum
    81of the numbers of `x`,
    821. every halting run of `A` on `x` outputs the encoding of `b`, and
    832. if `c · T + c ≤ 2^w`, then `A` halts on `x` within `c · T + c` steps,
    84 with the encoding of `b` as output. -/
    85def Algorithm.Computes {α β : Type*} [Encode α] [Encode β]
    86 (A : Algorithm) (c : ℕ) (a : α) (b : β) (T : ℝ) : Prop :=
    87 ∀ w : ℕ, c * ((encode a).length + (encode a).sum + 1) ^ A.wordExponent ≤ 2 ^ w →
    88 (∀ y t, RunsTo w A.program (encode a) y t → y = encode b) ∧
    89 ((c : ℝ) * T + c ≤ 2 ^ w →
    90 ∃ t : ℕ, (t : ℝ) ≤ c * T + c ∧ RunsTo w A.program (encode a) (encode b) t)
    91
    92open Classical in
    93/-- `A.Decides c a P T`: the algorithm `A` computes from `a` whether `P`
    94holds, writing `true` (encoded as `1`) or `false` (encoded as `0`), in
    95time `O(T)` with the constant `c`. -/
    96def Algorithm.Decides {α : Type*} [Encode α] (A : Algorithm) (c : ℕ) (a : α) (P : Prop)
    97 (T : ℝ) : Prop :=
    98 A.Computes c a (decide P) T
    99
    100end Lax185574.Algorithms
    101
    Formalization notes

    A statement about an algorithm has the form "there is an algorithm A such that for all parameters there is a constant c such that for all inputs a, A computes b from a in time O(T)". The program and the exponent e come first, so neither depends on the parameters or on the word length. The constant c is O-notation with explicit constants. It bounds both the running time and the additive term of the word length.

    The word length bound includes the numbers of the input, since the machine reduces every number modulo 2^w, so a number that does not fit into a word cannot be read. The sum of the numbers dominates each of them and also covers the empty input.

    The first clause is soundness at every word length of the hypothesis. An algorithm that calls another one runs it at word lengths fixed by its own memory, where the running time of the callee need not fit into a word. It can trust any output it receives. The second clause claims halting only once the time bound fits into a word, the usual word RAM convention that a word holds the running time. This matters when the output is large. Its size can be polynomial of a degree that grows with parameters fixed after e, and then at some word lengths of the hypothesis the output cannot be written. Since the machine is deterministic, the two clauses together say that the run is correct whenever it halts, and that it halts in time whenever the time fits.

    The time bound is a real number, so that bounds like n^(3+ε) can be written directly. The additive c lets T vanish on small inputs, as O-notation does. Several outputs are computed as one pair.

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…