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 ] in 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 the following until no elements remain in one of the arrays: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.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 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 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 Compute upper hull: 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 Compute lower hull in a similar manner P1P1 P2P2 P max

13 Design and Analysis of Algorithms - Chapter 413 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)

14 Design and Analysis of Algorithms - Chapter 414 Strassen’s matrix multiplication b Strassen observed [1969] that the product of two matrices 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

15 Design and Analysis of Algorithms - Chapter 415 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 )

16 Design and Analysis of Algorithms - Chapter 416 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 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