1-- Complete graphs, complements, cliques, and independent sets.2-- 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.Complement8import Walks9import Cardinal.Countable1011-- Separation from V × V makes this a set, even for infinite V.12def completeEdges (V : Set) : Set:=13 { p ∈ V × V |∃ x ∈ V, ∃ y ∈ V, p = (x, y) ∧ x ≠ y }1415theorem completeEdges.adjacent (V x y : Set)16shows Adjacent(completeEdges(V), x, y) ↔17 (x ∈ V ∧ y ∈ V ∧ x ≠ y)18proof19constructor20 . intro edge21unfold Adjacent at edge22unfold relates at edge23simp [completeEdges] at edge24rcases edge with⟨pair, a, ha, b, hb, equality, distinct⟩25obtain⟨xa, yb⟩:= orderedPair.injective with equality26rw← xa at ha27rw← yb at hb28rw [← xa, ← yb] at distinct29refine⟨ha, hb, distinct⟩30 . intro h31rcases h with⟨hx, hy, distinct⟩32unfold Adjacent33unfold relates34simp [completeEdges]35have pair := product.pair with hx, hy36refine⟨pair, x, hx, y, hy, rfl, distinct⟩3738theorem completeEdges.graph (V : Set)39shows SimpleGraph(V, completeEdges(V))40proof41constructor42intros p hp43simp [completeEdges] at hp44rcases hp with⟨pair, witness⟩45exact pair46 . constructor47intros x hx y hy edge48have adjacent : Adjacent(completeEdges(V), x, y) byexact edge49obtain⟨hx2, hy2, distinct⟩:= completeEdges.adjacent.forward with adjacent50have symmetricEdge : Adjacent(completeEdges(V), y, x)51apply completeEdges.adjacent.backward52constructor53 . exact hy54 . constructor55 . exact hx56 . intro equal57apply distinct58rw equal59rfl60exact symmetricEdge61intros x hx edge62have adjacent : Adjacent(completeEdges(V), x, x) byexact edge63obtain⟨hx2, hx3, distinct⟩:= completeEdges.adjacent.forward with adjacent64apply distinct65rfl6667theorem SimpleGraph.edges_in_complete (V E : Set)68requires graph : SimpleGraph(V, E)69shows E ⊆ completeEdges(V)70proof71intros p hp72have relation := SimpleGraph.relation with graph73unfold RelationOn at relation74unfold RelationBetween at relation75have pair := relation with p, hp76obtain⟨bounded, x, hx, y, hy, equality⟩:= product.membership.forward with pair77have edge : Adjacent(E, x, y)78unfold Adjacent79unfold relates80 rwa ← equality81have distinct := Adjacent.distinct with graph, edge82have complete : Adjacent(completeEdges(V), x, y) := completeEdges.adjacent.backward with⟨hx, hy, distinct⟩83unfold Adjacent at complete84unfold relates at complete85 rwa equality8687def complementEdges (V E : Set) : Set:= completeEdges(V) ∖ E8889theorem complementEdges.membership (V E x y : Set)90shows Adjacent(complementEdges(V, E), x, y) ↔91 (x ∈ V ∧ y ∈ V ∧ x ≠ y ∧¬Adjacent(E, x, y))92proof93constructor94 . intro edge95unfold Adjacent at edge96unfold relates at edge97unfold complementEdges at edge98simp [difference.membership] at edge99rcases edge with⟨complete, absent⟩100have adjacent : Adjacent(completeEdges(V), x, y)101unfold Adjacent102exact complete103obtain⟨hx, hy, distinct⟩:= completeEdges.adjacent.forward with adjacent104constructor105 . exact hx106 . constructor107 . exact hy108 . constructor109 . exact distinct110 . intro edge111unfold Adjacent at edge112apply absent113exact edge114 . intro h115rcases h with⟨hx, hy, distinct, absent⟩116have complete : Adjacent(completeEdges(V), x, y) := completeEdges.adjacent.backward with⟨hx, hy, distinct⟩117unfold Adjacent at complete118unfold Adjacent119unfold relates120unfold complementEdges121apply difference.membership.backward122constructor123 . exact complete124 . intro edge125apply absent126unfold Adjacent127exact edge128129theorem complementEdges.graph (V E : Set)130requires graph : SimpleGraph(V, E)131shows SimpleGraph(V, complementEdges(V, E))132proof133constructor134intros p hp135unfold complementEdges at hp136simp [difference.membership] at hp137rcases hp with⟨complete, absent⟩138have relation := SimpleGraph.relation at V, completeEdges(V) with completeEdges.graph at V139unfold RelationOn at relation140unfold RelationBetween at relation141exact relation with p, complete142 . constructor143intros x hx y hy edge144have adjacent : Adjacent(complementEdges(V, E), x, y) byexact edge145obtain⟨hx2, hy2, distinct, absent⟩:= complementEdges.membership.forward with adjacent146have reverseEdge : Adjacent(complementEdges(V, E), y, x)147apply complementEdges.membership.backward148constructor149 . exact hy150 . constructor151 . exact hx152 . constructor153 . intro equal154apply distinct155rw equal156rfl157 . intro reverse158have forward := Adjacent.symmetric with graph, reverse159apply absent160exact forward161exact reverseEdge162intros x hx edge163have adjacent : Adjacent(complementEdges(V, E), x, x) byexact edge164obtain⟨hx2, hx3, distinct, absent⟩:= complementEdges.membership.forward with adjacent165apply distinct166rfl167168predicate Clique (V E S : Set) :=169 S ⊆ V ∧ (∀ x ∈ S, ∀ y ∈ S, x ≠ y → Adjacent(E, x, y))170171predicate Independent (V E S : Set) :=172 S ⊆ V ∧ (∀ x ∈ S, ∀ y ∈ S, ¬Adjacent(E, x, y))173174theorem Independent.in_complement (V E S : Set)175requires independent : Independent(V, E, S)176shows Clique(V, complementEdges(V, E), S)177proof178rcases independent with⟨included, noEdges⟩179constructor180 . exact included181 . intros x hx y hy distinct182have xInV := included with x, hx183have yInV := included with y, hy184have absent := noEdges with x, hx, y, hy185exact complementEdges.membership.backward with⟨xInV, yInV, distinct, absent⟩186187theorem Clique.in_complement (V E S : Set)188requires clique : Clique(V, complementEdges(V, E), S)189requires graph : SimpleGraph(V, E)190shows Independent(V, E, S)191proof192rcases clique with⟨included, allEdges⟩193constructor194 . exact included195 . intros x hx y hy edge196have distinct := Adjacent.distinct with graph, edge197have complementary := allEdges with x, hx, y, hy, distinct198obtain⟨xInV, yInV, d, absent⟩:= complementEdges.membership.forward with complementary199apply absent200exact edge201202theorem complementEdges.involution (V E : Set)203requires graph : SimpleGraph(V, E)204shows complementEdges(V, complementEdges(V, E)) = E205proof206ext p207have included := SimpleGraph.edges_in_complete with graph208have membership : p ∈ E → p ∈ completeEdges(V)209intro hp210exact included with p, hp211constructor212 . intro h213unfold complementEdges at h214obtain⟨inComplete, notInner⟩:= difference.membership.forward with h215havecases : p ∈ E ∨¬(p ∈ E) bytauto216casescaseswith yes | no217 . exact yes218 . exfalso219apply notInner220exact difference.membership.backward with⟨inComplete, no⟩221 . intro he222unfold complementEdges223apply difference.membership.backward224constructor225 . exact membership with he226 . intro inner227obtain⟨inComplete, absent⟩:= difference.membership.forward with inner228apply absent229exact he230231def independentSets (V E : Set) : Set:=232 { S ∈𝒫(V) | Independent(V, E, S) }233234theorem independentSets.empty_graph (V : Set)235shows independentSets(V, empty) =𝒫(V)236proof237ext S238constructor239 . intro h240simp [independentSets] at h241rcases h with⟨inPower, independent⟩242exact inPower243 . intro inPower244have subset :=set.powerset.forward with inPower245simp [independentSets]246constructor247 . exact inPower248constructor249 . unfold Subset250exact subset251 . intros x hx y hy edge252unfold Adjacent at edge253unfold relates at edge254exactset.empty with edge255256-- Cantor's diagonal argument has a graph-theoretic consequence even when257-- there are no edges: the independent vertex subsets cannot be enumerated258-- by the vertices themselves.259theorem Independent.no_surjection_from_vertices (V f : Set)260shows¬SurjectiveBetween(f, V, independentSets(V, empty))261proof262intro onto263have allSubsets := independentSets.empty_graph at V264rw allSubsets at onto265exact Cantor.no_surjection_onto_powerset with onto266