Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.

Similar presentations


Presentation on theme: "Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations."— Presentation transcript:

1 Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations

2 Asymptotic Performance Review Asymptotic performance: How do algorithms behave as the problem size gets very large? ● Running time ● Memory/storage requirements We want to know the number of primitive steps (like MIPS instructions) that are executed – Except for time of executing a function call, most statements roughly require the same amount of time – We can be more exact if need be Worst case vs. average case

3 Doing analysis (Asymptotic Notations) Simplifications – You want to ignore as many things as possible to make things simple. Specifically, we can get rid of constants because they do not change. Order of growth is what we want to measure – Highest-order term is what counts ● As the input size grows larger it is the high order term that dominates – If the higher term dominates, that means we can throw out lower orders. Example: 2n 4 + 193n 2 + 159304 ==> n 4 – When "n" becomes a big number, "n 4" will be so much bigger than the other numbers, that they are unimportant

4 Upper Bound Notation: Big- O We can say an algorithm's running time is O(n 2 ) – Properly we should say run time is in O(n 2 ) – Read O as “Big-O” (you’ll also hear it as “order”, as in “it runs in order n 2 ”) Big-O means: – A function f(n) is O(g(n)) if there exists positive constants c and n 0 so f(n)  c  g(n) for all n  n 0 Formally (using math notation) – O(g(n)) = { f(n):  positive constants c and n 0 such that f(n)  c  g(n)  n  n 0

5 Upper-Bound (Big O) A function f(n) is O(g(n)) if there exists positive constants c and n 0 so f(n)  c  g(n) for all n  n 0 So, Big-O says that there is a function [g(n)] that is always bigger than your function [f(n)]. (Sometimes you need to multiply g(n) by a constant to make it bigger though) F(n) – your function G(n) – the function that is always bigger after n 0

6 Big O Information Let's look at an example of a Big-O function If we say there is a polynomial with degree k, then we can also say it is O(n k ) Proof: – Suppose f(n) = b k n k + b k-1 n k-1 + … + b 1 n + b 0 ● Let a i = | b i | – f(n)  a k n k + a k-1 n k-1 + … + a 1 n + a 0

7 Lower Bound Notation As well as an upper-bound, we can also have a lower bound, called  (n) This is called omega notation (and is for a lower bound) and it says – f(n) is  (g(n)) if  positive constants c and n 0 such that 0  c  g(n)  f(n)  n  n 0 Proof: – Suppose "a" and "b" are constants and the running time is f(n) = a*n + b ● Assume a and b are positive – Then we can also say that: an  an + b – And since "a" is a constant we can throw it out and say that f(n) is  (n)

8 Asymptotic Tight Bound This is a way to bound the function f(n) by another function (called the Theta function) A function f(n) is  (g(n)) if  positive constants c 1, c 2, and n 0 so: c 1 g(n)  f(n)  c 2 g(n)  n  n 0 Theorem – f(n) is  (g(n)) if and only if f(n) is both O(g(n)) and  (g(n))

9 Other Notations A function f(n) is o(g(n)) if  positive constants c and n 0 such that f(n) < c g(n)  n  n 0 A function f(n) is  (g(n)) if  positive constants c and n 0 such that c g(n) < f(n)  n  n 0 So we can say that: o() is like < O() is like   () is like >  () is like   () is like =

10 Illustrating Complexity We can see how functions react with large data sets. The first picture shows when x = 0..20 and y=0..250 for different functions

11 Illustrating Complexity

12

13

14 Order of Growth Questions What is the Big-O for: – F(x) = x 2 + 0.0034x 5 + 1934x 3 – F(a) = a! + 2 a What is the Omega Notation for: – F(t) = (t*(3t + 15))/t If we look at the following: – F(x) = x 2 +.001x 10 + 200x 5 – Which Big-O is correct: O(x 2 ) or O(x 5 ) or O(x 10 )

15 Other Math Notations There are some other mathematical notations and formulas we need to know in order to discuss running times Logarithms: – lg n = log 2 n  the binary logarithm – ln n = log e n  the natural logarithm – log b n => logarithm with base b of n – a = b log b a – lg (ab) = lg a + lg b – lg(a/b) = lg a – lg b – log b a n = n log b a – log 1 = 0 – log b (1/a) = -log b a

