Uploaded by Imran Zafar

W02-Maths-Review-Lecture-4-06102022-013136pm

advertisement
Recurrences
W02 - 17
Recurrence Relation
o
A Recurrence Relation relates
the nth element of a sequence
to some of its predecessors.
o Analysis of recursive algorithms requires
forming and solving recurrences.
W02 - 18
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
1
Basic Recurrences
Case 1: This recurrence arises for a recursive
algorithm that loops through the input to
eliminate one item:
T(n) = T(n-1) + n,
T(1) = 1
for n > 1
W02 - 19
Basic Recurrences
Case 2: This recurrence arises for a
recursive algorithm that halves the input in
one step:
T(n) = T(n/2) + 1,
T(1) = 0
for n > 1
W02 - 20
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
2
Basic Recurrences
Case 3: This recurrence arises for a
recursive algorithm that makes a linear
pass through the input, before, during, or
after it is split into two halves.
T(n) = 2T(n/2) + n, for n > 1
T(1) = 0
W02 - 21
Solving Recurrences
1. Substitution Method
2. The Recursion Tree
3. The Master Theorem
W02 - 22
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
3
Solution Case # 1
T(n) = T(n-1) + n
But, T(n-1) = T(n-2) + n-1, from above
Substituting back in (1):
T(n) = T(n-2) + n-1 + n
Keep going:
T(n) = T(n-2) + n-1 + n
but, T(n-2) = T(n-3) + n-2
T(n) = T(n-3) + n-2 + n-1 + n
One more:
T(n) = T(n-4) + n-3 + n-2 + n-1 + n
(1)
(2)
(3)
(4)
W02 - 23
Unwinding the Recurrence
Result at ith unwinding
i
T(n) = T(n-1) + n
1
T(n) = T(n-2) + n-1 + n
2
T(n) = T(n-3) + n-2 + n-1 + n
3
T(n) = T(n-4) + n-3 + n-2 + n-1 + n
4
W02 - 24
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
4
Solution Case # 1
An expression for the kth unwinding:
T(n) = T(n-k) + n-(k-1) +……n-1 + n
Let’s decide to stop at T(1)
T(n) = T(1) + 2 + 3 + …+ n-1 +n
n
 i  1  2   n 
Given T(1) = 1, so
T(n) = 1 + 2 + 3 + …+ n-1 + n
= n(n+1)/2
i 1
n(n  1)
2
W02 - 25
Solution Case # 2
T(n) = T(n/2) + 1,
for n > 1 and T(1) = 0
(1)
As T(n/2) = T(n/4) + 1, so
T(n) = T(n/4) + 1 + 1
T(n) = T(n/4) + 2
(2)
T(n/4) = T(n/8) + 1
T(n) = T(n/8) + 1 + 2
T(n) = T(n/8) + 3
(3)
W02 - 26
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
5
Unwinding the Recurrence
Result at ith unwinding
i
T(n) = T(n/2) + 1
=T(n/21) + 1
1
T(n) = T(n/4) + 2
=T(n/22) + 2
2
T(n) = T(n/8) + 3
=T(n/23) + 3
3
T(n) = T(n/16) + 4
=T(n/24) + 4
4
W02 - 27
Solution Case # 2
Expression after kth unwinding:
T(n) = T(n/2k) + k
Let’s stop at T(1)
n/2k = 1  n = 2k
T(n) = T(1) + k
 k = log2n
T(n) = 0 + log2n
T(n) = log2n
W02 - 28
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
6
Solution Case # 3
T(n) = 2T(n/2) + n, for n > 1
T(n) = 2T(n/2) + n
= 2 (2T(n/4) + n/2) + n
= 4T(n/4) + 2n = 22T(n/22) + 2n
= 4 (2T(n/8)+ n/4) +2n
= 8T(n/8) + 3n = 23T(n/23) + 3n
……………….
= 2kT(n/2k) + kn
= 2kT(1) + kn
= nT(1) + n log2 n
= n*0 + n log2 n
= n log2 n
Design and Analysis of Algorithms – Fall 2022
Instructor: Saima Jawad
T(1) = 0
Assume: n = 2k
n = 2k
log2 n = log2 (2k )
log2 n = k log2 2
log2 n = k x 1
log2 n = k
W02 - 29
7
Download