Download presentation
Presentation is loading. Please wait.
Published byAlyssa Rogers Modified over 10 years ago
1
Analysis of Algorithms Rate of Growth of functions Prof. Muhammad Saeed
2
Analysis of Algorithms2 Rate of Growth of functionsRate of Growth of functions
3
Analysis of Algorithms3
4
4
5
nn(log(n))n(log(log(n)))n(log^2(n))n(log(n^2))n^1.5n 10 0011 10-20121 211222 2202442 3415863 472101394 61142022156 81763634238 11261064533711 154015109805715 2060221811219020 27903229617914127 371324747526422237 491936775338634849 6728196117956154667 90406136182781285690 122584191280611691343122 164838268427716772106164 2221198374647323973304222 2991708521973634155182299 40424267251456448538129404 5463440100521677687912750546 7374865139132117972919999737 99568661922473891373131370995 Analysis of Algorithms5
6
Assume N = 100,000 and processor speed is 1,000,000 operations per secondAssume N = 100,000 and processor speed is 1,000,000 operations per second FunctionRunning Time 2N2N over 100 years N3N3 31.7 years N2N2 2.8 hours N*N 1/2 31.6 seconds N log N1.2 seconds N0.1 seconds N 1/2 3.2 x 10 -4 seconds log N1.2 x 10 -5 seconds Running Times Analysis of Algorithms6
7
7 Series and AsymptoticsSeries and Asymptotics
8
Series I Analysis of Algorithms8
9
Series II Analysis of Algorithms9
10
Infinite Series Analysis of Algorithms10
11
Fundamental Definitions Asymptotics Fundamental Definitions Asymptotics T(n) = O(f(n)) if there are constants c and n 0 such that T(n) cf(n) when n n 0T(n) = O(f(n)) if there are constants c and n 0 such that T(n) cf(n) when n n 0 T(n) = (g(n)) if there are constants c and n 0 such that T(n) cg(n) when n n 0T(n) = (g(n)) if there are constants c and n 0 such that T(n) cg(n) when n n 0 T(n) = (h(n)) if and only if T(n) = O(h(n)) and T(n) = (h(n))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)) andT(n) = o(p(n)) if T(n) = O(p(n)) and T(n) (p(n)) Analysis of Algorithms11
12
Analysis of Algorithms12 T(n) = O(f(n)) T(n) = (g(n)) T(n) = (h(n)) Asymptotics
13
Analysis Type Mathematical Expression Relative Rates of Growth Big OT(N) = O( F(N) )T(N) < F(N) Big T(N) = ( F(N) ) T(N) > F(N) Big T(N) = ( F(N) ) T(N) = F(N) Relative Rates of Growth Analysis of Algorithms13
14
Relative Growth Rate of Two Functions Compute using LHopitals Rule Limit=0: f(n)=o(g(n)) Limit=0: f(n)=o(g(n)) Limit=c 0:f(n)= (g(n)) Limit=c 0:f(n)= (g(n)) Limit= :g(n)=o(f(n)) Limit= :g(n)=o(f(n)) Analysis of Algorithms14
15
3n 3 = O(n 3 ) 3n 3 + 8 = O(n 3 ) 8n 2 + 10n * log(n) + 100n + 10 20 = O(n 2 ) 3log(n) + 2n 1/2 = O(n 1/2 ) 2 100 = O(1) T linearSearch (n) = O(n) T binarySearch (n) = O(log(n)) Important Rules Analysis of Algorithms15
16
Important Rules Rule 1: If T 1 (n) = O(f(n)) and T 2 (n) = O(g(n)), then a) T 1 (n) + T 2 (n) = max(O(f(n)), O(g(n))) a) T 1 (n) + T 2 (n) = max(O(f(n)), O(g(n))) b) T 1 (n) * T 2 (n) = O(f(n)*g(n)) Rule 2: If T(x) is a polynomial of degree n, then T(x)= (x n ) Rule 3: log k n = O(n) for any constant k. Analysis of Algorithms16
17
General Rules for LoopsLoops Nested LoopsNested Loops Consecutive statementsConsecutive statements if-then-elseif-then-else RecursionRecursion Analysis of Algorithms17
18
Analysis of Algorithms18 int gcd(int a, int b) { int t; while (b != 0) { t = b; b = a % b; a = t; } return a; } Euclids GCD
19
Analysis of Algorithms19 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
20
Analysis of Algorithms20 Insertion SortInsertion Sort A case A case
21
Analysis of Algorithms21 Insertion Sort Best Case: T(n)=O(n) Worst Case: T(n)= Worst Case: T(n)= O(n 2 )
22
Analysis of Algorithms22 Maximum Subsequence SumMaximum Subsequence Sum A case A case
23
int MaxSubsequenceSum( const int 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; } Maximum Subsequence Sum Algorithm 1 Analysis of Algorithms 23
24
Maximum Subsequence Sum Algorithm 2 int MaxSubsequenceSum( const int A[], const unsigned int N) { int Sum=0, MaxSum=0; for( i = 0; i<N; i++) Sum = 0; Sum = 0; for( j = i; j<N; j++) { Sum += A[ k ]; if (Sum > MaxSum) MaxSum = Sum; } return MaxSum; } Analysis of Algorithms24
25
Maximum Subsequence Sum Algorithm 3 int MaxSubsequenceSum( const int 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 Algorithms25
26
END Analysis of Algorithms26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.