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 O(N) 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.
Note that an edge is removed only if the resulting graph will still be
Hamiltonian. It follows that the edges that remain comprise a single
Hamiltonian cycle. The number of edges that will be examined will be
O(N2), so HamD will be called only O(N2) times. Each
instance of the problem called will be of size no larger than the size of G.
QED
Function DecideX(x) y <-- f(x); IF DecideY(f(x)) THEN return true ELSE return falseClearly DecideX would be a polynomial time algorithm if the call to DecideY could be executed at unit cost.