Skip to content

Commit

Permalink
Merge pull request #1794 from Ananya-vastare/main
Browse files Browse the repository at this point in the history
Cycle Detection in a Directed Graph (Using DFS)
  • Loading branch information
pankaj-bind authored Nov 10, 2024
2 parents 0cb932e + 976a881 commit 6ccac0e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
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;
}

0 comments on commit 6ccac0e

Please sign in to comment.