2 Notion of Algorithm.docx

# Let consider a routine with nested loops 24b function

• 19

This preview shows pages 16–19. Sign up to view the full content.

Let consider a routine with nested loops [2.4.b]. ------------------------------------------------------------ FUNCTION SumProd (n: integer): integer; VAR result,k,i: integer; BEGIN result:= 0; FOR k:= 1 TO n [2.4.b] FOR i:= 1 TO k result:= result+k*i; SumProd:= result END ;{ SumProd } ------------------------------------------------------------ Entering procedure and initializing O (1) FOR loop ( n iterations) FOR loop ( n iterations) O (n*1) = O (n) O (n*n) =O (n 2 ) O( 1+n 2 ) =O (n 2 ) Inner O (1) Fig. 2.4.b. Temporal scheme of the algorithm [2.4.b] To analyze the time complexity of this algorithm, we make use of assumption (2), and assume that the inner loop is executed the maximum number of times possible. In reality this is not true because the inner loop iterates with limit k n (and not with limit n ) We can demonstrate that this simplifying assumption does not affect the final temporal estimation. In a more careful analysis, we observe that the first time the inner loop is encountered it iterates once, the second time it iterates twice, and so on. In that case, as k varies in the outer loop, it causes the inner loop to take time O(k) . Since k in the outer loop takes on the values 1 , 2, ... , n, the exact amount of time contributed by the outer and inner loops is [2.4.c]: ------------------------------------------------------------ nnOn () ()+=12 2 1 + 2 + 3 + ... + n = [2.4.c] ------------------------------------------------------------ As result, the exact number of iterations is presented in [2.4.c], which is O(n 2 ).

This preview has intentionally blurred sections. Sign up to view the full version.

In an analog manner, the time estimation of the sequence [2.4.d] is O (n n n) = O (n 3 ). ------------------------------------------------------------ FOR i:= 1 TO n DO FOR j:= 1 TO i DO FOR k:= 1 TO i DO [2.4.d] something that is {O(1)}; ------------------------------------------------------------ The exact number of iterations can be computed as in [2.4.e], observing that the two nested inner loops iterate i 2 times for each iteration of the outer FOR loop. ------------------------------------------------------------ 1 2 + 2 2 + 3 2 + ... + n 2 = = O (n 3 ) [2.4.e] 61)(21)(+ ⋅⋅ + nnn ------------------------------------------------------------ Example 2.4.c. Consider a program segment with a multiplicative loop , one that is nested within another loop, but whose iterations are not a linear function of the control variable of the outer loop [2.4.f]. ------------------------------------------------------------ m:= 1 FOR i:= 1 TO n DO BEGIN m:= m*2; FOR j:= 1 TO m DO [2.4.f] something that is {O(1)} END ; {FOR} ------------------------------------------------------------ In this case, as the outer loop control takes on the values 1, 2, 3, …, the inner loop iterates 2, 4, 8, …, times, giving a running time of order O ( 2 + 4 + 8 + ... + 2 n ) [2.4.g]. In fact we have here the sum of a geometric series in which each term is a constant multiple of the preceding one. ------------------------------------------------------------ 2 + 4 + 8 + ... + 2 n = [2.4.g] )()( nn 1 2O12122 + =−− ------------------------------------------------------------ This estimation is completely different from estimation O (n 2 ), which we are tempted to advance, because it is valuable only for non-multiplicative loops. 2.5 Profiling an Algorithm Suppose we assume that a program solving some problem has been devised, coded, proved correct, and debugged on a computer. The program was built by expressing a certain algorithm in terms of a programming language . We are interested in producing a performance profile , that is determining the precise amounts of time and storage this program will consume.
In order to determine exact times, our computing system must be equipped with a clock whose time can be read using specific system functions .

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern