Chapter 4: Divide and Conquer
Divide and Conquer
Divide and Conquer (Normally, they can be ignored.)
Divide and Conquer Methods for solving recurrences: Substitution Method: Guess a bound and use Mathematical induction to prove. Master Method: Memorize three cases and use them to solve recurrences of the form: T(n) = a T(n/b) + f(n) Iteration Method: However, it is too easy to make an error in parenthesization, and that recursion trees give a better intuitive idea than iterating the recurrence of how the recurrence progresses. Recursion-tree Method: Converts the recurrence into a tree whose nodes represent the cost incurred at various levels of recursion. We use techniques for bounding summations to solve the recurrence.
The Maximum-subarray problem Consider investing in stock market. You can buy one unit of stock only one time and then sell it at a later date, buying and selling after close of the trading day. Suppose you have a “Cristal Ball” to see the price of the stock in the future. What is your strategy?
The Maximum-subarray problem Should you always buy at the lowest or sell at the highest? Consider: We need to find the nonempty, contiguous subarray of array A whose values have the largest sum.
The Maximum-subarray problem Three possibilities for the location of the maximum-subarray with respect to the midpoint (the divide point):
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication Note: We can partition matrices without copying entries by instead using index calculations. It would take only constant time, instead of Θ( 𝑛 2 ) time. However, the asymptotic analysis won’t change when we use either technique.
Strassen’s algorithm for matrix multiplication n x n
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication
Strassen’s algorithm for matrix multiplication To see how these computations work, expand each right-hand side, replacing each Pi with the sub-matrices of A and B that form it, and cancel terms:
Strassen’s algorithm for matrix multiplication
Substitution method 1. Guess the solution. 2. Use induction to find the constants and show that the solution works.
Substitution method
Substitution method
Substitution method
Substitution method
Substitution method Remedy: Subtract off a lower-order term.
Recursion Tree Use to generate a guess. Then verify by substitution method. Consider the recurrence 𝑇 𝑛 =3𝑇 𝑛 4 +𝑐 𝑛 2
Recursion Tree Note: not a complete binary tree. Depth for leftmost branch: Subproblem size for a node at depth i is 1 3 𝑖 𝑛 . Therefore, 1 3 𝑖 𝑛 = 1 → i = log3n Depth for the rigtmost: Subproblem size for a node at depth i is 2 3 𝑖 𝑛 . Therefore, 2 3 𝑖 𝑛 = 1 → i = 𝑙𝑜𝑔 3/2 𝑛
Recursion Tree
Recursion Tree
Recursion Tree
Master Method Let a, b, and k be integers satisfying a ≥ 1, b ≥ 2, and k ≥ 0 n/b can be either floor or ceiling function. Floor: T(0) = u is given Ceiling: T(1) = u is given Examples: T(n) = 4T(n/2) + n T(n) ? T(n) = 4T(n/2) + n2 T(n) ? T(n) = 4T(n/2) + n3 T(n) ?
Master Method F(n) is polynomially smaller than 𝑛 log 𝑏 𝑎 F(n) is the same size as 𝑛 log 𝑏 𝑎 F(n) is polynomially larger than 𝑛 log 𝑏 𝑎 . It should also satisfy the regularity condition
Master Method F(n) is polynomially smaller than 𝑛 log 𝑏 𝑎 F(n) is the same size as 𝑛 log 𝑏 𝑎 F(n) is polynomially larger than 𝑛 log 𝑏 𝑎 . It should also satisfy the regularity condition
Master Method
Master Method