-
Notifications
You must be signed in to change notification settings - Fork 312
Added Longest Increasing Subsequence #336
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
Changes from all commits
e882023
1cc929b
1ee32a6
1dd7734
63beb7f
a85ba89
5ca042f
9c88875
e153637
78c97d5
613c5aa
2be1290
e53706b
3ed9d54
bd7c943
cfc51d2
167fd7e
7a4c2bc
f3bc1d6
fa482ea
3ce0a31
9b28143
cb1f85c
99066c5
86e0d84
6fdef7a
2adf931
9fb7ba2
d5c40e1
09cf057
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 |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
'bucket_sort', | ||
'cocktail_shaker_sort', | ||
'quick_sort', | ||
'longest_common_subsequence' | ||
'longest_common_subsequence', | ||
'longest_increasing_subsequence' | ||
] | ||
|
||
def _merge(array, sl, el, sr, er, end, comp): | ||
|
@@ -787,3 +788,65 @@ def longest_common_subsequence(seq1: OneDimensionalArray, seq2: OneDimensionalAr | |
check_mat[i][j] = check_mat[i][j-1] | ||
|
||
return OneDimensionalArray(seq1._dtype, check_mat[row][col][-1]) | ||
|
||
def longest_increasing_subsequence(seq: OneDimensionalArray) -> int: | ||
""" | ||
Finds the length of longest increasing subsequence of the given sequences. | ||
Parameters | ||
======== | ||
seq: OneDimensionalArray | ||
The sequence. | ||
Returns | ||
======= | ||
output: int | ||
The length of longest increasing subsequence. | ||
Examples | ||
======== | ||
>>> from pydatastructs import longest_increasing_subsequence as LIS, OneDimensionalArray as ODA | ||
>>> arr1 = [1, 3, 9, 4, 6] | ||
>>> lis = LIS(arr1) | ||
>>> str(lis) | ||
'4' | ||
>>> arr1 = [8, 7, 2, 12, 16, 28, 44] | ||
>>> lis = LIS(arr1) | ||
>>> str(lis) | ||
'5' | ||
|
||
References | ||
========== | ||
.. [1] https://en.wikipedia.org/wiki/Longest_increasing_subsequence | ||
|
||
""" | ||
def CeilIndex(A, l, r, key): | ||
while (r - l > 1): | ||
m = l + (r - l)//2 | ||
if (A[m] >= key): | ||
r = m | ||
else: | ||
l = m | ||
return r | ||
Comment on lines
+820
to
+827
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. Can we just define and use methods or functions on sorted ODA for finding lower_bound and upper_bound (similar to how it works in cpp)? Instead of defining such private functions? 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.
Sure. |
||
|
||
size = len(seq) | ||
tailTable = [0 for i in range(size + 1)] | ||
length = 0 # always points empty slot | ||
|
||
tailTable[0] = seq[0] | ||
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. Use snake case and not camel case. |
||
length = 1 | ||
for i in range(1, size): | ||
if (seq[i] < tailTable[0]): | ||
|
||
# new smallest value | ||
tailTable[0] = seq[i] | ||
elif (seq[i] > tailTable[length-1]): | ||
|
||
# seq[i] wants to extend | ||
# largest subsequence | ||
tailTable[length] = seq[i] | ||
length+= 1 | ||
else: | ||
# seq[i] wants to be current | ||
# end candidate of an existing | ||
# subsequence. It will replace | ||
# ceil value in tailTable | ||
tailTable[CeilIndex(tailTable, -1, length-1, seq[i])] = seq[i] | ||
return length |
Uh oh!
There was an error while loading. Please reload this page.
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.
The function currently returns only the length. There should be a possibility of obtaining the longest increasing sub-sequence as well.
For example, if
ODA([1, 2, -1, 3, 4, 5])
is the input then,(ODA[1, 2, 3, 4, 5], 5)
should be the output. Specifically, the first element of the tuple is the LIS and the second is it's length though returning only the sequence will suffice.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 refer to this for restoring the sequence: https://cp-algorithms.com/sequences/longest_increasing_subsequence.html#toc-tgt-8