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.

Proposition 4.7: The AlgorithmarrayMax, for comput<strong>in</strong>g the maximum<br />

element <strong>in</strong> an array ofn <strong>in</strong>tegers, runs <strong>in</strong> O(n) time.<br />

Justification: The number of primitive operations executed by algorithm<br />

arrayMax <strong>in</strong> each iteration is a constant. Hence, s<strong>in</strong>ce each primitive operation<br />

runs <strong>in</strong> constant time, we can say that the runn<strong>in</strong>g time of algorithm arrayMax<br />

on an <strong>in</strong>put of size n is at most a constant times n, that is, we may conclude that<br />

the runn<strong>in</strong>g time of algorithm arrayMax is O(n).<br />

Some Properties of the Big-Oh Notation<br />

The big-Oh notation allows us to ignore constant factors <strong>and</strong> lower order terms<br />

<strong>and</strong> focus on the ma<strong>in</strong> components of a function that affect its growth.<br />

Example 4.8: 5n 4 + 3n 3 + 2n 2 + 4n + 1 is O(n 4 ).<br />

Justification: Note that 5n 4 + 3n 3 + 2n 2 + 4n+ 1 ≤ (5 + 3 + 2 + 4+ 1)n 4 =<br />

cn 4 , for c = 15, when n ≥ n 0 = 1.<br />

In fact, we can characterize the growth rate of any polynomial function.<br />

Proposition 4.9: If f(n) is a polynomial of degree d, that is,<br />

f(n) = a 0 + a 1 n+ … + a d n d ,<br />

<strong>and</strong> a d > 0, then f(n) is O(n d ).<br />

Justification: Note that, for n ≥ 1, we have 1 ≤ n ≤ n 2 ≤ … ≤ n d ; hence,<br />

a 0 + a 1 n + a 2 n 2 + … + a d n d ≤ (a 0 + a 1 + a 2 + … + a d )n d .<br />

Therefore, we can show f(n) is O(n d ) by def<strong>in</strong><strong>in</strong>g c = a 0 + a 1 +… + a d <strong>and</strong> n 0 = 1.<br />

Thus, the highest-degree term <strong>in</strong> a polynomial is the term that determ<strong>in</strong>es the<br />

asymptotic growth rate of that polynomial. We consider some additional<br />

properties of the big-Oh notation <strong>in</strong> the exercises. Let us consider some further<br />

examples here, however, focus<strong>in</strong>g on comb<strong>in</strong>ations of the seven fundamental<br />

functions used <strong>in</strong> algorithm design.<br />

Example 4.10: 5n 2 + 3nlog n+ 2n+ 5 is O(n 2 ).<br />

231

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

Saved successfully!

Ooh no, something went wrong!