Top Banner
COT 6405 Introduction to Theory of Algorithms Topic 4. Recurrences 8/29/2016 1
33

COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Jul 08, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

COT 6405 Introduction to Theory of Algorithms

Topic 4. Recurrences

8/29/2016 1

Page 2: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Recurrences

• What is a recurrence?

– An equation that describes a function in terms of its value on smaller functions

• The time complexity of divide-and-conquer algorithms can be expressed as recurrences

8/29/2016 2

Page 3: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Recurrence Examples

8/29/2016 3

Page 4: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Solving the recurrences

• Substitution method

• Recursion Tree

• Master method

8/29/2016 4

Page 5: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Substitution method

• The substitution method comprises two steps:

– 1. Guess the form of the solution

– 2. Use mathematical induction to show the correctness of the guess

5

Page 6: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Substitution method

6

Page 7: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Substitution method (cont’d)• We generally express the solution by

asymptotic notations

• We don’t worry about boundary cases, nor do we show base cases in the substitution proof.

– because we are ultimately interested in an asymptotic solution to a recurrence, it will always be possible to choose base cases that work.

7

Page 8: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

8

Guess T(n) = Θ 𝑛𝑙𝑔𝑛Prove: T(n) = O(nlgn) and Ω(𝑛𝑙𝑔𝑛)

Page 9: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

9

Guess T(n) = Θ 𝑛𝑙𝑔𝑛Prove: T(n) = O(nlgn) and Ω(𝑛𝑙𝑔𝑛)≥

Page 10: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Substitution method

• For the substitution method:

– Show the upper and lower bounds separately.

• Might need to use different constants for each.

• Making a good guess

– Unfortunately, there is no general way to guess the correct solutions to recurrences.

– Takes experience and creativity.

10

Page 11: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

11

How to fix this?

Guess T(n) = Θ(𝑛3)Prove: T(n) = O(𝑛3) and Ω(𝑛3)

Page 12: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

12

Page 13: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Avoiding Pitfalls

• It is easy to err in the use of asymptotic notation

• Solve T(n) = 2T(n/2) + Θ(n)

• Guess: T(n) = O(n) and T(n) ≤ dn for some positive constant number d

• Induction: T(n) ≤ 2T(n/2) + cn

≤ 2(d(n/2)) + cn

≤ dn + cn = (d+c)n = O(n)

Why wrong?8/31/2016 14

Page 14: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Changing variables

• Sometimes, a little algebraic manipulations can make an unknown recurrence similar to one you have seen before.

• Solve the recurrence 𝑇 𝑛 = 2𝑇( 𝑛) + 𝑙𝑔𝑛

– Renaming m = 𝑙𝑔𝑛 yields 𝑇(2𝑚) = 2𝑇(2𝑚/2) + m

– We can now rename S(m) = 𝑇 2𝑚 to produce the new recurrence 𝑆(𝑚) = 2𝑆(𝑚/2) + m

– S(m) = Θ(𝑚𝑙𝑔𝑚)

– T(n) = 𝑇(2𝑚) = S(m) = Θ(𝑚𝑙𝑔𝑚) = Θ(𝑙𝑔𝑛𝑙𝑔𝑙𝑔𝑛)

8/29/2016 15

Page 15: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Recursion tree method

• How to solve the recurrence of merge sort?

• By using substitution method, we can have

– T(n) = 2T(n/2) + n

= 2(2T(n/4) + n/2) + n

= 4T(n/4) + 2n

= …….

8/29/2016 16

Page 16: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Recursion tree method (cont’d)

• An alternative approach: draw a tree to diagram all the recursive calls that take place

T(n) = 2T(n/2) + n

• For the original problem, we have a cost of n, plus the two subproblems, each costing n/2

8/29/2016 17

Page 17: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Constructing the tree

8/29/2016 18

n

n/2 n/2

n/4 n/4 n/4 n/4

For each of the size-n/2 subproblems, we have a

cost of n/2, plus two subproblems, each costing

n/4

Page 18: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Constructing the tree (cont’d)

8/29/2016 19

n

n/2 n/2

n/4 n/4 n/4 n/4

T(1)

……

lgn

Page 19: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Constructing the tree (cont’d)

8/29/2016 20

n

n/2 n/2

n/4 n/4 n/4 n/4

T(1)

……

lgn

20 ∗ 𝑛

21 ∗𝑛

2

2𝑙𝑔𝑛*0

22 ∗𝑛

22

Page 20: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Computing the cost• We add up the costs over all levels to

determine the cost for the entire tree

• 𝑇 𝑛 = 20 ∗ 𝑛 + 21∗𝑛

2+22 ∗

𝑛

22+…….+ 2𝑙𝑔𝑛∗ 0

= nlgn = Θ(𝑛𝑙𝑔𝑛)

8/29/2016 21

