问题
Given an unsorted array of size n, it's obvious that finding whether an element exists in the array takes O(n) time.
If we let m = log n then it takes O(2^m) time.
Notice that if the array is sorted, a binary search actually takes O(m) time (which is polynomial) but the binary search cannot apply to an unsorted array.
Is it possible to prove that the problem to find an element in an array (yes or no) is NP complete in terms of m. What problem should I reduce from and how to reduce?
Any idea would be appreciated.
EDIT:
My description above probably did not express clearly what I was trying to say.
Let's reword the problem in the following way.
We have an oracle, which is a binary tree of height h with each node having random values. I.E. a tree that DOES NOT have the property that all values in the left subtree of a node must be smaller than the value in the node or all values in the right subtree of a node must be greater than the value in the node. However all nodes in the oracle tree are guaranteed to have value between 0 and 2^h-1.
The input is a number to be searched. The input is guaranteed to have value between 0 and 2^h-1. (The input has h bits)
(Let's say we are searching through the same array every time and hence we have the same oracle every time so the tree is not a part of input.)
- The output is YES or NO, indicating whether the input is in a node of the tree or not.
Question: whether this problem is NP complete or not in terms of h. This problem is NP because if a path to the YES node in the tree is given it can be verified in O(h) time.
(Note that if the oracle tree has the property that left subtree of a node is less than the node and right subtree of a node is greater than the node then the problem is NOT NP complete because binary search can be applied.)
回答1:
Finding an element in an array is NOT NP-complete as it can be done in linear time. (Assuming P ≠ NP)
In fact, the naive brute-force search algorithm you mentioned in your question is a linear time algorithm!
When we are talking about the complexity of a computational problem, we always measure the time with respect to the size of the input. You claimed the input size of our algorithm is m = log(n)
, but in our case, the size of our input is determined by the number of elements in the array, which is n
.
For your reference, testing whether a given number n
is a prime number is an example computational problem that takes input of size log(n)
. The input of the problem is n
, and it is of size log(n)
because we need to use log(n)
bits to represent n
in binary form.
Update
Deterministic search algorithm requires Ω(n) time for unsorted array.
Any search algorithm must read through the entire input (i.e. the n entries of the array). We are going to prove this by contradiction.
Suppose there is a search algorithm that does not read all n input entries, then there is an entry that is not read by this algorithm. you can then construct a case that the search item is at the entry that is not read by this hypothetical algorithm, this violates the correctness of the algorithm. Hence such algorithm does not exist.
来源:https://stackoverflow.com/questions/28847966/array-search-np-complete