Skip to content
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

Added Longest Bitonic Subsequence Algo #1774

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions Dynamic Programming/Longest Bitonic Subsequence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Longest Bitonic Subsequence

## Problem Description

A **bitonic subsequence** of a sequence is a subsequence that first increases and then decreases. The **Longest Bitonic Subsequence (LBS)** problem is to find the length of the longest subsequence in a given sequence that is bitonic.

In this specific implementation, we are working with a string and finding the longest bitonic subsequence by treating each character as an element in the sequence.

### Example

**Input:**
`str = "character"`

**Output:**
`Length of Longest Bitonic Subsequence is 5`

**Explanation:**
The longest bitonic subsequence here is `"charer"`, which first increases (`c -> h -> a -> r`) and then decreases (`r -> e -> r`), resulting in a length of 5.

## Approach

We solve this problem using **Dynamic Programming** by breaking it into two main parts:
1. **Longest Increasing Subsequence (LIS):** For each character in the sequence, calculate the length of the longest subsequence ending at that character.
2. **Longest Decreasing Subsequence (LDS):** For each character in the sequence, calculate the length of the longest subsequence starting from that character.

For each position `i`, the length of the longest bitonic subsequence passing through `i` is `LIS[i] + LDS[i] - 1`.

### Time Complexity
The solution has a time complexity of **O(n²)**, where `n` is the length of the input string.

### Space Complexity
The space complexity is **O(n)** for the `inc` and `dec` arrays.
54 changes: 54 additions & 0 deletions Dynamic Programming/Longest Bitonic Subsequence/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <string.h>

int max(int a, int b) {
return (a > b) ? a : b;
}

int longestBitonicSubsequence(const char* str) {
int n = strlen(str);
int inc[n];
int dec[n];

// Initialize all values to 1 for the increasing subsequence
for (int i = 0; i < n; i++) {
inc[i] = 1;
}

// Calculate longest increasing subsequence (LIS) ending at each position
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (str[i] > str[j]) {
inc[i] = max(inc[i], inc[j] + 1);
}
}
}

// Initialize all values to 1 for the decreasing subsequence
for (int i = 0; i < n; i++) {
dec[i] = 1;
}

// Calculate longest decreasing subsequence (LDS) starting at each position
for (int i = n - 2; i >= 0; i--) {
for (int j = n - 1; j > i; j--) {
if (str[i] > str[j]) {
dec[i] = max(dec[i], dec[j] + 1);
}
}
}

// Find the maximum value of inc[i] + dec[i] - 1
int maxLength = 0;
for (int i = 0; i < n; i++) {
maxLength = max(maxLength, inc[i] + dec[i] - 1);
}

return maxLength;
}

int main() {
const char* str = "character";
printf("Length of Longest Bitonic Subsequence is %d\n", longestBitonicSubsequence(str));
return 0;
}
Loading