410 likes | 437 Views
Learn the principles and applications of divide and conquer algorithms like Merge Sort, Quicksort, Binary Search, and Matrix Multiplication. Explore efficiency analysis, improvements, and advanced techniques.
E N D
Divide and Conquer 1.) A problem’s instance is divides into several smaller instances of the same problem, ideally of about the same size. 2.) The smaller instance are solved(typically recursively , though sometimes a different algorithm is employed when instances become small enough) 3.) If necessary, the solutions obtained for the smaller instances are combined to get a solution to the original instance.
Merge sort • Split array A[0..n-1] into about equal halves and make copies of each half in arrays B and C • Sort arrays B and C recursively • Merge sorted arrays B and C into array A as follows: • Repeat the following until no elements remain in one of the arrays: • compare the first elements in the remaining unprocessed portions of the arrays • copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array • Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.
p A[i]p A[i]p Quicksort • Select a pivot (partitioning element).(Here is first) • Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot. • Exchange the pivot with the last element in the first (i.e., )subarray — the pivot is now in its final position • Sort the two subarrays recursively
Quicksort Example 5 3 1 9 8 2 4 7 2 3 1 4 5 8 9 7 1 2 3 4 5 7 8 9 123 4 5789 123 4 5789 12345789
Analysis of Quicksort • Best case: split in the middle — Θ(n log n) • Worst case: sorted array! — Θ(n2) • Average case: random arrays —Θ(n log n) • Improvements: • better pivot selection: median of three partitioning • switch to insertion sort on small subfiles • elimination of recursion These combine to 20-25% improvement
Binary Search • If we place our items in an array and sort them in either ascending or descending order on the key first, then we can obtain much better performance with an algorithm called binary search. • In binary search, we first compare the key with the item in the middle position of the array. If there's a match, we can return immediately. If the key is less than the middle key, then the item sought must lie in the lower half of the array; if it's greater then the item sought must lie in the upper half of the array. So we repeat the procedure on the lower (or upper) half of the array.
Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as:A = 12345678901357986429 B = 87654321284820912836The grade-school algorithm: a1 a2 … anb1 b2 … bn(d10)d11d12 … d1n (d20)d21d22 … d2n … … … … … … … (dn0)dn1dn2 … dnn Efficiency: Θ(n2) single-digit multiplications
First Divide-and-Conquer Algorithm A small example: A B where A = 2135 and B = 4014 A = (21·102 + 35), B = (40 ·102 + 14) So, A B = (21 ·102 + 35) (40 ·102 + 14) = 21 40 ·104 + (21 14 + 35 40) ·102 + 35 14 In general, if A = A1A2 and B = B1B2 (where A and B are n-digit, A1, A2, B1,B2 are n/2-digit numbers), A B = A1 B1·10n+ (A1 B2 + A2 B1) ·10n/2 + A2 B2 Recurrence for the number of one-digit multiplications M(n): M(n) = 4M(n/2), M(1) = 1 Solution: M(n) = n2
Second Divide-and-Conquer Algorithm A B = A1 B1·10n+ (A1 B2 + A2 B1) ·10n/2 + A2 B2 The idea is to decrease the number of multiplications from 4 to 3: (A1 + A2 ) (B1 + B2 ) = A1 B1 + (A1 B2 + A2 B1) + A2 B2,I.e., (A1 B2 + A2 B1) = (A1 + A2 ) (B1 + B2 ) - A1 B1 - A2 B2,which requires only 3 multiplications at the expense of (4-1) extra add/sub.
Conventional Matrix Multiplication • Brute-force algorithm c00 c01 a00 a01 b00 b01 = * c10 c11 a10 a11 b10 b11 a00 * b00 + a01 * b10 a00 * b01 + a01 * b11 = a10 * b00 + a11 * b10 a10 * b01 + a11 * b11 efficiency in n3 8 multiplications 4 additions
Strassen’s Matrix Multiplication • Strassen’s algorithm for two 2x2 matrices (1969): c00 c01 a00 a01 b00 b01 = * c10 c11 a10 a11 b10 b11 m1 + m4 - m5 + m7 m3 + m5 = m2 + m4 m1 + m3 - m2 + m6 • m1 = (a00 + a11) * (b00 + b11) • m2 = (a10 + a11) * b00 • m3 = a00* (b01 - b11) • m4 = a11* (b10 - b00) • m5 = (a00 + a01) * b11 • m6 = (a10 - a00) * (b00 + b01) • m7 = (a01 - a11) * (b10 + b11) 7 multiplications 18 additions/subtraction
Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed in general as follows: C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6 Where A and B be two n by n matrices where n is a power of two. We can divide A,B and their product C into four n/2 by n/2 submatrices.
Formulas for Strassen’s Algorithm M1 = (A00 + A11) (B00 + B11) M2 = (A10 + A11) B00 M3 = A00 (B01 - B11) M4 = A11 (B10 - B00) M5 = (A00 + A01) B11 M6 = (A10 - A00) (B00 + B01) M7 = (A01 - A11) (B10 + B11)
Analysis of Strassen’s Algorithm If n is not a power of 2, matrices can be padded with zeros. Number of multiplications:
Recurrence • Recursive function call itself. • Recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. • Where T(n) is recurrence • an instance of size n can be divided into b instances of size n/b , with a of them needing to be solved. • a>= 1 and b> 1 • For obtaining Recurrence in Asymptotic notions:- • Substitution Method • Recursion Tree Method • Master Method
Substitution Method For example:- for Recurrence We guess that values is O(n lg n) Then substituting yields:- • Where c >= 1
Recursion Tree Method • In recursion tree, each node represents the cost of a single sub-problem somewhere in the set of recursive function invocations. • We sum the costs within each level of the tree to obtain a set of per level costs, and then we sum all he per-level costs to determine he total cost of all levels of the recursion. • For example:- T(n) = 3T(n/4) + cn2
Case 1 :- > f(n) • Case 2 :- = f(n) • Case 3:- < f(n) • For Example:-
Example 2:- Example 3:-
Closest-Pair Problem by Divide-and-Conquer Step 0 Sort the points by x (list one) and then by y (list two). Step 1 Divide the points given into two subsets S1 and S2 by a vertical line x = c so that half the points lie to the left or on the line and half the points lie to the right or on the line.
Closest Pair by Divide-and-Conquer (cont.) Step 2 Find recursively the closest pairs for the left and right subsets. Step 3 Set d = min{d1, d2} We can limit our attention to the points in the symmetric vertical strip of width 2d as possible closest pair. Let C1 and C2 be the subsets of points in the left subset S1 and of the right subset S2, respectively, that lie in this vertical strip. The points in C1 and C2 are stored in increasing order of their y coordinates, taken from the second list. Step 4 For every point P(x,y) in C1, we inspect points in C2 that may be closer to P than d. There can be no more than 6 such points (because d≤d2)!
Closest Pair by Divide-and-Conquer: Worst Case The worst case scenario is depicted below:
Efficiency of the Closest-Pair Algorithm Running time of the algorithm (without sorting) is: T(n) = 2T(n/2) + Θ(n) By the Master Theorem (with a = 2, b = 2) T(n) Θ(n log n) So the total time is Θ(n log n).
Quick hull Algorithm for Finding Convex Hull Convex hull: smallest convex set that includes given points. An O(n^3) brute force time. • Assume points are sorted by x-coordinate values • Identify extreme pointsP1 and P2 (leftmost and rightmost) • Compute upper hull recursively: • find point Pmax that is farthest away from line P1P2 • compute the upper hull of the points to the left of line P1Pmax • compute the upper hull of the points to the left of line PmaxP2 • Compute lower hull in a similar manner Pmax P2 P1
Efficiency of Quickhull Algorithm • Finding point farthest away from line P1P2 can be done in linear time. -- worst case: Θ(n2) -- average case: Θ(n) • If points are not initially sorted by x-coordinate value, this can be accomplished in O(n log n) time