1 / 35

CS 312: Algorithm Analysis

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 )

javen
Download Presentation

CS 312: Algorithm Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Objectives • Introduce Divide and Conquer • Apply to Multiplication • Analyze by defining a recurrence relation • Introduce the Convex Hull problem

  4. Thought “Nothing is particularly hard if you divide it into small jobs.” -- Henry Ford

  5. Divide and Conquer ... ... ... Solve the smaller pieces.

  6. Recall Efficiency of Multiplication Algorithms • Problem: Multiplication • Algorithms: • American • British • a la Francaise / Russe • Arabic • … • Efficiency so far:

  7. 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

  8. 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

  9. 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?

  10. Is it Faster? No.

  11. Can we do better? • Yes. We shall see …

  12. 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

  13. 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

  14. 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

  15. 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

  16. Recursive application ... ... ...

  17. Algorithm

  18. 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

  19. 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 .

  20. 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

  21. 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

  22. Another Logarithm Identity Consequence:

  23. 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

  24. 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 adoyiDC(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

  25. 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?

  26. 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

  27. Convex Hull (Proj. #2)

  28. Convexity

  29. Convexity

  30. Assignment • Read: Recurrence Relations Notes, Part 1 • HW #4: 1.27, 2.1, 2.5(a-e) using the Master Theorem • Due Wednesday

  31. Extra Slides

  32. 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

  33. Possible DC Solution: Step 2 Compute the convex hulls of L and R recursively

  34. 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

  35. 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

More Related