Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f0858b5
Added section to ViewString for edge weights
ap4108735 Oct 17, 2025
86500e9
Fixed a quick typo in the edit I made
ap4108735 Oct 20, 2025
8ef2dca
Added ViewString edits to testing
ap4108735 Oct 29, 2025
0e8e707
Updated edge-weighted section in testinstall
ap4108735 Oct 29, 2025
4d7f3c0
Some fixes on empty digraphs
ap4108735 Oct 29, 2025
a344dfe
Further updates to testing on weights.xml
ap4108735 Oct 29, 2025
248f7bd
Change of code location to avoid nesting issues
ap4108735 Oct 29, 2025
e739e8a
Some more fixes for the empty case
ap4108735 Oct 29, 2025
b13d603
Changing order that attributes appear in testing
ap4108735 Nov 3, 2025
023ceba
Merge branch 'digraphs:main' into main
ap4108735 Nov 3, 2025
8b9a7fc
Fixed trailing whitespace issue
ap4108735 Nov 3, 2025
24dbb9c
Renamed HamiltonPath to HamiltonCycle
ap4108735 Nov 5, 2025
bab6eb1
Merge branch 'digraphs:main' into main
ap4108735 Nov 5, 2025
2196bc7
Fixed some method issues
ap4108735 Nov 5, 2025
431e451
Merge remote-tracking branch 'origin/main'
ap4108735 Nov 5, 2025
542e616
Introduced HamiltonianPath and HamiltonianCycle
ap4108735 Nov 5, 2025
d4e93b2
Fixes to IsHamiltonianDigraph
ap4108735 Nov 5, 2025
38862a3
Tests HamiltonianCycle instead of HamiltonianPath
ap4108735 Nov 7, 2025
1ce3599
Merge branch 'digraphs:main' into main
ap4108735 Nov 7, 2025
57c3df3
Merge branch 'digraphs:main' into main
ap4108735 Nov 19, 2025
684c2e5
Fixes to big cases, syntax and new testing code
ap4108735 Dec 2, 2025
4646fb3
Fixes to extreme/attr.tst
ap4108735 Dec 3, 2025
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
7,111 changes: 7,111 additions & 0 deletions configure~

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions digraph.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//dot
digraph hgn{
node [shape=circle]
1
2
3
1 -> 2
1 -> 3
2 -> 1
2 -> 3
3 -> 1
}
10 changes: 5 additions & 5 deletions doc/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1599,23 +1599,23 @@ gap> FacialWalks(D3, rot);
<Example><![CDATA[
gap> D := Digraph([[]]);
<immutable empty digraph with 1 vertex>
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
[ 1 ]
gap> D := Digraph([[2], [1]]);
<immutable digraph with 2 vertices, 2 edges>
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
[ 1, 2 ]
gap> D := Digraph([[3], [], [2]]);
<immutable digraph with 3 vertices, 2 edges>
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
fail
gap> D := Digraph([[2], [3], [1]]);
<immutable digraph with 3 vertices, 3 edges>
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
[ 1, 2, 3 ]
gap> D := GeneralisedPetersenGraph(IsMutableDigraph, 5, 2);
<mutable digraph with 10 vertices, 30 edges>
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
fail
]]></Example>
</Description>
Expand Down
2 changes: 1 addition & 1 deletion doc/examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ gap> D := CirculantGraph(6, [2, 3]);
with 6 vertices, 18 edges>
gap> AutomorphismGroup(D) = DihedralGroup(IsPermGroup, 12);
true
gap> HamiltonianPath(D);
gap> HamiltonianCycle(D);
[ 1, 3, 5, 2, 6, 4 ]
gap> IsCompleteDigraph(CirculantGraph(6, [1, 2, 3]));
true]]></Example>
Expand Down
1 change: 1 addition & 0 deletions gap/attr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ DeclareOperation("DigraphAllChordlessCyclesOfMaximalLength",
DeclareAttribute("DigraphAllChordlessCycles", IsDigraph);
DeclareOperation("FacialWalks", [IsDigraph, IsList]);
DeclareAttribute("HamiltonianPath", IsDigraph);
DeclareAttribute("HamiltonianCycle", IsDigraph);
DeclareAttribute("DigraphPeriod", IsDigraph);
DeclareAttribute("DigraphLoops", IsDigraph);

Expand Down
48 changes: 48 additions & 0 deletions gap/attr.gi
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,54 @@ function(D)
end);

InstallMethod(HamiltonianPath, "for a digraph", [IsDigraph],
function(D)
local iter, recpath, vertices, n, v, start, finish;

v := DigraphNrVertices(D);

# trivial cases
if v <= 1 then
return DigraphVertices(D);
elif v < 256 then
recpath := DigraphMonomorphism(ChainDigraph(v), D);
if recpath = fail then
return fail;
fi;
return ImageListOfTransformation(recpath, v);
fi;

# For large graphs: search all simple paths from every start to every end
n := v; # Hamiltonian path must have exactly v vertices

for start in [1..v] do
for finish in [1..v] do

iter := IteratorOfPaths(D, start, finish);

while not IsDoneIterator(iter) do
recpath := NextIterator(iter);

# safety: ensure this is a path record with vertices
if not IsRecord(recpath) or not IsBound(recpath.vertices) then
continue;
fi;

vertices := recpath.vertices;

# Hamiltonian path condition
if Length(vertices) = n and IsDuplicateFreeList(vertices) then
return vertices;
fi;

od;

od;
od;

return fail;
end);

