Download presentation
Presentation is loading. Please wait.
1
Chapter 4: Divide and Conquer
2
Divide and Conquer
3
Divide and Conquer (Normally, they can be ignored.)
4
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.
5
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?
6
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.
7
The Maximum-subarray problem
Three possibilities for the location of the maximum-subarray with respect to the midpoint (the divide point):
8
The Maximum-subarray problem
9
The Maximum-subarray problem
10
The Maximum-subarray problem
11
The Maximum-subarray problem
12
The Maximum-subarray problem
13
The Maximum-subarray problem
14
Strassen’s algorithm for matrix multiplication
15
Strassen’s algorithm for matrix multiplication
16
Strassen’s algorithm for matrix multiplication
17
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.
18
Strassen’s algorithm for matrix multiplication
n x n
19
Strassen’s algorithm for matrix multiplication
20
Strassen’s algorithm for matrix multiplication
21
Strassen’s algorithm for matrix multiplication
22
Strassen’s algorithm for matrix multiplication
23
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:
24
Strassen’s algorithm for matrix multiplication
25
Substitution method 1. Guess the solution.
2. Use induction to find the constants and show that the solution works.
26
Substitution method
27
Substitution method
28
Substitution method
29
Substitution method
30
Substitution method Remedy: Subtract off a lower-order term.
31
Recursion Tree Use to generate a guess. Then verify by substitution method. Consider the recurrence 𝑇 𝑛 =3𝑇 𝑛 4 +𝑐 𝑛 2
32
Recursion Tree Note: not a complete binary tree.
Depth for leftmost branch: Subproblem size for a node at depth i is 𝑖 𝑛 Therefore, 𝑖 𝑛 = 1 → i = log3n Depth for the rigtmost: Subproblem size for a node at depth i is 𝑖 𝑛 . Therefore, 𝑖 𝑛 = 1 → i = 𝑙𝑜𝑔 3/2 𝑛
33
Recursion Tree
34
Recursion Tree
35
Recursion Tree
36
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) ?
37
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
38
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
39
Master Method
40
Master Method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.