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.

Simplify<strong>in</strong>g the Analysis Further<br />

In algorithm analysis, we focus on the growth rate of the runn<strong>in</strong>g time as a<br />

function of the <strong>in</strong>put size n, tak<strong>in</strong>g a "big-picture" approach, rather than be<strong>in</strong>g<br />

bogged down with small details. It is often enough just to know that the runn<strong>in</strong>g<br />

time of an algorithm such as arrayMax, given <strong>in</strong> Section 1.9.2, grows<br />

proportionally to n, with its true runn<strong>in</strong>g time be<strong>in</strong>g n times a constant factor that<br />

depends on the specific computer.<br />

We analyze data structures <strong>and</strong> algorithms us<strong>in</strong>g a mathematical notation for<br />

functions that disregards constant factors. Namely, we characterize the runn<strong>in</strong>g<br />

times of algorithms by us<strong>in</strong>g functions that map the size of the <strong>in</strong>put, n, to values<br />

that correspond to the ma<strong>in</strong> factor that determ<strong>in</strong>es the growth rate <strong>in</strong> terms of n.<br />

We do not formally def<strong>in</strong>e what n means, however, <strong>and</strong> <strong>in</strong>stead let n refer to a<br />

chosen measure of the <strong>in</strong>put "size," which is allowed to be def<strong>in</strong>ed differently for<br />

each algorithm we are analyz<strong>in</strong>g. This approach allows us to focus attention on<br />

the primary "big-picture" aspects <strong>in</strong> a runn<strong>in</strong>g time function. In addition, the same<br />

approach lets us characterize space usage for data structures <strong>and</strong> algorithms,<br />

where we def<strong>in</strong>e space usage to be the total number of memory cells used.<br />

The "Big-Oh" Notation<br />

Let f(n) <strong>and</strong> g(n) be functions mapp<strong>in</strong>g nonnegative <strong>in</strong>tegers to real numbers. We<br />

say that f(n) is O(g(n)) if there is a real constant c > 0 <strong>and</strong> an <strong>in</strong>teger constant n 0 ≥<br />

1 such that<br />

f(n) ≤cg(n), for n ≥ n 0 .<br />

This def<strong>in</strong>ition is often referred to as the "big-Oh" notation, for it is sometimes<br />

pronounced as "f(n) is big-Oh of g(n)." Alternatively, we can also say "f(n) is<br />

order of g(n)." (This def<strong>in</strong>ition is illustrated <strong>in</strong> Figure 4.5.)<br />

Figure 4.5: Illustrat<strong>in</strong>g the "big-Oh" notation. The<br />

function f(n) is O(g(n)), s<strong>in</strong>ce f(n) ≤ c · g(n) when n≥n 0 .<br />

229

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

Saved successfully!

Ooh no, something went wrong!