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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

notice that after we perform an array replacement, our new array allows us to add<br />

n new elements to the array list before the array must be replaced aga<strong>in</strong>. This<br />

simple fact allows us to show that perform<strong>in</strong>g a series of operations on an <strong>in</strong>itially<br />

empty array list is actually quite efficient. As a shorth<strong>and</strong> notation, let us refer to<br />

the <strong>in</strong>sertion of an element to be the last element <strong>in</strong> an array list as a push<br />

operation. (See Figure 6.3.)<br />

Figure 6.3: Runn<strong>in</strong>g times of a series of push<br />

operations on a java.util.ArrayList of <strong>in</strong>itial<br />

size 1.<br />

Us<strong>in</strong>g an algorithmic design pattern called amortization, we can show that<br />

perform<strong>in</strong>g a sequence of such push operations on an array list implemented with<br />

an extendable array is actually quite efficient. To perform an amortized analysis,<br />

we use an account<strong>in</strong>g technique where we view the computer as a co<strong>in</strong>-operated<br />

appliance that requires the payment of one cyber-dollar for a constant amount of<br />

comput<strong>in</strong>g time. When an operation is executed, we should have enough cyberdollars<br />

available <strong>in</strong> our current "bank account" to pay for that operation's runn<strong>in</strong>g<br />

time. Thus, the total amount of cyber-dollars spent for any computation will be<br />

proportional to the total time spent on that computation. The beauty of us<strong>in</strong>g this<br />

analysis method is that we can overcharge some operations <strong>in</strong> order to save up<br />

cyber-dollars to pay for others.<br />

324

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

Saved successfully!

Ooh no, something went wrong!