-
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #336 +/- ##
=============================================
+ Coverage 98.550% 98.572% +0.021%
=============================================
Files 25 25
Lines 3243 3292 +49
=============================================
+ Hits 3196 3245 +49
Misses 47 47
|
From now onwards, keep updating this PR for longest increasing sub-sequence. |
for i in range(1, n): | ||
for j in range(0, i): | ||
if seq[i] > seq[j] and lis[i]< lis[j] + 1: | ||
lis[i] = lis[j]+1 |
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.
It is still O(N^2). Please implement O(N log N) version of this algorithm. Update in this PR only. Do not make new PRs.
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.
Sure
@@ -493,12 +449,9 @@ def bucket_sort(array: Array, **kwargs) -> Array: | |||
|
|||
References | |||
========== | |||
|
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 restore these lines.
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.
Done
Have added nlogn approach for LIS and restored the lines as mentioned before. |
@@ -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: |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use snake case and not camel case.
Are you editing directly on Github? |
Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
yeah, just for the last few commits, as CLI wasn't working properly. Will do it from CLI from tomorrow. |
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 |
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.
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 comment
The 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)?
Sure.
Please provide some updates on the PR or if any difficulty is faced by you so that it can be resolved. As this was stalled for few weeks |
ping @tanyarajhans |
Hey I am really sorry, for some reason I just received the mail right now. I had exams going on. Will add printing LIS in nlogn in few days, is that fine? |
Also, could you please assign this issue to me, as I can see no one is assigned. |
You don't need to worry about the points. As when you will succeed in merging the PR, you will get the assigned points. |
I would like to work on this issue as part of my contribution for GSSoC'21. This method would be a part of the algorithms.py file. I would also like to make use of the lower_bound function which I have recently raised a PR for. The time complexity for this method, with the use of the lower_bound function would be O(nlogn). The function definition goes as follows:-
|
Closing in favor of #363 |
Longest Increasing Subsequence is a famous Dynamic Programming problem which finds out the length of longest increasing subsequence in an array.
This contribution is made as a part of my participation in GirlScript Summer of Code 2021.