Skip to content

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

Closed
wants to merge 30 commits into from

Conversation

tanyarajhans
Copy link

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.

@codecov
Copy link

codecov bot commented Feb 27, 2021

Codecov Report

Merging #336 (09cf057) into master (a21d00e) will increase coverage by 0.021%.
The diff coverage is 100.000%.

@@              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               
Impacted Files Coverage Δ
pydatastructs/linear_data_structures/__init__.py 100.000% <ø> (ø)
pydatastructs/linear_data_structures/algorithms.py 99.629% <100.000%> (+0.031%) ⬆️
...ucts/miscellaneous_data_structures/disjoint_set.py 100.000% <0.000%> (ø)

Impacted file tree graph

@tanyarajhans tanyarajhans changed the title Demo Added Longest Increasing Subsequence Feb 27, 2021
@czgdp1807
Copy link
Member

From now onwards, keep updating this PR for longest increasing sub-sequence.

Comment on lines 751 to 754
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
Copy link
Member

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.

Copy link
Author

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
==========

Copy link
Member

Choose a reason for hiding this comment

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

Please restore these lines.

Copy link
Author

Choose a reason for hiding this comment

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

Done

@czgdp1807 czgdp1807 added the hard label Feb 28, 2021
@tanyarajhans
Copy link
Author

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:
Copy link
Member

@czgdp1807 czgdp1807 Feb 28, 2021

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.

Copy link
Member

Choose a reason for hiding this comment

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

tailTable = [0 for i in range(size + 1)]
length = 0 # always points empty slot

tailTable[0] = seq[0]
Copy link
Member

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.

@czgdp1807
Copy link
Member

Are you editing directly on Github?

Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
@tanyarajhans
Copy link
Author

Are you editing directly on Github?

yeah, just for the last few commits, as CLI wasn't working properly. Will do it from CLI from tomorrow.

Comment on lines +820 to +827
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
Copy link
Member

@Smit-create Smit-create Mar 9, 2021

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?

Copy link
Member

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.

@Smit-create
Copy link
Member

This contribution is made as a part of my participation in GirlScript Summer of Code 2021.

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

@Smit-create
Copy link
Member

ping @tanyarajhans

@tanyarajhans
Copy link
Author

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?

@tanyarajhans
Copy link
Author

Also, could you please assign this issue to me, as I can see no one is assigned.

@Smit-create
Copy link
Member

You don't need to worry about the points. As when you will succeed in merging the PR, you will get the assigned points.

@Smit-create Smit-create reopened this Mar 16, 2021
@Smit-create Smit-create added the Please take over PRs that can be continued by anyone. label Mar 16, 2021
@sHiVaNgI821
Copy link
Contributor

sHiVaNgI821 commented Mar 21, 2021

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:-

def longest_increasing_subsequence(array: OneDimensionalArray ) -> OneDimensionalArray : 
        # algo (returns a OneDimensionalArray, which is the longest increasing subsequence
                     for that array)
        return longestIncreasingSubsequence

@Smit-create
Copy link
Member

Closing in favor of #363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants