Skip to content

feat: add longest alternating subsequence #624

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ Algorithms

.. autofunction:: pydatastructs.jump_search

.. autofunction:: pydatastructs.intro_sort
.. autofunction:: pydatastructs.intro_sort

.. autofunction:: pydatastructs.longest_increasing_subsequence
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
jump_search,
selection_sort,
insertion_sort,
intro_sort
intro_sort,
longest_alternating_subsequence,
)
__all__.extend(algorithms.__all__)
69 changes: 68 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
'jump_search',
'selection_sort',
'insertion_sort',
'intro_sort'
'intro_sort',
'longest_alternating_subsequence',
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -1850,3 +1851,69 @@ def partition(array, lower, upper):
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)

return array

def longest_alternating_subsequence(array: OneDimensionalArray, **kwargs) -> int:
"""
Finds the longest alternating subsequence in the given array.
An alternating sequence is one in which the elements are in alternating order,
i.e., a sequence where the differences between consecutive elements alternate
between positive and negative.

Parameters
==========

array: OneDimensionalArray
The array from which the longest alternating subsequence is to be found.
backend: pydatastructs.Backend
The backend to be used.
Optional, by default, the best available backend is used.

Returns
=======

output: OneDimensionalArray
The longest alternating subsequence.

Examples
========

>>> from pydatastructs import OneDimensionalArray, longest_alternating_subsequence
>>> arr = OneDimensionalArray(int, [1, 5, 4])
>>> las = longest_alternating_subsequence(arr)
>>> las
3
>>> arr = OneDimensionalArray(int, [1, 5, 4, 3, 2, 6, 7])
>>> las = longest_alternating_subsequence(arr)
>>> las
4

References
==========

.. [1] https://en.wikipedia.org/wiki/Longest_alternating_subsequence

Note
====

The data types of elements in the array should be comparable.
"""
raise_if_backend_is_not_python(
longest_alternating_subsequence, kwargs.get('backend', Backend.PYTHON))

increasing = 1
decreasing = 1
n = len(array)

# Iterate from second element
for i in range(1, n):

# Increasing changes if decreasing changes
if (array[i] > array[i-1]):
increasing = decreasing + 1

# Decreasing changes if increasing changes
elif (array[i] < array[i-1]):
decreasing = increasing + 1

# Return the maximum length
return max(increasing, decreasing)
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to return the increasing sequence as well and not just the length.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

31 changes: 30 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
selection_sort, insertion_sort, intro_sort, Backend)
selection_sort, insertion_sort, intro_sort, longest_alternating_subsequence, Backend)

from pydatastructs.utils.raises_util import raises
import random
Expand Down Expand Up @@ -414,3 +414,32 @@ def test_binary_search():
def test_jump_search():
_test_common_search(jump_search)
_test_common_search(jump_search, backend=Backend.CPP)


def test_longest_alternating_subsequence():
ODA = OneDimensionalArray

arr1 = ODA(int, [-4, 3, -5, 9, 10, 12, 2, -1])
output: int = longest_alternating_subsequence(arr1)
expected_result = [3, -5, 9, 2, -1]
assert len(expected_result) == output

arr2 = ODA(int, [10, 22, 9, 33, 49, 50, 31, 60])
output: int = longest_alternating_subsequence(arr2)
expected_result = [10, 22, 9, 33, 31, 60]
assert len(expected_result) == output

arr3 = ODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9])
output: int = longest_alternating_subsequence(arr3)
expected_result = [1, 2]
assert len(expected_result) == output

arr4 = ODA(int, [9, 8, 7, 6, 5, 4, 3, 2, 1])
output: int = longest_alternating_subsequence(arr4)
expected_result = [9, 8]
assert len(expected_result) == output

arr5 = ODA(int, [1, 5, 4, 3, 2, 1, 6, 7, 8, 9])
output: int = longest_alternating_subsequence(arr5)
expected_result = [1, 5, 4, 6]
assert len(expected_result) == output
Loading