12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

32 Chap. 2 Mathematical PreliminariesIn this book, nearly all logarithms used have a base of two. This is becausedata structures <strong>and</strong> algorithms most often divide things in half, or s<strong>to</strong>re codes withbinary bits. Whenever you see the notation log n in this book, either log 2 n is mean<strong>to</strong>r else the term is being used asymp<strong>to</strong>tically <strong>and</strong> the actual base does not matter. Ifany base for the logarithm other than two is intended, then the base will be shownexplicitly.Logarithms have the following properties, for any positive values of m, n, <strong>and</strong>r, <strong>and</strong> any positive integers a <strong>and</strong> b.1. log(nm) = log n + log m.2. log(n/m) = log n − log m.3. log(n r ) = r log n.4. log a n = log b n/ log b a.The first two properties state that the logarithm of two numbers multiplied (ordivided) can be found by adding (or subtracting) the logarithms of the two numbers.4 Property (3) is simply an extension of property (1). Property (4) tells us that,for variable n <strong>and</strong> any two integer constants a <strong>and</strong> b, log a n <strong>and</strong> log b n differ bythe constant fac<strong>to</strong>r log b a, regardless of the value of n. Most runtime analyses inthis book are of a type that ignores constant fac<strong>to</strong>rs in costs. Property (4) says thatsuch analyses need not be concerned with the base of the logarithm, because thiscan change the <strong>to</strong>tal cost only by a constant fac<strong>to</strong>r. Note that 2 log n = n.When discussing logarithms, exponents often lead <strong>to</strong> confusion. Property (3)tells us that log n 2 = 2 log n. How do we indicate the square of the logarithm(as opposed <strong>to</strong> the logarithm of n 2 )? This could be written as (log n) 2 , but it istraditional <strong>to</strong> use log 2 n. On the other h<strong>and</strong>, we might want <strong>to</strong> take the logarithm ofthe logarithm of n. This is written log log n.A special notation is used in the rare case where we would like <strong>to</strong> know howmany times we must take the log of a number before we reach a value ≤ 1. Thisquantity is written log ∗ n. For example, log ∗ 1024 = 4 because log 1024 = 10,log 10 ≈ 3.33, log 3.33 ≈ 1.74, <strong>and</strong> log 1.74 < 1, which is a <strong>to</strong>tal of 4 log operations.4 These properties are the idea behind the slide rule. Adding two numbers can be viewed as joiningtwo lengths <strong>to</strong>gether <strong>and</strong> measuring their combined length. Multiplication is not so easily done.However, if the numbers are first converted <strong>to</strong> the lengths of their logarithms, then those lengths canbe added <strong>and</strong> the inverse logarithm of the resulting length gives the answer for the multiplication (thisis simply logarithm property (1)). A slide rule measures the length of the logarithm for the numbers,lets you slide bars representing these lengths <strong>to</strong> add up the <strong>to</strong>tal length, <strong>and</strong> finally converts this <strong>to</strong>tallength <strong>to</strong> the correct numeric answer by taking the inverse of the logarithm for the result.

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

Saved successfully!

Ooh no, something went wrong!