Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Professor name:

Dr. Keyhanopur

Student name: Amirhossein Mohajersoltani


Student number: 401152577

Data Structures and Algorithms final project


Topic2
Algorithm: Longest Increasing Subsequence (LIS) using Dynamic
Programming
Initialization: Two arrays, ‘lis_lengths’ and ‘previous_indices’, are initialized. For example, given the input
sequence ‘[10, 22, 9, 33, 21, 50, 41, 60, 80]’, the initial state of ‘lis_lengths’ might be ‘[1, 1, 1, 1, 1, 1, 1, 1,
1]’, and ‘previous_indices’ might be ‘[-1, -1, -1, -1, -1, -1, -1, -1, -1]’.

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]’.

Sorting Algorithm: Using LIS for Sorting


The ‘sort_using_lis’ function utilizes the LIS found by the dynamic programming algorithm to sort the
input sequence. For example, using the LIS ‘[10, 22, 33, 50, 60, 80]’, the sorted sequence is ‘[10, 22, 33,
50, 60, 80, 9, 21, 41]’.

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.

The space complexity is O(n) as two arrays of size n are used.

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]’.

You might also like