Professional Documents
Culture Documents
Problem 2
Problem 2
Dr. Keyhanopur
Dynamic Programming: Iterating over the input sequence, the algorithm updates ‘lis_lengths’ and
‘previous_indices’. For example, during the iteration, ‘lis_lengths’ becomes ‘[1, 2, 1, 3, 2, 4, 4, 5, 6]’, and
‘previous_indices’ might be ‘[ -1, -1, 0, 1, 4, 2, 5, 6, 7]’.
Reconstruction of LIS: Finding the index with the maximum length in ‘lis_lengths’, let's say index ‘8’,
the LIS is reconstructed as ‘[10, 22, 33, 50, 60, 80]’.
Data Structure:
Arrays: The dynamic programming algorithm uses two arrays, ‘lis_lengths’ and ‘previous_indices’, to
store information about the longest increasing subsequence.
List (lis_sequence): The LIS is represented as a list (‘lis_sequence’), which is constructed by traversing the
‘previous_indices’ array. In our example, ‘lis_sequence’ is ‘[10, 22, 33, 50, 60, 80]’.
Complexity Analysis:
The dynamic programming algorithm has a time complexity of O(n^2), where n is the length of the input
sequence. For our example, with a sequence of length 9, the time complexity is proportional to 81
operations.
Note:
Given the input sequence ‘[10, 22, 9, 33, 21, 50, 41, 60, 80]’, the longest increasing subsequence is ‘[10,
22, 33, 50, 60, 80]’, and the sorted sequence using this LIS is ‘[10, 22, 33, 50, 60, 80, 9, 21, 41]’.