From e6e278d00e9039791508bbda161ee3482f9e1f58 Mon Sep 17 00:00:00 2001 From: William-Kenyon <115789274+William-Kenyon@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:45:26 +0000 Subject: [PATCH 1/5] add halfsquare and tests --- gap/oper.gd | 3 ++ gap/oper.gi | 71 +++++++++++++++++++++++++++++++++++++++++++ tst/standard/oper.tst | 37 +++++++++++++++++++++- 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/gap/oper.gd b/gap/oper.gd index ce77dc770..f69694db1 100644 --- a/gap/oper.gd +++ b/gap/oper.gd @@ -76,6 +76,9 @@ DeclareOperation("OnMultiDigraphs", [IsDigraph, IsPerm, IsPerm]); # 5. Substructures and quotients . . . DeclareOperation("QuotientDigraph", [IsDigraph, IsList]); DeclareOperation("InducedSubdigraph", [IsDigraph, IsList]); +DeclareOperation("DigraphHalfSquareNC", [IsDigraph, IsList]); +DeclareOperation("DigraphHalfSquare", [IsDigraph, IsList]); +DeclareOperation("DigraphHalfSquares", [IsDigraph, IsList]); # 6. In and out degrees, neighbours, and edges of vertices . . . DeclareOperation("InDegreeOfVertex", [IsDigraph, IsPosInt]); diff --git a/gap/oper.gi b/gap/oper.gi index 638bf5331..6dfe4556d 100644 --- a/gap/oper.gi +++ b/gap/oper.gi @@ -1272,6 +1272,77 @@ InstallMethod(QuotientDigraph, {D, partition} -> MakeImmutable(QuotientDigraph(DigraphMutableCopy(D), partition))); +InstallMethod(DigraphHalfSquareNC, "for a digraph and a list", +[IsDigraph, IsList], +function(D, U) + local pos, i, u, w, x, sharecommon, outnbs; + + # for vertex relabelling from D to U + pos := ListWithIdenticalEntries(DigraphNrVertices(D), 0); + for i in [1 .. Length(U)] do + pos[U[i]] := i; + od; + + # two vertices u, x are joined if they share a common neighbour w in + # the other side of the bipartition, i.e. there exists a path u -> w -> x + outnbs := []; + for i in [1 .. Length(U)] do + u := U[i]; + sharecommon := []; + for w in OutNeighboursOfVertex(D, u) do + for x in OutNeighboursOfVertex(D, w) do + if x <> u then + Add(sharecommon, pos[x]); + fi; + od; + od; + Add(outnbs, Set(sharecommon)); + od; + + return Digraph(outnbs); +end); + +InstallMethod(DigraphHalfSquare, "for a digraph and a list", +[IsDigraph, IsList], +function(D, U) + local v, w, nU, inU, whereverts; + + if not IsBipartiteDigraph(D) then + ErrorNoReturn("the 1st argument must be a bipartite digraph"); + fi; + + nU := Set(U); + if not IsSubset(DigraphVertices(D), nU) then + ErrorNoReturn("the 2nd argument must be a list of vertices of "); + fi; + + # check if U is a bicomponent of D, i.e. no edges between vertices in U + # can't do 'U in DigraphBicomponents(D)' because the partition isn't unique + inU := BlistList([1 .. DigraphNrVertices(D)], nU); + for v in DigraphVertices(D) do + for w in OutNeighboursOfVertex(D, v) do + if inU[v] = inU[w] then + whereverts := "lie in "; + if not inU[v] then + whereverts := "lie outside "; + fi; + ErrorNoReturn("the digraph is not bipartite with respect to ", + "the vertex set ; vertices ", v, " and ", w, + " are adjacent and ", whereverts); + fi; + od; + od; + + return DigraphHalfSquareNC(D, nU); +end); + +InstallMethod(DigraphHalfSquares, "for a digraph", [IsDigraph, IsList], +function(D, U) + local complement; + complement := Difference(DigraphVertices(D), U); + return [DigraphHalfSquare(D, U), DigraphHalfSquare(D, complement)]; +end); + ############################################################################# # 6. In and out degrees, neighbours, and edges of vertices ############################################################################# diff --git a/tst/standard/oper.tst b/tst/standard/oper.tst index 1c3872fc6..3cc17b37e 100644 --- a/tst/standard/oper.tst +++ b/tst/standard/oper.tst @@ -10,7 +10,7 @@ ## #@local C, D, D1, D2, D3, D3_edges, DD -#@local G, G1, L, TestPartialOrderDigraph +#@local G, G1, L, L1, TestPartialOrderDigraph #@local TestPartialOrderDigraph2, TestUnion, a, adj, b, comps, copy, d, e #@local edges, edges2, func, g, gr, gr1, gr2, gr3, gr4, gri, grrt, grt, h, i #@local i1, i2, id, idom, in1, in2, in3, iter, j1, j2, m, m1, m2, mat, n, nbs @@ -491,6 +491,41 @@ gap> qr := QuotientDigraph(gr, [[1], [2, 3, 5, 7], [4, 6, 8]]); gap> OutNeighbours(qr); [ [ 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ] +# DigraphHalfSquare[s] +gap> D := Digraph([[2], [3], [1]]);; +gap> DigraphHalfSquare(D, [1]); +Error, the 1st argument must be a bipartite digraph +gap> DigraphHalfSquares(D, [1]); +Error, the 1st argument must be a bipartite digraph +gap> D := ChainDigraph(5);; +gap> DigraphBicomponents(D); +[ [ 1, 3, 5 ], [ 2, 4 ] ] +gap> DigraphHalfSquare(D, [6]); +Error, the 2nd argument must be a list of vertices of +gap> DigraphHalfSquare(D, [1, 3]); +Error, the digraph is not bipartite with respect to the vertex set ; ve\ +rtices 4 and 5 are adjacent and lie outside +gap> OutNeighbours(DigraphHalfSquare(D, [1, 3, 5])); +[ [ 2 ], [ 3 ], [ ] ] +gap> OutNeighbours(DigraphHalfSquare(D, [2, 4])); +[ [ 2 ], [ ] ] +gap> D := DigraphSymmetricClosure(ChainDigraph(5));; +gap> OutNeighbours(DigraphHalfSquare(D, [1, 3, 5])); +[ [ 2 ], [ 1, 3 ], [ 2 ] ] +gap> D := CompleteBipartiteDigraph(3, 3);; +gap> D1 := DigraphHalfSquare(D, DigraphBicomponents(D)[1]);; +gap> IsIsomorphicDigraph(D1, CompleteDigraph(3)); +true +gap> D := CompleteBipartiteDigraph(2, 4);; +gap> L := DigraphHalfSquares(D, DigraphBicomponents(D)[1]);; +gap> IsIsomorphicDigraph(L[1], CompleteDigraph(2)); +true +gap> IsIsomorphicDigraph(L[2], CompleteDigraph(4)); +true +gap> L1 := DigraphHalfSquares(D, DigraphBicomponents(D)[2]);; +gap> L = Reversed(L1); +true + # DigraphInEdges and DigraphOutEdges: for a vertex gap> gr := Digraph([[2, 2, 2, 2, 2], [1, 1, 1, 1], [1], [3, 2]]); From 9077b3ce301f07338dcb5ae8929de439fca8163f Mon Sep 17 00:00:00 2001 From: William-Kenyon <115789274+William-Kenyon@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:45:32 +0000 Subject: [PATCH 2/5] docs --- doc/oper.xml | 45 +++++++++++++++++++++++++++++++++++++++++++++ doc/z-chap2.xml | 1 + 2 files changed, 46 insertions(+) diff --git a/doc/oper.xml b/doc/oper.xml index c7cf5d218..a5a1ec151 100644 --- a/doc/oper.xml +++ b/doc/oper.xml @@ -1053,6 +1053,51 @@ gap> QuotientDigraph(EmptyDigraph(0), []); <#/GAPDoc> +<#GAPDoc Label="DigraphHalfSquare"> + + + + A digraph. + + If digraph is a bipartite digraph D, and list is a list + of vertices U as one of the two sides of D (see + , ), then + returns the bipartite half or half-square of + digraph with respect to list. If digraph is not + bipartite, or list is not a side of digraph, then an + error is raised.

