cs 312: algorithm analysis

35
CS 312: Algorithm Analysis Lecture #6: Divide and Conquer This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License . by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warn

Upload: javen

Post on 23-Feb-2016

22 views

Category:

Documents


0 download

DESCRIPTION

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License . CS 312: Algorithm Analysis. Lecture #6: Divide and Conquer. Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick. Announcements. Project #1 Due: today (5pm ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 312:  Algorithm Analysis

CS 312: Algorithm Analysis

Lecture #6: Divide and Conquer

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick

Page 2: CS 312:  Algorithm Analysis

Announcements Project #1

Due: today (5pm)

Holiday: Monday

Project #2 Instructions and software distribution available from the schedule We do have one improvement to release later today, though Help session on Tuesday

5pm in 1066 TMCB

HW #4 due Wednesday

Page 3: CS 312:  Algorithm Analysis

Objectives

Introduce Divide and Conquer

Apply to Multiplication

Analyze by defining a recurrence relation

Introduce the Convex Hull problem

Page 4: CS 312:  Algorithm Analysis

Thought

“Nothing is particularly hard if you divide it into small jobs.”

-- Henry Ford

Page 5: CS 312:  Algorithm Analysis

Divide and Conquer

... ... ...Solve the smaller pieces.

Page 6: CS 312:  Algorithm Analysis

Recall Efficiency of Multiplication Algorithms

Problem: Multiplication Algorithms:

American British a la Francaise / Russe Arabic …

Efficiency so far:

Page 7: CS 312:  Algorithm Analysis

Divide and Conquer

05025001

05 50 shift 4 = 250000005 01 shift 2 = 50002 50 shift 2 = 1000002 01 shift 0 = 2 2510502

Page 8: CS 312:  Algorithm Analysis

Divide and Conquer

05025001

05 50 shift 4 = 250000005 01 shift 2 = 50002 50 shift 2 = 1000002 01 shift 0 = 2 2510502

Is it correct?

Page 9: CS 312:  Algorithm Analysis

Divide and Conquer

05025001

05 x 50 shift 4 = 250000005 x 01 shift 2 = 50002 x 50 shift 2 = 1000002 x 01 shift 0 = 2 2510502

5001 x 502 10002 025005002510502

Is it correct?

How long does it take on an input of size n? Θ(𝑛2)

Page 10: CS 312:  Algorithm Analysis

Is it Faster?

No.

Page 11: CS 312:  Algorithm Analysis

Can we do better?

Yes. We shall see …

Page 12: CS 312:  Algorithm Analysis

Divide and Conquer

05025001

05 x 50 shift 4 = 250000005 x 01 shift 2 = 50002 x 50 shift 2 = 1000002 x 01 shift 0 = 2 2510502

xw

y z

wy104 +wz102 +xy102 +xz100

product = wy104 + (wz + xy)102 + xz

Page 13: CS 312:  Algorithm Analysis

Divide and Conquer

05025001

05 x 50 shift 4 = 250000005 x 01 shift 2 = 50002 x 50 shift 2 = 1000002 x 01 shift 0 = 2 2510502

xw

y z

wy104 +wz102 +xy102 +xz100

product = wy104 + (wz + xy)102 + xz

Page 14: CS 312:  Algorithm Analysis

Divide and Conquer05025001

xw

y z

product = wy104 + (wz + xy)102 + xz

Let r = (w + x)(y + z) = wy + wz + xy + xzThen wz + xy = r – wy - xz

product = wy104 + (r – wy - xz)102 + xz

= wy104 + ((w+x)(y+z)– wy - xz)102 + xz

Page 15: CS 312:  Algorithm Analysis

Multiply only three timesOriginalproduct = wy104 + (wz + xy)102 + xz100

p = wyq = xzr = (w + x)(y + z)

p104 + (r-p-q)102 + q

= wy104 + (wy + wz + xy + xz - wy - xz)102 + xz= wy104 + (wz + xy)102 + xz

Page 16: CS 312:  Algorithm Analysis

Recursive application

... ... ...

Page 17: CS 312:  Algorithm Analysis

Algorithm

Page 18: CS 312:  Algorithm Analysis

How long does it take? Characterize running time using a recurrence

relation. Let be the amount of time to compute an

answer on an input of size . Then: *

where , the cost of dividing and reassembling sub-results

Answering a question with a question!

