You are on page 1of 23

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (20CST-282)
Prepared By: Dr. Vipin Tiwari (E10753)

03/07/2022 DISCOVER . LEARN . EMPOWER


1
Contents
• OVERVIEW

•  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

• Amount of computer memory required during


program execution.
• Instruction space – space required to store the
code.
• Fixed space requirement – input independent
• Variable space requirement – input dependent
• S = F(I) otherwise S = C

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

•  The amount of computer time that it needs to run


• to completion.
•  It is determined without considering the following
• information-
•  The machine we are executing,
•  Its machine language instruction set,
•  The time required by each machine instruction,
•  The translation, a compiler/interpreter will make from
• the source code to machine language.
7
Examples

•  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

• Instructions / code Step Count


• x=x+1 constant, c
• int main(){
• printf (“Chandigarh UNIVERSITY”);
• return 0;
•} ?
• for(i=1 ; i<=n ; i++)
• x=x+1; n
• for(i=1 ; i<=n ; i++)
• for(j=1 ; j<=n ; j++)
• x=x+1; n2

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

• T(n) = 1+(n+1)+n+1 = 2n+3 ≈ O(n)


• if n=0 then T(n) = 1+1 ≈ O(1)

10
Example-2

• 1. int a=0,b=1,c, i, n=5; ------------- 1


• 2. printf(“%d %d”,a,b); ------------- 1
• 3. for(i=1 ; i<=n-2 ; i++ ){ ------------- (n-2)+1 = (n-1)
• 4. c=a+b; ------------- (n-2)
• 5. printf(“%d”,c); ------------- (n-2)
• 6. a=b; ------------- (n-2)
• 7. b=c; } ------------- (n-2)
• T(n)= 1+1+(n-1)+2*(n-2) = 2+n-1+2n-4 = 3n-3 ≈ O(n)

11
Example-3

• 1. int i, j, n=5, a[5]={12, 2, 51, 35, 7};


• 2. for(i=0;i<n-1;i++){
• 3. for(j=0;j<n-1-i;j++)
• 4. if(a[j]>a[j+1]){
• 5. a[j] = a[j] + a[j+1];
• 6. a[j+1] = a[j] - a[j+1];
• 7. a[j] = a[j] - a[j+1]; }
• 8. for(i=0 ; i<n ; i++)
• 9. printf(“%d”, a[i]);
• T(n) = ?
12
ASYMPTOTIC NOTATIONS

•  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

•  Let, performance of X & Y are TX and TY respectively,


•  Conclusion is for a large n, TX(n) ≻ TY (n)
•  So, who is a better professional ?
•  Ans. X
•  When X outperforms Y and by what factor ?
•  Ans. n0 and c

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)

• Since (n+9) > 0 for all values n ≥ 0, then (n-15)=0


•  n0=15
•  Find n0 when c=2 & c=4.

15
NOTATIONS

Omega : 0 ≤ cg(n) ≤ f(n)


Big-oh : 0≤ f(n) ≤ cg(n)

Theta : 0≤ 𝑐1𝑔 𝑛 ≤ f(n) ≤ c2 𝑔 𝑛 )

16
RATE OF GROWTH

17
TYPES OF TIME COMPLEXITY
 Best Case Time complexity
 Exp(1) – Linear search/Binary search

 Linear search, key=3


 a[0]==key, T(n)=O(1)
 Binary search, key=24
 m=(0+4)/2 = 2
 a[2]==key, T(n)=O(1)

18
TYPES OF TIME COMPLEXITY
 Worst Case Time complexity
 Exp(2) – Linear search/Binary search

 Linear search, key=78 / 80


 a[0]==key, T(n)=O(n) / O(n+1) ≈ O(n)

 Binary search, key=78 / 80


 m=(0+4)/2 = 2 …
 a[2]!=key,…
 finally T(n)=O(log2 𝑛) ….(*)

19
TYPES OF TIME COMPLEXITY
 Average Case Time complexity
 Exp(3) – Linear search/Binary search

Linear search, key=24


 a[0]==key, T(n)=O((n+1)/2) ≈ O(n)
 There are n cases that can occur, i.e. find at the first place,
the second place, the third place and so on up to the nth
place. If found at the i th place then i comparisons are
required. Hence the average number of comparisons over
these n cases is:
 average = (1+2+3.....+n)/n = (n+1)/2
 where the result was used that 1+2+3 ...+n is equal to
n(n+1)/2.
 Binary search, key=5
 m=(0+4)/2 = 2, m=(0+1)/2=0, m=(1+1)/2=1
 a[1]==key, finally T(n) ≈ O(log2 𝑛) ….(*) 20
SORTING ALGORITHMS

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

You might also like