CS 350 Algorithms and Complexity Winter 2019 Lecture 3: Analyzing Non-Recursive Algorithms Andrew P. Black Department of Computer Science Portland State University Analysis of time efficiency ! Time efficiency is analyzed by determining the number of repetitions of the “basic operation” ! Almost always depends on the size of the input ! “Basic operation”: the operation that contributes most towards the running time of the algorithm T(n) ≈ cop ⨉ C(n) "2 Analysis of time efficiency ! Time efficiency is analyzed by determining the number of repetitions of the “basic operation” ! Almost always depends on the size of the input ! “Basic operation”: the operation that contributes most towards the running time of the algorithm run time T(n) ≈ cop ⨉ C(n) "2 Analysis of time efficiency ! Time efficiency is analyzed by determining the number of repetitions of the “basic operation” ! Almost always depends on the size of the input ! “Basic operation”: the operation that contributes most towards the running time of basic of the algorithm cost op: constant run time T(n) ≈ cop ⨉ C(n) "2 Analysis of time efficiency ! Time efficiency is analyzed by determining the number of repetitions of the “basic operation” ! Almost always depends on the size of the input ! “Basic operation”: the operation that contributes most towards the running time of basic of the algorithm cost op: constant number run time T(n) ≈ cop ⨉ C(n) of times basic op is executed "2 Problem Input size measure Basic operation Searching for key in a list of n items Multiplication of two matrices Checking primality of a given integer n Shortest path through a graph "3 Complete the table Problem Input size measure Basic operation Searching for key in a list of n items A: Number of list’s items, i.e. n A: Key comparison Multiplication of two matrices B: Matrix dimension, or total number of elements B: Multiplication of two numbers Checking primality of a given integer n C: size of n = number of C: Division digits Shortest path through a graph D: #vertices and/or edges D: Visiting a vertex or traversing an edge "4 Problem Input size measure Searching for key in a list of n items A: Number of list’s items, i.e. n Basic operation B: Matrix dimension, or total number of elements C: size of n = number of digits D: #vertices and/or edges "5 Problem Searching for key in a list of n items Input size measure Basic operation A: Key comparison B: Multiplication of two numbers C: Division D: Visiting a vertex or traversing an edge "6 Problem Input size measure Basic operation A: Number of list’s items, i.e. n Multiplication of two matrices B: Matrix dimension, or total number of elements C: size of n = number of digits D: #vertices and/or edges "7 Problem Input size measure Basic operation A: Key comparison Multiplication of two matrices B: Multiplication of two numbers C: Division D: Visiting a vertex or traversing an edge "8 Problem Input size measure Basic operation A: Number of list’s items, i.e. n B: Matrix dimension, or total number of elements Checking primality of a given integer n C: size of n = number of digits D: #vertices and/or edges "9 Problem Input size measure Basic operation A: Key comparison B: Multiplication of two numbers Checking primality of a given integer n C: Division D: Visiting a vertex or traversing an edge "10 Problem Input size measure Basic operation A: Number of list’s items, i.e. n B: Matrix dimension, or total number of elements C: size of n = number of digits Shortest path through a graph D: #vertices and/or edges "11 Problem Input size measure Basic operation A: Key comparison B: Multiplication of two numbers C: Division Shortest path through a graph D: Visiting a vertex or traversing an edge "12 Problem Input size measure Basic operation Searching for key in a list of n items Multiplication of two matrices Checking primality of a given integer n Shortest path through a graph "13 Best-case, average-case, worst-case ! For some algorithms, efficiency depends on the input: ! Worst ! Best case: Cworst(n) – maximum over inputs of size n case: Cbest(n) – minimum over inputs of size n ! Average " case: Cavg(n) – “average” over inputs of size n Number of times the basic operation will be executed on typical input # Not the average of worst and best case " Expected number of basic operations under some assumption about the probability distribution of all possible inputs "14 Discuss: ! What’s the best case, and its running time? A. constant — O(1) B. linear — O(n) C. quadratic — O(n2) "15 Discuss: ! What’s the worst case, and its running time? A. constant — O(1) B. linear — O(n) C. quadratic — O(n2) "16 Discuss: ! What’s the average case, and its running time? A. constant — O(1) B. linear — O(n) C. quadratic — O(n2) "17 General Plan for Analysis of non-recursive algorithms 1. Decide on parameter n indicating input size 2. Identify algorithm’s basic operation 3. Determine worst, average, and best cases for input of size n 4. Set up a sum for the number of times the basic operation is executed 5. Simplify the sum using standard formulae and rules (see Levitin Appendix A) "18 analyzing such algorithms. EXAMPLE 1 Consider the problem of finding the value of the largest element in a list of n numbers. For simplicity, we assume that the list is implemented as an array. The following is pseudocode of a standard algorithm for solving the problem. “Basic Operation” ALGORITHM MaxElement(A[0..n − 1]) //Determines the value of the largest element in a given array //Input: An array A[0..n − 1] of real numbers //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 The obvious measure of an input’s size here is the number of elements in the array,! i.e., n. The operations that are going to be executed most often are in the algorithm’s for loop. There are two operations in the loop’s body: the comparison A[i] > maxval and the assignment maxval ← A[i]. Which of these two operations should we " consider basic? Since the← comparison is executed on each repetition of the loop and the assignment is not, we should consider the comparison to be " basic operation. Note that the number of comparisons will be the the algorithm’s same for all arrays of size n; therefore, in terms of this metric, there is no need to distinguish among the worst, average, and best cases here. Why choose > as the basic operation? Why not i Or [ ] ? i+1? "19 Same Algorithm: ALGORITHM MaxElement (A: List) // Determines the value of the largest element in the list A // Input: a list A of real numbers // Output: the value of the largest element of A maxval ← A.first for each in A do if each > maxval maxval ← each return maxval ! Why " " choose > as the basic operation? Why not i ← i + 1 ? Or [ ] ? "20 From Algorithm to Formula We want a formula for the # of basic ops ! Basic op will normally be in inner loop ! Bounds of for loop become bounds of summation ! e.g. for i ← l .. h do: 3 basic operations ! ! h X <latexit sha1_base64="PgrGfqzWsuLxxVb2ld/iMhb1CnM=">AAAB+XicdZDLSgMxFIYz9VbrbdSlm2ARXJVMbel0IRTduKxgL9COQyZN29BkZkgyhTLMm7hxoYhb38Sdb2N6EVT0h8DHf87hnPxBzJnSCH1YubX1jc2t/HZhZ3dv/8A+PGqrKJGEtkjEI9kNsKKchbSlmea0G0uKRcBpJ5hcz+udKZWKReGdnsXUE3gUsiEjWBvLt+2+SoSfskue3afjDF74dhGVylVUdxE0UENV1zVQrdRRGUGnhBYqgpWavv3eH0QkETTUhGOleg6KtZdiqRnhNCv0E0VjTCZ4RHsGQyyo8tLF5Rk8M84ADiNpXqjhwv0+kWKh1EwEplNgPVa/a3Pzr1ov0UPXS1kYJ5qGZLlomHCoIziPAQ6YpETzmQFMJDO3QjLGEhNtwiqYEL5+Cv+HdrnkGL6tFBtXqzjy4AScgnPggBpogBvQBC1AwBQ8gCfwbKXWo/VivS5bc9Zq5hj8kPX2Ccikk8A=</latexit> 3 i=l "21 Works for nested loops too n X2 i=0 <latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit> 0 @ n X1 j=i+1 1 1A "22 Works for nested loops too n X2 i=0 <latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit> 0 @ n X1 j=i+1 1 1A "22 Works for nested loops too n X2 i=0 <latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit> 0 @ n X1 j=i+1 1 1A "22 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Useful Summation Formulae Σl≤i≤u1 = 1+1+…+1 = u - l + 1 In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n) Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2) Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3) Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1 In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) Σ(ai ± bi ) = Σ ai ± Σ bi Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai Σc ai = c Σ ai "23 Where do the Summation formulae come from? ! Answer: mathematics. ! Example: The Euler–Mascheroni constant 𝛾 is defined as: = lim n!1 n X 1 i=1 i ln n ! "24 Where do the Summation formulae come from? ! Answer: mathematics. ! Example: The Euler–Mascheroni constant 𝛾 is defined as: Harmonic series = lim n!1 n X 1 i=1 i ln n ! "24 Where do the Summation formulae come from? ! Answer: mathematics. ! Example: The Euler–Mascheroni constant 𝛾 is defined as: = lim Harmonic series n!1 n X 1 i=1 i ln n ! y = 1/x "24 Where do the Summation formulae come from? ! Answer: mathematics. ! Example: The Euler–Mascheroni constant 𝛾 is defined as: = lim Harmonic series n!1 n X 1 i=1 i ln n ! y = 1/x ln x "24 What does Levitin’s ⇡ mean? ! “becomes almost equal to as n → ∞” ! So formula 8 n X i=1 " lim n!1 lg i ⇡ n lg n means n X i=1 lg i n lg n ! =0 "25 Example: Counting Binary Digits "26 Example: Counting Binary Digits ! How many times is the basic operation executed? "26 Example: Counting Binary Digits ! How many times is the basic operation executed? ! Why is this algorithm harder to analyze than the earlier examples? "26 Ex 2.3, Problem 1 ! Working Exercises 2.3 with a partner: 1. Compute the following sums. a. 1 + 3 + 5 + 7 + ... + 999 b. 2 + 4 + 8 + 16 + ... + 1024 c. !n+1 f. !n i=3 1 j+1 j=1 3 d. !n+1 g. !n i=3 i=1 e. i !n j=1 ij h. !n−1 i=0 !n−1 P n 2. Find the order of growth of the following sums. !n−1 2 !n−1 2 a. i=0 (i +1) b. i=2 lgi2 i=0 i=1 i(i + 1) 1/i(i + 1) "27 Ex!2.3, Problem 2 ! b. 2 + 4 + 8 + 16 + ... + 1024 c. f. n+1 i=3 1 !n d. j+1 j=1 3 g. n+1 i=3 !n i=1 i !n j=1 ij e. !n−1 i(i + 1) h. !n−1 1/i(i + 1) i=0 i=0 2. Find the order of growth of the following sums. !n−1 2 !n−1 2 a. i=0 (i +1) b. i=2 lgi2 c. !n i=1 (i i−1 + 1)2 d. !n−1 !i−1 i=0 j=0 (i + j) Use the Θ(g(n)) notation with the simplest function g(n) possible. 3. The sample variance of n measurements x1 , x2 , ..., xn can be compute !n !n 2 i=1 (xi − x̄) i=1 xi where x̄ = n−1 n or "28 !n !n 2 2 i=1 xi − ( i=1 xi ) /n a. i=0 (i2 +1)2 b. i=2 lgi2 Ex 2.3, Problem 3 c. !n i=1 (i i−1 + 1)2 d. !n−1 !i−1 i=0 j=0 (i + j) Use the Θ(g(n)) notation with the simplest function g(n) possible. 3. The sample variance of n measurements x1 , x2 , ..., xn can be computed as !n !n 2 i=1 (xi − x̄) i=1 xi where x̄ = n−1 n or !n !n 2 2 i=1 xi − ( i=1 xi ) /n . n−1 Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n "29 Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"30 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS What does this algorithm compute? A. n2 Pn B. i=1 i a. What does this algorithm compute? Pn 2 C. b. What is its basic operation? i=1 i Pn c. How many times is the basic operation executed? D. i=1 2i d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"30 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"30 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS What is the basic operation? a. What does this algorithm compute? b. What is its basic operation? A. multiplication B. addition C. assignment c. How many times is the basic operation executed? D. squaring d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"31 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 How many times is the basic operation executed? 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS A. once B. n times a. What does this algorithm compute? C. lg n times b. What is its basic operation? D. none of the above c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"32 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Find and compare the number of divisions, multiplications, and additions/subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. Ex 2.3, Problem 4 4. Consider the following algorithm. Algorithm Mystery( n) //Input: A nonnegative integer n S←0 for i ← 1 to n do S ←S+i∗i returnS What is the efficiency class of this algorithm? [b is # of bits needed to represent n] A. ⇥(1) a. What does this algorithm compute? B. b. What is its basic operation? ⇥(n) C. ⇥(b) c. How many times is the basic operation executed? D. ⇥(2b ) d. What is the efficiency class of this algorithm? 16 e. Suggest an improvement or a better algorithm altogether and indi"33 cate its efficiency class. If you cannot do it, try to prove that, in fact, it Ex 2.3, Problem 4 (cont) d. What is the efficiency class of this algorithm? e. Suggest an improvement or a better algorithm altogether and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done. 5. Consider the following algorithm. Algorithm Secret(A[0..n − 1]) //Input: An array A[0..n − 1] of n real numbers minval ← A[0]; maxval ← A[0] for i ← 1 to n − 1 do if A[i] < minval minval ← A[i] if A[i] > maxval maxval ← A[i] return maxval − minval "34 e. Suggest an improvement or a better algorithm altogether a cate its efficiency class. If you cannot do it, try to prove that, i cannot be done. Problem 5 — Group work 5. Consider the following algorithm. Algorithm Secret(A[0..n − 1]) //Input: An array A[0..n − 1] of n real numbers minval ← A[0]; maxval ← A[0] for i ← 1 to n − 1 do if A[i] < minval minval ← A[i] a. What does this algorithm compute? if A[i] > maxval b. What is its basic operation? maxval ← A[i] c. How many times is the basic return maxval − minval operation executed? d. What is the efficiency class of this Answer questions a—e of Problem 4algorithm? about this algorithm. e. Suggest an improvement or a better algorithm altogether and indicate its "35 6. Consider the following algorithm. efficiency class. What effect will this change have on the algorithm’s efficiency? Ex 2.3, Problem 9 8. Determine the asymptotic order of growth for the total number of times all the doors are toggled in the Locker Doors puzzle (Problem 11 in Exercises 1.1). 9. Prove the formula n ! i = 1 + 2 + ... + n = n(n + 1) 2 i=1 either by mathematical induction or by following the insight of a 10-year old schoolboy named Karl Friedrich Gauss (1777—1855) who grew up to become one of the greatest mathematicians of all times. 17 "36 Ex 2.3, Problem 11 10. Consider the following version of an important algorithm that we will study later in the book. Algorithm GE (A[0..n − 1, 0..n]) //Input: An n-by-n + 1 matrix A[0..n − 1, 0..n] of real numbers for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do for k ← i to n do A[j, k] ← A[j, k] − A[i, k] ∗ A[j, i] / A[i, i] a.◃ Find the time efficiency class of this algorithm. a. Find the time efficiency class of this algorithm b.◃ What glaring inefficiency does this pseudocode contain and how can b. beWhat glaringtoinefficiency does this code contain, and how it eliminated speed the algorithm up? can it be eliminated? 11. von Neumann’s neighborhood How many one-by-one squares are generc. Estimate the reduction in runwith time. ated by the algorithm that starts a single square square and on each of its n iterations adds new squares all round the outside. How many one-by-one squares are generated on the nth iteration? [Gar99, p.88] (In "37 the parlance of cellular automata theory, the answer is the number of cells a.◃ Find the time efficiency class of this algorithm. Problem 11: von Neumann neighborhood b.◃ What glaring inefficiency does this pseudocode contain and how can it be eliminated to speed the algorithm up? 11. von Neumann’s neighborhoodsquares How many How many one-by-one are one-by-one generatedsquares by theare generated by the algorithm that starts with a single square square and on each that adds startsnew with a single square, on each its ofalgorithm its n iterations squares all round the and outside. How of many n iterations adds squares around the outside. How many one-by-one squares arenew generated on the nth iteration? [Gar99, p.88] (In the parlance of cellular automata theory, theon answer number ofHere cells one-by-one squares are generated the nisththe iteration? in the von Neumann neighborhood of range n.) The results for n = 0, 1, are2 the neighborhoods and are illustrated below: for n = 0, 1, and 2. n=0 n=1 n=2 "38