> ... there seems to be a fair amount of confusion
> about it on the web.
There's a lot of confusion on the web about this. And while it's true that this is largely a small technical point, this is an area where small technical details can matter. > It's true that TSP is not NP complete because it is
> not in NP, because it is an optimization problem, not
> a decision problem.
So we make it a decision problem by saying: "Is there a tour with cost less than K?" > ... if you have an algorithm that can solve any NP complete
> problem in polynomial time (ie. P == NP) then you can use that
> algorithm to solve TSP (actually any finite optimization problem)
> in polynomial time.
And, roughly speaking, you do that by binary searching on the cost of a solution. Set a lower limit of 0 and a random upper bound. Keep doubling that till you get a solution, then binary search.It's not a path that we're feeding to the decision problem. The decision problem is when we have a graph with costs on the edges, and when someone says "Is there a solution of cost less than K?" We're assuming we have a way to answer that, and using that to get a solution to the optimization problem.
We need to use that to find the best possible path. We start by finding the cost of the best possible path. Set k=1 and ask if there's a solution. If not, double K and ask again. Repeat until the answer is "Yes".
Now you know there is a solution with cost in the range [0,K]. Set L=0 and U=K. Now do a binary search. Set K = L + (U-L)/2 and ask if there is a solution of cost K. If not, set L=K, if so then set U=K. Lather, rinse, repeat, until U-L is sufficiently small (1 if using integer weights - more care required otherwise).
Now we know the optimum price. Remove an edge and see if there's still a solution. If there is, there's an optimal path without it. If not, the edge is in the optimal path. Repeat for all edges - some efficiencies can be made.
Think of it like this:
the decision version question is: Is there a path of length k that hits all of the cities?
The algorithm just has to answer yes/no, it doesn't have to give you the path itself. So you can use binary search to repeatedly ask whether there is a path with length less than k, and home in on the optimal length. You still don't know what the path is.
We assume it would be difficult for the algorithm to make this determination without having actually discovered the path, but the problem doesn't require that it output any such path.
Edit: This is wrong, ColinWright details how to get the actual path from just knowing the length
Each call to D should give you 1 bit of the solution and with n bits you can represent numbers from 0 to O(2^n).
(a1, a2) * (b1, b2) = (a1 * b1 + 2 * a2 * b2, a1 * b2 + a2 * b1) (#)
A formula for inversion can easily be derived from (#).
TLDR: there are different kinds of encoding than binary fractions.
It's also true that TSP is NP hard. The simplest way to see this is that if you have solved a TSP you have also automatically solved the Hamiltonian Path problem for the corresponding graph and the Hamiltonian Path problem is known to be NP complete.
What the op does not state and which is also very important to realize however is that TSP is also NP easy (and hence NP equivalent). In other words if you have an algorithm that can solve any NP complete problem in polynomial time (ie. P == NP) then you can use that algorithm to solve TSP (actually any finite optimization problem) in polynomial time. There is a sketch of a proof of this in one of my previous HN comments, but I'm pretty sure this is well known to complexity theorists, though there seems to be a fair amount of confusion about it on the web.