Download presentation

Presentation is loading. Please wait.

1
Algorithm Analysis

2
Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms –X A =B iff log X B=A –log A B =log C B/log C A; A,B,C > 0, A ≠ 1 –logAB = logA + logB; A, B > 0 Series N –∑ 2 i = 2 N+1 -1 i=0 N –∑ A i = A N+1 -1/A-1 i=0 N –∑ i = N(N+1)/2 i=0

3
Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this option bad? –What can we do about it?

4
Pseudo-Code Used to specify algorithms Part English, part code Algorithm (arrayMax(A, n)) curMax = A[0] for i=1 i<n i++ if curMax < A[i] curMax = A[i] return curMax

5
Counting Operations Algorithm (arrayMax(A, n)) curMax = A[0] //2 for i=1 i<n i++ //1+n if curMax < A[i] //4(n-1) 6(n-1) curMax = A[i] return curMax //1 Best case – 5n Worst case – 7n-2 Average case – hard to analyze

6
Asymptotic Notation 7n-2 n=5 -> 33 n=100 -> 698 n=1,000,000 -> 6,999,998 Running time grows proportionally to n What happens as n gets large?

7
Big-Oh T(N) is O(f(N)) if there are positive constants c and n 0 such that T(N) =n 0 7n 2 -2 is O(n 2 ) n 0 >=1 c=8 Upper bound

8
Omega T(N) is Ω(g(N)) if there are positive constants c and n 0 such that T(N) >= cg(N) when N>=n 0 7n 2 -2 is Ω(n 2 ) n 0 >=1 c=1 Lower bound

9
Theta T(N) is Θ(h(N)) if and only if T(N) = Oh(N) and T(N) = Ωh(N) 7n 2 -2 is Θ(n 2 ) Tightest result possible

10
Little-Oh T(N) is o(p(N)) if T(N) = O(p(N)) and T(N) ≠ Θ(p(N)) 7n 2 -2 is o(n 3 ) –O when n 0 >=8 c=1 Growth rate of T(N) is smaller than growth rate of p(N)

11
Rules Rule 1 –If T 1 (N) = O(f(N) and T 2 (N) = O(g(N)), then T 1 (N) + T 2 (N) = max(O(f(N)), O(g(N))) T 1 (N)* T 2 (N) = O(f(N)*g(N)) Rule 2 –If T(N) is a polynomial of degree k, then T(N) = Θ(N k ) Rule 3 –log k N = O(N) for any constant k. This tells us that logs grow very slowly.

12
Examples 87n 4 + 7n 3nlogn + 12logn 4n 4 + 7n 3 logn

13
Terminology Logarithmic – O(logn) Linear – O(n) Linearithmic – O(nlogn) Quadratic – O(n 2 ) Polynomial – O(n k ) k>=1 Exponential – O(a n ) a>1

14
Rule 1 – for loops The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations. for(i=0; i<5; i++) cout << “hello\n”;

15
Rule 2 – nested loops Analyze these inside out. The total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the sizes if all of the loops. for(i=0; i<n; i++) for(j=0; j<n; j++) cout << “hello\n”;

16
Rule 3 – consecutive statements These just add (which means that the max is the one that counts) for(i=0; i<n; i++) a[i] = 0; for(i=0; i<n; i++) for(j=0; j<n; j++) a[i]+=a[j]+i+j;

17
Rule 4 – if/else The running time of an if/else statement is never more than the running time of the test plus the larger of the running times of the statements, if(condition) S1 else S2

18
Example Find maximum number in nxn matrix Algorithm: 64…2 123…9 ………… 58…1 0 n-1 0

19
Example What is the big-oh running time of this algorithm? Algorithm: Input: A, n curMax = A[0][0] for i=0 i<n i++ for j=0 j<n j++ if curMax < A[i][j] curMax = A[i][j] return curMax

20
Another Example Determine how many elements of array 1 match elements of array 2 Algorithm? 24…6 n-10 68…3 0

21
Another Example Algorithm: Input: A, B, n for i=0 i<n i++ for j=0 j<n j++ if A[i] == A[j] matches++ break What is the running time of the algorithm? 24…6 n-10 68…3 0

22
Logs in the Running Time An algorithm is O(logN) if it takes constant time to cut the problem size by a fraction. Binary search –Given an integer X and integers A 0, A 1, …, A N-1, which are presorted and already in memory, find i such that A i =X, or return i = -1 if X is not in the input.

23
Binary Search Algorithm? Running time is at most ceil(log(N-1))+2 which is O(logN)

24
Example 1 – (N-1) - 1 2 – (N-1)/2 - 2 3 – (N-1)/4 - 4 4 – (N-1)/8 - 8 i – (N-1)/2 i-1 - 2 i-1 = N-1 –difference between high and low is 1 difference between high and low is 0

25
The Evil King King has N bottles of wine Exactly 1 bottle is poisoned How can the king figure out which bottle is poisoned and only kill at most logN of his testers?

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google