Combinatorial invariants and Floyd-Warshall closure for weighted deadline DP
DefinitionCountingLinExt33_DeadlineDPAlgoalgorithmscombinatoricsposets
Defines the Boolean transitive closure boolClosure (Floyd-Warshall), augmented partial-order acyclicity predicates, and reachable-state invariants (IdealOn) together with bit-length bounds on DP weights for the weighted deadline dynamic program.
Definition code
import Mathlib
import Definitions.Def_FCP_LinearExtensions
import Definitions.Def_CountingLinExt33_Main_Orders
import Definitions.Def_CountingLinExt33_Combinatorics
import Theorems.Thm_CountingLinExt33_partial_patterns
import Theorems.Thm_CountingLinExt33_deadline_dp_count
set_option autoImplicit false
/-!
# Pure Functional Deadline DP Algorithm (`DeadlineDPAlgo`)
Contains:
1. Floyd-Warshall Boolean closure (`boolClosure`) correctness and augmented acyclicity.
2. Reachable state invariants (`IdealOn`) and polynomial-bit-length bounds on DP weights and states.
3. Exact transition characterization (`AntichainStep` and `CoreStep`) matching the RAM loop.
4. Iterative DFS over `active P B` with step bound `(n + 2) * (n + 1) * numCompatible P B`.
-/
namespace DeadlineDP
open CountingLinExt33 CountingLinExt33.Main
open scoped Classical BigOperators
/-! ## Section 1: Floyd-Warshall Boolean Closure -/
/-- One Floyd-Warshall relaxation step using intermediate vertex `k`. -/
def fwStep {n : ℕ} (m : Fin n → Fin n → Bool) (k : Fin n) : Fin n → Fin n → Bool :=
fun i j => m i j || (m i k && m k j)
/-- First `r` steps of Floyd-Warshall (`k = 0, ..., r - 1`). -/
def fwPrefix {n : ℕ} (a : Fin n → Fin n → Bool) : ℕ → Fin n → Fin n → Bool
| 0 => a
| r + 1 =>
if hr : r < n then fwStep (fwPrefix a r) ⟨r, hr⟩
else fwPrefix a r
lemma fwPrefix_eq_foldl_take {n : ℕ} (a : Fin n → Fin n → Bool) :
∀ r ≤ n, fwPrefix a r = ((List.finRange n).take r).foldl fwStep a := by
intro r
induction r with
| zero => intro _; simp [fwPrefix]
| succ r ih =>
intro hr
have hlt : r < n := by omega
simp only [fwPrefix, hlt, ↓reduceDIte]
have htake : (List.finRange n).take (r + 1) = (List.finRange n).take r ++ [⟨r, hlt⟩] := by
rw [List.take_succ_eq_append_getElem (by simp; omega)]
simp
rw [htake, List.foldl_append, List.foldl_cons, List.foldl_nil, ← ih (by omega)]
lemma boolClosure_eq_fwPrefix {n : ℕ} (a : Fin n → Fin n → Bool) :
boolClosure a = fwPrefix a n := by
unfold boolClosure
rw [fwPrefix_eq_foldl_take a n le_rfl]
rw [List.take_of_length_le (by simp)]
rfl
/-- Reachability using only intermediate vertices `< r`. -/
inductive ReachUnder {n : ℕ} (a : Fin n → Fin n → Bool) : ℕ → Fin n → Fin n → Prop
| edge {r i j} : a i j = true → ReachUnder a r i j
| trans {r i k j} : k.val < r → ReachUnder a r i k → ReachUnder a r k j → ReachUnder a r i j
lemma reachUnder_mono {n : ℕ} {a : Fin n → Fin n → Bool} {r₁ r₂ : ℕ} (hle : r₁ ≤ r₂)
{i j : Fin n} (h : ReachUnder a r₁ i j) : ReachUnder a r₂ i j := by
induction h with
| edge he => exact .edge he
| trans hk _ _ ih1 ih2 => exact .trans (lt_of_lt_of_le hk hle) ih1 ih2
lemma reachUnder_succ_iff {n : ℕ} {a : Fin n → Fin n → Bool} {r : ℕ} (hr : r < n) {i j : Fin n} :
ReachUnder a (r + 1) i j ↔
ReachUnder a r i j ∨ (ReachUnder a r i ⟨r, hr⟩ ∧ ReachUnder a r ⟨r, hr⟩ j) := by
constructor
· intro h
induction h with
| edge he => exact Or.inl (.edge he)
| @trans _ k _ hk _ _ ih1 ih2 =>
rcases ih1 with h1 | ⟨h1a, h1b⟩
· rcases ih2 with h2 | ⟨h2a, h2b⟩
· by_cases hkr : k.val < r
· exact Or.inl (.trans hkr h1 h2)
· have hkeq : k = ⟨r, hr⟩ := Fin.ext (by dsimp; omega)
rw [hkeq] at h1 h2
exact Or.inr ⟨h1, h2⟩
· by_cases hkr : k.val < r
· exact Or.inr ⟨.trans hkr h1 h2a, h2b⟩
· have hkeq : k = ⟨r, hr⟩ := Fin.ext (by dsimp; omega)
rw [hkeq] at h1
exact Or.inr ⟨h1, h2b⟩
· rcases ih2 with h2 | ⟨h2a, h2b⟩
· by_cases hkr : k.val < r
· exact Or.inr ⟨h1a, .trans hkr h1b h2⟩
· have hkeq : k = ⟨r, hr⟩ := Fin.ext (by dsimp; omega)
rw [hkeq] at h2
exact Or.inr ⟨h1a, h2⟩
· exact Or.inr ⟨h1a, h2b⟩
· intro h
rcases h with h | ⟨h1, h2⟩
· exact reachUnder_mono (Nat.le_succ r) h
· exact .trans (by simp) (reachUnder_mono (Nat.le_succ r) h1) (reachUnder_mono (Nat.le_succ r) h2)
lemma fwPrefix_iff_reachUnder {n : ℕ} (a : Fin n → Fin n → Bool) :
∀ r ≤ n, ∀ i j, fwPrefix a r i j = true ↔ ReachUnder a r i j := by
intro r
induction r with
| zero =>
intro _ i j
constructor
· intro h; exact .edge h
· intro h
induction h with
| edge he => exact he
| trans hk => omega
| succ r ih =>
intro hr i j
have hlt : r < n := by omega
simp only [fwPrefix, hlt, ↓reduceDIte, fwStep, Bool.or_eq_true, Bool.and_eq_true]
rw [ih (by omega), ih (by omega), ih (by omega), ← reachUnder_succ_iff hlt]
lemma reachUnder_n_iff_transGen {n : ℕ} (a : Fin n → Fin n → Bool) (i j : Fin n) :
ReachUnder a n i j ↔ Relation.TransGen (fun x y => a x y = true) i j := by
constructor
· intro h
induction h with
| edge he => exact Relation.TransGen.single he
| trans _ _ _ ih1 ih2 => exact ih1.trans ih2
· intro h
induction h with
| single he => exact .edge he
| tail _ he ih => exact .trans (Fin.isLt _) ih (.edge he)
theorem boolClosure_iff_transGen {n : ℕ} (a : Fin n → Fin n → Bool) (i j : Fin n) :
boolClosure a i j = true ↔ Relation.TransGen (fun x y => a x y = true) i j := by
rw [boolClosure_eq_fwPrefix, fwPrefix_iff_reachUnder a n le_rfl, reachUnder_n_iff_transGen]
/-- Boolean augmented adjacency matrix for `(R, B, g)`. -/
def augAdj {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (x y : Fin n) : Bool :=
R x y || decide (∃ v ∈ B, g v = some x ∧ y ∉ B ∧ R v y = true ∧ x ≠ y)
lemma augAdj_iff {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (g : Fin n → Option (Fin n))
(x y : Fin n) :
augAdj R B g x y = true ↔ augmented P B g x y := by
unfold augAdj augmented successors core
simp only [Bool.or_eq_true, decide_eq_true_eq, Finset.mem_filter, Finset.mem_sdiff,
Finset.mem_univ, true_and, hR]
tauto
lemma boolClosure_augAdj_iff {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (g : Fin n → Option (Fin n))
(x y : Fin n) :
boolClosure (augAdj R B g) x y = true ↔ Relation.TransGen (augmented P B g) x y := by
rw [boolClosure_iff_transGen]
have heq : (fun a b => augAdj R B g a b = true) = augmented P B g := by
funext a b; exact propext (augAdj_iff hR B g a b)
rw [heq]
lemma augAcyclic_iff_boolClosure {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (g : Fin n → Option (Fin n)) :
AugAcyclic P B g ↔ ∀ x, boolClosure (augAdj R B g) x x = false := by
unfold AugAcyclic
constructor
· intro h x
cases hval : boolClosure (augAdj R B g) x x with
| false => rfl
| true => exact absurd ((boolClosure_augAdj_iff hR B g x x).mp hval) (h x)
· intro h x hcyc
have := (boolClosure_augAdj_iff hR B g x x).mpr hcyc
rw [h x] at this; cases this
lemma patternOrder_lt_iff_boolClosure {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (g : Fin n → Option (Fin n))
(hg : Compatible P B g) (x y : Fin n) :
(patternOrder P B g hg).lt x y ↔ boolClosure (augAdj R B g) x y = true := by
rw [boolClosure_augAdj_iff hR B g x y]
change ((x = y ∨ Relation.TransGen (augmented P B g) x y) ∧
¬ (y = x ∨ Relation.TransGen (augmented P B g) y x)) ↔ Relation.TransGen (augmented P B g) x y
constructor
· rintro ⟨rfl | h, hnot⟩
· exact absurd (Or.inl rfl) hnot
· exact h
· intro h
refine ⟨Or.inr h, ?_⟩
rintro (hxy | hrev)
· subst hxy; exact hg.2 y h
· exact hg.2 x (h.trans hrev)
/-! ## Section 2: DP State Invariants and Weight/Count Bounds -/
lemma dpInitial_ideal {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) :
IdealOn (patternOrder P B g hg) (core B) (dpInitial P B g).1 := by
refine ⟨Finset.empty_subset _, fun y hy => by simp [dpInitial] at hy⟩
lemma dpEdge_ideal {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} {s t : DPState n}
(hs : IdealOn (patternOrder P B g hg) (core B) s.1)
(he : 0 < dpEdge P B g hg s t) :
IdealOn (patternOrder P B g hg) (core B) t.1 := by
unfold dpEdge at he
by_cases h1 : 0 < ∑ c : Option (Fin n), if AntichainStep c s t then (s.2 c).val else 0
· obtain ⟨c, _, hc⟩ := Finset.exists_ne_zero_of_sum_ne_zero (by omega : (∑ c : Option (Fin n), if AntichainStep c s t then (s.2 c).val else 0) ≠ 0)
split_ifs at hc with hstep
· rw [hstep.2.1]; exact hs
· exact absurd rfl hc
· have h2 : 0 < ∑ x : Fin n, if CoreStep P B g hg x s t then 1 else 0 := by omega
obtain ⟨x, _, hx⟩ := Finset.exists_ne_zero_of_sum_ne_zero (by omega : (∑ x : Fin n, if CoreStep P B g hg x s t then 1 else 0) ≠ 0)
split_ifs at hx with hstep
· exact hstep.2.2.2.1
· exact absurd rfl hx
lemma dpPaths_ideal {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} :
∀ l s, 0 < dpPaths P B g hg l s → IdealOn (patternOrder P B g hg) (core B) s.1 := by
intro l
induction l with
| zero =>
intro s hs
simp only [dpPaths] at hs
split_ifs at hs with heq
· subst heq; exact dpInitial_ideal P B g hg
· omega
| succ l ih =>
intro t ht
simp only [dpPaths] at ht
obtain ⟨s, _, hs⟩ := Finset.exists_ne_zero_of_sum_ne_zero (by omega : (∑ s : DPState n, dpPaths P B g hg l s * dpEdge P B g hg s t) ≠ 0)
have hp : 0 < dpPaths P B g hg l s := Nat.pos_of_ne_zero (left_ne_zero_of_mul hs)
have he : 0 < dpEdge P B g hg s t := Nat.pos_of_ne_zero (right_ne_zero_of_mul hs)
exact dpEdge_ideal (ih s hp) he
lemma antichainStep_unique_target {n : ℕ} (c : Option (Fin n)) (s t₁ t₂ : DPState n)
(h1 : AntichainStep c s t₁) (h2 : AntichainStep c s t₂) : t₁ = t₂ := by
apply Prod.ext
· rw [h1.2.1, h2.2.1]
· funext d
by_cases hdc : d = c
· subst hdc
apply Fin.ext
have := h1.2.2.1; have := h2.2.2.1; omega
· rw [h1.2.2.2 d hdc, h2.2.2.2 d hdc]
lemma coreStep_unique_target {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} (x : Fin n) (s t₁ t₂ : DPState n)
(h1 : CoreStep P B g hg x s t₁) (h2 : CoreStep P B g hg x s t₂) : t₁ = t₂ := by
have hI : t₁.1 = t₂.1 := h1.2.2.1.trans h2.2.2.1.symm
apply Prod.ext
· exact hI
· funext c
apply Fin.ext
have hc1 := (h1.2.2.2.2.2 c).2
have hc2 := (h2.2.2.2.2.2 c).2
rw [hI] at hc1
omega
lemma sum_dpEdge_le {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (s : DPState n) :
∑ t : DPState n, dpEdge P B g hg s t ≤ (n + 1) ^ 2 := by
unfold dpEdge
rw [Finset.sum_add_distrib]
have h1 : ∑ t : DPState n, ∑ c : Option (Fin n), (if AntichainStep c s t then (s.2 c).val else 0) ≤
(n + 1) * n := by
rw [Finset.sum_comm]
calc ∑ c : Option (Fin n), ∑ t : DPState n, (if AntichainStep c s t then (s.2 c).val else 0)
≤ ∑ _c : Option (Fin n), n := by
apply Finset.sum_le_sum; intro c _
by_cases hex : ∃ t, AntichainStep c s t
· rcases hex with ⟨t₀, ht₀⟩
rw [Finset.sum_eq_single t₀]
· simp only [ht₀, ↓reduceIte]; exact Nat.lt_succ_iff.mp (s.2 c).isLt
· intro t _ hne; simp only [ite_eq_right_iff]
intro ht; exact absurd (antichainStep_unique_target c s t t₀ ht ht₀) hne
· intro h; exact absurd (Finset.mem_univ t₀) h
· push Not at hex
simp [hex]
_ = (n + 1) * n := by simp [Fintype.card_option]
have h2 : ∑ t : DPState n, ∑ x : Fin n, (if CoreStep P B g hg x s t then 1 else 0) ≤ n := by
rw [Finset.sum_comm]
calc ∑ x : Fin n, ∑ t : DPState n, (if CoreStep P B g hg x s t then 1 else 0)
≤ ∑ _x : Fin n, 1 := by
apply Finset.sum_le_sum; intro x _
by_cases hex : ∃ t, CoreStep P B g hg x s t
· rcases hex with ⟨t₀, ht₀⟩
rw [Finset.sum_eq_single t₀]
· simp [ht₀]
· intro t _ hne; simp only [ite_eq_right_iff]
intro ht; exact absurd (coreStep_unique_target x s t t₀ ht ht₀) hne
· intro h; exact absurd (Finset.mem_univ t₀) h
· push Not at hex
simp [hex]
_ = n := by simp
have hsq : (n + 1) ^ 2 = (n + 1) * n + n + 1 := by ring
omega
lemma sum_dpPaths_le {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) :
∀ l, ∑ s : DPState n, dpPaths P B g hg l s ≤ ((n + 1) ^ 2) ^ l := by
intro l
induction l with
| zero =>
simp only [dpPaths, pow_zero]
rw [Finset.sum_eq_single (dpInitial P B g)]
· simp
· intro s _ hne; simp [hne]
· intro h; exact absurd (Finset.mem_univ _) h
| succ l ih =>
calc ∑ t : DPState n, dpPaths P B g hg (l + 1) t
= ∑ t : DPState n, ∑ s : DPState n, dpPaths P B g hg l s * dpEdge P B g hg s t := rfl
_ = ∑ s : DPState n, dpPaths P B g hg l s * ∑ t : DPState n, dpEdge P B g hg s t := by
rw [Finset.sum_comm]
refine Finset.sum_congr rfl fun s _ => ?_
rw [Finset.mul_sum]
_ ≤ ∑ s : DPState n, dpPaths P B g hg l s * (n + 1) ^ 2 := by
apply Finset.sum_le_sum; intro s _
exact Nat.mul_le_mul_left _ (sum_dpEdge_le P B g hg s)
_ = (∑ s : DPState n, dpPaths P B g hg l s) * (n + 1) ^ 2 := by
rw [← Finset.sum_mul]
_ ≤ ((n + 1) ^ 2) ^ l * (n + 1) ^ 2 := Nat.mul_le_mul_right _ ih
_ = ((n + 1) ^ 2) ^ (l + 1) := by ring
lemma dpPaths_le_pow {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) {l : ℕ} (hl : l ≤ n) (s : DPState n) :
dpPaths P B g hg l s ≤ (n + 1) ^ (2 * n) := by
calc dpPaths P B g hg l s
≤ ∑ t : DPState n, dpPaths P B g hg l t :=
Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ s)
_ ≤ ((n + 1) ^ 2) ^ l := sum_dpPaths_le P B g hg l
_ ≤ ((n + 1) ^ 2) ^ n := Nat.pow_le_pow_right (by positivity) hl
_ = (n + 1) ^ (2 * n) := by rw [← pow_mul]
lemma dpStates_card_pos {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) :
1 ≤ (dpStates P B g hg).card := by
have hmem : dpInitial P B g ∈ dpStates P B g hg := by
unfold dpStates
simp only [Finset.mem_filter, Finset.mem_univ, true_and]
exact ⟨0, by simp [dpPaths]⟩
exact Finset.one_le_card.mpr ⟨_, hmem⟩
lemma numCompatible_le_totalStates {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n)) :
numCompatible P B ≤ totalStates P B := by
unfold numCompatible totalStates patternSum
apply Finset.sum_le_sum
intro g _
exact dpStates_card_pos P B g.1 g.2
lemma dpStates_card_le_pow {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) :
(dpStates P B g hg).card ≤ (n + 2) ^ (2 * n + 1) := by
calc (dpStates P B g hg).card
≤ Fintype.card (DPState n) := Finset.card_le_univ _
_ = 2 ^ n * (n + 1) ^ (n + 1) := by
simp [DPState, Fintype.card_prod, Fintype.card_finset, Fintype.card_pi, Fintype.card_option]
_ ≤ (n + 2) ^ n * (n + 2) ^ (n + 1) := by
exact Nat.mul_le_mul (Nat.pow_le_pow_left (by omega) n) (Nat.pow_le_pow_left (by omega) (n + 1))
_ = (n + 2) ^ (2 * n + 1) := by rw [← pow_add]; congr 1; omega
lemma numCompatible_le_pow {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n)) :
numCompatible P B ≤ (n + 1) ^ n := by
unfold numCompatible patternSum
simp only [Finset.sum_const, Finset.card_univ, smul_eq_mul, mul_one]
calc Fintype.card {g : Fin n → Option (Fin n) // Compatible P B g}
≤ Fintype.card (Fin n → Option (Fin n)) := Fintype.card_subtype_le _
_ = (n + 1) ^ n := by simp [Fintype.card_pi, Fintype.card_option]
lemma deadlineCount_le_pow {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n)) :
deadlineCount P B ≤ (n + 1) ^ (3 * n) := by
unfold deadlineCount patternSum
calc ∑ g : {g : Fin n → Option (Fin n) // Compatible P B g}, dpPaths P B g.1 g.2 n (dpTerminal B)
≤ ∑ _g : {g : Fin n → Option (Fin n) // Compatible P B g}, (n + 1) ^ (2 * n) := by
apply Finset.sum_le_sum; intro g _
exact dpPaths_le_pow P B g.1 g.2 le_rfl (dpTerminal B)
_ = numCompatible P B * (n + 1) ^ (2 * n) := by
simp [numCompatible, patternSum]
_ ≤ (n + 1) ^ n * (n + 1) ^ (2 * n) := Nat.mul_le_mul_right _ (numCompatible_le_pow P B)
_ = (n + 1) ^ (3 * n) := by rw [← pow_add]; congr 1; omega
/-! ## Section 3: Exact Transition Characterization -/
lemma idealOn_insert_iff {n : ℕ} {P : PartialOrder (Fin n)} {U I : Finset (Fin n)} {x : Fin n}
(hI : IdealOn P U I) (hxU : x ∈ U) :
IdealOn P U (insert x I) ↔ ∀ y ∈ U, P.lt y x → y ∈ I := by
constructor
· intro h y hyU hyx
have hyin : y ∈ insert x I := h.2 x (Finset.mem_insert_self x I) y hyU hyx
rcases Finset.mem_insert.mp hyin with rfl | hyI
· exact absurd hyx (lt_irrefl y)
· exact hyI
· intro h
refine ⟨Finset.insert_subset hxU hI.1, ?_⟩
intro z hz y hyU hyz
rcases Finset.mem_insert.mp hz with rfl | hzI
· exact Finset.mem_insert_of_mem (h y hyU hyz)
· exact Finset.mem_insert_of_mem (hI.2 z hzI y hyU hyz)
lemma released_insert_eq {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (I : Finset (Fin n)) (x : Fin n) (hxB : x ∉ B) (hxI : x ∉ I)
(c : Option (Fin n)) :
released P B g (insert x I) c = released P B g I c +
(B.filter (fun v => g v = c ∧ P.lt x v ∧
∀ z : Fin n, z ∉ B → P.lt z v → z = x ∨ z ∈ I)).card := by
unfold released
have hsub : B.filter (fun v => g v = c ∧ predecessors P B v ⊆ I) ⊆
B.filter (fun v => g v = c ∧ predecessors P B v ⊆ insert x I) := by
intro v hv
simp only [Finset.mem_filter] at hv ⊢
exact ⟨hv.1, hv.2.1, hv.2.2.trans (Finset.subset_insert x I)⟩
have hsdiff :
B.filter (fun v => g v = c ∧ predecessors P B v ⊆ insert x I) \
B.filter (fun v => g v = c ∧ predecessors P B v ⊆ I) =
B.filter (fun v => g v = c ∧ P.lt x v ∧
∀ z : Fin n, z ∉ B → P.lt z v → z = x ∨ z ∈ I) := by
ext v
simp only [Finset.mem_sdiff, Finset.mem_filter]
constructor
· rintro ⟨⟨hvB, hgv, hsub_ins⟩, hnot⟩
have hnot_sub : ¬ predecessors P B v ⊆ I := fun h => hnot ⟨hvB, hgv, h⟩
rw [Finset.not_subset] at hnot_sub
rcases hnot_sub with ⟨w, hw_pred, hw_notI⟩
have hwin : w ∈ insert x I := hsub_ins hw_pred
rcases Finset.mem_insert.mp hwin with rfl | hwI
· refine ⟨hvB, hgv, ?_, ?_⟩
· simp [predecessors, core] at hw_pred; exact hw_pred.2
· intro z hzB hzv
have hzpred : z ∈ predecessors P B v := by simp [predecessors, core, hzB, hzv]
exact Finset.mem_insert.mp (hsub_ins hzpred)
· exact absurd hwI hw_notI
· rintro ⟨hvB, hgv, hxv, hall⟩
refine ⟨⟨hvB, hgv, ?_⟩, ?_⟩
· intro z hz
simp only [predecessors, core, Finset.mem_filter, Finset.mem_sdiff, Finset.mem_univ, true_and] at hz
exact Finset.mem_insert.mpr (hall z hz.1 hz.2)
· rintro ⟨_, _, hsubI⟩
have hxpred : x ∈ predecessors P B v := by simp [predecessors, core, hxB, hxv]
exact hxI (hsubI hxpred)
rw [← hsdiff, Finset.card_sdiff_of_subset hsub]
have hle := Finset.card_le_card hsub
omega
/-- Exact computational characterization of `CoreStep` when `s.1` is already an ideal. -/
lemma coreStep_iff_computable {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} {s t : DPState n}
(hs : IdealOn (patternOrder P B g hg) (core B) s.1) (x : Fin n) :
CoreStep P B g hg x s t ↔
x ∉ B ∧ x ∉ s.1 ∧ t.1 = insert x s.1 ∧
(∀ y : Fin n, y ∉ B → (patternOrder P B g hg).lt y x → y ∈ s.1) ∧
((∃ v ∈ B, P.lt v x) → (s.2 (some x)).val = 0) ∧
∀ c, (t.2 c).val = (s.2 c).val +
(B.filter (fun v => g v = c ∧ P.lt x v ∧
∀ z : Fin n, z ∉ B → P.lt z v → z = x ∨ z ∈ s.1)).card := by
unfold CoreStep
have hcore : x ∈ core B ↔ x ∉ B := by simp [core]
have hup : x ∈ upperCore P B ↔ x ∉ B ∧ ∃ v ∈ B, P.lt v x := by
unfold upperCore successors core
simp only [Finset.mem_biUnion, Finset.mem_filter, Finset.mem_sdiff, Finset.mem_univ, true_and]
constructor
· rintro ⟨v, hvB, hxB, hvx⟩; exact ⟨hxB, v, hvB, hvx⟩
· rintro ⟨hxB, v, hvB, hvx⟩; exact ⟨v, hvB, hxB, hvx⟩
constructor
· rintro ⟨hxC, hxI, ht1, hId, hUp, hq⟩
have hxB : x ∉ B := hcore.mp hxC
refine ⟨hxB, hxI, ht1, ?_, ?_, ?_⟩
· have hId' := (idealOn_insert_iff hs hxC).mp (ht1 ▸ hId)
intro y hyB hyx; exact hId' y (by simp [core, hyB]) hyx
· intro hex; exact hUp (hup.mpr ⟨hxB, hex⟩)
· intro c
have hrel := released_insert_eq P B g s.1 x hxB hxI c
have hqc := (hq c).2
rw [ht1, hrel] at hqc
omega
· rintro ⟨hxB, hxI, ht1, hId, hUp, hq⟩
have hxC : x ∈ core B := hcore.mpr hxB
refine ⟨hxC, hxI, ht1, ?_, ?_, ?_⟩
· rw [ht1, idealOn_insert_iff hs hxC]
intro y hyC hyx; exact hId y (by simp [core] at hyC; exact hyC) hyx
· intro hxup; exact hUp (hup.mp hxup).2
· intro c
have hrel := released_insert_eq P B g s.1 x hxB hxI c
rw [ht1, hrel]
exact ⟨by omega, by have := hq c; omega⟩
/-! ## Section 4: Active List and Iterative DFS over Compatible Patterns -/
/-- Ordered list of active elements of `B` (`v ∈ B` with at least one core successor). -/
def activeList {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n)) : List (Fin n) :=
(List.finRange n).filter (fun v => decide (v ∈ B ∧ ∃ y : Fin n, y ∉ B ∧ R v y = true))
lemma activeList_nodup {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n)) :
(activeList R B).Nodup :=
(List.nodup_finRange n).filter _
lemma mem_activeList_iff {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (v : Fin n) :
v ∈ activeList R B ↔ v ∈ active P B := by
unfold activeList active successors core
simp only [List.mem_filter, List.mem_finRange, true_and, decide_eq_true_eq,
Finset.mem_filter, Finset.Nonempty, Finset.mem_sdiff, Finset.mem_univ, hR]
lemma activeList_length_le {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n)) :
(activeList R B).length ≤ n := by
calc (activeList R B).length ≤ (List.finRange n).length := List.length_filter_le _ _
_ = n := List.length_finRange
/-- Functional DFS tree over the suffix `act[d ..]` starting from partial pattern `g`. -/
def dfsTreeSum {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(eval : (Fin n → Option (Fin n)) → ℕ) :
List (Fin n) → (Fin n → Option (Fin n)) → ℕ
| [], g => eval g
| v :: vs, g =>
∑ y : Fin n,
if y ∉ B ∧ R v y = true ∧
(∀ x, boolClosure (augAdj R B (Function.update g v (some y))) x x = false)
then dfsTreeSum R B eval vs (Function.update g v (some y))
else 0
/-- Exact count of recursive calls (nodes visited) in `dfsTreeSum`. -/
def dfsTreeNodes {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n)) :
List (Fin n) → (Fin n → Option (Fin n)) → ℕ
| [], _ => 1
| v :: vs, g =>
1 + ∑ y : Fin n,
if y ∉ B ∧ R v y = true ∧
(∀ x, boolClosure (augAdj R B (Function.update g v (some y))) x x = false)
then dfsTreeNodes R B vs (Function.update g v (some y))
else 0
private lemma transGen_mono {α : Type*} {r s : α → α → Prop}
(h : ∀ a b, r a b → s a b) {x y : α} (hxy : Relation.TransGen r x y) :
Relation.TransGen s x y := by
induction hxy with
| single hr => exact .single (h _ _ hr)
| tail _ hr ih => exact ih.tail (h _ _ hr)
lemma dfsTreeSum_eq_sum_compatible {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (eval : (Fin n → Option (Fin n)) → ℕ) :
∀ (vs : List (Fin n)) (T : Finset (Fin n)) (g : Fin n → Option (Fin n)),
vs.Nodup →
Disjoint T vs.toFinset →
T ∪ vs.toFinset = active P B →
IsPartialPattern P B T g →
AugAcyclic P B g →
dfsTreeSum R B eval vs g =
∑ g' ∈ Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u), eval g' := by
intro vs
induction vs with
| nil =>
intro T g _ _ hcov hpart hacyc
simp only [List.toFinset_nil, Finset.union_empty] at hcov
have hcomp : Compatible P B g := by
refine ⟨?_, hacyc⟩
intro v
have hv := hpart v
constructor
· intro ⟨hvB, hne⟩
have hvT : v ∈ T := by rw [hcov]; exact Finset.mem_filter.mpr ⟨hvB, hne⟩
exact hv.1 hvT
· intro hnot
have hvT : v ∉ T := by
rw [hcov]; simp only [active, Finset.mem_filter, not_and]
intro hvB; exact hnot.resolve_left (not_not.mpr hvB)
exact hv.2 hvT
simp only [dfsTreeSum]
rw [Finset.sum_eq_single g]
· intro g' hg' hne
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hg'
exfalso; apply hne; funext v
by_cases hvT : v ∈ T
· exact hg'.2 v hvT
· have hv_not_act : v ∉ active P B := hcov ▸ hvT
have hnot : v ∉ B ∨ ¬ (successors P B v).Nonempty := by
simp only [active, Finset.mem_filter, not_and] at hv_not_act
by_cases hvB : v ∈ B
· exact Or.inr (hv_not_act hvB)
· exact Or.inl hvB
rw [(hg'.1.1 v).2 hnot, (hpart v).2 hvT]
· intro hnot
exfalso; apply hnot
exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hcomp, fun _ _ => rfl⟩
| cons v vs ih =>
intro T g hnd hdisj hcov hpart hacyc
have hv_not_vs : v ∉ vs := (List.nodup_cons.mp hnd).1
have hnd_vs : vs.Nodup := (List.nodup_cons.mp hnd).2
have hv_not_T : v ∉ T := fun h =>
Finset.disjoint_left.mp hdisj h (by simp)
have hdisj' : Disjoint (insert v T) vs.toFinset := by
rw [Finset.disjoint_insert_left]
refine ⟨by simpa using hv_not_vs, ?_⟩
exact Finset.disjoint_of_subset_right (by intro x hx; simp at hx ⊢; exact Or.inr hx) hdisj
have hcov' : insert v T ∪ vs.toFinset = active P B := by
rw [← hcov]; ext x; simp
have hv_act : v ∈ active P B := by rw [← hcov]; simp
have hvB : v ∈ B := (Finset.mem_filter.mp hv_act).1
simp only [dfsTreeSum]
-- Rewrite each summand using `ih`
have hstep : ∀ y : Fin n,
(if y ∉ B ∧ R v y = true ∧
(∀ x, boolClosure (augAdj R B (Function.update g v (some y))) x x = false)
then dfsTreeSum R B eval vs (Function.update g v (some y))
else 0) =
∑ g' ∈ Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ (∀ u ∈ T, g' u = g u) ∧ g' v = some y), eval g' := by
intro y
split_ifs with hy
· rcases hy with ⟨hyB, hvy, hcl⟩
have hacyc' : AugAcyclic P B (Function.update g v (some y)) :=
(augAcyclic_iff_boolClosure hR B _).mpr hcl
have hpart' : IsPartialPattern P B (insert v T) (Function.update g v (some y)) := by
intro u
by_cases huv : u = v
· subst huv
refine ⟨fun _ => ⟨y, by simp [successors, core, hyB, (hR u y).mp hvy], by simp⟩,
fun h => absurd (Finset.mem_insert_self _ _) h⟩
· simp only [Finset.mem_insert, huv, false_or, Function.update_of_ne huv]
exact hpart u
rw [ih (insert v T) (Function.update g v (some y)) hnd_vs hdisj' hcov' hpart' hacyc']
refine Finset.sum_congr ?_ (fun _ _ => rfl)
ext g'
simp only [Finset.mem_filter, Finset.mem_univ, true_and]
constructor
· rintro ⟨hc, hall⟩
refine ⟨hc, fun u hu => ?_, by simpa using hall v (Finset.mem_insert_self v T)⟩
have huv : u ≠ v := fun h => hv_not_T (h ▸ hu)
simpa [Function.update_of_ne huv] using hall u (Finset.mem_insert_of_mem hu)
· rintro ⟨hc, hall, hgv⟩
refine ⟨hc, fun u hu => ?_⟩
rcases Finset.mem_insert.mp hu with rfl | huT
· simpa using hgv
· have huv : u ≠ v := fun h => hv_not_T (h ▸ huT)
simpa [Function.update_of_ne huv] using hall u huT
· symm
apply Finset.sum_eq_zero
intro g' hg'
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hg'
exfalso; apply hy
have hgv_succ : y ∈ successors P B v := by
rcases (hg'.1.1 v).1 ⟨hvB, (Finset.mem_filter.mp hv_act).2⟩ with ⟨y', hy', hgy'⟩
rw [hg'.2.2] at hgy'
cases hgy'; exact hy'
simp only [successors, core, Finset.mem_filter, Finset.mem_sdiff, Finset.mem_univ, true_and] at hgv_succ
refine ⟨hgv_succ.1, (hR v y).mpr hgv_succ.2, ?_⟩
rw [← augAcyclic_iff_boolClosure hR B]
intro x hcyc
apply hg'.1.2 x
-- `augmented P B (update g v (some y))` is a subrelation of `augmented P B g'`
have hsub_rel : ∀ a b, augmented P B (Function.update g v (some y)) a b → augmented P B g' a b := by
intro a b hab
rcases hab with hlt | ⟨u, huB, hgu, hbu, hne⟩
· exact Or.inl hlt
· refine Or.inr ⟨u, huB, ?_, hbu, hne⟩
by_cases huv : u = v
· subst huv; simp only [Function.update_self] at hgu; rw [hg'.2.2, hgu]
· simp only [Function.update_of_ne huv] at hgu
have huT : u ∈ T := by
by_contra huNot
rw [(hpart u).2 huNot] at hgu; cases hgu
rw [hg'.2.1 u huT, hgu]
exact transGen_mono hsub_rel hcyc
simp_rw [hstep]
rw [← Finset.sum_biUnion]
· refine Finset.sum_congr ?_ (fun _ _ => rfl)
ext g'
simp only [Finset.mem_biUnion, Finset.mem_univ, true_and, Finset.mem_filter]
constructor
· rintro ⟨y, hc, hall, _⟩; exact ⟨hc, hall⟩
· rintro ⟨hc, hall⟩
rcases (hc.1 v).1 ⟨hvB, (Finset.mem_filter.mp hv_act).2⟩ with ⟨y, _, hgy⟩
exact ⟨y, hc, hall, hgy⟩
· intro y₁ _ y₂ _ hne
dsimp only [Function.onFun]
rw [Finset.disjoint_left]
intro g' h1 h2
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at h1 h2
have : some y₁ = some y₂ := h1.2.2.symm.trans h2.2.2
exact hne (Option.some.inj this)
private lemma augAcyclic_none {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n)) :
AugAcyclic P B (fun _ => none) := by
intro x hcyc
have hsub : ∀ a b, augmented P B (fun _ => none) a b → P.lt a b := by
intro a b hab
rcases hab with hlt | ⟨v, _, hgv, _, _⟩
· exact hlt
· cases hgv
have hlt : Relation.TransGen P.lt x x := transGen_mono hsub hcyc
have htrans : ∀ a b, Relation.TransGen P.lt a b → P.lt a b := by
intro a b hab
induction hab with
| single h => exact h
| tail _ h ih => exact lt_trans ih h
exact lt_irrefl x (htrans x x hlt)
theorem dfsTreeSum_activeList {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (hB : Antichain P B)
(eval : (Fin n → Option (Fin n)) → ℕ)
(heval : ∀ g (hg : Compatible P B g), eval g = dpPaths P B g hg n (dpTerminal B)) :
dfsTreeSum R B eval (activeList R B) (fun _ => none) = extensionCount P := by
rw [← deadline_dp_count n P B hB]
have hcov : (∅ : Finset (Fin n)) ∪ (activeList R B).toFinset = active P B := by
ext v; simp [mem_activeList_iff hR]
rw [dfsTreeSum_eq_sum_compatible hR B eval (activeList R B) ∅ (fun _ => none)
(activeList_nodup R B) (Finset.disjoint_empty_left _) hcov
(fun v => ⟨fun h => by simp at h, fun _ => rfl⟩) (augAcyclic_none P B)]
unfold deadlineCount patternSum
symm
refine Finset.sum_bij (fun g _ => g.1) ?_ ?_ ?_ ?_
· intro g _; simp [g.2]
· intro g₁ _ g₂ _ heq; exact Subtype.ext heq
· intro g hg
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hg
exact ⟨⟨g, hg.1⟩, Finset.mem_univ _, rfl⟩
· intro g _; exact (heval g.1 g.2).symm
/-- Number of nodes in `dfsTreeNodes` is bounded by `(vs.length + 1) * |compatible extensions|`. -/
lemma dfsTreeNodes_le {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (hB : Antichain P B) :
∀ (vs : List (Fin n)) (T : Finset (Fin n)) (g : Fin n → Option (Fin n)),
vs.Nodup →
Disjoint T vs.toFinset →
T ∪ vs.toFinset = active P B →
IsPartialPattern P B T g →
AugAcyclic P B g →
dfsTreeNodes R B vs g ≤
(vs.length + 1) * (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card := by
intro vs
induction vs with
| nil =>
intro T g _ _ hcov hpart hacyc
have hsum := dfsTreeSum_eq_sum_compatible hR B (fun _ => 1) [] T g
List.nodup_nil (Finset.disjoint_empty_right _) hcov hpart hacyc
simp only [dfsTreeSum, Finset.sum_const, smul_eq_mul, mul_one] at hsum
simp only [dfsTreeNodes, List.length_nil, zero_add, one_mul, ← hsum, le_refl]
| cons v vs ih =>
intro T g hnd hdisj hcov hpart hacyc
have hv_not_vs : v ∉ vs := (List.nodup_cons.mp hnd).1
have hnd_vs : vs.Nodup := (List.nodup_cons.mp hnd).2
have hv_not_T : v ∉ T := fun h => Finset.disjoint_left.mp hdisj h (by simp)
have hdisj' : Disjoint (insert v T) vs.toFinset := by
rw [Finset.disjoint_insert_left]
refine ⟨by simpa using hv_not_vs, ?_⟩
exact Finset.disjoint_of_subset_right (by intro x hx; simp at hx ⊢; exact Or.inr hx) hdisj
have hcov' : insert v T ∪ vs.toFinset = active P B := by
rw [← hcov]; ext x; simp
have hTsub : T ⊆ active P B := by rw [← hcov]; exact Finset.subset_union_left
have hex_ext : 1 ≤ (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card := by
rcases (partial_patterns n P B hB T hTsub).1 g hpart hacyc with ⟨g', hcg', hgg'⟩
exact Finset.one_le_card.mpr ⟨g', Finset.mem_filter.mpr ⟨Finset.mem_univ _, hcg', hgg'⟩⟩
-- Sum of children's extension counts equals parent's extension count
have hsum1 := dfsTreeSum_eq_sum_compatible hR B (fun _ => 1) (v :: vs) T g
hnd hdisj hcov hpart hacyc
simp only [Finset.sum_const, smul_eq_mul, mul_one] at hsum1
-- Bound each child using `ih`
have hchildren : ∑ y : Fin n,
(if y ∉ B ∧ R v y = true ∧
(∀ x, boolClosure (augAdj R B (Function.update g v (some y))) x x = false)
then dfsTreeNodes R B vs (Function.update g v (some y))
else 0) ≤
(vs.length + 1) * (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card := by
rw [← hsum1, dfsTreeSum, Finset.mul_sum]
apply Finset.sum_le_sum
intro y _
split_ifs with hy
· rcases hy with ⟨hyB, hvy, hcl⟩
have hacyc' : AugAcyclic P B (Function.update g v (some y)) :=
(augAcyclic_iff_boolClosure hR B _).mpr hcl
have hpart' : IsPartialPattern P B (insert v T) (Function.update g v (some y)) := by
intro u
by_cases huv : u = v
· subst huv
refine ⟨fun _ => ⟨y, by simp [successors, core, hyB, (hR u y).mp hvy], by simp⟩,
fun h => absurd (Finset.mem_insert_self _ _) h⟩
· simp only [Finset.mem_insert, huv, false_or, Function.update_of_ne huv]
exact hpart u
have hch := ih (insert v T) (Function.update g v (some y)) hnd_vs hdisj' hcov' hpart' hacyc'
have hch1 := dfsTreeSum_eq_sum_compatible hR B (fun _ => 1) vs (insert v T)
(Function.update g v (some y)) hnd_vs hdisj' hcov' hpart' hacyc'
simp only [Finset.sum_const, smul_eq_mul, mul_one] at hch1
rw [hch1]; exact hch
· simp
simp only [dfsTreeNodes, List.length_cons]
have hmul : (vs.length + 1 + 1) * (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card =
(Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card +
(vs.length + 1) * (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ T, g' u = g u)).card := by ring
omega
theorem dfsTreeNodes_activeList_le {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ i j, R i j = true ↔ P.lt i j) (B : Finset (Fin n)) (hB : Antichain P B) :
dfsTreeNodes R B (activeList R B) (fun _ => none) ≤ (n + 1) * numCompatible P B := by
have hcov : (∅ : Finset (Fin n)) ∪ (activeList R B).toFinset = active P B := by
ext v; simp [mem_activeList_iff hR]
have hle := dfsTreeNodes_le hR B hB (activeList R B) ∅ (fun _ => none)
(activeList_nodup R B) (Finset.disjoint_empty_left _) hcov
(fun v => ⟨fun h => by simp at h, fun _ => rfl⟩) (augAcyclic_none P B)
have hcard : (Finset.univ.filter (fun g' : Fin n → Option (Fin n) =>
Compatible P B g' ∧ ∀ u ∈ (∅ : Finset (Fin n)), g' u = none)).card = numCompatible P B := by
unfold numCompatible patternSum
simp only [Finset.notMem_empty, IsEmpty.forall_iff, implies_true, and_true,
Finset.sum_const, Finset.card_univ, smul_eq_mul, mul_one]
exact (Fintype.card_subtype (Compatible P B)).symm
rw [hcard] at hle
have hlen := activeList_length_le R B
exact le_trans hle (Nat.mul_le_mul_right _ (by omega))
end DeadlineDP
Source
STOC 2027 submission #725, Counting Linear Extensions Faster than 2^n, Section 3.2-3.3