Module2-Lecture Divide and Conquer
Module2-Lecture Divide and Conquer
•
The base case for the recursion are
subproblems of constant size
•
Analysis can be done using
recurrence equations
Divide-and-Conquer 2
Approach
DAC(P)
{
if (small (P))
// P is small and solution is obvious
return solution (P);
•
Binary search
•
Binary tree traversals
•
Matrix multiplication: Strassen’s algorithm
General Divide-and-Conquer Recurrence
•
Homework: Revise these algorithms and try to understand why these
are divide and conquer algorithms.
8
Integer Arithmetic 1 1 1 1 1 1 0 1
1 1 0 1 0 1 0 1
Add
+ 0 1 1 1 1 1 0 1
•
Add. Given two n-digit integers a and b, 1 0 1 0 1 0 0 1 0
•
compute a + b.
1 1 0 1 0 1 0 1
• O(n) bit operations. * 0 1 1 1 1 1 0 1
1 1 0 1 0 1 0 1 0
Multiply
0 0 0 0 0 0 0 0 0
•
Multiply. Given two n-digit integers a and b, 1 1 0 1 0 1 0 1 0
compute a × b. 1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
• Brute force solution: (n2) bit operations.
Can we optimize this in any way? 1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0
Integer Multiplication
•
So we haven’t really improved that much,
• Since we went from a O(n2) solution to a O(n2) solution
•
Can we optimize this in any way?
Karatsuba multiplication
Exercise
X =
Idea - Block Matrix Multiplication The idea behind Strassen's algorithm is in the formulation
of matrix multiplication as a recursive problem.
where block matrices Aij are of size n/2 x n/2 (same with respect to block entries of B and C).
The matrices Aij and Bkl are smaller matrices, hence we have broken down our initial problem of multiplying two n x n
matrices into a problem requiring 8 matrix multiplies between matrices of size n/2 x n/2 , as well as a total of 4 matrix
additions.
T(n) = 8T(n/2) + O(n2)
Strassen's algorithm
Reduce the number of sub-calls to matrix-multiplies to 7, using just a bit of algebra.
We write down Cij 's in terms of block matrices Mk's. Each Mk may be calculated simply from products and
sums of sub-blocks of A and B. That is, we let
Source :https://stanford.edu/~rezab/classes/cme323/S16/notes/Lecture03/cme323_lec3.pdf
It can be verified that
O(N2.8074)
Strassen’s Algorithm
> Problem: Given a list of prices on each day, find the days on which to buy & sell that
would have achieved max profit – maximize (sell price – buy price)
Approach#1: Brute-Force
Stock Pricing Problem -Maximum Single-Sell Profit
> Problem: Given a list of prices on each day, find the days on which to buy & sell that
would have achieved max profit – maximize (sell price – buy price)
Task is to find out subarray which has the largest sum. So we need to find out the 2 date indexes (left index and right
index) between which the profit is maximum.
Question: If the array is split into two subarrays, where could the maximum-subarray be located?
Answer: There are three possibilities:
• In the left subarray (Buy date index and sell date index both are in left half of the array)
• In the right subarray (Buy date index and sell date index both are in right half of the array).
• Across the two subarrays (Buy date index in the and left half of the array and sell date index in right half of the
array).
Example
Instead of looking at daily prices. Consider daily change in the price, where change on day i is the difference
between the prices after day (i-1) and after day i.
Given array A, find the nonempty, contiguous subarray of A whose values have the largest sum.
Buy the stock just before day 8(i.e. after day 7) and sell it after day 11, earning a profit of $43 per share.
Closest Pair Problem
Finding closest pair of points
Given a set of points {p1,..pn} find the pair of points{pi,pj} that are
closest together.
•
Brute force gives an algorithm: just check every pair of
points.
•
Can we do it faster?
•
we can find the closest pair in O(n log n) time.
Divide-and-conquer for closest pair
Source : https://www.bowdoin.edu/~ltoma/teaching/cs3250-CompGeom/spring17/Lectures/cg-closestPair.pdf
find vertical line that splits P in half
• let P1, P2 = set of points to the left/right of line
• recursively find closest pair in P1
• recursively find closest pair in P2
Running time?
• T(n) = 2T(n/2) + O(n2) => solves to
Refining the merge
Do we need to examine all pairs (p,q), with p in P 1, q in P2?