Hier ist ein bekanntes Problem.
Bei gegebenem Array positiver Ganzzahlen die kleinste positive Ganzzahl aus, die nicht im Array enthalten ist.
Das Problem kann in Raum und Zeit gelöst werden: Lesen Sie das Array, verfolgen Sie in Raum, ob aufgetreten sind, suchen Sie nach dem kleinsten Element.
Mir ist aufgefallen, dass man Raum gegen Zeit tauschen kann. Wenn Sie Nur Speicher, du kannst es inRunden machen und bekommst die Zeit. In einem speziellen Fall gibt es offensichtlich einen Algorithmus mit quadratischer Zeit und konstantem Raum.
Meine Frage ist:
Ist dies der optimale Kompromiss, dh ist ? Wie beweist man solche Grenzen überhaupt?
Nehmen Sie ein RAM-Modell mit beschränkter Arithmetik und wahlfreiem Zugriff auf Arrays in O (1) an.
Inspiration für dieses Problem: Zeit-Raum-Kompromiss für Palindrome im Ein-Band-Modell (siehe zum Beispiel hier ).
Antworten:
This can be done inO(nlogn) word operations and O(1) words of memory (respectively O(nlog2n) time and O(logn) memory in bit-level RAM model). Indeed, the solution will be based on the following observation.
Say there aren0 even and n1 odd numbers in range [1,n+1] (so n0≈n1 and n0+n1=n+1 ). Then there is b∈{0,1} such that there are at most nb−1 values with parity b in the input. Indeed, otherwise there are at least n0 even and at least n1 odd values in the input, meaning that there are at least n0+n1=n+1 values in the input, contradiction (there are only n of them). It means that we can continue searching for missing number only among the odd or even numbers only. The same algorithm can be applied to higher bits of binary notation as well.
So our algorithm will look like that:
Suppose that we already now that there are onlyx values in the input with remainder modulo 2b being equal to r∈[0,2b) but there are at least x+1 numbers in range [1,n+1] that have remainder r modulo 2b (at the start we know
that for sure for b=0,r=0 ).
Say there arex0 values in the input with remainder r modulo 2b+1 and x1 values in the input with remainder r+2b modulo 2b+1 (we can find these numbers in a single pass through the input). Clearly, x0+x1=x . Moreover, because there are at least x+1 numbers in the input with remainder r modulo 2b , at least one of the pairs (r,b+1),(r+2b,b+1) satisfies the requirements of the step 1 .
We have found the missing number when2b⩾n+1 : there is only one number in range [1,n+1] that may have remainder r modulo 2b (r itself if it is in range), so there are at most zero values in the input that have such remainder. So r is indeed missing.
Clearly, the algorithm halts inO(logn) steps, each of them needs O(n) time (single pass over the input array). Moreover, only O(1) words of memory are required.
quelle
If I understand your definitions, this can be done in linear time with constant space. This is obviously the lowest bound, because we need to at least read the entire input.
The answer given in this question satisfies.
It's impossible to run this with less time or space, and adding extra time or space is useless, so there's no space-time tradeoff here. (Observe thatn=O(n/k) , so the tradeoff you observed doesn't hold asymptotically, in any case.)
In terms of your general question, I don't know of any nice theorems offhand which will help you prove space-time tradeoffs. This question seems to indicate that there isn't a (known) easy answer. Basically:
is unknown, and a strong answer would solve a lot of open problems (most notably about SC), implying that no easy solution exists.
EDIT: Ok, with repetition (but I'm still assuming that with an input of sizen the maximum possible number is n+1 ).
Observe that our algorithm needs to be able to differentiate between at leastn possible answers. Suppose at each pass through the data we can get at most k pieces of data. Then we will need n/k passes to differentiate all answers. Assuming k=n/s then we run in nn/sn=sn time. So I think this proves what you want.
The difficulty is in showing that each time through we get onlyk bits. If you assume that our only legal operation is =, then we're good. However, if you allow more complex operations, then you'll be able to get more information.
quelle