tags:

views:

739

answers:

6

someone has an idea how can I do that ?

thanks

+8  A: 

This smells like a homework problem but I'll throw you a bone...

http://eli.thegreenplace.net/2008/08/23/initializing-an-array-in-constant-time/

I do not feel that this answers the question. This links gives a way of delaying the initialization of a dense array, not a way of having an initialized structure in constant time.
ddaa
Right, and as answered by others, if you take the question at face value, it is impossible to initialize n elements in constant time. That's why I suspect there is something else to the question...
+5  A: 

If you mean "initialize a dense array of N items in a time independent of N", then it is physically impossible. A dense array has a storage space that grows linearly with the number of items, and it will take a linear amount of time to initialize this space.

You can have constant-time initialization using a sparse array. That is essentially an associative array (or hashmap, or dictionary, or table, depending on the language) which initializes items when they are first accessed.

ddaa
+6  A: 

If you need to do something to each element of a set of n elements, you cannot do it with better than O(n) performance.

Robert Gamble
This is true if you take the question at face value. That's why I think the question is misleading (and why I suspect it is a homework question).
+1  A: 

I think its just a simple syntax question. In C++ you can do this:

int foo[1000] = {0};

All values in array are now 0

While it looks like its done in constant time, it still O(n)

Pyrolistical
That's *assuming* the compiler vectorizes the instructions.
Paul Nathan
Even if vectorized, the time is still O(n). Let's say it is able to set 4 elements at a time; the time then is proportional to n/4, which is O(n).
CesarB
That's true, I'll fix my answer
Pyrolistical
A: 

No. It's physically impossible. However, you can use vector instructions to make it some O(n * 1/k) time, where k is the width of a vector setter instruction. That's still O(n), but the constant is shrunk.

Paul Nathan
A: 

Actually, it is possible, but only with hardware help. On software, you will have to do a number of steps proportional to n, thus it is O(n); on hardware, however, you can wire things so that all elements of the array are set in parallel.

This is in fact a time/space tradeoff; while before one needed O(n) time, now one needs O(n) circuit elements but can do the operation in O(1) time.

And it is actually a common thing to do. A lot of hardware has a reset input which, when asserted, sets the whole hardware to a known state. This can involve for instance zeroing the whole memory.

CesarB
But the problem is you'll never have n circuits in the real world.It depends if the question is a theory question or not.
Pyrolistical
@Pyrolistical: when it is some sort of reset input for the memory, you already have n circuits in the real world. The same happens for the clock input. Of course, not all memory chips will have a reset input which actually clears the memory... And if you are programming for a FPGA, all bets are off.
CesarB
Big O notation denotes asymptotic complexity: when the variable (here, the size of the array) tends towards infinity. You cannot have infinity circuit elements, therefore you cannot have O(1) initialization. Matter solved.
ddaa
Well that could work, but that's now how computers work in the real world. If you have a large array, you'll be fetching parts of it from main memory and never be able to reset the entire array at once.
Pyrolistical
@ddaa: if you cannot have infinity circuit elements, you cannot have an array with infinity elements. The number of circuit elements is always at least proportional to the number of array elements, whether they have a reset input or not.
CesarB