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.

• We will have difficulty compar<strong>in</strong>g the experimental runn<strong>in</strong>g times of two<br />

algorithms unless the experiments were performed <strong>in</strong> the same hardware <strong>and</strong><br />

software environments.<br />

• We have to fully implement <strong>and</strong> execute an algorithm <strong>in</strong> order to study its<br />

runn<strong>in</strong>g time experimentally.<br />

This last requirement is obvious, but it is probably the most time consum<strong>in</strong>g aspect<br />

of perform<strong>in</strong>g an experimental analysis of an algorithm. The other limitations<br />

impose serious hurdles too, of course. Thus, we would ideally like to have an<br />

analysis tool that allows us to avoid perform<strong>in</strong>g experiments.<br />

In the rest of this chapter, we develop a general way of analyz<strong>in</strong>g the runn<strong>in</strong>g times<br />

of algorithms that:<br />

• Takes <strong>in</strong>to account all possible <strong>in</strong>puts<br />

• Allows us to evaluate the relative efficiency of any two algorithms <strong>in</strong> a<br />

way that is <strong>in</strong>dependent from the hardware <strong>and</strong> software environment<br />

• Can be performed by study<strong>in</strong>g a high-level description of the algorithm<br />

without actually implement<strong>in</strong>g it or runn<strong>in</strong>g experiments on it.<br />

This methodology aims at associat<strong>in</strong>g, with each algorithm, a function f(n) that<br />

characterizes the runn<strong>in</strong>g time of the algorithm as a function of the <strong>in</strong>put size n.<br />

Typical functions that will be encountered <strong>in</strong>clude the seven functions mentioned<br />

earlier <strong>in</strong> this chapter.<br />

4.2.2 Primitive Operations<br />

As noted above, experimental analysis is valuable, but it has its limitations. If we<br />

wish to analyze a particular algorithm without perform<strong>in</strong>g experiments on its<br />

runn<strong>in</strong>g time, we can perform an analysis directly on the high-level pseudo-code<br />

<strong>in</strong>stead. We def<strong>in</strong>e a set of primitive operations such as the follow<strong>in</strong>g:<br />

• Assign<strong>in</strong>g a value to a variable<br />

• Call<strong>in</strong>g a method<br />

• Perform<strong>in</strong>g an arithmetic operation (for example, add<strong>in</strong>g two numbers)<br />

• Compar<strong>in</strong>g two numbers<br />

• Index<strong>in</strong>g <strong>in</strong>to an array<br />

• Follow<strong>in</strong>g an object reference<br />

• Return<strong>in</strong>g from a method.<br />

226

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

Saved successfully!

Ooh no, something went wrong!