-
Notifications
You must be signed in to change notification settings - Fork 61
Hamiltonian path finding function and associated tests #889
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
base: main
Are you sure you want to change the base?
Changes from all commits
f0858b5
86500e9
8ef2dca
0e8e707
4d7f3c0
a344dfe
248f7bd
e739e8a
b13d603
023ceba
8b9a7fc
24dbb9c
bab6eb1
2196bc7
431e451
542e616
d4e93b2
38862a3
1ce3599
57c3df3
684c2e5
4646fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove, unless required for something, but I can't see how it could be. |
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Magic number, comment on reason for choice. |
||
| 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; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1586,6 +1586,40 @@ function(D, edges) | |||||
| return true; | ||||||
| end); | ||||||
|
|
||||||
| InstallMethod(TestHamiltonianPath, "for a digraph", [IsDigraph], | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be more consistent with elsewhere in the Digraphs package if it was:
Suggested change
i.e. returns |
||||||
| function(D) | ||||||
| local P, edges, i; | ||||||
|
|
||||||
| P := HamiltonianPath(D); | ||||||
|
|
||||||
| if P = fail then | ||||||
| Print("HamiltonianPath returned fail.\n"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be an info statement |
||||||
| 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"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above should be an info statement |
||||||
| 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), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And again |
||||||
| " but graph has ", DigraphNrVertices(D), " vertices.\n"); | ||||||
| return false; | ||||||
| fi; | ||||||
|
|
||||||
| Print("SUCCESS: Path is a valid Hamiltonian path.\n"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this should probably be removed. |
||||||
| return true; | ||||||
| end); | ||||||
|
|
||||||
| ############################################################################# | ||||||
| # 9. Connectivity | ||||||
| ############################################################################# | ||||||
|
|
||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
| 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 | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
| 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 | ||
| } | ||
| } |
| 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 | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
| 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 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was committed by accident and should removed.