16 Other Math Notations Factorial: –Factorials take a number and multiply all the numbers before it: –Example: 5! = 5*4*3*2*1 = 120 –(n-1)! = (n-1)(n-2)(n-3)…(2)(1) Conditional Notation –In math, if we want an if/else we write it like this This says that if x <= 2, then do an x 2, and if x > 2 then do a x 3

17 Summations If a function has a while loop or a for loop, the time it takes is the sum of each time it spends inside the loop Example: for (int k = 0; k < 20; k++) { // do something that takes 10 seconds } If the time inside the loop takes 10 seconds and we do the loop 20 times, then the loop takes 10*20. We can write this as a summation, like: The number on the bottom says were to start (like a variable). The number at the top says where to stop. The function is on the right

18 Summations Example: –F(a) = a 1 + a 2 + a 3 + a 4 –F(a) = –F(x) = 5x + 10x 2 + 15x 3 + 20x 4 + … –F(x) =

19 Arithmetic and Geometric Series There are a few special summations that people found out a long time ago. It would be good to be able to recognize them: Arithmetic Series:

20 Geometric Series The geometric series is similar to the arithmetic

21 Looking at algorithms With summations we have tools to analyze loops and other conditions. When we want to look at and analyze an algorithm, we want to find the total number of primitive operations. This means we want to find the total number of adds, multiplies and so on Many times, we will not know exactly how many there are because the input size is unknown. In that case, we use the variable "n" to represent an input size. When we have basic statements, we will often say they run in time O(c) which means that it is a constant time

22 Looking at Algorithms Example 1: int main() { 2: int x = 60*200; 3: for (int k = 0; k < n; k++) { 4: x = x*x; 5: } 6: } We want to look at each piece of the algorithm to find the total (usually big-O) running time Lines 1,2,5,6 are all O(c) because they take a constant time. The interesting lines are 3 and 4. How many times do lines 3 and 4 occur?

23 Looking at algorithms We can see that lines 3 and 4 occur n times (and we assume n is an input) So we say the total running time is the sum of all the different running times 3: for (int k = 0; k < n; k++) { 4: x = x*x; 5: } T(n) = O(c) + O(n)*O(c) = O(c)*O(n) = O(n) Remember, we are allowed to throw out the constants, because they are not important

24 Looking at algorithms We should also see that this is a summation 3: for (int k = 0; k < n; k++) { 4: x = x*x; 5: } The for loop does a constant expression n times. In summation form, that is:

25 Looking at algorithms We also need to be careful of what happens when there is something special inside a loop. For many sorting algorithms, there are loops inside of loops. Example: 3: for (int k = 0; k < n; k++) { 4: for (int k = n; k > 0; k--) { 5: x = x*2; 6: } 7: } What is the running time of this loop? –T(n) = n*(n*c) = cn 2 = O(n 2 ) What is the summation?

26 Looking at Algorithms It is very important not to get confused with operations inside the loop and with the number of times a loop runs. Example: for (int k = 0; k < a; k++) { int n = n*n; } Here, the loop depends on a, thus it would be O(a), and has nothing to do with n, which is just a variable in this example.

27 Can we analyze algorithms? What is the Big-O running time for the following algorithms int main() { for (int k = b; b > 0; k--) { if (k % 2 == 0) { cout << 0 << endl; } else { cout << 1 << endl; } int main() { for (int k = 0; k < a; k++) { int i = a; while (i > 0) { int z = i*30+73+k*k*a; i--; }

28 Recurrences When an algorithm contains a call to itself, it is called a recursive algorithm. When that happens, there is a special way to analyze the algorithm, using what is called a recurrence. There are generally three methods used in order to solve recurrences: Substitution, Iteration and the Master Method. An important note to remember is that when we have a recursive function, it must end at some point. We must make use of that in our "recurrence," otherwise our analysis will take forever (just like the function)

29 Recurrences The expression: is a recurrence. It says that the function T(n) is equal to 2*T(n/2) + cn if n is greater than 1. If n = 1, then T(n) = c only What is happening here is that the input size (n) is being cut in half each time. Thus, we will eventually get to the point where n is equal to 1. Then we know to stop

30 Recursion Examples

31 Recursion Algorithm and Recurrence Example: 1: int myFunction(int a) { 2: for (int k = 0; k < a; k++) { 3: myFunction(a/2); 4: } 5: return 1; 6: } When we want to analyze this function, we will look at each piece at a time. In line 2, we see we have a loop running at O(a), but there is something important inside the loop (a recursive call)

32 Recurrences Example: 1: int myFunction(int a) { 2: for (int k = 0; k < a; k++) { 3: myFunction(a/2); 4: } 5: return 1; 6: } In order to analyze this we can write it like: – T(a) = a*T(a/2) – The T(a/2) is like a recursive call, but with half the input. We multiply it by a because the loop happens "a" times

33 Solving Recurrences There are three ways to solve recurrences, so that we can determine the correct running time of a function The substitution method – this way looks for pieces that are familiar and replaces them with good guesses The iterative method - this way uses algebra and summations to expand the recursion until a pattern can be found The master method - uses known formulas for finding the solution

34 Substitution Method The substitution method involves guessing the answer and then using something called "mathematical induction" to prove it. This is a good method, but can only be used when you think you know the answer. "Mathematical Induction" is a formal way to prove something in math. Induction says that if you can prove something for one case (like when n = 1) and you can prove it for something like the n+1 case, then it is true for all cases.

35 Induction Example: Fibonacci Sequence Claim: We claim that F(n) < 2 n Proof: We will show our claim is correct by induction –Base Case: F(1) = 1 < 2 = 2 1 F(2) = 2 < 4 = 2 2 –Induction Step: (Here's the hard part) F(n) = F(n-1) + F(n-2). We also know n-1 < n and n -2 < n So, by using the "inductive hypothesis" we can say that F(n) = F(n-1) + F(n-2) < 2 n-1 + 2 n-2 < 2 n-1 + 2 n-1 = 2*2 n-1 = 2 n The inductive part allows us to substitute what we want to be true We assumed that F(n) < 2 n and so we could substitute in 2 n-1

36 Induction: Example II What if we want to prove the following Proof: By using induction –Base Case: If n = 1, then 1 = 1*(1+1)/2 = 2/2 = 1 –Induction: Assume claim is true for (n-1). We can then say that And by the induction hypothesis, we can say Which equals:

37 Substitution Example Now that we know what induction is about, we can do substitution Example: T(n) = 2T(n/2) + n – Remember, the hard part about substitution is guessing a good answer. Because this is an example, the answer is known to be O(n lg n). So we will guess that to be the answer – So our claim is that T(n) < cn lg n for a good constant "c" – So, to start the substitution we "substitute" in (cn lg n) into the equation and replace n by n/2

38 Substitution Example: T(n) = 2T(n/2) + n – T(n) < 2(c (n/2) lg (n/2)) + n  substitute here – T(n) < cn (lg n – lg 2) + n  2*c(n/2) & split lg – T(n) < cn lg n – cn lg 2 + n  distribute cn – T(n) < cn lg n – cn + n  lg 2 is constant – T(n) < cn lg n  -cn + n = 0 So this will be true as long as c > 1, but the Big-O needs us to show that there is a n 0 so that for all n > n 0, this is true. So we can take for example, n 0 =4 and c = 2 and see what happens. But we have a recursive thing here, so pay attention to what happens (We're also going to assume T(1) = 1 to make things easier.) T(4) = 2T(4/2) + 4 = 2(2T(2/2) + 2) + 4 = 2(2*1 + 2) + 4 = 12 So, T(4) = 12 and T(4) should < 2*4 lg 4 = 8 lg 4 = 8*2 = 16. True!! This will actually be true for any n > 3 and any c > 2 (Like Base Case)

39 Substitution Example: II Show T(n) = T(n/2) + 1 is O(lg n) So, we need to show T(n) < c lg n – Assume that it works for n/2 and substitute – T(n) < c lg (n/2) + 1 – T(n) < c lg n – c lg 2 + 1 – T(n) < c lg n – c*1 + 1 – T(n) < c lg n – c + 1 = O(lg n) – So we know that it holds for n/2. Can we find a c and a n 0 so that for any n > n 0, it will be true?

40 Substitution Example: II T(n) = T(n/2) + 1 is O(lg n) – So if we say c = 2 and n 0 = 2 – T(n) = T(2/2) + 1 < 2 lg 2 – T(n) = T(1) + 1 < 2*1 – T(n) = 1 + 1 < 2 … True! What if we try something bigger, say n = 64 – T(64) = T(64/2) + 1 < 2 lg 64 – T(64) = T(T(32/2) + 1) + 1 = T(T(T(16/2) + 1) + 1 + 1) – We should notice a pattern. The result is that T(64) = 7 – And 2 lg 64 = 2 * 6 = 12. So it is still true!

41 Iterative Method The iterative method is another way to solve recurrences. It does not rely so much on proofs, but instead on recognizing recursive patterns. Example: 1. c + s(n-1) 2. c + c + s(n-2)  iterate through steps 3. 2c + s(n-2)  continue iterating 4. 2c + c + s(n-3) 5. 3c + s(n-3)  iterate until you see a patter … kc + s(n-k) = ck + s(n-k)  here we found a pattern

42 Iterative Method So now that we found a pattern, we use the base case to find the upper bound (Big-O) – So our pattern so far is: ● s(n) = ck + s(n-k) – What happens if k = n? Then: ● s(n) = cn + s(0) = cn – Thus, in general, ● s(n) = cn and s(n) = O(n) ● Much easier than substitution. The only hard part is to identify the pattern.

43 Iterative Method Example: II S(n) = n + s(n-1) =n + n-1 + s(n-2) = 2n -1 + s(n-2) =2n - 1 + n-2 + s(n-3) = 3n -3 + s(n-3) =3n - 3 + n-3 + s(n-4) = 4n-6 + s(n-4) =4n – 6 + n-4 + s(n-5) = 5n-10 + s(n-5) What is at the "k" time? = s(n) = kn – [(k*(k+1)/2)-k] + s(n-k) We must use the arithematic series to change the constants = s(n) = k(n-1) – (k*(k+1)/2) + s(n-k)

44 Iterative Method Example = s(n) = k(n-1) – (k*(k+1)/2) + s(n-k) So what if we say that k = n? = s(n) = n*(n-1) – n*(n+1)/2 + s(0) = s(n) = n 2 -n – n 2 /2 - n/2 = s(n) = n 2 /2 – n/2 We can remove the constants of (1/2) and we are left with = s(n) = n 2 – n = O(n 2 ) This proves the running time of the recurrence

45 Iterative Method Example T(n) = 2T(n/2) + c 2(2T(n/2/2) + c) + c 2 2 T(n/2 2 ) + 2c + c 2 2 (2T(n/2 2 /2) + c) + 3c 2 3 T(n/2 3 ) + 4c + 3c 2 3 T(n/2 3 ) + 7c 2 3 (2T(n/2 3 /2) + c) + 7c 2 4 T(n/2 4 ) + 15c … 2 k T(n/2 k ) + (2 k – 1)c

46 Iterative Method Example – So far for n > 2k we have T(n) = 2 k T(n/2 k ) + c(2 k - 1) – What if k = lg n? T(n) = 2 lg n T(n/2 lg n ) + c(2 lg n - 1)  No we use our log's = n T(n/n) + c(n – 1) = n T(1) + c(n-1) = nc + c(n-1) = c(2n – 1) so T(n) = O(n)

47 General Example: Iterative T(n) = ■ aT(n/b) + cn ■ a(aT(n/b/b) + cn/b) + cn ■ a 2 T(n/b 2 ) + cna/b + cn ■ a 2 T(n/b 2 ) + cn(a/b + 1) ■ a 2 (aT(n/b 2 /b) + cn/b 2 ) + cn(a/b + 1) ■ a 3 T(n/b 3 ) + cn(a 2 /b 2 ) + cn(a/b + 1) ■ a 3 T(n/b 3 ) + cn(a 2 /b 2 + a/b + 1) ■ … ■ a k T(n/b k ) + cn(a k-1 /b k-1 + a k-2 /b k-2 + … + a 2 /b 2 + a/b + 1)

48 General Example: Iterative So we have – T(n) = a k T(n/b k ) + cn(a k-1 /b k-1 +... + a 2 /b 2 + a/b + 1) What if we pretend that k = log b n (to make things easier) – Then that would mean: n = b k  Do we know why? – T(n)= a k T(1) + cn(a k-1 /b k-1 +... + a 2 /b 2 + a/b + 1) = a k c + cn(a k-1 /b k-1 +... + a 2 /b 2 + a/b + 1) = ca k + cn(a k-1 /b k-1 +... + a 2 /b 2 + a/b + 1) = cna k /b k + cn(a k-1 /b k-1 +... + a 2 /b 2 + a/b + 1) = cn(a k /b k +... + a 2 /b 2 + a/b + 1)

49 General Example: Iterative So with k = log b n – T(n) = cn(a k /b k +... + a 2 /b 2 + a/b + 1) Now we need another what if. What if a = b? Then: – T(n) = cn(k + 1)  k because we had a k..a 1 = cn(log b n + 1)  put back our old k= log b n =  (n log n)

50 General Example: Iterative So with k = log b n – T(n) = cn(a k /b k +... + a 2 /b 2 + a/b + 1) What if a < b? Recall that  (x k + x k-1 + … + x + 1) = (x k+1 -1)/(x-1) T(n) = cn ·  (1) =  (n)

51 General Example: Iterative So with k = log b n – T(n) = cn(a k /b k +... + a 2 /b 2 + a/b + 1) What if a > b? – T(n) = cn ·  (a k / b k ) = cn ·  (a log n / b log n ) = cn ·  (a log n / n) recall logarithm fact: a log n = n log a = cn ·  (n log a / n) =  (cn · n log a / n) =  (n log a )

52 General Example: Iterative So this proves something special about the recurrence relations.

53 The Master Method If you have a divide and conquer algorithm that: – Uses an algorithm that divides the problem of size n into a subproblems, each of size n/b – If you let the cost of each stage (the amount of instructions used at each part of the function) be described by the function f(n) Then, the Master Method gives us a simple way to find the running time of an algorithm

54 The Master Method if T(n) = aT(n/b) + n c and a > 1, b > 1 and c > 0, then:

55 The Master Method Proof T(n) = aT(n/b) + n c = n c + a((n/b) c + aT(n/b 2 )) = n c + (a/b c )n c + a 2 T(n/b 2 ) = n c + (a/b c )n c +a 2 ((n/b 2 )c + aT(n/b 3 )) = n c + (a/b c )n c + (a/b c ) 2 n c + a 3 T(n/b 3 ) = n c + (a/b c )n c + (a/b c ) 2 n c + (a/b c ) 3 n c +... + (a/b c ) log b n-1 n c + a log b n T(1) = =

56 The Master Method Proof If (a < b c ) – a < b c if and only if a/b c < 1, then a < b c if log b a < log b b c = c

57 The Master Method Proof If (a = b c ) – a = b c if and only if a/b c = 1, then a = b c iff log b a = log b b c = c

58 The Master Method Proof If (a > b c ) – a > b c iff a/b c > 1, then

59 Other Recurrences Logarithmic  ( log n) – Recurrence: T(n) = 1 + T(cn/2) where c > 0 – Typical example: When you recurse using half the input, and ignore the other half Linear  (N) – Recurrence: T(n) = 1 + T(n-1) – Typical Example: A single loop Quadratic  ( n 2 ) – Recurrence: T(n) = n + T(n-1) – Typical Example: loops inside of loops Exponential  ( 2 n ) – Recurrence: T(n) = 2T(n-1)

60 Using the master method T(n) = 9T(n/3) + n – Remember the general form: T(n) = aT(n/b) + n c – So a = 9, b = 3 and c = 1 – Thus a > b^c – That means we'll use the form – So the answer:

61 Using the master method T(n) = 2T(n/2) + n – Remember the general form: T(n) = aT(n/b) + n c – So a = 2, b = 2 and c = 1 – Thus a = b^c (2 = 2^1) – That means we'll use the form – So the answer:

62 Summary Analyzing algorithms requires a lot of math. There are many tools you need to master before you will be able to analyze any algorithm you see. These tools include: notations, induction, recurrences, substitution and other methods covered Knowing different mathematical relationships about logarithms will also be important


Download ppt "Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations."

Similar presentations


Ads by Google