Lax11.ConnectedComponents
Connected components in linear time
concepts/Lax11/ConnectedComponents.lean · lax-11
No public endorsements yet.
Loading review…
Sign in with ORCIDConcept map
Theorem
The connected components of a graph can be computed in linear time. Label every vertex by the least vertex of its connected component; then there is one word RAM program and one constant c such that, at every word length, given any graph in compressed sparse row form as a word x for which c(|x|+1) is at most , the program halts within c(|x|+1) steps with the labels of all vertices, in vertex order, as its output.
Lean source view on GitHub
| 1 | import Lax67.RamComputes |
| 2 | import Lax11.GraphEncoding |
| 3 | import Mathlib.Combinatorics.SimpleGraph.Connectivity.Connected |
| 4 | import Mathlib.Data.Nat.Lattice |
| 5 | |
| 6 | /-! |
| 7 | --- |
| 8 | title: Connected components in linear time |
| 9 | type: theorem |
| 10 | --- |
| 11 | The connected components of a graph can be computed in linear time. |
| 12 | Label every vertex by the least vertex of its connected component; then |
| 13 | there is one word RAM program and one constant *c* such that, at every |
| 14 | word length, given any graph in compressed sparse row form as a word |
| 15 | *x* for which *c*(|x|+1) is at most `2 ^ w`, the program halts within |
| 16 | *c*(|x|+1) steps with the labels of all vertices, in vertex order, as |
| 17 | its output. |
| 18 | |
| 19 | # Formalization notes |
| 20 | |
| 21 | Labelling a vertex by the least vertex reachable from it makes the |
| 22 | output a *function* of the graph, so the statement is about computing a |
| 23 | function and needs no convention for choosing representatives. Any |
| 24 | other canonical choice would do; what matters is that the answer is |
| 25 | determined, since a program that may return any of several correct |
| 26 | answers would be a weaker claim dressed up as this one. |
| 27 | |
| 28 | The least vertex is the infimum of the set of numbers of vertices |
| 29 | reachable from `v`. That set contains `v` itself, so the value is a |
| 30 | genuine minimum and the convention `sInf ∅ = 0` for natural numbers is |
| 31 | never exercised. The labelling of the whole graph is the list of these |
| 32 | values in vertex order, so its length is the number of vertices. |
| 33 | |
| 34 | The order of quantifiers is the content of the theorem: the program and |
| 35 | the constant come first, the graph next and the word length last, so |
| 36 | one program with one constant serves every graph at every word length. |
| 37 | Quantifying the program before the word length is what makes it an |
| 38 | algorithm rather than a family of them — a program chosen after `w` |
| 39 | could hide an arbitrary amount of information in its literals — and it |
| 40 | is the strong form of uniformity the model supports, since a program |
| 41 | can measure `w` for itself. |
| 42 | |
| 43 | The bound is linear in the length of the input word — the number of |
| 44 | entries actually handed to the machine, namely `3 + n + 2m` — which is |
| 45 | the input size in the sense the model charges for. Reading the input |
| 46 | alone takes that many steps, so the bound is tight up to the constant. |
| 47 | The `+ 1` only keeps the bound from being vacuous on inputs of length |
| 48 | 0, of which there are none valid. |
| 49 | |
| 50 | One constant does both jobs. An encoding is admissible at word length |
| 51 | `w` when `c * (|x| + 1) ≤ 2 ^ w`, that is, when `2 ^ w` is at least |
| 52 | the very number of steps the claim allows; this is the "the word is |
| 53 | wide enough for the input" hypothesis of the word-RAM literature, written |
| 54 | out as an explicit inequality against `2 ^ w` rather than through a |
| 55 | logarithm. Nothing further need be asked of the entries: an encoding's |
| 56 | entries are vertex numbers, offsets and the two header numbers, all |
| 57 | smaller than its own length, so a word length that admits an encoding |
| 58 | also holds every number in it. |
| 59 | |
| 60 | The fitting condition is a condition on the admissible inputs and not a |
| 61 | hypothesis of the claim, because as a hypothesis it would be empty. A |
| 62 | graph with an edge has encodings of every length, since a block may |
| 63 | list a neighbour repeatedly, so no word length accommodates all |
| 64 | encodings of a fixed graph at once and "if every encoding of `G` fits |
| 65 | into a word" would never be satisfied. Restricting the inputs instead |
| 66 | says what is meant: at every word length, every encoding that fits is |
| 67 | computed within the bound. |
| 68 | |
| 69 | Only encodings of `G` are admitted as inputs; the program may behave |
| 70 | arbitrarily on words that encode nothing, and on words too long for its |
| 71 | word length. |
| 72 | -/ |
| 73 | |
| 74 | namespace Lax11.ConnectedComponents |
| 75 | |
| 76 | open Lax67.Ram Lax67.RamComputes Lax11.GraphEncoding |
| 77 | |
| 78 | /-- The label of a vertex: the least vertex of its connected |
| 79 | component. -/ |
| 80 | noncomputable def label {n : ℕ} (G : SimpleGraph (Fin n)) (v : Fin n) : ℕ := |
| 81 | sInf (Fin.val '' {u : Fin n | G.Reachable u v}) |
| 82 | |
| 83 | /-- The component labelling of a graph: the labels of all vertices, in |
| 84 | vertex order. -/ |
| 85 | noncomputable def ccLabels {n : ℕ} (G : SimpleGraph (Fin n)) : List ℕ := |
| 86 | List.ofFn (label G) |
| 87 | |
| 88 | /-- Connected components can be computed in linear time on a word |
| 89 | random access machine: one program labels the vertices of every graph |
| 90 | given in compressed sparse row form by the least vertex of their |
| 91 | component, within a constant multiple of the length of the input, at |
| 92 | every word length `w` with that constant multiple at most `2 ^ w`. -/ |
| 93 | axiom exists_linearTime_program_ccLabels : |
| 94 | ∃ (p : Program) (c : ℕ), ∀ (n : ℕ) (G : SimpleGraph (Fin n)) (w : ℕ), |
| 95 | ComputesInTime w p {x | EncodesGraph x n G ∧ c * (x.length + 1) ≤ 2 ^ w} |
| 96 | (fun _ => ccLabels G) (fun x => c * (x.length + 1)) |
| 97 | |
| 98 | end Lax11.ConnectedComponents |
| 99 |
Formalization notes
Labelling a vertex by the least vertex reachable from it makes the output a function of the graph, so the statement is about computing a function and needs no convention for choosing representatives. Any other canonical choice would do; what matters is that the answer is determined, since a program that may return any of several correct answers would be a weaker claim dressed up as this one.
The least vertex is the infimum of the set of numbers of vertices reachable from . That set contains itself, so the value is a genuine minimum and the convention for natural numbers is never exercised. The labelling of the whole graph is the list of these values in vertex order, so its length is the number of vertices.
The order of quantifiers is the content of the theorem: the program and the constant come first, the graph next and the word length last, so one program with one constant serves every graph at every word length. Quantifying the program before the word length is what makes it an algorithm rather than a family of them — a program chosen after could hide an arbitrary amount of information in its literals — and it is the strong form of uniformity the model supports, since a program can measure for itself.
The bound is linear in the length of the input word — the number of entries actually handed to the machine, namely — which is the input size in the sense the model charges for. Reading the input alone takes that many steps, so the bound is tight up to the constant. The only keeps the bound from being vacuous on inputs of length 0, of which there are none valid.
One constant does both jobs. An encoding is admissible at word length when , that is, when is at least the very number of steps the claim allows; this is the "the word is wide enough for the input" hypothesis of the word-RAM literature, written out as an explicit inequality against rather than through a logarithm. Nothing further need be asked of the entries: an encoding's entries are vertex numbers, offsets and the two header numbers, all smaller than its own length, so a word length that admits an encoding also holds every number in it.
The fitting condition is a condition on the admissible inputs and not a hypothesis of the claim, because as a hypothesis it would be empty. A graph with an edge has encodings of every length, since a block may list a neighbour repeatedly, so no word length accommodates all encodings of a fixed graph at once and "if every encoding of fits into a word" would never be satisfied. Restricting the inputs instead says what is meant: at every word length, every encoding that fits is computed within the bound.
Only encodings of are admitted as inputs; the program may behave arbitrarily on words that encode nothing, and on words too long for its word length.
Builds on
Used by
none
Community review
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above; your ORCID profile must share a public name.
0 comments