Anda di halaman 1dari 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


Example 1

Equation: W (n) = W ( n2 ) + c · lg n
First question: divide-and-conquer, chip-and-conquer, or
chip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 1

Equation: W (n) = W ( n2 ) + c · lg n
A: Divide-and-conquer
Second question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 1

Equation: W (n) = W ( n2 ) + c · lg n
A: b = 1, c = 2, f (n) = c · lg n
Now, find lg b, lg c, E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 1

Equation: W (n) = W ( n2 ) + c · lg n
lg b = 0, lg c = 1, E = lg b E
lg c = 0, n = 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


Example 1

Equation: W (n) = W ( n2 ) + c · lg n
f (n) is larger; however, there is no x where 1 ∈ o(nx ) and
nx ∈ o(lg n). Thus, neither case 1 nor case 3 applies. However,
since f (n) ∈ Θ(lg n = nE lg1 n), case 2 of the extended Master
Theorem applies.
So, W (n) ∈ Θ(nE lg1+1 n) = Θ(lg2 n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 2

Equation: W (n) = W ( n2 ) + c · n
First question: divide-and-conquer, chip-and-conquer, or
chip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 2

Equation: W (n) = W ( n2 ) + c · n
A: Divide-and-conquer
Second question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 2

Equation: W (n) = W ( n2 ) + c · n
A: b = 1, c = 2, f (n) = c · n
Now, find lg b, lg c, E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 2

Equation: W (n) = W ( n2 ) + c · n
lg b = 0, lg c = 1, E = lg b E
lg c = 0, n = 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


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, and
W (n) ∈ Θ(f (n)) = Θ(n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 3

Equation: W (n) = 2W ( n2 ) + c · n
First question: divide-and-conquer, chip-and-conquer, or
chip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 3

Equation: W (n) = 2W ( n2 ) + c · n
A: Divide-and-conquer
Second question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 3

Equation: W (n) = 2W ( n2 ) + c · n
A: b = 2, c = 2, f (n) = c · n
Now, find lg b, lg c, E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 3

Equation: W (n) = 2W ( n2 ) + c · n
lg b = 1, lg c = 1, E = lg b E
lg c = 1, n = 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


Example 3

Equation: W (n) = 2W ( n2 ) + c · n
Neither is larger; f (n) ∈ Θ(nE ).
Thus, case 2 of the Master Theorem applies, and
W (n) ∈ Θ(f (n) · log n) = Θ(n · lg n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 4

Equation: W (n) = 2W ( n2 ) + c · n · lg n
First question: divide-and-conquer, chip-and-conquer, or
chip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 4

Equation: W (n) = 2W ( n2 ) + c · n · lg n
A: Divide-and-conquer
Second question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 4

Equation: W (n) = 2W ( n2 ) + c · n · lg n
A: b = 2, c = 2, f (n) = c · n · lg n
Now, find lg b, lg c, E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 4

Equation: W (n) = 2W ( n2 ) + c · n · lg n
lg b = 1, lg c = 1, E = lg b E
lg c = 1, n = 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


Example 4

Equation: W (n) = 2W ( n2 ) + c · n · lg n
f (n) is larger; however, you cannot separate f (n) and nE with a
power of n (see example 1).
However, f (n) ∈ Θ(nE log1 n), so case 2 of the extended Master
Theorem applies, and W (n) ∈ Θ(f (n) · log2 n) = Θ(n · lg2 n).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 5

Equation: W (n) = 2W ( n2 ) + c · n2
First question: divide-and-conquer, chip-and-conquer, or
chip-and-be-conquered?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 5

Equation: W (n) = 2W ( n2 ) + c · n2
A: Divide-and-conquer
Second question: What are b, c and f (n)?

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 5

Equation: W (n) = 2W ( n2 ) + c · n2
A: b = 2, c = 2, f (n) = c · n2
Now, find lg b, lg c, E and nE

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples


Example 5

Equation: W (n) = 2W ( n2 ) + c · n2
lg b = 1, lg c = 1, E = lg b E
lg c = 1, n = 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


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, and
W (n) ∈ Θ(f (n)) = Θ(n2 ).

CSCI 5870 - Data Structures and Algorithms Master Theorem Examples

Anda mungkin juga menyukai