Another strategy you might consider is to begin with a topological sort of the graph, followed by using dynamic programming to determine the longest path within the graph. Here is a Python implementation of this method: from collections import defaultdict def longestPath ( edges , n ) : # create the adjacency list representation of the graph graph = defaultdict ( list ) for u , v in edges : graph [ u ] . append ( v ) # create a list to store the lengths of the longest paths to each node dp = [ 0 ] * n # perform a DFS traversal of each node in the graph for node in range ( n ) : for u in graph [ node ] : dp [ u ] = max ( dp [ u ] , dp [ node ] + 1 ) return max ( dp ) In this solution, we start by constructing an adjacency list representation of the graph. Next, we generate a list `dp`, where `dp[i]` holds the length of the longest path terminating at vertex `i`. During the traversal of the nodes, for each adjacent node `u` of the current node, if `dp[node] + 1` exceeds `dp[u]`, we update `dp[u]`. Ultimately, the maximum value in `dp` is returned, representing the length of the longest path in the directed acyclic graph.