Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MT-resource-bot committed Jan 23, 2020
1 parent 6b94e2e commit 722037c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 72 deletions.
2 changes: 2 additions & 0 deletions gap/grahom.gd
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ DeclareOperation("IsDigraphEmbedding",

DeclareOperation("IsDigraphColouring", [IsDigraph, IsList]);
DeclareOperation("IsDigraphColouring", [IsDigraph, IsTransformation]);

DeclareGlobalFunction("DIGRAPHS_RespectsColouring");
129 changes: 71 additions & 58 deletions gap/grahom.gi
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,50 @@ end);
# IsDigraph{Homo/Epi/...}morphism
########################################################################

# Given a non-negative integer <n> and a homogeneous list <partition>,
# this global function tests whether <partition> is a valid partition
# of the list [1 .. n]. A valid partition of [1 .. n] is either:
#
# 1. A list of length <n> consisting of a numbers, such that the set of these
# numbers is [1 .. m] for some m <= n.
# 2. A list of non-empty disjoint lists whose union is [1 .. n].
#
# If <partition> is a valid partition of [1 .. n] then this global function
# returns the partition, in form 1 (converting it to this form if necessary).
# If <partition> is invalid, then the function returns <fail>.

# Given:
#
# 1) two digraphs <src> and <ran>,
# 2) a transformation or permutation <x> mapping the vertices of <src> to
# <ran>, and
# 3) two lists <cols1> and <cols2> of positive integers defining vertex
# colourings of <src> and <ran>,
#
# this global function tests whether <x> respects the colouring, i.e. whether
# for all vertices i in <src>, cols[i] = cols[i ^ x].

InstallGlobalFunction(DIGRAPHS_RespectsColouring,
function(src, ran, x, cols1, cols2)

if not IsDigraph(src) then
ErrorNoReturn("the first argument <src> must be a digraph,");
elif not IsDigraph(ran) then
ErrorNoReturn("the second argument <ran> must be a digraph,");
elif not (IsPerm(x) or IsTransformation(x)) then
ErrorNoReturn("the third argument <x> must be a transformation or ",
"a permutation,");
elif Maximum(OnTuples(DigraphVertices(src), x)) > DigraphNrVertices(ran) then
ErrorNoReturn("the third argument <x> must map the vertices of the first ",
"argument <src> into the vertices of the second argument ",
"<ran>,");
fi;
DIGRAPHS_ValidateVertexColouring(DigraphNrVertices(src), cols1);
DIGRAPHS_ValidateVertexColouring(DigraphNrVertices(ran), cols2);

return ForAll(DigraphVertices(src), i -> cols1[i] = cols2[i ^ x]);
end);

InstallMethod(IsDigraphHomomorphism,
"for a digraph by out-neighbours, a digraph, and a perm",
[IsDigraphByOutNeighboursRep, IsDigraph, IsPerm],
Expand Down Expand Up @@ -492,15 +536,9 @@ end);
InstallMethod(IsDigraphHomomorphism,
"for a digraph by out-neighbours, a digraph, a transformation, and two lists",
[IsDigraphByOutNeighboursRep, IsDigraph, IsTransformation, IsList, IsList],
function(src, ran, x, colours1, colours2)
if not IsDigraphHomomorphism(src, ran, x) then
return false;
fi;

DIGRAPHS_ValidateVertexColouring(DigraphNrVertices(src), colours1);
DIGRAPHS_ValidateVertexColouring(DigraphNrVertices(ran), colours2);

return ForAll(DigraphVertices(src), i -> colours1[i] = colours2[i ^ x]);
function(src, ran, x, cols1, cols2)
return IsDigraphHomomorphism(src, ran, x) and
DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphEndomorphism, "for a digraph and a transformation",
Expand All @@ -521,9 +559,9 @@ end);

