8/2/20151 analysis of algorithms lecture: solving recurrence by recursion-tree method
TRANSCRIPT
![Page 1: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/1.jpg)
04/19/23 1
Analysis of Algorithms
Lecture: Solving recurrence by
recursion-tree method
![Page 2: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/2.jpg)
04/19/23 2
Problem of the day
• How many multiplications do you need to compute 316?
316 =38 x 38
38 =34 x 34
34 =32 x 32
32 =3 x 3
316 =3 x 3 x 3 …. x 3 Answer: 15
Answer: 4
![Page 3: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/3.jpg)
04/19/23 3
Outline
• Review of last lecture – analyzing recursive algorithms– Defining recurrence relation
• Today: analyzing recursive algorithms– Solving recurrence relation
![Page 4: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/4.jpg)
04/19/23 4
Solving recurrence
1. Recursion tree or iteration method- Good for guessing an answer
2. Substitution method- Generic method, rigid, but may be hard
3. Master method- Easy to learn, useful in limited cases only
- Some tricks may help in other cases
![Page 5: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/5.jpg)
04/19/23 5
Recurrence for merge sort
T(n) =(1) if n = 1;
2T(n/2) + (n) if n > 1.
We will usually ignore the base case, assuming it is always a constant (but not 0).
![Page 6: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/6.jpg)
04/19/23 6
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
![Page 7: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/7.jpg)
04/19/23 7
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
T(n)
![Page 8: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/8.jpg)
04/19/23 8
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
T(n/2) T(n/2)
dn
![Page 9: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/9.jpg)
04/19/23 9
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
T(n/4) T(n/4) T(n/4) T(n/4)
dn/2 dn/2
![Page 10: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/10.jpg)
04/19/23 10
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
![Page 11: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/11.jpg)
04/19/23 11
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
![Page 12: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/12.jpg)
04/19/23 12
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
dn
![Page 13: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/13.jpg)
04/19/23 13
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
dn
dn
![Page 14: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/14.jpg)
04/19/23 14
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
dn
dn
dn
…
![Page 15: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/15.jpg)
04/19/23 15
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
dn
dn
dn
#leaves = n (n)
…
![Page 16: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/16.jpg)
04/19/23 16
Recursion tree for merge sort
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/4 dn/4 dn/4 dn/4
dn/2 dn/2
(1)
…
h = log n
dn
dn
dn
#leaves = n (n)
Total(n log n)
…
Later we will usually ignore d
![Page 17: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/17.jpg)
04/19/23 17
Recurrence for computing powerint pow (b, n)
m = n >> 1;p=pow(b,m)*pow(b,m);if (n % 2)
return p * b;else
return p;
int pow (b, n)
m = n >> 1;
p = pow (b, m);
p = p * p;
if (n % 2)
return p * b;
else
return p;
T(n) = T(n/2)+(1) T(n) = 2T(n/2)+(1)
Which algorithm is more efficient asymptotically?
![Page 18: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/18.jpg)
04/19/23 18
Time complexity for Alg1
Solve T(n) = T(n/2) + 1
• T(n) = T(n/2) + 1
= T(n/4) + 1 + 1
= T(n/8) + 1 + 1 + 1
= T(1) + 1 + 1 + … + 1
= Θ (log(n)) log(n)
Iteration method
![Page 19: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/19.jpg)
04/19/23 19
Time complexity for Alg2
Solve T(n) = 2T(n/2) + 1.
![Page 20: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/20.jpg)
04/19/23 20
Time complexity for Alg2
Solve T(n) = 2T(n/2) + 1.
T(n)
![Page 21: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/21.jpg)
04/19/23 21
Time complexity for Alg2
T(n/2) T(n/2)
1
Solve T(n) = 2T(n/2) + 1.
![Page 22: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/22.jpg)
04/19/23 22
Time complexity for Alg2
1
T(n/4) T(n/4) T(n/4) T(n/4)
1 1
Solve T(n) = 2T(n/2) + 1.
![Page 23: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/23.jpg)
04/19/23 23
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…Solve T(n) = 2T(n/2) + 1.
![Page 24: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/24.jpg)
04/19/23 24
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
Solve T(n) = 2T(n/2) + 1.
![Page 25: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/25.jpg)
04/19/23 25
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
1
Solve T(n) = 2T(n/2) + 1.
![Page 26: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/26.jpg)
04/19/23 26
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
1
2
Solve T(n) = 2T(n/2) + 1.
![Page 27: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/27.jpg)
04/19/23 27
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
1
2
4
…
Solve T(n) = 2T(n/2) + 1.
![Page 28: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/28.jpg)
04/19/23 28
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
1
2
4
#leaves = n (n)
…
Solve T(n) = 2T(n/2) + 1.
![Page 29: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/29.jpg)
04/19/23 29
Time complexity for Alg2
1
1 1 1 1
1 1
(1)
…
h = log n
1
2
4
#leaves = n (n)
…
Solve T(n) = 2T(n/2) + 1.
Total(n)
![Page 30: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/30.jpg)
04/19/23 30
More iteration method examples
• T(n) = T(n-1) + 1
= T(n-2) + 1 + 1
= T(n-3) + 1 + 1 + 1
= T(1) + 1 + 1 + … + 1
= Θ (n) n - 1
![Page 31: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/31.jpg)
04/19/23 31
More iteration method examples
• T(n) = T(n-1) + n
= T(n-2) + (n-1) + n
= T(n-3) + (n-2) + (n-1) + n
= T(1) + 2 + 3 + … + n
= Θ (n2)
![Page 32: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/32.jpg)
04/19/23 32
3-way-merge-sort3-way-merge-sort (A[1..n])
If (n <= 1) return;3-way-merge-sort(A[1..n/3]);3-way-merge-sort(A[n/3+1..2n/3]);3-way-merge-sort(A[2n/3+1.. n]);Merge A[1..n/3] and A[n/3+1..2n/3];Merge A[1..2n/3] and A[2n/3+1..n];
•Is this algorithm correct?•What’s the recurrence function for the running
time?•What does the recurrence function solve to?
![Page 33: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/33.jpg)
04/19/23 33
Unbalanced-merge-sort
ub-merge-sort (A[1..n])if (n<=1) return;
ub-merge-sort(A[1..n/3]);
ub-merge-sort(A[n/3+1.. n]);
Merge A[1.. n/3] and A[n/3+1..n].
•Is this algorithm correct?•What’s the recurrence function for the running
time?•What does the recurrence function solve to?
![Page 34: 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d885503460f94a6d6c6/html5/thumbnails/34.jpg)
04/19/23 34
More recursion tree examples
• T(n) = 3T(n/3) + n
• T(n) = T(n/3) + T(2n/3) + n
• T(n) = 3T(n/4) + n
• T(n) = 3T(n/4) + n2