26
Master Theorem Examples CSCI 5870 - Data Structures and Algorithms Robert W. Kramer Department of Computer Science and Information Systems Youngstown State University CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Master Theorem Examples

Embed Size (px)

Citation preview

Page 1: Master Theorem Examples

Master Theorem Examples

CSCI 5870 - Data Structures and Algorithms

Robert W. KramerDepartment of Computer Science and Information Systems

Youngstown State University

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 2: Master Theorem Examples

Example 1

Equation: W (n) = W (n2 ) + c · lgn

First question: divide-and-conquer, chip-and-conquer, orchip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 3: Master Theorem Examples

Example 1

Equation: W (n) = W (n2 ) + c · lgn

A: Divide-and-conquerSecond question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 4: Master Theorem Examples

Example 1

Equation: W (n) = W (n2 ) + c · lgn

A: b = 1, c = 2, f (n) = c · lgnNow, find lgb, lgc , E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 5: Master Theorem Examples

Example 1

Equation: W (n) = W (n2 ) + c · lgn

lgb = 0, lgc = 1, E = lgblgc = 0, nE = 1

Next: which is larger, f (n) or nE ?Also, can you find nx that fits between them?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 6: Master Theorem Examples

Example 1

Equation: W (n) = W (n2 ) + c · lgn

f (n) is larger; however, there is no x where 1 ∈ o(nx) andnx ∈ o(lgn). Thus, neither case 1 nor case 3 applies. However,since f (n) ∈Θ(lgn = nE lg1 n), case 2 of the extended MasterTheorem applies.So, W (n) ∈Θ(nE lg1+1 n) = Θ(lg2 n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 7: Master Theorem Examples

Example 2

Equation: W (n) = W (n2 ) + c ·n

First question: divide-and-conquer, chip-and-conquer, orchip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 8: Master Theorem Examples

Example 2

Equation: W (n) = W (n2 ) + c ·n

A: Divide-and-conquerSecond question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 9: Master Theorem Examples

Example 2

Equation: W (n) = W (n2 ) + c ·n

A: b = 1, c = 2, f (n) = c ·nNow, find lgb, lgc , E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 10: Master Theorem Examples

Example 2

Equation: W (n) = W (n2 ) + c ·n

lgb = 0, lgc = 1, E = lgblgc = 0, nE = 1

Next: which is larger, f (n) or nE ?Also, can you find nx that fits between them?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 11: Master Theorem Examples

Example 2

Equation: W (n) = W (n2 ) + c ·n

f (n) is larger.We can choose n0.5, with 1 ∈ o(n0.5) and n0.5 ∈ o(f (n)). Thus,case 3 of the Master Theorem applies, andW (n) ∈Θ(f (n)) = Θ(n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 12: Master Theorem Examples

Example 3

Equation: W (n) = 2W (n2 ) + c ·n

First question: divide-and-conquer, chip-and-conquer, orchip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 13: Master Theorem Examples

Example 3

Equation: W (n) = 2W (n2 ) + c ·n

A: Divide-and-conquerSecond question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 14: Master Theorem Examples

Example 3

Equation: W (n) = 2W (n2 ) + c ·n

A: b = 2, c = 2, f (n) = c ·nNow, find lgb, lgc , E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 15: Master Theorem Examples

Example 3

Equation: W (n) = 2W (n2 ) + c ·n

lgb = 1, lgc = 1, E = lgblgc = 1, nE = n

Next: which is larger, f (n) or nE ?Also, can you find nx that fits between them?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 16: Master Theorem Examples

Example 3

Equation: W (n) = 2W (n2 ) + c ·n

Neither is larger; f (n) ∈Θ(nE ).Thus, case 2 of the Master Theorem applies, andW (n) ∈Θ(f (n) · logn) = Θ(n · lgn).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 17: Master Theorem Examples

Example 4

Equation: W (n) = 2W (n2 ) + c ·n · lgn

First question: divide-and-conquer, chip-and-conquer, orchip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 18: Master Theorem Examples

Example 4

Equation: W (n) = 2W (n2 ) + c ·n · lgn

A: Divide-and-conquerSecond question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 19: Master Theorem Examples

Example 4

Equation: W (n) = 2W (n2 ) + c ·n · lgn

A: b = 2, c = 2, f (n) = c ·n · lgnNow, find lgb, lgc , E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 20: Master Theorem Examples

Example 4

Equation: W (n) = 2W (n2 ) + c ·n · lgn

lgb = 1, lgc = 1, E = lgblgc = 1, nE = n

Next: which is larger, f (n) or nE ?Also, can you find nx that fits between them?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 21: Master Theorem Examples

Example 4

Equation: W (n) = 2W (n2 ) + c ·n · lgn

f (n) is larger; however, you cannot separate f (n) and nE with apower of n (see example 1).However, f (n) ∈Θ(nE log1 n), so case 2 of the extended MasterTheorem applies, and W (n) ∈Θ(f (n) · log2 n) = Θ(n · lg2 n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 22: Master Theorem Examples

Example 5

Equation: W (n) = 2W (n2 ) + c ·n2

First question: divide-and-conquer, chip-and-conquer, orchip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 23: Master Theorem Examples

Example 5

Equation: W (n) = 2W (n2 ) + c ·n2

A: Divide-and-conquerSecond question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 24: Master Theorem Examples

Example 5

Equation: W (n) = 2W (n2 ) + c ·n2

A: b = 2, c = 2, f (n) = c ·n2

Now, find lgb, lgc , E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 25: Master Theorem Examples

Example 5

Equation: W (n) = 2W (n2 ) + c ·n2

lgb = 1, lgc = 1, E = lgblgc = 1, nE = n

Next: which is larger, f (n) or nE ?Also, can you find nx that fits between them?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Page 26: Master Theorem Examples

Example 5

Equation: W (n) = 2W (n2 ) + c ·n2

f (n) is clearly larger, and n1.5 fits nicely between f (n) and nE .Thus, case 3 of the Master Theorem applies, andW (n) ∈Θ(f (n)) = Θ(n2).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples