Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an.

Similar presentations


Presentation on theme: "Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an."— Presentation transcript:

1

2 Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an Array Find Closest Point in a Plane Find Collinear Points in a Plane Maximum Contiguous Subsequence Sum Problem

3 Theorem 6.1

4 Proof Place the following N + 2 balls in a box: N balls numbered 1 through N, one unnumbered red ball and one unnumbered blue ball. Remove three balls from the box. If a red ball is drawn, number it as the lowest of the numbered balls drawn. If a blue ball is drawn, number it as highest of the numbered balls drawn. Note that if you draw both a red and a blue ball, then the effect is to have three balls identical numbered. Order the three balls. Each such order corresponds to a triplet solution to the equation in Theorem 6.1. The number of possible orders is the number of distinct ways to draw three balls without replacement from collection of N + 2 balls. This problem is similar to that of selecting three points from a group of N so we immediately obtain the stated result.

5

6

7 1 // Quadratic maximum contiguous subsequence sum algorithm. 2 // seqStart and seqEnd represent the actual best sequence. 3 template 4 Comparable maxSubsequenceSum( const vector & a, 5 int & seqstart, int & seqEnd ) 6 ( 7 int n = a.size( ); 8 Comparable maxSum = 0; 9 10 for( int i = 0; i < n; i++ ) 11 { 12 Comparable thisSum = 0; 13 for( int j = i; j < n; j++ ) 14 ( 15 thisSum += a [ j ] ; 16 17 if ( thisSum > maxSum ) 18 { 19 maxSum = thisSum; 20 seqStart = i; 21 seqEnd = j ; 22 } 23 } 24 } 25 26 return maxSum; 27 }

8 Linear Algorithm To move from a quadratic algorithm to a linear algorithm, we need to remove yet another loop The problem is that the quadratic algorithm is still an exhaustive search; that is, we are trying all possible subsequences The only difference between the quadratic and cubic algorithms is that the cost of testing each successive subsequence is a constant O(1) instead of linear O(N) Because a quadratic number of subsequences are possible, the only way we can attain a subquadratic bound is to find a clever way to eliminate from consideration a large number of subsequences, without actually computing their sum and testing to see if that sum is a new maximum

9 Theorem 6.2

10 Linear Algorithm Continued….

11 Theorem 6.3

12 1 // Linear maximum contiguous subsequence sum algorithm. 2 // seqStart and seqEnd represent the actual best sequence. 3 template 4 Comparable maxSubsequenceSum( const vector & a, 5 int & seqstart, int & seqEnd ) 6 { 7 int n = a. size ( ) ; 8 Comparable thissum = 0; 9 Comparable maxSum = 0; 10 11 for( int i = 0, j = 0; j < n; j++ ) 12 { 13 thissum += a [ j ] ; 14 15 if( thissum > maxSum ) 16 { 17 maxSum = thissum; 18 seqstart = i; 19 seqEnd = j ; 20 } 21 else if ( thissum < 0 ) 22 { 23 i=j+1; 24 thissum = 0; 25 } 26 } 27 return maxsum; 28 }

13 Linear Algorithm Continued…. According to Theorem 6.3 when a negative subsequence is detected, not only can we break the inner loop, but we can also advance i to j + 1 The running time of this algorithm is linear: At each step in the loop, we advance j, so the loop iterates at most N times The correctness of this algorithm is much less obvious than for the previous algorithms, which is typical. That is, algorithms that use the structure of a problem to beat an exhaustive search generally require some sort of correctness proof We proved that the algorithm is correct by using a short mathematical argument The purpose is not to make the discussion entirely mathematical, but rather to give a flavor of the techniques that might be required in advanced work

14 General Big-Oh Rules

15 Big-Oh Notation

16 Big-Omega Notation

17 Big-Theta Notation

18 Little-Oh Notation

19 Summary of Four Definition Mathematical ExpressionRelative Rates of Growth T(N)=O(F(N)) T(N)=Ω(F(N)) T(N)=Θ(F(N))Growth of T(N) is = growth of F(N) T(N)=o(F(N))

20 Running Time of Algorithms The running time of statements inside a group of nested loops is the running time of the statements multiplied by the sizes of all the loops The running time of a sequence of consecutive loops is the running time of the dominant loop The time difference between a nested loop in which both indices run from 1 to N and two consecutive loops that are not nested but run over the same indices is the same as the space difference between a two-dimensional array and two one-dimensional arrays The first case is quadratic The second case is linear because N + N is 2N, which is still O(N) Occasionally, this simple rule can overestimate the running time, but in most cases it does not Even if it does, Big-Oh does not guarantee an exact asymptotic answer-just an upper bound

21 Continued…. The analyses performed thus far involved use of a worst-case bound, which is a guarantee over all inputs of some size Another form of analysis is the average-case bound, in which the running time is measured as an average over all the possible inputs of size N The average might differ from the worst case if, for example, a conditional statement that depends on the particular input causes an early exit from a loop One algorithm has a better worst-case bound than another algorithm, that’s why nothing is implied about their relative average-case bounds However, in many cases average-case and worst-case bounds are closely correlated. When they are not, the bounds are treated separately


Download ppt "Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an."

Similar presentations


Ads by Google