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

Fix edge placement in HanoiGraph #699

Merged
merged 2 commits into from
Sep 5, 2024
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
2 changes: 1 addition & 1 deletion doc/examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ true]]></Example>
<E>Hanoi graph</E>. The Hanoi graph's vertices represent the possible states
of the 'Tower of Hanoi' puzzle on three 'towers', while its edges represent
possible moves. The Hanoi graph has <C>3^<A>n</A></C> vertices, and
<C>Binomial(3, 3) * (3^<A>n</A> - 1) / 2</C> undirected edges.<P/>
<C>3 * (3^<A>n</A> - 1) / 2</C> undirected edges.<P/>

The Hanoi graph is Hamiltonian. The graph superficially resembles the
Sierpinski triangle. The graph is also a 'penny graph' - a graph whose
Expand Down
25 changes: 16 additions & 9 deletions gap/examples.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1251,22 +1251,29 @@ InstallMethod(HalvedCubeGraph, "for a function and an integer",
InstallMethod(HanoiGraphCons, "for IsMutableDigraph and an integer",
[IsMutableDigraph, IsPosInt],
function(_, n)
local D, nrVert, prevNrVert, exp, i;
local e, D, nrVert, prevNrVert, anchor1, anchor2, anchor3, i;
D := Digraph(IsMutableDigraph, []);
nrVert := 3 ^ n;
DigraphAddVertices(D, nrVert);
DigraphAddEdges(D, [[1, 2], [2, 3], [3, 1]]);
e := [[1, 2], [2, 3], [3, 1]];
# Anchors correspond to the top, bottom left and bottom right node of the
# current graph.
anchor1 := 1;
anchor2 := 2;
anchor3 := 3;
prevNrVert := 1;
exp := 1;
# Starting from the triangle graph G := C_3, iteratively triplicate G, and
# connect each copy using their anchors.
for i in [2 .. n] do
prevNrVert := prevNrVert * 3;
DigraphAddEdges(D, Concatenation(DigraphEdges(D) + prevNrVert,
DigraphEdges(D) + (2 * prevNrVert)));
DigraphAddEdge(D, prevNrVert / 2 + (1 / 2), prevNrVert + 1);
DigraphAddEdge(D, prevNrVert, 2 * prevNrVert + 1);
DigraphAddEdge(D, 2 * prevNrVert, prevNrVert * 3 - exp);
exp := exp * 2;
Append(e, Concatenation(e + prevNrVert, e + (2 * prevNrVert)));
Add(e, [anchor2, anchor1 + prevNrVert]);
Add(e, [anchor3, anchor1 + (2 * prevNrVert)]);
Add(e, [anchor3 + prevNrVert, anchor2 + (2 * prevNrVert)]);
anchor2 := anchor2 + prevNrVert;
anchor3 := anchor3 + (2 * prevNrVert);
od;
DigraphAddEdges(D, e);

return DigraphSymmetricClosure(D);
end);
Expand Down
8 changes: 7 additions & 1 deletion tst/standard/examples.tst
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,15 @@ gap> D := HanoiGraph(1);
<immutable Hamiltonian connected symmetric digraph with 3 vertices, 6 edges>
gap> IsIsomorphicDigraph(D, CycleGraph(3));
true
gap> HanoiGraph(4);
gap> gr := HanoiGraph(4);
<immutable Hamiltonian connected symmetric digraph with 81 vertices, 240 edges\
>
gap> IsPlanarDigraph(gr);
true
gap> IsHamiltonianDigraph(gr);
true
gap> IsPlanarDigraph(DigraphMutableCopy(gr));
true
gap> HanoiGraph(0);
Error, no method found! For debugging hints type ?Recovery from NoMethodFound
Error, no 1st choice method found for `HanoiGraph' on 1 arguments
Expand Down
Loading