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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 2.4 Summ<strong>at</strong>ions <strong>and</strong> Recurrences 33We can exp<strong>and</strong> the recurrence as many steps as we like, but the goal isto detect some p<strong>at</strong>tern th<strong>at</strong> will permit us to rewrite the recurrence in termsof a summ<strong>at</strong>ion. In this example, we might notice th<strong>at</strong>(T(n − 2) + 1) + 1 = T(n − 2) + 2<strong>and</strong> if we exp<strong>and</strong> the recurrence again, we getT(n) = T(n − 2) + 2 = T(n − 3) + 1 + 2 = T(n − 3) + 3which generalizes to the p<strong>at</strong>tern T(n) = T(n − i) + i. We might concludeth<strong>at</strong>T(n) = T(n − (n − 1)) + (n − 1)= T(1) + n − 1= n − 1.Because we have merely guessed <strong>at</strong> a p<strong>at</strong>tern <strong>and</strong> not actually provedth<strong>at</strong> this is the correct closed form solution, we should use an inductionproof to complete the process (see Example 2.13).Example 2.9 A slightly more complic<strong>at</strong>ed recurrence isT(n) = T(n − 1) + n; T (1) = 1.Exp<strong>and</strong>ing this recurrence a few steps, we getT(n) = T(n − 1) + n= T(n − 2) + (n − 1) + n= T(n − 3) + (n − 2) + (n − 1) + n.We should then observe th<strong>at</strong> this recurrence appears to have a p<strong>at</strong>tern th<strong>at</strong>leads toT(n) = T(n − (n − 1)) + (n − (n − 2)) + · · · + (n − 1) + n= 1 + 2 + · · · + (n − 1) + n.This is equivalent to the summ<strong>at</strong>ion ∑ nclosed-form solution.i=1i, for which we already know theTechniques to find closed-form solutions for recurrence rel<strong>at</strong>ions are discussedin Section 14.2. Prior to Chapter 14, recurrence rel<strong>at</strong>ions are used infrequently inthis book, <strong>and</strong> the corresponding closed-form solution <strong>and</strong> an explan<strong>at</strong>ion for howit was derived will be supplied <strong>at</strong> the time of use.

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

Saved successfully!

Ooh no, something went wrong!