0% found this document useful (0 votes)
63 views

Week 5 Divide and Conquer

The document discusses the divide and conquer algorithm design strategy and technique. It begins by explaining that divide and conquer involves dividing a problem into smaller subproblems, solving the subproblems recursively, and then combining the solutions to solve the original problem. It then provides an example of using divide and conquer to solve the maximum subarray problem, explaining that the problem is divided by splitting the array in half at the midpoint and solving the subproblems recursively before combining the optimal solutions.

Uploaded by

vidishashukla03
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)
63 views

Week 5 Divide and Conquer

The document discusses the divide and conquer algorithm design strategy and technique. It begins by explaining that divide and conquer involves dividing a problem into smaller subproblems, solving the subproblems recursively, and then combining the solutions to solve the original problem. It then provides an example of using divide and conquer to solve the maximum subarray problem, explaining that the problem is divided by splitting the array in half at the midpoint and solving the subproblems recursively before combining the optimal solutions.

Uploaded by

vidishashukla03
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/ 31

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
• You can buy a unit of stock, only one time, then sell it at a later
date
– Buy/sell at end of day

• Strategy: buy low, sell high


– The lowest price may appear after the highest price

• Assume you know future prices

• Can you maximize profit by buying at lowest price and selling at


highest price?
Buy lowest sell highest
Brute force
• How many buy/sell pairs are possible over n
days?
• Evaluate each pair and keep track of maximum
• Can we do better?
Transformation
• Find sequence of days so that:
– the net change from last to first is maximized
• Look at the daily change in price
– Change on day i: price day i minus price day i-1
– We now have an array of changes (numbers), e.g.
12,-3,-24,20,-3,-16,-23,18,20,-7,12,-5,-22,14,-4,6
– Find contiguous subarray with largest sum
• maximum subarray
– E.g.: buy after day 7, sell after day 11
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 …


Divide-and-Conquer
• A[low..high]
• Divide in the middle:
– A[low,mid], A[mid+1,high]
• Any subarray A[i,..j] is
(1) Entirely in A[low,mid]
(2) Entirely in A[mid+1,high]
(3) In both
• (1) and (2) can be found recursively
Divide-and-Conquer (cont.)
• (3) find maximum subarray that crosses
midpoint
– Need to find maximum subarrays of the form
A[i..mid], A[mid+1..j], low <= i, j <= high
• Take subarray with largest sum of (1), (2), (3)
Karatsuba’s Multiplication Algorithm
• In native algorithm time required for two n –
digit number was O(n^2).
• But in Karatsuba’s Multiplication Algorithm
O(n^1.5)
• So, Karatsuba’s Multiplication Algorithm is
fastest algoritm.
• Karatsuba’s Multiplication Algorithm uses
devide and conquer technique.
Karatsuba’s Multiplication Algorithm
• Karatsuba’s Multiplication Algorithm can be
used to multiply in any base base10, base2
etc.
Steps for Karatsuba’s Multiplication
Algorithm
• Step1 : XH * YH
• Step 2 : Xl * Yl
• Step 3: (XH + Xl )* (YH + Yl )
• Step 4: S3-S2- S1
• Step 5: S1 * (bn ) + S4 * (bn/2) + S2
• Where b is base 10 and n is number of digits
Lets Solve
• X= 1026732
• Y = 732912
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
• 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

You might also like