I ran into a question on a midterm exam. Can anyone clarify the answer?
Problem A: Given a Complete Weighted Graph G, find a Hamiltonian Tour with minimum weight.
Problem B: Given a Complete Weighted Graph G and Real Number R, does G have a Hamiltonian Tour with weight at most R?
Suppose there is a machine that solves B. How many times can we call B (each time G and Real number R are given),to solve problem A with that machine? Suppose the sum of Edges in G up to M.
1) We cannot do this, because there is uncountable state.
2) O(|E|) times
3) O(lg m) times
4) because A is NP-Hard, This is cannot be done.
First algorithm
The answer is
(3) O(lg m) times
. You just have to perform a binary search for the minimum hamiltonian tour in the weighted graph. Notice that if there is a hamiltonian tour of lengthL
in the graph, there is no point in checking if a hamiltonian tour of lengthL'
, whereL' > L
, exists, since you are interested in the minimum-weight hamiltonian tour. So, in each step of your algorithm you can eliminate half of the remaining possible tour-weights. Consequently, you will have to callB
in your machineO(lg m)
times, wherem
stands for the total weight of all edges in the complete graph.Edit:
Second algorithm
I have a slight modification of the above algorithm, which uses the machine
O(|E|)
times, since some people said that we cannot apply binary search in an uncountable set of possible values (and they are possibly right): Take every possible subset of edges from the graph, and for each subset store a value that is the sum of weights of all edges from the subset. Lets store the values for all the subsets in an array calledVal
. The size of this array is2^|E|
. SortVal
in increasing order, and then apply binary search for the minimum hamiltonian path, but this time call the machine that solves problem B only with values from theVal
array. Since every subset of edges is included in the sorted array, it is guaranteed that the solution will be found. The total number of calls of the machine isO(lg(2^|E|))
, which isO(|E|)
. So, the correct choice is(2) O(|E|) times
.Note:
The first algorithm I proposed is probably incorrect, as some people noted that you cannot apply binary search in an uncountable set. Since we are talking about real numbers, we cannot apply binary search in the range
[0-M]
I believe that choice that was meant to be the answer is 1- you can't do that. The reason is that you can only do binary search on countable sets.
Note that the edges of the graph may even have negative weights, and besides, they may have fractional, or even irrational weights. In that case, the search space for the answer will be the set of all real values less than m.
However,you may get arbitrarily close to the answer of A in Log(n) time, but you cannot find the exact answer. (n being the size of the countable space).
Supposing that in the encoding of graphs the weights are encoded as binary strings representing nonnegative integers and that
Problem B
can actually algorithmically be solved by entering a real number and perform calculations based on that, things are apparently as follows.It is possible to do first binary search over the integral interval
{0,...,M}
to obtain the minumum weight of a Hamiltonian tour inO(log M)
calls to the algorithm forProblem B
. As afterwards the optimum is known, we can eliminate single edges inG
and use the resulting graph as an input to the algorithm forProblem B
to test whether or not the optimum changes. This process usesO(|E|)
calls to the algorithm forProblem B
to identify edges which occur in an optimal Hamiltonian tour. The overall running time of this approach isO( (|E| + log M ) * r(G))
, wherer(G)
denotes the running time of the algorithm forProblem B
taking a graphG
as an input. I suppose thatr
is a polynomial, although the question does not explicitly state this; in total, the overall running time would be polynomially bounded in the encoding length of the input, asM
can be computed in polynomial time (and hence is pseudopolynomially bounded in the encoding length of the inputG
).That being said, the supposed answers can be commented as follows.
Problem A
does not rule out a polynomial time algorithm; furthermore, the algorithm forProblem B
is not stated to be polynomial, so evenP=NP
does not follow ifProblem A
can be solved by a polynomial number of calls to the algorithm forProblem B
(which is the case by the algorithm sketched above).