Understanding Algorithm Complexity Through Asymptotic Analysis

Download Presenatation
complexity analysis asymptotic analysis n.w
1 / 60
Embed
Share

Dive into the world of algorithm complexity analysis with a focus on asymptotic notation and finding the asymptotic upper bound of algorithms. Learn how to measure algorithms, compare them, and grasp the significance of resource functions in determining algorithm efficiency.

  • Algorithm Complexity
  • Asymptotic Analysis
  • Resource Functions
  • Efficiency
  • Performance Analysis

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Complexity Analysis: Asymptotic Analysis

  2. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation

  3. Today Topic Finding the asymptotic upper bound of the algorithm

  4. WAIT Did we miss something? The resource function!

  5. Resource Function We don t know, yet Size of input Time Time used Function of an algorithm A Space Space used Size of input Function of an algorithm A And this one

  6. From Experiment We count the number of instructions executed Only count some instruction One that s promising

  7. Why instruction count? Does instruction count == time? Probably But not always But Usually, there is a strong relation between instruction count and time if we count the most occurred instruction

  8. Why count just some? Remember the findMaxCount()? Why we count just the max assignment? Why don t we count everything? What if, each max assignment takes N instruction That starts to make sense

  9. Time Function = instruction count Time Function = instruction count Time Function = instruction count Time Function = instruction count

  10. COMPUTE O()

  11. Interesting Topics of Upper Bound Rule of thumb! We neglect Lower order terms from addition E.g. n3+n2 = O(n3) Constant E.g. 3n3 = O(n3) Remember that we use = instead of (more correctly)

  12. Why Discard Constant? From the definition We can scale by adjusting the constant c E.g. 3n = O(n) Because When we let c >= 3, the condition is satisfied

  13. Why Discard Lower Order Term? Consider f(n) = n3+n2 g(n) = n3 If f(n) = O(g(n)) Then, for some c and n0 c * g(n)-f(n) > 0 Definitely, just use any c >1

  14. Why Discard Lower Order Term? Try c = 1.1 1.1 * g(n)-f(n) = 0.1n3-n2 Does 0.1n3-n2 > 0 ? It is when 0.1n > 1 E.g., n > 10 0.1n3-n2 > 0 0.1n3 > n2 0.1n3/n2 0.1n > 1 > 1

  15. Lower Order only? In fact, It s only the dominant term that count Which one is dominating term? The one that grows faster The non- dominant term Why? Eventually, it is g*(n)/f*(n) If g(n) grows faster, g(n)/f*(n) > some constant E.g, lim g(n)/f*(n) infinity The dominant term

  16. What dominating what? na n log n n2log n cn Log n n nb n n log2n nc 1 log n (when a > b) Left side dominates

  17. Putting into Practice What is the asymptotic class of 0.5n3+n4-5(n-3)(n-5)+n3log8n+25+n1.5 O(n4) O(n3) (n-5)(n2+3)+log(n20) O(n5.2) 20n5+58n4+15n3.2*3n2

  18. Asymptotic Notation from Program Flow Sequence Conditions Loops Recursive Call

  19. Sequence Block A f (n) f(n) + g(n) = g (n) Block B O( max(f(n),g(n)) )

  20. Example f(n) + g(n) = O(max (f(n),g(n)) Block A O(n) O(n2) O(n2) Block B

  21. Example f(n) + g(n) = O(max (f(n),g(n)) Block A (n) O(n2) O(n2) Block B

  22. Example f(n) + g(n) = O(max (f(n),g(n)) Block A (n) (n2) (n2) Block B

  23. Example f(n) + g(n) = O(max (f(n),g(n)) Block A O(n2) (n2) (n2) Block B

  24. Condition O( max (f(n),g(n)) ) Block A f (n) Block B g (n)

  25. Loops for (i = 1;i <= n;i++) { P(i) } n = i it 1 Let P(i) takes time ti

  26. Example for (i = 1;i <= n;i++) { sum += i; } n = ) 1 ( = ( ) n 1 i sum += i (1)

  27. Why dont we use max(ti)? Because the number of terms is not constant for (i = 1;i <= n;i++) { sum += i; } (n) (1) for (i = 1;i <= 100000;i++) { sum += i; } With large constant

  28. Example for (j = 1;j <= n;j++) { for (i = 1;i <= n;i++) { sum += i; } } n n n = = sum += i (1) ) 1 ( = ( ) n = 1 1 1 j i j = + + + ( ) ( ) ... ( ) n n n = 2 ( ) n

  29. Example j n n = j 1 = n = ) 1 ( ( ) j = 1 1 i j for (j = 1;j <= n;j++) { for (i = 1;i <= j;i++) { sum += i; } } = j = cj 1 n = j = c j 1 n ) 1 + ( n = c 2 sum += i (1) = 2 ( ) n

  30. Example : Another way j n n = = ) 1 ( = ( ) j for (j = 1;j <= n;j++) { for (i = 1;i <= j;i++) { sum += i; } } = 1 1 1 j i j n = = ( ) O n 1 j = 2 ( ) O n sum += i (1)

  31. Example Your turn j 1 1 n = n = ) 1 ( = ( ) 2 j = 2 3 2 j i j 1 n = = for (j = 2;j <= n-1;j++) { for (i = 3;i <= j;i++) { sum += i; } } j 2 j ) 1 ( n n = 1 2 2 n n = ) 1 ( 2 2 2 = ( ) n sum += i (1)

  32. Example : While loops While (n > 0) { n = n - 1; } (n)

  33. Example : While loops While (n > 0) { n = n - 10; } (n/10) = (n)

  34. Example : While loops While (n > 0) { n = n / 2; } (log n)

  35. Example : Euclids GCD function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a }

  36. Example : Euclids GCD Until the modding one is zero function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a How many iteration? } Compute mod and swap

  37. Example : Euclids GCD a b

  38. Example : Euclids GCD a b a mod b If a > b a mod b < a / 2

  39. Case 1: b > a / 2 a b a mod b

  40. Case 1: b a / 2 a b a mod b We can always put another b

  41. Example : Euclids GCD function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a O( log n) B always reduces at least half }

  42. RECURSIVE PROGRAM

  43. Code that calls itself void recur(int i) { // checking termination // do something // call itself }

  44. Find summation from 1 to i int sum(int i) { //terminating condition if (i == 1) //return 1; // recursive call int result; result = i + sum(i-1); return result; } void main() { printf( %d\n ,sum()); }

  45. Order of Call: Printing Example void seq(int i) { if (i == 0) return; void seq(int i) { if (i == 0) return; printf("%d ",i); seq(i - 1); seq(i - 1); printf("%d ",i); } }

  46. Draw Triangle void drawtri(int start,int i,int n) { if (i <= n) { for (int j = start;j <= i+start-1;j++) { printf("%d ",j); } printf("\n"); drawtri(start,i + 1,n); } }

  47. Programming Recursive Function First, define what will the function do Usually, it is related to smaller instant of problem Write the function to do that in the trivial case Call itself to do the defined things

  48. Analyzing Recursive Programming Use the same method, count the most occurred instruction Needs to consider every calls to the function

  49. Example void sum(int i) { if (i == 0) return 0; (n) int result; result = i + sum(i - 1); return result; }

  50. Example 2 void sum(int i) { if (i == 0) return 0; int result; result = i + sum(i - 1); int count = 0; for (int j = 0;j < i;j++) { count++; } return result; (n2) }

More Related Content