0% found this document useful (0 votes)
9 views19 pages

Week 5 Divide and Conquer

Uploaded by

anshkmr991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Week 5 Divide and Conquer

Uploaded by

anshkmr991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE408

Divide and Conquer


Divide-and-Conquer
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more
smaller instances

2. Solve smaller instances recursively

3. Obtain solution to original (larger) instance by


combining these solutions
Divide-and-Conquer Technique (cont.)

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

a solution to It general leads to a


the original problem
recursive algorithm!
Maximum Subarray Problem
• The Maximum Subarray Problem is a classic algorithmic problem
that involves finding the contiguous subarray within a one-
dimensional array of numbers that has the largest sum. Here are
some key points about the problem:

• Definition: Given an array of numbers, the goal is to find the


subarray (consecutive elements) with the maximum sum.

• 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 and Conquer Approach:


- **Divide**: Divide the array into two halves.
- **Conquer**: Recursively find the maximum subarray sum
in each half.
- **Combine**: Find the maximum subarray sum that
crosses the midpoint.
- Return the maximum of the three sums.
Example - Divide and Conquer: Consider the array:
[-2, 1, -3, 4, -1, 2, 1, -5, 4].

Divide: Split the array into two halves: [-2, 1, -3, 4] and [ -1, 2, 1, -5, 4].

Conquer: Recursively find the maximum subarray sums in each half.

Combine: Find the maximum subarray sum that crosses the midpoint,
which is [4, -1, 2, 1].

Return the maximum of the sums obtained in the previous steps. By


applying the divide and conquer approach, the maximum subarray sum is
found efficiently.
Brute force again
• Trivial if only positive numbers (assume not)

• Need to check O(n2) pairs

• For each pair, find the sum

• Thus total time is …


Time analysis
• Find-Max-Cross-Subarray: O(n) time

• Two recursive calls on input size n/2

• 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

You might also like