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.

4.1.5 The Quadratic function<br />

Another function that appears quite often <strong>in</strong> algorithm analysis is the quadratic<br />

function,<br />

f(n) = n 2 .<br />

That is, given an <strong>in</strong>put value n, the function f assigns the product of n with itself (<strong>in</strong><br />

other words, "n squared").<br />

The ma<strong>in</strong> reason why the quadratic function appears <strong>in</strong> the analysis of algo rithms is<br />

that there are many algorithms that have nested loops, where the <strong>in</strong>ner loop<br />

performs a l<strong>in</strong>ear number of operations <strong>and</strong> the outer loop is performed a l<strong>in</strong>ear<br />

number of times. Thus, <strong>in</strong> such cases, the algorithm performs n · n = n 2 operations.<br />

Nested Loops <strong>and</strong> the Quadratic function<br />

The quadratic function can also arise <strong>in</strong> the context of nested loops where the first<br />

iteration of a loop uses one operation, the second uses two operations, the third<br />

uses three operations, <strong>and</strong> so on. That is, the number of operations is<br />

1+ 2 + 3 +… + (n − 2) + (n − 1) + n.<br />

In other words, this is the total number of operations that will be performed by the<br />

nested loop if the number of operations performed <strong>in</strong>side the loop <strong>in</strong>creases by<br />

one with each iteration of the outer loop. This quantity also has an <strong>in</strong>terest<strong>in</strong>g<br />

history.<br />

In 1787, a German schoolteacher decided to keep his 9- <strong>and</strong> 10-year-old pupils<br />

occupied by add<strong>in</strong>g up the <strong>in</strong>tegers from 1 to 100. But almost immediately one of<br />

the children claimed to have the answer! The teacher was suspicious, for the<br />

student had only the answer on his slate. But the answer was correct—5,050—<br />

<strong>and</strong> the student, Carl Gauss, grew up to be one of the greatest mathematicians of<br />

his time. It is widely suspected that young Gauss used the follow<strong>in</strong>g identity.<br />

Proposition 4.3: For any <strong>in</strong>teger n ≥ 1, we have:<br />

1 + 2 + 3 + … + (n − 2) + (n − 1) + n = n(n + 1)/2.<br />

We give two "visual" justifications of Proposition 4.3 <strong>in</strong> Figure 4.1.<br />

Figure 4.1: Visual justifications of Proposition 4.3.<br />

Both illustrations visualize the identity <strong>in</strong> terms of the<br />

total area covered by n unit-width rectangles with<br />

heights 1,2,…,n. In (a) the rectangles are shown to<br />

217

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

Saved successfully!

Ooh no, something went wrong!