divide and conquerarashr/lecture12.pdfdivide the problem into a number of subproblems conquer the...
TRANSCRIPT
![Page 1: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/1.jpg)
Divide and Conquer
June 2, 2014
Divide and Conquer
![Page 2: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/2.jpg)
Divide the problem into a number of subproblems
Conquer the subproblems by solving them recursively or ifthey are small, there must be an easy solution.
Combine the solutions to the subproblems to the solution ofthe problem
Example:1. Merge-Sort(A, p, r)2. if p < r3. q := b(p + r)/2c4. Merge-Sort(A, p, q)5. Merge-Sort(A, q + 1, r)6. Merge(A, p, q, r)
Initial call for input array A = A[1] . . .A[n] isMerge-Sort(A, 1, n).
Divide and Conquer
![Page 3: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/3.jpg)
Divide the problem into a number of subproblems
Conquer the subproblems by solving them recursively or ifthey are small, there must be an easy solution.
Combine the solutions to the subproblems to the solution ofthe problem
Example:1. Merge-Sort(A, p, r)2. if p < r3. q := b(p + r)/2c4. Merge-Sort(A, p, q)5. Merge-Sort(A, q + 1, r)6. Merge(A, p, q, r)
Initial call for input array A = A[1] . . .A[n] isMerge-Sort(A, 1, n).
Divide and Conquer
![Page 4: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/4.jpg)
Divide the problem into a number of subproblems
Conquer the subproblems by solving them recursively or ifthey are small, there must be an easy solution.
Combine the solutions to the subproblems to the solution ofthe problem
Example:1. Merge-Sort(A, p, r)2. if p < r3. q := b(p + r)/2c4. Merge-Sort(A, p, q)5. Merge-Sort(A, q + 1, r)6. Merge(A, p, q, r)
Initial call for input array A = A[1] . . .A[n] isMerge-Sort(A, 1, n).
Divide and Conquer
![Page 5: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/5.jpg)
Divide the problem into a number of subproblems
Conquer the subproblems by solving them recursively or ifthey are small, there must be an easy solution.
Combine the solutions to the subproblems to the solution ofthe problem
Example:1. Merge-Sort(A, p, r)2. if p < r3. q := b(p + r)/2c4. Merge-Sort(A, p, q)5. Merge-Sort(A, q + 1, r)6. Merge(A, p, q, r)
Initial call for input array A = A[1] . . .A[n] isMerge-Sort(A, 1, n).
Divide and Conquer
![Page 6: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/6.jpg)
Divide the problem into a number of subproblems
Conquer the subproblems by solving them recursively or ifthey are small, there must be an easy solution.
Combine the solutions to the subproblems to the solution ofthe problem
Example:1. Merge-Sort(A, p, r)2. if p < r3. q := b(p + r)/2c4. Merge-Sort(A, p, q)5. Merge-Sort(A, q + 1, r)6. Merge(A, p, q, r)
Initial call for input array A = A[1] . . .A[n] isMerge-Sort(A, 1, n).
Divide and Conquer
![Page 7: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/7.jpg)
Example:
sorted sequence1 2 2 3 4 5 6 7
2 4 5 7 1 2 3 6
2 5 4 7 1 3 2 6
5 2 4 7 1 3 2 6initial sequence
Merge-Sort
splits length-` sequence into two length-`/2 sequencessorts them recursivelymerges the two sorted subsequences
Divide and Conquer
![Page 8: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/8.jpg)
Merge
Merge(A, p, q, r)
Take the smallest of the two front most elements of sequencesA[p..q] and A[q + 1..r ] and put it into a temporary array.
Repeat this, until both sequences are empty. Copy the resultingsequence from temporary array into A[p..r ].
Write a pseudo code for the procedure Merge(A, p, q, r) used inMerge-sort algorithm for merging two sorted arrays A[p . . . q] andA[q + 1 . . . r ] into one.
Divide and Conquer
![Page 9: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/9.jpg)
Merge
Merge(A, p, q, r)
Take the smallest of the two front most elements of sequencesA[p..q] and A[q + 1..r ] and put it into a temporary array.
Repeat this, until both sequences are empty. Copy the resultingsequence from temporary array into A[p..r ].
Write a pseudo code for the procedure Merge(A, p, q, r) used inMerge-sort algorithm for merging two sorted arrays A[p . . . q] andA[q + 1 . . . r ] into one.
Divide and Conquer
![Page 10: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/10.jpg)
MERGE(A, p, q, r)1. n1 := q − p + 1; n2 := r − q;2. for i := 1 to n1
3. L[i ] := A[p + i − 1]4. for i := 1 to n2
5. R[i ] := A[q + i ]6. i := 1; j := 1; k := p7. while i ≤ n1 and j ≤ n2
8. if L[i ] ≤ R[j ]9. A[k] := L[i ]; i := i + 1;10. else11. A[k] := R[j ]; j := j + 1;12. k := k + 1;13. if i > n1
14. while j ≤ n2
15. A[k] := R[j ]; j := j + 1; k := k + 1;16. while i ≤ n1
17. A[k] := L[i ]; i := i + 1; k := k + 1;
Divide and Conquer
![Page 11: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/11.jpg)
Analyzing an D&Q algorithm
Let T (n) be running time on problem of size n
If n is small enough (say, n ≤ c for constant c), thenstraightforward solution takes Θ(1)
If division of problem yields a subproblems, each of which 1/bof original (Merge-Sort: a = b = 2)
Division into subproblems takes D(n)
Combination of solutions to subproblems takes C (n)
Then,
T (n) =
Θ(1) if n ≤ caT (n/b) + D(n) + C (n) otherwise
Divide and Conquer
![Page 12: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/12.jpg)
Analyzing an D&Q algorithm
Let T (n) be running time on problem of size n
If n is small enough (say, n ≤ c for constant c), thenstraightforward solution takes Θ(1)
If division of problem yields a subproblems, each of which 1/bof original (Merge-Sort: a = b = 2)
Division into subproblems takes D(n)
Combination of solutions to subproblems takes C (n)
Then,
T (n) =
Θ(1) if n ≤ caT (n/b) + D(n) + C (n) otherwise
Divide and Conquer
![Page 13: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/13.jpg)
Analyzing an D&Q algorithm
Let T (n) be running time on problem of size n
If n is small enough (say, n ≤ c for constant c), thenstraightforward solution takes Θ(1)
If division of problem yields a subproblems, each of which 1/bof original (Merge-Sort: a = b = 2)
Division into subproblems takes D(n)
Combination of solutions to subproblems takes C (n)
Then,
T (n) =
Θ(1) if n ≤ caT (n/b) + D(n) + C (n) otherwise
Divide and Conquer
![Page 14: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/14.jpg)
Analyzing an D&Q algorithm
Let T (n) be running time on problem of size n
If n is small enough (say, n ≤ c for constant c), thenstraightforward solution takes Θ(1)
If division of problem yields a subproblems, each of which 1/bof original (Merge-Sort: a = b = 2)
Division into subproblems takes D(n)
Combination of solutions to subproblems takes C (n)
Then,
T (n) =
Θ(1) if n ≤ caT (n/b) + D(n) + C (n) otherwise
Divide and Conquer
![Page 15: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/15.jpg)
For Merge-Sort:
a = b = 2D(n) = Θ(1) (just compute “middle” of array)C (n) = Θ(n) (merging has running time linear in length ofresulting sequence)
Thus
TMS(n) =
Θ(1) if n ≤ 1TMS(bn/2c) + TMS(dn/2e) + Θ(n) otherwise.
That’s what’s called a recurrenceBut: we want closed form, i.e., we want to solve the recurrence.
Divide and Conquer
![Page 16: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/16.jpg)
Solving recurrences
There are a few methods for solving recurrences, some easy andnot powerful, some complicated and powerful.Methods:
1 guess & verify (also called“substitution method”)
2 master method3 generating functions
and some others.We’re going to see 1. and 2.
Divide and Conquer
![Page 17: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/17.jpg)
Substitution method
Basic idea:
1 “guess” the form of the solution
2 Use mathematical induction to find constants and show thatsolution works.
Usually more difficult part is the part 1.Back to example: we had
TMS(n) ≤ 2TMS(dn/2e) + Θ(n)
for n ≥ 2.If you hadn’t seen something like this before, how would youguess?There is no general way to guess the correct solutions. It takesexperience.
Divide and Conquer
![Page 18: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/18.jpg)
Heuristics that can help to find a good guess.
One way would be to have a look at first few terms. Say ifwe had T (n) = 2T (n/2) + 3n, then
T (n) = 2T (n/2) + 3n
= 2(2T (n/4) + 3(n/2)) + 3n
= 2(2(2T (n/8) + 3(n/4)) + 3(n/2)) + 3n
= 23T (n/23) + 223(n/22) + 213(n/21) + 203(n/20)
We can do this log n times
2log n · T (n/2log n) +
log(n)−1∑i=0
2i3(n/2i )
= n · T (1) + 3n ·log(n)−1∑
i=0
1
= n · T (1) + 3n log n = Θ(n log n)
After guessing a solution you’ll have to prove the correctness.
Divide and Conquer
![Page 19: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/19.jpg)
Heuristics that can help to find a good guess.
One way would be to have a look at first few terms. Say ifwe had T (n) = 2T (n/2) + 3n, then
T (n) = 2T (n/2) + 3n
= 2(2T (n/4) + 3(n/2)) + 3n
= 2(2(2T (n/8) + 3(n/4)) + 3(n/2)) + 3n
= 23T (n/23) + 223(n/22) + 213(n/21) + 203(n/20)
We can do this log n times
2log n · T (n/2log n) +
log(n)−1∑i=0
2i3(n/2i )
= n · T (1) + 3n ·log(n)−1∑
i=0
1
= n · T (1) + 3n log n = Θ(n log n)
After guessing a solution you’ll have to prove the correctness.Divide and Conquer
![Page 20: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/20.jpg)
similar recurrences might have similar solutionsConsider
T (n) = 2T (bn/2c+ 25) + n
Looks similar to last example, but is the additional 25 in theargument going to change the solution?Not really, because for large n, difference between
T (bn/2c) and T (bn/2c+ 25)
is not large: both cut n nearly in half: for n = 2, 000 we have
T (1, 000) and T (1, 025),
for n = 1, 000, 000 we have
T (500, 000) and T (500, 025).
Thus, reasonable assumption is that nowT (n) = O(n log n) as well.
Divide and Conquer
![Page 21: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/21.jpg)
stepwise refinement – guessing loose lower and upperbounds, and gradually taking them closer to each otherFor T (n) = 2T (bn/2c) + n we see
T (n) = Ω(n) (because of the n term)
T (n) = O(n2) (easily proven)
From there, we can perhaps “converge” on asymptoticallytight bound Θ(n log n).
Divide and Conquer
![Page 22: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/22.jpg)
stepwise refinement – guessing loose lower and upperbounds, and gradually taking them closer to each otherFor T (n) = 2T (bn/2c) + n we see
T (n) = Ω(n) (because of the n term)
T (n) = O(n2) (easily proven)
From there, we can perhaps “converge” on asymptoticallytight bound Θ(n log n).
Divide and Conquer
![Page 23: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/23.jpg)
stepwise refinement – guessing loose lower and upperbounds, and gradually taking them closer to each otherFor T (n) = 2T (bn/2c) + n we see
T (n) = Ω(n) (because of the n term)
T (n) = O(n2) (easily proven)
From there, we can perhaps “converge” on asymptoticallytight bound Θ(n log n).
Divide and Conquer
![Page 24: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/24.jpg)
Proving correctness of a guess
For Merge-Sort we have T (n) ≤ 2T (dn/2e) + Θ(n), which means,there is a constant d > 0 such that
T (n) ≤ 2T (dn/2e) + dn for n ≥ 2.
We guessed that T (n) = O(n log n).We prove T (n) ≤ cn log n for appropriate choice of constant c > 0.
Induction hypothesis: assume that bound holds for any n < m,hence also for n = dm/2e, i.e.,
T (dm/2e) ≤ cdm/2e log(dm/2e)
Divide and Conquer
![Page 25: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/25.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 26: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/26.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 27: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/27.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 28: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/28.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 29: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/29.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 30: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/30.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 31: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/31.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 32: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/32.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 33: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/33.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 34: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/34.jpg)
and prove it for m ≥ 4 (induction step):
T (m) ≤ 2T (dm/2e) + dm
≤ 2(cdm/2e log(dm/2e)) + dm
≤ 2(c(m + 1)/2 · log((m + 1)/2)) + dm
≤ c(m + 1) log((m + 1)/2) + dm
= c(m + 1) log(m + 1)− c(m + 1) log 2 + dm
≤ c(m + 1)(log m + 1/3)− c(m + 1) + dm
= cm log m + c log m + c/3(m + 1)− c(m + 1) + dm
≤ cm log m + cm/2− 2/3c(m + 1) + dm
≤ cm log m + (c/2− 2/3c + d)m = cm log m + (d − c/6)m
≤ cm log m for c ≥ 6d .
Divide and Conquer
![Page 35: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/35.jpg)
Base step
It remains to show that boundary conditions of recurrence (m < 4)are suitable as base cases for the inductive proof.We have got to show that we can choose c large enough s.t.bound
T (m) ≤ cm log m
works for boundary conditions as well (when m < 4).Assume that T (1) = b > 0.For m = 1,
T (m) ≤ cm log m = c · 1 · 0 = 0
is a bit of a problem, because T (1) is a constant greater than 0.
Divide and Conquer
![Page 36: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/36.jpg)
How to resolve this problem?Recall that we wanted to prove that T (n) = O(n log n).Also, recall that by def of O(), we are free to disregard a constantnumber of small values of n: f (n) = O(g(n))⇔there exist constants c , n0 : f (n) ≤ c · g(n) for n ≥ n0
A way out of our problem is to remove difficult boundarycondition T (1) = b > 0 from consideration in inductive proof.
Note: for m ≥ 4, T (m) does not depend directly on T (1)(T (2) ≤ 2T (1) + 2d = 2b + 2d ,T (3) ≤ 2T (2) + 3d = 2(2b + 2d) + 3d = 4b + 7d ,T (4) ≤ 2T (2) + 4d)
This means:We replace T (1) by T (2) and T (3) as the base case in theinductive proof, letting n0 = 2.
Divide and Conquer
![Page 37: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/37.jpg)
In other words, we are showing that the bound
T (n) ≤ cn log n
holds for any n ≥ 2.
For m = 2: T (2) = 2b + 2d? ≤?c .2 log 2 = 2c
For m = 3: T (3) = 4b + 7d? ≤?c .3 log 3
Hence, set c to max(6d , b + d , 4b+7d3 log 3 ) and the above boundary
conditions as well as the induction step will work.
Important:General technique! Can be used very often!
Divide and Conquer
![Page 38: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/38.jpg)
A neat trick called “changing variables”
Suppose we have
T (n) = 2T (√
n) + log n
Now rename m = log n⇔ 2m = n. We know√n = n1/2 = (2m)1/2 = 2m/2 and thus obtain
T (2m) = 2T (2m/2) + m
Now rename S(m) = T (2m) and get
S(m) = 2S(m/2) + m
Looks familiar. We know the solution S(m) = Θ(m log m).Going back from S(m) to T (n) we obtain
T (n) = T (2m) = S(m) = Θ(m log m) = Θ(log n log log n)
Divide and Conquer
![Page 39: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/39.jpg)
A neat trick called “changing variables”
Suppose we have
T (n) = 2T (√
n) + log n
Now rename m = log n⇔ 2m = n. We know√n = n1/2 = (2m)1/2 = 2m/2 and thus obtain
T (2m) = 2T (2m/2) + m
Now rename S(m) = T (2m) and get
S(m) = 2S(m/2) + m
Looks familiar. We know the solution S(m) = Θ(m log m).Going back from S(m) to T (n) we obtain
T (n) = T (2m) = S(m) = Θ(m log m) = Θ(log n log log n)
Divide and Conquer
![Page 40: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/40.jpg)
A neat trick called “changing variables”
Suppose we have
T (n) = 2T (√
n) + log n
Now rename m = log n⇔ 2m = n. We know√n = n1/2 = (2m)1/2 = 2m/2 and thus obtain
T (2m) = 2T (2m/2) + m
Now rename S(m) = T (2m) and get
S(m) = 2S(m/2) + m
Looks familiar. We know the solution S(m) = Θ(m log m).
Going back from S(m) to T (n) we obtain
T (n) = T (2m) = S(m) = Θ(m log m) = Θ(log n log log n)
Divide and Conquer
![Page 41: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/41.jpg)
A neat trick called “changing variables”
Suppose we have
T (n) = 2T (√
n) + log n
Now rename m = log n⇔ 2m = n. We know√n = n1/2 = (2m)1/2 = 2m/2 and thus obtain
T (2m) = 2T (2m/2) + m
Now rename S(m) = T (2m) and get
S(m) = 2S(m/2) + m
Looks familiar. We know the solution S(m) = Θ(m log m).Going back from S(m) to T (n) we obtain
T (n) = T (2m) = S(m) = Θ(m log m) = Θ(log n log log n)
Divide and Conquer
![Page 42: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/42.jpg)
Some basic algebraSums
a + (a + 1) + (a + 2) + · · ·+ (b − 1) + b =∑b
i=a i = (a+b)(b−a+1)2
a + a · c + a · c2 + · · ·+ a · cn−1 + a · cn =∑n
i=0 a · c i = a · 1−cn+1
1−c
if 0 < c < 1 then we can estimate the sum as follows∑ni=0 a · c i = a · 1−cn+1
1−c < a · 11−c
Divide and Conquer
![Page 43: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/43.jpg)
Some basic algebraSums
a + (a + 1) + (a + 2) + · · ·+ (b − 1) + b =∑b
i=a i = (a+b)(b−a+1)2
a + a · c + a · c2 + · · ·+ a · cn−1 + a · cn =∑n
i=0 a · c i = a · 1−cn+1
1−c
if 0 < c < 1 then we can estimate the sum as follows
∑ni=0 a · c i = a · 1−cn+1
1−c < a · 11−c
Divide and Conquer
![Page 44: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/44.jpg)
Some basic algebraSums
a + (a + 1) + (a + 2) + · · ·+ (b − 1) + b =∑b
i=a i = (a+b)(b−a+1)2
a + a · c + a · c2 + · · ·+ a · cn−1 + a · cn =∑n
i=0 a · c i = a · 1−cn+1
1−c
if 0 < c < 1 then we can estimate the sum as follows∑ni=0 a · c i = a · 1−cn+1
1−c < a · 11−c
Divide and Conquer
![Page 45: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/45.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 46: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/46.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 47: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/47.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 48: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/48.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 49: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/49.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 50: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/50.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 51: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/51.jpg)
logarithms and powers
loga n and an are inverse functions to each other:
loga(an) = n for all n
aloga n = n for all n > 0
properties:loga(b · c) = loga b + loga c
loga b = logc blogc a
loga b = 1logb a
alogc b = blogc a
Divide and Conquer
![Page 52: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/52.jpg)
Example. T (n) = 3T (bn/4c) + Θ(n2)summing the rows of the tree we get
T (n) =cn2 + 316cn2 + ( 3
16)2cn2 + · · ·+ ( 316)log4 n−1cn2 + Θ(nlog4 3)
<1
1− 3/16cn2 + Θ(nlog4 3)
=1613cn2 + Θ(nlog4 3) ∈ O(n2)
log4 3 = 1.262, hence nlog4 3 ∈ o(n2) and so
T (n) ∈ O(n2)
then prove by the substitution method (MI) that the guess O(n2)is correct
Divide and Conquer
![Page 53: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/53.jpg)
The “Master Method”
Recipe for recurrences of the form
T (n) = aT (n/b) + f (n)
with a ≥ 1 and b > 1 constant, and f (n) an asymptoticallypositive function (f (n) = 5, f (n) = c log n, f (n) = n, f (n) = n12
are just fine).Split problem into a subproblems each of size n/b.Subproblems are solved recursively, each in time T (n/b).Dividing problem and combining solutions of subproblems iscaptured by f (n).Deals with many frequently seen recurrences (in particular, ourMerge-Sort example with a = b = 2 and f (n) = Θ(n)).
Divide and Conquer
![Page 54: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/54.jpg)
Theorem
Let a ≥ 1 and b > 1 be constants, let f (n) be a function, and letT (n) be defined on the nonnegative integers by the recurrence
T (n) = aT (n/b) + f (n),
where we interpret n/b to mean either bn/bc or dn/be. Then T (n)can be bounded asymptotically as follows.
1 If f (n) = O(n(logb a)−ε) for some constant ε > 0, thenT (n) = Θ(nlogb a).
2 If f (n) = Θ(nlogb a), thenT (n) = Θ(nlogb a · log n).
3 If f (n) = Ω(n(logb a)+ε) for some constant ε > 0, and ifa · f (n/b) ≤ c · f (n) for some constant c < 1 and allsufficiently large n, then T (n) = Θ(f (n)).
Divide and Conquer
![Page 55: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/55.jpg)
Notes on Master’s Theorem
2. If f (n) = Θ(nlogb a), thenT (n) = Θ(nlogb a · log n).
Note 1: Although it’s looking rather scary, it really isn’t. Forinstance, with Merge-Sort’s recurrence T (n) = 2T (n/2) + Θ(n)we have nlogb a = nlog2 2 = n1 = n, and we can apply case 2.The result is therefore Θ(nlogb a · log n) = Θ(n log n).
1 If f (n) = O(n(logb a)−ε) for some constant ε > 0, thenT (n) = Θ(nlogb a).
Note 2: In case 1,
f (n) = n(logb a)−ε = nlogb a/nε = o(nlogb a) ,
so the ε does matter. This case is basically about “small”functions f . But it’s not enough if f (n) is just asymptoticallysmaller than nlogb a (that is f (n) ∈ o(nlogb a), it must bepolynomially smaller!
Divide and Conquer
![Page 56: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/56.jpg)
Notes on Master’s Theorem
2. If f (n) = Θ(nlogb a), thenT (n) = Θ(nlogb a · log n).
Note 1: Although it’s looking rather scary, it really isn’t. Forinstance, with Merge-Sort’s recurrence T (n) = 2T (n/2) + Θ(n)we have nlogb a = nlog2 2 = n1 = n, and we can apply case 2.The result is therefore Θ(nlogb a · log n) = Θ(n log n).
1 If f (n) = O(n(logb a)−ε) for some constant ε > 0, thenT (n) = Θ(nlogb a).
Note 2: In case 1,
f (n) = n(logb a)−ε = nlogb a/nε = o(nlogb a) ,
so the ε does matter. This case is basically about “small”functions f . But it’s not enough if f (n) is just asymptoticallysmaller than nlogb a (that is f (n) ∈ o(nlogb a), it must bepolynomially smaller!
Divide and Conquer
![Page 57: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/57.jpg)
3. If f (n) = Ω(n(logb a)+ε) for some constant ε > 0, and ifa · f (n/b) ≤ c · f (n) for some constant c < 1 and allsufficiently large n, then T (n) = Θ(f (n)).
Note 3: Similarly, in case 3,
f (n) = n(logb a)+ε = nlogb a · nε = ω(nlogb a) ,
so the ε does matter again. This case is basically about “large”functions n. But again, f (n) ∈ ω(nlogb a) is not enough, it must bepolynomially larger. And in addition f (n) has to satisfy the“regularity condition”:
af (n/b) ≤ cf (n)
for some constant c < 1 and n ≥ n0 for some n0.
Divide and Conquer
![Page 58: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/58.jpg)
3. If f (n) = Ω(n(logb a)+ε) for some constant ε > 0, and ifa · f (n/b) ≤ c · f (n) for some constant c < 1 and allsufficiently large n, then T (n) = Θ(f (n)).
Note 3: Similarly, in case 3,
f (n) = n(logb a)+ε = nlogb a · nε = ω(nlogb a) ,
so the ε does matter again. This case is basically about “large”functions n. But again, f (n) ∈ ω(nlogb a) is not enough, it must bepolynomially larger. And in addition f (n) has to satisfy the“regularity condition”:
af (n/b) ≤ cf (n)
for some constant c < 1 and n ≥ n0 for some n0.
Divide and Conquer
![Page 59: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/59.jpg)
The idea is that we compare nlogb a to f (n).
Result is (intuitively) determined by larger of two.
In case 1, nlogb a is larger, so result is Θ(nlogb a).
In case 3, f (n) is larger, so result is Θ(f (n)).
In case 2, both have same order, we multiply it by a logarithmicfactor, and result is Θ(nlogb a · log n) = Θ(f (n) · log n).
Important: Does not cover all possible cases.
For instance, there is a gap between cases 1 and 2 whenever f (n)is smaller than nlogb a but not polynomially smaller.
Divide and Conquer
![Page 60: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/60.jpg)
The idea is that we compare nlogb a to f (n).
Result is (intuitively) determined by larger of two.
In case 1, nlogb a is larger, so result is Θ(nlogb a).
In case 3, f (n) is larger, so result is Θ(f (n)).
In case 2, both have same order, we multiply it by a logarithmicfactor, and result is Θ(nlogb a · log n) = Θ(f (n) · log n).
Important: Does not cover all possible cases.
For instance, there is a gap between cases 1 and 2 whenever f (n)is smaller than nlogb a but not polynomially smaller.
Divide and Conquer
![Page 61: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/61.jpg)
The idea is that we compare nlogb a to f (n).
Result is (intuitively) determined by larger of two.
In case 1, nlogb a is larger, so result is Θ(nlogb a).
In case 3, f (n) is larger, so result is Θ(f (n)).
In case 2, both have same order, we multiply it by a logarithmicfactor, and result is Θ(nlogb a · log n) = Θ(f (n) · log n).
Important: Does not cover all possible cases.
For instance, there is a gap between cases 1 and 2 whenever f (n)is smaller than nlogb a but not polynomially smaller.
Divide and Conquer
![Page 62: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/62.jpg)
The idea is that we compare nlogb a to f (n).
Result is (intuitively) determined by larger of two.
In case 1, nlogb a is larger, so result is Θ(nlogb a).
In case 3, f (n) is larger, so result is Θ(f (n)).
In case 2, both have same order, we multiply it by a logarithmicfactor, and result is Θ(nlogb a · log n) = Θ(f (n) · log n).
Important: Does not cover all possible cases.
For instance, there is a gap between cases 1 and 2 whenever f (n)is smaller than nlogb a but not polynomially smaller.
Divide and Conquer
![Page 63: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/63.jpg)
The idea is that we compare nlogb a to f (n).
Result is (intuitively) determined by larger of two.
In case 1, nlogb a is larger, so result is Θ(nlogb a).
In case 3, f (n) is larger, so result is Θ(f (n)).
In case 2, both have same order, we multiply it by a logarithmicfactor, and result is Θ(nlogb a · log n) = Θ(f (n) · log n).
Important: Does not cover all possible cases.
For instance, there is a gap between cases 1 and 2 whenever f (n)is smaller than nlogb a but not polynomially smaller.
Divide and Conquer
![Page 64: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/64.jpg)
Using the master theoremSimple enough. Some examples:
T (n) = 9T (n/3) + n
We have a = 9, b = 3, f (n) = n. Thus, nlogb a = nlog3 9 = n2.Clearly, f (n) = O(nlog3(9)−ε) for ε = 1, so case 1 givesT (n) = Θ(n2).
T (n) = T (2n/3) + 1
We have a = 1, b = 3/2, and f (n) = 1, sonlogb a = nlog2/3 1 = n0 = 1.
Apply case 2 (f (n) = Θ(nlogb a) = Θ(1), result is T (n) = Θ(log n).
Divide and Conquer
![Page 65: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/65.jpg)
Using the master theoremSimple enough. Some examples:
T (n) = 9T (n/3) + n
We have a = 9, b = 3, f (n) = n. Thus, nlogb a = nlog3 9 = n2.Clearly, f (n) = O(nlog3(9)−ε) for ε = 1, so case 1 givesT (n) = Θ(n2).
T (n) = T (2n/3) + 1
We have a = 1, b = 3/2, and f (n) = 1, sonlogb a = nlog2/3 1 = n0 = 1.
Apply case 2 (f (n) = Θ(nlogb a) = Θ(1), result is T (n) = Θ(log n).
Divide and Conquer
![Page 66: Divide and Conquerarashr/lecture12.pdfDivide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be an easy](https://reader035.vdocument.in/reader035/viewer/2022070221/613790c30ad5d2067648b3e4/html5/thumbnails/66.jpg)
T (n) = 3T (n/4) + n log n
We have a = 3, b = 4, and f (n) = n log n, sonlogb a = nlog4 3 = O(n0.793).
Clearly, f (n) = n log n = Ω(n) and
thus also f (n) = Ω(nlogb(a)+ε)
for ε ≈ 0.2. Also,a · f (n/b) = 3(n/4) log(n/4) ≤ (3/4)n log n = c · f (n) for anyc = 3/4 < 1.
Thus we can apply case 3 with result T (n) = Θ(n log n).
Divide and Conquer