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.

484 Chap. 14 <strong>Analysis</strong> TechniquesExample 14.1 Find the closed form solution for ∑ ni=1i using the divide<strong>and</strong>-guessapproach. We will try two example functions <strong>to</strong> illustrate thedivide-<strong>and</strong>-guess method: dividing by n <strong>and</strong> dividing by f(n − 1). Ourgoal is <strong>to</strong> find patterns that we can use <strong>to</strong> guess a closed-form expression asour c<strong>and</strong>idate for testing with an induction proof. To aid us in finding suchpatterns, we can construct a table showing the first few numbers of eachfunction, <strong>and</strong> the result of dividing one by the other, as follows.n 1 2 3 4 5 6 7 8 9 10f(n) 1 3 6 10 15 21 28 36 46 57n 1 2 3 4 5 6 7 8 9 10f(n)/n 2/2 3/2 4/2 5/2 6/2 7/2 8/2 9/2 10/2 11/2f(n − 1) 0 1 3 6 10 15 21 28 36 46f(n)/f(n − 1) 3/1 4/2 5/3 6/4 7/5 8/6 9/7 10/8 11/9Dividing by both n <strong>and</strong> f(n − 1) happen <strong>to</strong> give us useful patterns <strong>to</strong>f(n)work with.n= n+1 f(n)2, <strong>and</strong>f(n−1)= n+1n−1. Of course, lots of otherapproaches do not work. For example, f(n)−n = f(n−1). Knowing thatf(n) = f(n − 1) + n is not useful for determining the closed form solution<strong>to</strong> this summation. Or consider f(n) − f(n − 1) = n. Again, knowingthat f(n) = f(n − 1) + n is not useful. Finding the right combination ofequations can be like finding a needle in a haystack.In our first example, we can see directly what the closed-form solutionshould be.f(n)n = n + 12Obviously, f(n) = n(n + 1)/2.Dividing f(n) by f(n − 1) does not give so obvious a result, but itprovides another useful illustration.f(n)f(n − 1)= n + 1n − 1f(n)(n − 1) = (n + 1)f(n − 1)f(n)(n − 1) = (n + 1)(f(n) − n)nf(n) − f(n) = nf(n) + f(n) − n 2 − n2f(n) = n 2 + n = n(n + 1)f(n) =n(n + 1)2

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

Saved successfully!

Ooh no, something went wrong!