* Actually: -- solution in same asymptotic order of growth

Page 19: CS 312:  Algorithm Analysis

Master Theorem:Analysis of Divide & Conquer

Define: = number of sub-instances that must be solved = original instance size (variable) *

= size of sub-instances= polynomial order of , where = cost of dividing and recombining

( ) ( / ) ( ) for ( )( )t n a t n b g n g n n

log

( ) log( ) ( log ) log

( ) logb

d db

d db

a db

n if a b d at n n n if a b d a

n if a b d a

Then:

We’ll prove this! * Assume that is a power of .

Page 20: CS 312:  Algorithm Analysis

Analysis of DC Multiplicationwith Karatsuba’s Insight

a = 3 there are 3 instances of multiplicationb = 2 each is 1/2 the size of the original multiplication d = 1 the divide and the reassemble has linear complexity

( ) 3 ( / 2) ( )t n t n g n

Page 21: CS 312:  Algorithm Analysis

Analysis of DC Multiplicationwith Karatsuba’s Insight

2log 31 1.585since a=3 2 , ( ) ( ) ( )db t n n n

a = 3 there are 3 instances of multiplicationb = 2 each is 1/2 the size of the original multiplication d = 1 the divide and the reassemble has linear complexity

( ) 3 ( / 2) ( )t n t n g n

Page 22: CS 312:  Algorithm Analysis

Another Logarithm Identity

Consequence:

Page 23: CS 312:  Algorithm Analysis

Essentials ofDivide and Conquer

Given problem instance of size Divide into sub-instances of size Recursively solve these sub-instances Appropriately combine their answers

Analyze with the Master Theorem

Page 24: CS 312:  Algorithm Analysis

General: Divide and Conquer

function DC (x) : answer if length(x) < threshold then return adhoc(x) decompose x into a sub-instances x1, x2 … xa of size n/b for i 1 to a do yi DC(xi) recombine the yi’s to get a solution y for x return y

Where : adhoc(x) = is the basic algorithm for small instancesa = the number of divisions at each leveln/b = the fraction of the whole for a sub-instance

Page 25: CS 312:  Algorithm Analysis

Choosing a Threshold Threshold = t means:

“stop dividing when problem size = t”

Threshold depends on Algorithm and implementation and platform

If t is too big? If t is too small?

Page 26: CS 312:  Algorithm Analysis

Recursive vs. Iterative Algorithms Tail Recursion can be written iteratively easily

When results of recursive call aren’t used in an assignment

In fact, all recursive algorithms can be written as iteration. How? By maintaining your own stack

Is the iterative implementation better? Saves on stack allocation and function call

Is the recursive implementation better? More “elegant” to some tastes:

more natural, more readable, more maintainable.

Same algorithmic complexity

Page 27: CS 312:  Algorithm Analysis

Convex Hull (Proj. #2)

Page 28: CS 312:  Algorithm Analysis

Convexity

Page 29: CS 312:  Algorithm Analysis

Convexity

Page 30: CS 312:  Algorithm Analysis

Assignment

Read: Recurrence Relations Notes, Part 1

HW #4: 1.27, 2.1, 2.5(a-e) using the Master Theorem Due Wednesday

Page 31: CS 312:  Algorithm Analysis

Extra Slides

Page 32: CS 312:  Algorithm Analysis

Possible DC Solution: Step 1

Given set of n points Divide into two subsets

L containing the leftmost points R containing the rightmost points

All points with same x coordinate Assign to the same subset Even if this makes the division not exactly

into halves

Page 33: CS 312:  Algorithm Analysis

Possible DC Solution: Step 2

Compute the convex hulls of L and R recursively

Page 34: CS 312:  Algorithm Analysis

Possible DC Solution: Step 3 Combine the left hull CH(L) and the right hull CH(R):

Find the two edges known as the upper and lower common tangents (shown in red)

Common tangent: a line segment in the exterior of both polygons intersecting each polygon at a single vertex or a single edge.

The line containing the common tangent does not intersect the interior of either polygon

Page 35: CS 312:  Algorithm Analysis

Possible DC Solution: Tips Find the upper common tangent:

Scan around the left hull in a clockwise direction and around the right hull in a counter-clockwise direction

Come up with the details of finding the common tangents – hints available in the guidelines document

The tangents divide each hull into two pieces Delete the right edges belonging to the left hull and the

left edges belonging to the right hull