Documentation

CslibFrontier.Algorithms.Lean.Matrix.SMAWK

Monge matrices, total monotonicity, and the SMAWK algorithm #

An m × n matrix M : ℕ → ℕ → β is Monge (IsMonge) if M i₁ j₁ + M i₂ j₂ ≤ M i₁ j₂ + M i₂ j₁ for all i₁ < i₂ < m and j₁ < j₂ < n, and totally monotone (IsTotallyMonotone) if M i₁ j₂ < M i₁ j₁ → M i₂ j₂ < M i₂ j₁ for all i₁ < i₂ < m and j₁ < j₂ < n. Every Monge matrix over an ordered cancellative additive commutative monoid is totally monotone (IsMonge.isTotallyMonotone).

The SMAWK algorithm of Aggarwal, Klawe, Moran, Shor and Wilber finds the leftmost minimizing column of every row of an m × n totally monotone matrix in O(m + n) entry comparisons [AKM+87]:

  1. reduceQ prunes the candidate columns cols to a sublist of length at most rows.length while preserving the leftmost minimum of every row in rows.
  2. smawkQ applies reduceQ, recurses on the odd-indexed rows oddRows, and interpolates the even-indexed rows between the adjacent odd-row minima.

The algorithm is written directly in CSLib's comparison-query model FreeM (LEQuery (ℕ × ℕ)), where each matrix comparison (r, c₁) ≤ (r, c₂) is a query (LEQuery.ask) and pure index and list bookkeeping is uncharged. Thus smawkQ_countQueries_le and rowMinimaQ_upperBound certify linear comparison-query complexity (≤ 4 m + 2 n comparisons against every oracle), not a Word-RAM running time. Evaluating smawkQ / rowMinimaQ against LEQuery.keyOracle (Function.uncurry M) yields smawk / rowMinima, proved exact on every totally monotone matrix (smawk_spec, isLeftmostRowMin_rowMinima).

Main definitions #

Main results #

References #

Monge matrices, total monotonicity, and leftmost row minima #

def Cslib.Algorithms.Lean.Matrix.SMAWK.IsMonge {β : Type u_1} [Add β] [LE β] (M : ℕ → ℕ → β) (m n : ℕ) :

An m × n matrix M is Monge if M i₁ j₁ + M i₂ j₂ ≤ M i₁ j₂ + M i₂ j₁ for all i₁ < i₂ < m and j₁ < j₂ < n.

