AAA Week03 04
AAA Week03 04
Algorithms
Solving Recurrence
Week #3-4
Saima Gul
BACKGROUND
08/03/23 06:54 2
Logarithms
lg lg n lglg n (composition)
08/03/23 06:54 3
Logarithms
a logb n n logb a
b logb a a logb b a
log b (ac) log b a log b c
log c a
log b a
log c b
08/03/23 06:54 4
Summations
n
n(n 1)
k 1
k
2
n 1
n
x 1
k 0
k
x
x 1
08/03/23 06:54 5
RECURRENCES
08/03/23 06:54 6
Recurrence
(1) if n 1
T ( n)
2T (n / 2) (n) otherwise
We will see a number of methods to solve recurrences.
08/03/23 06:54 7
Solution of a Recurrence
08/03/23 06:54 8
Technicalities
While solving recurrences, we will usually neglect some
details.
08/03/23 06:54 10
Why we can assume constant time on small
inputs
When functions are polynomially bounded, the initial
conditions (the value on small inputs) do not make a
difference for the solution in asymptotic notations.
T (n) T (n 1) 1
T (0) 3 T (0) 5
T (1) T (0) 1 3 1 4 T (1) T (0) 1 5 1 6
T (2) T (1) 1 4 1 5 T (2) T (1) 1 6 1 7
T ( n) n 3 T ( n) n 5
T ( n ) ( n ) T ( n ) ( n )
08/03/23 06:54 11
Does matter when exponentially bounded!!!
T (n) T (n / 2)
2
For example:
T (1) 2 T (1) 3 T (1) 1
T (2) T (1) 2 2 T (2) T (1) 32
2 2
?
T (4) T (2) 2 T (4) T (2) 3
2 4 2 4
T (n) (1)
T (8) T (4) 28 T (8) T (4) 38
2 2
T ( n ) ( 2 n ) T (n) (3n )
08/03/23 06:54 12
Paying attention to the technicalities
08/03/23 06:54 13
SOLVING RECURRENCES
08/03/23 06:54 14
Method 1: The Substitution Method
08/03/23 06:54 15
Method 1: The Substitution Method
08/03/23 06:54 16
The Substitution Method: Exact solution
08/03/23 06:54 17
The Substitution Method: Exact solution
08/03/23 06:54 18
Example for the substitution method
T (n) 4T (n / 2) n
3
Step 1 (Guessing): Claim that T ( n ) O ( n )
3
Step 2 (Verification): Use induction to prove T (n) O( n )
08/03/23 06:54 19
Example for the substitution method
08/03/23 06:54 20
Example for the substitution method
T (n) 4T (n / 2) n trick is to try to write it as:
induction “what we want” – “something nonnegative”
hypothesis 3
n c 3
4c n T ( n) n n
2 2
3 c 3
n3 T (n) cn n n cn 3
4c n 2
8 0 if c 2 and n 1
c 3
n n
2 T (n) cn3
08/03/23 06:54 21
Example for the substitution method
2
New claim is: T ( n ) O ( n )
08/03/23 06:54 22
Example for the substitution method
2
So, we have derived: T (n) cn n
08/03/23 06:54 24
Example for the substitution method
c1n 2 c2 n
08/03/23 06:54 25
Showing lower bounds using the substitution
method
Consider again: T (n) 4T (n / 2) n
2
Step1 (Guess and claim): T (n) (n )
2
Step2 (prove by induction): assume T ( k ) ck for k n
T (n) 4T (n / 2) n
2
n
4c n
2 T (n) cn 2
2
cn n
answer something 0
we want
08/03/23 06:54 26
Showing tight bounds using the substitution
method
Note that for
T (n) 4T (n / 2) n
We have shown that:
T (n) O(n 2 ) and T (n) (n 2 )
Therefore:
T ( n ) ( n 2 )
08/03/23 06:54 27
Method 3: The iteration method
08/03/23 06:54 28
Example for the iteration method
iteration level
i 1 T (n) 4T (n / 2) n T (n / 2) 4T (n / 4) n / 2
i 2 T (n) n 4( 4T (n / 4) n / 2)
n 2n 16T (n / 4) T (n / 4) 4T (n / 8) n / 4
i 3 T (n) n 2n 16(4T (n / 8) n / 4)
n 2n 4n 64T (n / 8)
grows as grows as
grows as
2 i 1 4i 2i
Note that when i lg n, T (n / 2i ) T (n / 2lg n ) T (n / n) T (1)
Hence the recursion will bottom out when i lg n
08/03/23 06:54 29
Example for the iteration method
So when i lg n, we will have
T (n) n 2n 4n ... 2 lg n 1 n 4 lg n T (1)
a lg b b lg a
lg n 1
T (n) n 2 k n lg 4T (1) t
x t 1
1
k 0
k 0
k
x
x 1
2lg n 1 2
T (n) n n T (1)
2 1
T (n) n(n 1) n 2 c
Book keeping can be easier if we
T ( n ) ( n 2 ) use “recursion trees” to visualize
the iterations.
08/03/23 06:54 30
Using Recursion Trees total cost
of the level
recursion level T (n) 4T (n / 2) n
l 0: T (n) : n n
l 1: T (n / 2) : n / 2 T (n / 2) : n / 2 T (n / 2) : n / 2 T (n / 2) : n / 2 2n
l 2: T (n / 4) : n / 4 T (n / 4) : n / 4 T (n / 4) : n / 4 T (n / 4) : n / 4 4n
grows as 2l
l lg n 1 : 2lg n 1 n
08/03/23 06:54 31
Method 4: The Master Method
08/03/23 06:54 32
The Master Theorem
T (n) 9T (n / 3) n
a 9 1, b 3 1, f (n) n (asymptotically positive)
- We need n lgb a , let' s calculate it :
n lgb a n lg3 9 n 2
- We need to compare n lgb a with f (n)
f (n) n O(n lgb a ) O(n lg3 9 ) O (n 2 ) for any 0 1
08/03/23 06:54 34
Example for using the master method
T (n) 2T (n / 2) (n) (merge sort)
a 2 1, b 2 1, f (n) (n) (asymptotically positive)
- We need n lgb a , let' s calculate it :
n lg 2 2 n
- We need to compare n lgb a with f (n)
f (n) (n) ( n lgb a )
- Case 2 applies, therefore
T (n) (n lgb a lg n) (n lg n)
08/03/23 06:54 35
Example for using the master method
T (n) 4T (n / 2) n 3
a 4, b 2, f (n) n 3
- We need n lgb a , let' s calculate it : n lg 2 4 n 2
- We need to compare n lgb a with f (n)
f (n) n 3 (n lgb a ) (n 2 )
- Case 3 may apply, we need to check :
?
af (n / b) cf (n)
?
3
4(n / 2) cn 3 T ( n) ( f (n)) (n 3 )
n 3 / 2 cn 3 , for 1 / 2 c 1
08/03/23 06:54 36