1 / 239

Recursive Algorithms

Recursive Algorithms. Introduction. Applications to Numeric Computation. Complex Numbers. Remember how to multiply 2 complex numbers? (a+bi)(c+di) = [ac –bd] + [ad + bc] i Input: a,b,c,d Output: ac-bd, ad+bc

cfairbanks
Download Presentation

Recursive Algorithms

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

  2. Introduction Applications to Numeric Computation

  3. Complex Numbers • Remember how to multiply 2 complex numbers? • (a+bi)(c+di) = [ac –bd] + [ad + bc] i • Input: a,b,c,d Output: ac-bd, ad+bc • If a real multiplication costs $1 and an addition cost a penny. What is the cheapest way to obtain the output from the input? • Can you do better than $4.02? COSC 3101B, PROF. J. ELDER

  4. Gauss’ Method: • Input: a,b,c,d Output: ac-bd, ad+bc • m1 = ac • m2 = bd • A1 = m1 – m2 = ac-bd • m3 = (a+b)(c+d) = ac + ad + bc + bd • A2 = m3 – m1 – m2 = ad+bc Total Cost? $3.05! COSC 3101B, PROF. J. ELDER

  5. Question: • The Gauss method saves one multiplication out of four. It requires 25% less work. • Could there be a context where performing 3 multiplications for every 4 provides a more dramatic savings? COSC 3101B, PROF. J. ELDER

  6. How to add 2 n-bit numbers. ** ** ** ** ** ** ** ** ** ** ** + COSC 3101B, PROF. J. ELDER

  7. How to add 2 n-bit numbers. ** ** ** ** ** ** ** ** ** * ** ** * + COSC 3101B, PROF. J. ELDER

  8. How to add 2 n-bit numbers. ** ** ** ** ** ** ** ** * ** * ** * ** * + COSC 3101B, PROF. J. ELDER

  9. How to add 2 n-bit numbers. ** ** ** ** ** ** ** * ** * ** * * ** * ** * + COSC 3101B, PROF. J. ELDER

  10. How to add 2 n-bit numbers. ** ** ** ** ** ** * ** * ** * * ** * * ** * ** * + COSC 3101B, PROF. J. ELDER

  11. How to add 2 n-bit numbers. * * * ** * * ** * * ** * * ** * * ** * *** * * ** * * ** * * ** * * ** * ** * + COSC 3101B, PROF. J. ELDER

  12. * * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * ** * + Time complexity of grade school addition On any reasonable computer adding 3 bits can be done in constant time. T(n) = θ(n) COSC 3101B, PROF. J. ELDER

  13. Is there a faster way to add? • QUESTION: Is there an algorithm to add two n-bit numbers whose time grows sub-linearly in n? COSC 3101B, PROF. J. ELDER

  14. Any algorithm for addition must read all of the input bits • Suppose there is a mystery algorithm that does not examine each bit • Give the algorithm a pair of numbers. There must be some unexamined bit position i in one of the numbers • If the algorithm returns the wrong answer, we have found a bug • If the algorithm is correct, flip the bit at position i and give the algorithm this new input. • The algorithm must return the same answer, which now is wrong. COSC 3101B, PROF. J. ELDER

  15. So any algorithm for addition must use time at least linear in the size of the numbers. Grade school addition is essentially as good as it can be. COSC 3101B, PROF. J. ELDER

  16. n2 How to multiply 2 n-bit numbers. * * * * * * * * X * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * COSC 3101B, PROF. J. ELDER

  17. I get it! The total time is bounded by cn2. COSC 3101B, PROF. J. ELDER

  18. a × b = a + a + a + ... + a b I easily ask the question.You take a lifetime to answer it. How to multiply 2 n-bit numbers:Kindergarten Algorithm T(n) = θ(bn) Fast? 10000000000000000000 × 100000000000000000000 COSC 3101B, PROF. J. ELDER

  19. Grade School Addition: Linear timeGrade School Multiplication: Quadratic time Kindergarten Multiplication: Exponential time time # of bits in numbers COSC 3101B, PROF. J. ELDER

  20. Neat! We have demonstrated that multiplication is a harder problem than addition.Mathematical confirmation of our common sense. COSC 3101B, PROF. J. ELDER

  21. Don’t jump to conclusions!We have argued that grade school multiplication uses more time than grade school addition. This is a comparison of the complexity of two algorithms. To argue that multiplication is an inherently harder problem than addition we would have to show that no possible multiplication algorithm runs in linear time. COSC 3101B, PROF. J. ELDER

  22. Grade School Addition: θ(n) timeGrade School Multiplication: θ(n2) time Is there a clever algorithm to multiply two numbers in linear time? COSC 3101B, PROF. J. ELDER

  23. Despite years of research, no one knows! COSC 3101B, PROF. J. ELDER

  24. Is there a faster way to multiply two numbers than the way you learned in grade school? COSC 3101B, PROF. J. ELDER

  25. Recursive Divide And Conquer • DIVIDE a problem into smaller subproblems • CONQUERthem recursively • GLUEthe answers together so as to obtain the answer to the larger problem COSC 3101B, PROF. J. ELDER

  26. Multiplication of 2 n-bit numbers a b • X = • Y = • X = a 2n/2 + b Y = c 2n/2 + d • XY = ac 2n + (ad+bc) 2n/2 + bd c d COSC 3101B, PROF. J. ELDER

  27. Multiplication of 2 n-bit numbers a b • X = • Y = • XY = ac 2n + (ad+bc) 2n/2 + bd c d MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d RETURN MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d) COSC 3101B, PROF. J. ELDER

  28. Time required by MULT • T(n) = time taken by MULT on two n-bit numbers • What is T(n)? What is its growth rate? Is it θ(n2)? COSC 3101B, PROF. J. ELDER

  29. Recurrence Relation • T(1) = k for some constant k • T(n) = 4 T(n/2) + k’ n + k’’ for some constants k’ and k’’ MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d RETURN MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d) COSC 3101B, PROF. J. ELDER

  30. Let’s be concrete • T(1) = 1 • T(n) = 4 T(n/2) + n • How do we unravel T(n) so that we can determine its growth rate? COSC 3101B, PROF. J. ELDER

  31. Technique 1: (Substitution) • Recurrence Relation: T(1) = 1 & T(n) = 4T(n/2) + n • Guess: T(n) = 2n2 – n • Verify: COSC 3101B, PROF. J. ELDER

  32. T(1) 1 T(n) T(n) n n = = T(n/2) T(n/2) T(n/2) T(n/2) T(n/2) T(n/2) T(n/2) T(n/2) Technique 2: Recursion Tree • T(1) = 1 = • T(n) = n + 4 T(n/2) COSC 3101B, PROF. J. ELDER

  33. T(n) n = T(n/2) T(n/2) T(n/2) T(n/2) COSC 3101B, PROF. J. ELDER

  34. n/2 T(n/4) T(n/4) T(n/4) T(n/4) T(n) n = T(n/2) T(n/2) T(n/2) COSC 3101B, PROF. J. ELDER

  35. n/2 n/2 n/2 n/2 T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n) n = COSC 3101B, PROF. J. ELDER

  36. T(n) n = n/2 n/2 n/2 n/2 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 11111111111111111111111111111111 . . . . . . 111111111111111111111111111111111 COSC 3101B, PROF. J. ELDER

  37. n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 n 0 n/2 + n/2 + n/2 + n/2 1 2 Level i is the sum of 4i copies of n/2i i . . . . . . . . . . . . . . . . . . . . . . . . . . 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 COSC 3101B, PROF. J. ELDER

  38. Multiplication of 2 n-bit numbers a b • X = • Y = • XY = ac 2n + (ad+bc) 2n/2 + bd c d MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d RETURN MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d) COSC 3101B, PROF. J. ELDER

  39. =1×n = 4×n/2 = 16×n/4 = 4i ×n/2i = 4logn×n/2logn =nlog4×1 COSC 3101B, PROF. J. ELDER

  40. Divide and Conquer MULT: θ(n2) time Grade School Multiplication: θ(n2) time All that work for nothing! COSC 3101B, PROF. J. ELDER

  41. End of Lecture 8 Oct 3, 2006

  42. MULT revisited MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d RETURN MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d) • MULT calls itself 4 times. Can you see a way to reduce the number of calls? COSC 3101B, PROF. J. ELDER

  43. Gauss’ Idea: Input: a,b,c,d Output:ac, ad+bc, bd • A1 = ac • A3 = bd • m3 = (a+b)(c+d) = ac + ad + bc + bd • A2 = m3 – A1- A3 = ad + bc COSC 3101B, PROF. J. ELDER

  44. Gaussified MULT (Karatsuba 1962) MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d e = MULT(a,c) and f =MULT(b,d) RETURN e2n + (MULT(a+b, c+d) – e - f) 2n/2 + f T(n) = 3 T(n/2) + n (More precisely: T(n) = 2 T(n/2) + T(n/2 + 1) + kn) COSC 3101B, PROF. J. ELDER

  45. n/2 n/2 n/2 n/2 T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n) n = COSC 3101B, PROF. J. ELDER

  46. T(n) n = T(n/2) T(n/2) T(n/2) COSC 3101B, PROF. J. ELDER

  47. n/2 T(n/4) T(n/4) T(n/4) T(n) n = T(n/2) T(n/2) COSC 3101B, PROF. J. ELDER

  48. n/2 n/2 n/2 T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n) n = COSC 3101B, PROF. J. ELDER

  49. n 0 n/2 + n/2 + n/2 1 2 n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 Level i is the sum of 3i copies of n/2i i . . . . . . . . . . . . . . . . . . . . . . . . . . 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 COSC 3101B, PROF. J. ELDER

  50. =1×n = 3×n/2 = 9×n/4 = 3i ×n/2i = 3logn×n/2logn =nlog3×1 COSC 3101B, PROF. J. ELDER

More Related