1-- The even set of a disconnected last colour: on the stable partition the2-- packing condition leaves the last colour enough crossing edges to close a3-- nonempty even set, which holds an edge of finite level.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.GraphTheory.TreePacking.LastColour10import Levels1112-- An edge of a colour below j lies in the union of the first j colours.13theorem coloursBelow.contains (T g : Set, j i : Nat)14requires index : i ∈ j15requires member : g ∈ T(i)16shows g ∈ coloursBelow(T, j)17proof18induction j19| zero =>20intro index21exfalso22rw nat.zero at index23exactset.empty with index24| succ(m) using ih =>25intro index26havecases:= membership.successor_forward{n := m} with index27unfold coloursBelow28rcasescaseswith earlier | same29 . have inBelow := ih with earlier30simp [inBelow]31 . rw same at member32simp [member]3334-- An edge of the first j colours has a colour below j, and the first j35-- colours lie in E.36theorem coloursBelow.colour (T g : Set, j : Nat)37requires member : g ∈ coloursBelow(T, j)38shows∃ i ∈ j, g ∈ T(i)39proof40induction j41| zero =>42intro member43unfold coloursBelow at member44exfalso45exactset.empty with member46| succ(m) using ih =>47intro member48unfold coloursBelow at member49simpat member50rcases member with earlier | last51 . obtain⟨i, index, inT⟩:= ih with earlier52have inSucc : i ∈ succ(m) bysimp [index]53exact⟨i, inSucc, inT⟩54 . haveself : m ∈ succ(m) bysimp55exact⟨m, self, last⟩5657-- Two facts about n − 1 beside sub.one_le_of_le_add_one.58theorem sub.one_of_add_one (a b : Nat)59requires sum : a +1= b60shows b - 1= a61proof62have shape := natural.zero_or_successor at b63rcases shape with zero |⟨q, natural, rfl⟩64 . rw zero at sum65arith66 . rw [sub.one, predecessor.succ]67arith6869theorem sub.le_one_add_one (a : Nat)70shows a ≤ (a - 1) +171proof72have shape := natural.zero_or_successor at a73rcases shape with zero |⟨q, natural, rfl⟩74 . rw [zero, sub.zero_left]75arith76 . rw [sub.one, predecessor.succ]77arith7879-- The first j trees, each internally connected on P, cross it in at most80-- j(|P| − 1) edges: exactly |P| − 1 each.81theorem coloursBelow.crossing_upper (V E P leftEnd rightEnd T : Set, k j : Nat)82requires graph : Graph(V, E, leftEnd, rightEnd)83requires finiteVertices : Finite(V)84requires finiteEdges : Finite(E)85requires partition : Partition(V, P)86requires classes : ∀ i ∈ k, T(i) ⊆ E87requires trees : ∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i))88requires internal :89∀ i ∈ k, InternallyConnected(leftEnd, rightEnd, T(i), P)90requires below : j ≤ k91shows coloursBelow(T, j) ⊆ E ∧92 cardinality(crossingClasses(P, leftEnd, rightEnd, coloursBelow(T, j))) ≤93 j * (cardinality(P) - 1)94proof95induction j96| zero =>97intro below98have finiteClasses := Partition.finite with finiteVertices, partition99unfold coloursBelow100refine⟨empty.subset, ?_⟩101have none : crossingClasses(P, leftEnd, rightEnd, ∅) =∅102ext e103simp [crossingClasses]104rw [none, cardinality.empty, mul.left_zero]105arith106| succ(m) using ih =>107intro below108have finiteClasses := Partition.finite with finiteVertices, partition109have earlier : m ≤ k byarith110obtain⟨subsetE, count⟩:= ih with earlier111have less : m < k byarith112have inK := membership.of_Less at m, k with*, less113have classE := classes with m, inK114have ends := ends_in.subset with (Graph.ends with graph), classE115haveexact:= SpanningTree.crossing_count with finiteVertices,116 partition, ends, (trees with m, inK), (internal with m, inK)117have finiteBelow : Finite(crossingClasses(P, leftEnd, rightEnd,118 coloursBelow(T, m))) :=119 Finite.subset with finiteEdges,120 (Subset.transitive with crossingClasses.subset, subsetE)121have finiteClass : Finite(crossingClasses(P, leftEnd, rightEnd, T(m))) :=122 Finite.subset with finiteEdges,123 (Subset.transitive with crossingClasses.subset, classE)124have sum := cardinality.union_le with finiteBelow, finiteClass125have bound := sub.one_of_add_one withexact126have step := mul.succ_left at m, cardinality(P) - 1127unfold coloursBelow128refine⟨(Subset.union2_iff.backward with⟨subsetE, classE⟩), ?_⟩129rw bound at count130rw [crossingClasses.union, step, bound]131arith132133-- The forests inside a finite edge set form a finite family holding ∅.134theorem forestsIn.finite (V leftEnd rightEnd F : Set)135requires finite : Finite(F)136shows Finite(forestsIn(V, leftEnd, rightEnd, F))137proof138have subset : forestsIn(V, leftEnd, rightEnd, F) ⊆𝒫(F)139intros G inFamily140exact inFamily.left141exact Finite.subset with (Finite.powerset with finite), subset142143theorem forestsIn.contains_empty (V leftEnd rightEnd F : Set)144shows∅∈ forestsIn(V, leftEnd, rightEnd, F)145proof146have forest : Forest(V, leftEnd, rightEnd, ∅)147constructor148 . exact Finite.empty149 . intros C bound even150exact Subset.antisymmetric with bound, empty.subset151exact⟨(powerset.membership_iff.backward with empty.subset), forest⟩152153-- A maximal forest inside an edge set R joins whatever R joins: an edge of154-- R leaving a component of the forest could be added to it.155theorem Forest.maximal_joined (V leftEnd rightEnd R G u v : Set)156requires finiteVertices : Finite(V)157requires ends : ∀ e ∈ R, leftEnd(e) ∈ V ∧ rightEnd(e) ∈ V158requires member : G ∈ forestsIn(V, leftEnd, rightEnd, R)159requires maximal :160∀ H ∈ forestsIn(V, leftEnd, rightEnd, R), G ⊆ H → H = G161requires vertex : u ∈ V162requires joined : Joined(leftEnd, rightEnd, R, u, v)163shows Joined(leftEnd, rightEnd, G, u, v)164proof165obtain⟨inPowerset, forest⟩:= member166have subsetR := powerset.membership_iff.forward with inPowerset167have endsG := ends_in.subset with ends, subsetR168have closedG := componentBy.closed at V, leftEnd, rightEnd, G, u with endsG169have selfU := componentBy.self at V, leftEnd, rightEnd, G, u with vertex170have closedR : ClosedUnder(leftEnd, rightEnd, R,171 componentBy(V, leftEnd, rightEnd, G, u))172intros e inR173by_contra unbalanced174have edgeEnds := ends with e, inR175have outside : ¬e ∈ G176intro inG177exact unbalanced with (closedG with e, inG)178have apartEnds : ¬Joined(leftEnd, rightEnd, G, leftEnd(e), rightEnd(e))179intro joinedEnds180exact unbalanced with (Joined.balanced with181 endsG, edgeEnds.left, edgeEnds.right, joinedEnds, closedG)182have bigger := Forest.add_edge with183 finiteVertices, endsG, edgeEnds, forest, apartEnds184have biggerSubset : G ∪ {e} ⊆ R := Subset.union2_iff.backward with185⟨subsetR, (singleton.subset_iff.backward with inR)⟩186have biggerIn : G ∪ {e} ∈ forestsIn(V, leftEnd, rightEnd, R) :=187⟨(powerset.membership_iff.backward with biggerSubset), bigger⟩188have equal := maximal with G ∪ {e}, biggerIn, union2.subset_left189have inBigger : e ∈ G ∪ {e} bysimp190rw equal at inBigger191exact outside with inBigger192exact (joined with componentBy(V, leftEnd, rightEnd, G, u), selfU,193 closedR).right194195theorem Forest.maximal_components (V leftEnd rightEnd R G : Set)196requires finiteVertices : Finite(V)197requires ends : ∀ e ∈ R, leftEnd(e) ∈ V ∧ rightEnd(e) ∈ V198requires member : G ∈ forestsIn(V, leftEnd, rightEnd, R)199requires maximal :200∀ H ∈ forestsIn(V, leftEnd, rightEnd, R), G ⊆ H → H = G201shows componentsBy(V, leftEnd, rightEnd, G) =202 componentsBy(V, leftEnd, rightEnd, R)203proof204have subsetR := powerset.membership_iff.forward with member.left205have same : ∀ u ∈ V, componentBy(V, leftEnd, rightEnd, G, u) =206 componentBy(V, leftEnd, rightEnd, R, u)207intros u vertex208ext v209constructor210 . intro inG211exact⟨inG.left, (Joined.monotone with subsetR, inG.right)⟩212 . intro inR213exact⟨inR.left, (Forest.maximal_joined with214 finiteVertices, ends, member, maximal, vertex, inR.right)⟩215ext X216constructor217 . intro inG218obtain⟨u, vertex, rfl⟩:= inG219rw same with u, vertex220exact componentsBy.contains with vertex221 . intro inR222obtain⟨u, vertex, rfl⟩:= inR223rw← same with u, vertex224exact componentsBy.contains with vertex225226-- On a partition P where every colour is internally connected, the packing227-- condition leaves the last colour at least |P| − 1 crossing edges; with a228-- maximal forest of its edges inside the classes, which has the classes as229-- components, these make |V| − 1 edges that do not connect V, so they hold230-- a nonempty even set, which the forest cannot contain.231theorem Kaiser.crossing_cycle (V E P leftEnd rightEnd T : Set, k : Nat)232requires graph : Graph(V, E, leftEnd, rightEnd)233requires finiteVertices : Finite(V)234requires finiteEdges : Finite(E)235requires partition : Partition(V, P)236requires decomposition : Decomposition(E, T, succ(k))237requires trees : ∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i))238requires internal :239∀ i ∈ succ(k), InternallyConnected(leftEnd, rightEnd, T(i), P)240requires condition : PackingCondition(V, E, leftEnd, rightEnd, succ(k))241requires disconnected : ¬Connects(V, leftEnd, rightEnd, T(k))242shows∃ C, C ⊆ T(k) ∧ EvenEdgeSet(V, leftEnd, rightEnd, C) ∧243 (∃ e ∈ C, e ∈ crossingClasses(P, leftEnd, rightEnd, T(k)))244proof245have kIn : k ∈ succ(k) bysimp246have endsE := Graph.ends with graph247have classK := Decomposition.class_subset with decomposition, kIn248have endsK := ends_in.subset with endsE, classK249have finiteK := Finite.subset with finiteEdges, classK250have finiteClasses := Partition.finite with finiteVertices, partition251have crossingSubset := crossingClasses.subset at P, leftEnd, rightEnd, T(k)252have finiteCrossing : Finite(crossingClasses(P, leftEnd, rightEnd, T(k))) :=253 Finite.subset with finiteK, crossingSubset254-- The last colour crosses P at least |P| − 1 times.255have classes : ∀ i ∈ k, T(i) ⊆ E256intros i index257have iNat := natural.member at k with i, index258have inSucc : i ∈ succ(k) bysimp [index]259exact Decomposition.class_subset with decomposition, inSucc260have internalBelow :261∀ i ∈ k, InternallyConnected(leftEnd, rightEnd, T(i), P)262intros i index263have inSucc : i ∈ succ(k) bysimp [index]264exact internal with i, inSucc265obtain⟨belowE, upper⟩:= coloursBelow.crossing_upper with graph,266 finiteVertices, finiteEdges, partition, classes, trees, internalBelow,267 (LessEqual.reflexive at k)268have cover : E ⊆ coloursBelow(T, k) ∪ T(k)269intros g inE270obtain⟨i, index, inT⟩:= decomposition.right.left with g, inE271have iNat := natural.member at succ(k) with i, index272havecases:= membership.successor_forward{n := k} with index273rcasescaseswith earlier | same274 . have inBelow := coloursBelow.contains with earlier, inT275simp [inBelow]276 . rw same at inT277simp [inT]278have finiteBelow : Finite(crossingClasses(P, leftEnd, rightEnd,279 coloursBelow(T, k))) :=280 Finite.subset with finiteEdges,281 (Subset.transitive with crossingClasses.subset, belowE)282have covered := crossingClasses.monotone at P, leftEnd, rightEnd, E,283 coloursBelow(T, k) ∪ T(k) with cover284rw crossingClasses.union at covered285have boundE := cardinality.monotone with286 (Finite.union with finiteBelow, finiteCrossing), covered287have sumLe := cardinality.union_le with finiteBelow, finiteCrossing288have packed := condition with P, partition289have step := mul.succ_left at k, cardinality(P) - 1290rw step at packed291have lowerK : cardinality(P) - 1≤292 cardinality(crossingClasses(P, leftEnd, rightEnd, T(k))) byarith293have lowerP := sub.le_one_add_one at cardinality(P)294-- A maximal forest among the edges of T(k) inside the classes.295have restSubset := difference.subset_left296at T(k), crossingClasses(P, leftEnd, rightEnd, T(k))297have restFinite := Finite.subset with finiteK, restSubset298have restEnds := ends_in.subset with endsK, restSubset299have finiteFamily := forestsIn.finite at V, leftEnd, rightEnd,300 T(k) ∖ crossingClasses(P, leftEnd, rightEnd, T(k)) with restFinite301have emptyForest := forestsIn.contains_empty at V, leftEnd, rightEnd,302 T(k) ∖ crossingClasses(P, leftEnd, rightEnd, T(k))303obtain⟨G, inFamily, _, maximal⟩:=304 Finite.maximal_extension with finiteFamily, emptyForest305obtain⟨inPowerset, forest⟩:= inFamily306have finiteG := forest.left307have subsetRest := powerset.membership_iff.forward with inPowerset308have endsG := ends_in.subset with restEnds, subsetRest309have components : componentsBy(V, leftEnd, rightEnd, G) = P310rw← (componentsBy.eq_classes with311 partition, endsK, (internal with k, kIn))312exact Forest.maximal_components with313 finiteVertices, restEnds, inFamily, maximal314have upperG := Forest.edge_bound with finiteVertices, endsG, forest315have lowerG := componentsBy.lower_bound with finiteVertices, finiteG, endsG316rw components at upperG, lowerG317-- The forest with the crossing edges: |V| − 1 edges or more, inside T(k).318have apart : Disjoint(G, crossingClasses(P, leftEnd, rightEnd, T(k))) :=319 Disjoint.of_subset_left with Disjoint.difference_left, subsetRest320have sum := cardinality.union_disjoint with finiteG, finiteCrossing, apart321have subsetK : G ∪ crossingClasses(P, leftEnd, rightEnd, T(k)) ⊆ T(k) :=322 Subset.union2_iff.backward with323⟨(Subset.transitive with subsetRest, restSubset), crossingSubset⟩324have endsF := ends_in.subset with endsK, subsetK325have finiteF := Finite.subset with finiteK, subsetK326have dense : cardinality(V) ≤327 cardinality(G ∪ crossingClasses(P, leftEnd, rightEnd, T(k))) +1328rw sum329arith330have disconnectedF : ¬Connects(V, leftEnd, rightEnd,331 G ∪ crossingClasses(P, leftEnd, rightEnd, T(k)))332intro connected333apply disconnected334intros u hu v hv335exact Joined.monotone with subsetK, (connected with u, hu, v, hv)336obtain⟨C, subsetC, nonemptyC, even⟩:= EvenEdgeSet.of_dense_disconnected337with finiteVertices, finiteF, endsF, dense, disconnectedF338-- The even set is not inside the forest, so it holds a crossing edge.339have escapes : ∃ e ∈ C, ¬e ∈ G340by_contra none341have inG : C ⊆ G342intros e inC343by_contra outside344exact none with⟨e, inC, outside⟩345exact nonemptyC with (forest.right with C, inG, even)346obtain⟨e, inC, outside⟩:= escapes347have crossing : e ∈ crossingClasses(P, leftEnd, rightEnd, T(k))348have inF := subsetC with e, inC349simpat inF350rcases inF with inG | inCrossing351 . exfalso352exact outside with inG353 . exact inCrossing354exact⟨C, (Subset.transitive with subsetC, subsetK), even,355 e, inC, crossing⟩356357-- When the last colour is disconnected, the packing condition leaves it at358-- least |P_∞| − 1 edges crossing the stable partition, so it contains an359-- even set, and that even set has an edge of finite level.360theorem Kaiser.cycle_edge (V E leftEnd rightEnd T : Set, k : Nat)361requires graph : Graph(V, E, leftEnd, rightEnd)362requires finiteVertices : Finite(V)363requires finiteEdges : Finite(E)364requires nonempty : V ≠∅365requires decomposition : Decomposition(E, T, succ(k))366requires trees : ∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i))367requires condition : PackingCondition(V, E, leftEnd, rightEnd, succ(k))368requires disconnected : ¬Connects(V, leftEnd, rightEnd, T(k))369shows∃ C, C ⊆ T(k) ∧ EvenEdgeSet(V, leftEnd, rightEnd, C) ∧370 (∃ e ∈ C, ∃ n ∈ ℕ,371 FiniteLevel(V, leftEnd, rightEnd, T, e, succ(k), n))372proof373have partition :374 Partition(V, stablePartition(V, leftEnd, rightEnd, T, succ(k)))375unfold stablePartition376exact partitionSequence.partition with graph, nonempty, decomposition377have internal := stablePartition.internal with378 graph, finiteVertices, nonempty, decomposition379obtain⟨C, subsetC, even, e, inC, crossing⟩:= Kaiser.crossing_cycle with380 graph, finiteVertices, finiteEdges, partition, decomposition, trees,381 internal, condition, disconnected382have kIn : k ∈ succ(k) bysimp383have ends := Decomposition.ends with graph, decomposition, kIn384obtain⟨n, natural, _, level⟩:= FiniteLevel.of_crossing with385 finiteVertices, partition, (ends with e, (subsetC with e, inC)), crossing386exact⟨C, subsetC, even, e, inC, n, natural, level⟩387