Longest Path in DAG

Given a directed acyclic graph (DAG) with V vertices labeled from 1 to V, find the length of the longest directed path in the graph. The graph is represented using an adjacency list.

The graph is guaranteed to be a DAG (Directed Acyclic Graph).

Your task is to Find the length of the longest directed path in the graph.

Example 1:

Input: N = 4 , edges = [[1,2], [1,3], [3,2], [2,4], [3,4]]

Output: 3

Explanation : Longest path: 1 → 3 → 2 → 4

Length = 3

Example 2:

Input: N = 6 , edges = [[2,3], [4,5], [5,6]]

Output: 2

Explanation: Longest path: 4 → 5 → 6

Length = 2

Now Your Turn!

Pick the correct output for the given input

Input: N = 6 ,edges = [[1,2], [1,3], [2,4], [3,4], [4,5], [5,6]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 ≤ N ≤ 105
  • 1 ≤ u, v ≤ N

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int longestPath(int n, vector<vector<int>>& edges) {
// Write your code here
return 0;
}
};
Test Case

Input:

N
Edges