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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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