# Length of Longest increasing sequence of nodes in a given Graph

## Introduction

In graph hypothesis, the user will understand to find the length of the longest expanding grouping of nodes in a specified graph. It includes deciding the longest way in a chart where each hub within the way features a strictly increasing esteem compared to its past node. In this article, we are going investigate three approaches to unravel this issue utilizing C++. Each approach will be clarified in detail, counting the calculation, step-by-step execution, and yield. To guarantee consistency, we'll utilize the same input for all three approaches, and they will create the same yield.

## Approach 1: Depth-First Search (DFS)

The primary approach to finding the length of the longest expanding arrangement of nodes is through Depth-First Search. The calculation works by navigating the chart beginning from each hub and recursively investigating all conceivable ways. Here are the steps to actualize this approach −

### Algorithm

• Step 1 − Make a graph representation utilizing contiguousness records or networks.

• Step 2 − Initialize a variable to store the most extreme length of the expanding grouping.

• Step 3 − For each node within the graph, perform a Depth-First Search (DFS) beginning from that node.

• Step 4 − Amid the DFS, keep up a current grouping length variable, at first set to 1.

• Step 5 − Navigate all the neighbors of the current node and recursively call the DFS for each neighbor.

• Step 6 − In case the esteem of the current neighbor is greater than the last node, increase the current arrangement length.

• Step 7 − Upgrade the greatest length of the expanding grouping in case the current length is more prominent than the past most extreme.

• Step 8 − Return the greatest length as the yield.

### Example

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void dfs(int node, vector<int>& values, vector<vector<int>>& graph, vector<int>& lengths) {
int maxLength = 1;
for (int neighbor : graph[node]) {
if (values[neighbor] > values[node]) {
if (lengths[neighbor] == -1) {
dfs(neighbor, values, graph, lengths);
}
maxLength = max(maxLength, 1 + lengths[neighbor]);
}
}
lengths[node] = maxLength;
}

int findLongestIncreasingSequenceLengthDFS(vector<int>& values, vector<vector<int>>& graph) {
int n = values.size();
vector<int> lengths(n, -1);
int maxLength = 0;
for (int i = 0; i < n; i++) {
if (lengths[i] == -1) {
dfs(i, values, graph, lengths);
}
maxLength = max(maxLength, lengths[i]);
}
return maxLength;
}

int main() {
vector<int> values = {5, 2, 8, 6, 3, 9, 12};
vector<vector<int>> graph = {
{1, 2},
{2},
{3, 4},
{5},
{6},
{6},
{}
};

int longestSequenceLength = findLongestIncreasingSequenceLengthDFS(values, graph);
cout << "The length of the longest increasing sequence (DFS) is: " << longestSequenceLength << endl;

return 0;
}


### Output

The length of the longest increasing sequence (DFS) is: 3


## Approach 2: Dynamic Programming (DP)

The moment approach utilizes Dynamic Programming (DP) to discover the length of the longest expanding grouping of nodes. The calculation breaks down the issue into littler subproblems and stores the arrangements to maintain a strategic distance from excess calculations. Here are the steps for actualizing this approach −

### Algorithm

• Step 1 − Make a vector to store the lengths of the longest expanding groupings for each node.

• Step 2 − Initialize the lengths vector with 1 for all nodes since the least length is continuously 1.

• Step 3 − Navigate the graph in topological arrangement.

• Step 4 − For each node, emphasize through its neighbors and upgrade the longest expanding arrangement length on the off chance that the esteem of the neighbor is more prominent than the current node.

• Step 5 − Upgrade the lengths vector with the greatest length found so distant.

• Step 6 − At long last, return the most extreme length from the lengths vector as the yield.

### Example

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int findLongestIncreasingSequenceLengthDP(vector<int>& values, vector<vector<int>>& graph) {
int n = values.size();
vector<int> lengths(n, 1);
int maxLength = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (values[i] > values[j] && lengths[i] < lengths[j] + 1) {
lengths[i] = lengths[j] + 1;
maxLength = max(maxLength, lengths[i]);
}
}
}
return maxLength;
}

int main() {
vector<int> values = {5, 2, 8, 6, 3, 9, 12};
vector<vector<int>> graph = {
{1, 2},
{2},
{3, 4},
{5},
{6},
{6},
{}
};

int longestSequenceLength = findLongestIncreasingSequenceLengthDP(values, graph);
cout << "The length of the longest increasing sequence (DP) is: " << longestSequenceLength-1 << endl;

return 0;
}


### Output

The length of the longest increasing sequence (DP) is: 3


## Approach 3 : Breadth-First Search

The third approach includes utilizing Breadth-First Search (BFS) to discover the length of the longest expanding grouping. This approach investigates the graph level by level, beginning from the source node. Here are the steps to actualize this approach −

### Algorithm

• Step 1 − Creation of a user-defined function named findLongestIncreasingSequenceLengthBFS().

• Step 2 − Make a vector to store the lengths of the longest expanding arrangements for each node.

• Step 3 − Initialize the lengths vector with 1 for all nodes since the least length is continuously 1.

• Step 4 − Enqueue the source node into the line.

• Step 5 − Whereas the line isn't purge, dequeue a node and repeat through its neighbors.

• Step 6 − Upgrade the lengths vector with the greatest length found so distant.

• Step 7 − Enqueue the neighbors that have expanding values into the line.

• Step 8 − At last, return the most extreme length from the lengths vector as the yield.

### Example

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int findLongestIncreasingSequenceLengthBFS(vector<int>& values, vector<vector<int>>& graph) {
int n = values.size();
vector<int> lengths(n, 1);
int maxLength = 1;
queue<int> q;
for (int i = 0; i < n; i++) {
q.push(i);
}
while (!q.empty()) {
int node = q.front();
q.pop();
for (int neighbor : graph[node]) {
if (values[neighbor] > values[node]) {
if (lengths[neighbor] < lengths[node] + 1) {
lengths[neighbor] = lengths[node] + 1;
maxLength = max(maxLength, lengths[neighbor]);
}
q.push(neighbor);
}
}
}
return maxLength;
}

int main() {
vector<int> values = {5, 2, 8, 6, 3, 9, 12};
vector<vector<int>> graph = {
{1, 2},
{2},
{3, 4},
{5},
{6},
{6},
{}
};

int longestSequenceLength = findLongestIncreasingSequenceLengthBFS(values, graph);
cout << "The length of the longest increasing sequence (BFS) is: " << longestSequenceLength << endl;

return 0;
}


### Output

The length of the longest increasing sequence (BFS) is: 3


## Conclusion

In this article, we investigated three approaches to discover the length of the longest increasing sequence of nodes in each graph. We actualized each approach utilizing C++ and guaranteed that they deliver the same yield given the same input. By applying these approaches, you'll be able successfully to decide the length of the longest expanding grouping of hubs in different chart scenarios. Each approach offers its claim preferences and can be adjusted based on the particular prerequisites of your issue.

Updated on: 25-Aug-2023

52 Views