Skip to content

Corrected API in longest_increasing_subsequence #410

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 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 16 additions & 11 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydatastructs.linear_data_structures.arrays import (
OneDimensionalArray, DynamicArray, Array)
OneDimensionalArray, DynamicArray, DynamicOneDimensionalArray, Array)
from pydatastructs.utils.misc_util import _check_type, _comp
from concurrent.futures import ThreadPoolExecutor
from math import log, floor
Expand Down Expand Up @@ -1010,16 +1010,18 @@ def longest_increasing_subsequence(array):
>>> from pydatastructs import longest_increasing_subsequence as LIS
>>> array = ODA(int, [2, 5, 3, 7, 11, 8, 10, 13, 6])
>>> longest_inc_subsequence = LIS(array)
>>> longest_inc_subsequence
[2, 3, 7, 8, 10, 13]
>>> str(longest_inc_subsequence)
'[2, 3, 7, 8, 10, 13]'
>>> array2 = ODA(int, [3, 4, -1, 5, 8, 2, 2 ,2, 3, 12, 7, 9, 10])
>>> longest_inc_subsequence = LIS(array2)
>>> longest_inc_subsequence
[-1, 2, 3, 7, 9, 10]
>>> str(longest_inc_subsequence)
'[-1, 2, 3, 7, 9, 10]'
"""
n = len(array)
dp = [0]*n
parent = [-1]*n
dp = OneDimensionalArray(int, n)
dp.fill(0)
parent = OneDimensionalArray(int, n)
parent.fill(-1)
length = 0
for i in range(1, n):
if array[i] <= array[dp[0]]:
Expand All @@ -1033,13 +1035,16 @@ def longest_increasing_subsequence(array):
ceil = lower_bound(curr_array, array[i])
dp[ceil] = i
parent[i] = dp[ceil - 1]
ans = []

ans = DynamicOneDimensionalArray(int, 0)
last_index = dp[length]
while last_index != -1:
ans[:0] = [array[last_index]]
ans.append(array[last_index])
last_index = parent[last_index]
return ans
n = ans._last_pos_filled + 1
ans_ODA = OneDimensionalArray(int, n)
for i in range(n):
ans_ODA[n-1-i] = ans[i]
return ans_ODA

def _permutation_util(array, start, end, comp, perm_comp):
size = end - start + 1
Expand Down
10 changes: 5 additions & 5 deletions pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,27 @@ def test_longest_increasing_subsequence():
arr1 = ODA(int, [2, 5, 3, 7, 11, 8, 10, 13, 6])
output = longest_increasing_subsequence(arr1)
expected_result = [2, 3, 7, 8, 10, 13]
assert expected_result == output
assert str(expected_result) == str(output)

arr2 = ODA(int, [3, 4, -1, 5, 8, 2, 2, 2, 3, 12, 7, 9, 10])
output = longest_increasing_subsequence(arr2)
expected_result = [-1, 2, 3, 7, 9, 10]
assert expected_result == output
assert str(expected_result) == str(output)

arr3 = ODA(int, [6, 6, 6, 19, 9])
output = longest_increasing_subsequence(arr3)
expected_result = [6, 9]
assert expected_result == output
assert str(expected_result) == str(output)

arr4 = ODA(int, [5, 4, 4, 3, 3, 6, 6, 8])
output = longest_increasing_subsequence(arr4)
expected_result = [3, 6, 8]
assert expected_result == output
assert str(expected_result) == str(output)

arr5 = ODA(int, [7, 6, 6, 6, 5, 4, 3])
output = longest_increasing_subsequence(arr5)
expected_result = [3]
assert expected_result == output
assert str(expected_result) == str(output)

def _test_permutation_common(array, expected_perms, func):
num_perms = len(expected_perms)
Expand Down