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.

Count<strong>in</strong>g Primitive Operations<br />

Specifically, a primitive operation corresponds to a low-level <strong>in</strong>struction with an<br />

execution time that is constant. Instead of try<strong>in</strong>g to determ<strong>in</strong>e the specific<br />

execution time of each primitive operation, we will simply count how many<br />

primitive operations are executed, <strong>and</strong> use this number t as a measure of the<br />

runn<strong>in</strong>g-time of the algorithm.<br />

This operation count will correlate to an actual runn<strong>in</strong>g time <strong>in</strong> a specific<br />

computer, for each primitive operation corresponds to a constant-time <strong>in</strong>struction,<br />

<strong>and</strong> there are only a fixed number of primitive operations. The implicit<br />

assumption <strong>in</strong> this approach is that the runn<strong>in</strong>g times of different primitive<br />

operations will be fairly similar. Thus, the number, t, of primitive operations an<br />

algorithm performs will be proportional to the actual runn<strong>in</strong>g time of that<br />

algorithm.<br />

An algorithm may run faster on some <strong>in</strong>puts than it does on others of the same<br />

size. Thus, we may wish to express the runn<strong>in</strong>g time of an algorithm as the<br />

function of the <strong>in</strong>put size obta<strong>in</strong>ed by tak<strong>in</strong>g the average over all possible <strong>in</strong>puts<br />

of the same size. Unfortunately, such an average-case analysis is typically quite<br />

challeng<strong>in</strong>g. It requires us to def<strong>in</strong>e a probability distribution on the set of <strong>in</strong>puts,<br />

which is often a difficult task. Figure 4.4 schematically shows how, depend<strong>in</strong>g on<br />

the <strong>in</strong>put distribution, the runn<strong>in</strong>g time of an algorithm can be anywhere between<br />

the worst-case time <strong>and</strong> the best-case time. For example, what if <strong>in</strong>puts are really<br />

only of types "A" or "D"?<br />

Figure 4.4: The difference between best-case <strong>and</strong><br />

worst-case time. Each bar represents the runn<strong>in</strong>g time<br />

of some algorithm on a different possible <strong>in</strong>put.<br />

227

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

Saved successfully!

Ooh no, something went wrong!