+ + The half-square of D with respect to U is a digraph whose + vertices are the elements of U, with an edge from u_1 to + u_2 if and only if there exists a directed path u_1 \to x \to u_2 in + D. The vertices are labelled 1, \ldots, |U|, + where the vertex i corresponds to the i-th smallest element of + U.

+ + returns a list containing two digraphs, the + half square of digraph with respect to list, and the half + square with respect to the vertices of digraph not in list, + i.e. the half squares of both sides of the bipartition. + D := ChainDigraph(5);; +gap> DigraphBicomponents(D); +[ [ 1, 3, 5 ], [ 2, 4 ] ] +gap> D1 := DigraphHalfSquare(D, [1, 3, 5]);; +gap> OutNeighbours(D1); +[ [ 2 ], [ 3 ], [ ] ] +gap> D := DigraphSymmetricClosure(ChainDigraph(5));; +gap> OutNeighbours(DigraphHalfSquare(D, [1, 3, 5])); +[ [ 2 ], [ 1, 3 ], [ 2 ] ] +gap> D := DigraphHalfSquares(CompleteBipartiteDigraph(2, 4), [1, 2]);; +gap> IsIsomorphicDigraph(D[1], CompleteDigraph(2)); +true +gap> IsIsomorphicDigraph(D[2], CompleteDigraph(4)); +true +]]> + + +<#/GAPDoc> + <#GAPDoc Label="IsDigraphEdge"> <#Include Label="DigraphShortestPathSpanningTree"> <#Include Label="QuotientDigraph"> + <#Include Label="DigraphHalfSquare"> <#Include Label="DigraphReverse"> <#Include Label="DigraphDual"> <#Include Label="DigraphSymmetricClosure"> From 5ee75b70e623d6050640fdf30a34d3dca983072e Mon Sep 17 00:00:00 2001 From: William-Kenyon <115789274+William-Kenyon@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:51:53 +0000 Subject: [PATCH 3/5] docs reword --- doc/oper.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/oper.xml b/doc/oper.xml index a5a1ec151..fa06b6885 100644 --- a/doc/oper.xml +++ b/doc/oper.xml @@ -1057,7 +1057,7 @@ gap> QuotientDigraph(EmptyDigraph(0), []); - A digraph. + A digraph or a list of digraphs. If digraph is a bipartite digraph D, and list is a list of vertices U as one of the two sides of D (see From 237076aefc87de9bd50cb2abd43fd5750404478d Mon Sep 17 00:00:00 2001 From: William-Kenyon <115789274+William-Kenyon@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:39:25 +0000 Subject: [PATCH 4/5] doc wording, simpler error check --- doc/oper.xml | 8 ++++---- gap/oper.gi | 29 +++++++++++------------------ tst/standard/oper.tst | 3 +-- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/doc/oper.xml b/doc/oper.xml index fa06b6885..d530ad712 100644 --- a/doc/oper.xml +++ b/doc/oper.xml @@ -1062,19 +1062,19 @@ gap> QuotientDigraph(EmptyDigraph(0), []); If digraph is a bipartite digraph D, and list is a list of vertices U as one of the two sides of D (see , ), then - returns the bipartite half or half-square of + DigraphHalfSquare" returns the bipartite half or half-square of digraph with respect to list. If digraph is not bipartite, or list is not a side of digraph, then an error is raised.

