23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Proposition 6.2: Let S be an array list implemented by means of an<br />

extendable array with <strong>in</strong>itial length one. The total time to perform a series of n<br />

push operations <strong>in</strong> S, start<strong>in</strong>g from S be<strong>in</strong>g empty is O(n).<br />

Justification: Let us assume that one cyber-dollar is enough to pay for the<br />

execution of each push operation <strong>in</strong> S, exclud<strong>in</strong>g the time spent for grow<strong>in</strong>g the<br />

array. Also, let us assume that grow<strong>in</strong>g the array from size k to size 2k requires k<br />

cyber-dollars for the time spent copy<strong>in</strong>g the elements. We shall charge each push<br />

operation three cyber-dollars. Thus, we overcharge each push operation that does<br />

not cause an overflow by two cyber-dollars. Th<strong>in</strong>k of the two cyber-dollars<br />

profited <strong>in</strong> an <strong>in</strong>sertion that does not grow the array as be<strong>in</strong>g "stored" at the<br />

element <strong>in</strong>serted. An overflow occurs when the array list S has 2 i elements, for<br />

some <strong>in</strong>teger i ≥ 0, <strong>and</strong> the size of the array used by the array list represent<strong>in</strong>g S is<br />

2 i . Thus, doubl<strong>in</strong>g the size of the array will require 2 i cyber-dollars. Fortunately,<br />

these cyber-dollars can be found at the elements stored <strong>in</strong> cells 2 i−1 through 2 i − 1.<br />

(See Figure 6.4.) Note that the previous overflow occurred when the number of<br />

elements became larger than 2 i−1 for the first time, <strong>and</strong> thus the cyber-dollars<br />

stored <strong>in</strong> cells 2 i−1 through 2 i − 1 were not previously spent. Therefore, we have a<br />

valid amortization scheme <strong>in</strong> which each operation is charged three cyber-dollars<br />

<strong>and</strong> all the comput<strong>in</strong>g time is paid for. That is, we can pay for the execution of n<br />

push operations us<strong>in</strong>g 3n cyber-dollars. In other words, the amortized runn<strong>in</strong>g<br />

time of each push operation is O(1); hence, the total runn<strong>in</strong>g time of n push<br />

operations is O(n).<br />

Figure 6.4: Illustration of a series of push operations<br />

on an array list: (a) an 8-cell array is full, with two<br />

cyber-dollars "stored" at cells 4 through 7; (b) a push<br />

operation causes an overflow <strong>and</strong> a doubl<strong>in</strong>g of<br />

capacity. Copy<strong>in</strong>g the eight old elements to the new<br />

array is paid for by the cyber-dollars already stored <strong>in</strong><br />

the table. Insert<strong>in</strong>g the new element is paid for by one<br />

of the cyber-dollars charged to the push operation,<br />

<strong>and</strong> the two cyber-dollars profited are stored at cell 8.<br />

325

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!