Skip to content
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

Added Sparse table data structure for RMQ #414

Merged
merged 24 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
stack,
binomial_trees,
queue,
disjoint_set
disjoint_set,
sparse_table
)

from .binomial_trees import (
Expand All @@ -27,3 +28,8 @@
DisjointSetForest,
)
__all__.extend(disjoint_set.__all__)

from .sparse_table import (
SparseTable,
)
__all__.extend(sparse_table.__all__)
32 changes: 32 additions & 0 deletions pydatastructs/miscellaneous_data_structures/algorithms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pydatastructs.miscellaneous_data_structures.sparse_table import SparseTable


class RangeMinimumQuery:
def __new__(cls, array, ds='sparse_table'):
if ds == 'array':
pass
pratikgl marked this conversation as resolved.
Show resolved Hide resolved
elif ds == 'sparse_table':
return RangeMinimumQuerySparseTable(cls)
pratikgl marked this conversation as resolved.
Show resolved Hide resolved
else:
raise NotImplementedError(
"Currently %s data structure for range "
"minimum query isn't implemented yet."
% (ds))

def query(L, R):
pratikgl marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError(
"This is an abstract method.")


class RangeMinimumQuerySparseTable(RangeMinimumQuery):

__slots__ = ["sparse_table"]

def __new__(cls, array):
obj = object.__new__(cls)
sparse_table = SparseTable(array)
obj.sparse_table = sparse_table
return obj

def query(self, i, j):
return self.sparse_table.__rangequery__(i, j)
55 changes: 55 additions & 0 deletions pydatastructs/miscellaneous_data_structures/sparse_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pydatastructs.linear_data_structures.arrays import (
MultiDimensionalArray, OneDimensionalArray)
from pydatastructs.utils.misc_util import NoneType
import math

__all__ = ['SparseTable']


class SparseTable(object):
"""
Represents a sparse table

References
==========

.. [1] https://cp-algorithms.com/data_structures/sparse-table.html
"""

__slots__ = ['table']

def __new__(cls, array, N=500000):
pratikgl marked this conversation as resolved.
Show resolved Hide resolved
obj = object.__new__(cls)
MAXLOG = int(math.log2(2*N))

def __init__(self, array, N):
self.table = MultiDimensionalArray(int, 2*N+10, self.MAXLOG)
pratikgl marked this conversation as resolved.
Show resolved Hide resolved
self.logs = OneDimensionalArray(int, 2*N+10)
self.logs[0] = self.logs[1] = 0
for i in range(2, N):
self.logs[i] = self.logs[int(i/2)] + 1

@classmethod
def methods(cls):
return ['__createtable__', '__rangequery__']

def _comp(x, y):
if(x < y):
return x
else:
return y

def __createtable__(self, arr, comp=_comp):
for i in range(len(arr)):
self.table[i][0] = arr[i]
for j in range(1, self.MAXLOG):
for i in range(len(arr) - (1 << j) + 1):
self.table[i][j] = comp(
self.table[i][j-1],
self.table[i+(1 << (j-1))][j-1])
pratikgl marked this conversation as resolved.
Show resolved Hide resolved

def __rangequery__(self, L, R, comp=_comp):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __rangequery__(self, L, R, comp=_comp):
def query(self, left, right, comp=_comp):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a scope of flexibility here. One may find gcd as well by replacing comp with gcd. See, https://gist.github.com/yDeepak1889/dee5cce7fff3354c50ff07a43a5be981
I am not sure about this. We will discuss more in the meeting.

j = self.logs[R-L+1]
return comp(
self.table[L][j],
self.table[R-(1 << j)+1][j])