Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 41 Divide and Conquer 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

2 Design and Analysis of Algorithms - Chapter 42 Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n

3 Design and Analysis of Algorithms - Chapter 43 Divide and Conquer Examples b Sorting: mergesort and quicksort b Tree traversals b Binary search b Matrix multiplication - Strassen’s algorithm b Convex hull - QuickHull algorithm

4 Design and Analysis of Algorithms - Chapter 44 General Divide and Conquer recurrence: T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(n k ) 1. a < b k T(n) ∈ Θ(n k ) 2. a = b k T(n) ∈ Θ(n k lg n ) 3. a > b k T(n) ∈ Θ( n log b a ) Note: the same results hold with O instead of Θ.

5 Design and Analysis of Algorithms - Chapter 45 Mergesort Algorithm: b Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] arrays B[1.. n/2 ] and C[1.. n/2 ] b Sort arrays B and C b Merge sorted arrays B and C into array A as follows: Repeat until no elements remain in one of the arrays: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.Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.

6 Design and Analysis of Algorithms - Chapter 46 Mergesort Example 7 2 1 6 4 7 2 1 6 4

7 Design and Analysis of Algorithms - Chapter 47 Efficiency of mergesort b C(n) = 2 C(n/2) + C merge (n) for n>1, C(1)=0 b All cases have same efficiency: Θ(n log n) b Number of comparisons is close to theoretical minimum for comparison-based sorting: log n ! ≈ n lg n - 1.44 n log n ! ≈ n lg n - 1.44 n b Space requirement: Θ(n) (NOT in-place) b Can be implemented without recursion (bottom-up)

8 Design and Analysis of Algorithms - Chapter 48 Quicksort b Select a pivot (partitioning element) b 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) b Exchange the pivot with the last element in the first (i.e., ≤ sublist) – the pivot is now in its final position b Sort the two sublists p A[i]≤p A[i]>p

9 Design and Analysis of Algorithms - Chapter 49 The partition algorithm

10 Design and Analysis of Algorithms - Chapter 410 Quicksort Example 15 22 13 27 12 10 20 25 15 22 13 27 12 10 20 25

11 Design and Analysis of Algorithms - Chapter 411 Efficiency of quicksort b Best case: split in the middle — Θ( n log n) b Worst case: sorted array! — Θ( n 2 ) b Average case: random arrays — Θ( n log n) b Improvements: better pivot selection: median of three partitioning avoids worst case in sorted filesbetter pivot selection: median of three partitioning avoids worst case in sorted files switch to insertion sort on small subfilesswitch to insertion sort on small subfiles elimination of recursionelimination of recursion these combine to 20-25% improvement b Considered the method of choice for internal sorting for large files (n ≥ 10000)

12 Design and Analysis of Algorithms - Chapter 412 Multiplication of large integers b To multiply two integers of n 1 and n 2 digits by a pen-and-pencil algorithm, we perform n 1 n 2 multiplications b However, remark the example of 23*14, where 23=2. 10 1 +3. 10 0 and 14=1. 10 1 +4. 10 0 b In general: c=a*b=c 2. 10 n +c 1. 10 n/2 +c 0. 10 0 where c 2 =a 1 *b 1, c 0 =a 0 *b 0, c 1 =(a 1 +a 0 )*(b 1 +b 0 )-(c 2 +c 0 ) a 0,a 1,b 0,b 1 be the left and right parts of a and b. b Recurrence relation b Efficiency

13 Design and Analysis of Algorithms - Chapter 413 Closest Pair Problem b Problem: find closest points among n ones in k- dimensional space b Assume points are sorted by x-coordinate values b Divide the points in two subsets S 1 and S 2 of n/2 points by drawing a vertical line at x=c b Select the closest pair among the closest pair of the left subset (distance d 1 ), of the closest pair of the right subset (distance d 2 ), and the closest pair with points from both sides b We need to examine a stripe of width 2d, where d=min[d 1,d 2 ]

14 Design and Analysis of Algorithms - Chapter 414 Closest Pair Problem (2) b Geometric explanation b Recurrence relation T(n) = 2T(n/2) + M(n) b Efficiency in O(n logn) b Optimality

