Prepin
Log in

For a Directed Acyclic Graph (DAG), create a function that calculates and returns the length of the longest path.

Approach / Explanation

This question involves a Directed Acyclic Graph (DAG), which is a directed graph that does not contain any directed cycles. This means you cannot start at a node and follow the directed edges to return to that same node. A path in a graph is defined as a sequence of vertices in which each pair of adjacent vertices is linked by an edge. The path's length is determined by the number of edges connecting the starting vertex to the ending vertex. Your task is to assume the input represents a DAG and create a function that returns the length of the longest path within the graph. The length of a path is measured by the number of edges it contains. When analyzing this problem, it's important to keep these key points in mind: Comprehension of the graph and its characteristics, particularly those of a Directed Acyclic Graph (DAG). Familiarity with the longest path problem within graphs. Knowledge of depth-first search (DFS) or dynamic programming, as these are common approaches to solving graph-related issues.

Suggested Answer

A potential approach to address this issue is by implementing a depth-first search (DFS) algorithm. The concept involves initiating DFS from each unvisited node, which will yield the longest path in the directed acyclic graph (DAG). Here is the Python implementation for this problem: def longestPath ( graph ) : def dfs ( node ) : # If the longest path from node i has already been calculated, # simply return the computed result if node in memo : return memo [ node ] # If it hasn't been calculated yet, we will determine it by finding the maximum of all longest # paths through its neighbors, adding one for the edge to the neighbor. longest = 0 for neighbor in graph [ node ] : longest = max ( longest , 1 + dfs ( neighbor ) ) # Store and return the calculated result memo [ node ] = longest return longest # Create a memoization table memo = { } # Calculate and return the longest path return max ( dfs ( node ) for node in graph ) In this implementation, we utilize a memoization table to prevent recalculating the longest path for any given node.

Alternative Answer

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.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.