Design and Analysis of Algorithms: Divide and Conquer Divide - and - Conquer Technique
Design and Analysis of Algorithms: Divide and Conquer Divide - and - Conquer Technique
Chapter 4
DivideDivide-andand-conquer technique
a problem of size n
Solve smaller instances recursively Obtain solution to original (larger) instance by combining these solutions
3.
a solution to subproblem 1
a solution to subproblem 2
Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplicationmultiplication-Strassens algorithm Convex hullhull-QuickHull algorithm
Design and Analysis of Algorithms - Chapter 4 3
3.
a < bk a = bk a > bk
Mergesort
Algorithm: I Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] I Sort arrays B and C I Merge sorted arrays B and C into array A as follows:
Repeat the following until no elements remain in one of the arrays: 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
Mergesort Example
7 2 1 6 4
Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.
Design and Analysis of Algorithms - Chapter 4 5 Design and Analysis of Algorithms - Chapter 4 6
Chapter 4
Quicksort
I I
All cases have same efficiency: ( n log n) Number of comparisons is close to theoretical minimum for comparisoncomparison-based sorting: log n ! n lg n - 1.44 n
I I
Space requirement: ( n ) (NOT (NOT inin-place) Can be implemented without recursion (bottom(bottom-up)
Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than the pivot (See algorithm Partition in section 4.2) Exchange the pivot with the last element in the first (i.e., sublist) sublist) the pivot is now in its final position Sort the two sublists p A[i]p A[i]>p
8
Quicksort Example
15 22 13 27 12 10 20 25
10
Efficiency of quicksort
I I I I
QuickHull Algorithm
Inspired by Quicksort compute Convex Hull: Assume points are sorted by x-coordinate values I Identify extreme points P1 and P2 (part of hull) I Compute upper hull:
I
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 avoids worst worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 2020-25% improvement
find point Pmax that is farthest away from line P1P2 compute the hull of the points to the left of line P1Pmax compute the hull of the points to the left of line PmaxP2
I
Considered the method of choice for internal sorting for large files (n 10000)
Design and Analysis of Algorithms - Chapter 4 11
12
Chapter 4
Strassens matrix multiplication
I
Finding point farthest away from line P1P2 can be done in linear time This gives same efficiency as quicksort: quicksort:
Worst case: ( n2) Average case: ( n log n)
Strassen observed [1969] that the product of two matrices can be computed as follows:
C00 C01 = C10 C11 A10 A11 A00 A01 * B10 B11 M3 + M5 M1 + M3 - M2 + M6 B00 B01
If points are not initially sorted by xx-coordinate value, this can be accomplished in ( n log n) no increase in asymptotic efficiency class Other algorithms for convex hull:
Grahams scan DCHull also in ( n log n)
Design and Analysis of Algorithms - Chapter 4 13
M1 + M4 - M5 + M7 = M2 + M4
14
Submatrices: Submatrices:
I
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)
Design and Analysis of Algorithms - Chapter 4 15
Number of additions:
Other algorithms have improved this result, but are even more complex
Design and Analysis of Algorithms - Chapter 4 16