Algorithms and their running times
Lax185574.Algorithms · concepts/Lax185574/Algorithms.lean · lax-185574
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
An algorithm is a program of the word RAM of . It reads the encoding of a value and writes the encoding of a value, in the encodings of .
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,
- every run of A on x that halts outputs the encoding of b, and
- 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
Lean source view on GitHub
| 1 | import Lax185574.Encodings |
| 2 | import Lax808846.Ram |
| 3 | import Mathlib.Data.Real.Basic |
| 4 | |
| 5 | /-! |
| 6 | --- |
| 7 | title: Algorithms and their running times |
| 8 | type: definition |
| 9 | --- |
| 10 | An *algorithm* is a program of the word RAM of `Lax808846.Ram`. It reads |
| 11 | the encoding of a value and writes the encoding of a value, in the |
| 12 | encodings of `Lax185574.Encoding`. |
| 13 | |
| 14 | The word RAM computes with words of w bits. As usual, the word length |
| 15 | must grow logarithmically with the input, so that a word can hold an |
| 16 | address of the input and each number of the input. Each algorithm states |
| 17 | how much it needs: an exponent e such that it works with words of |
| 18 | e · log₂ (N + Σ) + O(1) bits on inputs of length N whose numbers sum to |
| 19 | Σ. |
| 20 | |
| 21 | The algorithm A *computes* b from a *in time O(T)*, with the constant c, |
| 22 | if the following holds for the encoding x of a, of length N, whose |
| 23 | numbers sum to Σ. At every word length w with c · (N + Σ + 1)^e ≤ 2^w, |
| 24 | |
| 25 | 1. every run of A on x that halts outputs the encoding of b, and |
| 26 | 2. if c · T + c ≤ 2^w, then A halts on x within c · T + c steps. |
| 27 | |
| 28 | A word length w with c · (N + Σ + 1)^e ≤ 2^w is called *admissible* for |
| 29 | c on the input x. |
| 30 | |
| 31 | The algorithm A *decides* a property P of a in time O(T) if it computes |
| 32 | the truth value of P in time O(T). |
| 33 | |
| 34 | # Formalization notes |
| 35 | |
| 36 | A statement about an algorithm has the form "there is an algorithm A |
| 37 | such that for all parameters there is a constant c such that for all |
| 38 | inputs a, A computes b from a in time O(T)". The program and the |
| 39 | exponent e come first, so neither depends on the parameters or on the |
| 40 | word length. The constant c is O-notation with explicit constants. It |
| 41 | bounds both the running time and the additive term of the word length. |
| 42 | |
| 43 | The word length bound includes the numbers of the input, since the |
| 44 | machine reduces every number modulo 2^w, so a number that does not fit |
| 45 | into a word cannot be read. The sum of the numbers dominates each of |
| 46 | them and also covers the empty input. |
| 47 | |
| 48 | The first clause is soundness at every word length of the hypothesis. |
| 49 | An algorithm that calls another one runs it at word lengths fixed by its |
| 50 | own memory, where the running time of the callee need not fit into a |
| 51 | word. It can trust any output it receives. The second clause claims |
| 52 | halting only once the time bound fits into a word, the usual word RAM |
| 53 | convention that a word holds the running time. This matters when the |
| 54 | output is large. Its size can be polynomial of a degree that grows with |
| 55 | parameters fixed after e, and then at some word lengths of the |
| 56 | hypothesis the output cannot be written. Since the machine is |
| 57 | deterministic, the two clauses together say that the run is correct |
| 58 | whenever it halts, and that it halts in time whenever the time fits. |
| 59 | |
| 60 | The time bound is a real number, so that bounds like n^(3+ε) can be |
| 61 | written directly. The additive c lets T vanish on small inputs, as |
| 62 | O-notation does. Several outputs are computed as one pair. |
| 63 | -/ |
| 64 | |
| 65 | namespace Lax185574.Algorithms |
| 66 | |
| 67 | open Lax808846.Ram Lax185574.Encoding |
| 68 | |
| 69 | /-- An algorithm: a word RAM program, together with the exponent `e` |
| 70 | such that it needs words of `e · log₂ (N + Σ) + O(1)` bits on inputs of |
| 71 | length `N` whose numbers sum to `Σ`. -/ |
| 72 | structure 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 |
| 80 | word length `w` with `c · (|x| + Σx + 1)^e ≤ 2^w`, where `Σx` is the sum |
| 81 | of the numbers of `x`, |
| 82 | 1. every halting run of `A` on `x` outputs the encoding of `b`, and |
| 83 | 2. if `c · T + c ≤ 2^w`, then `A` halts on `x` within `c · T + c` steps, |
| 84 | with the encoding of `b` as output. -/ |
| 85 | def 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 | |
| 92 | open Classical in |
| 93 | /-- `A.Decides c a P T`: the algorithm `A` computes from `a` whether `P` |
| 94 | holds, writing `true` (encoded as `1`) or `false` (encoded as `0`), in |
| 95 | time `O(T)` with the constant `c`. -/ |
| 96 | def Algorithm.Decides {α : Type*} [Encode α] (A : Algorithm) (c : ℕ) (a : α) (P : Prop) |
| 97 | (T : ℝ) : Prop := |
| 98 | A.Computes c a (decide P) T |
| 99 | |
| 100 | end 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.
Builds on
Used by
Lax185574.BetterMergeSequencesLax185574.CanonicalFormLax185574.ChainConstructionSequencesLax185574.ChainTheoremLax185574.ComputeScatterTypeLax185574.ComputingChainsLax185574.ConstructionModelCheckingLax185574.DefinableSequencesLax185574.EvaluationLax185574.FarCompositionLax185574.FewTypesLax185574.MergeLiftLax185574.ModelCheckingLax185574.RankShiftLax185574.ResolveLiftLax185574.RestrictionLax185574.ToFarTree
From Mathlib
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments