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. 14.1 Summ<strong>at</strong>ion Techniques 463which in turn yield c 1 = 1/2 <strong>and</strong> c 2 = 1/2. Thus, if the closed-form solution forthe summ<strong>at</strong>ion is a polynomial, it can only bewhich is more commonly written1/2n 2 + 1/2n + 0n(n + 1).2At this point, we still must do the “test” part of the guess-<strong>and</strong>-test approach. Wecan use an induction proof to verify whether our c<strong>and</strong>id<strong>at</strong>e closed-form solution iscorrect. In this case it is indeed correct, as shown by Example 2.11. The inductionproof is necessary because our initial assumption th<strong>at</strong> the solution is a simplepolynomial could be wrong. For example, it might have been th<strong>at</strong> the true solutionincludes a logarithmic term, such as c 1 n 2 + c 2 n log n. The process shown here isessentially fitting a curve to a fixed number of points. Because there is always ann-degree polynomial th<strong>at</strong> fits n + 1 points, we have not done enough work to besure th<strong>at</strong> we to know the true equ<strong>at</strong>ion without the induction proof.Guess-<strong>and</strong>-test is useful whenever the solution is a polynomial expression. Inparticular, similar reasoning can be used to solve for ∑ n∑ i=1 i2 , or more generallyni=1 ic for c any positive integer. Why is this not a universal approach to solvingsumm<strong>at</strong>ions? Because many summ<strong>at</strong>ions do not have a polynomial as their closedform solution.A more general approach is based on the subtract-<strong>and</strong>-guess or divide-<strong>and</strong>guessstr<strong>at</strong>egies. One form of subtract-<strong>and</strong>-guess is known as the shifting method.The shifting method subtracts the summ<strong>at</strong>ion from a vari<strong>at</strong>ion on the summ<strong>at</strong>ion.The vari<strong>at</strong>ion selected for the subtraction should be one th<strong>at</strong> makes most of theterms cancel out. To solve sum f, we pick a known function g <strong>and</strong> find a p<strong>at</strong>tern interms of f(n) − g(n) or f(n)/g(n).Example 14.1 Find the closed form solution for ∑ ni=1i using the divide<strong>and</strong>-guessapproach. We will try two example functions to illustr<strong>at</strong>e thedivide-<strong>and</strong>-guess method: dividing by n <strong>and</strong> dividing by f(n − 1). Ourgoal is to find p<strong>at</strong>terns th<strong>at</strong> we can use to guess a closed-form expression asour c<strong>and</strong>id<strong>at</strong>e for testing with an induction proof. To aid us in finding suchp<strong>at</strong>terns, 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/9

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

Saved successfully!

Ooh no, something went wrong!