-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Added data_structures/arrays/sparse_table.py #10437
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e8cd5c1
Create sparse_table.py
hollowcrust 25d453e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e232e37
Descriptive names for variables
hollowcrust fdba816
Fix ruff check error
hollowcrust 6be290c
Update sparse_table.py
hollowcrust 01ee53a
Add comments, change variable names
hollowcrust 2750717
Merge branch 'TheAlgorithms:master' into patch-sparse
hollowcrust 60e1a07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 01be1ea
Fix typo
hollowcrust 78c0f81
Update sparse_table.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Sparse table is a data structure that allows answering range queries on | ||
a static array, i.e. the elements do not change throughout all the queries. | ||
|
||
The implementation below will solve the problem of Range Minimum Query: | ||
Finding the minimum value of a subset [L..R] of a static array. | ||
|
||
Overall time complexity: O(nlogn) | ||
Overall space complexity: O(nlogn) | ||
|
||
Wikipedia link: https://en.wikipedia.org/wiki/Range_minimum_query | ||
""" | ||
|
||
|
||
def build_sparse_table(arr: list[int], arr_length: int) -> list[list[int]]: | ||
""" | ||
Precompute range minimum queries with power of two length | ||
and store the precomputed values in a table. | ||
|
||
>>> build_sparse_table([8, 1, 0, 3, 4, 9, 3], 7) | ||
[[8, 1, 0, 3, 4, 9, 3], [1, 0, 0, 3, 4, 3, 0], [0, 0, 0, 3, 0, 0, 0]] | ||
>>> build_sparse_table([3, 1, 9], 3) | ||
[[3, 1, 9], [1, 1, 0]] | ||
>>> build_sparse_table([], 0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: math domain error | ||
""" | ||
import math | ||
|
||
if arr == []: | ||
raise ValueError("math domain error") | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Initialise lookup table | ||
k = int(math.log2(arr_length)) + 1 | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lookup = [[0 for i in range(arr_length)] for j in range(k)] | ||
|
||
for i in range(arr_length): | ||
lookup[0][i] = arr[i] | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
j = 1 | ||
|
||
while (1 << j) <= arr_length: | ||
# Compute the minimum value for all intervals with size (2 ** j) | ||
i = 0 | ||
while (i + (1 << j) - 1) < arr_length: | ||
lookup[j][i] = min(lookup[j - 1][i + (1 << (j - 1))], lookup[j - 1][i]) | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
i += 1 | ||
|
||
j += 1 | ||
|
||
return lookup | ||
|
||
|
||
def query(lookup: list[list[int]], left_bound: int, right_bound: int) -> int: | ||
""" | ||
>>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3], 7), 0, 4) | ||
0 | ||
>>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3], 7), 4, 6) | ||
3 | ||
>>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3], 7), 0, 11) | ||
Traceback (most recent call last): | ||
... | ||
IndexError: list index out of range | ||
|
||
>>> query(build_sparse_table([3, 1, 9], 3), 2, 2) | ||
9 | ||
>>> query(build_sparse_table([3, 1, 9], 3), 0, 1) | ||
1 | ||
|
||
>>> query(build_sparse_table([], 0), 0, 0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: math domain error | ||
""" | ||
import math | ||
|
||
if lookup == []: | ||
raise ValueError("math domain error") | ||
if left_bound < 0 or right_bound >= len(lookup[0]): | ||
raise IndexError("list index out of range") | ||
|
||
""" | ||
Find the highest power of 2 | ||
that is at least the number of elements in a given range. | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
j = int(math.log2(right_bound - left_bound + 1)) | ||
return min(lookup[j][right_bound - (1 << j) + 1], lookup[j][left_bound]) | ||
hollowcrust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.