Divide-and-Conquer • This approach, divides an instance of a problem into two or more smaller instances. • If solutions to the smaller instances can be obtained readily, the solution to the original instance can be obtained by combining these solutions. • If the smaller instances are still too large to be solved readily, they can be divided into still smaller instances. • This process of dividing the instances continues until they are so small that a solution is readily obtainable.
Binary Search • A recursive version to illustrate the top-down used by divide and conquer • Worst Case: Ө(logn).
Merge Sort • First, divide the array into 2 sub-arrays each of n/2. • Second, solve each sub-array • Third Combine solutions to the sub-arrays by merging them into a single sorted array. • Worst Case: Ө(nlogn).
Divide-and-Conquer Approach • Step 1: Divide an instance of a problem into one or more smaller instances. • Step 2: Conquer that is solve each of the smaller instances (use recursion till array is sufficiently small). • Step 3: Combine the solutions of the smaller instances to obtain the solution of the original instance.
Quick Sort l Sorts in place l Sorts O(n lg n) in the average case l Sorts O(n 2) in the worst case
Strassen’s Matrix Multiplication Algorithm • • Multiply two 2 * 2 matrices A and B m 1 = (a 11 + a 22) (b 11 + b 22) m 2 = (a 21 + a 22) b 11 m 3 = a 11 (b 12 - b 22) m 4 = a 22 (b 21 - b 11) m 5 = (a 11 + a 12) b 22 m 6 = (a 21 - a 11) (b 11 + b 12) m 7 = (a 12 - a 22) (b 21 + b 22)
• So the product C is given by C = [ m 1+m 4 -m 5+m 7 m 3+m 5 ] m 2+m 4 m 1+m 3 -m 2+m 6 • T(n) is Ө(n 2. 81)
Multiplication of Large Integers • • u=x * 10 m + y v=w * 10 m + z then: u * v = xw * 102 m + (xz + wy) * 10 m + yz Worst Case: Ө(n 2).