11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Sec. 16.3 Numerical <strong>Algorithm</strong>s 523• There are 2 k instances of length k or less.• The size (length) of value n is log n.• The cost of a particular algorithm can decrease when n increases in value(say when going from a value of 2 k − 1 to 2 k to 2 k + 1), but generallyincreases when n increases in length.16.3.1 Exponenti<strong>at</strong>ionWe will start our examin<strong>at</strong>ion of st<strong>and</strong>ard numerical algorithms by considering howto perform exponenti<strong>at</strong>ion. Th<strong>at</strong> is, how do we compute m n ? We could multiplyby m a total of n − 1 times. Can we do better? Yes, there is a simple divide<strong>and</strong> conquer approach th<strong>at</strong> we can use. We can recognize th<strong>at</strong>, when n is even,m n = m n/2 m n/2 . If n is odd, then m n = m ⌊n/2⌋ m ⌊n/2⌋ m. This leads to thefollowing recursive algorithmint Power(base, exp) {if exp = 0 return 1;int half = Power(base, exp/2); // integer division of exphalf = half * half;if (odd(exp)) then half = half * base;return half;}Function Power has recurrence rel<strong>at</strong>ion{ 0 n = 1f(n) =f(⌊n/2⌋) + 1 + n mod 2 n > 1whose solution isf(n) = ⌊log n⌋ + β(n) − 1where β is the number of 1’s in the binary represent<strong>at</strong>ion of n.How does this cost compare with the problem size? The original problem sizeis log m + log n, <strong>and</strong> the number of multiplic<strong>at</strong>ions required is log n. This is farbetter (in fact, exponentially better) than performing n − 1 multiplic<strong>at</strong>ions.16.3.2 Largest Common FactorWe will next present Euclid’s algorithm for finding the largest common factor(LCF) for two integers. The LCF is the largest integer th<strong>at</strong> divides both inputsevenly.First we make this observ<strong>at</strong>ion: If k divides n <strong>and</strong> m, then k divides n−m. Weknow this is true because if k divides n then n = ak for some integer a, <strong>and</strong> if kdivides m then m = bk for some integer b. So, LCF (n, m) = LCF (n − m, n) =LCF (m, n − m) = LCF (m, n).

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

Saved successfully!

Ooh no, something went wrong!