3.Mathematical Analysis of Nonrecursive Algorithms



Comments



Description

CS3024-FAZ 1Mathematical Analysis of Nonrecursive Algorithms Design and Analysis of Algorithms (CS3024) 23/02/2006 CS3024-FAZ 2 Learning by Examples We systematically apply the general framework outlined before to analyzing the efficiency of nonrecursive algorithms. CS3024-FAZ 3 Important Sum Manipulations (1) ) ( 2 1 2 ) 1 ( ... 2 1 limits integer upper & lower : ; 1 1 ) ( 2 2 1 0 1 1 1 1 1 n n n n n i i u l l u b a b a a c ca n i n i u l i u i i u i i u i i i u i i u i i O e ~ + = + + + = = s + ÷ = ± = ± = ¿ ¿ ¿ ¿ ¿ ¿ ¿ ¿ = = = = = = = = CS3024-FAZ 4 Important Sum Manipulations (2) ¿ ¿ ¿ ¿ ¿ = = + = + = = ~ ~ + ~ + + + = = ÷ ÷ = + + + + = + ~ + + + = ~ + + = + + + = n i n n i i n n n i i k k k k n i k n i n n i n a a a a a a a n k n i n n n n n i 1 1 2 1 1 1 1 2 0 1 1 3 2 2 2 1 2 lg lg ... 5772 . 0 ; ln ... 1 ) 1 ( 1 1 ... 1 1 1 ... 2 1 3 1 6 ) 1 2 )( 1 ( ... 2 1 ¸ ¸ CS3024-FAZ 5 Example 1 Algorithm MaxElement(A[0..n-1]) // determine the value of the largest element in a // given array // input: An array A[0..n-1] of real number // output: the value of the largest element in A maxval ÷ A[0] for i ÷ 1 to n-1 do if A[i] > maxval maxval ÷ A[i] return maxval CS3024-FAZ 6 Exp1: Analysis (1) Input size = number of elements = n Most often executed  inside the for loop The comparison A[i] > maxval The assignment maxval ÷ A[i] Basic operation = the comparison Executed on each repetition of the loop The assignment are not The number of comparison will be the same for all array of size n CS3024-FAZ 7 Exp1: Analysis (2) C(n) = the number of times this comparison is executed The algorithm makes one comparison on each execution of the loop  within the bounds between 1 and n-1 (inclusively) Thus, we have: ) ( 1 1 ) ( 1 1 n n n C n i O e ÷ = = ¿ ÷ = CS3024-FAZ 8 Analyzing Efficiency of Nonrecursive Algorithms (1) 1. Decide on a parameter(s) indicating an input’s size 2. Identify the algorithm’s basic operation (typically, it is located in its inner most loop) 3. Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depend on some additional property, the worst-case, average- case, and, if necessary, the best-case efficiencies have to be investigated separately CS3024-FAZ 9 Analyzing Efficiency of Nonrecursive Algorithms (2) 4. Set up a sum expressing the number of times the algorithm’s basic operation is executed 5. Using standard formulas and rules of sum manipulation, either find a closed- form formula for the count or, at the very least, establish its order of growth CS3024-FAZ 10 Example 2 Algorithm UniqueElements(A[0..n-1]) //checks whether all the elements in a given // array are distinct //input: an array A[0..n-1] //output: returns ‘true’ if all elements in A are // distinct, and ‘false’ otherwise for i ÷ 0 to n – 2 do for j ÷ i +1 to n – 1 do if A[i] = A[j] return false return true CS3024-FAZ 11 Exp2: Analysis (1) Input’s size = n Innermost loop contains a single operation Basic operation = comparison The number of comparison will depend not only on n but also on whether there are equal elements in the array and, if there are, which array positions they occupy We will limit our investigation on the worst- case only CS3024-FAZ 12 Exp2: Analysis (2) The worst-case input is an array for which C worst (n) is the largest among all arrays of size n There are two kinds of worst-case inputs: Array with no equal elements Array in which the last two elements are the only pair of equal elements CS3024-FAZ 13 Exp2: Analysis (3) ) ( 2 1 2 ) 1 ( 1 ... ) 2 ( ) 1 ( ) 1 ( ] 1 ) 1 ( ) 1 [( 1 ) ( 2 2 2 0 2 0 2 0 1 1 n n n n n n i n i n n C n i n i n i n i j worst O e ~ ÷ = + + ÷ + ÷ = ÷ ÷ = + + ÷ ÷ = = ¿ ¿ ¿¿ ÷ = ÷ = ÷ = ÷ + = CS3024-FAZ 14 Example 3 Algorithm MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1]) //multiplies two square matrices of order n by the // definition-based algorithm //input: two n-by-n matrices A and B //output: matrix C = AB for i ÷ 0 to n-1 do for j ÷ 0 to n-1 do C[i,j] ÷ 0,0 for k ÷ 0 to n-1 do C[i,j] ÷ C[i,j] + A[i,k]* B[k,j] return C CS3024-FAZ 15 Exp3: Analysis (1) Input’s size = matrix order n In the innermost loop: multiplication & addition  basic operation candidates MUL & ADD executed exactly once on each repetition on innermost loop  we don’t have to choose between these two operations Sum of total number of multiplication ¿ ¿¿ ¿¿¿ ÷ = ÷ = ÷ = ÷ = ÷ = ÷ = = = = = 1 0 3 2 1 0 1 0 1 0 1 0 1 0 1 ) ( n i n i n j n i n j n k n n n n M CS3024-FAZ 16 Exp3: Analysis (2) Estimate the running time of the algorithm on a particular machine More accurate estimation (include addition) c m : the time of one multiplication c a : the time of one addition 3 ) ( ) ( n c n M c n T m m = ~ 3 3 3 ) ( ) ( ) ( ) ( n c c n c n c n A c n M c n T a m a m a m + = + = + ~ CS3024-FAZ 17 Example 4 Algorithm Binary(n) //input: a positive decimal integer n //output: the number of binary digits in n’s binary representation count ÷ 1 while n > 1 do count ÷ count + 1 n ÷ ¸n/2¸ return count CS3024-FAZ 18 Exp4: Analysis (1) The most frequent executed operation is the comparison n > 1 The number of times the comparison will be executed is larger than the number of repetition of the loop’s body by exactly 1 CS3024-FAZ 19 Exp4: Analysis (2) The value of n is about halved on each repetition of the loop  about log 2 n The exact formula: ¸log 2 n¸ + 1 Another approach  analysis techniques based on recurrence relation CS3024-FAZ 20 Exercises (1) 1. Compute the following sums: 1 + 3 + 5 + 7 +…+ 999 2 + 4 + 6 + 8 +…+ 1024 2. Compute order of growth of the following sums ¿¿ ¿ ¿ ¿ ¿ = = ÷ = = + + = + = + n i n j n i n j j n i n i ij i i i 1 1 1 0 1 1 1 3 1 3 ; 3 ); 1 ( ; ; 1 ; 2 ) 1 ( ; lg ; ) 1 ( 1 1 1 2 2 1 0 2 2 ¿ ¿ ¿ = ÷ ÷ = ÷ = + + n i i n i n i i i i CS3024-FAZ 21 Exercises (2) 3. Algorithm Mystery(n) //input: a nonnegative integer n S ÷ 0 for i ÷ 1 to n do S ÷ S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic op executed? d. What is the efficiency class of this algorithm? e. Can you make any improvement? CS3024-FAZ 22 Exercises (3) 4. Algorithm Secret(A[0..n-1]) //input: an array A[0..n-1] of n real number mi ÷ A[0]; ma ÷ A[0] for i ÷ 1 to n-1 do if A[i] < mi then mi ÷ A[i] if A[i] > ma then ma ÷ A[i] return ma - mi a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic op executed? d. What is the efficiency class of this algorithm? e. Can you make any improvement?
Copyright © 2024 DOKUMEN.SITE Inc.