Professional Documents
Culture Documents
• Algorithm
• Types of Algorithm complexity
• Space Complexity
• Time Complexity
• Step Counts
• Asymptotic Notations
• Types of Analysis
2
Definition
• Algorithm is a finite set of well defined computational instructions written in a proper
sequence in order to solve a problem.
• Criteria-
• Input
• Output
• Definiteness
• Finiteness
• Effectiveness
3
ANALYZING AN ALGORITHM
• Why is it important?
• Predict the feasibility requirement of your code
• (algorithm).
• Usual requirements
• Execution time
• Memory space
• The complexity of the algorithm is determined in
• terms of space and time.
• Space complexity ← execution time
• Time complexity ← memory space
4
SPACE COMPLEXITY
5
Examples of Algorithms
• EXAMPLES
• Exp(1)
• int main(){
• printf(“Chandigarh UNIVERSITY”);
•}
• Exp(2)
• void bubble(int a[], int n){
• int i,j;
• for(i=0;i<n-1;i++){
• for(j=0;j<n-1-i;j++)
• if(a[j]>a[j+1])
• swap (a[j],a[j+1])
•}
• }.
6
TIME COMPLEXITY
• Exp(1)
• x=x+1;
• Exp(2)
• for(i=1 ; i<=n ; i++)
• x=x+1;
• Exp(3)
• for(i=1 ; i<=n ; i++)
• for(j=1 ; j<=n ; j++)
• x=x+1;
• Our concern should be the order of magnitude/ growth of an algorithm
• for an input n.
8
STEP COUNT
9
Example-1
• 1. int i, f=1, n=5; -------------- 1 time
• 2. for(i=1 ; i<=n ; i++) -------------- n+1 times
• 3. f=f*i; -------------- n times
• 4. printf(“%d”,f); -------------- 1 time
10
Example-2
11
Example-3
• O (Big-oh) Notation
• Consider a function f(n) which is non-negative for all
• integers n=0. we say that f(n)is Big-oh g(n), which we
• write f(n)=O(g(n)), if there exits an integer n0 and a
• constant c>0 such that for all integers n=n0, f(n)=cg(n).
• In other words,
• O(g(n))={f(n): ∃ positive constants c and n0 ∋ 0 ≤ f(n) ≤cg(n) ∀ 𝑛 ≥ 𝑛0 }
• O-notation to give an upper bound on a function to within a
• constant factor.
13
EXAMPLE
• Two students, X & Y
14
EXAMPLE
• Suppose C=1 then, f(n)=6n+135
• f(n) = cn2
• 6n +135 = cn2 = n2 (since c=1)
• 0 = n2 - 6n -135
• 0 = (n-15)(n+9)
15
NOTATIONS
16
RATE OF GROWTH
17
TYPES OF TIME COMPLEXITY
Best Case Time complexity
Exp(1) – Linear search/Binary search
18
TYPES OF TIME COMPLEXITY
Worst Case Time complexity
Exp(2) – Linear search/Binary search
19
TYPES OF TIME COMPLEXITY
Average Case Time complexity
Exp(3) – Linear search/Binary search
21
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald
22
THANK YOU
For queries
Email: vipin.e10753@cumail.in
03/07/2022 23