0% found this document useful (0 votes)
5 views

Algorithm - Analysis & Big O

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Algorithm - Analysis & Big O

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Algorithm Complexity

Analysis

Engr. Hammad Shahab 1


Recursive vs Non Recursive

#include <iostream> #include <iostream>


using namespace std; using namespace std;
int fact (int n); int fact (int n);
int main( ) { int main( ) {
cout<<fact(3); cout<<fact(3);}
} int fact (int n){
int fact (int n) int i,j;
{ i=1;
if(n<=1) return 1; for(j=2;j<=n;j++){
else i=i*j;
return n*fact(n-1); }
} return i; }

Engr. Hammad Shahab 2


The focus

• How to estimate the time required for a program?


• How to reduce the running time of a program from days or
years to fractions of a second?
• When and when not to use recursion?
• Review of some efficient algorithms for some sample problems.

Engr. Hammad Shahab 3


Asymptotic Notation
• One problem  many solutions
• Which one is best?
• running time? (in seconds, minutes)
• need some way to compare algorithms against one another.
• Asymptotic complexity
• expressing the main component of the cost of an algorithm, using idealized units
of computational work.

Engr. Hammad Shahab 4


Big O Notation

• The most common method and notation for


discussing the execution time
• Often pronounced as Big-Oh notation
• Big-Oh notation gives an upper bound on the
growth rate of a function.
• The statement T(N) = O(f(N)) means that the
growth rate of T(N) is no more than the growth
rate of f(N).
• We can use the big-Oh notation to rank
functions according to their growth rate.
Engr. Hammad Shahab 5
Big O Notation
• The notation Ο(n) is the formal way to express the upper bound of an algorithm's
running time. It measures the worst case time complexity or the longest amount of
time an algorithm can possibly take to complete.

• For example, for a function f(n)


• Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
• In general, the lower bound is the best case (least amount of work performed) and the
upper bound is the worst case.

Engr. Hammad Shahab 6


Other Notations

• T(N) = Ω(g(N)) says that the growth rate of T(N)


is greater than or equal to g(N)
• Omega gives a lower bound on T(N)
• T(N) = Θ(h(N)) says that the growth rate of
T(N) equals (=) the growth rate of h(N)
• T(N) = o(p(N)) says that the growth rate of T(N)
is less than (<) the growth rate of p(N)

Engr. Hammad Shahab 7


Typical growth rates

Function Name
c Constant
Log N Logarithmic
Log2 N Log-squared
N Linear
N log N N log N
N2 Quadratic
N3 Cubic
Nd, d > 3 Polynomial
2N Exponential
Engr. Hammad Shahab 8
N! factorial
Engr. Hammad Shahab 9
Engr. Hammad Shahab 10
Big O Example
• 1000N > N2 for smaller values of N
• However, 1000N ≤ N2 for N ≥ 1000
T(N) ≤ c(f(N)) when N ≥ n0
c = 1, n0 = 1000

Engr. Hammad Shahab 11


More Example
• N3 grows faster than N2 so we can say that
• N2 = O(N3)

Engr. Hammad Shahab 12


A few rules

• In order to denote the growth rate of a function using Big-Oh, ignore


• the lower order terms, and
• the coefficients of the highest-order term
• Example:
• O(2N3 + 3N2 +N) = O(N3)

Engr. Hammad Shahab 13


A few rules

• If T1(N) = O(f(N) and T2(N) = O(g(N)), then


• T1(N) + T2(N) = max(O(f(N)), O(g(N))),
• T1(N) * T2(N) = O(f(N) * g(N))

Engr. Hammad Shahab 14


Running Time Calculations
• Simple for loop
int Sum (int N) {
/* 1 */ int sum = 0;
/* 2 */ for (int i =1; i <= N; i++)
/* 3 */ sum = sum + i * i * i;
/* 4 */ return sum ; }
Q: What is the running time?
Line 1 & 4  2 units of time 2
Line 2  1 unit (initialize) + (N +1) tests + N Increments  2N +
2
Line 3  4 units (1 add, 2 muls., 1 assign) * N executions  4N
Total  6N + 4

A: O(N)
Engr. Hammad Shahab 15
Example:
void count_lines(char* str, int size)
{ int i; // no complexity. It's only a declaration
int c = 0;
for (i = 0; i < size; ++i) // loop executed size times -> O(size)
if (str[i] == '\n') // array access -> O(1)
c++; // simple Variable access -> O(1)
return c; } // simple variable access -> O(1)

Running time??
O(size)

Engr. Hammad Shahab 16


Running Time Calculations

• Nested for loop


int k=0;
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
k++;

Q: What is the running time?


A: O(N2)

Engr. Hammad Shahab 17


Running Time Calculations

• Consecutive loop statments


int a[10]=0; N=10;
for (int i=0; i<N; i++)
a[i]=0;
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
a[i]+=a[j] + i + j;
Q: What is the running time?
A: O(N2)

Engr. Hammad Shahab 18

You might also like