Lecture 1-4 Intro To Algo, Running Time, Asymptotic and Recurrence
Lecture 1-4 Intro To Algo, Running Time, Asymptotic and Recurrence
Algorithm abc(a, b, c)
{
Return (a+b+b*c+(a+b-c)/(a+b)+4.0);
}
2. if n>10 t1 1
4. for i=1 to n do t3 1
0……………………………………………………………….n-1
If element=10 , then best case Ω(1)
Running time
constants c > 0 and n0 ≥ 1 ,
s.t. f(n) ≤ c g(n) for n ≥ n0
f(n) and g(n) are functions
Running time
if limn->∞f(n)/g(n)= c < ∞
Then
f(n) € O(g(n))
Input size
Asymptotic Notation…
For example: f(n) = 3n+2 g(n)=n
f(n)= O(g(n))
f(n) ≤ c. g(n)
3n+2 ≤ c . n
c=4 , n0 = 2
Asymptotic Notation…
For example: f(n) = 3n+2 g(n)=n
f(n)= O(g(n))
limn->∞f(n)/g(n)= c < ∞
= limn->∞(3n+2)/n
= limn->∞(3+2/n)
= limn->∞(3+2/ꝏ)
= 3+ 2/(1/0)
= 3+ 2*0/1
= 3+0
=3<ꝏ
Hence, we can say f(n)=O(g(n))
f(n)=O(n)
Asymptotic Notation…
Learned about the asymptotic notation
Categories worst, average and best cases
Learned worst case complexity representation using Big Oh
notation
Asymptotic Notation…
The Big-Omega (Ω) Notation
Asymptotic lower bound
Running time
constants c > 0 and n0 ≥ 1 ,
s.t. f(n) ≥ c g(n) ≥ 0 for n ≥ n0
f(n) and g(n) are functions
Running time
if limn->∞f(n)/g(n) > 0
Then
f(n) € Ω(g(n))
Input size
Asymptotic Notation
For example: f(n) = 3n+2 g(n)=n
f(n)= Ω(g(n))
f(n) ≥ c. g(n)
3n+2 ≥ c . n
c=1, n0 = 1 n ≥ 1
Asymptotic Notation
The Big-Theta (Θ) Notation
Asymptotically tight bound
Running time
constants c1 & c2 and n0 , s.t.
c1. g(n) ≤ f(n) ≤ c2. g(n)
for n ≥ n0
f(n) and g(n) are functions
Running time
if limn->∞f(n)/g(n) < ∞
Then,
f(n) € Θ(g(n))
Input size
Asymptotic Notation
For example: f(n) = 3n+2 g(n)=n
f(n) = Θ (g(n))
c1.g(n) ≤ f(n) ≤ c2. g(n)
f(n) ≤ c2 . g(n)
3n+2 ≤ c2 . n
c2=4 , n0 ≥ 1
Running time
constants c > 0 and n0 ≥ 1 ,
s.t. f(n) < c g(n) for n ≥ n0
f(n) and g(n) are functions
Input size
Asymptotic Notation
The Little-oh (o) Notation
Asymptotic upper bound
if f & g be the functions then
Running time
if limn->∞f(n)/g(n)= 0
Then
f(n) € o(g(n))
Input size
Asymptotic Notation
The Little-omega (Ω) Notation
Asymptotic lower bound
Running time
constants c > 0 and n0 ≥ 1 ,
s.t. f(n) > c g(n) ≥ 0 for n ≥ n0
f(n) and g(n) are functions
Input size
Asymptotic Notation
The Little-omega (Ω) Notation
Asymptotic lower bound
if f & g be the functions then
Running time
if limn->∞f(n)/g(n) = ∞
Then
f(n) € Ω(g(n))
Input size
Introduction
Recurrence
Solving Methods
Substitution Method
Master Method
Recursion Tree
Iterative Method
Introduction
Recurrence:
A recurrence is an equation or inequality that describes a
function in terms of its value on smaller inputs.
An algorithm contains a recursive call to itself, we can often
describe its running time by a recurrence equation or
recurrence.
Different forms:
T(n)= 2 T(n/2)+f(n)
T(n)=T(n-1)+1
T(n)=T(2n/3)+T(n/3)+f(n)
Recurrence
Substitution Method
Guess a bound and then use mathematical induction to
prove our guess correct.
Recurrence
Substitution Method
The substitution method for solving recurrences comprises
two steps:
Guess the form of the solution.
Use mathematical induction to find the constants and show
that the solution works.
Recurrence
Substitution Method
T(n)=2T(n/2)+n
Guess the solution is: O(n log n)
Prove that T(n)<= c. (n log n) for c>0
T(n)=2T(n/2)+n………………………………..(1)
Compute for T(n/2)<= c. (n/2).log(n/2) and put into (1)
T(n)<= 2. c. (n/2).log(n/2) +n
T(n)<= c. n. log(n/2) +n
T(n)<= c.n. log n –c.n.log2 +n
T(n)<= c.n.log n –c.n + n for c>=1
Recurrence
Substitution Method
T(n)<= c.n.log n –c.n + n for c=1
T(n)<= 1.n.log n -1.n +n= n.log n
T(n)<= 2. n. log n -2. n +n = n.logn + n.logn –n
.
.
.
T(n)=O(n log n)
Recurrence
Substitution Method
T(n)=T(n/3)+T(2n/3)+d. n
Guess the solution is: O(n log n)
Prove that T(n)<= c. (n log n) for c>0
T(n)=T(n/3)+T(2n/3)+n………………………………..(1)
Compute for T(n/3)<= c. (n/3).log(n/3) and
T(2n/3)<=c. (2n/3). log(2n/3) put into (1)
T(n)<= c. n/3. log(n/3)+ c. 2n/3. log(2n/3) + d. n
T(n)<= c. n/3. log n – c. n/3. log 3 + c. 2n/3.log 2n – c. 2n/3
log 3 + d. n
T(n)<= c. n/3. log n – c. n/3. log 3 + c. 2n/3.log 2 + c. 2n/3 log n
– c. 2n/3 log 3 + d. n
Recurrence
Substitution Method
T(n)<= c. n/3. log n – c. n/3. log 3 + c. 2n/3.log 2 + c. 2n/3 logn
– c. 2n/3 log 3 +d. n
<= c.n.log n –c.n log 3 + c. 2n/3 + d. n
<= c. n. log n – c.n.(log 3 -2/3) + d.n
for c>= d/(log3 – 2/3)
Thank You
Keep Learning…
Design & Analysis of Algorithms
Recurrence-Master Method
Thank You
Keep Learning…
Design & Analysis of Algorithms
Recurrence-Recursion Tree
Presented by:
Rajesh Kumar Tripathi
Associate Professor
Dept. of Computer Engineering & Applications, IET,
GLA University, Mathura
Introduction
Recurrence…
Solving Methods…
Substitution
Master Method
Recursion Tree
Iterative Method
Recurrence
Recurrence
Recursion Tree…
T(n)= 3 T(n/4) + c n2
Recurrence
Recursion Tree…
T(n)= 3 T(n/4) + c n2
Total=O(n2)
Recurrence
1
n log 4
2 3
3 cn
1
16
Thank You
Keep Learning…
Design & Analysis of Algorithms
(BCSC0012)
Iterative Method
Presented by:
Rajesh Kumar Tripathi
AssociateProfessor
Dept. of Computer Engineering & Applications, IET,
GLA University, Mathura
Introduction
Recurrence…
Solving Methods…
Substitution Method
Master Method
Recursion Tree
Iterative Method
Recurrence
Iterative Method…T(n)= 2 T(n/2)+ n s.t T(n/2)=2. T(n/2/2)+n/2
T(n)= 2 T(n/2)+ n
T(n)= 2 T(n/2)+ n ………………………………………………….(1)
T(n/2)= 2 T(n/2/2)+ n/2 = 2. T(n/22)+n/2
T(n)= 2 (2. T(n/22)+n/2 )+ n
T(n)= 22 . T(n/22) +n + n
Recurrence
Iterative Method…T(n)= 2 T(n/2)+ n T(n/22)=2. T(n/22/2)+n/22
T(n)= 22 . T(n/22) +n + n ………………………………………..(2)
T(n/22) = 2 .T(n/22/2)+n/22
T(n/22)=2. T(n/23)+n/22 ……………………………………....(3)
T(n)= 22. (2.T(n/23)+n/22) + n + n
T(n)= 23. T(n/23) + 22. n/22 +n + n
T(n)= 23. T(n/23) + n +n + n
T(n)= 23. T(n/23) +3.n
.
.
T(n)= 2i. T(n/2i) +…+ i.n …………………………………….........(4)
Recurrence
Recurrence
Complexity of Algorithm
A()
{
int i=1,s=1;
while(s<=n)
{ i++;
s=s+i;
pf(“Hello”);
}
}
s: 1 3 6 10 15 21 ……….n
i: 1 2 3 4 5 6 ………..k
k*(k+1)/2>n
k=O(n1/2 )