Data Structure - Algorithms - 01
Data Structure - Algorithms - 01
Non-
Linear
Linear
Linked
Array Stack Queue Tree Graph
lists
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Analysis involves –
Tracing the algorithm for logical correctness, implementing
the algorithm and testing it on some data.
Checking the algorithm for simplicity
Definition of Time
• # of seconds (machine, implementation dependent).
• Basic operation: the operation that contributes most towards the running time of the algorithm.
T(n) ≈ copC(n)
Number of times
Running time the basic operation
Execution time is executed
for one basic operation
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Asymptotic Complexity
• Running time of an algorithm as a function of
input size n for large n.
• Expressed using only the highest-order term
in the expression for the exact running time.
– Instead of exact running time, say O(n2).
• Describes behavior of function in the limit.
• Written using Asymptotic Notation.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
• The rate of change of execution time T(n) with the change in the
number of inputs is called growth of function.
Asymptotic Notation
• ɵ, O, W, o, w
• Defined for functions over the natural numbers.
– Ex: f(n) = ɵ(n2).
– Describes how f(n) grows in comparison to n2.
• Define a set of functions; in practice used to
compare two function sizes.
• The notations describe different rate-of-growth
relations between the defining function and the
defined set of functions.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
• 100n + 5 is O(n^2)
• 100n + 5
• ≤ 100n + n, for n ≥ 5
• = 101n ≤ 101n^2, so n0 = 5, c = 101
• Alternatively
• 100n + 5
• ≤ 100n + 5n, for n ≥1
• = 105n ≤ 105n^2, so n0 = 1, c = 105
• n0 and c are not unique!
• Of course, by the same argument, 100n+5 is also O(n)
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
(g(n)) = {f(n) :
positive constants c and n0, such
that n n0,
we have 0 cg(n) f(n)}
Intuitively: Set of all functions whose rate
of growth is the same as or higher than that
of g(n).
g(n) is an asymptotic lower bound for f(n).
f(n) = (g(n)) f(n) = (g(n)).
(g(n)) (g(n)).
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
EXAMPLES:
• int sum(a,n)
• { int s=0;
• for(i=0;i<n;i++)
• s=s+a[i];
• return s;
• }
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
• void Add(A,B,n)
• { for(i=0;i<n;i++)
• { for(j=0;j<n;j++)
• Sum[i,j]=A[i][j]+B[i][j];
• }
• }
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
• void multiply(a,b,n)
• { for(i=0;i<n;i++)
• {for(j=0;j<n;j++)
• { c[i][j]=0;
• for(k=0;k<n;k++)
• { c[i][j]=c[i][j]+a[i][k]*b[k][j];}
• }}}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued
QUESTIONS
1. for(i=0;i<n;i=i+2)
{ stmt;}
2. for(i=0;i<n;i++)
{ for(j=0;j<i;j++)
{ stmt;}
}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued…
3. int p=0;
for(i=1; p<=n; i++)
{ p=p+i;}
4. for(i=1;i<n; i=i*2)
{ stmt;}
5.for(i=n;i>=1;i=i/2)
{ stmt;}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued…
6. for(i=1; i*i< n; i++)
{ stmt;}
7. for(i=0;i<n;i++)
{stmt;}
for(j=0;j<n;j++)
{ stmt;}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
8. p=0;
for(i=1; i<n; i=i*2)
{ p++;}
for(j=1;j< p; j=j*2)
{ stmt;}
9. for(i=0;i<n;i++)
{ for (j=1;j<n;j=j*2)
{ stmt;}
}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
Solutions:
1. O(n)
2. O(n^2)
3. O(sqrt(n))
4. O(log n)
5. O(log n)
6. O(sqrt(n))
7. O(n)
8. O(log (log n))
9. O(n log n)
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Thank You
33