Divide and Conquer: Andreas Klappenecker (Based On Slides by Prof. Welch)
Divide and Conquer: Andreas Klappenecker (Based On Slides by Prof. Welch)
Andreas Klappenecker
[based on slides by Prof. Welch]
Divide and Conquer Paradigm
5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
2 5 4 6 1 3 2 6
2 4 5 6 1 2 3 6
1 2 2 3 4 5 6 6
Mergesort Animation
• http://ccl.northwestern.edu/netlogo/m
odels/run.cgi?MergeSort.862.378
Recurrence Relation for
Mergesort
• Ad hoc method:
• expand several times
• guess the pattern
• can verify with proof by induction
• Master theorem
• general formula that works if recurrence has the form
T(n) = aT(n/b) + f(n)
• a is number of subproblems
• n/b is size of each subproblem
• f(n) is cost of non-recursive part
Master Theorem
• computational geometry
• finding closest pair of points
• finding convex hull
• mathematical calculations
• converting binary to decimal
• integer multiplication
• matrix multiplication
• matrix inversion
• Fast Fourier Transform
Strassen’s Matrix Multiplication
Matrix Multiplication
• Consider two n by n matrices A and B
• Definition of AxB is n by n matrix C whose (i,j)-
th entry is computed like this:
• consider row i of A and column j of B
• multiply together the first entries of the rown and
column, the second entries, etc.
• then add up all the products
• Number of scalar operations (multiplies and
adds) in straightforward algorithm is O(n3).
• Can we do it faster?
Divide-and-Conquer
A B = C
A0 A1 B0 B1 A0B0+A1B2 A0B1+A1B3
=
A2 A3 B2 B3 A2B0+A3B2 A2B1+A3B3
A B = C
A0 A1 B0 B1 C11 C12
=
A2 A3 B2 B3 C21 C22
P1 = (A11+ A22)(B11+B22)
C11 = P1 + P4 - P5 + P7
P2 = (A21 + A22) * B11
C12 = P3 + P5
P3 = A11 * (B12 - B22)
C21 = P2 + P4
P4 = A22 * (B21 - B11)
C22 = P1 + P3 - P2 + P6
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
Strassen's Matrix Multiplication