Divide and Conquer The most well known algorithm

  • Slides: 16
Download presentation
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of

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 Design and Analysis of Algorithms - Chapter 4 1

Divide-and-conquer technique a problem of size n subproblem 1 of size n/2 subproblem 2

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

Divide and Conquer Examples b Sorting: mergesort and quicksort b Tree traversals b Binary

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

General Divide and Conquer recurrence: T(n) = a. T(n/b) + f (n) 1. 2.

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

Mergesort Algorithm: b Split array A[1. . n] in two and make copies of

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 ] 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: – 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 Design and Analysis of Algorithms - Chapter

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

Efficiency of mergesort b All cases have same efficiency: Θ( n log n) b

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 b Space requirement: Θ( n ) (NOT in-place) b Can be implemented without recursion (bottom-up) Design and Analysis of Algorithms - Chapter 4 7

Quicksort b b Select a pivot (partitioning element) Rearrange the list so that all

Quicksort b b 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 Design and Analysis of Algorithms - Chapter 4 8

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

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

Quicksort Example 15 22 13 27 12 10 20 25 Design and Analysis of

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

Efficiency of quicksort b Best case: split in the middle — Θ( n log

Efficiency of quicksort b Best case: split in the middle — Θ( n log n) Worst case: sorted array! — Θ( n 2) Average case: random arrays — Θ( n log n) b Improvements: b b • 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 b Considered the method of choice for internal sorting for large files (n ≥ 10000) Design and Analysis of Algorithms - Chapter 4 11

Quick. Hull Algorithm Inspired by Quicksort compute Convex Hull: b Assume points are sorted

Quick. Hull 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: • • • b find point Pmax that is farthest away from line P 1 P 2 compute the hull of the points to the left of line P 1 Pmax compute the hull of the points to the left of line Pmax. P 2 Compute lower hull in a similar manner Pmax P 2 P 1 Design and Analysis of Algorithms - Chapter 4 12

Efficiency of Quick. Hull algorithm b b Finding point farthest away from line P

Efficiency of Quick. Hull algorithm b b Finding point farthest away from line P 1 P 2 can be done in linear time This gives same efficiency as quicksort: • Worst case: Θ( n 2) • Average case: Θ( n log n) b 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 Other algorithms for convex hull: • Graham’s scan • DCHull also in Θ( n log n) Design and Analysis of Algorithms - Chapter 4 13

Strassen’s matrix multiplication b Strassen observed [1969] that the product of two matrices can

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 = C 10 C 11 B 00 B 01 * A 10 A 11 B 10 B 11 M 1 + M 4 - M 5 + M 7 M 3 + M 5 = M 2 + M 4 M 1 + M 3 - M 2 + M 6 Design and Analysis of Algorithms - Chapter 4 14

Submatrices: b M 1 = (A 00 + A 11) * (B 00 +

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

Efficiency of Strassen’s algorithm b If n is not a power of 2, matrices

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