0% found this document useful (0 votes)
4 views5 pages

Algorithms Section6 1

The document discusses recurrence relations, specifically using the Fibonacci sequence as an example to illustrate the time complexity of recursive algorithms. It provides detailed calculations for the time taken to compute Fibonacci numbers, leading to the conclusion that the time complexity is O(2^(n/2)). Additionally, it includes examples of other recurrence relations and their respective big O notations, such as O(log n) and O(n log n).

Uploaded by

nh696831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Algorithms Section6 1

The document discusses recurrence relations, specifically using the Fibonacci sequence as an example to illustrate the time complexity of recursive algorithms. It provides detailed calculations for the time taken to compute Fibonacci numbers, leading to the conclusion that the time complexity is O(2^(n/2)). Additionally, it includes examples of other recurrence relations and their respective big O notations, such as O(log n) and O(n log n).

Uploaded by

nh696831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithms Section (6)

Section Content:
Recurrence relation

Recurrence relation:
Ex: Fibonacci
0 1 2 3 4 5 6

0 1 1 2 3 5 8

int fib(n){
if(n==0 || n==1){
return n;
}
Else{
Returm fib(n-1)+fib(n-2)
}
}

Fib (4)=1+0+1+1+0=3

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Notes:
Assume that time taken to calculate fib of n is T(n)
We make assumption that each single operation take 1 unite
T(n)= T(n-1) + T(n-2) + C
where C is the constant that represent the unit of time 4
T(n-1) ≅ T(n-2)
T(n-1) & T(n+1) is very close in value, which are approximately equal so, we
have two choices:
 T(n-1)  Upper bound
 T(n-2)  Lower bound

Example 1:
Assume that time taken to calculation fib(n) is T(n), T(n)= T(n-1) + T(n-2) + C,
where C is constant find the big O for the algorithm.
Answer:
Lower case: T(n-2)
T(n)= 2T(n-2) +C K=1  equ (1)
T(n-2) =2T((n-2)-2) +C
T(n) =2(2T(n-4) +C) +C
T(n) =4T(n-4) +2C+C
T(n)=4T(n-4) +3C K=2 (substituted T(n-4) in equ (1))
T(n-4) =2T((n-4) -2) +C
T(n) =4(2T(n-6) +C) +3C
T(n) =8T(n-6) +4C+3C
T(n)=8T(n-6) +7C K=3 (substituted T(n-4) in equ (1))
T(n-6) =2T((n-6)-2) +C
T(n) =8(2T(n-8) +C) +7C
T(n) =16T(n-8) +8C+7C
T(n) =16T(n-8) +15C K=4
General form:
T(n)= 𝟐𝒌 𝑻(𝒏 − 𝟐𝒌) + (𝟐𝒌 − 𝟏)𝑪
n-2k=0  n=2k  k=n/2 substitute in T(n)
T(n)=2(n/2) T(n-(2(n/2))) +( 2(n/2) -1) C
T(n)=2(n/2) T(n-n) +( 2(n/2) -1) C
=2(n/2) T (0) +( 2(n/2) -1) C
= (2(n/2) -1) C
Big O = O ( 2(n/2) )

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Example2:
𝟎 𝒊𝒇 𝒏 = 𝟏

T(n)=
𝑛
𝑇( )+1 𝒊𝒇 𝒏 > 𝟏
2
Answer:
T (1) =0
𝒏
T(n)= T ( )+1 K=1  equ (1)
𝟐
𝑛
𝑛 2
T ( )=T ( )+1
2 2
𝑛
T(n)= T ( )+1+1
4
𝒏
T(n)= T ( )+2 K=2 (substituted T(n/4) in equ (1))
𝟒
𝑛
𝑛 4
T ( )= T ( )+1
4 2
𝑛
T(n)= T ( )+2+1
8
𝒏
T(n)= T ( )+3 K=3 (substituted T(n/8) in equ (1))
𝟖
𝑛
𝑛 8
T ( )= T ( )+1
8 2
𝑛
T(n)= T ( )+1+3
16
𝐧
T(n)= T ( )+4 K=4
𝟏𝟔
General form:
n
T(n)= T ( K)+K
2
n
=1  n=2k  log 2 2k =log 2 n  k=log 2 n
2k
n
T(n)=T ( )+log 2 n
2log2 n
n
= T ( ) +log 2 n
n
= T (1) + log 2 n (where T(1) = 0)
= 0+log 2 n=log 2 n
Big O= O (𝐥𝐨𝐠 𝟐 𝒏)
Remember:
log 𝑎 𝑎 = 1
log 𝑎 𝑎𝑏 = 𝑏 log 𝑎 𝑎 = 𝑏
𝑎log𝑎 𝑏 = 𝑏

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Example 3:

𝟏 𝐢𝐟 𝐧 = 𝟏
T(n)=

𝐧
𝟐𝐓 ( ) + 𝐧 𝐢𝐟 𝐧 > 𝟏
𝟐
Answer:
T (1) =1
𝐧
T(n)= 2T ( )+n K=1  equ (1)
𝟐
n
n 2 n
T ( )=2T ( )+
2 2 2
n n
T(n)= 2(2T ( )+ )+n
4 2
𝐧
T(n)= 4T ( )+2n K=2 (substituted T(n/4) in equ (1))
𝟒
n
n 4 n
T ( )= 2T ( )+
4 2 4
n n
T(n)= 4(2T ( )+ )+2n
8 4
𝐧
T(n)= 8T ( )+3n K=3 (substituted T(n/8) in equ (1))
𝟖
n
n 8 n
T ( )=2 T ( )+
8 2 8
n n
T(n)= 8(2T ( )+ )+3n
16 28
𝐧
T(n)= 16T ( )+4n K=4
𝟏𝟔
General form:
𝑛
T(n)= 2𝑘 T ( 𝐾)+Kn
2
𝑛
=1  n=2𝐾  log 2 2𝑘 = log 2 𝑛  k = log 2 𝑛
2𝐾
𝑛
T(n)= 2log2 𝑛 T ( )+nlog 2 𝑛
2log2 𝑛
𝑛
=n T ( )+ nlog 2 𝑛
𝑛
=n(1)+ nlog 2 𝑛
= n+𝑛 log 2 𝑛
Big O= O (𝐧𝐥𝐨𝐠 𝟐 𝒏)

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Assignment
Example:
Assume that time taken to calculation fib(n) is T(n), T(n)= T(n-1) +
T(n-2) + C, where C is constant find the big O for the algorithm use
upper case.

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed

You might also like