CS3343, Fall 2012, Final Exam, 13 Dec 2012 |
Recurrences, solution by Master Method, Examples | |||||||
---|---|---|---|---|---|---|---|
Recurrence | a | b | logb(a) | nlogb(a) | f(n) | Case | Behavior |
a. T(n) = 32 T(n / 2) + n2 log(n) | 32 | 2 | log2(32) = 5 | n5 | n2 log(n) | 1 | Θ(n5) |
b. T(n) = 2 T(n / 4) + √n log(n) | 2 | 4 | log4(2) = 1/2 | n1/2 = √n | √n log(n) | none | Master fails |
c. T(n) = 2 T(n / 2) + n2 | 2 | 2 | log2(2) = 1 | n1 | n2 | 3 | Θ(n2) |
d. T(n) = 16 T(n / 4) + n2 | 16 | 4 | log4(16) = 2 | n2 | n2 | 2 | Θ(n2 log(n)) |
|
![]() |
|
|
![]() |
|
Vertices | m | n | o | p | q | r | s | t | u | v | w | x | y | z | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Initial | 0 | 0 | 2 | 0 | 2 | 3 | 2 | 2 | 2 | 2 | 1 | 2 | 1 | 2 | |
Rm: m | Dec. q,r,x | 0 | 2 | 0 | 1 | 2 | 2 | 2 | 2 | 2 | 1 | 1 | 1 | 2 | |
Rm: n | Dec. o,q,u | 1 | 0 | 0 | 2 | 2 | 2 | 1 | 2 | 1 | 1 | 1 | 2 | ||
Rm: p | Dec. o,s,z | 0 | 0 | 2 | 1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | |||
Rm: o | Dec. r,s,v | 0 | 1 | 0 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | ||||
Rm: q | Dec. t | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |||||
Rm: s | Dec. r | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ||||||
Rm: r | Dec. u,y | 1 | 0 | 1 | 1 | 1 | 0 | 1 | |||||||
Rm: u | Dec. t | 0 | 1 | 1 | 1 | 0 | 1 | ||||||||
Rm: t | none | 1 | 1 | 1 | 0 | 1 | |||||||||
Rm: y | Dec. v | 0 | 1 | 1 | 1 | ||||||||||
Rm: v | Dec. w,x | 0 | 0 | 1 | |||||||||||
Rm: w | Dec. z | 0 | 0 | ||||||||||||
Rm: x | none | 0 | |||||||||||||
Rm: z | none |
|
|
Original tape looks like: L = left segment of 1's, M = middle segment of 0's, R = right segment of 1's All 0's outside these segments, indefinitely far. <--L--><--M--><--R--> ...00011...1100...0011...1100... OK, here is what the program is doing: 1. Print 0 at left end of R. 2-3. Scan right to 1 past right end of R. 4. Print 1 at 1 past right end of R. 5-6. Scan left to 1 past left end of R (= right end of M). 7-8. Scan left to 1 past left end of M (= right end of L). 9. Print 0 at right end of L. 10-11. Scan left to 1 past left end of L. 12. Print 1 at 1 past left end of L. 13-14. Scan right to 1 past right end of L (= left end of M). 15-16. Scan right to 1 past right end of M (= left end of R). 17. Now scanning the 1 at left end of R (same position relative to L, M, and R as at start). Will always goto step 1. Start over.
Effect of 1 time through loop: R and L stay the same length, and all of R moves right one square, and all of L moves left one square, and M has an extra 2 0's.
As it runs, L and R pushed apart. Each loop, M has extra 2 0's.
Initial ...00011100111000... tape: | |
We have the optimal value trapped between bounds L < R, where initially L = 0 and R = M. Set Mid = (L + R)/2. L ("no") Mid (?) R("yes") |--------------|------------| if the bound of Mid gives a "yes" then change to: L ("no") R ("yes) |--------------|------------| and if bound of Mid gives a "no" then change to: L ("no") R ("yes) |--------------|------------| In this way the optimal value is trapped between bounds whose distance apart in halved at each stage. So in log2(M) steps, we will have the optimal value.