1-- Simple graphs and their local structure2-- 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.Basic8import Relation910-- Edges are ordered pairs; symmetry makes their orientation immaterial.11predicate SimpleGraph (V E : Set) :=12 RelationOn(E, V) ∧ SymmetricOn(V, E) ∧ IrreflexiveOn(V, E)1314predicate Adjacent (E x y : Set) := relates(E, x, y)1516theorem SimpleGraph.relation (V E : Set)17requires graph : SimpleGraph(V, E)18shows RelationOn(E, V)19proof20simp [SimpleGraph]2122theorem SimpleGraph.symmetric (V E : Set)23requires graph : SimpleGraph(V, E)24shows SymmetricOn(V, E)25proof26simp [SimpleGraph]2728theorem SimpleGraph.irreflexive (V E : Set)29requires graph : SimpleGraph(V, E)30shows IrreflexiveOn(V, E)31proof32simp [SimpleGraph]3334theorem Adjacent.bounds (V E x y : Set)35requires graph : SimpleGraph(V, E)36requires edge : Adjacent(E, x, y)37shows x ∈ V ∧ y ∈ V38proof39unfold Adjacent at edge40have relation := SimpleGraph.relation with graph41unfold RelationOn at relation42exact RelationBetween.bounds with relation, edge4344theorem Adjacent.symmetric (V E x y : Set)45requires graph : SimpleGraph(V, E)46requires edge : Adjacent(E, x, y)47shows Adjacent(E, y, x)48proof49obtain⟨hx, hy⟩:= Adjacent.bounds with graph, edge50have symmetric := SimpleGraph.symmetric with graph51unfold Adjacent52exact symmetric with x, hx, y, hy, edge5354theorem Adjacent.distinct (V E x y : Set)55requires graph : SimpleGraph(V, E)56requires edge : Adjacent(E, x, y)57shows x ≠ y58proof59intro equal60obtain⟨hx, hy⟩:= Adjacent.bounds with graph, edge61have noLoop := SimpleGraph.irreflexive with graph62havecontradiction:= noLoop with x, hx63rw← equal at edge64applycontradiction65exact edge6667-- All neighbors lie in V; this is a set by separation.68def neighbors (V E x : Set) : Set:= { y ∈ V | Adjacent(E, x, y) }6970theorem neighbors.symmetric (V E x y : Set)71requires graph : SimpleGraph(V, E)72requires hx : x ∈ V73requires hy : y ∈ neighbors(V, E, x)74shows x ∈ neighbors(V, E, y)75proof76simp [neighbors.membership] at hy77rcases hy with⟨hyV, edge⟩78apply neighbors.membership.backward79constructor80 . exact hx81 . exact Adjacent.symmetric with graph, edge8283theorem neighbors.excludes_self (V E x : Set)84requires graph : SimpleGraph(V, E)85shows¬(x ∈ neighbors(V, E, x))86proof87intro member88simp [neighbors.membership] at member89rcases member with⟨hx, edge⟩90have distinct := Adjacent.distinct with graph, edge91apply distinct92rfl9394-- A subgraph can delete vertices and edges independently.95predicate Subgraph (V E W F : Set) :=96 SimpleGraph(W, F) ∧ W ⊆ V ∧ F ⊆ E9798theorem Subgraph.vertices (V E W F : Set)99requires sub : Subgraph(V, E, W, F)100shows W ⊆ V101proof102simp [Subgraph]103104theorem Subgraph.edges (V E W F : Set)105requires sub : Subgraph(V, E, W, F)106shows F ⊆ E107proof108simp [Subgraph]109110theorem Subgraph.adjacent (V E W F x y : Set)111requires sub : Subgraph(V, E, W, F)112requires edge : Adjacent(F, x, y)113shows Adjacent(E, x, y)114proof115have included := Subgraph.edges with sub116unfold Adjacent at edge117unfold Adjacent118unfold relates119exact included with (x, y), edge120121-- Restriction to a vertex set keeps precisely the edges with both ends there.122def inducedEdges (E W : Set) : Set:= E ∩ (W × W)123124theorem inducedEdges.membership (E W x y : Set)125shows relates(inducedEdges(E, W), x, y) ↔126 (relates(E, x, y) ∧ x ∈ W ∧ y ∈ W)127proof128constructor129 . intro h130unfold relates at h131unfold inducedEdges at h132simp [intersection.membership] at h133rcases h with⟨edge, pair⟩134obtain⟨bounded, a, ha, b, hb, equality⟩:= product.membership.forward with pair135obtain⟨xa, yb⟩:= orderedPair.injective with equality136rw← xa at ha137rw← yb at hb138constructor139exact edge140 . constructor141 . exact ha142 . exact hb143 . intro h144rcases h with⟨edge, hx, hy⟩145unfold relates146unfold inducedEdges147apply intersection.membership.backward148have pair := product.pair with hx, hy149refine⟨edge, pair⟩150151theorem inducedEdges.graph (V E W : Set)152requires graph : SimpleGraph(V, E)153requires vertices : W ⊆ V154shows SimpleGraph(W, inducedEdges(E, W))155proof156constructor157intros p hp158unfold inducedEdges at hp159simp [intersection.membership] at hp160rcases hp with⟨edge, pair⟩161exact pair162 . constructor163intros x hx y hy edge164obtain⟨original, wx, wy⟩:= inducedEdges.membership.forward with edge165have symmetric := SimpleGraph.symmetric with graph166have xInV := vertices with x, hx167have yInV := vertices with y, hy168have old := symmetric with x, xInV, y, yInV, original169exact inducedEdges.membership.backward with⟨old, hy, hx⟩170intros x hx edge171obtain⟨original, wx, wy⟩:= inducedEdges.membership.forward with edge172have noLoop := SimpleGraph.irreflexive with graph173have xInV := vertices with x, hx174have impossible := noLoop with x, xInV175apply impossible176exact original177178theorem inducedEdges.subgraph (V E W : Set)179requires graph : SimpleGraph(V, E)180requires vertices : W ⊆ V181shows Subgraph(V, E, W, inducedEdges(E, W))182proof183constructor184 . exact inducedEdges.graph with graph, vertices185 . constructor186 . exact vertices187 . unfold inducedEdges188exact intersection.subset_left189