Instances For
    def Cslib.Algorithms.Lean.Matrix.SMAWK.IsTotallyMonotone {β : Type u_1} [LT β] (M : ℕ → ℕ → β) (m n : ℕ) :

    An m × n matrix M is totally monotone (for row minima) if every 2 × 2 submatrix satisfies M i₁ j₂ < M i₁ j₁ → M i₂ j₂ < M i₂ j₁ for i₁ < i₂ < m and j₁ < j₂ < n.

    Instances For
      def Cslib.Algorithms.Lean.Matrix.SMAWK.IsLeftmostMinOn {β : Type u_1} [LE β] [LT β] (M : ℕ → ℕ → β) (r : ℕ) (cols : List ℕ) (c : ℕ) :

      Column c is the leftmost minimizer of row r among the candidate columns cols.

      def Cslib.Algorithms.Lean.Matrix.SMAWK.IsLeftmostRowMin {β : Type u_1} [LE β] [LT β] (M : ℕ → ℕ → β) (n r c : ℕ) :

      Column c is the leftmost minimizer of row r among all columns 0, …, n - 1.

      Instances For
        @[instance_reducible]
        instance Cslib.Algorithms.Lean.Matrix.SMAWK.instDecidableIsMongeOfDecidableLE {β : Type u_1} [Add β] [LE β] [DecidableLE β] {M : ℕ → ℕ → β} {m n : ℕ} :
        @[instance_reducible]
        @[instance_reducible]

        Every Monge matrix over an ordered cancellative additive commutative monoid is totally monotone.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.IsTotallyMonotone.le_of_le {β : Type u_1} [LinearOrder β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) {i₁ i₂ j₁ j₂ : ℕ} (hi₁ : i₁ < m) (hi₂ : i₂ < m) (hi : i₁ < i₂) (hj₁ : j₁ < n) (hj₂ : j₂ < n) (hj : j₁ < j₂) (hle : M i₂ j₁ ≤ M i₂ j₂) :
        M i₁ j₁ ≤ M i₁ j₂

        Contrapositive form of total monotonicity: if j₁ < j₂ and M i₂ j₁ ≤ M i₂ j₂, then M i₁ j₁ ≤ M i₁ j₂ for every earlier row i₁ < i₂.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.isLeftmostMinOn_range_iff {β : Type u_1} [LE β] [LT β] {M : ℕ → ℕ → β} {n r c : ℕ} :
        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.IsLeftmostMinOn.unique {β : Type u_1} [LinearOrder β] {M : ℕ → ℕ → β} {r : ℕ} {cols : List ℕ} {c₁ c₂ : ℕ} (h₁ : IsLeftmostMinOn M r cols c₁) (h₂ : IsLeftmostMinOn M r cols c₂) :
        c₁ = c₂
        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.IsLeftmostMinOn.mono_subset {β : Type u_1} [LE β] [LT β] {M : ℕ → ℕ → β} {r : ℕ} {cols cols' : List ℕ} {c : ℕ} (h : IsLeftmostMinOn M r cols c) (hsub : ∀ x ∈ cols', x ∈ cols) (hmem : c ∈ cols') :
        IsLeftmostMinOn M r cols' c
        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.exists_isLeftmostMinOn {β : Type u_1} [LinearOrder β] (M : ℕ → ℕ → β) (r : ℕ) {cols : List ℕ} (hne : cols ≠ []) :
        ∃ (c : ℕ), IsLeftmostMinOn M r cols c

        Every nonempty column list admits a leftmost minimizer for row r.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.IsTotallyMonotone.leftmostMinOn_mono {β : Type u_1} [LinearOrder β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) {cols : List ℕ} (hcols : ∀ c ∈ cols, c < n) {r₁ r₂ c₁ c₂ : ℕ} (hr₁ : r₁ < m) (hr₂ : r₂ < m) (hr : r₁ < r₂) (h₁ : IsLeftmostMinOn M r₁ cols c₁) (h₂ : IsLeftmostMinOn M r₂ cols c₂) :
        c₁ ≤ c₂

        In a totally monotone matrix, leftmost row minima are monotone nondecreasing in the row index.

        SMAWK in FreeM (LEQuery (ℕ × ℕ)) #

        Odd-indexed rows rows[1], rows[3], ….

        @[irreducible]

        Stack-based column reduction step of SMAWK. The surviving column stack stk is stored in reverse order (stk.reverse is increasing).

        Column-reduction phase of SMAWK in FreeM (LEQuery (ℕ × ℕ)): prunes cols to a sublist of length at most rows.length while preserving the leftmost minimum of every row in rows.

        Split l at the first occurrence of x, returning (pref, x :: after) or none if x ∉ l.

        Scan cs for the leftmost column minimizing row r, starting from incumbent best.

        Leftmost minimizing column of row r over a nonempty column list (or 0 if empty).

        Interpolation phase of SMAWK: given the odd-row minima oddMins and remaining candidate columns remCols, scan the bracketed intervals for the even-indexed rows and interleave them with oddMins.

        @[irreducible]

        SMAWK algorithm in FreeM (LEQuery (ℕ × ℕ)): computes the leftmost minimizing column of every row in rows among cols, using at most 4 * rows.length + 2 * cols.length entry comparisons against every oracle.

        Comparison-query program computing the leftmost minimizing column of every row of an m × n matrix.

        def Cslib.Algorithms.Lean.Matrix.SMAWK.reduce {β : Type u_1} [LE β] [DecidableLE β] (M : ℕ → ℕ → β) (rows cols : List ℕ) :

        Pure evaluation of reduceQ against matrix M.

        def Cslib.Algorithms.Lean.Matrix.SMAWK.smawk {β : Type u_1} [LE β] [DecidableLE β] (M : ℕ → ℕ → β) (rows cols : List ℕ) :

        Pure evaluation of smawkQ against matrix M on row list rows and column list cols.

        def Cslib.Algorithms.Lean.Matrix.SMAWK.rowMinima {β : Type u_1} [LE β] [DecidableLE β] (M : ℕ → ℕ → β) (m n : ℕ) :

        Leftmost row minima of an m × n matrix M, computed by SMAWK.

        Comparison-query complexity bounds (for every oracle) #

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.reduceQ_countQueries_add_length_le (oracle : {ι : Type} → Query.LEQuery (ℕ × ℕ) ι → ι) (rows cols : List ℕ) :
        (FreeM.eval (fun {ι : Type} => oracle) (reduceQ rows cols)).length ≤ rows.length ∧ (FreeM.eval (fun {ι : Type} => oracle) (reduceQ rows cols)).Sublist cols ∧ FreeM.countQueries (fun {ι : Type} => oracle) (reduceQ rows cols) + (FreeM.eval (fun {ι : Type} => oracle) (reduceQ rows cols)).length ≤ 2 * cols.length

        For every oracle, reduceQ returns a sublist of cols of length at most rows.length, and the sum of its query count and output length is at most 2 * cols.length.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.smawkQ_countQueries_le (oracle : {ι : Type} → Query.LEQuery (ℕ × ℕ) ι → ι) (rows cols : List ℕ) :
        FreeM.countQueries (fun {ι : Type} => oracle) (smawkQ rows cols) ≤ 4 * rows.length + 2 * cols.length

        Linear comparison-query bound for SMAWK: for every oracle, smawkQ rows cols makes at most 4 * rows.length + 2 * cols.length comparison queries. This measures entry comparisons in FreeM (LEQuery (ℕ × ℕ)) and makes no claim about Word-RAM running time.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.rowMinimaQ_countQueries_le (oracle : {ι : Type} → Query.LEQuery (ℕ × ℕ) ι → ι) (m n : ℕ) :
        FreeM.countQueries (fun {ι : Type} => oracle) (rowMinimaQ m n) ≤ 4 * m + 2 * n

        For every oracle, rowMinimaQ m n makes at most 4 * m + 2 * n comparison queries.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.rowMinimaQ_upperBound :
        Query.UpperBound (fun (p : ℕ × ℕ) => rowMinimaQ p.1 p.2) (fun (p : ℕ × ℕ) => p.1 + p.2) fun (x : ℕ) => 4 * x

        Cslib.Query.UpperBound formulation of the O(m + n) comparison-query bound for SMAWK: on matrices of size m + n ≤ k, rowMinimaQ m n makes at most 4 * k comparisons against every oracle.

        Exact correctness on totally monotone matrices #

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.reduce_spec {β : Type u_1} [LinearOrder β] [DecidableLE β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) {rows cols : List ℕ} (hrows_pw : List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) rows) (hrows_lt : ∀ r ∈ rows, r < m) (hcols_pw : List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) cols) (hcols_lt : ∀ c ∈ cols, c < n) :
        (reduce M rows cols).length ≤ rows.length ∧ (reduce M rows cols).Sublist cols ∧ ∀ r ∈ rows, ∀ (c : ℕ), IsLeftmostMinOn M r cols c → IsLeftmostMinOn M r (reduce M rows cols) c

        Correctness of reduce: on a totally monotone m × n matrix, reduce M rows cols is a sublist of cols of length at most rows.length that retains the leftmost minimizing column of every row in rows.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.smawk_spec {β : Type u_1} [LinearOrder β] [DecidableLE β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) {rows cols : List ℕ} (hrows_pw : List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) rows) (hrows_lt : ∀ r ∈ rows, r < m) (hcols_ne : cols ≠ []) (hcols_pw : List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) cols) (hcols_lt : ∀ c ∈ cols, c < n) :
        List.Forall₂ (fun (r c : ℕ) => IsLeftmostMinOn M r cols c) rows (smawk M rows cols)

        Correctness of smawk: on a totally monotone m × n matrix, for strictly increasing rows (all < m) and nonempty strictly increasing cols (all < n), smawk M rows cols returns the exact leftmost minimizing column in cols for each row in rows.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.length_rowMinima {β : Type u_1} [LinearOrder β] [DecidableLE β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) (hn : 0 < n) :
        (rowMinima M m n).length = m

        On any totally monotone m × n matrix with 0 < n, rowMinima M m n has length m.

        theorem Cslib.Algorithms.Lean.Matrix.SMAWK.isLeftmostRowMin_rowMinima {β : Type u_1} [LinearOrder β] [DecidableLE β] {M : ℕ → ℕ → β} {m n : ℕ} (hTM : IsTotallyMonotone M m n) (hn : 0 < n) (i : ℕ) (hi : i < m) :

        Exact optimality of SMAWK: on any totally monotone m × n matrix with 0 < n, the i-th entry of rowMinima M m n is the leftmost minimizing column of row i.