Lecture 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Design and Analysis of

Algorithm
Lecture 3: Recursion

Dr. Tauheed Ahmed


Contents
Asymptotic notation

Recursion

2
Properties of asymptotic notations
Transitivity

Reflexivity
Symmetry
Questions
(1) (2) (3) Let k and m are constants

Which of the following is Which of the following is (a)


true true (b)
(a) (a) (c)
(b) (b)
(c) (c) Which of the following is
not correct.

(a) (a) (c)


Analyzing Complexity
main()
{
a=b+c
How many times a main()
for (i=1;i≤n;i+1)
particular {{ main()
a=b+c;
x=y+z;{
statement is } for (i=1;i≤n;i+1)
a=b+c;
executed while for{ (i=1;i≤n;i+1)
}
running. { x=y+z;
}for (j=1;j≤n;j+1)
} {
p=q+r;
} } }
Analyzing Complexity

main() 8⇒1 16 ⇒ 1
{ 4⇒2 8⇒ 2
int n; 4⇒3
while(n>1) 2⇒3 2⇒ 4
{ 1⇒?? 1⇒??
n=n/2
}
} Complexity=
Problem 1 Problem 2 Problem 3
𝑚𝑎𝑖𝑛() main()
{ {
𝑓𝑜𝑟 (𝑖=1;𝑖≤𝑛;) for (i=1;i≤n;)
{ {
𝑓𝑜𝑟 (𝑗=1;𝑗≤𝑛;) for(j=1;j≤i^2;)
{ {
𝑓𝑜r for(k=1;k≤133;)
(𝑘=1;𝑘≤133;) {
{ x=y+a
𝑥=𝑦+𝑎 }
} }
} }
} }
}
Analyzing Insertion Sort
Worst Case 𝑂 ( 𝑛2 )

Best Case O(n)

Average Case 𝑂 ( 𝑛2 )
Recursion
Definition
A function calling itself to solve a particular problem is called a
recursion

e.g.
Properties of recursion
•Recursive step
•Termination condition
•There should be change in its parametric value

e.g.

You might also like