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.

<strong>in</strong>to a bowl of water to see if they both displaced the same amount. This discovery<br />

was unfortunate for the goldsmith, however, for when Archimedes did his analysis,<br />

the crown displaced more water than an equal-weight lump of pure gold, <strong>in</strong>dicat<strong>in</strong>g<br />

that the crown was not, <strong>in</strong> fact, pure gold.<br />

In this book, we are <strong>in</strong>terested <strong>in</strong> the design of "good" data structures <strong>and</strong> algorithms.<br />

Simply put, a data structure is a systematic way of organiz<strong>in</strong>g <strong>and</strong> access<strong>in</strong>g data,<br />

<strong>and</strong> an algorithm is a step-by-step procedure for perform<strong>in</strong>g some task <strong>in</strong> a f<strong>in</strong>ite<br />

amount of time. These concepts are central to comput<strong>in</strong>g, but to be able to classify<br />

some data structures <strong>and</strong> algorithms as "good," we must have precise ways of<br />

analyz<strong>in</strong>g them.<br />

The primary analysis tool we will use <strong>in</strong> this book <strong>in</strong>volves characteriz<strong>in</strong>g the runn<strong>in</strong>g<br />

times of algorithms <strong>and</strong> data structure operations, with space usage also be<strong>in</strong>g of<br />

<strong>in</strong>terest. Runn<strong>in</strong>g time is a natural measure of "goodness," s<strong>in</strong>ce time is a precious<br />

resource—computer solutions should run as fast as possible.<br />

In general, the runn<strong>in</strong>g time of an algorithm or data structure method <strong>in</strong>creases with<br />

the <strong>in</strong>put size, although it may also vary for different <strong>in</strong>puts of the same size. Also,<br />

the runn<strong>in</strong>g time is affected by the hardware environment (as reflected <strong>in</strong> the<br />

processor, clock rate, memory, disk, etc.) <strong>and</strong> software environment (as reflected <strong>in</strong><br />

the operat<strong>in</strong>g system, programm<strong>in</strong>g language, compiler, <strong>in</strong>terpreter, etc.) <strong>in</strong> which the<br />

algorithm is implemented, compiled, <strong>and</strong> executed. All other factors be<strong>in</strong>g equal, the<br />

runn<strong>in</strong>g time of the same algorithm on the same <strong>in</strong>put data will be smaller if the<br />

computer has, say, a much faster processor or if the implementation is done <strong>in</strong> a<br />

program compiled <strong>in</strong>to native mach<strong>in</strong>e code <strong>in</strong>stead of an <strong>in</strong>terpreted implementation<br />

run on a virtual mach<strong>in</strong>e. Nevertheless, <strong>in</strong> spite of the possible variations that come<br />

from different environmental factors, we would like to focus on the relationship<br />

between the runn<strong>in</strong>g time of an algorithm <strong>and</strong> the size of its <strong>in</strong>put.<br />

We are <strong>in</strong>terested <strong>in</strong> characteriz<strong>in</strong>g an algorithm's runn<strong>in</strong>g time as a function of the<br />

<strong>in</strong>put size. But what is the proper way of measur<strong>in</strong>g it?<br />

4.2.1 Experimental Studies<br />

if an algorithm has been implemented, we can study its runn<strong>in</strong>g time by execut<strong>in</strong>g it<br />

on various test <strong>in</strong>puts <strong>and</strong> record<strong>in</strong>g the actual time spent <strong>in</strong> each execution.<br />

Fortunately, such measurements can be taken <strong>in</strong> an accurate manner by us<strong>in</strong>g<br />

system calls that are built <strong>in</strong>to the language or operat<strong>in</strong>g system (for example, by<br />

us<strong>in</strong>g the System.current Time Millis () method or call<strong>in</strong>g the run-time<br />

environment with profil<strong>in</strong>g enabled). Such tests assign a specific runn<strong>in</strong>g time to a<br />

specific <strong>in</strong>put size, but we are <strong>in</strong>terested <strong>in</strong> determ<strong>in</strong><strong>in</strong>g the general dependence of<br />

runn<strong>in</strong>g time on the size of the <strong>in</strong>put. In order to determ<strong>in</strong>e this dependence, we<br />

should perform several experiments on many different test <strong>in</strong>puts of various sizes.<br />

Then we can visualize the results of such experiments by plott<strong>in</strong>g the performance<br />

of each run of the algorithm as a po<strong>in</strong>t with x-coord<strong>in</strong>ate equal to the <strong>in</strong>put size, n,<br />

224

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

Saved successfully!

Ooh no, something went wrong!