Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Randomized Algorithms Prof. Dr. Th. Ottmann University of Freiburg [email protected] Classes of Randomised Algorithms Las Vegas type • Yield always a correct result. • For a specific input: Performance (runtime) may be bad, but the extected runtime is good! • Example: Randomised version of Quicksort Monte Carlo type (most correctly): • May produce an incorrect result (with a certain error probability). • For each specific input: (Worst case) runtime is good • Example: Randomised primality test. 2 Quicksort Unsorted part A[l,r] in an array A A[l … r-1] A[l...m – 1] Quicksort p p A[m + 1...r] Quicksort 3 Quicksort Algorithm: Quicksort Input: unsorted part [l, r] of an array A Output: sorted part [l, r] of the array A 1 if r > l 2 then choose pivot-element p = A[r] 3 m = divide(A, l , r) /* partition Awith respect to p: A[l],....,A[m – 1] p A[m + 1],...,A[r] */ 4 Quicksort(A, l , m - 1) Quicksort (A, m + 1, r) 4 Division of the Array l r 5 Division divide(A, l , r): • Yields the index of the pivot elements in A • Can be carried out in time O(r – l) 6 Worst-Case-Input n elements: Runtime: (n-1) + (n-2) + … + 2 + 1 = n(n-1)/2 7 Randomised Version of Quicksort Algorithmus: Quicksort Input: unsorted part [l, r] of an array A Output: sorted part [l, r] of the array A if r > l then ramdomly choose a pivot-element p = A[i] in the part [l, r] of the array; exchange A[ i] and A[r]; m = divide(A, l, r); /* divide A with respect to p: A[l],....,A[m – 1] p A[m + 1],...,A[r] */ Quicksort(A, l, m - 1); Quicksort(A, m + 1, r) 8 Primality Test Definition: The natural number p 2 is prime, iff a | p implies a = 1 or a = p. Algorithm: Deterministic primality test (naive version) Input: A natural number n 2 Output: Answer to the question: Is n prime? if n = 2 then return true; if n even then return false; for i = 1 to n/2 do if 2i + 1 divides n then return false return true Runtime: (n) 9 Primality Test Goal: Randomised algorithm • With polynomial runtime • If the algorithm yields the answer “not prime”, then n is definitely not prime. • If the algorithm yields the answer “prime”, then this answer is wrong with a certain error probability p>0 , i.e. n is prime with a certain probability (1- p) only. k iterations of the algorithm: the algorithm yields the wrong answer with probability pk only. 10 Randomised Primality Test Theorem 1: (Fermat‘s theorem) Is p prim and 1 < a < p, then ap-1 mod p = 1. Theorem 2: Is p prim and 0 < a < p, then the equation a2 mod p = 1 Has exactly two solutions, namely a = 1 und a = p – 1. Randomised algorithm: Choose an a with 1 < a < p randomly and check whether it fulfills the test of theorem 1; while computing ap-1 simultaneously check whether the test of theorem 2 is fulfilled for all numbers occurring during the computation of ap-1 using the fast exponentiation method. 11 Randomisierter Primzahltest Algorithmus: Randomisierter Primzahltest 1 1 Wähle a im Bereich [2, n-1] zufällig 2 Berechne an-1 mod n 3 if an-1 mod n = 1 4 then n ist möglicherweise prim 5 else n ist definitiv nicht prim Prob(n ist nicht prim, aber an-1 mod n = 1 ) ? 12 Randomised Primality Test Theorem: Is n not prime, then there are at most n – 4/ 9 numbers 0 < a < n, such that the randomized algorithm for primality testing yields the wrong result. 13