In Problem 33.1-3, we saw that we can perform a sorting of the polar angles of the input sequence S of n points about an arbitrary point O in O(nlogn) time. Call O the origin of this polar sorting. Suppose A, B, C are collinear, with B between A and C. If you perform a sorting of the polar angles using A as origin, it is easy to detect this collinearity. Hence, it we perform n polar sortings, using each of the points of S as origin, we can detect if there are any collinerity of three points.
(b) If S=(p1\dd pn) is already sorted by the x-coordinates, then the convex hull of S can be broken up into two convex chains, both running from p1 to pn. One chain is convex upwards, and the other is convex downwards. Call them the upper and lower chains, respectively. For each i=2\dd n−1, we call pi a candidate for the upper chain if pi lies above the line through p1,pn. Otherwise it is a candidate for the lower chain. We now just have to check that the subsequence of candidates for the upper chain is really convex upwards. Similarly for the lower chain. This checking takes O(n) time.
We want a polynomial time algorithm A, which given a graph G, with either say that G has no Hamiltonian cycle, or else list the vertices in a Hamiltonian cycle of G. Let B be the polynomial time algorithm that checks if an input graph belongs to HAM−CYCLE.
We may assume that G does have a Hamiltonian cycle (why?). Pick an arbitrary starting node v1. Inductively, assume that we have found a sequence (v1,v2,...,vi) of vertices that can be completed into a Hamiltonian cycle. Pick a new vertex u. We want to know whether we can make u into vi+1. To do this, we construct a graph G' in which all the vertices v1,...,vi and u have been replaced by a new vertex u* (all other vertices remain intact). All edges involving the old vertices are also intact. We introduce a new edge (u*,v) whenever there is an old edge (u,v), for all v that remain. We also introduce a new edge (v,u*) whenever there is an old edge (v,v1) for all v that remain. It is easy to see that:
There is a Hamiltonian cycle in G′ that extends (v1,...,vi,u*) iff there is a Hamiltonian cycle in G that extends (v1,...,vi,u).
If B rejects G′, then we must try another choice of u. Eventually we succeed. Thus we can find a candidate for ui+1 with at most n calls to B. Thus in n2 calls to B, we can find a Hamiltonian cycle for G. Clearly this is in polynomial time.