Chapter 6 Randomization Algorithm Theory WS 2012/13 Fabian Kuhn
Randomization Randomized Algorithm: An algorithm that uses (or can use) random coin flips in order to make decisions We will see: randomization can be a powerful tool to Make algorithms faster Make algorithms simpler Make the analysis simpler Sometimes it s also the opposite Allow to solve problems (efficiently) that cannot be solved (efficiently) without randomization True in some computational models (e.g., for distributed algorithms) Not clear in the standard sequential model Algorithm Theory, WS 2012/13 Fabian Kuhn 2
Contention Resolution A simple starter example (from distributed computing) Allows to introduce important concepts and to repeat some basic probability theory Setting: processes, 1 resource (e.g., shared database, communication channel, ) There are time slots 1,2,3, In each time slot, only one client can access the resource All clients need to regularly access the resource If client tries to access the resource in slot : Successful iff no other client tries to access the resource in slot Algorithm Theory, WS 2012/13 Fabian Kuhn 3
Algorithm Algorithm Ideas: Accessing the resource deterministically seems hard need to make sure that processes access the resource at different times or at least: often only a single process tries to access the resource Randomized solution: In each time slot, each process tries with probability. Analysis: How large should be? How long does it take until some process succeeds? How long does it take until all processes succeed? What are the probabilistic guarantees? Algorithm Theory, WS 2012/13 Fabian Kuhn 4
Analysis Events:, : process tries to access the resource in time slot Complementary event:, P,, P, 1, : process is successful in time slot,,, Success probability (for process ): Algorithm Theory, WS 2012/13 Fabian Kuhn 5
Fixing P, 1 is maximized for P, 1 11. Asymptotics: For 2: 1 4 11 1 11 1 2 Success probability: P, Algorithm Theory, WS 2012/13 Fabian Kuhn 6
Time Until First Success Random Variable : if proc. is successful in slot for the first time Distribution: is geometrically distributed with parameter P, 1 11 Expected time until first success: 1. Algorithm Theory, WS 2012/13 Fabian Kuhn 7
Time Until First Success Failure Event, : Process does not succeed in time slots 1,, The events, are independent for different : P, P, P, 1 1 11 We know that P, : P, 1 1 Algorithm Theory, WS 2012/13 Fabian Kuhn 8
Time Until First Success No success by time : P, : P, Generally if Θ: constant success probability ln: P, For success probability 1, we need Θ log. We say that succeeds with high probability in log time. Algorithm Theory, WS 2012/13 Fabian Kuhn 9
Time Until All Processes Succeed Event : some process has not succeeded by time, Union Bound: For events,,, P P Probability that not all processes have succeeded by time : P P, P,. Algorithm Theory, WS 2012/13 Fabian Kuhn 10
Time Until All Processes Succeed Claim: With high probability, all processes succeed in the first log time slots. Proof: P / Set 1 ln Remark: Θ log time slots are necessary for all processes to succeed with reasonable probability Algorithm Theory, WS 2012/13 Fabian Kuhn 11
Primality Testing Problem: Given a natural number 2, is a prime number? Simple primality test: 1. if is even then 2. return 2 3. for 1to 2 do 4. if 2 1 divides then 5. return false 6. return true Running time: Algorithm Theory, WS 2012/13 Fabian Kuhn 12
A Better Algorithm? How can we test primality efficiently? We need a little bit of basic number theory Square Roots of Unity: In, where is a prime, the only solutions of the equation 1 mod are 1 mod If we find an 1 mod such that 1 mod, we can conclude that is not a prime. Algorithm Theory, WS 2012/13 Fabian Kuhn 13
Algorithm Idea Claim: Let 2be a prime number such that 12 for an integer 0and some odd integer 3. Then for all, 1 mod 1 mod for some 0. Proof: Fermat s Little Theorem: Given a prime number, : 1 mod Algorithm Theory, WS 2012/13 Fabian Kuhn 14
Primality Test We have: If is an odd prime and 12 for an integer 0 and an odd integer 3. Then for all 1,,1, 1 mod 1 mod for some 0. Idea: If we find an 1,,1such that 1 mod 1 mod for all 0, we can conclude that is not a prime. For every odd composite 2, at least of all possible satisfy the above condition How can we find such a witness efficiently? Algorithm Theory, WS 2012/13 Fabian Kuhn 15
Miller Rabin Primality Test Given a natural number 2, is a prime number? Miller Rabin Test: 1. if is even then return 2 2. compute, such that 12 ; 3. choose 2,,2uniformly at random; 4. mod ; 5. if 1or 1then return true; 6. for 1to 1do 7. mod ; 8. if 1then return true; 9. return false; Algorithm Theory, WS 2012/13 Fabian Kuhn 16
Analysis Theorem: If is prime, the Miller Rabin test always returns true. If is composite, the Miller Rabin test returns false with probability at least. Proof: If is prime, the test works for all values of If is composite, we need to pick a good witness Corollary: If the Miller Rabin test is repeated times, it fails to detect a composite number with probability at most 4. Algorithm Theory, WS 2012/13 Fabian Kuhn 17
Running Time Cost of Modular Arithmetic: Representation of a number : log bits Cost of adding two numbers mod : Cost of multiplying two numbers mod : It s like multiplying degree log polynomials use FFT to compute Algorithm Theory, WS 2012/13 Fabian Kuhn 18
Running Time Cost of exponentiation mod : Can be done using log multiplications Base 2 representation of : 2 Fast exponentiation: 1. 1; 2. for logto 0 do 3. mod ; 4. if 1then mod ; 5. return ; Example: 22 10110 Algorithm Theory, WS 2012/13 Fabian Kuhn 19
Running Time Theorem: One iteration of the Miller Rabin test can be implemented with running time log log log log log log. 1. if is even then return 2 2. compute, such that 12 ; 3. choose 2,,2uniformly at random; 4. mod ; 5. if 1or 1then return true; 6. for 1to 1do 7. mod ; 8. if 1then return true; 9. return false; Algorithm Theory, WS 2012/13 Fabian Kuhn 20
Deterministic Primality Test If a conjecture called the generalized Riemann hypothesis (GRH) is true, the Miller Rabin test can be turned into a polynomialtime, deterministic algorithm It is then sufficient to try all 1,, log It has long not been proven whether a deterministic, polynomial time algorithm exist In 2002, Agrawal, Kayal, and Saxena gave an log time deterministic algorithm Has been improved to log In practice, the randomized Miller Rabin test is still the fastest algorithm Algorithm Theory, WS 2012/13 Fabian Kuhn 21