1 / 26

Analysis of Algorithms

Analysis of Algorithms. Rate of Growth of functions. Prof. Muhammad Saeed. Rate of Growth of functions. Running Times. Assume N = 100,000 and processor speed is 1,000,000 operations per second. Series and Asymptotics. Series I. Series II. Infinite Series.

Télécharger la présentation

Analysis of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Analysis of Algorithms Rate of Growth of functions Prof. Muhammad Saeed

  2. Rate of Growth of functions Analysis of Algorithms

  3. Analysis of Algorithms

  4. Analysis of Algorithms

  5. Analysis of Algorithms

  6. Running Times • Assume N = 100,000 and processor speed is 1,000,000 operations per second Analysis of Algorithms

  7. Series and Asymptotics Analysis of Algorithms

  8. Series I Analysis of Algorithms

  9. Series II Analysis of Algorithms

  10. Infinite Series Analysis of Algorithms

  11. Fundamental DefinitionsAsymptotics • T(n) = O(f(n)) if there are constants c and n0 such that T(n) ≤ cf(n) when n  n0 • T(n) = (g(n)) if there are constants c and n0 such that T(n)  cg(n) when n  n0 • T(n) = (h(n)) if and only if T(n) = O(h(n)) and T(n) = (h(n)) • T(n) = o(p(n)) if T(n) = O(p(n)) and T(n)  (p(n)) Analysis of Algorithms

  12. T(n) = (h(n)) T(n) = O(f(n)) T(n) = (g(n)) Asymptotics Analysis of Algorithms

  13. Relative Rates of Growth Analysis of Algorithms

  14. Relative Growth Rate ofTwo Functions Compute using L’Hopital’s Rule • Limit=0: f(n)=o(g(n)) • Limit=c0: f(n)=(g(n)) • Limit=: g(n)=o(f(n)) Analysis of Algorithms

  15. Important Rules 3n3 = O(n3) 3n3 + 8 = O(n3) 8n2 + 10n * log(n) + 100n + 1020 = O(n2) 3log(n) + 2n1/2 = O(n1/2) 2100 = O(1) TlinearSearch(n) = O(n) TbinarySearch(n) = O(log(n)) Analysis of Algorithms

  16. Important Rules Rule 1: If T1(n) = O(f(n)) and T2(n) = O(g(n)), then a) T1(n) + T2(n) = max(O(f(n)), O(g(n))) b) T1(n) * T2(n) = O(f(n)*g(n)) Rule 2: If T(x) is a polynomial of degree n, then T(x)=(xn) Rule 3: logk n = O(n) for any constant k. Analysis of Algorithms

  17. General Rulesfor • Loops • Nested Loops • Consecutive statements • if-then-else • Recursion Analysis of Algorithms

  18. Euclid’s GCD intgcd(int a, int b) { int t; while (b != 0) { t = b; b = a % b; a = t; } return a; } Analysis of Algorithms

  19. Binary Search function BinarySearch(a, value, left, right) while left ≤ right mid := floor((right+left)/2) if a[mid] = value return mid if value < a[mid] right := mid-1 else left := mid+1 endwhile return not found Analysis of Algorithms

  20. Insertion Sort A case Analysis of Algorithms

  21. Insertion Sort Best Case: T(n)=O(n) Worst Case: T(n)=O(n2) Analysis of Algorithms

  22. Maximum Subsequence Sum A case Analysis of Algorithms

  23. Maximum Subsequence SumAlgorithm 1 intMaxSubsequenceSum( constint A[], const unsigned int N) { int Sum=0, MaxSum=0; for( i = 0; i<N; i++) for( j = i; j<N; j++) { Sum = 0; for( k = i; k<=j; k++) Sum += A[ k ]; if (Sum > MaxSum) MaxSum = Sum; } return MaxSum; } Analysis of Algorithms

  24. Maximum Subsequence SumAlgorithm 2 int MaxSubsequenceSum( const int A[], const unsigned int N) { int Sum=0, MaxSum=0; for( i = 0; i<N; i++) Sum = 0; for( j = i; j<N; j++) { Sum += A[ k ]; if (Sum > MaxSum) MaxSum = Sum; } return MaxSum; } Analysis of Algorithms

  25. Maximum Subsequence SumAlgorithm 3 intMaxSubsequenceSum( constint A[], const unsigned int N) { int Sum = 0, MaxSum = 0, Start = 0, End = 0; for( End = 0; End<N; End++) { Sum += A[ End ]; if (Sum > MaxSum) MaxSum = Sum; else if (Sum < 0) { Start= end+1; Sum=0; } } return MaxSum; } Analysis of Algorithms

  26. END Analysis of Algorithms

More Related