1 / 12

Randomized Algorithms

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.

zared
Download Presentation

Randomized Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Randomized Algorithms

  2. 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)

  3. 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

  4. 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

  5. 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 Ø

  6. 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?

  7. 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

  8. 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

  9. 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

  10. Primality Testing • Application: cryptography • No polynomial-time algorithm for testing primality of d-digit number

  11. 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

  12. 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

More Related