- 58 Views
- Uploaded on
- Presentation posted in: General

Randomized Algorithms

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Randomized Algorithms

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

- 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

- 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

- 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

Ø

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

- 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

- 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

- 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

- Application: cryptography
- No polynomial-time algorithm for testing primality of d-digit number

- 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

- Given N, compute 2N-1%N

- 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