Certainly! Let's delve into the Longest Increasing Subsequence (LIS) problem using a recursive approach, similar to how the Climbing Stairs problem was explained. We'll explore the recursive nature, define the recurrence relation, and analyze the time and space complexities.
Longest Increasing Subsequence (LIS) Using Recursion
Problem Statement
Given an array of integers, find the length of the longest subsequence that is strictly increasing. A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.
Recursive Approach
The recursive approach to solving the LIS problem involves making decisions at each index whether to include the current element in the subsequence or not, based on whether it maintains the increasing order.
Recurrence Relation
At each step, for a given index idx and the index of the previous element included in the subsequence prev_idx, we have two choices:
- Exclude the Current Element (
notTake): Move to the next element without including the current one.
- Include the Current Element (
take): If the current element is greater than the previous element in the subsequence, include it and move to the next element.
The recurrence relation can be defined as:
findLIS(idx, prev_idx) = max(
findLIS(idx + 1, prev_idx), // Exclude current element
1 + findLIS(idx + 1, idx) // Include current element (if valid)
)
Base Case:
- If
idx reaches the end of the array (idx == n), return an empty subsequence.
Time and Space Complexity
-
Time Complexity: The recursive approach explores all possible subsequences. For each element, there are two choices (include or exclude), leading to a time complexity of O(2^n), where n is the number of elements in the array.
-
Space Complexity: The space complexity is O(n) due to the recursion stack.
While this approach is straightforward, it's inefficient for large inputs. To optimize, we can use memoization or dynamic programming techniques.
C++ Implementation
Here's the recursive C++ implementation for finding the Longest Increasing Subsequence:
// C++ Program to Print Longest Increasing
// Subsequence using a Recursive Naive Approach
#include <iostream>
#include <vector>
using namespace std;
// Helper Recursive Function to Find LIS
vector<int> findLIS(int idx, int prev_idx, int n, vector<int>& arr) {
// Base Case
if (idx == n)
return {};
// Do not include the current element
vector<int> notTake = findLIS(idx + 1, prev_idx, n, arr);
// Include the current element (if it's greater than the previous element)
vector<int> take;
if (prev_idx == -1 || arr[idx] > arr[prev_idx]) {
take = findLIS(idx + 1, idx, n, arr);
// Insert current element at the beginning
take.insert(take.begin(), arr[idx]);
}
// Return the longer of the two choices
if (take.size() > notTake.size())
return take;
else
return notTake;
}
// Function to get Longest Increasing Subsequence
vector<int> getLis(vector<int>& arr, int n) {
return findLIS(0, -1, n, arr);
}
int main() {
vector<int> arr = {3, 2, 6, 4, 5, 1};
int n = arr.size();
// Get the LIS using the recursive approach
vector<int> lis = getLis(arr, n);
cout << "Longest Increasing Subsequence: ";
for(auto num : lis){
cout << num << " ";
}
return 0;
}
Output:
Longest Increasing Subsequence: 2 4 5
Explanation of the Code
-
findLIS Function:
- Parameters:
idx: Current index in the array.
prev_idx: Index of the previous element included in the LIS.
n: Size of the array.
arr: The input array.
- Base Case: If
idx equals n, return an empty vector, signaling the end of the array.
- Exclude Current Element (
notTake): Recursively call findLIS for the next index without changing prev_idx.
- Include Current Element (
take):
- Check if the current element can be included (i.e., it's greater than the previous element).
- If yes, recursively call
findLIS for the next index, updating prev_idx to idx.
- Insert the current element at the beginning of the returned subsequence.
- Decision: Return the subsequence (
take or notTake) that has a greater length.
-
getLis Function:
- Initializes the recursive process starting from index
0 with no previous element (prev_idx = -1).
-
main Function:
- Defines an example array.
- Calls
getLis to compute the LIS.
- Prints the resulting subsequence.
Optimizing the Recursive Approach
The naive recursive approach has exponential time complexity. To optimize, consider the following methods:
-
Memoization (Top-Down DP):
- Store intermediate results to avoid redundant computations.
- Time Complexity reduces to O(n^2).
-
Dynamic Programming (Bottom-Up):
- Use a DP array where
dp[i] stores the length of the LIS ending at index i.
- Time Complexity is O(n^2).
-
Patience Sorting / Binary Search:
- Achieve O(n log n) time complexity.
- Efficient for large input sizes.
Implementing these optimizations can significantly enhance performance, especially for large datasets.
Summary
- Recursive Nature: At each step, decide to include or exclude the current element based on increasing order.
- Recurrence Relation:
findLIS(idx, prev_idx) = max(findLIS(idx + 1, prev_idx), 1 + findLIS(idx + 1, idx))
- Time Complexity: Exponential O(2^n)
- Space Complexity: Linear O(n)
- Optimization: Utilize memoization or dynamic programming to reduce time complexity to O(n^2) or O(n log n).
The recursive approach provides an intuitive understanding of the problem but is impractical for large inputs without optimization. Leveraging dynamic programming techniques is essential for efficient computation of the Longest Increasing Subsequence.