InstallMethod(IsDigraphEpimorphism, "for digraph, digraph, and transformation",
[IsDigraph, IsDigraph, IsTransformation, IsList, IsList],
function(src, ran, x, c1, c2)
return IsDigraphHomomorphism(src, ran, x, c1, c2)
and OnSets(DigraphVertices(src), x) = DigraphVertices(ran);
function(src, ran, x, cols1, cols2)
return IsDigraphEpimorphism(src, ran, x) and
DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphEpimorphism, "for digraph, digraph, and perm",
Expand All @@ -536,9 +574,9 @@ end);
InstallMethod(IsDigraphEpimorphism,
"for digraph, digraph, perm, list, and list",
[IsDigraph, IsDigraph, IsPerm, IsList, IsList],
function(src, ran, x, c1, c2)
return IsDigraphHomomorphism(src, ran, x, c1, c2)
and OnSets(DigraphVertices(src), x) = DigraphVertices(ran);
function(src, ran, x, cols1, cols2)
return IsDigraphEpimorphism(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphMonomorphism,
Expand All @@ -552,38 +590,19 @@ end);
InstallMethod(IsDigraphMonomorphism,
"for digraph, digraph, transformation, list, list",
[IsDigraph, IsDigraph, IsTransformation, IsList, IsList],
function(src, ran, x, c1, c2)
return IsDigraphHomomorphism(src, ran, x, c1, c2)
and IsInjectiveListTrans(DigraphVertices(src), x);
function(src, ran, x, cols1, cols2)
return IsDigraphMonomorphism(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphMonomorphism, "for digraph, digraph, and perm",
[IsDigraph, IsDigraph, IsPerm], IsDigraphHomomorphism);

InstallMethod(IsDigraphMonomorphism, "for digraph, digraph, perm, list, list",
[IsDigraph, IsDigraph, IsPerm, IsList, IsList], IsDigraphHomomorphism);

InstallMethod(IsDigraphEmbedding,
"for digraph, digraph by out-neighbours, transformation, list, and list",
[IsDigraph, IsDigraphByOutNeighboursRep, IsTransformation, IsList, IsList],
function(src, ran, x, c1, c2)
local y, induced, i, j;
if not IsDigraphMonomorphism(src, ran, x, c1, c2) then
return false;
fi;
y := MappingPermListList(OnTuples(DigraphVertices(src), x),
DigraphVertices(src));
induced := BlistList(DigraphVertices(ran), OnSets(DigraphVertices(src), x));
for i in DigraphVertices(ran) do
if induced[i] then
for j in OutNeighbours(ran)[i] do
if induced[j] and not IsDigraphEdge(src, i ^ y, j ^ y) then
return false;
fi;
od;
fi;
od;
return true;
[IsDigraph, IsDigraph, IsPerm, IsList, IsList],
function(src, ran, x, cols1, cols2)
return IsDigraphMonomorphism(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphEmbedding,
Expand All @@ -609,6 +628,14 @@ function(src, ran, x)
return true;
end);

InstallMethod(IsDigraphEmbedding,
"for digraph, digraph by out-neighbours, transformation, list, and list",
[IsDigraph, IsDigraphByOutNeighboursRep, IsTransformation, IsList, IsList],
function(src, ran, x, cols1, cols2)
return IsDigraphEmbedding(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphEmbedding,
"for a digraph, a digraph by out-neighbours, and a perm",
[IsDigraph, IsDigraphByOutNeighboursRep, IsPerm],
Expand All @@ -634,23 +661,9 @@ end);
InstallMethod(IsDigraphEmbedding,
"for a digraph, a digraph by out-neighbours, a perm, a list, and a list",
[IsDigraph, IsDigraphByOutNeighboursRep, IsPerm, IsList, IsList],
function(src, ran, x, c1, c2)
local y, induced, i, j;
if not IsDigraphHomomorphism(src, ran, x, c1, c2) then
return false;
fi;
y := x ^ -1;
induced := BlistList(DigraphVertices(ran), OnSets(DigraphVertices(src), x));
for i in DigraphVertices(ran) do
if induced[i] then
for j in OutNeighbours(ran)[i] do
if induced[j] and not IsDigraphEdge(src, i ^ y, j ^ y) then
return false;
fi;
od;
fi;
od;
return true;
function(src, ran, x, cols1, cols2)
return IsDigraphEmbedding(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphColouring, "for a digraph by out-neighbours and a list",
Expand Down
20 changes: 6 additions & 14 deletions gap/isomorph.gi
Original file line number Diff line number Diff line change
Expand Up @@ -721,13 +721,9 @@ end);
InstallMethod(IsDigraphIsomorphism,
"for digraph, digraph, permutation, list, and list",
[IsDigraph, IsDigraph, IsPerm, IsList, IsList],
function(src, ran, x, c1, c2)
if IsMultiDigraph(src) or IsMultiDigraph(ran) then
ErrorNoReturn("the 1st and 2nd arguments <src> and <ran> must not have ",
"multiple edges,");
fi;
return IsDigraphHomomorphism(src, ran, x, c1, c2)
and IsDigraphHomomorphism(ran, src, x ^ -1, c1, c2);
function(src, ran, x, cols1, cols2)
return IsDigraphIsomorphism(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphAutomorphism, "for a digraph and a permutation",
Expand All @@ -753,13 +749,9 @@ end);
InstallMethod(IsDigraphIsomorphism,
"for digraph, digraph, transformation, list, and list",
[IsDigraph, IsDigraph, IsTransformation, IsList, IsList],
function(src, ran, x, c1, c2)
local y;
y := AsPermutation(RestrictedTransformation(x, DigraphVertices(src)));
if y = fail then
return false;
fi;
return IsDigraphIsomorphism(src, ran, y, c1, c2);
function(src, ran, x, cols1, cols2)
return IsDigraphIsomorphism(src, ran, x)
and DIGRAPHS_RespectsColouring(src, ran, x, cols1, cols2);
end);

InstallMethod(IsDigraphAutomorphism, "for a digraph and a transformation",
Expand Down

0 comments on commit 722037c

Please sign in to comment.