function HamD(G : graph)
s <-- Ham(G) ;
IF s defines a Hamiltonian
cycle in G
THEN return true
ELSE return false
Note that the algorithm gives the correct result, and that it
is simple to make it run in polynomial time if Ham is assumed
to be of unit cost. Part of what makes this feasible is that
it is easy to check "s" in linear time to see if it is a
Hamiltonian path.
function Ham(G=(N,A): graph)
/* N is the node set, and A is the edge set. */
IF HamD(G)=false
THEN return "there is no solution"
FOR each edge e in the edge set A of G
DO IF HamD(N,A-{e})
THEN A <-- A-{e}
s <-- sequence of nodes obtained by following
the unique cycle remaining in G.
Function DecideX(x) y <-- f(x); IF DecideY(f(x)) THEN return true ELSE return false