Page 21: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Example• Solve T(n) = 3T(n/4) + c𝑛2

8/29/2016 22

c𝑛2

c(𝑛

4)2 c(

𝑛

4)2 c(

𝑛

4)2

c(𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2 c(

𝑛

16)2

T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1)

…… ……

……

……

Page 22: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Example(cont’d)

• The subproblem size for a node at depth iis 𝑛/4𝑖

• The subproblem size hits T(1), when 𝑛/4𝑖 = 1, or 𝑖 = log4 𝑛

• Thus, tree has 1+log4 𝑛 levels (i = 0,1,…log4 𝑛)

8/29/2016 23

Page 23: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Example(cont’d)

• Each node at level i has a cost of c(𝑛/4𝑖)2

• Each level has 3𝑖 nodes

• Thus, the total cost of level i is 3𝑖c(𝑛/4𝑖)2 = c𝑛2(3/16)𝑖

8/29/2016 24

Page 24: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Example(cont’d)

• The bottom level has 3log4 𝑛 = 𝑛log4 3nodes, each costing T(1)

• Assume T(1) is a constant. The total cost of the bottom level will be

T(1) 𝑛log4 3 = Θ(𝑛log4 3)

8/29/2016 25

Page 25: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Total cost

• The total cost of level i is c𝑛2(3/16)𝑖

• The total cost of the bottom level Θ(𝑛log4 3)

• We add up the costs over all levels to determine the total cost for the entire tree:

T(n) = c𝑛2+3

16c𝑛2+(

3

16)2c𝑛2 +⋯+

3

16

log4 𝑛−1c𝑛2 + Θ(𝑛log4 3)

= σ𝑖=0log4 𝑛−1(

3

16)𝑖c𝑛2 +Θ(𝑛log4 3)

=3

16

log4 𝑛−1−1

3

16−1

c𝑛2 + Θ(𝑛log4 3)

8/29/2016 26

Page 26: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

How to simplify the answer

T(n) = σ𝑖=0log4 𝑛−1(

3

16)𝑖c𝑛2 +Θ(𝑛log4 3)

≤ σ𝑖=0∞ (

3

16)𝑖c𝑛2 +Θ(𝑛log4 3)

= 1

1−3

16

c𝑛2 + Θ(𝑛log4 3) = 16

13c𝑛2 +Θ(𝑛log4 3)

= O(𝑛2)

8/29/2016 27

Page 27: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

How to simplify the answer (cont’d)

• On the other hand,

T(n) = 3T(n/4) + c𝑛2 ≥ c𝑛2

Thus, T(n) = Ω(𝑛2) and we conclude that

T(n) = Θ(𝑛2)

How to use substitution method to verify?

8/29/2016 28

Page 28: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise

• Solve T(n) = aT(n/b) + f(n)

8/29/2016 29

Page 29: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise (cont’d)

8/29/2016 30

f(n)

f(𝑛

𝑏)

T(1)

……

……

……

……

f(𝑛

𝑏2) ……

Page 30: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise (cont’d)

• The subproblem size for a node at depth iis 𝑛/𝑏𝑖

• The subproblem size hits T(1), when 𝑛/𝑏𝑖 = 1, or 𝑖 = log𝑏 𝑛

• Thus, tree has 1+log𝑏 𝑛 levels (i = 0,1,…log𝑏 𝑛)

8/29/2016 31

Page 31: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise (cont’d)

• Each node at level i has a cost of f(𝑛/𝑏𝑖)

• Each level has 𝑎𝑖 nodes

– Level 0: 1, level 1: a, level 2: 𝑎2, level 3: 𝑎3….

• Thus, the total cost of level i is 𝑎𝑖f(𝑛/𝑏𝑖)

8/29/2016 32

Page 32: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise (cont’d)

• The bottom level has 𝑎log𝑏 𝑛 = 𝑛log𝑏 𝑎nodes, each costing T(1)

• Assume T(1) is a constant. The total cost of the bottom level will be

T(1)𝑛log𝑏 𝑎= Θ(𝑛log𝑏 𝑎)

8/29/2016 33

Page 33: COT 6405 Introduction to Theory of Algorithmsyliu21/Algorithm/Lecture 4.pdf · asymptotic notations •We don’t worry about boundary cases, nor do we show base cases in the substitution

Exercise (cont’d)• We add up the costs over all levels to

determine the total cost for the entire tree:

T(n) = f(n) + af(𝑛/𝑏)+𝑎2f(𝑛/𝑏2) +⋯+ 𝑎log𝑏 𝑛−1f(𝑛/𝑏log𝑏 𝑛−1) + Θ(𝑛log𝑏 𝑎)

= σ𝑖=0log𝑏 𝑛−1𝑎𝑖f(𝑛/𝑏𝑖) +Θ(𝑛log𝑏 𝑎)

8/29/2016 34