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.

cover a big triangle of area n 2 /2 (base n <strong>and</strong> height n)<br />

plus n small triangles of area 1/2 each (base 1 <strong>and</strong><br />

height 1). In (b), which applies only when n is even, the<br />

rectangles are shown to cover a big rectangle of base<br />

n/2 <strong>and</strong> height n+ 1.<br />

The lesson to be learned from Proposition 4.3 is that if we perform an algorithm<br />

with nested loops such that the operations <strong>in</strong> the <strong>in</strong>ner loop <strong>in</strong>crease by one each<br />

time, then the total number of operations is quadratic <strong>in</strong> the number of times, n,<br />

we perform the outer loop. In particular, the number of operations is n 2 /2 + n/2, <strong>in</strong><br />

this case, which is a little more than a constant factor (1/2) times the quadratic<br />

function n 2 . In other words, such an algorithm is only slightly better than an<br />

algorithm that uses n operations each time the <strong>in</strong>ner loop is performed. This<br />

observation might at first seem non<strong>in</strong>tuitive, but it is nevertheless true, as shown<br />

<strong>in</strong> Figure 4.1.<br />

4.1.6 The Cubic Function <strong>and</strong> Other<br />

Polynomials<br />

Cont<strong>in</strong>u<strong>in</strong>g our discussion of functions that are powers of the <strong>in</strong>put, we consider the<br />

cubic function,<br />

f(n) = n 3 ,<br />

218

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

Saved successfully!

Ooh no, something went wrong!