130 likes | 251 Views
Randomized Algorithms. Prof. Dr. Th. Ottmann University of Freiburg ottmann@informatik.uni-freiburg.de. 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!
E N D
Randomized Algorithms Prof. Dr. Th. Ottmann University of Freiburg ottmann@informatik.uni-freiburg.de
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.
Quicksort Unsorted part A[l,r] in an array A A[l … r-1] p A[l...m – 1] p A[m + 1...r] Quicksort Quicksort
Quicksort Algorithm:Quicksort Input: unsorted part [l, r] of an array A Output: sorted part [l, r] of the array A • ifr > l • then choose pivot-element p = A[r] 3 m = divide(A, l , r) /* partition Awith respect top: A[l],....,A[m – 1] p A[m + 1],...,A[r] */ • Quicksort(A, l , m - 1) Quicksort (A, m + 1, r)
Division • divide(A, l , r): • Yields the index of the pivot elements in A • Can be carried out in time O(r – l)
Worst-Case-Input n elements: Runtime: (n-1) + (n-2) + … + 2 + 1 = n(n-1)/2
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 thenramdomly choose apivot-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)
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? ifn = 2 then return true; if n even then return false; fori = 1 ton/2do if 2i + 1 divides n then return false return true Runtime: (n)
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 pkonly.
Randomised Primality Test Theorem 1: (Fermat‘s theorem) Is p prim and 1 < a < p, then ap-1mod p =1. Theorem 2: Is p prim and 0 < a < p, then the equation a2mod 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.
Randomisierter Primzahltest Algorithmus: Randomisierter Primzahltest 1 1 Wähle a im Bereich [2, n-1] zufällig 2 Berechne an-1 mod n 3 ifan-1 mod n= 1 4 thenn ist möglicherweise prim 5 elsen ist definitiv nicht prim Prob(n ist nicht prim, aber an-1 mod n = 1 ) ?
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.