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.

462 Chap. 14 <strong>Analysis</strong> Techniques14.1 Summ<strong>at</strong>ion TechniquesConsider the following simple summ<strong>at</strong>ion.n∑i.i=1In Section 2.6.3 it was proved by induction th<strong>at</strong> this summ<strong>at</strong>ion has the well-knownclosed form n(n + 1)/2. But while induction is a good technique for proving th<strong>at</strong>a proposed closed-form expression is correct, how do we find a c<strong>and</strong>id<strong>at</strong>e closedformexpression to test in the first place? Let us try to think through this problemfrom first principles, as though we had never seen it before.A good place to begin analyzing a summ<strong>at</strong>ion it is to give an estim<strong>at</strong>e of itsvalue for a given n. Observe th<strong>at</strong> the biggest term for this summ<strong>at</strong>ion is n, <strong>and</strong>there are n terms being summed up. So the total must be less than n 2 . Actually,most terms are much less than n, <strong>and</strong> the sizes of the terms grows linearly. If wewere to draw a picture with bars for the size of the terms, their heights would form aline, <strong>and</strong> we could enclose them in a box n units wide <strong>and</strong> n units high. It is easy tosee from this th<strong>at</strong> a closer estim<strong>at</strong>e for the summ<strong>at</strong>ion is about (n 2 )/2. Having thisestim<strong>at</strong>e in h<strong>and</strong> helps us when trying to determine an exact closed-form solution,because we will hopefully recognize if our proposed solution is badly wrong.Let us now consider some ways th<strong>at</strong> we might hit upon an exact equ<strong>at</strong>ion forthe closed form solution to this summ<strong>at</strong>ion. One particularly clever approach wecan take is to observe th<strong>at</strong> we can “pair up” the first <strong>and</strong> last terms, the second <strong>and</strong>(n − 1)th terms, <strong>and</strong> so on. Each pair sums to n + 1. The number of pairs is n/2.Thus, the solution is n(n + 1)/2. This is pretty, <strong>and</strong> there is no doubt about it beingcorrect. The problem is th<strong>at</strong> it is not a useful technique for solving many othersumm<strong>at</strong>ions.Now let us try to do something a bit more general. We already recognizedth<strong>at</strong>, because the largest term is n <strong>and</strong> there are n terms, the summ<strong>at</strong>ion is lessthan n 2 . If we are lucky, the closed form solution is a polynomial. Using th<strong>at</strong> asa working assumption, we can invoke a technique called guess-<strong>and</strong>-test. We willguess th<strong>at</strong> the closed-form solution for this summ<strong>at</strong>ion is a polynomial of the formc 1 n 2 + c 2 n + c 3 for some constants c 1 , c 2 , <strong>and</strong> c 3 . If this is true, then we can plugin the answers to small cases of the summ<strong>at</strong>ion to solve for the coefficients. Forthis example, substituting 0, 1, <strong>and</strong> 2 for n leads to three simultaneous equ<strong>at</strong>ions.Because the summ<strong>at</strong>ion when n = 0 is just 0, c 3 must be 0. For n = 1 <strong>and</strong> n = 2we get the two equ<strong>at</strong>ionsc 1 + c 2 = 14c 1 + 2c 2 = 3,

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

Saved successfully!

Ooh no, something went wrong!