130 likes | 234 Views
Randomized Algorithms. Introduction. Algorithm uses a random number to make at least one decision Running time depends on input and random numbers generated Worst-case running time often same as nonrandom algorithm, but no inputs are bad (only random numbers). Example.
E N D
Introduction • Algorithm uses a random number to make at least one decision • Running time depends on input and random numbers generated • Worst-case running time often same as nonrandom algorithm, but no inputs are bad (only random numbers)
Example • Quicksort using randomly generated pivot • running time will vary on same input • choosing first element as pivot will perform poorly for almost-sorted input • almost-sorted input is often common
Generating Random Numbers • Actually pseudorandom • Need to be able to generate a sequence of random numbers • xi+1 = AximodM • x0 – seed • M needs to be large to make period large – sequence will repeat
Skip Lists • Linked list that supports insertion and sorting in O(logN) expected time • Basic idea – each node may link to several other nodes ahead of it in the list • Running time of search if every other node has an additional link to the node two ahead of it in the list? Ø 5 13 22 29 25 2 11 16 Ø
Skip Lists • Running time if every 2ith node has a link to node 2i ahead of it in the list • essentially, a balanced tree • Problems? • how would we insert and delete?
Skip Lists • A level k node has k links to next node with at least i levels • Randomly choose level for a new node Ø Ø 13 Ø 8 10 20 22 29 Ø 2 11 19 23
Skip Lists • Find algorithm http://www.sable.mcgill.ca/~dbelan2/cs251/skip_lists.html 1) Start at the highest level of the list. 2) Move forward following the pointers at the same level until the next key is greater than the searched key 3) If the current level is not the lowest, go down one level and repeat the search at that level from the current node 4) Stop when the level is 1 and the next key is greater than the searched key 5) If the current key is the searched key return the value of that node. Otherwise, return a failure. Ø Ø 13 Ø 8 10 20 22 29 Ø 2 11 19 23
Skip Lists • Insert algorithm • similar to find algorithm • keep track of points where you switch to lower level • set new node pointers • update pointers to new node Ø Ø 13 Ø 8 10 20 22 29 Ø 2 11 19 23
Primality Testing • Application: cryptography • No polynomial-time algorithm for testing primality of d-digit number
Primality Testing • Fermat’s Lesser Theorem: If P is prime, and 0 < A < P, then AP-1≡1(modP) • AP-1 % P = 1 • Algorithm • Given N, compute 2N-1%N • if result is not 1, N is not prime • if result is 1, N is robably prime – but not necessarily • Randomly choose A – but does not always work
Primality Testing • Add additional test to improve result • Theorem: if P is prime and 0 < X < P, the only solutions to X2≡ 1(modP) are X=1, P-1 • Test as we calculate AN-1