Professional Documents
Culture Documents
Combine the solutions to the sub-problems into the solution for the original problem.
Recursive Algorithms
Recurrence (or Recurrence Equation) Divide: Divide the n-element sequence to be (1) if n <= c sorted into two subsequences of n/2 T(n) = elements aT(n/b) each. + D(n) + C(n) otherwise Conquer: Sort the two subsequences Parts Solution forusing smallest subdivision (Terminating condition) recursively merge sort. Subdivision relation (aT(n/b)) Combine: Merge the two sorted subsequences Division efforts (D(n)) to the sorted produce Combining effort (C(n)) answer.
Matrix Multiplication
The product of two nn matrices X and Y is a third nn matrix Z = XY, where
Matrix Multiplication
O(n3) as there are n2 entries to be computed and each takes O(n) time For quite sometime, this was believed to be the smallest time In 1969, a German mathematician Volker Strassen lowered this bound to O(n2.81) A solution that is based on Divide and Conquer approach
The Idea
Matrix multiplication is particularly easy to break into subproblems, because it can be performed block-wise
where
p i j 2 i 2 i 2 i 2 8 i 2 1 7 8 7 8 8 j 8 7 j 7 1 j 1 3 j 3 5 6 5 6 3 5 6 1 3 5 6 7 1 3 5 6
10
i 2 1 7 i 2 1 3 i 2 1 3 i 2 1 3 i 2 1 3 4 8 8 8 8
j 3 5 j 7 5 6 j 7 5 6 4 j 7 5 6 4 j 7 5 6 8
11
2 p
4 r
Partition(A, p, r) x A[r] i p-1 for j p to r-1 do if A[j] x then i i+1 exchange A[i] A[j] exchange A[i+1] A[r] return i+1
12
Quicksort Algorithm
Quicksort: Complexity
16
17
Observations
We cant guarantee good splits But intuitively on random inputs we will get good splits
18
Summary
Divide and Conquer Examples
Binary Search Matrix Multiplication Median finding Sorting methods like Mergesort and Quicksort Closest Pair Convex Hull And others
10