-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1794 from Ananya-vastare/main
Cycle Detection in a Directed Graph (Using DFS)
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Longest Increasing Subsequence in C | ||
|
||
This program calculates the **Longest Increasing Subsequence (LIS)** in an array of integers, using dynamic programming. It allows users to input the array size and its elements, then outputs the length of the longest increasing subsequence. | ||
|
||
## How It Works | ||
|
||
The program uses a dynamic programming approach where: | ||
- An auxiliary array, `lis`, is maintained to store the LIS length at each index. | ||
- For each element, it calculates the maximum LIS that ends at that position by considering all previous elements. | ||
- Finally, it finds the maximum value in the `lis` array to determine the length of the longest increasing subsequence. | ||
|
||
### Time Complexity | ||
The time complexity of this approach is **O(n²)** due to the nested loops. | ||
|
||
### Space Complexity | ||
The space complexity is **O(n)**, as it uses an auxiliary array `lis` of the same length as the input array. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
|
||
- A C compiler (like GCC). | ||
|
||
### Running the Program | ||
|
||
1. **Clone the Repository** (optional if using version control): | ||
```bash | ||
git clone https://github.com/your-username/longest-increasing-subsequence | ||
cd longest-increasing-subsequence | ||
2. **Compile the Code **: | ||
|
||
```bash | ||
Copy code | ||
gcc lis.c -o lis | ||
3. **Run the Program**: | ||
```bash | ||
Copy code | ||
./lis | ||
|
||
**Example Usage** | ||
```bash | ||
Enter the number of elements in the array: 9 | ||
Enter the elements of the array: | ||
10 22 9 33 21 50 41 60 80 | ||
Length of LIS is 6 | ||
In this example, the longest increasing subsequence is [10, 22, 33, 50, 60, 80] with a length of 6. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <stdio.h> | ||
|
||
int longestIncreasingSubsequence(int arr[], int n) | ||
{ | ||
int lis[n]; | ||
for (int i = 0; i < n; i++) | ||
lis[i] = 1; // Initialize LIS values for all indexes as 1 | ||
|
||
for (int i = 1; i < n; i++) | ||
{ | ||
for (int j = 0; j < i; j++) | ||
{ | ||
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) | ||
{ | ||
lis[i] = lis[j] + 1; | ||
} | ||
} | ||
} | ||
|
||
int max = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if (max < lis[i]) | ||
max = lis[i]; | ||
} | ||
return max; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
|
||
// Take the number of elements as input | ||
printf("Enter the number of elements in the array: "); | ||
scanf("%d", &n); | ||
|
||
int arr[n]; | ||
|
||
// Take array elements as input | ||
printf("Enter the elements of the array:\n"); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
// Calculate and print the length of LIS | ||
printf("Length of LIS is %d\n", longestIncreasingSubsequence(arr, n)); | ||
return 0; | ||
} |