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

## Presentation on theme: "Analysis of Algorithms Rate of Growth of functions Prof. Muhammad Saeed."— Presentation transcript:

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

Analysis of Algorithms2 Rate of Growth of functionsRate of Growth of functions

Analysis of Algorithms3

4

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

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 Series and AsymptoticsSeries and Asymptotics

Series I Analysis of Algorithms8

Series II

Infinite Series Analysis of Algorithms10

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

Analysis of Algorithms12 T(n) = O(f(n)) T(n) = (g(n)) T(n) = (h(n)) Asymptotics

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

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

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

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

General Rules for LoopsLoops Nested LoopsNested Loops Consecutive statementsConsecutive statements if-then-elseif-then-else RecursionRecursion Analysis of Algorithms17

Analysis of Algorithms18 Maximum Subsequence SumMaximum Subsequence Sum A case A case

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 19

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 Algorithms20

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 Algorithms21

END Analysis of Algorithms22

Similar presentations