Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions doc/oper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,51 @@ gap> QuotientDigraph(EmptyDigraph(0), []);
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="DigraphHalfSquare">
<ManSection>
<Oper Name="DigraphHalfSquare" Arg="digraph, list"/>
<Oper Name="DigraphHalfSquares" Arg="digraph, list"/>
<Returns>A digraph or a list of digraphs.</Returns>
<Description>
If <A>digraph</A> is a bipartite digraph <M>D</M>, and <A>list</A> is a list
of vertices <M>U</M> as one of the two sides of <M>D</M> (see
<Ref Prop="IsBipartiteDigraph"/>, <Ref Attr="DigraphBicomponents"/>), then
<C>DigraphHalfSquare</C> returns the bipartite half or half-square of
<A>digraph</A> with respect to <A>list</A>. If <A>digraph</A> is not
bipartite, or <A>list</A> is not a side of <A>digraph</A>, then an
error is raised. <P/>

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

<C>DigraphHalfSquares</C> returns a list containing two digraphs, the
half square of <A>digraph</A> with respect to <A>list</A>, and the half
square with respect to the vertices of <A>digraph</A> not in <A>list</A>,
i.e. the half squares of both sides of the bipartition.
<Example><![CDATA[
gap> 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
]]></Example>
</Description>
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="IsDigraphEdge">
<ManSection>
<Oper Name="IsDigraphEdge" Arg="digraph, list"
Expand Down
1 change: 1 addition & 0 deletions doc/z-chap2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<#Include Label="UndirectedSpanningTree">
<#Include Label="DigraphShortestPathSpanningTree">
<#Include Label="QuotientDigraph">
<#Include Label="DigraphHalfSquare">
<#Include Label="DigraphReverse">
<#Include Label="DigraphDual">
<#Include Label="DigraphSymmetricClosure">
Expand Down
3 changes: 3 additions & 0 deletions gap/oper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
64 changes: 64 additions & 0 deletions gap/oper.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,70 @@ 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 re-numbering 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 nU;

if not IsBipartiteDigraph(D) then
ErrorNoReturn("the 1st argument <D> must be a bipartite digraph");
fi;

nU := Set(U);
if not IsSubset(DigraphVertices(D), nU) then
ErrorNoReturn("the 2nd argument <U> must be a list of vertices of <D>");
fi;

# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm intrigued by this comment, why is the partition not unique? In what sense? Anyway, if you really only want to check "no edges between vertices in U" then you can use IsIndependentSet(D, U); to replace all these lines.

@William-Kenyon William-Kenyon Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs: For a bipartite digraph with at least 3 vertices, there is a unique pair of bicomponents of bipartite if and only if the digraph is connected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're right, of course, not sure what I was thinking

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other comment might make the code a little simpler though.

if not (IsIndependentSet(D, nU) and
IsIndependentSet(D, Difference(DigraphVertices(D), nU))) then
ErrorNoReturn("the digraph <D> is not bipartite with respect to the vertex",
" set <U>");
fi;

return DigraphHalfSquareNC(D, nU);
end);

InstallMethod(DigraphHalfSquares, "for a digraph and a list",
[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
#############################################################################
Expand Down
36 changes: 35 additions & 1 deletion tst/standard/oper.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -491,6 +491,40 @@ 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 <D> must be a bipartite digraph
gap> DigraphHalfSquares(D, [1]);
Error, the 1st argument <D> 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 <U> must be a list of vertices of <D>
gap> DigraphHalfSquare(D, [1, 3]);
Error, the digraph <D> is not bipartite with respect to the vertex set <U>
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]]);
<immutable multidigraph with 4 vertices, 12 edges>
Expand Down