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.

4.1.2 The Logarithm function<br />

One of the <strong>in</strong>terest<strong>in</strong>g <strong>and</strong> sometimes even surpris<strong>in</strong>g aspects of the analysis of data<br />

structures <strong>and</strong> algorithms is the ubiquitous presence of the logarithm function, f(n)<br />

= log b n, for some constant b > 1. This function is def<strong>in</strong>ed as follows:<br />

x = log b n if <strong>and</strong> only if b x = n.<br />

By def<strong>in</strong>ition, log b 1 = 0. The value b is known as the base of the logarithm.<br />

Comput<strong>in</strong>g the logarithm function exactly for any <strong>in</strong>teger n <strong>in</strong>volves the use of<br />

calculus, but we can use an approximation that is good enough for our purposes<br />

without calculus. In particular, we can easily compute the smallest <strong>in</strong>teger greater<br />

than or equal to log a n, for this number is equal to the number of times we can<br />

divide n by a until we get a number less than or equal to 1. For example, this<br />

evaluation of log 3 27 is 3, s<strong>in</strong>ce 27/3/3/3 = 1. Likewise, this evaluation of log 4 64 is<br />

4, s<strong>in</strong>ce 64/4/4/4/4 = 1, <strong>and</strong> this approximation to log 2 12 is 4, s<strong>in</strong>ce 12/2/2/2/2 =<br />

0.75 ≤ 1. This base-two approximation arises <strong>in</strong> algorithm analysis, actually, s<strong>in</strong>ce a<br />

common operation <strong>in</strong> many algorithms is to repeatedly divide an <strong>in</strong>put <strong>in</strong> half.<br />

Indeed, s<strong>in</strong>ce computers store <strong>in</strong>tegers <strong>in</strong> b<strong>in</strong>ary, the most common base for the<br />

logarithm function <strong>in</strong> computer science is 2. In fact, this base is so common that we<br />

will typically leave it off when it is 2. That is, for us,<br />

logn = log 2 n.<br />

We note that most h<strong>and</strong>held calculators have a button marked LOG, but this is<br />

typically for calculat<strong>in</strong>g the logarithm base-10, not base-two.<br />

There are some important rules for logarithms, similar to the exponent rules.<br />

Proposition 4.1 (Logarithm Rules): Given real numbers a > 0, b > 1,<br />

c > 0 <strong>and</strong> d > 1, we have:<br />

1. log b ac = log b a + log b c<br />

2. log b a/c = log b a− log b c<br />

3. log b a c = clog b a<br />

4. log b a = (log d a)/log d b<br />

5. b log da<br />

= a log db<br />

.<br />

Also, as a notational shorth<strong>and</strong>, we use log c n to denote the function (logn) c . Rather<br />

than show how we could derive each of the identities above which all follow from<br />

the def<strong>in</strong>ition of logarithms <strong>and</strong> exponents, let us illustrate these identities with a<br />

few examples <strong>in</strong>stead.<br />

215

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

Saved successfully!

Ooh no, something went wrong!