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
• Thus:
T(n) = 2T(n/2) + O(n)
T(n) = O(n log n)
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 halves, 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 halves.
• Perform four multiplications, i.e.,
a_1 b_1, a_2 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
Quiz Time
In the Divide and Conquer approach for the
Maximum Subarray Problem, what is the base
case for termination of recursion?
a) When the subarray size becomes 0
b) When the subarray size becomes 1
c) When the subarray size becomes 2
d) When the subarray size becomes n
Q1. What is the main idea behind the Divide and Conquer
approach for the Maximum Subarray Problem?
a) Find the maximum subarray by repeatedly dividing the
array into smaller subarrays
b) Iterate through the array and find the maximum
subarray using dynamic programming
c) Perform a binary search to find the maximum subarray
d) Randomly select subarrays and check for the maximum
sum
Q2. What does the Combine phase in the Divide
and Conquer approach for the Maximum Subarray
Problem involve?
a) Combining the maximum subarrays of the left
and right halves
b) Combining all elements of the array
c) Combining the positive and negative elements of
the array
d) Combining the elements using a hash function
Q3. In the context of the maximum subarray
problem, what does a negative sum of a
subarray indicate?
a) The subarray does not contribute to the
maximum subarray sum
b) There is a bug in the algorithm c)
The array contains invalid elements
d) The subarray is the maximum subarray
Q4. If an array contains both positive and negative
elements, what is the significance of the maximum
subarray sum?
a) It represents the sum of the subarray with the most
positive elements
b) It represents the sum of the subarray with the least
negative elements
c) It represents the sum of a contiguous subarray with
the largest sum among all possible contiguous subarrays
d) It represents the sum of the entire array
Q5. 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
Q6. 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)
Q7. 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
Q8. 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
Q9. 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
Answers
• 5- a) Karatsuba algorithm
• 6- c) O(n^log23)
• 7- a) No. of additions are required
• 8- c) It becomes less efficient for very large
numbers
• 9- b) It has a lower time complexity