Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KnightsGraph #425

Merged
merged 1 commit into from
Mar 11, 2021
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
44 changes: 44 additions & 0 deletions doc/examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,47 @@ true
</Description>
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="KnightsGraph">
<ManSection>
<Oper Name="KnightsGraph" Arg="[filt, ]m, n"/>
<Returns>A digraph.</Returns>
<Description>
If <A>m</A> and <A>n</A> are both positive integers, then this operation
returns the <E>Knight's Graph</E> for a <A>m</A> by <A>n</A> board. <P/>

From
<URL>https://en.wikipedia.org/wiki/Knight%27s_graph</URL>:
<P/>

<Q>In graph theory, a knight's graph, or a knight's tour graph, is a
graph that represents all legal moves of the knight chess piece on a
chessboard. Each vertex of this graph represents a square of the
chessboard, and each edge connects two squares that are a knight's move
apart from each other. More specifically, an <A>m</A> by <A>n</A> knight's
graph is a knight's graph of an <A>m</A> by <A>n</A> chessboard.</Q>
<P/>

If the optional first argument <A>filt</A> is present, then this should
specify the category or representation the digraph being created will
belong to. For example, if <A>filt</A> is <Ref Filt="IsMutableDigraph"/>,
then the digraph being created will be mutable, if <A>filt</A> is <Ref
Filt="IsImmutableDigraph"/>, then the digraph will be immutable.
If the optional first argument <A>filt</A> is not present, then <Ref
Filt="IsImmutableDigraph"/> is used by default.<P/>

<Example><![CDATA[
gap> D := KnightsGraph(8, 8);
<immutable connected symmetric digraph with 64 vertices, 336 edges>
gap> IsConnectedDigraph(D);
true
gap> D := KnightsGraph(3, 3);
<immutable symmetric digraph with 9 vertices, 16 edges>
gap> IsConnectedDigraph(D);
false
gap> KnightsGraph(IsMutable, 3, 9);
<mutable digraph with 27 vertices, 88 edges>
]]></Example>
</Description>
</ManSection>
<#/GAPDoc>
1 change: 1 addition & 0 deletions doc/z-chap2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<#Include Label="JohnsonDigraph">
<#Include Label="PetersenGraph">
<#Include Label="GeneralisedPetersenGraph">
<#Include Label="KnightsGraph">
Copy link
Collaborator

Choose a reason for hiding this comment

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

When you write a new piece of documentation, you also have to explicitly link it include the manual by including it in one of the chapters, i.e. in one of the doc/z-chap*.xml files. Sometimes it's difficult to work out where to put your documentation, but in this case it's easy - we put it with the other standard examples.

If you don't do this step, then your documentation doesn't end up in the compiled version of the manual, and you can't access the manual entry in GAP by doing (in this case) ?KnightsGraph.

<#Include Label="StarDigraph">
</Section>

Expand Down
4 changes: 4 additions & 0 deletions gap/examples.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ DeclareOperation("GeneralisedPetersenGraph", [IsFunction, IsInt, IsInt]);
DeclareConstructor("StarDigraphCons", [IsDigraph, IsPosInt]);
DeclareOperation("StarDigraph", [IsPosInt]);
DeclareOperation("StarDigraph", [IsFunction, IsPosInt]);

DeclareConstructor("KnightsGraphCons", [IsDigraph, IsPosInt, IsPosInt]);
DeclareOperation("KnightsGraph", [IsPosInt, IsPosInt]);
DeclareOperation("KnightsGraph", [IsFunction, IsPosInt, IsPosInt]);
46 changes: 46 additions & 0 deletions gap/examples.gi
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,49 @@ function(filt, k)
SetIsCompleteBipartiteDigraph(D, k > 1);
return D;
end);

InstallMethod(KnightsGraphCons,
"for IsMutableDigraph and two positive integers",
[IsMutableDigraph, IsPosInt, IsPosInt],
function(filt, m, n)
local D, moveOffSets, coordinates, target, i, j, iPos;
D := EmptyDigraph(IsMutableDigraph, m * n);
moveOffSets := [[2, 1], [-2, 1], [2, -1], [-2, -1],
[1, 2], [-1, 2], [1, -2], [-1, -2]];
coordinates := [];
for i in [1 .. n] do
for j in [1 .. m] do
Add(coordinates, [i, j]);
od;
od;
wilfwilson marked this conversation as resolved.
Show resolved Hide resolved
iPos := 0;
for i in coordinates do
iPos := iPos + 1;
for j in moveOffSets do
target := [i[1] + j[1], i[2] + j[2]];
if target[1] in [1 .. n] and target[2] in [1 .. m] then
DigraphAddEdge(D, [iPos, (target[1] - 1) * m + target[2]]);
fi;
od;
od;
return D;
end);

InstallMethod(KnightsGraphCons,
"for IsImmutableDigraph and two positive integers",
[IsImmutableDigraph, IsPosInt, IsPosInt],
function(filt, m, n)
local D;
D := MakeImmutable(KnightsGraphCons(IsMutableDigraph, m, n));
SetIsMultiDigraph(D, false);
SetIsSymmetricDigraph(D, true);
wilfwilson marked this conversation as resolved.
Show resolved Hide resolved
SetIsConnectedDigraph(D, m > 2 and n > 2 and not (m = 3 and n = 3));
return D;
end);

InstallMethod(KnightsGraph, "for a function and two positive integers",
[IsFunction, IsPosInt, IsPosInt],
KnightsGraphCons);

InstallMethod(KnightsGraph, "for two positive integers", [IsPosInt, IsPosInt],
{m, n} -> KnightsGraphCons(IsImmutableDigraph, m, n));
12 changes: 12 additions & 0 deletions tst/standard/examples.tst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ true
gap> IsMultiDigraph(StarDigraph(3));
false

# Knight's Graph
gap> D := KnightsGraph(8, 8);
<immutable connected symmetric digraph with 64 vertices, 336 edges>
gap> IsConnectedDigraph(D);
true
gap> D := KnightsGraph(3, 3);
<immutable symmetric digraph with 9 vertices, 16 edges>
gap> IsConnectedDigraph(D);
false
gap> KnightsGraph(IsMutable, 3, 9);
<mutable digraph with 27 vertices, 88 edges>

#
gap> DIGRAPHS_StopTest();
gap> STOP_TEST("Digraphs package: standard/examples.tst", 0);