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. 3.5 Calcul<strong>at</strong>ing the Running Time for a Program 71When analyzing these two code fragments, we will assume th<strong>at</strong> n isa power of two. The first code fragment has its outer for loop executedlog n + 1 times because on each iter<strong>at</strong>ion k is multiplied by two until itreaches n. Because the inner loop always executes n times, the total cost forthe first code fragment can be expressed as ∑ log ni=0n. Note th<strong>at</strong> a variablesubstitution takes place here to cre<strong>at</strong>e the summ<strong>at</strong>ion, with k = 2 i . FromEqu<strong>at</strong>ion 2.3, the solution for this summ<strong>at</strong>ion is Θ(n log n). In the secondcode fragment, the outer loop is also executed log n + 1 times. The innerloop has cost k, which doubles each time. The summ<strong>at</strong>ion can be expressedas ∑ log ni=0 2i where n is assumed to be a power of two <strong>and</strong> again k = 2 i .From Equ<strong>at</strong>ion 2.8, we know th<strong>at</strong> this summ<strong>at</strong>ion is simply Θ(n).Wh<strong>at</strong> about other control st<strong>at</strong>ements? While loops are analyzed in a mannersimilar to for loops. The cost of an if st<strong>at</strong>ement in the worst case is the gre<strong>at</strong>erof the costs for the then <strong>and</strong> else clauses. This is also true for the average case,assuming th<strong>at</strong> the size of n does not affect the probability of executing one of theclauses (which is usually, but not necessarily, true). For switch st<strong>at</strong>ements, theworst-case cost is th<strong>at</strong> of the most expensive branch. For subroutine calls, simplyadd the cost of executing the subroutine.There are rare situ<strong>at</strong>ions in which the probability for executing the variousbranches of an if or switch st<strong>at</strong>ement are functions of the input size. For example,for input of size n, the then clause of an if st<strong>at</strong>ement might be executed withprobability 1/n. An example would be an if st<strong>at</strong>ement th<strong>at</strong> executes the thenclause only for the smallest of n values. To perform an average-case analysis forsuch programs, we cannot simply count the cost of the if st<strong>at</strong>ement as being thecost of the more expensive branch. In such situ<strong>at</strong>ions, the technique of amortizedanalysis (see Section 14.3) can come to the rescue.Determining the execution time of a recursive subroutine can be difficult. Therunning time for a recursive subroutine is typically best expressed by a recurrencerel<strong>at</strong>ion. For example, the recursive factorial function fact of Section 2.5 callsitself with a value one less than its input value. The result of this recursive call isthen multiplied by the input value, which takes constant time. Thus, the cost ofthe factorial function, if we wish to measure cost in terms of the number of multiplic<strong>at</strong>ionoper<strong>at</strong>ions, is one more than the number of multiplic<strong>at</strong>ions made by therecursive call on the smaller input. Because the base case does no multiplic<strong>at</strong>ions,its cost is zero. Thus, the running time for this function can be expressed asT(n) = T(n − 1) + 1 for n > 1; T (1) = 0.We know from Examples 2.8 <strong>and</strong> 2.13 th<strong>at</strong> the closed-form solution for this recurrencerel<strong>at</strong>ion is Θ(n).

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

Saved successfully!

Ooh no, something went wrong!