The half-square of D with respect to U is a digraph whose vertices are the elements of U, with an edge from u_1 to - u_2 if and only if there exists a directed path u_1 \to x \to u_2 in - D. The vertices are labelled 1, \ldots, |U|, + u_2 if and only if there exists a directed path u_1 \to x \to + u_2 in D. The vertices are 1, \ldots, |U|, where the vertex i corresponds to the i-th smallest element of U.

- returns a list containing two digraphs, the + DigraphHalfSquares returns a list containing two digraphs, the half square of digraph with respect to list, and the half square with respect to the vertices of digraph not in list, i.e. the half squares of both sides of the bipartition. diff --git a/gap/oper.gi b/gap/oper.gi index 6dfe4556d..dd01c73b2 100644 --- a/gap/oper.gi +++ b/gap/oper.gi @@ -1277,7 +1277,7 @@ InstallMethod(DigraphHalfSquareNC, "for a digraph and a list", function(D, U) local pos, i, u, w, x, sharecommon, outnbs; - # for vertex relabelling from D to U + # for vertex re-numbering from D to U pos := ListWithIdenticalEntries(DigraphNrVertices(D), 0); for i in [1 .. Length(U)] do pos[U[i]] := i; @@ -1305,7 +1305,7 @@ end); InstallMethod(DigraphHalfSquare, "for a digraph and a list", [IsDigraph, IsList], function(D, U) - local v, w, nU, inU, whereverts; + local nU; if not IsBipartiteDigraph(D) then ErrorNoReturn("the 1st argument must be a bipartite digraph"); @@ -1316,27 +1316,20 @@ function(D, U) ErrorNoReturn("the 2nd argument must be a list of vertices of "); fi; - # check if U is a bicomponent of D, i.e. no edges between vertices in U + # check if the set U and its complement D \ U are bicomponents, i.e. no edges + # between vertices in the same set # can't do 'U in DigraphBicomponents(D)' because the partition isn't unique - inU := BlistList([1 .. DigraphNrVertices(D)], nU); - for v in DigraphVertices(D) do - for w in OutNeighboursOfVertex(D, v) do - if inU[v] = inU[w] then - whereverts := "lie in "; - if not inU[v] then - whereverts := "lie outside "; - fi; - ErrorNoReturn("the digraph is not bipartite with respect to ", - "the vertex set ; vertices ", v, " and ", w, - " are adjacent and ", whereverts); - fi; - od; - od; + if not (IsIndependentSet(D, nU) and + IsIndependentSet(D, Difference(DigraphVertices(D), nU))) then + ErrorNoReturn("the digraph is not bipartite with respect to the vertex", + " set "); + fi; return DigraphHalfSquareNC(D, nU); end); -InstallMethod(DigraphHalfSquares, "for a digraph", [IsDigraph, IsList], +InstallMethod(DigraphHalfSquares, "for a digraph and a list", +[IsDigraph, IsList], function(D, U) local complement; complement := Difference(DigraphVertices(D), U); diff --git a/tst/standard/oper.tst b/tst/standard/oper.tst index 3cc17b37e..5b7014386 100644 --- a/tst/standard/oper.tst +++ b/tst/standard/oper.tst @@ -503,8 +503,7 @@ gap> DigraphBicomponents(D); gap> DigraphHalfSquare(D, [6]); Error, the 2nd argument must be a list of vertices of gap> DigraphHalfSquare(D, [1, 3]); -Error, the digraph is not bipartite with respect to the vertex set ; ve\ -rtices 4 and 5 are adjacent and lie outside +Error, the digraph is not bipartite with respect to the vertex set gap> OutNeighbours(DigraphHalfSquare(D, [1, 3, 5])); [ [ 2 ], [ 3 ], [ ] ] gap> OutNeighbours(DigraphHalfSquare(D, [2, 4])); From d07989f5d690c2faeaeda0b6cdba91308e29c2da Mon Sep 17 00:00:00 2001 From: William-Kenyon <115789274+William-Kenyon@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:28:57 +0000 Subject: [PATCH 5/5] lonely quotation --- doc/oper.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/oper.xml b/doc/oper.xml index d530ad712..8710f6e68 100644 --- a/doc/oper.xml +++ b/doc/oper.xml @@ -1062,7 +1062,7 @@ gap> QuotientDigraph(EmptyDigraph(0), []); If digraph is a bipartite digraph D, and list is a list of vertices U as one of the two sides of D (see , ), then - DigraphHalfSquare" returns the bipartite half or half-square of + DigraphHalfSquare returns the bipartite half or half-square of digraph with respect to list. If digraph is not bipartite, or list is not a side of digraph, then an error is raised.