powering a number
play

Powering a number Problem: Compute a n , where n N . Naive - PowerPoint PPT Presentation

CS 3343 -- Spring 2009 Powering a number Problem: Compute a n , where n N . Naive algorithm: ( n ). Divide-and-conquer algorithm: (recursive squaring) a n/ 2 a n/ 2 if n is even; a n = More Divide & Conquer a ( n 1) / 2 a (


  1. CS 3343 -- Spring 2009 Powering a number Problem: Compute a n , where n ∈ N . Naive algorithm: Θ ( n ). Divide-and-conquer algorithm: (recursive squaring) a n/ 2 ⋅ a n/ 2 if n is even; a n = More Divide & Conquer a ( n– 1) / 2 ⋅ a ( n– 1) / 2 ⋅ a if n is odd. Carola Wenk T ( n ) = T ( n /2) + Θ (1) ⇒ T ( n ) = Θ (log n ) . Slides courtesy of Charles Leiserson with small changes by Carola Wenk 2/5/09 CS 3343 Analysis of Algorithms 1 2/5/09 CS 3343 Analysis of Algorithms 2 Computing Fibonacci Fibonacci numbers numbers Recursive definition: Naive recursive squaring: F n = φ n / 5 rounded to the nearest integer. 0 if n = 0; • Recursive squaring: Θ (log n ) time. 1 if n = 1; F n = F n– 1 + F n –2 if n ≥ 2. • This method is unreliable, since floating-point arithmetic is prone to round-off errors. 0 1 1 2 3 5 8 13 21 34 L Bottom-up (one-dimensional dynamic programming) : Naive recursive algorithm: Ω ( φ n ) • Compute F 0 , F 1 , F 2 , …, F n in order, forming ( + (exponential time), where φ = 1 5 ) / 2 each number by summing the two previous. is the golden ratio . • Running time: Θ ( n ). 2/5/09 CS 3343 Analysis of Algorithms 3 2/5/09 CS 3343 Analysis of Algorithms 4 1

  2. Convex Hull Convex Hull: Divide & Conquer • Preprocessing: sort the points by x- • Given a set of pins on a pinboard 4 coordinate 5 3 • And a rubber band around them • Divide the set of points into two sets A and B : • How does the rubber band look 6 • A contains the left  n/2  points, when it snaps tight? 2 • B contains the right  n/2  points • Recursively compute the convex • We represent convex hull as the 1 0 hull of A sequence of points on the convex A B • Recursively compute the convex hull polygon, in counter-clockwise order. hull of B • Merge the two convex hulls 2/5/09 CS 3343 Analysis of Algorithms 5 2/5/09 CS 3343 Analysis of Algorithms 6 Merging Finding the lower tangent 3 • Find upper and lower tangent a = rightmost point of A 4=b • With those tangents the convex hull b = leftmost point of B 4 2 of A ∪ B can be computed from the while T=ab not lower tangent to both convex hulls of A and the convex hull 3 convex hulls of A and B do{ 5 of B in O(n) linear time 5 a=2 while T not lower tangent to 6 1 convex hull of A do{ 7 a=a-1 1 } 0 0 while T not lower tangent to A B A B convex hull of B do{ b=b+1 } right turn or can be checked } left turn? in constant time 2/5/09 CS 3343 Analysis of Algorithms 7 2/5/09 CS 3343 Analysis of Algorithms 8 2

  3. Convex Hull: Runtime Convex Hull: Runtime • Preprocessing: sort the points by x- • Runtime Recurrence: O(n log n) just once coordinate T(n) = 2 T(n/2) + cn • Divide the set of points into two O(1) sets A and B : • A contains the left  n/2  points, • Solves to T(n) = Θ (n log n) • B contains the right  n/2  points • Recursively compute the convex T(n/2) hull of A • Recursively compute the convex T(n/2) hull of B O(n) • Merge the two convex hulls 2/5/09 CS 3343 Analysis of Algorithms 9 2/5/09 CS 3343 Analysis of Algorithms 10 Matrix multiplication Standard algorithm Input: A = [ a ij ], B = [ b ij ]. for i ← 1 to n i , j = 1, 2,… , n . Output: C = [ c ij ] = A ⋅ B . do for j ← 1 to n do c ij ← 0  c c c   a a a   b b b  L L L 11 12 1 n 11 12 1 n 11 12 1 n for k ← 1 to n       c c c a a a b b b L L L  21 22 2 n   21 22 2 n   21 22 2 n  = ⋅ do c ij ← c ij + a ik ⋅ b kj       M M O M M M O M M M O M       c c c a a a b b b  L   L   L  n 1 n 2 nn n 1 n 2 nn n 1 n 2 nn Running time = Θ ( n 3 ) n ∑ = ⋅ c a b ij ik kj = k 1 2/5/09 CS 3343 Analysis of Algorithms 11 2/5/09 CS 3343 Analysis of Algorithms 12 3

  4. Divide-and-conquer algorithm Analysis of D&C algorithm I DEA : T ( n ) = 8 T ( n /2) + Θ ( n 2 ) n × n matrix = 2 × 2 matrix of ( n /2) ×( n /2) submatrices:  r s   a b   e f  work adding = ⋅ # submatrices       t u c d g h       submatrices submatrix size ⋅ C = A B n log ba = n log 2 8 = n 3 ⇒ C ASE 1 ⇒ T ( n ) = Θ ( n 3 ). r = a·e+b·g s = a·f+ b·h 8 recursive mults of ( n /2) ×( n /2) submatrices 4 adds of ( n /2) ×( n /2) submatrices No better than the ordinary algorithm. t = c·e+d·g u = c·f +d·h 2/5/09 CS 3343 Analysis of Algorithms 13 2/5/09 CS 3343 Analysis of Algorithms 14 Strassen’s idea Strassen’s idea • Multiply 2 × 2 matrices with only 7 recursive mults. • Multiply 2 × 2 matrices with only 7 recursive mults. P 1 = a ⋅ ( f – h ) P 1 = a ⋅ ( f – h ) r = P 5 + P 4 – P 2 + P 6 r = P 5 + P 4 – P 2 + P 6 P 2 = ( a + b ) ⋅ h P 2 = ( a + b ) ⋅ h s = P 1 + P 2 = ( a + d )( e + h ) P 3 = ( c + d ) ⋅ e P 3 = ( c + d ) ⋅ e t = P 3 + P 4 + d ( g – e ) – ( a + b ) h P 4 = d ⋅ ( g – e ) P 4 = d ⋅ ( g – e ) u = P 5 + P 1 – P 3 – P 7 + ( b – d )( g + h ) P 5 = ( a + d ) ⋅ ( e + h ) P 5 = ( a + d ) ⋅ ( e + h ) = ae + ah + de + dh 7 mults, 18 adds/subs. 7 mults, 18 adds/subs. P 6 = ( b – d ) ⋅ ( g + h ) P 6 = ( b – d ) ⋅ ( g + h ) + dg –de – ah – bh Note: No reliance on Note: No reliance on P 7 = ( a – c ) ⋅ ( e + f ) P 7 = ( a – c ) ⋅ ( e + f ) + bg + bh – dg – dh commutativity of mult! commutativity of mult! = ae + bg 2/5/09 CS 3343 Analysis of Algorithms 15 2/5/09 CS 3343 Analysis of Algorithms 16 4

  5. Strassen’s algorithm Analysis of Strassen 1. Divide: Partition A and B into T ( n ) = 7 T ( n /2) + Θ ( n 2 ) ( n /2) × ( n /2) submatrices. Form P -terms n log ba = n log 2 7 ≈ n 2.81 ⇒ C ASE 1 ⇒ T ( n ) = Θ ( n log 7 ). to be multiplied using + and – . 2. Conquer: Perform 7 multiplications of The number 2.81 may not seem much smaller than ( n /2) × ( n /2) submatrices recursively. 3, but because the difference is in the exponent, the 3. Combine: Form C using + and – on impact on running time is significant. In fact, ( n /2) × ( n /2) submatrices. Strassen’s algorithm beats the ordinary algorithm on today’s machines for n ≥ 30 or so. T ( n ) = 7 T ( n /2) + Θ ( n 2 ) Best to date (of theoretical interest only): Θ ( n 2.376 L ). 2/5/09 CS 3343 Analysis of Algorithms 17 2/5/09 CS 3343 Analysis of Algorithms 18 Conclusion • Divide and conquer is just one of several powerful techniques for algorithm design. • Divide-and-conquer algorithms can be analyzed using recurrences and the master method (so practice this math). • Can lead to more efficient algorithms 2/5/09 CS 3343 Analysis of Algorithms 19 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend