Documente Academic
Documente Profesional
Documente Cultură
MSIT
Agenda
6
Guideline 1: Loops
The running time of a loop is, at most, the running time of the
statements inside the loop (including tests) multiplied by the number
of iterations.
7
Guideline 2: Nested loops
Analyse inside out. Total running time is the product of the
sizes of all the loops.
8
Guideline 3: Consecutive
statements
Add the time complexities of each statement.
9
Guideline 4: If-then-else
statements
Worst-case running time: the test, plus either the
then part or the else part (whichever is the larger).
10
Guideline 5: Logarithmic
complexity
11
Theorems
Let d(n),e(n),f(n) and g(n) be functions
mapping non negative integers real. Then
If d(n) is O(f(n)),then ad(n) is O(f(n)),for any
constant a>0
If d(n) is O(f(n)) and e(n) is O(g(n)),then d(n)+e(n)
is O(f(n)+g(n))
If d(n) is O(f(n)) and e(n) is O(g(n)),then d(n).e(n)
is O(f(n) g(n))
If d(n) is O(f(n)) and f(n) is O(g(n)),then d(n) is
O(g(n)).
Contd..
If f(n) is a polynomial of degree d
i.e, f(n)=(a0+a1n+…+ad nd)
then f(n) is O(nd).
log n x
is O(log n) for any fixed x>0
big-Theta
f(n) is θ(g(n)) if there are constants c ’ >
0 and c’’ > 0 and an integer constant n0 ≥
1 such that c’•g(n) > f(n) > c’’•g(n) for n
≥ n0
Contd…
little-oh
f(n) is o(g(n)) if, for any constant c > 0,
there is an integer constant n0 ≥ 0 such
that f(n) ≤ c•g(n) for n ≥ n0
little-omega
f(n) is w(g(n)) if, for any constant c > 0,
there is an integer constant n ≥ 0 such
that f(n) ≥c•g(n) for n ≥n0
Intuition for Asymptotic
Notation
Big-Oh : f(n) is O(g(n)) if f(n) is asymptotically
less than or equal to g(n)
big-Omega : f(n) is W(g(n)) if f(n) is
asymptotically greater than or equal to g(n)
big-Theta :f(n) is Q(g(n)) if f(n) is
asymptotically equal to g(n)
little-oh : f(n) is o(g(n)) if f(n) is asymptotically
strictly less than g(n)
little-omega: f(n) is w(g(n)) if is asymptotically
strictly greater than g(n)
Bubble Sort – O(n)2
main()
{
int a[6]={7,5,3,4,2,1};
int i=0,j=0;
int n=6;
for(j=n-1;j>0;j--)
{
for(i=0;i<j;i++)
{
if(a[i]>a[i+1])
{
int temp=a[i]; //swap
a[i]=a[i+1];
a[i+1]=temp;
}
}
}}
Assignment
Find the complexity of the following Algorithm in
Big-Oh – Binary Search
for (i=1;i<10 ;i++ )
{
for (j=0;j<i ;j++ )
{
if (arr[j]>arr[i])
{
temp=arr[j];
arr[j]=arr[i];
for (k=i;k>j ;k-- )
arr[k]=arr[k-1];
arr[k+1]=temp;
}
}}
References
Fundamentals of Computer Algorithms
Ellis Horowitz,Sartaj Sahni,Sanguthevar
Rajasekaran
Algorithm Design
Jeffrey J. McConnell
THANK YOU