Académique Documents
Professionnel Documents
Culture Documents
The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions
Divide-and-conquer technique
a problem of size n
a solution to subproblem 1
a solution to subproblem 2
Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplication - Strassens algorithm Convex hull - QuickHull algorithm
2.
3.
a < bk a = bk a > bk
Mergesort
Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A as follows: Repeat 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.
Design and Analysis of Algorithms - Chapter 4 5
Mergesort Example
7 2 1 6 4
Efficiency of mergesort
C(n)
= 2 C(n/2) + Cmerge(n) for n>1, C(1)=0 All cases have same efficiency: (n log n) Number of comparisons is close to theoretical minimum for comparison-based sorting: log n ! n lg n - 1.44 n Space requirement: (n) (NOT in-place) Can be implemented without recursion (bottom-up)
Design and Analysis of Algorithms - Chapter 4 7
Quicksort
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) the pivot is now in its final position Sort the two sublists
p
A[i]p A[i]>p
Quicksort Example
15 22 13 27 12 10 20 25
10
Efficiency 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 avoids worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 20-25% improvement
Considered the method of choice for internal sorting for large files (n 10000)
Design and Analysis of Algorithms - Chapter 4 11
Geometric explanation
14
QuickHull Algorithm
Inspired by Quicksort compute Convex Hull: Assume points are sorted by x-coordinate values Identify extreme points P1 and P2 (part of hull) The set S of points is divided in two subsets S1 and S2 Compute Convex Hull for S1 Compute Convex Hull for S2 in a similar manner
Pmax
P2
P1
Design and Analysis of Algorithms - Chapter 4 15
The points inside triangle PmaxP1P2 can be excluded from further consideration How to find geometrically the point Pmax and the points to the left of line P1Pmax
How to find geometrically the point Pmax and the points to the left of line P1Pmax Given points P1(x1,y1), P2(x2,y2), Pmax(xmax,ymax) The area of the triangle is half of the magnitude of the determinant
x1 x2 y1 y2 1 1
=x1y2+xmaxy1+x2ymaxxmaxy2x2y1-x1ymax
xmax ymax 1
The sign of the expression is positive if and only if the point Pmax is to the left of the line P1P2
Design and Analysis of Algorithms - Chapter 4 17
If points are not initially sorted by x-coordinate value, this can be accomplished in (n log n) no increase in asymptotic efficiency class Other algorithms for convex hull:
Strassen observed [1969] that the product of two matrices A and B (of size 2nx2n) can be computed as follows:
C00 C01 = A00 A01 * B00 B01
C10 C11
A10 A11
B10 B11
M3 + M 5 M1 + M3 - M2 + M6
M1 + M4 - M5 + M7 = M2 + M4
19
Submatrices:
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)
20
If n is not a power of 2, matrices can be padded with zeros Number of multiplications: Number of additions: Recurrence relations: Other algorithms have improved this result, but are even more complex
21