This preview shows pages 1–16. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 473: Algorithms Chandra Chekuri [email protected] 3228 Siebel Center University of Illinois, UrbanaChampaign Fall 2009 Chekuri CS473ug Exponentiation Binary Search Part I Exponentiation, Binary Search Chekuri CS473ug Exponentiation Binary Search Exponentiation Input Two numbers: a and integer n ≥ Goal Compute a n Chekuri CS473ug Exponentiation Binary Search Exponentiation Input Two numbers: a and integer n ≥ Goal Compute a n Obvious algorithm: SlowPow(a,n): x = 1; for i = 1 to n do x = x*a Output x O ( n ) multiplications. Chekuri CS473ug Exponentiation Binary Search Fast Exponentiation Observation: a n = a b n / 2 c a d n / 2 e = a b n / 2 c a b n / 2 c a d n / 2 eb n / 2 c . Chekuri CS473ug Exponentiation Binary Search Fast Exponentiation Observation: a n = a b n / 2 c a d n / 2 e = a b n / 2 c a b n / 2 c a d n / 2 eb n / 2 c . FastPow(a,n): if (n = 0) return 1 x = FastPow(a, b n / 2 c ) x = x*x if (n is odd) x = x*a return x Chekuri CS473ug Exponentiation Binary Search Fast Exponentiation Observation: a n = a b n / 2 c a d n / 2 e = a b n / 2 c a b n / 2 c a d n / 2 eb n / 2 c . FastPow(a,n): if (n = 0) return 1 x = FastPow(a, b n / 2 c ) x = x*x if (n is odd) x = x*a return x T ( n ): number of multiplications for n Chekuri CS473ug Exponentiation Binary Search Fast Exponentiation Observation: a n = a b n / 2 c a d n / 2 e = a b n / 2 c a b n / 2 c a d n / 2 eb n / 2 c . FastPow(a,n): if (n = 0) return 1 x = FastPow(a, b n / 2 c ) x = x*x if (n is odd) x = x*a return x T ( n ): number of multiplications for n T ( n ) = T ( b n / 2 c ) + 2 T ( n ) = Chekuri CS473ug Exponentiation Binary Search Fast Exponentiation Observation: a n = a b n / 2 c a d n / 2 e = a b n / 2 c a b n / 2 c a d n / 2 eb n / 2 c . FastPow(a,n): if (n = 0) return 1 x = FastPow(a, b n / 2 c ) x = x*x if (n is odd) x = x*a return x T ( n ): number of multiplications for n T ( n ) = T ( b n / 2 c ) + 2 T ( n ) =Θ(log n ). Chekuri CS473ug Exponentiation Binary Search Complexity of Exponentiation Question: Is SlowPow() a polynomial time algorithm? FastPow? Chekuri CS473ug Exponentiation Binary Search Complexity of Exponentiation Question: Is SlowPow() a polynomial time algorithm? FastPow? Input size: log a + log n Chekuri CS473ug Exponentiation Binary Search Complexity of Exponentiation Question: Is SlowPow() a polynomial time algorithm? FastPow? Input size: log a + log n Output size: Chekuri CS473ug Exponentiation Binary Search Complexity of Exponentiation Question: Is SlowPow() a polynomial time algorithm? FastPow? Input size: log a + log n Output size: n log a . Not necessarily polynomial in input size! Both SlowPow and FastPow are polynomial in output size. Chekuri CS473ug Exponentiation Binary Search Exponentiation modulo a given number Exponentiation in applications: Input Three integers: a , n ≥ 0, p ≥ 2 (typically a prime) Goal Compute a n mod p Chekuri CS473ug Exponentiation Binary Search Exponentiation modulo a given number Exponentiation in applications:...
View
Full
Document
This note was uploaded on 01/22/2012 for the course CS 573 taught by Professor Chekuri,c during the Fall '08 term at University of Illinois, Urbana Champaign.
 Fall '08
 Chekuri,C
 Algorithms, Binary Search

Click to edit the document details