Constant time to initialize using more pearl programming space - column 1

I read Pearl Programming, and I really got confused in one of the explanations for the solution - Problem 9 in column 1.

Question: when using raster data to represent a set of integers, the first phase initializes the set empty. But space initialization can take a lot of time. Show how to get around this problem by developing a method to initialize writing a vector to zero at first access.

The answer was: The effect of initializing vector data [0 ... n-1] can be performed using the signature contained in two additional n-element vectors, from and to, and the integer top. If the data item [i] was initialized, then from [i] top and [* from * [i]] = i. Thus, from the signature, as well as together and from above together, make sure that because of this, it is not accidentally written to random memory contents.

I have read this answer several times. I do not understand this.

Can someone explain this?

Thank,

+3
source share
2 answers

This page helped me: http://comments.gmane.org/gmane.comp.programming.algogeeks/30667

But let me see if I can explain this.

: 0s , . , 0s, ? , ?

Bentley "from" () "to" ( ( )) , , "top", - . , from[i] < top, .

: :

top = 0
data = int array of integers of size 1,000,000 
       (all random values since we did not initialize it)

1 (i = 1 ). , ? . "from" .

from[i] = top
to[top] = i
data[i] = 0 (I don't think it matters whether you set it to 0 or your intended value of 3)
top++ (top is now 1)

, , , to[from[i]] == i . , from[i] < top, .

:

A) (.. top = 0) from[i] < 0, . .

B) ( top > 0, , 1) from[i] < top = > from[i] = 0. , to[from[i]] = i.

top = 2... n

+4

All Articles