Divide and Conquer Algorithms
Divide and Conquer Algorithms
Student Outcomes
Describe and answer questions about example divide and conquer algorithms
Binary Search
Quick Sort
Merge Sort
Integer Multiplication
Matrix Multiplication (Strassen's algorithm)
Maximal Subsequence
Binary Search
https://sites.radford.edu/~nokie/classes/360/divcon.html 1/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
return mid
elsif x < s[mid] then
return binsearch(n, low, mid-1, S, x)
else
return binsearch(n, mid+1, high, S, x)
else
return 0
end binsearch
Merge Sort
Time Performance:
T (n) = 2T (n/2) + Θ(n)
https://sites.radford.edu/~nokie/classes/360/divcon.html 2/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
Space Performance:
Requires θ(k/2) new space for each recursive call
Number of recursive calls until size 1: lg n
lg n
1
Space used for call with size n array: n ∑ i
= n(1 + 1/2 + 1/4 + …) = nΘ(2) = Θ(2n)
i=0
2
This extra space can be reused for next recursive call (think about the diagram)
Total space: Θ(3n)
Can we do better?
S(LOW..HIGH) := U(LOW..HIGH)
Time Performance:
T (n) = 2T (n/2) + Θ(n)
Space Performance:
Never uses more than θ(n) extra space for call to merge
Extra space is reused for next call
Total space: Θ(2n)
Another improvement: make S size 2n, and merge from one side to the other
Quick Sort
https://sites.radford.edu/~nokie/classes/360/divcon.html 3/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
T (n) = Θ(n )
2
(by induction)
Assume T (k) = k
2
+ Θ(k) for k < n
T (n) = T (n − 1) + Θ(n)
2
= (n − 1) + Θ(n)
2
= n − 2n + 1 + Θ(n)
2
= n + Θ(n) + Θ(n)
2
= n + Θ(n)
2
= Θ(n )
https://sites.radford.edu/~nokie/classes/360/divcon.html 4/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
n
1
T (n) = ∑[T (p − 1) + T (n − p)] + n − 1
n
p=1
n
1
= ∑ [T (p − 1) + T (n − p)] + n − 1
n
p=1
= Θ(n lg n)
Space Performance:
No extra space needed
Total space: Θ(n)
But, ... what about the space for ...?
https://sites.radford.edu/~nokie/classes/360/divcon.html 5/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
Don't sort small lists and use insertion sort on entire list
Matrix Multiplication
cij = ∑ a ik bkj
k=1
2
T (n) = Θ(1) + 8T (n/2) + Θ(n )
T (n) = Θ(n )
3
by master method
Strassen's Algorithm
Algorithm:
Create 10 n/2 by n/2 sum arrays: S1 .. S10
Recursively compute 7 product arrays: m .. m 1 7
Θ(1), ifn = 1
T (n) = {
2
7T (n/2) + Θ(n ), ifn > 1
https://sites.radford.edu/~nokie/classes/360/divcon.html 7/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
p = xw
q = yz
2k k
xy × wz = p × 10 + (r − p − q) × 10 + q
Maximal Subarray
Problem:
Input: A: Array(1 .. n) of numbers
Output: Indices i and j such that sum(A(i .. j)) has the maximum value
https://sites.radford.edu/~nokie/classes/360/divcon.html 8/9
11/24/23, 10:32 AM Divide and Conquer Algorithms
Performance:
Base Case: T (1) = Θ(1)
Divide: Θ(1)
Conquer: 2T (n/2) and Θ(n)
Combine: Θ(1)
Can we do better?
Yes! A Θ(n) algorithm exists! (Dynamic programming!)
https://sites.radford.edu/~nokie/classes/360/divcon.html 9/9