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

Cycle Detection in a Directed Graph (Using DFS) #1794

Merged
merged 1 commit into from
Nov 10, 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
49 changes: 49 additions & 0 deletions Graph Algorithms/Cycle detection using dfs/Readme.md
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.
49 changes: 49 additions & 0 deletions Graph Algorithms/Cycle detection using dfs/code.c
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;
}
Loading