Presentation is loading. Please wait.

Presentation is loading. Please wait.

Randomized Algorithms. Introduction Algorithm uses a random number to make at least one decision Running time depends on input and random numbers generated.

Similar presentations


Presentation on theme: "Randomized Algorithms. Introduction Algorithm uses a random number to make at least one decision Running time depends on input and random numbers generated."— 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 x i+1 = Ax i modM x 0 – 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? Ø Ø 29 25 22 16 13 11 5 2

6 Skip Lists Running time if every 2 i th node has a link to node 2 i 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 Ø Ø 8 2 2210 1119 20 23 29 13 Ø Ø

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. Ø Ø 8 2 2210 1119 20 23 29 13 Ø Ø

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 Ø Ø 8 2 2210 1119 20 23 29 13 Ø Ø

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 A P-1 ≡1(modP) –A P-1 % P = 1 Algorithm –Given N, compute 2 N-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 X 2 ≡ 1(modP) are X=1, P-1 Test as we calculate A N-1


Download ppt "Randomized Algorithms. Introduction Algorithm uses a random number to make at least one decision Running time depends on input and random numbers generated."

Similar presentations


Ads by Google