15 Design and Analysis of Algorithms - Chapter 415 QuickHull Algorithm Inspired by Quicksort compute Convex Hull: b Assume points are sorted by x-coordinate values b Identify extreme points P 1 and P 2 (part of hull) b The set S of points is divided in two subsets S 1 and S 2 b Compute Convex Hull for S 1 b Compute Convex Hull for S 2 in a similar manner P1P1 P2P2 P max

16 Design and Analysis of Algorithms - Chapter 416 QuickHull Algorithm (2) b How to compute the Convex (upper) Hull for S 1 find point P max that is farthest away from line P 1 P 2find point P max that is farthest away from line P 1 P 2 compute the hull of the points to the left of line P 1 P maxcompute the hull of the points to the left of line P 1 P max compute the hull of the points to the left of line P max P 2compute the hull of the points to the left of line P max P 2 b How to find the P max point P max maximizes the area of the triangle P max P 1 P 2P max maximizes the area of the triangle P max P 1 P 2 if tie, select the P max that maximizes the angle P max P 1 P 2if tie, select the P max that maximizes the angle P max P 1 P 2 b The points inside triangle P max P 1 P 2 can be excluded from further consideration b How to find geometrically the point P max and the points to the left of line P 1 P max

17 Design and Analysis of Algorithms - Chapter 417 QuickHull Algorithm (3) b How to find geometrically the point P max and the points to the left of line P 1 P max Given points P 1 (x 1,y 1 ), P 2 (x 2,y 2 ), P max (x max,y max )Given points P 1 (x 1,y 1 ), P 2 (x 2,y 2 ), P max (x max,y max ) The area of the triangle is half of the magnitude of the determinantThe area of the triangle is half of the magnitude of the determinant =x 1 y 2 +x max y 1 +x 2 y max –x max y 2 –x 2 y 1 -x 1 y max =x 1 y 2 +x max y 1 +x 2 y max –x max y 2 –x 2 y 1 -x 1 y max The sign of the expression is positive if and only if the point P max is to the left of the line P 1 P 2The sign of the expression is positive if and only if the point P max is to the left of the line P 1 P 2 x1x1x1x1 y1y1y1y11 x2x2x2x2 y2y2y2y21 x max y max 1

18 Design and Analysis of Algorithms - Chapter 418 Efficiency of QuickHull algorithm b Finding point farthest away from line P 1 P 2 can be done in linear time b This gives same efficiency as quicksort: Worst case: Θ(n 2 )Worst case: Θ(n 2 ) Average case: Θ(n log n)Average case: Θ(n log n) b If points are not initially sorted by x-coordinate value, this can be accomplished in Θ(n log n) — no increase in asymptotic efficiency class b Other algorithms for convex hull: Graham’s scanGraham’s scan DCHullDCHull also in Θ(n log n)

19 Design and Analysis of Algorithms - Chapter 419 Strassen’s matrix multiplication b Strassen observed [1969] that the product of two matrices A and B (of size 2 n x2 n ) can be computed as follows: C 00 C 01 A 00 A 01 B 00 B 01 = * = * C 10 C 11 A 10 A 11 B 10 B 11 M 1 + M 4 - M 5 + M 7 M 3 + M 5 M 1 + M 4 - M 5 + M 7 M 3 + M 5 = M 2 + M 4 M 1 + M 3 - M 2 + M 6 M 2 + M 4 M 1 + M 3 - M 2 + M 6

20 Design and Analysis of Algorithms - Chapter 420 Submatrices: b M 1 = (A 00 + A 11 ) * (B 00 + B 11 ) b M 2 = (A 10 + A 11 ) * B 00 b M 3 = A 00 * (B 01 - B 11 ) b M 4 = A 11 * (B 10 - B 00 ) b M 5 = (A 00 + A 01 ) * B 11 b M 6 = (A 10 - A 00 ) * (B 00 + B 01 ) b M 7 = (A 01 - A 11 ) * (B 10 + B 11 )

21 Design and Analysis of Algorithms - Chapter 421 Efficiency of Strassen’s algorithm b If n is not a power of 2, matrices can be padded with zeros b Number of multiplications: b Number of additions: b Recurrence relations: b Other algorithms have improved this result, but are even more complex


Download ppt "Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two."

Similar presentations


Ads by Google