Weighted simple paths, bounded-word output, and disjointness matrices
DefinitionWeightedDirectedKPath_InterfacesExplicit weighted digraph input, exact path costs, a two-word answer format, and the two matrices and representation order used for deterministic updates.
import Definitions.Def_EdgeSublinearMWIS_Main_NativeWordRAM
import Mathlib.Data.Matrix.Mul
import Mathlib.Data.Fintype.Powerset
import Mathlib.Data.Nat.Sqrt
namespace WeightedDirectedKPath
structure Arc (n : ℕ) where
src : Fin n
dst : Fin n
weight : ℕ
deriving DecidableEq
abbrev Graph (n : ℕ) := List (Arc n)
def valid {n : ℕ} (E : Graph n) : Prop := (E.map fun e => (e.src, e.dst)).Nodup
def input {n : ℕ} (E : Graph n) (k : ℕ) : List ℕ :=
[n, E.length, k] ++ E.flatMap (fun e => [e.src.val, e.dst.val, e.weight])
def pathWeight {n : ℕ} (E : Graph n) (k z : ℕ) : Prop :=
∃ (vs : List (Fin n)) (ws : List ℕ),
vs.length = k ∧ vs.Nodup ∧
List.Forall₂ (fun (uv : Fin n × Fin n) w => Arc.mk uv.1 uv.2 w ∈ E)
(vs.zip vs.tail) ws ∧ ws.sum = z
def answer {n : ℕ} (E : Graph n) (k w : ℕ) (out : List ℕ) : Prop :=
(out = [0] ∧ ¬ ∃ z, pathWeight E k z) ∨
∃ z, pathWeight E k z ∧ (∀ z', pathWeight E k z' → z ≤ z') ∧
out = [1, z / (2 ^ w), z % (2 ^ w)]
def overhead (k : ℕ) : ℕ :=
(Nat.sqrt k + 1) * (1 + Nat.log2 (k + 1)) ^ 2
def timeBound (c k n m : ℕ) : ℕ :=
c * 2 ^ (k + c * overhead k) * (n + m + 1) * (1 + Nat.log2 (n + 1))
abbrev Subsets (n : ℕ) := Finset (Fin n)
def extend (S : Type*) [Zero S] [One S] {n : ℕ} (e : Fin n) :
Matrix (Subsets n) (Subsets n) S :=
fun A B => if e ∉ A ∧ insert e A = B then 1 else 0
def disjointMatrix (S : Type*) [Zero S] [One S] (n k : ℕ) :
Matrix (Subsets n) (Subsets n) S :=
fun A B => if Disjoint A B ∧ (A ∪ B).card ≤ k then 1 else 0
def below {S ι : Type*} [Add S] (x y : ι → S) : Prop := ∀ i, x i + y i = x i
def represents {S : Type*} [Semiring S] {n k r : ℕ}
(L : Matrix (Subsets n) (Fin r) S) (R : Matrix (Fin r) (Subsets n) S)
(a : Subsets n → S) (b : Fin r → S) : Prop :=
below (Matrix.vecMul a (disjointMatrix S n k)) (Matrix.vecMul b R) ∧
below b (Matrix.vecMul a L)
def updateLaws {S : Type*} [Semiring S] {n r : ℕ}
(L : Matrix (Subsets n) (Fin r) S) (R : Matrix (Fin r) (Subsets n) S)
(U : (Fin r → S) → Fin n → (Fin r → S)) : Prop :=
∀ b e, below (Matrix.vecMul (Matrix.vecMul b R) (extend S e).transpose)
(Matrix.vecMul (U b e) R) ∧
∀ a : Subsets n → S, below b (Matrix.vecMul a L) →
below (U b e) (Matrix.vecMul (Matrix.vecMul a (extend S e)) L)
end WeightedDirectedKPathRead-back
What the Lean code literally says, in plain math · GPT-6
Weighted paths and subset matrices use the same vertex set for each natural , with . A path on vertices carries one arc weight for each consecutive pair.
Graphs and path weights
An arc is a triple with decidable equality by coordinates. A graph is a finite list of arcs. It is valid when its ordered pairs are all distinct. Loops are allowed. For any arc list, a natural weight is realized at if there is a list of distinct vertices and a list of weights, one for each consecutive pair in order, such that every resulting arc belongs to and the weights sum to . The empty path has weight . A one-vertex path exists exactly when and has weight . No path has .
Subset matrices
Write . For any type with chosen elements , define two matrices. For , the entry is if and , and otherwise. The entry is if and , and otherwise. The two chosen scalars may coincide.
Vector representation
For any index set and type with addition, write for vectors when for every . Now take a semiring , independent natural numbers , and matrices and . A row represents a row when both and hold.
Updates
A function satisfies the update laws when, for every and , it satisfies both requirements below. Products are row-vector or matrix products over .
- .
- For every with , one has .
Numerical bounds
For natural , put . The time-bound function is
In particular, and .
Input and output
The input list starts with , followed by each arc's source, destination and weight in list order. At any natural word width , the accepted output is if no weight is realized. Otherwise, for the least realized weight , it is . At this last list is .