1-- Short walks and triangles in simple graphs2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.GraphTheory.Walks8import Basic910predicate TwoStep (V E x y : Set) :=11∃ z ∈ V, Adjacent(E, x, z) ∧ Adjacent(E, z, y)1213theorem TwoStep.intro (V E x y z : Set)14requires hz : z ∈ V15requires first : Adjacent(E, x, z)16requires second : Adjacent(E, z, y)17shows TwoStep(V, E, x, y)18proof19unfold TwoStep20refine⟨z, hz, first, second⟩2122theorem TwoStep.bounds (V E x y : Set)23requires graph : SimpleGraph(V, E)24requires walk : TwoStep(V, E, x, y)25shows x ∈ V ∧ y ∈ V26proof27rcases walk with⟨z, hz, first, second⟩28have firstBounds := Adjacent.bounds with graph, first29have secondBounds := Adjacent.bounds with graph, second30rcases firstBounds with⟨hx, hz2⟩31rcases secondBounds with⟨hz3, hy⟩32refine⟨hx, hy⟩3334theorem TwoStep.symmetric (V E x y : Set)35requires graph : SimpleGraph(V, E)36requires walk : TwoStep(V, E, x, y)37shows TwoStep(V, E, y, x)38proof39rcases walk with⟨z, hz, first, second⟩40have back := Adjacent.symmetric with graph, second41have front := Adjacent.symmetric with graph, first42exact TwoStep.intro with hz, back, front4344theorem TwoStep.common_neighbor (V E x y : Set)45requires walk : TwoStep(V, E, x, y)46shows∃ z ∈ neighbors(V, E, x), Adjacent(E, z, y)47proof48rcases walk with⟨z, hz, first, second⟩49have member : z ∈ neighbors(V, E, x) := neighbors.membership.backward with⟨hz, first⟩50refine⟨z, member, second⟩5152theorem TwoStep.of_common_neighbor (V E x y : Set)53requires common : ∃ z ∈ neighbors(V, E, x), Adjacent(E, z, y)54shows TwoStep(V, E, x, y)55proof56rcases common with⟨z, member, second⟩57obtain⟨hz, first⟩:= neighbors.membership.forward with member58exact TwoStep.intro with hz, first, second5960theorem TwoStep.backtrack (V E x y : Set)61requires graph : SimpleGraph(V, E)62requires edge : Adjacent(E, x, y)63shows TwoStep(V, E, x, x)64proof65obtain⟨hx, hy⟩:= Adjacent.bounds with graph, edge66have reverse := Adjacent.symmetric with graph, edge67exact TwoStep.intro with hy, edge, reverse6869predicate Triangle (E x y z : Set) :=70 Adjacent(E, x, y) ∧ Adjacent(E, y, z) ∧ Adjacent(E, z, x)7172theorem Triangle.distinct (V E x y z : Set)73requires graph : SimpleGraph(V, E)74requires triangle : Triangle(E, x, y, z)75shows x ≠ y ∧ y ≠ z ∧ z ≠ x76proof77rcases triangle with⟨xy, yz, zx⟩78have dxy := Adjacent.distinct with graph, xy79have dyz := Adjacent.distinct with graph, yz80have dzx := Adjacent.distinct with graph, zx81refine⟨dxy, dyz, dzx⟩8283theorem Triangle.rotate (E x y z : Set)84requires triangle : Triangle(E, x, y, z)85shows Triangle(E, y, z, x)86proof87rcases triangle with⟨xy, yz, zx⟩88unfold Triangle89refine⟨yz, zx, xy⟩9091theorem Triangle.reverse (V E x y z : Set)92requires graph : SimpleGraph(V, E)93requires triangle : Triangle(E, x, y, z)94shows Triangle(E, x, z, y)95proof96rcases triangle with⟨xy, yz, zx⟩97have xz := Adjacent.symmetric with graph, zx98have zy := Adjacent.symmetric with graph, yz99have yx := Adjacent.symmetric with graph, xy100unfold Triangle101refine⟨xz, zy, yx⟩102103theorem Triangle.two_step (V E x y z : Set)104requires graph : SimpleGraph(V, E)105requires triangle : Triangle(E, x, y, z)106shows TwoStep(V, E, x, z)107proof108rcases triangle with⟨xy, yz, zx⟩109obtain⟨hx, hy⟩:= Adjacent.bounds with graph, xy110exact TwoStep.intro with hy, xy, yz111112theorem Triangle.of_two_step (V E x y : Set)113requires graph : SimpleGraph(V, E)114requires walk : TwoStep(V, E, x, y)115requires closing : Adjacent(E, y, x)116shows∃ z ∈ V, Triangle(E, x, z, y)117proof118rcases walk with⟨z, hz, first, second⟩119have triangle : Triangle(E, x, z, y)120unfold Triangle121refine⟨first, second, closing⟩122refine⟨z, hz, triangle⟩123124theorem Triangle.subgraph (V E W F x y z : Set)125requires sub : Subgraph(V, E, W, F)126requires triangle : Triangle(F, x, y, z)127shows Triangle(E, x, y, z)128proof129rcases triangle with⟨xy, yz, zx⟩130have exy := Subgraph.adjacent with sub, xy131have eyz := Subgraph.adjacent with sub, yz132have ezx := Subgraph.adjacent with sub, zx133unfold Triangle134refine⟨exy, eyz, ezx⟩135