260 likes | 374 Views
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.
E N D
Analysis of Algorithms Rate of Growth of functions Prof. Muhammad Saeed
Rate of Growth of functions Analysis of Algorithms
Running Times • Assume N = 100,000 and processor speed is 1,000,000 operations per second Analysis of Algorithms
Series and Asymptotics Analysis of Algorithms
Series I Analysis of Algorithms
Series II Analysis of Algorithms
Infinite Series Analysis of Algorithms
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
T(n) = (h(n)) T(n) = O(f(n)) T(n) = (g(n)) Asymptotics Analysis of Algorithms
Relative Rates of Growth Analysis of Algorithms
Relative Growth Rate ofTwo Functions Compute using L’Hopital’s Rule • Limit=0: f(n)=o(g(n)) • Limit=c0: f(n)=(g(n)) • Limit=: g(n)=o(f(n)) Analysis of Algorithms
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
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
General Rulesfor • Loops • Nested Loops • Consecutive statements • if-then-else • Recursion Analysis of Algorithms
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
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
Insertion Sort A case Analysis of Algorithms
Insertion Sort Best Case: T(n)=O(n) Worst Case: T(n)=O(n2) Analysis of Algorithms
Maximum Subsequence Sum A case Analysis of Algorithms
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
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
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
END Analysis of Algorithms