Mastering Divide and Conquer in Algorithm Analysis
350 likes | 430 Vues
Understand Divide and Conquer, apply to multiplication, illustrate recurrence relation, and solve problems effectively using this technique. Learn about the Convex Hull problem and optimize multiplication algorithms.
Mastering Divide and Conquer in Algorithm Analysis
E N D
Presentation Transcript
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) • 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
Objectives • Introduce Divide and Conquer • Apply to Multiplication • Analyze by defining a recurrence relation • Introduce the Convex Hull problem
Thought “Nothing is particularly hard if you divide it into small jobs.” -- Henry Ford
Divide and Conquer ... ... ... Solve the smaller pieces.
Recall Efficiency of Multiplication Algorithms • Problem: Multiplication • Algorithms: • American • British • a la Francaise / Russe • Arabic • … • Efficiency so far:
Divide and Conquer 0502 5001 05 50 shift 4 = 2500000 05 01 shift 2 = 500 02 50 shift 2 = 10000 02 01 shift 0 = 2 2510502
Divide and Conquer 0502 5001 Is it correct? 05 50 shift 4 = 2500000 05 01 shift 2 = 500 02 50 shift 2 = 10000 02 01 shift 0 = 2 2510502
Divide and Conquer 0502 5001 Is it correct? 5001 x 502 10002 0 2500500 2510502 05 x 50 shift 4 = 2500000 05 x 01 shift 2 = 500 02 x 50 shift 2 = 10000 02 x 01 shift 0 = 2 2510502 How long does it take on an input of size n?
Is it Faster? No.
Can we do better? • Yes. We shall see …
Divide and Conquer w x 05 x 50 shift 4 = 2500000 05 x 01 shift 2 = 500 02 x 50 shift 2 = 10000 02 x 01 shift 0 = 2 2510502 wy104 + wz102 + xy102 + xz100 0502 5001 y z product = wy104 + (wz + xy)102 + xz
Divide and Conquer w x 05 x 50 shift 4 = 2500000 05 x 01 shift 2 = 500 02 x 50 shift 2 = 10000 02 x 01 shift 0 = 2 2510502 wy104 + wz102 + xy102 + xz100 0502 5001 y z product = wy104 + (wz + xy)102 + xz
Divide and Conquer w x product = wy104 + (wz + xy)102 + xz 0502 5001 Let r = (w + x)(y + z) = wy + wz + xy + xz Thenwz + xy = r – wy - xz y z = wy104 + ((w+x)(y+z)– wy - xz)102 + xz product = wy104 + (r – wy - xz)102 + xz
Multiply only three times Original product = wy104 + (wz + xy)102 + xz100 p = wy q = xz r = (w + x)(y + z) p104+ (r-p-q)102 + q = wy104 + (wy + wz + xy + xz - wy - xz)102 + xz = wy104 + (wz + xy)102 + xz
Recursive application ... ... ...
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
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 Then: We’ll prove this! * Assume that is a power of .
Analysis of DC Multiplicationwith Karatsuba’s Insight a = 3 there are 3 instances of multiplication b = 2 each is 1/2 the size of the original multiplication d = 1 the divide and the reassemble has linear complexity
Analysis of DC Multiplicationwith Karatsuba’s Insight a = 3 there are 3 instances of multiplication b = 2 each is 1/2 the size of the original multiplication d = 1 the divide and the reassemble has linear complexity
Another Logarithm Identity Consequence:
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
General: Divide and Conquer functionDC(x) : answer iflength(x) < threshold then returnadhoc(x) decompose x into a sub-instances x1, x2 … xa of size n/b for i 1 to adoyiDC(xi) recombine the yi’s to get a solution y for x return y Where : adhoc(x) = is the basic algorithm for small instances a = the number of divisions at each level n/b = the fraction of the whole for a sub-instance
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?
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
Assignment • Read: Recurrence Relations Notes, Part 1 • HW #4: 1.27, 2.1, 2.5(a-e) using the Master Theorem • Due Wednesday
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
Possible DC Solution: Step 2 Compute the convex hulls of L and R recursively
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
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