More On Divide and Conquer
More On Divide and Conquer
1. Divide: Trivial.
2. Conquer: Recursively sort 2 subarrays.
3. Combine: Linear-time merge.
T(n) = 2 T(n/2) + O(n)
Recursive definition:
0 1 1 2 3 5 8 13 21 34 …
Naive recursive algorithm: Ω( φn)
(exponential time), where
is the golden ratio.
Computing Fibonacci
numbers
Theorem:
T(n)=Θ (n log n)
Initial call: FIND-MAXIMUM-SUBARRAY(A, 1, n)
Matrix multiplication
Input:
Output:
Standard algorithm
for i ← 1 to n
do for j ← 1 to n
do cij ← 0
for k ← 1 to n
do cij ← cij + aik ⋅bkj
Running time = Θ(n3)
Divide-and-conquer algorithm
IDEA:
n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices:
Area = Θ(n lg n)
H-tree embedding
Area = Θ(n)
Conclusion