Week 5 Divide and Conquer
Week 5 Divide and Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
• Example:
Consider the array: [-2, 1, -3, 4, -1, 2, 1, -5, 4].
The maximum subarray is [4, -1, 2, 1], with a sum of `6`.
Brute Force Approach: A simple way to solve this problem is to
try all possible subarrays and calculate their sums, then
choose the one with the maximum sum. However, this
approach has a time complexity of O(n^2) and is not efficient
for large arrays.
Divide: Split the array into two halves: [-2, 1, -3, 4] and [ -1, 2, 1, -5, 4].
Combine: Find the maximum subarray sum that crosses the midpoint,
which is [4, -1, 2, 1].
• Thus:
T(n) = 2T(n/2) + O(n)
T(n) = O(n log n)
Karatsuba’s Multiplication Algorithm
• Karatsuba's multiplication algorithm is a fast
multiplication algorithm that was discovered by
Anatolii Karatsuba in 1960. It is a divide-and-
conquer algorithm designed to efficiently multiply
two large numbers. The traditional grade-school
multiplication algorithm has a time complexity of
O(n^2), where n is the number of digits in the
numbers being multiplied. Karatsuba's algorithm
improves this to O(n^log2(3)), which is
approximately O(n^1.585).
Karatsuba’s Multiplication Algorithm
• Lets suppose we have two arrays
– a: [1,0,0,0…..1] of n bit size
– b: [1,0,1,0,…..1] of n bit size
– And for simplicity, n is a power of 2
• Using Divide and Conquer, divide the arrays a,
b in two parts, i.e. a1 a2
a: [1,0,0,0…..1]
b: [1,0,1,0,…..1]
b1 b2
Continued.
• By dividing, a1, a2, b1, and b2 contains n/2
bits in each.
• Now perform multiplication, i.e.,
• a*b= 2^n[a_1]*[b_2]+2^n/2[a_1][b_2]+[a_2]
[b_1].
• Now if a and b are one bit, then perform one-
bit multiplication
• But, if a and b are more than one bit
Continued.
• Then, recursively split a and b in two parts.
• Perform four multiplications, i.e.,
a_1 b_1, a_1 b_2, a_2 b_1, a_2 b_2.
• Perform four n/2 bit multiplications
• Add one n-bit padding
• Add one n/2 bit padding
• So the recurrence relations is:
Continued.
T(n)= Theta (1) , if n=1
T(n) = 4T(n/2) + Theta (n) , if n>1
Where Theta(n) is complexity for padding
• This complexity is high, therefore, we are
going to convert this four multiplication to
three multiplications and this conversion is
known as Karatsuba’s multiplication.
Continued.
• Change that four multiplies into three
multiplies, 2+1, three multiplies,
• Add a bunch of adds subtract padding,
• all of which is Theta n. Therefore,
• We get a recurrence which looks like
T(n) is 3T (n/2)+Theta n
• By solving, the complexity is n^log23
Which of the following is a divide and conquer
application for the fast multiplication?
a) Karatsuba algorithm.
b) Booths algorithm
c) Euclid algorithm
d) None of the above
What is the time complexity of Karatsuba's
algorithm for multiplying two n-digit numbers?
a) O(n)
b) O(n^2)
c) O(n^log23).
d) O(2^n)
What is the primary factor that influences the
efficiency of Karatsuba's algorithm?
a) Number of additions required.
b) Number of recursive calls
c) Number of subtractions required
d) Number of divisions required
Which of the following is a limitation of
Karatsuba's algorithm?
a) It cannot handle negative numbers
b) It requires more memory compared to
traditional methods
c) It becomes less efficient for very large
numbers.
d) It is not applicable for decimal numbers
What is the key advantage of Karatsuba's
algorithm over traditional multiplication
methods?
a) It requires less memory
b) It has a lower time complexity.
c) It is easier to implement
d) It can handle negative numbers more
efficiently