Skip to content

Commit

Permalink
fix edge placement in HanoiGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed Sep 4, 2024
1 parent 1a59c6e commit 7748aa8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
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, itteratively 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

0 comments on commit 7748aa8

Please sign in to comment.