InstallMethod(HamiltonianCycle, "for a digraph", [IsDigraph],
function(D)
local path, iter, n;

Expand Down
1 change: 1 addition & 0 deletions gap/oper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ DeclareOperation("IsPerfectMatching", [IsDigraph, IsHomogeneousList]);
DeclareOperation("IsDigraphPath",
[IsDigraph, IsHomogeneousList, IsHomogeneousList]);
DeclareOperation("IsDigraphPath", [IsDigraph, IsList]);
DeclareOperation("TestHamiltonianPath", [IsDigraph]);

# 9. Connectivity . . .
DeclareOperation("DigraphIsKing", [IsDigraph, IsPosInt, IsPosInt]);
Expand Down
34 changes: 34 additions & 0 deletions gap/oper.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,40 @@ function(D, edges)
return true;
end);

InstallMethod(TestHamiltonianPath, "for a digraph", [IsDigraph],
function(D)
local P, edges, i;

P := HamiltonianPath(D);

if P = fail then
Print("HamiltonianPath returned fail.\n");
return false;
fi;

# convert vertex list to edge list
edges := [];
for i in [1 .. Length(P)-1] do
Add(edges, [ P[i], P[i+1] ]);
od;

# 2. Check if path has no repeated vertices
if not IsDuplicateFreeList(P) then
Print("FAIL: Path repeats vertices.\n");
return false;
fi;

# 3. Check if path length equals number of vertices
if Length(P) <> DigraphNrVertices(D) then
Print("FAIL: Path length is ", Length(P),
" but graph has ", DigraphNrVertices(D), " vertices.\n");
return false;
fi;

Print("SUCCESS: Path is a valid Hamiltonian path.\n");
return true;
end);

#############################################################################
# 9. Connectivity
#############################################################################
Expand Down
4 changes: 2 additions & 2 deletions gap/prop.gi
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ function(D)
return true;
fi;
fi;
return HamiltonianPath(D) <> fail;
return HamiltonianCycle(D) <> fail;
end);

InstallMethod(IsHamiltonianDigraph, "for a digraph with hamiltonian path",
[IsDigraph and HasHamiltonianPath], x -> HamiltonianPath(x) <> fail);
[IsDigraph and HasHamiltonianPath], x -> HamiltonianCycle(x) <> fail);

InstallMethod(IsDigraphCore, "for a digraph",
[IsDigraph],
Expand Down
20 changes: 20 additions & 0 deletions k4.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//dot
digraph hgn{
node [shape=circle]
1
2
3
4
1 -> 2
1 -> 3
1 -> 4
2 -> 1
2 -> 3
2 -> 4
3 -> 1
3 -> 2
3 -> 4
4 -> 1
4 -> 2
4 -> 3
}
23 changes: 23 additions & 0 deletions my_digraph.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//dot
digraph hgn{
subgraph lowverts{
node [shape=circle, color=black]
edge [color=black]
3
}
subgraph highverts{
node [shape=circle, color=red]
edge [color=red]
1
2
}
subgraph lowverts{
3 -> 1
3 -> 3
}
subgraph highverts{
1 -> 2
1 -> 3 [color=black]
2 -> 2
}
}
11 changes: 11 additions & 0 deletions poset.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//dot
digraph hgn{
node [shape=circle]
1
2
3
4
1 -> 4
3 -> 4
3 -> 2
}
9 changes: 9 additions & 0 deletions preset.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//dot
digraph graphname {
node [shape=Mrecord, height=0.5, fixedsize=true]ranksep=1;
1 [label="4", width=0.5]
2 [label="1|2|5", width=1.5]
3 [label="3", width=0.5]
2 -> 1
3 -> 1
}
8 changes: 4 additions & 4 deletions tst/extreme/attr.tst
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ gap> circs := DigraphAllSimpleCircuits(gr);;
gap> Length(circs);
1291792

# HamiltonianPath and IsHamiltonianDigraph
# HamiltonianCycle and IsHamiltonianDigraph
gap> g := CompleteDigraph(20);
<immutable complete digraph with 20 vertices>
gap> HamiltonianPath(g);
gap> HamiltonianCycle(g);
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
gap> IsDigraphMonomorphism(CycleDigraph(20),
> g,
> Transformation(HamiltonianPath(g)));
> Transformation(HamiltonianCycle(g)));
true
gap> IsHamiltonianDigraph(g);
true
gap> g := CompleteMultipartiteDigraph([1, 9, 1, 1, 2, 1, 1, 1]);
<immutable complete multipartite digraph with 17 vertices, 198 edges>
gap> HamiltonianPath(g);
gap> HamiltonianCycle(g);
fail
gap> IsHamiltonianDigraph(g);
false
Expand Down
Loading
Loading