DAA M1 Recurrence
DAA M1 Recurrence
5
• Solve : T (n) = 1 if n=1
= 2T (n-1) if n>1
Iteration Method
• This method iteratively unfold the recurrence relation until we get a pattern
T(n) = 2T(n-1)
T(n-1 ) = 2T(n-1-1) Substitute this
=2[2T(n-2)] = 4T(n-2) = 22 T(n-2)
=4[2T(n-3)] = 8T(n-3) = 23 T(n-3)
…………….
=2i T(n-i) ----- Equation 1
• Find a value such a way that the recursive in the pattern becomes base condition
• n-i = 1 🡪 i = n-1
• Put n-i = 1 in Equation 1 : 2i T(1) = 2i
• Convert the pattern variable back to n 6
• = 2n-1 = O( 2n )
• Solve T (n) = T (n-1) +1 and T (1) = θ (1).
• T (n) = 1+ T (n-1)
= 1+ [1+ T (n-2)] = 2 + T(n-2)
= 2+[1+T(n-3)] = 3+T(n-3)
….
= k + T(n-k) put n- k =1
= k + T(1)
= K + θ (1) Substitute k = n -1
= n-1 + θ (1)
= θ (n).
7
• T(n) = 8T(n/2) +n2 , T(1) = 1
T(n) = n2 + 8T(n/2)
= n2 +8 [(n/2)2 +8T(n/4)]
= n2 + 8 n2 /4 + 82 T(n/ 4)
= n2 +2 n2 + 82 [(n/4)2 + 8 T(n/8) ]
= n2 +2 n2 + 82 n2 / 42 + 83 T(n/ 8 )
= n2 +2 n2 + 22 n2 + 83 T(n/ 8 )
= n2 +2 n2 + 22 n2 + 83 T(n/ 23 )
…….
=n2 +2 n2 + 22 n2 +….. + 2k-1 n2 + 8k T(n/ 2k)
8
•
https://youtu.be/UXNRcFIZsjo
10
Recursion Tree Method
• visual representation of recursive call hierarchy where each node represents the cost of a single
subproblem
• For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is the non recursive function then :
• F(n) is the cost of splitting or combining the sub problems.
• A recursion tree is a graphical representation of iteration method.
• In this method, we draw a recurrence tree and calculate the time taken by every level of tree.
• In general, we consider the second term (non recurrence ) in recurrence equation as root.
• Find the sum of cost in every level of tree
• Finally, we sum the work done at all levels.
• To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a
pattern among levels.
11
• The pattern is typically a arithmetic or geometric series.
• Solve T(n) = 2T(n/2) + n : using recursion tree method
T(n/2) T(n/2) 12
• Solve T(n) = 2T(n/2) + n : using recursion tree method
n
T(n/2) T(n/2)
13
T(n/2) = 2T(n/4) + n/2
n
n
n/2 n/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
• Apply n/4 in our equation :
• T(n/4) = 2T(n/4 /2) + n/4 = 2T(n/8) + n/4
• Cost corresponding to T(n/2) is n/2 and the recursive parts are T(n/8), T(n/8)
14
n n
n/2 n/2 n
n/4 n/4
n/4 n/4 n
…
…
…
…
…
…
T(1) T(1) T(1) T(1) T(1)
T(1) T(1) T(1) n
15
• Subproblem size at level i is: n/2i
• Subproblem size hits 1 when n/2i =1 ⇒ n = 2i ⇒ logn = log 2i ⇒ i = log n
• There are log n levels
• Cost of the problem at level i = n
• Total cost will be logn times n
• Solution is : O(n logn)
16
• Solve T(n) = 2T(n/2) + c : using recursion tree method
c
T(n/2) T(n/2)
17
c
c
c c
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
• Apply n/4 in our equation :
• T(n/4) = 2T(n/4 /2) + C = 2T(n/8) + C
• Cost corresponding to T(n/2) is C and the recursive parts are T(n/8), T(n/8)
18
C
C
C 2C
C
C C
C C 4C
…
…
…
…
…
…
T(1) T(1) T(1) T(1) T(1)
T(1) T(1) T(1)
19
•
20
•
21
•
• r =½<1
• Then GP is 1/ (1-r)
22
Solve T(n) = T(n/3) + T(2n/3) + cn
23
cn
cn
24
• Subproblem size at level i is: n/3i and 2n/3i
• n/3i converges to 1 before 2n/3i
• For the best case we can take n/3i as base case
• Subproblem size hits 1 when 1 = n/3i ⇒ i = log3n
• Or Elements from shortest path are being divided by 3, so length of this path will be equal
to log3n
• Sum will be cn+cn+cn … , log3n times
• = c n log3n
• =Ω(n logn)
25
• Solve T(n) = T(n/4) + T(n/2) + n2: using recursion tree method
T(n/4) T(n/2) 26
• Solve T(n) = T(n/4) + T(n/2) + n2: using recursion tree method
n2
T(n/4) T(n/2)
28
n
2
(n/4) (n/2)
2 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
…
…
…
…
29
•
30
31
Homework
Using Recursion Tree method, solve. Assume constant time for small values of n.
T(n)= 2T(n/10)+ T(9n/10)+n
DESIGN AND ANALYSIS OF ALGORITHMS, LECTURE - #7, Recursion tree method to solve
recurrence relations - YouTube
32
Substitution Method
• The substitution method for solving recurrences is described using two steps:
– Guess the form of the solution.
– Use induction to show that the guess is valid.
• For example consider the recurrence T(n) = 2T(n/2) + n.
• We guess the solution as T(n) = O(nLogn).
• Now we use induction to prove our guess.
• We need to prove that T(n) <= cnLogn.
Here, T(n) = 2T(n/2) + n [ T(n/2) = c n/2 Log(n/2) ]
= 2 [ c * n/2 * log(n/2)]+n
= c*n*log(n/2)+n
= cnLogn - cnLog2 + n
= cnLogn - cn + n
<= c nLogn 33
O(nLogn)
• T (n) = T +n
34
Exercise -3
•
35
• T(n) = T(n/2)+f(n) =O(logn)
• T(n) = 2T(n/2)+f(n) =O(nlogn)
36
Master Theorem
• The master theorem is used for calculating the time complexity of recurrence relations (divide and
conquer algorithms) in a simple and quick way.
• The master method is a formula for solving recurrence relations of the form:
• T(n) = aT(n/b) + f(n), where,
n = size of input
a = number of subproblems in the recursion
n/b = size of each subproblem. All subproblems are assumed to have the same size.
f(n) = cost of the work done outside the recursive call, which includes the cost of dividing the problem
and the cost of merging the solutions
Here, a ≥ 1 and b > 1 are constants, and f(n) is an asymptotically positive function. An asymptotically
positive function means that for a sufficiently large value of n, we have f(n) > 0.
Then T (n) has the following asymptotic bounds:
37
Masters Theorem
Case 1: if f(n) = O(nlogba -ε) for some ε > 0, then: T(n) = Θ(nlogba)
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
T(n) = Θ(f(n))
38
Master Method
• Provides a “cookbook” method for solving recurrences
• Recurrence must be of the form: T(n) = aT(n/b) + f(n)
• where a≥1 and b>1 are constants and f(n) is an asymptotically positive function.
Idea: compare f(n) with nlogba
40
• Solve T(n) = 2T(n/2) + n using Master method
• a =2 , b = 2 , f(n) = n
f(n) < nlogba 🡪 T(n) = Θ(nlogba)
• nlogba = nlog22
f(n) = nlogba 🡪 Θ(nlogba lgn)
= n*1 = n
n log a f(n) > nlogba 🡪 T(n) = Θ(f(n)), Regularity
Compare b & f(n)
• n log a
b = f(n) = n 🡪 Case 2
• T(n) = Θ(nlogba lgn)
= Θ(n lgn)
41
Solve T(n) = 2T(n/2) + n2 using master method
a = 2, b = 2,
nlogba = nlog22 = n*1 = n
Compare nlogba with f(n) = n2
n2 ?n
43
Example -4
• Solve 9T(n/3)+ n
• a =9 , b =3 , f(n) = n
• nlogba = nlog39 = nlog332 f(n) < nlogba 🡪 T(n) = Θ(nlogba)
= n2 log33 [log33 =1] f(n) = nlogba 🡪 Θ(nlogba lgn)
= n2
f(n) > nlogba 🡪 T(n) = Θ(f(n)), Regularity
• Compare f(n) and nlogba
• n < n2 , f(n) < nlogba case 1.
• T(n) = Θ(nlogba)
• T(n) = Θ (n2 )
44
•
45
•
46
Tutorial
•
47