Lecture02-Algorithm Analysis I
Lecture02-Algorithm Analysis I
Lecture 02
Algorithm Analysis I
Mohammed Alqmase
Frequency Count Method
Frequency Count Method
i=0
while(i < n) {
stmt
i++ Time Function f(n): 4n + 2
}
Frequency Count Method i=i+1
𝟏 𝟐𝒏 𝟏
Time Function f(n): 𝟏+ 𝒏 + 𝟏 + + 𝒏 = 𝟐𝒏 + 𝟐
𝟐 𝟐 𝟐
Frequency Count Method i=i*2
Iteration i stmt execution i*=2
For( i=1; i<n; i*=2 )
1 𝟏 = 𝟐𝟎 1 i*2=2
stmt
𝟐𝒌 ≥ 𝒏 2 𝟐 = 𝟐𝟏 1 i*2=4
3 𝟒 = 𝟐𝟐 1 i*2=8
log 𝟐 𝟐𝒌 ≥ log 𝟐 𝒏
4 𝟖 = 𝟐𝟑 1 i * 2 = 16
𝒌log 𝟐 𝟐 ≥ log 𝟐 𝒏 .
.
.
𝒌 ≥ log 𝟐 𝒏 k+1 𝟐𝒌
𝑖∗𝒊<𝒏
𝒊𝟐 < 𝒏
𝒊< 𝒏
function2 ( n ):
For( i=0; i<n; i++ ){
For( a=0; a<n; a++ ) stmt executed: 𝒏𝟐
stmt
}
Frequency Count Method 𝒊 𝒋 No. of 𝒔𝒕𝒎𝒕 Execute
0 0 0
function( n ): 1
0
1
1
For( 𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + + ){ 0
2 1 2
2
For( 𝒋 = 𝟎; 𝒋 < 𝒊 ; 𝒋 + + ){ 0
1
3 3
𝒔𝒕𝒎𝒕 2
3
} .
.
.
} 0
..
n n
.
n
stmt exectue:
𝒏 𝒏 𝒏+𝟏
𝟎 + 𝟏 + 𝟐 + 𝟑 + ⋯+ 𝐧 = 𝐢 = = 𝒎
𝒊=𝟎 𝟐
Frequency Count Method
function( n ):
}
Frequency Count Method
function( n ):
1 n+1 2n
For( 𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + + ){ 𝒏 𝒏+𝟏
= 𝒎
n m+1 2m 𝟐
For( 𝒋 = 𝟎; 𝒋 < 𝒊 ; 𝒋 + + ){
𝒔𝒕𝒎𝒕 m
Time Function 𝒇(𝒏): 𝟒𝒏 + 𝟒𝒎 + 𝟐
}
𝒏 𝒏+𝟏
= 𝟒𝒏 + 𝟒 + 𝟐
} 𝟐
= 𝟒𝒏 + 𝟐𝒏(𝒏 + 𝟏) + 𝟐
= 𝟒𝒏 + 𝟐𝒏𝟐 + 𝟐𝒏 + 𝟐
= 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
Frequency Count Method
𝒇(𝒏) = 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
𝟐
𝒇(𝒏) = 𝑶( 𝒏 )
Asymptotic Notation
Asymptotic Notation
Asymptotic Notation
𝒇(𝒏) = 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
𝟐
𝒇(𝒏) = 𝑶( 𝒏 )
Why Asymptotic Analysis?
Order Big-Theta
Constant Θ(1)
Logarithmic Θ(log 𝑛)
Linear Θ(𝑛)
Quadratic Θ(𝑛2 )
Exponential Θ(2𝑛 )
What is Order of Growth?
𝒇(𝒏) = 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
𝒇(𝒏) = 𝑶( 𝒏𝟐 )
Why Focus on Order of Growth?
𝒇(𝒏) = 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
𝒇(𝒏) = 𝑶( 𝒏𝟐 )
Why Focus on Order of Growth?
❖ Example
𝒇(𝒏) = 𝟐𝒏𝟐 + 𝟔𝒏 + 𝟐
o Dominant term: 𝟐𝒏𝟐
o Lower-order terms: 𝟔𝒏 + 𝟐
o 𝑓(𝑛) ∼ 𝟐𝒏𝟐 as 𝑛 → ∞
Asymptotic Analysis
❖ 𝛺(1)
❖ 𝑂(𝑛)
❖ Θ(𝑛) .