DAA Assignment 2
DAA Assignment 2
ASSIGNMENT-02
NAME:S.HARSHITHA
ROLL NO:160122733307
CLASS:CSE 3RD YEAR V-SEM
SECTION:CSE-I
DESIGN AND ANALYSIS OF ALGORITHMS-22CSC14N
ASSIGNMENT-02
NAME:S.HARSHITHA ROLL-NO:160122733307
DESIGN AND ANALYSIS OF ALGORITHMS-22CSC14N
ASSIGNMENT-02
the time complexity for this algorithm is O(n2). To keep track which elements are part of the
LIS, every time that Xj<Xi and Lj+1>Li we say that element j precedes element i. In that way
we only need the last element of the LIS and then move backwards until reach the first
element to obtain the whole sequence.
1. Algorithm Design Approach
The Longest Increasing Subsequence (LIS) problem involves finding the length of the longest
subsequence in an array where the elements are strictly increasing. Below are key steps in
designing the solution:
1. Problem Understanding:
o Given an array A of length n, the task is to find the maximum length of
subsequence SSS such that S[i]<S[i+1]S[i] < S[i+1]S[i]<S[i+1] for all valid iii.
o Subsequence implies we can skip elements but must preserve order.
2. Approach Selection:
The two common approaches are:
o Dynamic Programming (DP): Straightforward but less efficient.
o Binary Search + DP: Optimized solution using a greedy approach.
3. Optimal Design:
o Use Binary Search + DP for O(nlogn) time complexity.
o Maintain a list dp, where each element represents the smallest possible end
value of an increasing subsequence of a particular length.
o Use binary search to find the position where an element fits into dp, replacing
it if necessary, or appending it to extend the subsequence.
2. Algorithm
Input:
An array A of length n.
Output:
The length of the Longest Increasing Subsequence.
Pseudocode:
python
Copy code
def length_of_LIS(A):
NAME:S.HARSHITHA ROLL-NO:160122733307
DESIGN AND ANALYSIS OF ALGORITHMS-22CSC14N
ASSIGNMENT-02
4. Time Complexity
For an input array A=[10,9,2,5,3,7,101,18]
1. Traversal (O(n)):
o Each element is processed once.
2. Binary Search (O(logk)):
o Element 10: O(1) to add to dp.
o Element 9: O(log 1) = O(1) to replace 10 in dp.
o Element 101: O(log 4) = O(2) to append to dp.
o Overall, this is log-efficient per element.
NAME:S.HARSHITHA ROLL-NO:160122733307
DESIGN AND ANALYSIS OF ALGORITHMS-22CSC14N
ASSIGNMENT-02
4. Report
The problem is to compute the Longest Increasing Subsequence (LIS) of an array using an
efficient algorithm.
Solution Description:
We use a greedy approach combined with dynamic programming. By maintaining a list dp
that stores the smallest end element of sub sequences, we utilize binary search to efficiently
determine the position of each element.
Implementation:
1. Binary search ensures O(logn) efficiency for updates to dp.
2. The dp array at the end represents an abstraction, not the LIS itself, but its length
gives the correct result.
Strengths of the Approach:
Efficient with O(nlogn) time complexity.
Simple to implement and understand with a focus on key operations.
Example:
For A=[10,9,2,5,3,7,101,18]:
1. Initial dp=[].
2. Process each element:
o 10: dp=[10]
o 9: Replace 10, dp=[9]
o 2: Replace 9, dp=[2]
o 5: Append, dp=[2,5]
o 3: Replace 5, dp=[2,3]
o 7: Append, dp=[2,3,7]
o 101: Append, dp=[2,3,7,101]
o 18: Replace:101, dp=[2,3,7,18]
Result: LIS length = 4.
NAME:S.HARSHITHA ROLL-NO:160122733307