03.01.2015 Views

Design and Analysis of Algorithms Chapter 4

Design and Analysis of Algorithms Chapter 4

Design and Analysis of Algorithms Chapter 4

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> <strong>Chapter</strong> 4<br />

Divide <strong>and</strong> Conquer<br />

Divide-<strong>and</strong><br />

<strong>and</strong>-conquer technique<br />

The most well known algorithm design strategy:<br />

1. Divide instance <strong>of</strong> problem into two or more smaller<br />

instances<br />

2. Solve smaller instances recursively<br />

subproblem 1<br />

<strong>of</strong> size n/2<br />

a problem <strong>of</strong> size n<br />

subproblem 2<br />

<strong>of</strong> size n/2<br />

3. Obtain solution to original (larger) instance by combining<br />

these solutions<br />

a solution to<br />

subproblem 1<br />

a solution to<br />

subproblem 2<br />

a solution to<br />

the original problem<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 1<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 2<br />

Divide <strong>and</strong> Conquer Examples<br />

General Divide <strong>and</strong> Conquer recurrence:<br />

<br />

Sorting: mergesort <strong>and</strong> quicksort<br />

T(n) ) = aT(n/b<br />

n/b) ) + f (n) where f (n) ∈ Θ(n k )<br />

<br />

<br />

Tree traversals<br />

Binary search<br />

1. a < b k T(n) ∈ Θ(n k )<br />

2. a = b k T(n) ∈ Θ(n k lg n )<br />

3. a > b k T(n) ∈ Θ(n )<br />

<br />

<br />

Matrix multiplication-Strassen’s<br />

algorithm<br />

Convex hull-QuickHull<br />

algorithm<br />

Note: the same results hold with O instead <strong>of</strong> Θ.<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 3<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 4<br />

Mergesort<br />

Algorithm:<br />

Split array A[1..n] ] in two <strong>and</strong> make copies <strong>of</strong> each half<br />

in arrays B[1.. n/2 ] <strong>and</strong> C[1.. n/2 ]<br />

Sort arrays B <strong>and</strong> C<br />

Merge sorted arrays B <strong>and</strong> C into array A as follows:<br />

• Repeat the following until no elements remain in one <strong>of</strong> the arrays:<br />

– compare the first elements in the remaining unprocessed portions <strong>of</strong><br />

the arrays<br />

– copy the smaller <strong>of</strong> the two into A, while incrementing the index<br />

indicating the unprocessed portion <strong>of</strong> that array<br />

• Once all elements in one <strong>of</strong> the arrays are processed, copy the<br />

remaining unprocessed elements from the other array into A.<br />

Mergesort Example<br />

7 2 1 6 4<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 5<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 6


<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> <strong>Chapter</strong> 4<br />

Efficiency <strong>of</strong> mergesort<br />

All cases have same efficiency: Θ( n log n)<br />

Number <strong>of</strong> comparisons is close to theoretical minimum for<br />

comparison-based sorting:<br />

• log n ! ≈<br />

n lg n - 1.44 n<br />

Space requirement: Θ( n ) (NOT(<br />

in-place)<br />

Can be implemented without recursion (bottom-up)<br />

Quicksort<br />

Select a pivot (partitioning element)<br />

Rearrange the list so that all the elements in the positions<br />

before the pivot are smaller than or equal to the pivot <strong>and</strong><br />

those after the pivot are larger than the pivot (See<br />

algorithm Partition in section 4.2)<br />

Exchange the pivot with the last element in the first (i.e., ≤<br />

sublist) – the pivot is now in its final position<br />

Sort the two sublists<br />

p<br />

A[i]≤p<br />

A[i]>p<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 7<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 8<br />

The partition algorithm<br />

Quicksort Example<br />

15 22 13 27 12 10 20 25<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 9<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 10<br />

Efficiency <strong>of</strong> quicksort<br />

Best case: : split in the middle — Θ( n log n)<br />

Worst case: : sorted array! — Θ( n 2 )<br />

Average case: : r<strong>and</strong>om arrays — Θ( n log n)<br />

Improvements:<br />

• better pivot selection: median <strong>of</strong> three partitioning avoids worst<br />

case in sorted files<br />

• switch to insertion sort on small subfiles<br />

• elimination <strong>of</strong> recursion<br />

these combine to 20-25% 25% improvement<br />

Considered the method <strong>of</strong> choice for internal sorting for<br />

large files (n(<br />

≥ 10000)<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 11<br />

QuickHull Algorithm<br />

Inspired by Quicksort compute Convex Hull:<br />

Assume points are sorted by x-coordinate values<br />

Identify extreme points P 1 <strong>and</strong> P 2 (part <strong>of</strong> hull)<br />

Compute upper hull:<br />

• find point P max that is farthest away from line P 1 P 2<br />

• compute the hull <strong>of</strong> the points to the left <strong>of</strong> line P 1 P max<br />

• compute the hull <strong>of</strong> the points to the left <strong>of</strong> line P max P 2<br />

Compute lower hull in a similar manner<br />

P max<br />

P 1<br />

P 2<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 12


<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> <strong>Chapter</strong> 4<br />

Efficiency <strong>of</strong> QuickHull algorithm<br />

Finding point farthest away from line P 1 P 2 can be done in<br />

linear time<br />

This gives same efficiency as quicksort:<br />

• Worst case: Θ( n 2 )<br />

• Average case: Θ( n log n)<br />

If points are not initially sorted by x-coordinate x<br />

value, this<br />

can be accomplished in Θ( n log n) — no increase in<br />

asymptotic efficiency class<br />

Other algorithms for convex hull:<br />

• Graham’s scan<br />

• DCHull<br />

also in Θ( n log n)<br />

Strassen’s matrix multiplication<br />

Strassen observed [1969] that the product <strong>of</strong> two matrices<br />

can be computed as follows:<br />

C 00 C 01 A 00 A 01 B 00 B 01<br />

= *<br />

C 10 C 11 A 10 A 11 B 10 B 11<br />

=<br />

M 1<br />

+ M 4 - M 5 + M 7 M 3 + M 5<br />

M 2 + M 4 M 1 + M 3 - M 2 + M 6<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 13<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 14<br />

Submatrices:<br />

M 1 = (A 00 + A 11 ) * (B 00 + B 11 )<br />

M 2 = (A 10 + A 11 ) * B 00<br />

M 3 = A 00 * (B 01 - B 11 )<br />

M 4 = A 11 * (B 10 - B 00 )<br />

M 5 = (A 00 + A 01 ) * B 11<br />

M 6 = (A 10 - A 00 ) * (B 00 + B 01 )<br />

M 7 = (A 01 - A 11 ) * (B 10 + B 11 )<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 15<br />

Efficiency <strong>of</strong> Strassen’s algorithm<br />

<br />

<br />

<br />

If n is not a power <strong>of</strong> 2, matrices can be padded with zeros<br />

Number <strong>of</strong> multiplications:<br />

Number <strong>of</strong> additions:<br />

Other algorithms have improved this result, but are even<br />

more complex<br />

<strong>Design</strong> <strong>and</strong> <strong>Analysis</strong> <strong>of</strong> <strong>Algorithms</strong> - <strong>Chapter</strong> 4 16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!