-
-
Notifications
You must be signed in to change notification settings - Fork 48.9k
Added randomized algorithms #13495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Added randomized algorithms #13495
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import random | ||
Check failure on line 1 in randomized_algorithms/miller_rabin_primality test.py
|
||
|
||
|
||
def miller_rabin_primality_test(n: int, k: int = 5) -> bool: | ||
""" | ||
Probabilistic primality test using Miller-Rabin algorithm. | ||
|
||
Args: | ||
n (int): Number to test for primality. | ||
k (int): Number of iterations for accuracy. | ||
|
||
Returns: | ||
bool: True if n is probably prime, False if composite. | ||
|
||
Examples: | ||
>>> miller_rabin_primality_test(2) | ||
True | ||
>>> miller_rabin_primality_test(15) | ||
False | ||
>>> miller_rabin_primality_test(17) | ||
True | ||
""" | ||
if n <= 1: | ||
return False | ||
if n <= 3: | ||
return True | ||
if n % 2 == 0: | ||
return False | ||
|
||
# Write n-1 as 2^r * d | ||
d = n - 1 | ||
r = 0 | ||
while d % 2 == 0: | ||
d //= 2 | ||
r += 1 | ||
|
||
for _ in range(k): | ||
a = random.randint(2, n - 2) | ||
x = pow(a, d, n) | ||
if x == 1 or x == n - 1: | ||
continue | ||
for _ in range(r - 1): | ||
x = pow(x, 2, n) | ||
if x == n - 1: | ||
break | ||
else: | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import random | ||
Check failure on line 1 in randomized_algorithms/randomized_quicksort.py
|
||
from typing import List | ||
Check failure on line 2 in randomized_algorithms/randomized_quicksort.py
|
||
|
||
|
||
def randomized_quicksort(arr: List[int]) -> List[int]: | ||
Check failure on line 5 in randomized_algorithms/randomized_quicksort.py
|
||
""" | ||
Sorts a list of integers using randomized QuickSort algorithm. | ||
|
||
Args: | ||
arr (List[int]): List of integers to sort. | ||
|
||
Returns: | ||
List[int]: Sorted list of integers. | ||
|
||
Examples: | ||
>>> randomized_quicksort([3, 6, 1, 8, 4]) | ||
[1, 3, 4, 6, 8] | ||
>>> randomized_quicksort([]) | ||
[] | ||
""" | ||
if len(arr) <= 1: | ||
return arr | ||
pivot = random.choice(arr) | ||
less = [x for x in arr if x < pivot] | ||
equal = [x for x in arr if x == pivot] | ||
greater = [x for x in arr if x > pivot] | ||
return randomized_quicksort(less) + equal + randomized_quicksort(greater) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import random | ||
Check failure on line 1 in randomized_algorithms/reservoir_sampling.py
|
||
from typing import List, Iterator, TypeVar | ||
Check failure on line 2 in randomized_algorithms/reservoir_sampling.py
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]: | ||
Check failure on line 7 in randomized_algorithms/reservoir_sampling.py
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Randomly select k items from a stream of unknown length using reservoir sampling. | ||
|
||
Args: | ||
stream (Iterator[T]): Input data stream. | ||
k (int): Number of items to sample. | ||
|
||
Returns: | ||
List[T]: List of k randomly sampled items. | ||
|
||
Examples: | ||
>>> stream = iter(range(1, 11)) | ||
>>> len(reservoir_sampling(stream, 5)) | ||
5 | ||
""" | ||
reservoir: List[T] = [] | ||
for i, item in enumerate(stream): | ||
if i < k: | ||
reservoir.append(item) | ||
else: | ||
j = random.randint(0, i) | ||
if j < k: | ||
reservoir[j] = item | ||
return reservoir | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n
Please provide descriptive name for the parameter:
k