Analysis of Iterative (Nonrecursive) Algorithm Hopefully, you’ll be able to: • Understand how to calculate for the analysis of iterative algorithm • When you see a code, you can estimate its running time Example A( ) A( ) { { for (๐ = 1 ๐ก๐ ๐){ int ๐, ๐; for (๐ = 1 ๐ก๐ ๐){ printf (“Salam”); for (๐ = 1 ๐ก๐ ๐){ }} printf (“Salam”); }}} © S. Turaev CSC 3102 DSA II 3 Analysis of Nonrecursive Algos Example: Find the value of the largest element in a list of n real numbers. Algorithm MaxElement(A 0. . n − 1 ) // Input: An array A 0. . n − 1 //Output: The value the max element in A max ← A[0] for i ← 1 to n − 1 do Two operations in the loop if A i > max Basic operation: the max ← A[i] comparison (it is executed in return max each repetition of the loop!) © S. Turaev CSC 3102 DSA II 4 Analysis of Nonrecursive Algos Analysis: โช n is the number of times the comparison is executed. โช The number of comparisons will be the same. โช No need to distinguish among the worst, average and best cases. n−1 C n = เท1=n−1∈ Θ n . i=1 © S. Turaev CSC 3102 DSA II 5 General Plan for Analysis โช Decide on parameter(s) indicating input size(s) โช Identify algorithm’s basic operation(s) โช Determine worst, average, and best case efficiencies โช Set up a sum for the number of times the basic operation(s) is (are) executed โช Simplify the sum using standard formulas and rules © S. Turaev CSC 3102 DSA II 6 Analysis of Nonrecursive Algos Example: The element uniqueness problem: check if all elements in a given array of n elements are distinct. Algorithm UniqueElement(A 0. . n − 1 ) // Input: An array A 0. . n − 1 //Output: Return “True” if all elements in A are distinct & “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 © S. Turaev CSC 3102 DSA II 7 Analysis of Nonrecursive Algos โช The input size: n, the number of the elements in an array โช The basic operation: the comparison operation in the innermost loop โช The number of comparisons depends not only on n but also on if there are equal elements in the array, if there are, which array positions they occupy. โช The worst-case inputs: • arrays with no equal elements • arrays with the last two equal elements only © S. Turaev CSC 3102 DSA II 8 Analysis of Nonrecursive Algos โช Worst-case analysis: n−2 n−1 Cworst n = เท เท 1 n−2 i=0 j=i+1 = เท n−1 − i+1 −1 i=0 n−2 = เท n − 1 −i = n− 1 + n −2 + โฏ+ 1 i=0 n n−1 1 2 = ≈ n ∈ Θ n2 2 2 © S. Turaev CSC 3102 DSA II 9 Exercise 1 A( ) A( ) { { for (๐ = 1 ๐ก๐ ๐){ int ๐, ๐; for (๐ = 1 ๐ก๐ ๐){ printf (“Salam”); for (๐ = 1 ๐ก๐ ๐){ }} printf (“Salam”); }}} © S. Turaev CSC 3102 DSA II 10 Exercise 2 a) 1+2+3+4+….+100 = ? (Generalize: if 1+2+3+4+….+n ?) b) 10 + 20 + 30 + 40 + … + 1000 = ? (Generalize) c) ๐ − 1 + ๐ − 2 + ๐ − 3 + โฏ + 1 =? d) 1 + 3 + 5+ 7 +9 + … + 999 = ? (Generalize) e) 1 + 4 + 9 + 16 + 25 =? f) 1 + 4 + 9 + 16 + โฏ + 100 =? Exercise 3 a) ๐+1 σ๐=3 1 b) ๐+1 σ๐=3 ๐ c) ๐ ๐ σ๐=1 σ๐=1 ๐๐ Exercise 4 & 5 A(n) { A(n) int ๐, ๐, ๐, ๐; { for (๐ = 1; ๐ ≤ ๐; ๐ + +){ for (๐ = 1; ๐ 2 ≤ ๐; ๐ + +){ for (๐ = 1; ๐ ≤ ๐; ๐ + +){ printf (“Salam”); for (๐ = 1; ๐ ≤ 100; ๐ + +){ } printf (“Salam”); }}} } 13 Exercise 6 A(n) { int ๐, ๐, ๐, ๐; for (๐ = 1; ๐ ≤ ๐; ๐ + +){ for (๐ = 1; ๐ ≤ ๐ 2 ; ๐ + +){ for (๐ = 1; ๐ ≤ ๐ ;๐ 2 + +){ printf (“Salam”); }}} } 15 Alg 7(n) Exercise 7 & 8 Alg 8(n) { int ๐ = 1, ๐ = 1; { int ๐ = 1, ๐ = 1; while (๐ ≤ ๐){ while (๐ ≤ ๐){ ๐ + +; ๐ + +; ๐ + +; ๐ = ๐ + ๐; printf (“Salam”); printf (“Salam”); }} }} 17