3-Divide and Conquer
3-Divide and Conquer
1
Introduction
• Divide and Conquer algorithms consist of two parts:
• Divide: Smaller problems are solved recursively (except, of course, the base cases).
• Conquer: The solution to the original problem is then formed from the solutions to the
subproblems.
• Examples
• Binary Search
• Quick Sort
• Merge Sort
• Long Integer Multiplication (Karatsuba’s Multiplication]
• Maximum Sum Subarray
• Strassen’s Algorithm for Matrix Multiplication etc.
• Closest Pair Problem
• Min- max problem
2
Integer Multiplication
1011
x1111
• The standard integer multiplication routine of 2 n-digit 1011
numbers 10110
101100
• Involves n multiplications of an n-digit number by a single +1011000
digit 10100101
• Plus the addition of n numbers, which have at most 2 n digits
quantity time
1) Multiplication n-digit by 1-digit n O(n)
2) Additions 2n-digit by n-digit max n O(n)
4
Integer Multiplication
• So for multiplying any n-bit integers I and J
• We can split up I into (Ih * 2n/2) + Il
• And J into (Jh * 2n/2) + Jl
• Then we get
• I x J = [(Ih x 2n/2) + Il] x [(Jh x 2n/2) + Jl]
• I x J = Ih x Jh x 2n + (Il x Jh + Ih x Jl) x 2n/2 + Il x Jl
8
Integer Multiplication - Example
9
Integer Multiplication - Example
10
Integer Multiplication - Example
11
Integer Multiplication - Example
12
Integer Multiplication - Example
13
Pseudocode
14
Long Integer Multiplication
• Solve
• 1101 x 1011
• 1234 x 2345
15
Maximum Sum Subarray
• The maximum subarray problem is the task of finding
the largest possible sum of a contiguous subarray, within a
given one-dimensional array A[1…n] of numbers.
• The time complexity of the Naive method is O(n^2).
• Using Divide and Conquer approach, we can find the maximum
subarray sum in O(nLogn) time.
16
Maximum Sum Subarray
17
Solution #1 - Brute Force
• The naive approach is to generate all the possible subarray and print that
subarray which has maximum sum.
int MSS (int a[], int n) int MSS (int a[], int n)
{ int max=0 {
for(i=0 to n-1 do) int max=0
for(i=0 to n-1 do)
for(j=i to n-1 do) int s=0
int s=0 for (k=i to n do)
s=s+a[k]
for (k=i to j do)
if(s>max)
s=s+a[k] max= s
if(s>max) return max
}
max= s
return max } O(n2)
O(n3)
18
Solution #2 – Divide and
Conquer
/ Returns sum of maxium sum subarray in aa[l..h]
int maxSubArraySum(int arr[], int l, int h)
{
// Base Case: Only one element
if (l == h)
return arr[l];
21
Maximum Sum Subarray -
Example
• 3, -5, 1, -2, 6, 4, -3 Soln: 10
22
Maximum Sum Subarray -
Example
• 2, -5, 6, -2, -3, 1, 5, -6 Soln: 7
23
Strassen’s Algorithm
24
Strassen’s Algorithm
25
Strassen’s Algorithm
• Strassen showed how two matrices can be multiplied using only 7 multiplications and 18
additions:
• Consider calculating the following 7 products:
• q1 = (a11 + a22) * (b11 + b22)
• q2 = (a21 + a22) * b11
• q3 = a11*( b12 – b22)
• q4 = a22 * (b21 – b11)
• q5 = (a11 + a12) * b22
• q6 = (a21 – a11) * (b11 + b12)
• q7 = (a12 – a22) * (b21 + b22)
27
Example
28
Example
29
Example
30
Example
31
Example
32
Example
33
Example
34
Example
35
Strassen’s Algorithm
36
Strassen’s Algorithm
• No idea how Strassen came up with these combinations.
• He probably realized that he wanted to determine each element in the product
using less than 8 multiplications.
• From there, he probably just played around with it.