Dsa
Graphs Dfs
Easy
Longest Path in a DAG
You are given a Directed Acyclic Graph (DAG) represented by a list of edges. Write a program to find the length of the longest path in the graph. The nodes are indexed from 0 to n-1.
Input format:
- The first line contains two integers n (1 <= n <= 100) and m (0 <= m <= 1000).
- The next m lines each contain two integers u and v, indicating a directed edge from node u to node v.
Output format:
- Output a single integer representing the length of the longest path. If there's no path, output 0.
Example:
Input:
4 4
0 1
0 2
1 3
2 3
Output:
3
Key concepts
graphdfslongest path
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now